def get_backend_proxy_module_name(config): backend_type = config.get_backend_type(config.get_proxy_name()).lower() try: return backends_package_path + '.' + backend_modules[backend_type] except KeyError: abort("Unsupported back-end type '%s' (supported types: %s)" %\ (backend_type, ','.join(supported_proxy_types)))
def _set_backend_proxy_specific_attributes(self, backend_attributes, proxy_name): try: for attribute in backend_attributes.keys(): backend_attributes[attribute] = self._config.get(proxy_name, attribute) except NoOptionError: abort("Required option '%s' is missing in section '%s'." % (attribute, proxy_name)) except NoSectionError: abort("Section '%s' is missing in configuration" % proxy_name)
def execute(self): status = 0 self.debug("Executing command: '%s'" % (' '.join(self.action_cmd)), 1) try: self.proc = Popen(self.action_cmd, shell=False, stdout=PIPE, stderr=STDOUT) except OSError, details: abort('Failed to execute %s action: %s' % (self.action, details)) status = 1
def _read_configuration_from_file(self, config, config_file): try: config.readfp(open(config_file)) except IOError as (errno, errmsg): if errno == 2: abort("Configuration file (%s) is missing." % config_file) else: abort("Error opening configuration file (%s): %s (errno=%s)" % (config_file, errmsg, errno))
def checkStatus(self): optInfo = () try: retcode, output = self._getStatusOutputOrRetry(self.action) output = self._filter_command_output(output) if retcode != 0 and len(output) != 0: self.debug("ERROR: %s action, exit code %s. Command output:\n%s\n%s\n%s" % \ (self.action, retcode, self.cmd_output_start, output, self.cmd_output_end)) # In some cases we are OK when failure happens. for failurePattern in self.failureOkMsgs: output_regexp = re.compile(failurePattern, re.MULTILINE) matcher = output_regexp.search(output) if matcher: retcode = 0 self.debug('... But we are OK to proceed. Setting retcode to 0.') break else: # Need to check if the command is expected to return an output when successful success = False if len(output) == 0: success = True if self.successMsgs and len(output) > 0: success = False for successPattern in self.successMsgs: output_regexp = re.compile(successPattern, re.MULTILINE) matcher = output_regexp.search(output) if matcher: # Return only the first capturing group if output_regexp.groups > 0: optInfo = matcher.groups() success = True break if success: self.debug("SUCCESS: %s action completed successfully." % self.action, 1) if len(output) > 0: self.debug('Command output:\n%s\n%s\n%s' % (self.cmd_output_start, output, self.cmd_output_end), 2) else: self.debug("ERROR: %s action, exit code %s. But a failure case detected after parsing the output. Command output:\n%s\n%s\n%s" % \ (self.action, retcode, self.cmd_output_start, output, self.cmd_output_end)) retcode = -1 self.debug('exit code was reset to %i' % retcode) except OSError as ex: abort('Failed to execute %s action: %s' % (self.action, ex)) if self.action in ['map', 'delete'] and retcode == 255 and not output.strip(): retcode = 0 self.debug('map and delete actions (command exited with 255 and no output returned) - exit code was reset to %i.' % retcode) if retcode == 255: if self.action in ['map', 'delete'] and not output.strip(): retcode = 0 self.debug('map and delete actions (no output returned) - exit code was reset to %i.' % retcode) if self.action in ['unmap']: retcode = 0 self.debug('unmap action - exit code was reset to %i.' % retcode) return retcode, optInfo
def get_proxy_names(self): """Return all proxy names as list from the comma separated list.""" try: return self._config.get(CONFIG_MAIN_SECTION, "iscsi_proxies").split(",") except ValueError: abort( "Invalid value specified for 'iscsi_proxies' (section %s) (must be a comma-separated list)" % CONFIG_MAIN_SECTION )
def rebase(self): if self.proxy.newLunRequired('rebase'): # TODO: generate a UUID based on creation timestamp as in PDisk new_lun_uuid = str(uuid.uuid4()) self.getSize() self.associatedLUN = LUN(new_lun_uuid, size=self.size, proxy=self.proxy) if self.associatedLUN.create() != 0: abort('An error occured creating a new LUN for rebasing %s' % (self.uuid)) else: self.associatedLUN = self # To simplify returned value status, rebasedLUN = self._execute_action('rebase') if status != 0: abort('Failure to rebase LUN %s' % (self.uuid)) # Don't return directly self.associatedLUN but use optional information # returned by action execution to allow reformatting if needed. return rebasedLUN
def _execute_action(self, action): # Ensure that the local variables are always initialized. optInfos = () status = 0 for backendCmd in self._getBackendCmd(action): if not backendCmd: abort("Action '%s' not implemented by back-end type '%s'" % \ (action, self._getBackendType())) self._detokenizeBackendCmd(backendCmd) status, optInfo = self._runCommand(backendCmd) if status != 0: if backendCmd.run_on_failure(): self._runRollback(backendCmd) if optInfo: if optInfos: optInfos += optInfo else: optInfos = optInfo break if optInfo: if optInfos: optInfos += optInfo else: optInfos = optInfo # Append an optional additional value built from LUN attributes, if necessary if status == 0 and action in self.additional_opt_info: if not optInfos: optInfos = () optInfos += self._detokenize(self.additional_opt_info[action]), if isinstance(optInfos, (basestring,)): optInfos = (optInfos,) if status == 0 and optInfos: optInfosStr = self.proxy.formatOptInfos(action, optInfos) else: optInfosStr = ' '.join(optInfos) return status, optInfosStr
def _get_mgt_info_from_config(self, backend_section): """Return a tuple with the appropriated management information.""" mgt_user_name = None mgt_user_private_key = None if backend_section != "local": try: mgt_user_name = self._config.get(backend_section, "mgt_user_name") except: try: mgt_user_name = self._config.get(CONFIG_MAIN_SECTION, "mgt_user_name") except: abort("Undefined user name to connect to the proxy.") try: mgt_user_private_key = self._config.get(backend_section, "mgt_user_private_key") except: try: mgt_user_private_key = self._config.get(CONFIG_MAIN_SECTION, "mgt_user_private_key") except: abort("Undefined SSH private key to connect to the proxy.") return mgt_user_name, mgt_user_private_key
parser = OptionParser() options, args = parse_args(parser) ch = ConfigHolder(config_file_name=options.config_file, verbosity=options.verbosity) initialize_logger(ch.get(defaults.CONFIG_MAIN_SECTION, 'log_direction'), ch.verbosity) if options.action in VALID_ACTIONS: if len(args) < VALID_ACTIONS[options.action]: print_detail("Insufficient argument provided (%d required)" % VALID_ACTIONS[options.action]) parser.print_help() abort("") else: if options.action: print_detail("Invalid action requested (%s)\n" % options.action) else: print_detail("No action specified\n") parser.print_help() abort("") backend_proxy = PdiskBackendProxyFactory.createBackendProxy(ch) # Execute requested action status = 0 if options.action == 'check':
def getTurl(self): status, self.turl = self._execute_action('getturl') if status != 0: abort('Failure to retrieve Transport URL of %s' % (self.uuid)) return self.turl
def getSize(self): status, self.size = self._execute_action('size') if status != 0: abort('Failure to retrieve size of LUN %s' % (self.uuid)) return status
def _get_command(self, action): if action in self.backend_cmds: return self._buildCmd(self.backend_cmds[action]) else: abort("Internal error: action '%s' unknown" % (action))
def _get_backend_actions(self, lun_action): try: return self.lun_backend_cmd_mapping[lun_action] except KeyError: abort("Internal error: LUN action '%s' unknown" % (lun_action))
options, args = parser.parse_args() return options, args parser = OptionParser() options, args = parse_args(parser) ch = ConfigHolder(config_file_name=options.config_file, verbosity=options.verbosity) initialize_logger(ch.get(defaults.CONFIG_MAIN_SECTION, 'log_direction'), ch.verbosity) if options.action in VALID_ACTIONS: if len(args) < VALID_ACTIONS[options.action]: print_detail("Insufficient argument provided (%d required)" % VALID_ACTIONS[options.action]) parser.print_help() abort("") else: if options.action: print_detail("Invalid action requested (%s)\n" % options.action) else: print_detail("No action specified\n") parser.print_help() abort("") backend_proxy = PdiskBackendProxyFactory.createBackendProxy(ch) # Execute requested action status = 0 if options.action == 'check':
def get_backend_type(self, proxy_name): try: return self._config.get(proxy_name, "type") except: abort("Section '%s' or required attribute 'type' missing" % (proxy_name))