def run(self, line): """ Main rdmc command worker function :param line: entered command line :type line: list. """ if os.name == 'nt': if not ctypes.windll.shell32.IsUserAnAdmin() != 0: self.app.typepath.adminpriv = False elif not os.getuid() == 0: self.app.typepath.adminpriv = False nargv = [] if "--version" in line or "-V" in line: sys.stdout.write("""%(progname)s %(version)s\n""" % \ {'progname': versioning.__longname__, 'version': \ versioning.__version__}) sys.stdout.flush() sys.exit(self.retcode) (self.opts, nargv) = self.parser.parse_known_args(line) if self.opts.silent: if isinstance(self.opts.silent, (int, float)): new_tgt = os.devnull else: new_tgt = self.opts.silent sys.stdout = open(new_tgt, 'w') sys.stdout.write("Start stdout file.\n\n") self.app.verbose = self.opts.verbose try: #Test encoding functions are there Encryption.encode_credentials('test') self.app.set_encode_funct(Encryption.encode_credentials) self.app.set_decode_funct(Encryption.decode_credentials) self.encoding = True except redfish.hpilo.risblobstore2.ChifDllMissingError: self.encoding = False if self.opts.config is not None and len(self.opts.config) > 0: if not os.path.isfile(self.opts.config): self.retcode = ReturnCodes.CONFIGURATION_FILE_ERROR sys.exit(self.retcode) self.config.configfile = self.opts.config else: # Default locations: Windows: executed directory Linux: /etc/ilorest/redfish.conf self.config.configfile = os.path.join(os.path.dirname(sys.executable), \ 'redfish.conf') if os.name == 'nt' else '/etc/ilorest/redfish.conf' if not os.path.isfile(self.config.configfile): LOGGER.warning("Config file '%s' not found\n\n", self.config.configfile) self.config.load() cachedir = None if not self.opts.nocache: self.config.cachedir = os.path.join(self.opts.config_dir, 'cache') cachedir = self.config.cachedir if cachedir: self.app.cachedir = cachedir try: os.makedirs(cachedir) except OSError as ex: if ex.errno == errno.EEXIST: pass else: raise if self.opts.logdir and self.opts.debug: logdir = self.opts.logdir else: logdir = self.config.logdir if logdir and self.opts.debug: try: os.makedirs(logdir) except OSError as ex: if ex.errno == errno.EEXIST: pass else: raise if self.opts.debug: logfile = os.path.join(logdir, versioning.__shortname__ + '.log') # Create a file logger since we got a logdir lfile = logging.FileHandler(filename=logfile) formatter = logging.Formatter( "%(asctime)s %(levelname)s\t: %(message)s") lfile.setFormatter(formatter) lfile.setLevel(logging.DEBUG) LOGGER.addHandler(lfile) self.app.LOGGER = LOGGER if ("login" in line or any(x.startswith("--url") for x in line) or not line)\ and not (any(x.startswith(("-h", "--h")) for x in nargv) or "help" in line): self.app.logout() else: creds, enc = self._pull_creds(nargv) self.app.restore(creds=creds, enc=enc) self.opts.is_redfish = self.app.typepath.updatedefinesflag(\ redfishflag=self.opts.is_redfish) if nargv: try: self.retcode = self._run_command(self.opts, nargv) if self.app.cache: if ("logout" not in line) and ("--logout" not in line): self.app.save() self.app.redfishinst = None else: self.app.logout() except Exception as excp: self.handle_exceptions(excp) return self.retcode else: self.cmdloop(self.opts) if self.app.cache: self.app.save() else: self.app.logout()
def run(self, line): """ Main rdmc command worker function :param line: entered command line :type line: list. """ if os.name == 'nt': if not ctypes.windll.shell32.IsUserAnAdmin() != 0: self.app.typepath.adminpriv = False elif not os.getuid() == 0: self.app.typepath.adminpriv = False if "--version" in line or "-V" in line: CLI.printer("%s %s\n" % (versioning.__longname__, versioning.__version__)) sys.exit(self.retcode) all_opts = True help_indx = None help_list = ['-h', '--help', 'help'] for indx, elem in enumerate(line): if elem in help_list: help_indx = indx if '-' in elem: continue else: all_opts = False if all_opts and ('-h' in line) or ('--help' in line): line = ['-h'] elif help_indx: del line[help_indx] (self.opts, nargv) = self.parser.parse_known_args(line) if self.opts.redirect: try: sys.stdout = open("console.log", 'w') except: print("Unable to re-direct output for STDOUT.\n") else: print("Start of stdout file.\n\n") try: sys.stderr = open("console_err.log", 'w') except IOError: print("Unable to re-direct output for STDERR.\n") else: print("Start of stderr file.\n\n") self.app.verbose = self.ui.verbosity = self.opts.verbose try: #Test encoding functions are there Encryption.encode_credentials('test') self.app.set_encode_funct(Encryption.encode_credentials) self.app.set_decode_funct(Encryption.decode_credentials) self.encoding = True except redfish.hpilo.risblobstore2.ChifDllMissingError: self.encoding = False if self.opts.config is not None and len(self.opts.config) > 0: if not os.path.isfile(self.opts.config): self.retcode = ReturnCodes.CONFIGURATION_FILE_ERROR sys.exit(self.retcode) self.config.configfile = self.opts.config else: # Default locations: Windows: executed directory Linux: /etc/ilorest/redfish.conf self.config.configfile = os.path.join(os.path.dirname(sys.executable), \ 'redfish.conf') if os.name == 'nt' else '/etc/ilorest/redfish.conf' if not os.path.isfile(self.config.configfile): LOGGER.warning("Config file '%s' not found\n\n", self.config.configfile) self.config.load() cachedir = None if not self.opts.nocache: self.config.cachedir = os.path.join(self.opts.config_dir, 'cache') cachedir = self.config.cachedir if cachedir: self.app.cachedir = cachedir try: os.makedirs(cachedir) except OSError as ex: if ex.errno == errno.EEXIST: pass else: raise if self.opts.logdir and self.opts.debug: logdir = self.opts.logdir else: logdir = self.config.logdir if logdir and self.opts.debug: try: os.makedirs(logdir) except OSError as ex: if ex.errno == errno.EEXIST: pass else: raise if self.opts.debug: logfile = os.path.join(logdir, versioning.__shortname__ + '.log') # Create a file logger since we got a logdir lfile = logging.FileHandler(filename=logfile) formatter = logging.Formatter( "%(asctime)s %(levelname)s\t: %(message)s") lfile.setFormatter(formatter) lfile.setLevel(logging.DEBUG) LOGGER.addHandler(lfile) self.app.LOGGER = LOGGER if ("login" in line or any(x.startswith("--url") for x in line) or not line)\ and not (any(x.startswith(("-h", "--h")) for x in nargv) or "help" in line): self.app.logout() else: creds, enc = self._pull_creds(nargv) self.app.restore(creds=creds, enc=enc) self.opts.is_redfish = self.app.typepath.updatedefinesflag(\ redfishflag=self.opts.is_redfish) if nargv: try: self.retcode = self._run_command(self.opts, nargv) if self.app.cache: if ("logout" not in line) and ("--logout" not in line): self.app.save() self.app.redfishinst = None else: self.app.logout() except Exception as excp: self.handle_exceptions(excp) return self.retcode else: self.cmdloop(self.opts) if self.app.cache: self.app.save() else: self.app.logout()