def _get_images(self, name=None, show_all=False, filters={}, image_list=None): """ :param name(str): Only show images belonging to the repository name :param show_all(bool): Show all images (by default filter out the intermediate image layers) :parma filters(dict): Filters to be applied on the image list :param image_list(list): List of image id or name """ cli = self.swarm.client if cli is not None: try: ret = cli.images(name=name,all=show_all,filters=filters) except errors.NotFound as e: print(e.explanation) return except errors.APIError as e: print(e.explanation) return except errors.DockerException: print(e.explanation) return finally: cli.close() if ret: for image in ret: # if image_list provide, then get images against it if image_list is not None: if not image['Id'].startswith(image_list) and\ not image['RepoTags'].startswith(image_list): continue image_id = image['Id'][:12] # convert created timestamp to human-readable string created_delta = datetime.now() - datetime.fromtimestamp(image['Created']) if created_delta.days > 1: created = '{day} days ago'.format(day=created_delta.days) else: created = timeformat(created_delta.seconds + created_delta.days * 86400) # convert virtual size to human-readable string virtual_size = byteformat(image['VirtualSize'],base=1000) # get the longest created field length for pretty print self.created_length = len(created) if len(created) > self.created_length\ else self.created_length for repotag in image['RepoTags']: repo, tag = repotag.split(':') data = (repo, tag, image_id, created, virtual_size) self.images.add(data) # get the longest repo/tag field length for pretty print self.repo_length = len(repo) if len(repo) > self.repo_length else self.repo_length self.tag_length = len(tag) if len(tag) > self.tag_length else self.tag_length
def __call__(self): cli = self.swarm.client if cli is not None: ret = cli.info() cli.close() string = '''\ Containers: {Containers} Images: {Images} {DriverStatus} CPUs: {NCPU} Total Memory: {MemTotal} Name: {Name}\ '''.format(\ Containers=ret['Containers'],\ Images=ret['Images'],\ DriverStatus='\n'.join([ ': '.join((item[0].encode('utf8'), item[1].encode('utf8')))\ for item in ret['DriverStatus']]),\ NCPU=ret['NCPU'],\ MemTotal=byteformat(ret['MemTotal']),\ Name=ret['Name']) print(string)