def has_image(self, img): err_append = "Refine your search to narrow results." self.input = img image_info = self._get_images(get_all=True) img_obj = self.inspect_image(image=img) if img_obj: return img_obj # If we cannot find the image locally AND it has a digest # then bail. if '@sha256:' in img: return None name_search = util.image_by_name(img, images=image_info) length = len(name_search) if length == 0: # No dice return None if length > 1: tmp_image = dict((x['Id'], x['RepoTags']) for x in image_info) repo_tags = [] for name in name_search: for repo_tag in tmp_image.get(name['Id']): if repo_tag.find(img) > -1: repo_tags.append(repo_tag) raise ValueError("Found more than one image possibly " "matching '{0}'. They are:\n {1} \n{2}" .format(img, "\n ".join(repo_tags), err_append)) inspected = self._inspect_image(img) if not inspected: return None return self._make_image(img, inspected, deep=True)
def test_image_by_name_glob(self): matches = util.image_by_name('atomic-test-*') self.assertTrue(len(matches) > 2) self.assertTrue( all([ m['Labels']['Name'].startswith('atomic-test-') for m in matches ]))
def _is_image(self, identifier): ''' Checks is the identifier is a image ID or a matches an image name. If it finds a match, it returns the full image ID. Else it will return an AtomicError. ''' err_append = "Refine your search to narrow results." image_info = self.get_images() inspect = self._inspect_image(image=identifier) if inspect is not None: self.inspect = inspect return inspect['Id'] name_search = util.image_by_name(identifier, images=image_info) if len(name_search) > 0: if len(name_search) > 1: tmp_image = dict((x['Id'], x['RepoTags']) for x in image_info) repo_tags = [] for name in name_search: for repo_tag in tmp_image.get(name['Id']): if repo_tag.find(identifier) > -1: repo_tags.append(repo_tag) raise ValueError( "Found more than one image possibly " "matching '{0}'. They are:\n {1} \n{2}".format( identifier, "\n ".join(repo_tags), err_append)) return name_search[0]['Id'] # No dice raise AtomicError
def has_image(self, img): err_append = "Refine your search to narrow results." self.input = img image_info = self._get_images(get_all=True) img_obj = self.inspect_image(image=img) if img_obj: return img_obj name_search = util.image_by_name(img, images=image_info) length = len(name_search) if length == 0: # No dice return None if length > 1: tmp_image = dict((x["Id"], x["RepoTags"]) for x in image_info) repo_tags = [] for name in name_search: for repo_tag in tmp_image.get(name["Id"]): if repo_tag.find(img) > -1: repo_tags.append(repo_tag) raise ValueError( "Found more than one image possibly " "matching '{0}'. They are:\n {1} \n{2}".format(img, "\n ".join(repo_tags), err_append) ) return self._make_image(img, self._inspect_image(img), deep=True)
def _is_image(self, identifier): ''' Checks is the identifier is a image ID or a matches an image name. If it finds a match, it returns the full image ID. Else it will return an AtomicError. ''' err_append = "Refine your search to narrow results." image_info = self.get_images() inspect = self._inspect_image(image=identifier) if inspect is not None: self.inspect = inspect return inspect['Id'] name_search = util.image_by_name(identifier, images=image_info) if len(name_search) > 0: if len(name_search) > 1: tmp_image = dict((x['Id'], x['RepoTags']) for x in image_info) repo_tags = [] for name in name_search: for repo_tag in tmp_image.get(name['Id']): if repo_tag.find(identifier) > -1: repo_tags.append(repo_tag) raise ValueError("Found more than one image possibly " "matching '{0}'. They are:\n {1} \n{2}" .format(identifier, "\n ".join(repo_tags), err_append)) return name_search[0]['Id'] # No dice raise AtomicError
def _identifier_as_cid(self, identifier): """ Returns a container uuid for identifier. If identifier is an image UUID or image tag, create a temporary container and return its uuid. """ def __cname_matches(container, identifier): return any([ n for n in (container['Names'] or []) if matches(n, '/' + identifier) ]) # Determine if identifier is a container containers = [ c['Id'] for c in self.client.containers(all=True) if (__cname_matches(c, identifier) or matches(c['Id'], identifier + '*')) ] if len(containers) > 1: raise SelectionMatchError(identifier, containers) elif len(containers) == 1: c = containers[0] return c if self.live else self._clone(c) # Determine if identifier is an image UUID images = [ i for i in set(self.client.images(all=True, quiet=True)) if i.startswith(identifier) ] if len(images) > 1: raise SelectionMatchError(identifier, images) elif len(images) == 1: return self._create_temp_container(images[0]) # Match image tag. images = util.image_by_name(identifier) if len(images) > 1: tags = [t for i in images for t in i['RepoTags']] raise SelectionMatchError(identifier, tags) elif len(images) == 1: return self._create_temp_container(images[0]['Id']) raise MountError('{} did not match any image or container.' ''.format(identifier))
def _identifier_as_cid(self, identifier): """ Returns a container uuid for identifier. If identifier is an image UUID or image tag, create a temporary container and return its uuid. """ def __cname_matches(container, identifier): return any([n for n in (container['Names'] or []) if matches(n, '/' + identifier)]) # Determine if identifier is a container containers = [c['Id'] for c in self.client.containers(all=True) if (__cname_matches(c, identifier) or matches(c['Id'], identifier + '*'))] if len(containers) > 1: raise SelectionMatchError(identifier, containers) elif len(containers) == 1: c = containers[0] return c if self.live else self._clone(c) # Determine if identifier is an image UUID images = [i for i in set(self.client.images(all=True, quiet=True)) if i.startswith(identifier)] if len(images) > 1: raise SelectionMatchError(identifier, images) elif len(images) == 1: return self._create_temp_container(images[0]) # Match image tag. images = util.image_by_name(identifier) if len(images) > 1: tags = [t for i in images for t in i['RepoTags']] raise SelectionMatchError(identifier, tags) elif len(images) == 1: return self._create_temp_container(images[0]['Id']) raise MountError('{} did not match any image or container.' ''.format(identifier))
def test_image_by_name_no_match(self): matches = util.image_by_name('this is not a real image name') self.assertTrue(len(matches) == 0)
def test_image_by_name_registry_match(self): matches = util.image_by_name('/centos:latest') self.assertTrue(len(matches) == 1)
def test_image_by_name(self): matches = util.image_by_name('atomic-test-1') self.assertEqual(len(matches), 1) self.assertEqual(matches[0]['Labels']['Name'], 'atomic-test-1')
def test_image_by_name_tag_glob(self): matches = util.image_by_name('/busybox:*atest*') self.assertTrue(len(matches) == 1)
def test_image_by_name_glob(self): matches = util.image_by_name("atomic-test-*") self.assertTrue(len(matches) > 2) self.assertTrue(all([m["Labels"]["Name"].startswith("atomic-test-") for m in matches]))
def test_image_by_name_glob(self): matches = util.image_by_name('atomic-test-*') self.assertTrue(len(matches) > 2) self.assertTrue(all([m['Labels']['Name'].startswith('atomic-test-') for m in matches]))
def test_image_by_name_tag_glob(self): matches = util.image_by_name('busybox:*atest*') self.assertTrue(len(matches) == 1)
def test_image_by_name(self): matches = util.image_by_name("atomic-test-1") self.assertEqual(len(matches), 1) self.assertEqual(matches[0]["Labels"]["Name"], "atomic-test-1")