def install_image(self, path): """ Install a container image from a remote path. :param path: Path to the container image :type path: ``str`` :rtype: :class:`libcloud.container.base.ContainerImage` """ payload = {} data = json.dumps(payload) result = self.connection.request('/images/create?fromImage=%s' % (path), data=data, method='POST') if "errorDetail" in result.body: raise DockerException(None, result.body) try: # get image id image_id = re.findall( r'{"status":"Download complete"' r',"progressDetail":{},"id":"\w+"}', result.body)[-1] image_id = json.loads(image_id).get('id') except: raise DockerException(None, 'failed to install image') image = ContainerImage(id=image_id, name=path, path=path, version=None, driver=self.connection.driver, extra={}) return image
def list_images(self): """ List the installed container images :rtype: ``list`` of :class:`libcloud.container.base.ContainerImage` """ result = self.connection.request('/v%s/images/json' % (self.version)).object images = [] for image in result: try: name = image.get('RepoTags')[0] except: name = image.get('Id') images.append( ContainerImage( id=image.get('Id'), name=name, path=name, version=None, driver=self.connection.driver, extra={ "created": image.get('Created'), "size": image.get('Size'), "virtual_size": image.get('VirtualSize'), }, )) return images
def _to_image(self, data, host, repository_name): path = '%s/%s:%s' % (host, repository_name, data['imageTag']) return ContainerImage(id=None, name=path, path=path, version=data['imageTag'], driver=self.connection.driver)
def _to_container(self, data, container_status, pod_data): """ Convert container in Container instances """ state = container_status.get("state") created_at = None if state: started_at = list(state.values())[0].get("startedAt") if started_at: created_at = datetime.datetime.strptime( started_at, "%Y-%m-%dT%H:%M:%SZ") extra = { "pod": pod_data["metadata"]["name"], "namespace": pod_data["metadata"]["namespace"], } resources = data.get("resources") if resources: extra["resources"] = resources return Container( id=container_status.get("containerID") or data["name"], name=data["name"], image=ContainerImage( id=container_status.get("imageID") or data["image"], name=data["image"], path=None, version=None, driver=self.connection.driver, ), ip_addresses=None, state=(ContainerState.RUNNING if container_status else ContainerState.UNKNOWN), driver=self.connection.driver, created_at=created_at, extra=extra, )
def test_deploy_container(self): image = ContainerImage(id=None, name="hello-world", path=None, driver=self.driver, version=None) container = self.driver.deploy_container("hello-world", image=image) self.assertEqual(container.name, "hello-world")
def _to_container(self, data): """ Convert container in Container instances """ try: name = data.get('Name').strip('/') except: try: name = data.get('Names')[0].strip('/') except: name = data.get('Id') state = data.get('State') if isinstance(state, dict): status = data.get( 'Status', state.get('Status') if state is not None else None) else: status = data.get('Status') if 'Exited' in status: state = ContainerState.STOPPED elif status.startswith('Up '): state = ContainerState.RUNNING elif 'running' in status: state = ContainerState.RUNNING else: state = ContainerState.STOPPED image = data.get('Image') ports = data.get('Ports', []) created = data.get('Created') if isinstance(created, float): created = ts_to_str(created) extra = { 'id': data.get('Id'), 'status': data.get('Status'), 'created': created, 'image': image, 'ports': ports, 'command': data.get('Command'), 'sizerw': data.get('SizeRw'), 'sizerootfs': data.get('SizeRootFs'), } ips = [] if ports is not None: for port in ports: if port.get('IP') is not None: ips.append(port.get('IP')) return Container(id=data['Id'], name=name, image=ContainerImage(id=data.get('ImageID', None), path=image, name=image, version=None, driver=self.connection.driver), ip_addresses=ips, state=state, driver=self.connection.driver, extra=extra)
def _to_image(self, repository_name, obj): path = '%s/%s:%s' % (self.connection.host, repository_name, obj['name']) return ContainerImage(id=obj['id'], path=path, name=path, version=obj['name'], extra={'full_size': obj['full_size']}, driver=None)
def _to_image(self, repository_name, obj): path = "%s/%s:%s" % (self.connection.host, repository_name, obj["name"]) return ContainerImage( id=obj["id"], path=path, name=path, version=obj["name"], extra={"full_size": obj["full_size"]}, driver=None, )
def test_deploy_container(self): for driver in self.drivers: image = ContainerImage(id=None, name=None, path=None, version=None, driver=driver) container = driver.deploy_container(name='first_lxd_container', image=image, parameters='{"source":{"type":"image", ' '"fingerprint":"7ed08b435c92cd8a8a884c88e8722f2e7546a51e891982a90ea9c15619d7df9b"}}') self.assertIsInstance(container, Container) self.assertEqual(container.name, 'first_lxd_container')
def test_deploy_container(self): container = self.driver.deploy_container( name='jim', image=ContainerImage( id=None, name='mysql', path='mysql', version=None, driver=self.driver ) ) self.assertEqual(container.id, 'arn:aws:ecs:ap-southeast-2:647433528374:container/e443d10f-dea3-481e-8a1e-966b9ad4e498')
def test_deploy_container(self): container = self.driver.deploy_container( name='newcontainer', image=ContainerImage(id="hastebin", name="hastebin", path="rlister/hastebin", version="latest", driver=None), environment={"STORAGE_TYPE": "file"}, networkMode="managed") self.assertEqual(container.id, '1i31') self.assertEqual(container.name, 'newcontainer') self.assertEqual(container.extra['environment'], {'STORAGE_TYPE': 'file'})
def _to_container(self, data, task_definition_arn): return Container(id=data['containerArn'], name=data['name'], image=ContainerImage(id=None, name=data['name'], path=None, version=None, driver=self.connection.driver), ip_addresses=None, state=self.status_map.get(data['lastStatus'], None), extra={ 'taskArn': data['taskArn'], 'taskDefinitionArn': task_definition_arn }, driver=self.connection.driver)
def _gen_image(self, imageuuid): """ This function converts a valid Rancher ``imageUuid`` string to a valid image object. Only supports docker based images hence `docker:` must prefix!! Please see the deploy_container() for details on the format. :param imageuuid: A valid Rancher image string i.e. ``docker:rlister/hastebin:8.0`` :type imageuuid: ``str`` :return: Converted ContainerImage object. :rtype: :class:`libcloud.container.base.ContainerImage` """ # Obtain just the name(:version) for parsing if '/' not in imageuuid: # String looks like `docker:mysql:8.0` image_name_version = imageuuid.partition(':')[2] else: # String looks like `docker:oracle/mysql:8.0` image_name_version = imageuuid.rpartition("/")[2] # Parse based on ':' if ':' in image_name_version: version = image_name_version.partition(":")[2] id = image_name_version.partition(":")[0] name = id else: version = 'latest' id = image_name_version name = id # Get our path based on if there was a version if version != 'latest': path = imageuuid.partition(':')[2].rpartition(':')[0] else: path = imageuuid.partition(':')[2] return ContainerImage( id=id, name=name, path=path, version=version, driver=self.connection.driver, extra={ "imageUuid": imageuuid } )
def test_deploy_container(self): container = self.driver.deploy_container( name="newcontainer", image=ContainerImage( id="hastebin", name="hastebin", path="rlister/hastebin", version="latest", driver=None, ), environment={"STORAGE_TYPE": "file"}, networkMode="managed", ) self.assertEqual(container.id, "1i31") self.assertEqual(container.name, "newcontainer") self.assertEqual(container.extra["environment"], {"STORAGE_TYPE": "file"})
def test_ex_deploy_service(self): image = ContainerImage(id="hastebin", name="hastebin", path="rlister/hastebin", version="latest", driver=None) service = self.driver.ex_deploy_service( name="newservice", environment_id="1e1", image=image, environment={"root_password": "******"}) self.assertEqual(service['id'], "1s13") self.assertEqual(service['environmentId'], "1e6") self.assertEqual( service['launchConfig']['environment']['root_password'], "password") self.assertEqual(service['launchConfig']['imageUuid'], "docker:rlister/hastebin:latest")
def _to_container(self, data, container_status, pod_data): """ Convert container in Container instances """ return Container(id=container_status['containerID'], name=data['name'], image=ContainerImage(id=container_status['imageID'], name=data['image'], path=None, version=None, driver=self.connection.driver), ip_addresses=None, state=ContainerState.RUNNING, driver=self.connection.driver, extra={ 'pod': pod_data['metadata']['name'], 'namespace': pod_data['metadata']['namespace'] })
def install_image(self, path): """ Install a container image from a remote path. :param path: Path to the container image :type path: ``str`` :rtype: :class:`libcloud.container.base.ContainerImage` """ payload = {} data = json.dumps(payload) result = self.connection.request( "/v%s/images/create?fromImage=%s" % (self.version, path), data=data, method="POST", ) if "errorDetail" in result.body: raise DockerException(None, result.body) image_id = None # the response is slightly different if the image is already present # and it's not downloaded. both messages below indicate that the image # is available for use to the daemon if re.search(r"Downloaded newer image", result.body) or re.search( r'"Status: Image is up to date', result.body ): if re.search(r"sha256:(?P<id>[a-z0-9]{64})", result.body): image_id = re.findall(r"sha256:(?P<id>[a-z0-9]{64})", result.body)[-1] # if there is a failure message or if there is not an image id in the # response then throw an exception. if image_id is None: raise DockerException(None, "failed to install image") image = ContainerImage( id=image_id, name=path, path=path, version=None, driver=self.connection.driver, extra={}, ) return image
def _to_container(self, data, task_definition_arn): return Container( id=data["containerArn"], name=data["name"], image=ContainerImage( id=None, name=data["name"], path=None, version=None, driver=self.connection.driver, ), ip_addresses=None, state=self.status_map.get(data["lastStatus"], None), extra={ "taskArn": data["taskArn"], "taskDefinitionArn": task_definition_arn, }, driver=self.connection.driver, )
def ex_search_images(self, term): """Search for an image on Docker.io. Returns a list of ContainerImage objects >>> images = conn.ex_search_images(term='mistio') >>> images [<ContainerImage: id=rolikeusch/docker-mistio...>, <ContainerImage: id=mist/mistio, name=mist/mistio, driver=Docker ...>] :param term: The search term :type term: ``str`` :rtype: ``list`` of :class:`libcloud.container.base.ContainerImage` """ term = term.replace(" ", "+") result = self.connection.request( "/v%s/images/search?term=%s" % (self.version, term) ).object images = [] for image in result: name = image.get("name") images.append( ContainerImage( id=name, path=name, version=None, name=name, driver=self.connection.driver, extra={ "description": image.get("description"), "is_official": image.get("is_official"), "is_trusted": image.get("is_trusted"), "star_count": image.get("star_count"), }, ) ) return images
def _to_container(self, data, container_status, pod_data): """ Convert container in Container instances """ return Container( id=container_status["containerID"], name=data["name"], image=ContainerImage( id=container_status["imageID"], name=data["image"], path=None, version=None, driver=self.connection.driver, ), ip_addresses=None, state=ContainerState.RUNNING, driver=self.connection.driver, extra={ "pod": pod_data["metadata"]["name"], "namespace": pod_data["metadata"]["namespace"], }, )
from libcloud.container.base import ContainerImage from libcloud.container.types import Provider from libcloud.container.providers import get_driver cls = get_driver(Provider.ECS) conn = cls(access_id='SDHFISJDIFJSIDFJ', secret='THIS_IS)+_MY_SECRET_KEY+I6TVkv68o4H', region='ap-southeast-2') for cluster in conn.list_clusters(): print(cluster.name) if cluster.name == 'default': container = conn.deploy_container( cluster=cluster, name='my-simple-app', image=ContainerImage( id=None, name='simple-app', path='simple-app', version=None, driver=conn ) )
from libcloud.container.base import ContainerImage from libcloud.container.types import Provider from libcloud.container.providers import get_driver cls = get_driver(Provider.ECS) conn = cls( access_id="SDHFISJDIFJSIDFJ", secret="THIS_IS)+_MY_SECRET_KEY+I6TVkv68o4H", region="ap-southeast-2", ) for cluster in conn.list_clusters(): print(cluster.name) if cluster.name == "my-cluster": conn.list_containers(cluster=cluster) container = conn.deploy_container( name="my-simple-app", image=ContainerImage(id=None, name="simple-app", path="simple-app", version=None, driver=conn), cluster=cluster, )
from libcloud.container.types import Provider from libcloud.container.providers import get_driver from libcloud.container.base import ContainerImage driver = get_driver(Provider.RANCHER) connection = driver("MYRANCHERACCESSKEY", "MYRANCHERSECRETKEY", host="17.23.66.4", port=443) image = ContainerImage("hastebin", "hastebin", "rlister/hastebin", "latest", driver=None) new_service = connection.ex_deploy_service( name="excitingservice", image=image, environmentid="1e2", environment={"STORAGE_TYPE": "file"})
def _to_container(self, data): """ Convert container in Container instances """ try: name = data.get("Name").strip("/") except Exception: try: name = data.get("Names")[0].strip("/") except Exception: name = data.get("Id") state = data.get("State") if isinstance(state, dict): status = data.get( "Status", state.get("Status") if state is not None else None ) else: status = data.get("Status") if "Exited" in status: state = ContainerState.STOPPED elif status.startswith("Up "): state = ContainerState.RUNNING elif "running" in status: state = ContainerState.RUNNING else: state = ContainerState.STOPPED image = data.get("Image") ports = data.get("Ports", []) created = data.get("Created") if isinstance(created, float): created = ts_to_str(created) extra = { "id": data.get("Id"), "status": data.get("Status"), "created": created, "image": image, "ports": ports, "command": data.get("Command"), "sizerw": data.get("SizeRw"), "sizerootfs": data.get("SizeRootFs"), } ips = [] if ports is not None: for port in ports: if port.get("IP") is not None: ips.append(port.get("IP")) return Container( id=data["Id"], name=name, image=ContainerImage( id=data.get("ImageID", None), path=image, name=image, version=None, driver=self.connection.driver, ), ip_addresses=ips, state=state, driver=self.connection.driver, extra=extra, )