X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=resources%2Flibraries%2Fpython%2FContainerUtils.py;h=4d5f8ee4d6a5f9294789195c7297d938c2d55969;hb=fd6fedc983a7d1796eb2531782be8a37b6d0921d;hp=da3d7057dcab36fc95335e87fb4c43dd3ba6322a;hpb=20ebb7aa4f5ab46206b472fc52b24e354bbc8dc4;p=csit.git diff --git a/resources/libraries/python/ContainerUtils.py b/resources/libraries/python/ContainerUtils.py index da3d7057dc..4d5f8ee4d6 100644 --- a/resources/libraries/python/ContainerUtils.py +++ b/resources/libraries/python/ContainerUtils.py @@ -1,4 +1,4 @@ -# Copyright (c) 2017 Cisco and/or its affiliates. +# Copyright (c) 2018 Cisco and/or its affiliates. # 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: @@ -42,8 +42,8 @@ class ContainerManager(object): try: self.engine = globals()[engine]() except KeyError: - raise NotImplementedError('{e} is not implemented.' - .format(e=engine)) + raise NotImplementedError('{engine} is not implemented.'. + format(engine=engine)) self.containers = OrderedDict() def get_container_by_name(self, name): @@ -58,8 +58,8 @@ class ContainerManager(object): try: return self.containers[name] except KeyError: - raise RuntimeError('Failed to get container with name: {n}' - .format(n=name)) + raise RuntimeError('Failed to get container with name: {name}'. + format(name=name)) def construct_container(self, **kwargs): """Construct container object on node with specified parameters. @@ -75,7 +75,7 @@ class ContainerManager(object): # Set additional environmental variables setattr(self.engine.container, 'env', - 'MICROSERVICE_LABEL={n}'.format(n=kwargs['name'])) + 'MICROSERVICE_LABEL={label}'.format(label=kwargs['name'])) # Set cpuset.cpus cgroup skip_cnt = kwargs['cpu_skip'] @@ -156,6 +156,12 @@ class ContainerManager(object): self.engine.install_vpp() self.engine.restart_vpp() + def restart_vpp_in_all_containers(self): + """Restart VPP on all containers.""" + for container in self.containers: + self.engine.container = self.containers[container] + self.engine.restart_vpp() + def configure_vpp_in_all_containers(self, vat_template_file): """Configure VPP in all containers. @@ -253,9 +259,9 @@ class ContainerEngine(object): self.execute('sleep 3') self.execute('apt-get update') self.execute('apt-get install -y supervisor') - self.execute('echo "{0}" > {1}' - .format( - '[unix_http_server]\n' + self.execute('echo "{config}" > {config_file}'. + format( + config='[unix_http_server]\n' 'file = /tmp/supervisor.sock\n\n' '[rpcinterface:supervisor]\n' 'supervisor.rpcinterface_factory = ' @@ -269,42 +275,40 @@ class ContainerEngine(object): 'logfile=/tmp/supervisord.log\n' 'loglevel=debug\n' 'nodaemon=false\n\n', - SUPERVISOR_CONF)) - self.execute('supervisord -c {0}'.format(SUPERVISOR_CONF)) + config_file=SUPERVISOR_CONF)) + self.execute('supervisord -c {config_file}'. + format(config_file=SUPERVISOR_CONF)) - def install_vpp(self, install_dkms=False): - """Install VPP inside a container. - - :param install_dkms: If install dkms package. This will impact install - time. Dkms is required for installation of vpp-dpdk-dkms. Default is - false. - :type install_dkms: bool - """ + def install_vpp(self): + """Install VPP inside a container.""" self.execute('ln -s /dev/null /etc/sysctl.d/80-vpp.conf') self.execute('apt-get update') - if install_dkms: - self.execute('apt-get install -y dkms && ' - 'dpkg -i --force-all {0}/install_dir/*.deb' - .format(self.container.guest_dir)) + if self.container.install_dkms: + self.execute( + 'apt-get install -y dkms && ' + 'dpkg -i --force-all {guest_dir}/install_dir/*.deb'. + format(guest_dir=self.container.mnt[0].split(':')[1])) else: - self.execute('for i in $(ls -I \"*dkms*\" {0}/install_dir/); ' - 'do dpkg -i --force-all {0}/install_dir/$i; done' - .format(self.container.guest_dir)) + self.execute( + 'for i in $(ls -I \"*dkms*\" {guest_dir}/install_dir/); do ' + 'dpkg -i --force-all {guest_dir}/install_dir/$i; done'. + format(guest_dir=self.container.mnt[0].split(':')[1])) self.execute('apt-get -f install -y') self.execute('apt-get install -y ca-certificates') - self.execute('echo "{0}" >> {1}' - .format( - '[program:vpp]\n' + self.execute('echo "{config}" >> {config_file}'. + format( + config='[program:vpp]\n' 'command=/usr/bin/vpp -c /etc/vpp/startup.conf\n' 'autorestart=false\n' 'redirect_stderr=true\n' 'priority=1', - SUPERVISOR_CONF)) + config_file=SUPERVISOR_CONF)) self.execute('supervisorctl reload') def restart_vpp(self): """Restart VPP service inside a container.""" self.execute('supervisorctl restart vpp') + self.execute('cat /tmp/supervisord.log') def create_vpp_startup_config(self, config_filename='/etc/vpp/startup.conf'): @@ -327,8 +331,7 @@ class ContainerEngine(object): if cpuset_cpus: corelist_workers = ','.join(str(cpu) for cpu in cpuset_cpus) vpp_config.add_cpu_corelist_workers(corelist_workers) - vpp_config.add_plugin('disable', 'default') - vpp_config.add_plugin('enable', 'memif_plugin.so') + vpp_config.add_plugin('disable', 'dpdk_plugin.so') self.execute('mkdir -p /etc/vpp/') self.execute('echo "{c}" | tee {f}' @@ -364,12 +367,12 @@ class ContainerEngine(object): def _configure_cgroup(self, name): """Configure the control group associated with a container. - By default the cpuset cgroup is using exclusive CPU/MEM. When Docker + By default the cpuset cgroup is using exclusive CPU/MEM. When Docker/LXC container is initialized a new cgroup /docker or /lxc is created under cpuset parent tree. This newly created cgroup is inheriting parent setting for cpu/mem exclusive parameter and thus cannot be overriden - within /docker or /lxc cgroup. This patch is supposed to set cpu/mem - exclusive parameter for both parent and subgroup. + within /docker or /lxc cgroup. This function is supposed to set cgroups + to allow coexistence of both engines. :param name: Name of cgroup. :type name: str @@ -412,10 +415,10 @@ class LXC(ContainerEngine): """Acquire a privileged system object where configuration is stored. :param force: If a container exists, destroy it and create a new - container. + container. :type force: bool :raises RuntimeError: If creating the container or writing the container - config fails. + config fails. """ if self.is_container_present(): if force: @@ -433,16 +436,6 @@ class LXC(ContainerEngine): if int(ret) != 0: raise RuntimeError('Failed to create container.') - if self.container.host_dir and self.container.guest_dir: - entry = 'lxc.mount.entry = '\ - '{c.host_dir} /var/lib/lxc/{c.name}/rootfs{c.guest_dir} ' \ - 'none bind,create=dir 0 0'.format(c=self.container) - ret, _, _ = self.container.ssh.exec_command_sudo( - "sh -c 'echo \"{e}\" >> /var/lib/lxc/{c.name}/config'" - .format(e=entry, c=self.container)) - if int(ret) != 0: - raise RuntimeError('Failed to write {c.name} config.' - .format(c=self.container)) self._configure_cgroup('lxc') def create(self): @@ -450,27 +443,42 @@ class LXC(ContainerEngine): :raises RuntimeError: If creating the container fails. """ + if self.container.mnt: + for mount in self.container.mnt: + host_dir, guest_dir = mount.split(':') + entry = 'lxc.mount.entry = {host_dir} '\ + '/var/lib/lxc/{c.name}/rootfs{guest_dir} none ' \ + 'bind,create=dir 0 0'.format(c=self.container, + host_dir=host_dir, + guest_dir=guest_dir) + ret, _, _ = self.container.ssh.exec_command_sudo( + "sh -c 'echo \"{e}\" >> /var/lib/lxc/{c.name}/config'". + format(e=entry, c=self.container)) + if int(ret) != 0: + raise RuntimeError('Failed to write {c.name} config.' + .format(c=self.container)) + cpuset_cpus = '{0}'.format( ','.join('%s' % cpu for cpu in self.container.cpuset_cpus))\ if self.container.cpuset_cpus else '' - cmd = 'lxc-start --name {c.name} --daemon'.format(c=self.container) - - ret, _, _ = self.container.ssh.exec_command_sudo(cmd) + ret, _, _ = self.container.ssh.exec_command_sudo( + '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)) + raise RuntimeError('Failed to start container {c.name}.'. + format(c=self.container)) self._lxc_wait('RUNNING') # Workaround for LXC to be able to allocate all cpus including isolated. - cmd = 'cgset --copy-from / lxc/' - ret, _, _ = self.container.ssh.exec_command_sudo(cmd) + ret, _, _ = self.container.ssh.exec_command_sudo( + 'cgset --copy-from / lxc/') if int(ret) != 0: raise RuntimeError('Failed to copy cgroup to LXC') - cmd = 'lxc-cgroup --name {c.name} cpuset.cpus {cpus}'\ - .format(c=self.container, cpus=cpuset_cpus) - ret, _, _ = self.container.ssh.exec_command_sudo(cmd) + ret, _, _ = self.container.ssh.exec_command_sudo( + 'lxc-cgroup --name {c.name} cpuset.cpus {cpus}'. + format(c=self.container, cpus=cpuset_cpus)) if int(ret) != 0: raise RuntimeError('Failed to set cpuset.cpus to container ' '{c.name}.'.format(c=self.container)) @@ -489,8 +497,8 @@ class LXC(ContainerEngine): ' '.join('--set-var %s' % env for env in self.container.env))\ if self.container.env else '' - cmd = "lxc-attach {env} --name {c.name} -- /bin/sh -c '{command}'"\ - .format(env=env, c=self.container, command=command) + cmd = "lxc-attach {env} --name {c.name} -- /bin/sh -c '{command}; "\ + "exit $?'".format(env=env, c=self.container, command=command) ret, _, _ = self.container.ssh.exec_command_sudo(cmd, timeout=180) if int(ret) != 0: @@ -628,6 +636,8 @@ class Docker(ContainerEngine): cpuset_mems = '--cpuset-mems={0}'.format(self.container.cpuset_mems)\ if self.container.cpuset_mems is not None else '' + # Temporary workaround - disabling due to bug in memif + cpuset_mems = '' env = '{0}'.format( ' '.join('--env %s' % env for env in self.container.env))\ @@ -640,8 +650,9 @@ class Docker(ContainerEngine): ' '.join('--publish %s' % var for var in self.container.publish))\ if self.container.publish else '' - volume = '--volume {c.host_dir}:{c.guest_dir}'.format(c=self.container)\ - if self.container.host_dir and self.container.guest_dir else '' + volume = '{0}'.format( + ' '.join('--volume %s' % mnt for mnt in self.container.mnt))\ + if self.container.mnt else '' cmd = 'docker run '\ '--privileged --detach --interactive --tty --rm '\ @@ -668,8 +679,8 @@ class Docker(ContainerEngine): :type command: str :raises RuntimeError: If runnig the command in a container failed. """ - cmd = "docker exec --interactive {c.name} /bin/sh -c '{command}'"\ - .format(c=self.container, command=command) + cmd = "docker exec --interactive {c.name} /bin/sh -c '{command}; "\ + "exit $?'".format(c=self.container, command=command) ret, _, _ = self.container.ssh.exec_command_sudo(cmd, timeout=180) if int(ret) != 0: