def parseFile(cls, ymlFile): with open(ymlFile, "r") as ifp: yml = yaml.safe_load(ifp) img_tag = ImageTag.parse(yml["image"]) container = cls(yml["name"], img_tag) container.hostname = yml.get("hostname", None) container.command = yml.get("command", None) if container.command: ## convert all arguments to strings container.command = [ str(a) for a in container.command ] container.env = yml.get("env", None) if container.env is not None: for key in container.env: ## coerce all values to strings container.env[key] = str(container.env[key]) container.volumes = yml.get("volumes", None) if container.volumes is not None: for v in container.volumes: if "ReadWrite" not in container.volumes[v]: container.volumes[v]["ReadWrite"] = True container.ports = yml.get("ports", None) if container.ports is not None: for p in container.ports: if "HostIp" not in container.ports[p]: container.ports[p]["HostIp"] = "0.0.0.0" return container
def parseFile(cls, ymlFile): with open(ymlFile, "r") as ifp: yml = yaml.safe_load(ifp) img_tag = ImageTag.parse(yml["image"]) container = cls(yml["name"], img_tag) container.hostname = yml.get("hostname", None) container.command = yml.get("command", None) if container.command: ## convert all arguments to strings container.command = [str(a) for a in container.command] container.env = yml.get("env", {}) for key in container.env: ## coerce all values to strings container.env[key] = str(container.env[key]) container.volumes = yml.get("volumes", None) if container.volumes is not None: for v in container.volumes: if "ReadWrite" not in container.volumes[v]: container.volumes[v]["ReadWrite"] = True container.ports = yml.get("ports", None) if container.ports is not None: for p in container.ports: if "HostIp" not in container.ports[p]: container.ports[p]["HostIp"] = "0.0.0.0" return container
def getImage(self, name): """ returns an Image for the given name, or None if not found """ retVal = None img_data = self.client.inspect_image(name) if img_data: retVal = Image.fromJson(img_data) retVal.tags = [ name if isinstance(name, ImageTag) else ImageTag.parse(name) ] return retVal
def test_parseFile(self): cdef = ContainerDefinition.parseFile( os.path.join(self.EXAMPLE_DIR, "00-private-registry.yaml")) eq_(cdef.name, "private-registry") eq_(cdef.image_tag, ImageTag("registry", tag="0.8.1")) eq_(cdef.env["SETTINGS_FLAVOR"], "local") eq_(cdef.ports["5000/tcp"], {"HostIp": "0.0.0.0", "HostPort": 11003}) eq_(cdef.volumes["/var/lib/docker/registry"], { "HostPath": "/tmp", "ReadWrite": True })
def test_getImageIdFromRegistry(self): docker = DockerSyncWrapper(docker_host=self.DOCKER_HOST_URL) httpretty.register_uri( httpretty.GET, "http://index.docker.io/v1/repositories/some/repo/tags", body=json.dumps([ { "layer": "84422536", "name": "latest" } ]), content_type="application/json", ) img_id = docker.getImageIdFromRegistry(ImageTag("some/repo", tag="latest")) eq_("84422536", img_id)
def getImages(self): """ returns a dict of image_tag => Image instance """ self.images = {} for img in self.client.images(all=True): tags = [ ImageTag.parse(t) for t in img["RepoTags"] ] image = Image(img["Id"]) image.tags = tags for t in image.tags: if t: self.images[t] = image return self.images
def getImages(self): """ returns a dict of image_tag => Image instance """ self.images = {} for img in self.client.images(all=True): tags = [ImageTag.parse(t) for t in img["RepoTags"]] image = Image(img["Id"]) image.tags = tags for t in image.tags: if t: self.images[t] = image return self.images
def getImage(self, name): """ returns an Image for the given name, or None if not found """ retVal = None img_data = self.client.inspect_image(name) if img_data: img_cfg = img_data.get("config", {}) retVal = Image(img_data["id"]) retVal.tags = [ name if isinstance(name, ImageTag) else ImageTag.parse(name) ] retVal.entrypoint = img_cfg.get("Entrypoint", [None]) retVal.command = img_cfg.get("Cmd", [None]) return retVal
def test_getImages(self): docker = DockerSyncWrapper(docker_host=self.DOCKER_HOST_URL) httpretty.register_uri( httpretty.GET, self.DOCKER_HOST_URL + "/v%s/images/json" % (self.VERSION), body=json.dumps([ { "RepoTags": [ "ubuntu:12.04", "ubuntu:precise", "ubuntu:latest" ], "Id": "8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c", "Created": 1365714795, "Size": 131506275, "VirtualSize": 131506275 }, { "RepoTags": [ "ubuntu:12.10", "ubuntu:quantal" ], "ParentId": "27cf784147099545", "Id": "b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc", "Created": 1364102658, "Size": 24653, "VirtualSize": 180116135 } ]), content_type="application/json", ) images = docker.getImages() eq_(httpretty.last_request().querystring["all"], ["1"]) assert ImageTag.parse("ubuntu:12.10") in images, "tag not found"
def test_remote_registry_with_tag(self): eq_(ImageTag(repository="some/repo", tag="aTag", registry="remote.registry:5000"), ImageTag.parse("remote.registry:5000/some/repo:aTag"))
def test_official_repo_only(self): # http://index.docker.io/v1/repositories/ubuntu/tags/latest eq_(ImageTag(repository="ubuntu", tag="latest"), ImageTag.parse("ubuntu"))
def test_official_repo_with_simple_tag(self): eq_(ImageTag(repository="ubuntu", tag="12.04"), ImageTag.parse("ubuntu:12.04"))
def test_official_repo_with_latest(self): eq_(ImageTag(repository="ubuntu", tag="latest"), ImageTag.parse("ubuntu:latest"))
def test_untagged(self): eq_(None, ImageTag.parse("<none>:<none>"))
def test_remote_registry(self): eq_(ImageTag(repository="some/repo", tag="latest", registry="remote.registry:5000"), ImageTag.parse("remote.registry:5000/some/repo"))
def test_repo_with_path_and_tag(self): eq_(ImageTag(repository="some/repo", tag="aTag"), ImageTag.parse("some/repo:aTag"))
def test_repo_with_path(self): eq_(ImageTag(repository="some/repo", tag="latest"), ImageTag.parse("some/repo"))