def __str__(self): sio = StringIO() Output.handle_ret(self._proto_msg, no_color=True, warn_as_error=False, outstream=sio) return sio.getvalue()
def list(cc, args): lstmsg = Commands._get_list_message(cc, API_LST_RSC, MsgLstRsc(), args) if lstmsg: rsc_dfns = Commands._get_list_message(cc, API_LST_RSC_DFN, MsgLstRscDfn(), args).rsc_dfns rsc_dfn_map = {x.rsc_name: x for x in rsc_dfns} stor_pools = Commands._get_list_message(cc, API_LST_STOR_POOL, MsgLstStorPool(), args).stor_pools stor_pool_map = {x.stor_pool_name: x for x in stor_pools} tbl = Table(utf8=not args.no_utf8, colors=not args.no_color, pastable=args.pastable) tbl.add_column("ResourceName") tbl.add_column("Node") tbl.add_column("Port") tbl.add_column("Device", color=Output.color(Color.DARKGREEN, args.no_color)) tbl.add_column("State", color=Output.color(Color.DARKGREEN, args.no_color), just_txt='>') for rsc in lstmsg.resources: rsc_dfn = rsc_dfn_map[rsc.name] diskless = FLAG_DISKLESS in rsc.rsc_flags if not diskless: storage_pool_name = Commands._get_prop( rsc.props, KEY_STOR_POOL_NAME) storage_pool = stor_pool_map[storage_pool_name] driver_key = StoragePoolCommands.get_driver_key( storage_pool.driver) block_device = Commands._get_prop(storage_pool.props, driver_key) # print(storage_pool) marked_delete = FLAG_DELETE in rsc.rsc_flags # rsc_state = ResourceCommands.find_rsc_state(lstmsg.resource_states, rsc.name, rsc.node_name) tbl.add_row([ rsc.name, rsc.node_name, rsc_dfn.rsc_dfn_port, tbl.color_cell("DISKLESS", Color.GREEN) if diskless else block_device, tbl.color_cell("DELETING", Color.RED) if marked_delete else "ok" ]) tbl.show() return None
def list(cc, args): lstmsg = Commands._get_list_message(cc, API_LST_RSC_DFN, MsgLstRscDfn(), args) if lstmsg: tbl = Table(utf8=not args.no_utf8, colors=not args.no_color, pastable=args.pastable) tbl.add_column("ResourceName") tbl.add_column("Port") tbl.add_column("State", color=Output.color(Color.DARKGREEN, args.no_color)) for rsc_dfn in lstmsg.rsc_dfns: tbl.add_row([ rsc_dfn.rsc_name, rsc_dfn.rsc_dfn_port, tbl.color_cell("DELETING", Color.RED) if FLAG_DELETE in rsc_dfn.rsc_dfn_flags else tbl.color_cell( "ok", Color.DARKGREEN) ]) tbl.show() # prntfrm = "{rsc:<20s} {port:<10s} {uuid:<40s}" # print(prntfrm.format(rsc="Resource-name", port="Port", uuid="UUID")) # for rsc_dfn in lstmsg.rsc_dfns: # print(prntfrm.format(rsc=rsc_dfn.rsc_name, # port=str(rsc_dfn.rsc_dfn_port), uuid=rsc_dfn.rsc_dfn_uuid)) return None
def _level_color(self, level): """ Selects a color for a level returned by GenericView subclasses """ # TODO(rck): just a hack level_color = Color.RED return Output.color(level_color)
def list(cc, args): lstmsg = Commands._get_list_message(cc, API_LST_NODE, MsgLstNode(), args) if lstmsg: tbl = Table(utf8=not args.no_utf8, colors=not args.no_color, pastable=args.pastable) tbl.add_column("Node") tbl.add_column("NodeType") tbl.add_column("IPs") tbl.add_column("State", color=Output.color(Color.DARKGREEN, args.no_color)) for n in lstmsg.nodes: ips = [if_.address for if_ in n.net_interfaces] tbl.add_row([ n.name, n.type, ",".join(ips), tbl.color_cell("ok", Color.DARKGREEN) if n.connected else tbl.color_cell("OFFLINE", Color.RED) ]) tbl.show() # prntfrm = "{node:<20s} {type:<10s} {uuid:<40s}" # print(prntfrm.format(node="Node", type="NodeType", uuid="UUID")) # netiffrm = " + {name:<20s} {address:>20s}" # for n in lstmsg.nodes: # print(prntfrm.format(node=n.name, type=n.type, uuid=n.uuid)) # for interface in n.net_interfaces: # print(netiffrm.format( # name=interface.name, # address=interface.address)) return None
def wrapper(*args, **kwargs): cliargs = args[0] servers = CommController.controller_list(cliargs.controllers) p = None with CommController(servers, cliargs.timeout) as cc: try: p = f(cc, *args, **kwargs) except ApiCallResponseError as callresponse: return callresponse.apicallresponse.output( no_color=args[0].no_color, warn_as_error=args[0].warn_as_error) if p: # could be None if no payload or if cmd_xyz does implicit return if isinstance(p, list): for call_resp in p: rc = call_resp.output(warn_as_error=args[0].warn_as_error, no_color=args[0].no_color) if rc: return rc else: return Output.handle_ret(p, no_color=args[0].no_color, warn_as_error=args[0].warn_as_error) return 0
def highlight(option_type, color, found): if found: return True for o in option_type: if line.find(o) != -1: sys.stdout.write( Output.color_str(line.rstrip(), color, args.no_color) + '\n') return True return False
def create(cc, args): p = MsgCrtNode() def gen_nif(k, v): prop = LinStorMapEntry() prop.key = "%s/%s/%s" % (NAMESPC_NETIF, args.interface_name, k) prop.value = v p.node.props.extend([prop]) # interface gen_nif(KEY_NETIF_TYPE, args.interface_type) gen_nif(KEY_NETCOM_TYPE, args.communication_type) p.node.name = args.name p.node.type = args.node_type netif = p.node.net_interfaces.add() netif.name = args.interface_name netif.address = args.ip port = args.port if not port: if args.communication_type == VAL_NETCOM_TYPE_PLAIN: port = DFLT_CTRL_PORT_PLAIN if p.node.type == VAL_NODE_TYPE_CTRL else DFLT_STLT_PORT_PLAIN elif args.communication_type == VAL_NETCOM_TYPE_SSL: port = DFLT_CTRL_PORT_SSL else: Output.err( "Communication type %s has no default port" % (args.communication_type), args.no_color) gen_nif(KEY_PORT_NR, str(port)) satcon = p.satellite_connections.add() satcon.net_interface_name = args.interface_name satcon.port = port satcon.encryption_type = args.communication_type return Commands._create(cc, API_CRT_NODE, p, args)
def list_volumes(cc, args): lstmsg = Commands._get_list_message(cc, API_LST_RSC, MsgLstRsc(), args) # type: MsgLstRsc if lstmsg: tbl = Table(utf8=not args.no_utf8, colors=not args.no_color, pastable=args.pastable) tbl.add_column("Node") tbl.add_column("Resource") tbl.add_column("VolumeNr") tbl.add_column("MinorNr") tbl.add_column("State", color=Output.color(Color.DARKGREEN, args.no_color), just_txt='>') for rsc in lstmsg.resources: rsc_state = ResourceCommands.get_resource_state( lstmsg.resource_states, rsc.node_name, rsc.name) for vlm in rsc.vlms: if rsc_state: vlm_state = ResourceCommands.get_volume_state( rsc_state.vlm_states, vlm.vlm_nr) else: vlm_state = None state = tbl.color_cell("unknown", Color.YELLOW) if vlm_state: state = "ok" problems = [] if not vlm_state.is_present: problems.append("not present") if vlm_state.disk_failed: problems.append("disk failed") if problems: state = tbl.color_cell(", ".join(problems), Color.RED) tbl.add_row([ rsc.node_name, rsc.name, str(vlm.vlm_nr), str(vlm.vlm_minor_nr), state ]) tbl.show() return None
def list(cc, args): lstmsg = Commands._get_list_message(cc, API_LST_RSC_DFN, MsgLstRscDfn(), args) if lstmsg: tbl = Table(utf8=not args.no_utf8, colors=not args.no_color, pastable=args.pastable) tbl.add_column("ResourceName") tbl.add_column("VolumeNr") tbl.add_column("VolumeMinor") tbl.add_column("Size") tbl.add_column("State", color=Output.color(Color.DARKGREEN, args.no_color)) for rsc_dfn in lstmsg.rsc_dfns: for vlmdfn in rsc_dfn.vlm_dfns: tbl.add_row([ rsc_dfn.rsc_name, vlmdfn.vlm_nr, vlmdfn.vlm_minor, approximate_size_string(vlmdfn.vlm_size), tbl.color_cell("DELETING", Color.RED) if FLAG_DELETE in rsc_dfn.rsc_dfn_flags else tbl.color_cell( "ok", Color.DARKGREEN) ]) tbl.show() # prntfrm = "{res:<15s} {uuid:<40s} {vlmnr:<5s} {vlmminor:<10s} {vlmsize:<10s}" # print(prntfrm.format(res="Resource", uuid="UUID", vlmnr="VlmNr", vlmminor="VlmMinor", vlmsize="Size")) # prntfrm = "{res:<15s} {uuid:<40s} {vlmnr:<5d} {vlmminor:<10d} {vlmsize:<20s}" # for rscdfn in lstmsg.rsc_dfns: # for vlmdfn in rscdfn.vlm_dfns: # print(prntfrm.format( # res=rscdfn.rsc_name, # uuid=vlmdfn.vlm_dfn_uuid, # vlmnr=vlmdfn.vlm_nr, # vlmminor=vlmdfn.vlm_minor, # vlmsize=approximate_size_string(vlmdfn.vlm_size))) return None
def cmd_enoimp(self, args): Output.err('This command is deprecated or not implemented', args.no_color)
def cmd_list_options(self, args): net_options = args.noobj.get_options() disk_options = args.doobj.get_options() peer_device_options = args.pdoobj.get_options() resource_options = args.roobj.get_options() # filter net-options drbdmanage sets unconditionally. net_options = filter_prohibited(net_options, ('shared-secret', 'cram-hmac-alg')) colors = { 'net-options': Output.color(Color.TEAL, args), 'disk-options': Output.color(Color.BROWN, args), 'peer-device-options': Output.color(Color.GREEN, args), 'resource-options': Output.color(Color.DARKPINK, args), } # TODO(rck): # ret, conf = self.dsc(self._server.get_selected_config_values, [KEY_DRBD_CONFPATH]) conf_path = "/dev/null" return res_file = 'linstor_' + args.resource + '.res' # TODO(rck): # conf_path = self._get_conf_path(conf) res_file = os.path.join(conf_path, res_file) if not os.path.isfile(res_file): sys.stderr.write('Resource file "' + res_file + '" does not exist\n') sys.exit(1) common_file = os.path.join(conf_path, FILE_GLOBAL_COMMON_CONF) def highlight(option_type, color, found): if found: return True for o in option_type: if line.find(o) != -1: sys.stdout.write( Output.color_str(line.rstrip(), color, args.no_color) + '\n') return True return False for res_f in (common_file, res_file): sys.stdout.write(res_f + ":\n") with open(res_f) as f: for line in f: if line.find('{') != -1 or line.find('}') != -1: sys.stdout.write(line) continue found = highlight(net_options, colors['net-options'], False) found = highlight(disk_options, colors['disk-options'], found) found = highlight(peer_device_options, colors['peer-device-options'], found) found = highlight(resource_options, colors['resource-options'], found) if not found: sys.stdout.write(line) sys.stdout.write("\n") sys.stdout.write("Legend:\n") for k, v in colors.items(): sys.stdout.write(v + k + Color.NONE + "\n") sys.stdout.write("\nNote: Do not directly edit these auto-generated" " files as they will be overwritten.\n") sys.stdout.write( "Use the according linstor sub-commands to set/unset options.\n")
def output(self, warn_as_error, no_color): return Output.handle_ret(self._proto_msg, no_color=no_color, warn_as_error=warn_as_error)