def parse_key_value_pairs(cls, kv_pairs): """ Parses a key value pair pairs in an easier to use dict. If a key has no value it will be put on the delete list. :param list[str] kv_pairs: a list of key value pair strings. ['key=val', 'key2=val2'] :return dict[str, str]: """ pairs = {} delete = [] for kv in kv_pairs: if '=' not in kv: raise LinstorClientError( "KeyValueParseError: Key value '{kv}' pair does not contain a '='".format(kv=kv), ExitCode.ARGPARSE_ERROR ) key, value = kv.split('=', 1) if value: pairs[key] = value else: delete.append(key) return { 'pairs': pairs, 'delete': delete }
def cmd_list_error_reports(self, args): since = args.since since_dt = None if since: m = re.match(r'(\d+\W*d)?(\d+\W*h)?', since) if m: since_dt = datetime.now() if m.group(1): since_dt -= timedelta(days=int(m.group(1)[:-1])) if m.group(2): since_dt -= timedelta(hours=int(m.group(2)[:-1])) else: raise LinstorClientError( "Unable to parse since string: '{s_str}'. e.g.: 1d10h or 3h'" .format(s_str=since), ExitCode.ARGPARSE_ERROR) to_dt = None if args.to: to_dt = datetime.strptime(args.to, '%Y-%m-%d') to_dt = to_dt.replace(hour=23, minute=59, second=59) lstmsg = self._linstor.error_report_list(nodes=args.nodes, since=since_dt, to=to_dt, ids=args.report_id) return self.output_list(args, lstmsg, self.show_error_report_list, single_item=False)
def _props_list(cls, args, lstmsg): result = [] node = NodeCommands.find_node(lstmsg, args.node_name) if node: result.append(node.props) else: raise LinstorClientError("Node '{n}' not found on controller.".format(n=args.node_name), ExitCode.OBJECT_NOT_FOUND) return result
def _resolve_remote_ip(cls, hostname): """ Tries to resolve a non local ip address of the given hostname :param str hostname: hostname to resolve :return: ip address as string or None if it couldn't be resolved :rtype: str :raise: LinstorClientError if unable to determine an address """ try: addrinfo = socket.getaddrinfo(hostname, None) non_local = [y for y in addrinfo if y[0] == 2 and not y[4][0].startswith('127.')] if non_local: return non_local[0][4][0] raise LinstorClientError( "Unable determine a valid ip address '" + hostname + "'", ExitCode.ARGPARSE_ERROR) except socket.gaierror as err: raise LinstorClientError( "Unable to resolve ip address for '" + hostname + "': " + str(err), ExitCode.ARGPARSE_ERROR)
def cmd_crypt_create_passphrase(self, args): if args.passphrase: passphrase = args.passphrase else: # read from keyboard passphrase = getpass.getpass("Passphrase: ") re_passphrase = getpass.getpass("Reenter passphrase: ") if passphrase != re_passphrase: raise LinstorClientError("Passphrase doesn't match.", ExitCode.ARGPARSE_ERROR) replies = self._linstor.crypt_create_passphrase(passphrase) return self.handle_replies(args, replies)
def show_netinterfaces(cls, args, lstnodes): node = NodeCommands.find_node(lstnodes, args.node_name) if node: tbl = linstor_client.Table(utf8=not args.no_utf8, colors=not args.no_color, pastable=args.pastable) tbl.add_column(node.name, color=Color.GREEN) tbl.add_column("NetInterface") tbl.add_column("IP") for netif in node.net_interfaces: tbl.add_row(["+", netif.name, netif.address]) tbl.show() else: raise LinstorClientError( "Node '{n}' not found on controller.".format(n=args.node_name), ExitCode.OBJECT_NOT_FOUND)
def parse_time_str(timestr): """ Parses a day and hour string to a datetime from the current time. e.g.: `1d10h or 3h` :param str timestr: string to parse :return: datetime of the timestr :rtype: datetime """ m = re.match(r'(\d+\W*d)?(\d+\W*h)?', timestr) if m: since_dt = datetime.now() if m.group(1): since_dt -= timedelta(days=int(m.group(1)[:-1])) if m.group(2): since_dt -= timedelta(hours=int(m.group(2)[:-1])) return since_dt else: raise LinstorClientError( "Unable to parse since string: '{s_str}'. e.g.: 1d10h or 3h'". format(s_str=timestr), ExitCode.ARGPARSE_ERROR)
def show_netinterfaces(cls, args, lstnodes): node = lstnodes.node(args.node_name) if node: tbl = linstor_client.Table(utf8=not args.no_utf8, colors=not args.no_color, pastable=args.pastable) tbl.add_column(node.name, color=Color.GREEN) tbl.add_column("NetInterface") tbl.add_column("IP") tbl.add_column("Port") tbl.add_column("EncryptionType") # warning: system test depends on alphabetical ordering for net_if in node.net_interfaces: tbl.add_row([ "+ StltCon" if net_if.is_active else "+", net_if.name, net_if.address, net_if.stlt_port if net_if.stlt_port else "", net_if.stlt_encryption_type if net_if.stlt_encryption_type else "" ]) tbl.show() else: raise LinstorClientError("Node '{n}' not found on controller.".format(n=args.node_name), ExitCode.OBJECT_NOT_FOUND)
def check_list_sanity(self, args, replies): if replies: if self.check_for_api_replies(replies): rc = self.handle_replies(args, replies) raise LinstorClientError("List reply error", rc) return True