X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FContainerUtils.py;h=50868cb8f2c3d8305ed264b1f6a06323cd887038;hb=d5e00a369ffef5edf929ef5b03b7c544021b2e41;hp=9c42a3c99cac1a5928d22b5ff00b24553414e28b;hpb=26637ca4fdb026cea585df4c9e6c0fc2c07492ba;p=csit.git diff --git a/resources/libraries/python/ContainerUtils.py b/resources/libraries/python/ContainerUtils.py index 9c42a3c99c..50868cb8f2 100644 --- a/resources/libraries/python/ContainerUtils.py +++ b/resources/libraries/python/ContainerUtils.py @@ -16,11 +16,12 @@ """Library to manipulate Containers.""" +from string import Template from collections import OrderedDict, Counter from resources.libraries.python.ssh import SSH -from resources.libraries.python.constants import Constants -from resources.libraries.python.topology import Topology +from resources.libraries.python.Constants import Constants +from resources.libraries.python.topology import Topology, SocketType from resources.libraries.python.VppConfigGenerator import VppConfigGenerator @@ -211,7 +212,7 @@ class ContainerManager(object): """ self.engine.create_vpp_startup_config() self.engine.create_vpp_exec_config( - 'memif_create_chain_l2xc.vat', + 'memif_create_chain_l2xc.exec', mid1=kwargs['mid1'], mid2=kwargs['mid2'], sid1=kwargs['sid1'], sid2=kwargs['sid2'], socket1='{guest_dir}/memif-{c.name}-{sid1}'. @@ -237,7 +238,7 @@ class ContainerManager(object): self.engine.container.node, kwargs['dut2_if']) self.engine.create_vpp_startup_config_dpdk_dev(if_pci) self.engine.create_vpp_exec_config( - 'memif_create_cross_horizon.vat', + 'memif_create_cross_horizon.exec', mid1=kwargs['mid1'], sid1=kwargs['sid1'], if_name=if_name, socket1='{guest_dir}/memif-{c.name}-{sid1}'. format(c=self.engine.container, **kwargs)) @@ -250,7 +251,7 @@ class ContainerManager(object): """ self.engine.create_vpp_startup_config_func_dev() self.engine.create_vpp_exec_config( - 'memif_create_chain_functional.vat', + 'memif_create_chain_functional.exec', mid1=kwargs['mid1'], mid2=kwargs['mid2'], sid1=kwargs['sid1'], sid2=kwargs['sid2'], socket1='{guest_dir}/memif-{c.name}-{sid1}'. @@ -274,7 +275,7 @@ class ContainerManager(object): if (kwargs['mid2'] - 1) % kwargs['nodes'] + 1 == kwargs['nodes'] \ else '52:54:00:00:{0:02X}:01'.format(kwargs['mid2'] + 1) self.engine.create_vpp_exec_config( - 'memif_create_chain_ip4.vat', + 'memif_create_chain_ip4.exec', mid1=kwargs['mid1'], mid2=kwargs['mid2'], sid1=kwargs['sid1'], sid2=kwargs['sid2'], socket1='{guest_dir}/memif-{c.name}-{sid1}'. @@ -319,7 +320,7 @@ class ContainerManager(object): format(c=self.engine.container, **kwargs) self.engine.create_vpp_exec_config( - 'memif_create_pipeline_ip4.vat', + 'memif_create_pipeline_ip4.exec', mid1=kwargs['mid1'], mid2=kwargs['mid2'], sid1=kwargs['sid1'], sid2=kwargs['sid2'], socket1=socket1, socket2=socket2, role1=role1, role2=role2, @@ -397,6 +398,7 @@ class ContainerEngine(object): self.execute('sleep 3; apt-get update') self.execute('apt-get install -y supervisor') self.execute('echo "{config}" > {config_file} && ' + 'unlink /tmp/supervisor.sock && ' 'supervisord -c {config_file}'. format( config='[unix_http_server]\n' @@ -410,9 +412,9 @@ class ContainerEngine(object): 'pidfile = /tmp/supervisord.pid\n' 'identifier = supervisor\n' 'directory = /tmp\n' - 'logfile=/tmp/supervisord.log\n' - 'loglevel=debug\n' - 'nodaemon=false\n\n', + 'logfile = /tmp/supervisord.log\n' + 'loglevel = debug\n' + 'nodaemon = false\n\n', config_file=SUPERVISOR_CONF)) def start_vpp(self): @@ -420,15 +422,31 @@ class ContainerEngine(object): self.execute('echo "{config}" >> {config_file}'. format( config='[program:vpp]\n' - 'command=/usr/bin/vpp -c /etc/vpp/startup.conf\n' - 'autostart=false\n' - 'autorestart=false\n' - 'redirect_stderr=true\n' - 'priority=1', + 'command = /usr/bin/vpp -c /etc/vpp/startup.conf\n' + 'autostart = false\n' + 'autorestart = false\n' + 'redirect_stderr = true\n' + 'priority = 1', config_file=SUPERVISOR_CONF)) self.execute('supervisorctl reload') self.execute('supervisorctl start vpp') + from robot.libraries.BuiltIn import BuiltIn + topo_instance = BuiltIn().get_library_instance( + 'resources.libraries.python.topology.Topology') + topo_instance.add_new_socket( + self.container.node, + SocketType.PAPI, + self.container.name, + '{root}/tmp/vpp_sockets/{name}/api.sock'. + format(root=self.container.root, name=self.container.name)) + topo_instance.add_new_socket( + self.container.node, + SocketType.STATS, + self.container.name, + '{root}/tmp/vpp_sockets/{name}/stats.sock'. + format(root=self.container.root, name=self.container.name)) + def restart_vpp(self): """Restart VPP service inside a container.""" self.execute('supervisorctl restart vpp') @@ -448,6 +466,8 @@ class ContainerEngine(object): vpp_config.add_unix_cli_listen() vpp_config.add_unix_nodaemon() vpp_config.add_unix_exec('/tmp/running.exec') + vpp_config.add_socksvr(socket=Constants.SOCKSVR_PATH) + vpp_config.add_statseg_per_node_counters(value='on') # We will pop the first core from the list to be a main core vpp_config.add_cpu_main_core(str(cpuset_cpus.pop(0))) # If more cores in the list, the rest will be used as workers. @@ -497,6 +517,8 @@ class ContainerEngine(object): vpp_config.add_unix_cli_listen() vpp_config.add_unix_nodaemon() vpp_config.add_unix_exec('/tmp/running.exec') + vpp_config.add_socksvr(socket=Constants.SOCKSVR_PATH) + vpp_config.add_statseg_per_node_counters(value='on') vpp_config.add_plugin('disable', 'dpdk_plugin.so') # Apply configuration @@ -504,23 +526,23 @@ class ContainerEngine(object): self.execute('echo "{config}" | tee /etc/vpp/startup.conf' .format(config=vpp_config.get_config_str())) - def create_vpp_exec_config(self, vat_template_file, **kwargs): + def create_vpp_exec_config(self, template_file, **kwargs): """Create VPP exec configuration on container. - :param vat_template_file: File name of a VAT template script. - :param kwargs: Parameters for VAT script. - :type vat_template_file: str + :param template_file: File name of a template script. + :param kwargs: Parameters for script. + :type template_file: str :type kwargs: dict """ - vat_file_path = '{p}/{f}'.format(p=Constants.RESOURCES_TPL_VAT, - f=vat_template_file) + running = '/tmp/running.exec' + + template = '{res}/{tpl}'.format( + res=Constants.RESOURCES_TPL_CONTAINER, tpl=template_file) - with open(vat_file_path, 'r') as template_file: - cmd_template = template_file.readlines() - for line_tmpl in cmd_template: - vat_cmd = line_tmpl.format(**kwargs) - self.execute('echo "{c}" >> /tmp/running.exec' - .format(c=vat_cmd.replace('\n', ''))) + with open(template, 'r') as src_file: + src = Template(src_file.read()) + self.execute('echo "{out}" > {running}'.format( + out=src.safe_substitute(**kwargs), running=running)) def is_container_running(self): """Check if container is running.""" @@ -590,8 +612,12 @@ class LXC(ContainerEngine): else: return + target_arch = 'arm64' \ + if Topology.get_node_arch(self.container.node) == 'aarch64' \ + else 'amd64' + image = self.container.image if self.container.image else\ - "-d ubuntu -r bionic -a amd64" + "-d ubuntu -r bionic -a {arch}".format(arch=target_arch) cmd = 'lxc-create -t download --name {c.name} -- {image} '\ '--no-validate'.format(c=self.container, image=image) @@ -608,16 +634,24 @@ class LXC(ContainerEngine): :raises RuntimeError: If creating the container fails. """ if self.container.mnt: + # LXC fix for tmpfs + # https://github.com/lxc/lxc/issues/434 + ret, _, _ = self.container.ssh.exec_command_sudo( + "sh -c 'echo \"{e}\" >> /var/lib/lxc/{c.name}/config'". + format(e="lxc.mount.entry = tmpfs run tmpfs defaults", + c=self.container)) + if int(ret) != 0: + raise RuntimeError('Failed to write {c.name} config.'. + format(c=self.container)) + for mount in self.container.mnt: host_dir, guest_dir = mount.split(':') options = 'bind,create=dir' \ if guest_dir.endswith('/') else 'bind,create=file' - entry = 'lxc.mount.entry = {host_dir} '\ - '/var/lib/lxc/{c.name}/rootfs{guest_dir} none ' \ - '{options} 0 0'.format(c=self.container, - host_dir=host_dir, - guest_dir=guest_dir, - options=options) + entry = 'lxc.mount.entry = {host_dir} {guest_dir} none ' \ + '{options} 0 0'.format( + host_dir=host_dir, guest_dir=guest_dir[1:], + options=options) ret, _, _ = self.container.ssh.exec_command_sudo( "sh -c 'echo \"{e}\" >> /var/lib/lxc/{c.name}/config'". format(e=entry, c=self.container)) @@ -630,8 +664,7 @@ class LXC(ContainerEngine): if self.container.cpuset_cpus else '' ret, _, _ = self.container.ssh.exec_command_sudo( - 'lxc-start --name {c.name} --daemon'. - format(c=self.container)) + 'lxc-start --name {c.name} --daemon'.format(c=self.container)) if int(ret) != 0: raise RuntimeError('Failed to start container {c.name}.'. format(c=self.container)) @@ -783,7 +816,10 @@ class Docker(ContainerEngine): return if not self.container.image: - setattr(self.container, 'image', 'snergster/csit-sut:latest') + img = Constants.DOCKER_SUT_IMAGE_UBUNTU_ARM \ + if Topology.get_node_arch(self.container.node) == 'aarch64' \ + else Constants.DOCKER_SUT_IMAGE_UBUNTU + setattr(self.container, 'image', img) cmd = 'docker pull {image}'.format(image=self.container.image) @@ -791,6 +827,7 @@ class Docker(ContainerEngine): if int(ret) != 0: raise RuntimeError('Failed to create container {c.name}.' .format(c=self.container)) + if self.container.cpuset_cpus: self._configure_cgroup('docker') @@ -846,7 +883,7 @@ class Docker(ContainerEngine): :param command: Command to run inside container. :type command: str - :raises RuntimeError: If runnig the command in a container failed. + :raises RuntimeError: If running the command in a container failed. """ cmd = "docker exec --interactive {c.name} /bin/sh -c '{command}; "\ "exit $?'".format(c=self.container, command=command)