def get_linstorapi(self, **kwargs): # if self._linstor: # return self._linstor if self._linstor_completer: return self._linstor_completer # TODO also read config overrides # servers = ['linstor://localhost'] with open(LinstorAPI.LINSTOR_CONF) as f: data = f.read() contrl_list = re.findall('controllers=(.*)', data)[0] servers = linstor.MultiLinstor.controller_uri_list(contrl_list) if 'parsed_args' in kwargs: cliargs = kwargs['parsed_args'] servers = linstor.MultiLinstor.controller_uri_list( cliargs.controllers) if not servers: return None for server in servers: try: self._linstor_completer = linstor.Linstor(server) self._linstor_completer.connect() break except linstor.LinstorNetworkError as le: pass return self._linstor_completer
def get_sp(): try: lin = linstor.Linstor(DEFAULT_LINSTOR_URI) lin.connect() # Fetch Storage Pool List sp_list_reply = jsons.dump(lin.storage_pool_list()[0].storage_pools) assert sp_list_reply, "Empty Storage Pool list" print(len(sp_list_reply)) sp_diskless_list = [] sp_list = [] node_count = 0 if sp_list_reply: for node in sp_list_reply: print(node["node_name"]) sp_node = {} sp_node['node_name'] = node["node_name"] sp_node['sp_uuid'] = node["uuid"] sp_node['sp_name'] = node["name"] if node["provider_kind"] == DISKLESS: diskless = True else: diskless = False # Driver selection if node["provider_kind"] == LVM: sp_node['driver_name'] = LVM elif node["provider_kind"] == LVM_THIN: sp_node['driver_name'] = LVM_THIN elif node["provider_kind"] == ZFS: sp_node['driver_name'] = ZFS elif node["provider_kind"] == ZFS_THIN: sp_node['driver_name'] = ZFS_THIN else: sp_node['driver_name'] = str(node["provider_kind"]) if diskless: sp_diskless_list.append(sp_node) else: sp_list.append(sp_node) node_count += 1 print(sp_node) # Add the diskless nodes to the end of the list if sp_diskless_list: sp_list.extend(sp_diskless_list) print('\nFound ' + str(len(sp_list)) + ' storage pools.') lin.disconnect() pprint.pprint(sp_list) return sp_list except Exception as e: print(str(e))
def linstor_driver_init(): try: lin = linstor.Linstor(DEFAULT_LINSTOR_URI) lin.connect() # Check for Storage Pool List sp_list = get_sp() # pprint.pprint(sp_list) # Get default Storage Pool Definition spd_default = VOL_GROUP if not sp_list: print("No existing Storage Pools found") # Check for Ns node_list = get_nodes() # pprint.pprint(node_list) if len(node_list) == 0: print("Error: No resource nodes available" ) # Exception needed here # Create Storage Pool (definition is implicit) spd_name = get_spd()[0] pool_driver = LVM_THIN # LVM_THIN if pool_driver == LVM: driver_pool = VOL_GROUP elif pool_driver == LVM_THIN: driver_pool = VOL_GROUP + "/" + spd_name for node in node_list: lin.storage_pool_create(node_name=node['node_name'], storage_pool_name=spd_name, storage_driver=pool_driver, driver_pool_name=driver_pool) print('Created Storage Pool for ' + spd_name + ' @ ' + node['node_name'] + ' in ' + driver_pool) # Ready to Move on lin.disconnect() return else: print("Found existing Storage Pools") # Ready to Move on lin.disconnect() return except Exception as e: print(str(e))
def parse_and_execute(self, pargs): rc = ExitCode.OK try: args = self.parse(pargs) local_only_cmds = [ self.cmd_list, MigrateCommands.cmd_dmmigrate, self._zsh_generator.cmd_completer, self.cmd_help ] # only connect if not already connected or a local only command was executed if self._linstorapi is None and args.func not in local_only_cmds: self._linstorapi = linstor.Linstor(Commands.controller_list( args.controllers)[0], timeout=args.timeout) self._controller_commands._linstor = self._linstorapi self._node_commands._linstor = self._linstorapi self._storage_pool_dfn_commands._linstor = self._linstorapi self._storage_pool_commands._linstor = self._linstorapi self._resource_dfn_commands._linstor = self._linstorapi self._volume_dfn_commands._linstor = self._linstorapi self._resource_commands._linstor = self._linstorapi self._snapshot_commands._linstor = self._linstorapi self._misc_commands._linstor = self._linstorapi self._linstorapi.connect() rc = args.func(args) except ArgumentError as ae: sys.stderr.write(ae.message + '\n') try: self.parse( list( itertools.takewhile(lambda x: not x.startswith('-'), pargs)) + ['-h']) except SystemExit: pass return ExitCode.ARGPARSE_ERROR except utils.LinstorClientError as lce: sys.stderr.write(lce.message + '\n') return lce.exit_code except linstor.LinstorNetworkError as le: self._report_linstor_error(le) rc = ExitCode.CONNECTION_ERROR except linstor.LinstorTimeoutError as le: self._report_linstor_error(le) rc = ExitCode.CONNECTION_TIMEOUT except linstor.LinstorError as le: self._report_linstor_error(le) rc = ExitCode.UNKNOWN_ERROR return rc
def get_linstorapi(self, **kwargs): if self._linstor: return self._linstor if self._linstor_completer: return self._linstor_completer # TODO also read config overrides servers = ['linstor://localhost'] if 'parsed_args' in kwargs: cliargs = kwargs['parsed_args'] servers = Commands.controller_list(cliargs.controllers) if not servers: return None self._linstor_completer = linstor.Linstor(servers[0]) self._linstor_completer.connect() return self._linstor_completer
def linstor_deploy_resource(rsc_name=DEFAULT_RSC): try: lin = linstor.Linstor(DEFAULT_LINSTOR_URI) lin.connect() linstor_driver_init() # Check for RD # rd_list = lin.resource_dfn_list() # rsc_name_target = rsc_name rd_reply = lin.resource_dfn_create(rsc_name) # Need error checking print(check_api_response(rd_reply)) rd_list = lin.resource_dfn_list() print("Created RD") # Create a New VD vd_reply = lin.volume_dfn_create( rsc_name=rsc_name, size=int(DEFAULT_RSC_SIZE)) # size is in KiB print(check_api_response(vd_reply)) print("Created VD: " + str(vd_reply)) # Create RSC's sp_list = get_sp() pprint.pprint(sp_list) for node in sp_list: if node["driver_name"] == "DISKLESS": is_diskless = True else: is_diskless = False new_rsc = linstor.ResourceData(rsc_name=rsc_name, node_name=node["node_name"], storage_pool=node["sp_name"], diskless=is_diskless) rsc_reply = lin.resource_create([new_rsc], async_msg=False) print("RSC Create: " + str(check_api_response(rsc_reply))) lin.disconnect() return except Exception as e: print(str(e))
def nuke(): with linstor.Linstor(DEFAULT_LINSTOR_URI) as lin: # Nuke Resources rsc_reply = jsons.dump(lin.resource_list()[0].resources) if rsc_reply: print(rsc_reply) for rsc in rsc_reply: print(rsc["_rest_data"]["name"] + ' at ' + rsc["_rest_data"]["node_name"]) lin.resource_delete(node_name=rsc["_rest_data"]["node_name"], rsc_name=rsc["_rest_data"]["name"]) time.sleep(1) rsc_dfn_list = jsons.dump( lin.resource_dfn_list()[0].resource_definitions) print(rsc_dfn_list) for rsc_dfn in rsc_dfn_list: print(rsc_dfn["name"]) # Delete VD print('Deleting Volume Definition for ' + rsc_dfn["name"]) # TODO: Need work here for volume number api_reply = lin.volume_dfn_delete(rsc_dfn["name"], 0) print(api_reply) time.sleep(1) # Delete RD print('Deleting Resource Definition for ' + rsc_dfn["name"]) api_reply = lin.resource_dfn_delete(rsc_dfn["name"]) print(api_reply) else: print('NO RSCs to delete')
def get_spd(): try: lin = linstor.Linstor(DEFAULT_LINSTOR_URI) lin.connect() # Storage Pool Definition List spd_list_reply = jsons.dump( lin.storage_pool_dfn_list()[0].storage_pool_definitions) assert len(str( spd_list_reply[0])), "Empty Storage Pool Definition list" node_list = spd_list_reply[0] # print(node_list.proto_msg) spd_list = [] for spd in spd_list_reply: spd_list.append(spd["name"]) lin.disconnect() return spd_list except Exception as e: print(str(e))
def get_nodes(): try: lin = linstor.Linstor(DEFAULT_LINSTOR_URI) lin.connect() # Get Node List node_list_reply = jsons.dump(lin.node_list()[0].nodes) assert node_list_reply, "No LINSTOR nodes found" node_list = [] if node_list_reply: for node in node_list_reply: node_item = {} node_item["node_name"] = node["name"] # TODO (wap): Remove after testing # node_item["node_uuid"] = node['uuid'] node_item["node_address"] = ( node["net_interfaces"][0]["address"]) node_list.append(node_item) lin.disconnect() return node_list except Exception as e: print(str(e))
def parse_and_execute(self, pargs, is_interactive=False): rc = ExitCode.OK try: args = self.parse(pargs) local_only_cmds = [ self.cmd_list, MigrateCommands.cmd_dmmigrate, self._zsh_generator.cmd_completer, self.cmd_help ] # only connect if not already connected or a local only command was executed conn_errors = [] contrl_list = linstor.MultiLinstor.controller_uri_list( os.environ.get(KEY_LS_CONTROLLERS, "") + ',' + args.controllers) if self._linstorapi is None and args.func not in local_only_cmds: username = None password = None if args.user: username = args.user if args.password: password = args.password else: password = getpass.getpass("Enter Linstor password:"******"Client " + VERSION) self._linstorapi.username = username self._linstorapi.password = password self._linstorapi.certfile = args.certfile self._linstorapi.keyfile = args.keyfile self._linstorapi.cafile = args.cafile self._linstorapi.allow_insecure = args.allow_insecure_auth self._linstorapi.curl = args.curl for cmd in self._command_list: cmd._linstor = self._linstorapi self._linstorapi.connect() break except linstor.LinstorNetworkError as le: conn_errors.append(le) if len(conn_errors) == len(contrl_list): for x in conn_errors: self._report_linstor_error(x) rc = ExitCode.CONNECTION_ERROR else: if args.verbose and args.func != self.cmd_interactive: print("Connected to {h}".format( h=self._linstorapi.controller_host())) current_state = self._state_service.get_state() allowed_states = vars(args).get('allowed_states', [DefaultState]) always_allowed = vars(args).get('always_allowed', False) if always_allowed or current_state.__class__ in allowed_states: rc = args.func(args) else: sys.stderr.write( "Error: Command not allowed in state '{state.name}'\n". format(state=current_state)) rc = ExitCode.ILLEGAL_STATE except (ArgumentError, argparse.ArgumentTypeError, linstor.LinstorArgumentError) as ae: try: self.parse( list( itertools.takewhile(lambda x: not x.startswith('-'), pargs)) + ['-h']) except SystemExit: pass sys.stderr.write(ae.message + '\n') return ExitCode.ARGPARSE_ERROR except utils.LinstorClientError as lce: sys.stderr.write(lce.message + '\n') return lce.exit_code except linstor.LinstorNetworkError as le: self._report_linstor_error(le) rc = ExitCode.CONNECTION_ERROR except linstor.LinstorTimeoutError as le: self._report_linstor_error(le) rc = ExitCode.CONNECTION_TIMEOUT self._linstorapi.disconnect() self._linstorapi = None # should trigger reconnect in interactive mode except linstor.LinstorApiCallError as le: rc = self._controller_commands.handle_replies( args, le.all_errors()) except linstor.LinstorError as le: self._report_linstor_error(le) rc = ExitCode.UNKNOWN_ERROR finally: if self._linstorapi and not is_interactive: self._linstorapi.disconnect() return rc
def __init__(self, ip, port=3370, timeout=300): self.ip = ip self.port = port self.timeout = timeout self.url = "linstor://{ip}:{port}".format(ip=self.ip, port=self.port) self._lin_api = linstor.Linstor(self.url, timeout=self.timeout)