def stop(cls, container_id): """stop continer""" try: cls.docker_cli.stop(container_id) except dockerpy.errors.APIError as e: logger.error(traceback.format_exc()) raise ContainerActionError(str(e))
def get_pid(cls, container_id): try: pid = cls.docker_cli.inspect_container(container_id)['State']['Pid'] except dockerpy.errors.APIError as e: logger.error(traceback.format_exc()) raise ContainerActionError(str(e)) return pid
def remove(cls, container_id): """remove eths and continer""" try: cls.docker_cli.stop(container_id) cls.docker_cli.remove_container(container_id) except dockerpy.errors.APIError as e: logger.error(traceback.format_exc()) raise ContainerActionError(str(e))
def create(cls, image_name, run_cmd, container_name=None): """create continer""" if not container_name: container_name = "dockerfly_%s_%s" % (image_name.replace(':','_').replace('/','_'), datetime.fromtimestamp(int(time.time())).strftime('%Y%m%d%H%M%S')) try: container = cls.docker_cli.create_container(image=image_name, command=run_cmd, name=container_name) except dockerpy.errors.APIError as e: logger.error(traceback.format_exc()) raise ContainerActionError(str(e)) return container.get('Id')
def start(cls, container_id, veths, gateway): """start eths and continer""" try: cls.docker_cli.start( container=container_id, #extra_hosts={'ldap.xxx.com.cn':'172.16.11.3'}, privileged=True) except dockerpy.errors.APIError as e: logger.error(traceback.format_exc()) raise ContainerActionError(str(e)) for index, (veth, link_to, ip_netmask) in enumerate(veths): macvlan_eth = MacvlanEth(veth, ip_netmask, link_to).create() if index == 0: macvlan_eth.attach_to_container(container_id, is_route=True, gateway=gateway) else: macvlan_eth.attach_to_container(container_id)
def run(cls, image_name, run_cmd, veths, gateway): """create basic container, then running Args: image_name: docker image name veths: virtual eths, [('em0v0', 'em0', '192.168.159.10/24'), ('em1v1', 'em1', '192.168.159.11/24')... )], !!!notify!!!: the first eth will be assigned to the default gateway for container Return: container_id """ try: container_id = cls.create(image_name, run_cmd) cls.start(container_id, veths, gateway) except dockerpy.errors.APIError as e: logger.error(traceback.format_exc()) raise ContainerActionError(str(e)) return container_id