240 lines
9.9 KiB
Python
240 lines
9.9 KiB
Python
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from rally.task import context
|
|
from rally.common import logging
|
|
from rally.common import utils
|
|
from rally import consts
|
|
from rally_openstack.common import osclients
|
|
from rally_openstack.common.wrappers import network as network_wrapper
|
|
|
|
import os
|
|
from jinja2 import Environment
|
|
from jinja2 import FileSystemLoader
|
|
import paramiko
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
@context.configure(name="crucible_provider_networks", order=1000)
|
|
class CreateExternalNetworksContext(context.Context):
|
|
"""This plugin creates provider networks with specified option."""
|
|
|
|
CONFIG_SCHEMA = {
|
|
"type": "object",
|
|
"$schema": consts.JSON_SCHEMA,
|
|
"additionalProperties": False,
|
|
"properties": {
|
|
"cidr_prefix": {
|
|
"type": "string",
|
|
},
|
|
"num_provider_networks": {
|
|
"type": "integer",
|
|
"minimum": 0
|
|
},
|
|
"provider_phys_net": {
|
|
"type": "string"
|
|
},
|
|
"hostname_or_ip": {
|
|
"type": "string"
|
|
},
|
|
"user": {
|
|
"type": "string"
|
|
},
|
|
"password": {
|
|
"type": "string"
|
|
},
|
|
"ext_iface": {
|
|
"type": "string"
|
|
}
|
|
}
|
|
}
|
|
|
|
def _create_subnet(self, tenant_id, network_id, network_number):
|
|
"""Create subnet for provider network
|
|
|
|
:param tenant_id: ID of tenant
|
|
:param network_id: ID of provider network
|
|
:param network_number: int, number for CIDR of subnet
|
|
:returns: subnet object
|
|
"""
|
|
|
|
subnet_args = {
|
|
"subnet": {
|
|
"tenant_id": tenant_id,
|
|
"network_id": network_id,
|
|
"name": self.net_wrapper.owner.generate_random_name(),
|
|
"ip_version": 4,
|
|
"cidr": "{}.{}.0/24".format(self.cidr_prefix, network_number),
|
|
"enable_dhcp": True,
|
|
"gateway_ip": "{}.{}.1".format(self.cidr_prefix, network_number),
|
|
"allocation_pools": [{"start": "{}.{}.2".format(self.cidr_prefix, network_number),
|
|
"end": "{}.{}.254".format(
|
|
self.cidr_prefix, network_number)}]
|
|
}
|
|
}
|
|
return self.net_wrapper.client.create_subnet(subnet_args)["subnet"]
|
|
|
|
def get_neutron_client(self):
|
|
return osclients.Clients(self.context["admin"]["credential"]).neutron()
|
|
|
|
def run_ssh_script(self, host, username, password, script_path , action):
|
|
try:
|
|
with paramiko.SSHClient() as ssh:
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
ssh.connect(host, username=username, password=password)
|
|
_, stdout, stderr = ssh.exec_command(f"bash {script_path} {action}")
|
|
|
|
# Check the exit status of the command
|
|
if stdout.channel.recv_exit_status() == 0:
|
|
LOG.info(f"Script on {host} executed successfully.")
|
|
return True
|
|
else:
|
|
raise Exception(
|
|
f"Script execution failed on {host}. Error: {stderr.read().decode()}"
|
|
)
|
|
except Exception as e:
|
|
raise Exception(f"Error in run_ssh_script: {e}")
|
|
|
|
def render_template(self, env, template, ext_iface, num_vlans, cidr_prefix,
|
|
output_file_path):
|
|
rendered_template = template.render(
|
|
ext_iface=ext_iface,
|
|
num_vlans=num_vlans,
|
|
cidr_prefix=cidr_prefix
|
|
)
|
|
|
|
with open(output_file_path, 'w') as file:
|
|
file.write(rendered_template)
|
|
|
|
def setup(self):
|
|
"""This method is called before the task starts."""
|
|
self.net_wrapper = network_wrapper.wrap(
|
|
osclients.Clients(self.context["admin"]["credential"]),
|
|
self,
|
|
config=self.config,
|
|
)
|
|
self.context["provider_networks"] = []
|
|
self.context["provider_subnets"] = {}
|
|
self.num_provider_networks = self.config.get("num_provider_networks", 16)
|
|
self.cidr_prefix = self.config.get("cidr_prefix", "192.18")
|
|
self.hostname_or_ip = self.config.get("hostname_or_ip", "hostname.example.com")
|
|
self.user = self.config.get("user", "root")
|
|
self.password = self.config.get("password", "passwd")
|
|
self.ext_iface = self.config.get("ext_iface", "ens2f1np1")
|
|
num_provider_networks_created = 0
|
|
|
|
while num_provider_networks_created < self.num_provider_networks:
|
|
has_error_occured = False
|
|
for user, tenant_id in utils.iterate_per_tenants(
|
|
self.context.get("users", [])
|
|
):
|
|
try:
|
|
kwargs = {
|
|
"network_create_args": {
|
|
"provider:network_type": "vlan",
|
|
"provider:physical_network": self.config.get("provider_phys_net",
|
|
"datacentre"),
|
|
"provider:segmentation_id": num_provider_networks_created + 1,
|
|
"router:external": False,
|
|
"port_security_enabled": False
|
|
}
|
|
}
|
|
self.context["provider_networks"].append(
|
|
self.net_wrapper.create_network(tenant_id, **kwargs)
|
|
)
|
|
LOG.debug(
|
|
"Provider network with id '%s' created as part of context"
|
|
% self.context["provider_networks"][-1]["id"]
|
|
)
|
|
# update network name
|
|
updated_name = 'provider_' + str(num_provider_networks_created + 1)
|
|
cmd = f"source /home/stack/overcloudrc && openstack network set --name " \
|
|
f"{updated_name} {self.context['provider_networks'][-1]['id']}"
|
|
os.system(cmd)
|
|
self.context["provider_networks"][-1]["name"] = updated_name
|
|
|
|
num_provider_networks_created += 1
|
|
except Exception as e:
|
|
msg = "Can't create provider network {} as part of context: {}".format(
|
|
num_provider_networks_created, e
|
|
)
|
|
LOG.exception(msg)
|
|
has_error_occured = True
|
|
break
|
|
|
|
try:
|
|
subnet = self._create_subnet(tenant_id,
|
|
self.context["provider_networks"][-1]["id"],
|
|
num_provider_networks_created)
|
|
self.context["provider_subnets"][
|
|
self.context["provider_networks"][-1]["id"]] = subnet
|
|
LOG.debug(
|
|
"provider subnet with id '%s' created as part of context"
|
|
% subnet["id"]
|
|
)
|
|
except Exception as e:
|
|
msg = "Can't create provider subnet {} as part of context: {}".format(
|
|
num_provider_networks_created, e
|
|
)
|
|
LOG.exception(msg)
|
|
has_error_occured = True
|
|
break
|
|
|
|
if has_error_occured:
|
|
break
|
|
|
|
# prepare the script
|
|
env = Environment(loader=FileSystemLoader(os.getcwd()))
|
|
template_path = './rally/rally-plugins/workloads/crucible/templates/vlans.sh.j2'
|
|
template = env.get_template(template_path)
|
|
self.output_file_path = './rally/rally-plugins/workloads/crucible/browbeat_vlans.sh'
|
|
self.render_template(env, template, self.ext_iface, self.num_provider_networks,
|
|
self.cidr_prefix, self.output_file_path)
|
|
|
|
# copy over scp
|
|
scp_command = (
|
|
f"sshpass -p '{self.password}' scp {self.output_file_path} "
|
|
f"{self.user}@{self.hostname_or_ip}:/root/"
|
|
)
|
|
os.system(scp_command)
|
|
# create vlan interfaces
|
|
self.run_ssh_script(self.hostname_or_ip, self.user, self.password,
|
|
"/root/browbeat_vlans.sh", "create")
|
|
|
|
def cleanup(self):
|
|
"""This method is called after the task finishes."""
|
|
for i in range(self.num_provider_networks):
|
|
try:
|
|
provider_net = self.context["provider_networks"][i]
|
|
provider_net_id = provider_net["id"]
|
|
provider_subnet = self.context["provider_subnets"][provider_net_id]
|
|
provider_subnet_id = provider_subnet["id"]
|
|
self.net_wrapper._delete_subnet(provider_subnet_id)
|
|
LOG.debug(
|
|
"Provider subnet with id '%s' deleted from context"
|
|
% provider_subnet_id
|
|
)
|
|
self.net_wrapper.delete_network(provider_net)
|
|
LOG.debug(
|
|
"Provider network with id '%s' deleted from context"
|
|
% provider_net_id
|
|
)
|
|
except Exception as e:
|
|
msg = "Can't delete provider network {} from context: {}".format(
|
|
provider_net_id, e
|
|
)
|
|
LOG.warning(msg)
|
|
|
|
# delete vlan interfaces
|
|
self.run_ssh_script(self.hostname_or_ip, self.user, self.password,
|
|
"/root/browbeat_vlans.sh", "delete")
|