Esempio n. 1
0
    def convertcurly(self):
        if "{" in self.nodes:
            self.nodes = self.nodes.replace("{", "[")
            self.nodes = self.nodes.replace("}", "]")
            self.nodes = self.nodes.replace("..", "-")

        return hostlists.expand(self.nodes)
Esempio n. 2
0
def converttonewline(hostlist):
  fw=open("/tmp/hl-l","w")
  hostlist=args.expand2line
  xl=[]
  xl=(hostlists.expand(hostlist))
  fw.write('\n'.join(xl))
  print type('\n'.join(xl))
Esempio n. 3
0
def converttocoma(hostlist):
  fw=open("/tmp/hl-c","w")
  hostlist=args.expand2comma
  xl=[]
  xl=(hostlists.expand(hostlist))
  fw.write(', '.join(xl))
  fw.close()
Esempio n. 4
0
def converttonewline(hostlist):
    fw = open("/tmp/hl-l", "w")
    hostlist = args.expand2line
    xl = []
    xl = (hostlists.expand(hostlist))
    fw.write('\n'.join(xl))
    print type('\n'.join(xl))
Esempio n. 5
0
def converttocoma(hostlist):
    fw = open("/tmp/hl-c", "w")
    hostlist = args.expand2comma
    xl = []
    xl = (hostlists.expand(hostlist))
    fw.write(', '.join(xl))
    fw.close()
Esempio n. 6
0
 def test_expand_file(self):
     with open('test_expand_file.hostlist', 'w') as fh:
         fh.write('foo[1-2]\n')
     result = hostlists.expand(['file:test_expand_file.hostlist'])
     expected_result = ['foo1', 'foo2']
     os.remove('test_expand_file.hostlist')
     result.sort()
     self.assertListEqual(result, expected_result)
Esempio n. 7
0
 def test_expand_file(self):
     with open('test_expand_file.hostlist', 'w') as fh:
         fh.write('foo[1-2]\n')
     result = hostlists.expand(['file:test_expand_file.hostlist'])
     expected_result = ['foo1', 'foo2']
     os.remove('test_expand_file.hostlist')
     result.sort()
     self.assertListEqual(result, expected_result)
Esempio n. 8
0
 def test_expand(self):
     """
     Expand a list of lists and set operators into a final host lists
     >>> hostlists.expand(['foo[01-10]','-','foo[04-06]'])
     ['foo09', 'foo08', 'foo07', 'foo02', 'foo01', 'foo03', 'foo10']
     >>>
     """
     result = hostlists.expand(['foo[01-10]', '-', 'foo[04-06]'])
     expected_result = [
         'foo09', 'foo08', 'foo07', 'foo02', 'foo01', 'foo03', 'foo10']
     result.sort()
     expected_result.sort()
     self.assertLessEqual(result, expected_result)
Esempio n. 9
0
 def test_expand(self):
     """
     Expand a list of lists and set operators into a final host lists
     >>> hostlists.expand(['foo[01-10]','-','foo[04-06]'])
     ['foo09', 'foo08', 'foo07', 'foo02', 'foo01', 'foo03', 'foo10']
     >>>
     """
     result = hostlists.expand(['foo[01-10]', '-', 'foo[04-06]'])
     expected_result = [
         'foo09', 'foo08', 'foo07', 'foo02', 'foo01', 'foo03', 'foo10'
     ]
     result.sort()
     expected_result.sort()
     self.assertLessEqual(result, expected_result)
Esempio n. 10
0
def pingtest(h):
  success = list()
  fail = list()
  #with settings(warn_only=True):
  #with settings(hide('stderr', 'warnings','stdout'),warn_only=True):
  with settings(hide('everything'),warn_only=True):
    nodes = hostlists.expand(h)
    for each in nodes:
      result = local("ping -c1 "+each)
      if result.return_code == 0:
        success.append(each)
      else:
        fail.append(each)
    logging.error(fail)
    logging.error(success)
    logger.addHandler(mail_handler)
    print "success:",len(success),success,"failed:",len(fail),fail
Esempio n. 11
0
 def test_expand_invalid_plugin(self):
     with self.assertRaises(hostlists.HostListsError):
         hostlists.expand(['boozle:bar'])
Esempio n. 12
0
 def expand(self):
     return hostlists.expand(self.hl)
Esempio n. 13
0
 def __init__(self):
     result = hostlists.expand(self.range_list)
     self.assertIsInstance(result, list)
     self.assertListEqual(result, self.expected_result)
Esempio n. 14
0
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
Esempio n. 15
0
 def test_expand_dnsip(self):
     result = hostlists.expand(['dnsip:yahoo.com'])
     self.assertGreater(len(result), 0)
Esempio n. 16
0
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
Esempio n. 17
0
 def test_expand_invalid_plugin(self):
     with self.assertRaises(hostlists.HostListsError):
         hostlists.expand(['boozle:bar'])
Esempio n. 18
0
 def converttonewline(self):
     hl = []
     hl = hostlists.expand(self.hosts)
     print '\n'.join(hl)
Esempio n. 19
0
 def converttocomma(self):
   hl=[]
   hl=hostlists.expand(self.hosts)
   print ', '.join(hl)
Esempio n. 20
0
 def __init__(self):
     result = hostlists.expand(self.range_list)
     self.assertIsInstance(result, list)
     self.assertListEqual(result, self.expected_result)
Esempio n. 21
0
 def test_expand_dnsip(self):
     result = hostlists.expand(['dnsip:yahoo.com'])
     self.assertGreater(len(result), 0)
Esempio n. 22
0
 def converttonewline(self):
   hl=[]
   hl=hostlists.expand(self.hosts)
   print '\n'.join(hl)
Esempio n. 23
0
 def converttocomma(self):
     hl = []
     hl = hostlists.expand(self.hosts)
     print ', '.join(hl)