def new_container(): try: service = request.form['service'] except KeyError: return Response("service field should be providen", status=422) try: image = request.form['image'] except KeyError: return Response("image field should be providen", status=422) try: port = int(request.form['port']) return json.dumps(Container.create(service=service, port=port, image=image)) except KeyError: return json.dumps(Container.create(service=service, image=image), cls=ContainerJSONEncoder)
def run(self): docker_image = Container.get_docker_image(self.testcase) if dt_cfg.dovetail_config['offline']: exist = Container.get_image_id(docker_image) if not exist: self.logger.error( "{} image doesn't exist, can't run offline.".format( self.testcase.validate_type())) return else: if not Container.pull_image(docker_image): self.logger.error("Failed to pull the image.") return container_id = Container.create(self.testcase.validate_type(), self.testcase.name(), docker_image) if not container_id: self.logger.error('Failed to create container.') return self.logger.debug('container id: {}'.format(container_id)) dest_path = self.testcase.pre_copy_path("dest_path") src_file_name = self.testcase.pre_copy_path("src_file") exist_file_name = self.testcase.pre_copy_path("exist_src_file") if src_file_name or exist_file_name: if not self.pre_copy(container_id, dest_path, src_file_name, exist_file_name): return cmds = self.testcase.pre_condition() if cmds: for cmd in cmds: ret, msg = Container.exec_cmd(container_id, cmd) if ret != 0: self.logger.error("Failed to exec all pre_condition cmds.") break if not self.testcase.prepare_cmd(self.type): self.logger.error('Failed to prepare test case: {}'.format( self.testcase.name())) else: for cmd in self.testcase.cmds: ret, msg = Container.exec_cmd(container_id, cmd) if ret != 0: self.logger.error( 'Failed to exec {}, ret: {}, msg: {}'.format( cmd, ret, msg)) break cmds = self.testcase.post_condition() if cmds: for cmd in cmds: ret, msg = Container.exec_cmd(container_id, cmd) self.testcase.cleaned(True) if not dt_cfg.dovetail_config['noclean']: Container.clean(container_id, self.type)
def run(self): docker_image = Container.get_docker_image(self.testcase) if dt_cfg.dovetail_config['offline']: exist = Container.get_image_id(docker_image) if not exist: self.logger.error( "{} image doesn't exist, can't run offline.".format( self.testcase.validate_type())) return else: if not Container.pull_image(docker_image): self.logger.error("Failed to pull the image.") return # for sdnvpn, there is a need to download needed images to config_dir # in dovetail_config.yml first. if 'sdnvpn' in str(self.testcase.name()): img_name = dt_cfg.dovetail_config['sdnvpn_image'] img_file = os.path.join(dt_cfg.dovetail_config['images_dir'], img_name) if not os.path.isfile(img_file): self.logger.error('Image {} not found.'.format(img_name)) return container_id = Container.create(self.testcase.validate_type(), self.testcase.name(), docker_image) if not container_id: self.logger.error('Failed to create container.') return self.logger.debug('container id: {}'.format(container_id)) dest_path = self.testcase.pre_copy_path("dest_path") src_file_name = self.testcase.pre_copy_path("src_file") exist_file_name = self.testcase.pre_copy_path("exist_src_file") if src_file_name or exist_file_name: if not self.pre_copy(container_id, dest_path, src_file_name, exist_file_name): return cmds = self.testcase.pre_condition() if cmds: for cmd in cmds: ret, msg = Container.exec_cmd(container_id, cmd) if ret != 0: self.logger.error("Failed to exec all pre_condition cmds.") break if not self.testcase.prepare_cmd(self.type): self.logger.error('Failed to prepare test case: {}'.format( self.testcase.name())) else: for cmd in self.testcase.cmds: ret, msg = Container.exec_cmd(container_id, cmd) if ret != 0: self.logger.error( 'Failed to exec {}, ret: {}, msg: {}'.format( cmd, ret, msg)) break cmds = self.testcase.post_condition() if cmds: for cmd in cmds: ret, msg = Container.exec_cmd(container_id, cmd) self.testcase.cleaned(True) if not dt_cfg.dovetail_config['noclean']: Container.clean(container_id, self.type)
import yaml from docker import Client from container import Container client = Client(base_url='unix://var/run/docker.sock') yaml = yaml.load(open(".travis.yml", 'r').read()) _image = "%s:%s" % (yaml["language"], yaml[yaml["language"]][0]) # container = cli.create_container(image=_image, command='ls -la') # cid = container.get('Id') # cli.start(container=cid) container = Container.create(client, image=_image, command='ls -la') print container.id, container.name container.start() for line in container.attach(stream=True): print line container.wait() container.remove()