def run(self, *args, **kwargs): self.setup(*args, **kwargs) num_workers = int(kwargs['workers']) q = MultiprocessQueue() # multiprocess procs = [] for i in range(num_workers): p = Process(target=firmware_worker, args=(q, self.netconfig, self.group, self.release, self.fw, self.options, True)) p.daemon = True p.start() procs.append(p) # check to see if hosts is a file and determine appropriate argument array for d in get_array(kwargs['device']): q.put(d) # await all to be finished q.join() # clean up for i in range(num_workers): q.put(None)
def run(self, *args, **kwargs): init_loggers(**kwargs) options = {} for i in ('profile', 'port', 'prime', 'password', 'username', 'cache_group'): if i in kwargs and kwargs[i]: options[i] = kwargs[i] devices = get_array(kwargs['device']) if kwargs['randomise']: shuffle(devices) # get lock manager = Manager() mutex = manager.Lock() # map/reduce target_args = [ kwargs['config'], mutex, kwargs, options, kwargs['quiet'] ] res = self.map(api, devices, num_workers=kwargs['workers'], target_args=target_args) for hostname, status in self.fold(devices, res): # don't bother if not changed logging.info("%s: %s" % (hostname, status))
def _run( self, write_config=True, **kwargs): init_loggers(**kwargs) num_workers = int(kwargs['workers']) options = {} for i in ( 'password', 'username', 'cache_group' ): if kwargs[i]: options[i] = kwargs[i] devices = get_array( kwargs['device'] ) if kwargs['randomise']: shuffle(devices) # get lock manager = Manager() mutex = manager.Lock() # map/reduce target_args = [ kwargs['config'], mutex, kwargs['file_store_path'], options, kwargs['quiet'], True, kwargs['commit'] ] res = self.map( backup, devices, num_workers=kwargs['workers'], target_args=target_args ) for hostname, status in self.fold( devices, res, ignore=kwargs['donotreport'] ): config = status['config'] del status['config'] yield hostname, config, status return
def run(self, *args, **kwargs): init_loggers(**kwargs) options = {} for i in ('profile', 'port', 'prime', 'password', 'username'): if i in kwargs and kwargs[i]: options[i] = kwargs[i] report = {} # get lock manager = Manager() mutex = manager.Lock() devices = get_array(kwargs['device']) fix_only = [] if kwargs['fix_only']: fix_only = [i[0] for i in kwargs['fix_only']] target_args = [ kwargs['config'], mutex, kwargs['interfaces'], kwargs['baselines']['BASELINES'], kwargs['baselines']['EXCEPTIONS'], options, kwargs['fix'], fix_only ] # logging.debug("TARGET: %s" % (target_args,)) res = self.map(port_baseline, devices, num_workers=kwargs['workers'], target_args=target_args) for hostname, status in self.fold(devices, res): for d in status['deltas']: fixed = '-' if d['fixed'] == True: fixed = 'Y' elif d['fixed'] == False: fixed = 'N' profile = d['profile'] if profile == None: profile = 'UNKNOWN' delta = report_baseline_delta(d['delta']) output = False if kwargs[ 'delta_only'] == True and delta == {} else True if output: print("%-16s\t%-9s\t%26s\t%s\t%s" % (hostname, d['physical_port'], d['profile'], fixed, delta)) if not hostname in report: report[hostname] = {} report[hostname][d['physical_port']] = { 'profile': d['profile'], 'delta': d['delta'] } disk(report, kwargs['report_dir'])
def run(self, *args, **kwargs): self.setup(*args, **kwargs) # check to see if hosts is a file and determine appropriate argument array for d in get_array(kwargs['device']): device = self.netconfig.get(d, options=self.options) plan = upgrade_device(self.netconfig, device, self.group, self.release, self.fw, options=self.options, dry_run=kwargs['dry_run']) if kwargs['reload']: for e in reload_and_monitor(self.netconfig, device, options=self.options, wait=900, delay=15): if isinstance(e, str): print "%s" % (e, ) else: print device = e # check version actual = {} # recast to something more useable for v in device.system.get(): logging.error("%s" % (v, )) if 'number' in v: actual[v['number']] = v # match against plan for m in plan: if 'number' in m: stack = m['number'] if not m['to'] == actual[stack]['sw_version']: logging.error( "member %s is at incorrect firmware release: %s -> %s" % (stack, m, actual))
def run(self, *args, **kwargs): init_loggers(**kwargs) # get list of commands from stdin commands = [l.rstrip() for l in sys.stdin] options = {} for i in ('profile', 'password', 'username', 'cache_group', 'profile'): if i in kwargs and kwargs[i]: options[i] = kwargs[i] mode = None # set up enable acess if kwargs['enable']: mode = 'enable' if kwargs['confterm']: mode = 'config' devices = get_array(kwargs['device']) target_args = [kwargs['config'], mode, commands, options] res = self.map(pipe, devices, num_workers=kwargs['workers'], target_args=target_args) for hostname, status in self.fold(devices, res): for s in status: if kwargs['echo']: print "! %s: %s" % (hostname, s['command']) output = s['output'] # print "%s: %s" % (status,output) if kwargs['report']: print "%s\t%s" % (hostname, '\n'.join(output).strip()) else: print '\n'.join(output)
def run(self, *args, **kwargs): init_loggers(**kwargs) options = {} for i in ('profile', ): if i in kwargs and kwargs[i]: options[i] = kwargs[i] netconfig = NetConfig(kwargs['config']) for d in get_array(kwargs['device']): try: # get device device = netconfig.get(d, options=options) netconfig.connect(device, **options) # reload device and make sure it comes back! msg_per_loop = '.' for e in reload_and_monitor(netconfig, device, options=options, wait=int(kwargs['wait']), delay=int(kwargs['delay']), message_per_loop=msg_per_loop): if isinstance(e, str): if e == msg_per_loop: print "%s" % (e, ), else: print "%s" % (e, ) else: print device = e logging.info("%s" % (device.system.get(), )) except Exception, e: logging.error("%s: %s" % (type(e), e))
def parallel_run(self, target, items, num_workers=1, target_args=[]): # work queue q = MultiprocessQueue() # results queue p = MultiprocessQueue() # multiprocess procs = [] full_args = [q, p] full_args.extend(target_args) for i in range(int(num_workers)): t = Process(target=target, args=full_args) t.daemon = True t.start() procs.append(t) # check to see if hosts is a file and determine appropriate argument array items = get_array(items) for d in items: q.put(d) # get results n = 0 while n < len(items): yield p.get() n = n + 1 p.task_done() # await all to be finished q.join() # clean up for i in range(num_workers): q.put(None) return
def parse_file(**kwargs): # parse the file! use_delimiter = delimiter_map['ssv'] if delimiter_map.has_key(kwargs['delimiter']): use_delimiter = delimiter_map[kwargs['delimiter']] # logging.debug( "using delimiter " + kwargs['delimiter'] + " -> '" + use_delimiter + "'") else: use_delimiter = None logging.info("parsing file %s with %s (%s)" % (kwargs['input_file'], kwargs['delimiter'], use_delimiter)) fields = kwargs['field'] for l in get_array(kwargs['input_file']): logging.debug(" >: %s" % (l, )) f = [] if not use_delimiter: f = i.split(use_delimiter) else: m = sub(use_delimiter, "\x00", l) f = m.split("\x00") # logging.debug("splittting sub " + str(options['delimiter']) + ': ' + str(fields)) if len(f) < len(fields): logging.warning(' skipping line with wrong number of fields: %s', l[0:19]) continue this = {} # set defaults for this port logging.debug(" setting defaults:") for i in ('device', 'state', 'vlan'): if not i in kwargs: continue val = kwargs[i] if i in ('state', ): val = boolean(val) # logging.error("VAL: %s" % (val)) if i in kwargs: if i in ('state', 'vlan'): this[i] = val else: this[i] = str(val) logging.debug(" set %s as %s" % (i, val)) # set all optiosn from file logging.debug(" setting options: %s" % (fields)) for i in fields: val = f.pop(0) if not i == 'none': if i == 'alias' and val == '-': val = '' this[i] = val logging.debug(" set %s as %s" % (i, val)) for k in this.keys(): if this[k] == None: del this[k] yield this return