def delete(self, container_id, sync=True, local=False, host=None): if local: exe_cmd('{docker} stop {name} && {docker} rm {name}'.format( docker=local_docker_cmd(host), name=container_id, )) else: container = self.get(container_id) if container.status == states['RUNNING']: self.stop(container_id) attempts = ATTEMPTS while 1: if attempts <= 0: self.operator.delete_container(container_id, sync, force=False) break container = self.get(container_id) if container.status != states['RUNNING']: self.operator.delete_container(container_id, sync, force=False) break attempts -= 1 time.sleep(1) else: self.operator.delete_container(container_id, sync, force=False)
def cp(self, container_id, src_path, dest_path, local=False, host=None): if local: exe_cmd("{docker} cp {src} {name}:{dest}".format( docker=local_docker_cmd(host), name=container_id, src=src_path, dest=dest_path)) else: pass
def local_create(self, **kwargs): container_name = str(uuid.uuid4()) image = kwargs['image'] ports = kwargs.get('ports', []) port_info = kwargs.get('port_info') or { port: get_new_valid_port() for port in ports } port_str = '' for port, proxy_port in port_info.items(): port_str += ' -p {proxy_port}:{port} '.format( port=port, proxy_port=proxy_port) host = kwargs.get('host') or random_docker_host() try: host_ip = socket.gethostbyname(host) except: host_ip = host cmd = '{docker} run --name {name} -itd {port_str} {image}'.format( docker=local_docker_cmd(host), name=container_name, port_str=port_str, image=image, ) logger.info('local create container cmd: %s', cmd) try: exe_cmd(cmd, raise_exception=True) except Exception as e: time.sleep(2) raise e server = type('Container', (object, ), {})() server.id = container_name server.port_info = port_info server.id = container_name server.host_ip_address = host_ip return server
def unpause(self, container_id, local=False, host=None): if local: exe_cmd('{docker} unpause {name}'.format( docker=local_docker_cmd(host), name=container_id)) else: self.operator.unpause_container(container_id)
def execute_command(self, container_id, cmd, local=False, host=None): if local: exe_cmd("{docker} exec {name} {cmd}".format( docker=local_docker_cmd(host), name=container_id, cmd=cmd)) else: self.operator.execute_container_cmd(container_id, cmd)
def restart(self, container_id, local=False, host=None): if local: exe_cmd('{docker} restart {name}'.format( docker=local_docker_cmd(host), name=container_id)) else: self.operator.restart_container(container_id)
def stop(self, container_id, local=False, host=None): if local: exe_cmd('{docker} stop {name}'.format( docker=local_docker_cmd(host), name=container_id)) else: self.operator.stop_container(container_id)