def save_model(self,request,obj,form,change): print 'Saving model',obj,request.REQUEST super(HostAdmin, self).save_model(request, obj, form, change) obj.save() tags=obj.tags.all() print 'tags',tags tag_names=[] tag_names=request.REQUEST['tags'].split(',') print 'tags2',tag_names hosts=hostlists.expand_item(hostlists.range_split(obj.name)) if len(hosts) > 1: for hostname in hosts: host,created=Host.objects.get_or_create(name=hostname) host.save() if created: host.tags.set(*tag_names) else: for tag in tags_names: host.tags.add(tag) host.save() Host.objects.get(name=obj.name).delete() else: obj.save()
def run(host_range, command, username=None, password=None, sudo=False, script=None, timeout=None, sort=False, jobs=0, output_callback=None, parms=None, shuffle=False, chunksize=None, exit_on_error=False): """ Run a command on a hostlists host_range of hosts :param host_range: :param command: :param username: :param password: :param sudo: :param script: :param timeout: :param sort: :param jobs: :param output_callback: :param parms: :param shuffle: :param chunksize: :param exit_on_error: Exit as soon as one result comes back with a non 0 return code. >>> res=run(host_range='localhost',command="echo ok") >>> print(res[0].dump()) localhost ok 0 0 {'failures': [], 'total_host_count': 1, 'completed_host_count': 1} """ if not output_callback: output_callback = [callback.summarize_failures] utility.status_info(output_callback, 'Looking up hosts') # Expand the host range if we were passed a string host list if 'basestring' not in dir(__builtins__): # basestring is not in python3.x basestring = str if isinstance(host_range, basestring): hosts = hostlists.expand(hostlists.range_split(host_range)) else: hosts = host_range if shuffle: random.shuffle(hosts) utility.status_clear() results = ssh_results() if parms: results.parm = parms else: results.parm = {} if sudo and not password: for host in hosts: result = ssh_result() result.host = host result.err = 'Sudo password required' result.retcode = defaults.RUN_FAIL_NOPASSWORD results.append(result) results.parm['total_host_count'] = len(hosts) results.parm['completed_host_count'] = 0 results.parm['failures'] = hosts return results if jobs < 1: jobs = 1 if jobs > defaults.JOB_MAX: jobs = defaults.JOB_MAX # Set up our ssh client #status_info(output_callback,'Setting up the SSH client') client = fastSSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # load_system_host_keys slows things way down #client.load_system_host_keys() results.parm['total_host_count'] = len(hosts) results.parm['completed_host_count'] = 0 utility.status_clear() utility.status_info(output_callback, 'Spawning processes') if jobs > len(hosts): jobs = len(hosts) pool = multiprocessing.Pool(processes=jobs, initializer=init_worker) if not chunksize: if jobs == 1 or jobs >= len(hosts): chunksize = 1 else: chunksize = int(len(hosts) / jobs) - 1 if chunksize < 1: chunksize = 1 if chunksize > 10: chunksize = 10 results.parm['chunksize'] = chunksize if sort: map_command = pool.imap else: map_command = pool.imap_unordered if isinstance(output_callback, list) and \ callback.status_count in output_callback: callback.status_count(ssh_result(parm=results.parm)) # Create a process pool and pass the parameters to it utility.status_clear() utility.status_info( output_callback, 'Sending %d commands to each process' % chunksize) if callback.status_count in output_callback: callback.status_count(ssh_result(parm=results.parm)) try: for result in map_command( run_command, [ ( host, command, username, password, sudo, script, timeout, results.parm, client ) for host in hosts ], chunksize ): results.parm['completed_host_count'] += 1 result.parm = results.parm if isinstance(output_callback, list): for cb in output_callback: result = cb(result) else: # noinspection PyCallingNonCallable result = output_callback(result) results.parm = result.parm results.append(result) if exit_on_error and result.retcode != 0: break pool.close() except KeyboardInterrupt: print('ctrl-c pressed') pool.terminate() #except Exception as e: # print 'unknown error encountered',Exception,e # pass pool.terminate() if isinstance(output_callback, list) and \ callback.status_count in output_callback: utility.status_clear() return results
def run(host_range, command, username=None, password=None, sudo=False, script=None, timeout=None, sort=False, jobs=0, output_callback=None, parms=None, shuffle=False, chunksize=None, exit_on_error=False): """ Run a command on a hostlists host_range of hosts :param host_range: :param command: :param username: :param password: :param sudo: :param script: :param timeout: :param sort: :param jobs: :param output_callback: :param parms: :param shuffle: :param chunksize: :param exit_on_error: Exit as soon as one result comes back with a non 0 return code. >>> res=run(host_range='localhost',command="echo ok") >>> print(res[0].dump()) localhost ok 0 0 {'failures': [], 'total_host_count': 1, 'completed_host_count': 1} """ if not output_callback: output_callback = [callback.summarize_failures] utility.status_info(output_callback, 'Looking up hosts') # Expand the host range if we were passed a string host list if 'basestring' not in dir(__builtins__): # basestring is not in python3.x basestring = str if isinstance(host_range, basestring): hosts = hostlists.expand(hostlists.range_split(host_range)) else: hosts = host_range if shuffle: random.shuffle(hosts) utility.status_clear() results = ssh_results() if parms: results.parm = parms else: results.parm = {} if sudo and not password: for host in hosts: result = ssh_result() result.host = host result.err = 'Sudo password required' result.retcode = defaults.RUN_FAIL_NOPASSWORD results.append(result) results.parm['total_host_count'] = len(hosts) results.parm['completed_host_count'] = 0 results.parm['failures'] = hosts return results if jobs < 1: jobs = 1 if jobs > defaults.JOB_MAX: jobs = defaults.JOB_MAX # Set up our ssh client #status_info(output_callback,'Setting up the SSH client') client = fastSSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # load_system_host_keys slows things way down #client.load_system_host_keys() results.parm['total_host_count'] = len(hosts) results.parm['completed_host_count'] = 0 utility.status_clear() utility.status_info(output_callback, 'Spawning processes') if jobs > len(hosts): jobs = len(hosts) pool = multiprocessing.Pool(processes=jobs, initializer=init_worker) if not chunksize: if jobs == 1 or jobs >= len(hosts): chunksize = 1 else: chunksize = int(len(hosts) / jobs) - 1 if chunksize < 1: chunksize = 1 if chunksize > 10: chunksize = 10 results.parm['chunksize'] = chunksize if sort: map_command = pool.imap else: map_command = pool.imap_unordered if isinstance(output_callback, list) and \ callback.status_count in output_callback: callback.status_count(ssh_result(parm=results.parm)) # Create a process pool and pass the parameters to it utility.status_clear() utility.status_info(output_callback, 'Sending %d commands to each process' % chunksize) if callback.status_count in output_callback: callback.status_count(ssh_result(parm=results.parm)) try: for result in map_command(run_command, [(host, command, username, password, sudo, script, timeout, results.parm, client) for host in hosts], chunksize): results.parm['completed_host_count'] += 1 result.parm = results.parm if isinstance(output_callback, list): for cb in output_callback: result = cb(result) else: # noinspection PyCallingNonCallable result = output_callback(result) results.parm = result.parm results.append(result) if exit_on_error and result.retcode != 0: break pool.close() except KeyboardInterrupt: print('ctrl-c pressed') pool.terminate() #except Exception as e: # print 'unknown error encountered',Exception,e # pass pool.terminate() if isinstance(output_callback, list) and \ callback.status_count in output_callback: utility.status_clear() return results
def test_range_split(self): result = hostlists.range_split('foo1, foo[3-9]') expected_result = ['foo1', 'foo[3-9]'] self.assertListEqual(result, expected_result)