Beispiel #1
0
 def read_inventory_file(self):
     ''' get and check inventory information from file '''
     op = self.options
     if op.inventoryfile and os.path.isfile(op.inventoryfile):
         config = cfgparse()
         try:
             config.read(op.inventoryfile)
             groups = {}
             for x in config.sections():
                 if 'hosts' not in [k for k, w in config.items(x)]:
                     raise ForwardError("%s: No Hosts in Group %s." % (
                         self._InventoryOptionIllegal, x))
                 gp = {}
                 for k, w in config.items(x):
                     if k in self._inventory_list:
                         gp[k] = w
                     else:
                         raise ForwardError(
                             "%s: Unknown Item '%s' in Group '%s'." % (
                                 self._InventoryOptionIllegal, k, x))
                 groups[x] = gp
             return groups
         except Exception as e:
             raise ForwardError(
                 'Inventory Information File Illegal: %s.' % e)
     else:
         raise ForwardError('Inventory Information File Not Found.')
Beispiel #2
0
 def output(data, path, format):
     ''' runs a child defined method using the ouput_<action> pattern '''
     if format not in C.DEFAULT_OUTPUT_TYPES:
         raise ForwardError('Output Type Illegal.')
     output_function = getattr(Output, "output_%s" % format)
     if format == 'stdout':
         return output_function(data)
     if path and os.path.isdir(os.path.dirname(path)):
         return output_function(data, path)
     else:
         raise ForwardError('Output Path Illegal.')
Beispiel #3
0
 def _get_instance(self, host):
     ''' connect to a remote host '''
     if not host.model or host.model == list(C.DEFAULT_DEVICE_MODELS)[0]:
         raise ForwardError('No Device Model Defined.')
     if not host.vender or host.vender == list(C.DEFAULT_DEVICE_VENDERS)[0]:
         raise ForwardError('No Device Vender Defined.')
     if not host.remote_user:
         raise ForwardError('No Remote User Defined.')
     # use threading lock to ensure globals
     # lock.acquire()
     self.logger.debug('Connecting to %s ...' % host.ip)
     try:
         instance = getattr(
             import_module(
                 '%s.%s.%s' % (self._module, host.connect, host.model)),
             host.model.upper())(
                 ip=host.ip, port=host.remote_port,
                 timeout=self.options.timeout)
         login = instance.login(
             username=host.remote_user, password=host.conpass)
         self._status[host.ip] = {}
         if login['status']:
             self.logger.info(
                 '%s: Login Succeed.' % host.ip.ljust(self._just))
             activate = instance.privilegeMode(
                 secondPassword=host.actpass, deviceType=host.vender)
             self._status[host.ip]['connect'] = 'ok'
             if activate['status']:
                 self.logger.info(
                     '%s: Enable Succeed.' % host.ip.ljust(self._just))
                 self.logger.debug('Already Connected to %s.' % host.ip)
                 self._status[host.ip]['activate'] = 'ok'
                 self._success.append(host.ip)
                 return instance, host.ip
             else:
                 self._status[host.ip]['activate'] = 'failed'
                 self.logger.warn(
                     '%s: Enable Failed.' % host.ip.ljust(self._just))
         else:
             self._status[host.ip]['connect'] = 'failed'
             self._status[host.ip]['activate'] = 'skipped'
             self.logger.warn(
                 '%s: Login Failed.' % host.ip.ljust(self._just))
     except ForwardError:
         raise
     except ImportError:
         raise ForwardError('Unknown Model `%s`.' % host.model)
     except Exception as e:
         self.logger.debug('Cannot Connect to %s.' % host.ip)
         self.logger.error(repr(e))
         self.logger.debug(traceback.format_exc())
Beispiel #4
0
 def run(self):
     ''' use multi-threads to execute tasks '''
     op = self.options
     # check host inventory
     if not self.inventory:
         raise ForwardError('No Host Inventory Defined.')
     # use multi-threads to get task instances then run the play
     try:
         pool = ThreadPool(op.worker) if op.worker else ThreadPool()
         instances_t = pool.map(self._get_instance, self.inventory)
         instances = {}
         for x in instances_t:
             if x:
                 instances[x[1]] = x[0]
         result = TaskExecutor(
             instances=instances, script=op.script,
             args=op.args).run()
         display.display('\r\nSTATUS%s' % ('-' * 74))
         pool.map(self._print_task_status, self.inventory)
         return {'stdout': result, 'status': self._status}
     except ForwardError:
         raise
     except Exception as e:
         self.logger.error(repr(e))
         self.logger.debug(traceback.format_exc())
     finally:
         pool.close()
         pool.join()
         self.cleanup()
Beispiel #5
0
def check_loglevel(level):
    ''' check log level legitimacy '''
    if isinstance(level, str):
        if level.isalpha():
            level = level.upper()
            if level in logging._levelNames:
                level = logging._levelNames[level]
        elif level.isdigit():
            level = int(level)
    if not (isinstance(level, int) and level in logging._levelNames):
        raise ForwardError('Unknown Loglevel.')
    return level
Beispiel #6
0
 def read_play_file(self):
     ''' get and check play options from file '''
     op = self.options
     if op.playfile and os.path.isfile(op.playfile):
             config = cfgparse()
             try:
                 config.read(op.playfile)
                 items = {}
                 for x in config.sections():
                     if x not in self._play_matrix.keys():
                         raise ForwardError("%s: Unknown Section '%s'." % (
                             self._PlayOptionIllegal, x))
                     for k, w in config.items(x):
                         if k not in self._play_matrix[x]:
                             raise ForwardError(
                                 "%s: Unknown Item '%s' in Section '%s'."
                                 % (self._PlayOptionIllegal, k, x))
                         items[k] = w
                 return items
             except Exception as e:
                 raise ForwardError('Play Option File Illegal: %s.' % e)
     else:
         raise ForwardError('Play Option File Not Found.')
Beispiel #7
0
 def get_inventory(self):
     ''' get a inventory list of hosts '''
     try:
         inventory = []
         gps = self.read_inventory_file()
         for n, g in gps.items():
             # get host list
             hosts = []
             try:
                 g['hosts'] = eval(g['hosts']) \
                     if isinstance(g['hosts'], str) else g['hosts']
                 hosts = get_ip_list(g['hosts'])
                 g.pop('hosts')
             except:
                 raise ForwardError("%s: Bad Hosts in Group '%s'." % (
                     self._InventoryOptionIllegal, n))
             # get password requirement
             ask_pass = C.DEFAULT_ASK_PASS
             ask_activate = C.DEFAULT_ASK_ACTIVATE
             share = C.DEFAULT_SHARE
             if 'ask_pass' in g.keys():
                 ask_pass = boolean(g['ask_pass'])
                 g.pop('ask_pass')
             if 'ask_activate' in g.keys():
                 ask_activate = boolean(g['ask_activate'])
                 g.pop('ask_activate')
             if 'share' in g.keys():
                 share = boolean(g['share'])
                 g.pop('share')
             # get host inventory
             display.display(
                 u'GROUP %s%s' % (n.upper(), '-' * (80 - len(n) - 6)))
             if share:
                 g['conpass'], g['actpass'] = CLI.ask_passwords(
                     ask_pass, ask_activate)
                 for h in hosts:
                     g['ip'] = h
                     host = HostOption(**g)
                     inventory.append(host)
             else:
                 for h in hosts:
                     g['ip'] = h
                     display.display(u'[%s]' % h.ljust(C.DEFAULT_IP_JUST))
                     g['conpass'], g['actpass'] = CLI.ask_passwords(
                         ask_pass, ask_activate)
                     host = HostOption(**g)
                     inventory.append(host)
         return inventory
     except ForwardError:
         raise
Beispiel #8
0
def makedirs_safe(path, mode=None):
    """ safe way to create dirs in muliprocess/thread environments """
    path = purepath(path)
    if not os.path.exists(path):
        try:
            if mode:
                os.makedirs(path, mode)
            else:
                os.makedirs(path)
        except OSError as e:
            if e.errno != EEXIST:
                raise ForwardError(
                    'Unable to create local directories(%s): %s' %
                    (path, str(e)))
Beispiel #9
0
 def run(self):
     ''' run a forward task '''
     op = self.options
     inv = self.inventory
     # set logger
     try:
         if op.logfile:
             self.logger = add_file_logger(level=op.loglevel,
                                           file=op.logfile,
                                           default=(not op.no_std_log))
         elif op.loglevel != C.DEFAULT_LOGLEVEL:
             self.logger = set_loglevel_only(level=op.loglevel)
     except ForwardError:
         raise
     except Exception as e:
         self.logger.error(repr(e))
         self.logger.debug(traceback.format_exc())
     # check ip inventory
     for x in inv:
         if not check_ip_format(x.ip):
             raise ForwardError('Host IP Illegal: %s.' % x)
     # now execute tasks
     self._tqm = None
     result = None
     try:
         self._tqm = TaskQueueManager(options=op,
                                      inventory=inv,
                                      logger=self.logger)
         result = self._tqm.run()
     except ForwardError:
         raise
     except Exception as e:
         self.logger.error(repr(e))
         self.logger.debug(traceback.format_exc())
     finally:
         if self._tqm:
             self._tqm.cleanup()
     # results output
     if result:
         try:
             Output.output(result, op.outfile, op.out)
         except ForwardError:
             raise
         except Exception as e:
             self.logger.error(repr(e))
             self.logger.debug(traceback.format_exc())
     return result
Beispiel #10
0
 def __init__(
         self, ip=list(C.LOCALHOST)[2],
         vender=list(C.DEFAULT_DEVICE_VENDERS)[0],
         model=list(C.DEFAULT_DEVICE_MODELS)[0],
         connect=C.DEFAULT_TRANSPORT, conpass=None, actpass=None,
         remote_port=C.DEFAULT_REMOTE_PORT,
         remote_user=C.DEFAULT_REMOTE_USER):
     super(HostOption, self).__init__()
     try:
         self.ip = str(ip)
         self.vender = str(vender)
         self.model = str(model)
         self.connect = str(connect)
         self.conpass = str(conpass) if conpass else ''
         self.actpass = str(actpass) if actpass else ''
         self.remote_port = int(remote_port)
         self.remote_user = str(remote_user)
     except TypeError as e:
         raise ForwardError('Bad Host Option: %s.' % e)
Beispiel #11
0
 def __init__(
         self, worker=C.DEFAULT_WORKER,
         script=os.path.join(C.DEFAULT_HOME, C.DEFAULT_SCRIPT_PATH),
         args=None, no_std_log=False, loglevel=C.DEFAULT_LOGLEVEL,
         logfile=os.path.join(C.DEFAULT_HOME, C.DEFAULT_FORWARD_LOG_PATH),
         timeout=30, out=C.DEFAULT_OUTPUT,
         outfile=os.path.join(C.DEFAULT_HOME, C.DEFAULT_OUT_PATH)):
     super(PlayOption, self).__init__()
     try:
         self.worker = int(worker)
         self.script = str(script)
         self.args = args
         self.loglevel = str(loglevel)
         self.logfile = str(logfile)
         self.no_std_log = boolean(no_std_log)
         self.out = str(out)
         self.outfile = str(outfile)
         self.timeout = int(timeout)
     except TypeError as e:
         raise ForwardError('Bad Play Option: %s.' % e)
Beispiel #12
0
def add_file_logger(level=C.DEFAULT_LOGLEVEL,
                    file=C.DEFAULT_FORWARD_LOG_PATH,
                    default=1):
    ''' add a file logger '''
    # check file legitimacy
    if file and ((os.path.exists(file) and os.access(file, os.W_OK))
                 or os.access(os.path.dirname(file), os.W_OK)):
        # add log file handler
        handler = TimedRotatingFileHandler(filename=file,
                                           when='d',
                                           backupCount=1)
        # set log format
        handler.formatter = formatter
        logger.addHandler(handler)
        if not boolean(default):
            logger.removeHandler(default_handler)
        # set log level
        level = check_loglevel(level)
        logger.setLevel(level)
    else:
        raise ForwardError('Log File Illegal.')
    return logger
Beispiel #13
0
def load_config_file():
    ''' Load Config File order(first found is used):
        ENV, CWD, CWD/.conf, /apps/conf/forward
    '''
    p = ConfigParser()

    path0 = os.getenv("FORWARD_CONFIG", None)
    if path0 is not None:
        path0 = os.path.expanduser(path0)
        if os.path.isdir(path0):
            path0 += '/forward.cfg'
    path1 = os.path.join(os.path.dirname(sys.path[0]), 'conf/forward.cfg')

    for path in [path0, path1]:
        if path is not None and os.path.exists(path):
            try:
                p.read(path)
            except ConfigParser.Error as e:
                raise ForwardError(
                    "Error reading configuration file: \n{0}".format(e))
            return p, path
    return None, ''
Beispiel #14
0
 def run(self):
     ''' use Runner lib to do SSH things '''
     op = self.options
     # set logger: may raise ForwardError if illegal args
     if op.logfile:
         self.logger = add_file_logger(level=op.loglevel,
                                       file=op.logfile,
                                       default=(not op.no_std_log))
     elif op.loglevel != C.DEFAULT_LOGLEVEL:
         self.logger = set_loglevel_only(level=op.loglevel)
     # get inventory of hosts
     inventory = []
     hosts = op.hosts
     try:
         hosts = eval(hosts) \
             if isinstance(hosts, str) else hosts
         hosts = get_ip_list(hosts)
     except:
         raise ForwardError('Bad Hosts.')
     if op.share:
         # get connection passwords for all
         conpass, actpass = CLI.ask_passwords(op.ask_pass, op.ask_activate)
         for h in hosts:
             host = HostOption(ip=h,
                               vender=op.vender,
                               model=op.model,
                               connect=op.connect,
                               conpass=conpass,
                               actpass=actpass,
                               remote_port=op.remote_port,
                               remote_user=op.remote_user)
             inventory.append(host)
     else:
         for h in hosts:
             display.display(u'[%s]' % h.ljust(C.DEFAULT_IP_JUST))
             # get connection passwords one by one
             conpass, actpass = CLI.ask_passwords(op.ask_pass,
                                                  op.ask_activate)
             host = HostOption(ip=h,
                               vender=op.vender,
                               model=op.model,
                               connect=op.connect,
                               conpass=conpass,
                               actpass=actpass,
                               remote_port=op.remote_port,
                               remote_user=op.remote_user)
             inventory.append(host)
     # now execute tasks
     self._tqm = None
     result = None
     try:
         self._tqm = TaskQueueManager(options=op,
                                      inventory=inventory,
                                      logger=self.logger)
         result = self._tqm.run()
     except ForwardError:
         raise
     except Exception as e:
         self.logger.error(repr(e))
         self.logger.debug(traceback.format_exc())
     finally:
         if self._tqm:
             self._tqm.cleanup()
     # results output
     if result:
         try:
             Output.output(result, op.outfile, op.out)
         except ForwardError:
             raise
         except Exception as e:
             self.logger.error(repr(e))
             self.logger.debug(traceback.format_exc())
     return result
Beispiel #15
0
def to_string(data):
    try:
        data = str(data)
        return data
    except:
        raise ForwardError('Output FORMAT Illegal.')
Beispiel #16
0
 def load_args(args):
     ''' load script arguments '''
     d = eval(args) if isinstance(args, str) else args
     if not isinstance(d, dict):
         raise ForwardError('Script Argument Illegal.')
     return d
Beispiel #17
0
 def load_module(path):
     ''' load script module '''
     if not os.path.isfile(path):
         raise ForwardError('Script File Not Found.')
     return purepath(path)