def utc(x, totimestamp=False): if totimestamp == False: return timeformat(datetime.utcfromtimestamp(x)) else: d = datetime(*x) epoch = datetime(1970, 1, 1) return int((d - epoch).total_seconds())
def queryMoneyHours(self, count=None): try: query = self.session.query(Money_Hour).order_by(Money_Hour.id.desc()).all() for index, row in enumerate(query): row.timeshow = timeformat(row.timestamp) if index % 6 == 0: row.annotation = str(row.value) else: row.annotation = '' if count != None: query = query[:count] query.reverse() return query except: self.session.rollback()
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 queryMoneyMinutes(self, count=30): #return latest cur of last 30 minutes try: query = self.session.query(Money_Minute).order_by(Money_Minute.id.desc()).limit(count).all() for index, row in enumerate(query): row.timeshow = timeformat(row.timestamp) if index % 6 == 0: row.annotation = str(row.value) else: row.annotation = '' query.reverse() return query except: self.session.rollback() finally: self.session.close()
def queryMoneyHours(self, count=None): try: query = self.session.query(Money_Hour).order_by( Money_Hour.id.desc()).all() for index, row in enumerate(query): row.timeshow = timeformat(row.timestamp) if index % 6 == 0: row.annotation = str(row.value) else: row.annotation = '' if count != None: query = query[:count] query.reverse() return query except: self.session.rollback()
def queryMoneyMinutes(self, count=30): #return latest cur of last 30 minutes try: query = self.session.query(Money_Minute).order_by( Money_Minute.id.desc()).limit(count).all() for index, row in enumerate(query): row.timeshow = timeformat(row.timestamp) if index % 6 == 0: row.annotation = str(row.value) else: row.annotation = '' query.reverse() return query except: self.session.rollback() finally: self.session.close()
def _get_containers(self, show_all=False, filters={}, limit=None, latest=None, since=None,\ container_list=None): """ :param show_all(bool): Show all containers. Only running containers are shown by default :param filters(dict): Filters to be processed on the image list :parma limit(tuple or list): Filter containers by node name or node pattern :param latest(bool): Show only the latest created container, include non-running ones :param since(str): Show only containers created since Id or Name, include non-running ones :param container_list(list): List containes container ids """ cli = self.swarm.client if cli is not None: try: ret = cli.containers(all=show_all, filters=filters, latest=latest, since=since) 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 container in ret: # if container_list provide, then get containers against it # name and id are both allowed for query from api version '1.20' if container_list is not None: if not container['Id'].startswith(container_list): continue # if limit is provide, then get containers against it node = container['Names'][0].split('/', 2)[1] if limit is not None: if not node in limit: continue # 'Names' includes self container name as well as names of linked containers # Filter name by checking '/' for names in container['Names']: if names.count('/') == 2: name = names.split('/')[2] break # convert created timestamp to human-readable string created_delta = datetime.now() - datetime.fromtimestamp(container['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) # get the longest node/created/status field length for pretty print self.node_length = len(node) if len(node) > self.node_length else self.node_length self.image_length = len(container['Image']) if len(container['Image']) > self.image_length\ else self.image_length if len(container['Command']) < self.command_length: command = container['Command'] else: command = container['Command'] if len(container['Command']) < self.max_command_length\ else container['Command'][:self.max_command_length] self.command_length = len(container['Command']) if len(container['Command']) < self.max_command_length\ else self.max_command_length self.created_length = len(created) if len(created) > self.created_length\ else self.created_length self.status_length = len(container['Status']) if len(container['Status']) > self.status_length\ else self.status_length # (Id, Node, Image, Command, Created, Status, Names) data = (container['Id'], node, container['Image'], command, created, container['Status'], name) self.containers.setdefault(node, []).append(data)