def test_inventory_tag_add_for_all_hosts(self): tags = ( "spam-spam-spam", "spam-spam-eggs", ) tagsf = ( "spam-spam-spam-eggs", "spam-spam-eggs-spam", ) tagfile = op.join(fixtures, "tags") with open(tagfile, "w") as f: for x in tagsf: f.write("{}\n".format(x)) invTag = inventory.InventoryTag(db_basedir=inventory_dir, action="add", tags=tags, tagfile=tagfile) invTag.run() invList = inventory.InventoryList(db_basedir=inventory_dir) failed = False for host, taglist in invList.entries(): for x in tagsf + tags: if x not in taglist: failed = True break if failed: break os.remove(tagfile) if failed: self.fail()
def test_inventory_list_by_host_hosts_hostfile(self): hosts = ( "shell1", "shell4", ) hostsf = ( "web1", "web2", "web3", ) hostfile = op.join(fixtures, "hosts") with open(hostfile, "w") as f: for x in hostsf: f.write("{}\n".format(x)) invList = inventory.InventoryList(db_basedir=inventory_dir, hosts=hosts, hostfile=hostfile) entries = invList.entries() db = {host: sorted(tags) for host, tags in entries} import itertools expected_db = { host: sorted(self.db[host]) for host in itertools.chain(hostsf, hosts) } self.assertEqual(db, expected_db) os.remove(hostfile)
def test_inventory_list_by_tag_hosts(self): tags = ( "web", "shell", ) invList = inventory.InventoryList(db_basedir=inventory_dir, istag=True, hosts=tags) entries = invList.entries() db = {host: sorted(tags) for host, tags in entries} expected_db = self._gen_expected_db_for_tags(tags) self.assertEqual(db, expected_db)
def test_inventory_list_by_host_hosts(self): hosts = ( "web1", "web2", "web3", ) invList = inventory.InventoryList(db_basedir=inventory_dir, hosts=hosts) entries = invList.entries() db = {host: sorted(tags) for host, tags in entries} expected_db = {host: sorted(self.db[host]) for host in hosts} self.assertEqual(db, expected_db)
def test_inventory_host_del_hosts(self): hosts = ( "web1", "shell1", ) invHost = inventory.InventoryHost(db_basedir=inventory_dir, action="del", hosts=hosts) invHost.run() invList = inventory.InventoryList(db_basedir=inventory_dir, hosts=hosts) expected_hosts = tuple(invList.host_entries()) self.assertTupleEqual(expected_hosts, ())
def test_inventory_host_add_hosts(self): hosts = ( "spam", "eggs", "foo", ) invHost = inventory.InventoryHost(db_basedir=inventory_dir, action="add", hosts=hosts) invHost.run() invList = inventory.InventoryList(db_basedir=inventory_dir) expected_hosts = tuple(x for x in invList.host_entries() if x in hosts) self.assertEqual(sorted(hosts), sorted(expected_hosts))
def test_inventory_tag_add(self): tags = ( "spam-spam-spam", "spam-spam-eggs", ) tagsf = ( "spam-spam-spam-eggs", "spam-spam-eggs-spam", ) hosts = ( "loadbalancer1", "loadbalancer2", "shell2", ) hostsf = ( "web2", "web3", ) tagfile = op.join(fixtures, "tags") with open(tagfile, "w") as f: for x in tagsf: f.write("{}\n".format(x)) hostfile = op.join(fixtures, "hosts") with open(hostfile, "w") as f: for x in hostsf: f.write("{}\n".format(x)) invTag = inventory.InventoryTag(db_basedir=inventory_dir, action="add", tags=tags, tagfile=tagfile, hosts=hosts, hostfile=hostfile) invTag.run() invList = inventory.InventoryList(db_basedir=inventory_dir, hosts=hosts + hostsf) failed = False for host, taglist in invList.entries(): if host not in hosts + hostsf: failed = True break for x in tagsf + tags: if x not in taglist: failed = True break if failed: break os.remove(tagfile) os.remove(hostfile) if failed: self.fail()
def test_inventory_list_by_tag_hostfile(self): tags = ( "web", "shell", ) tagfile = op.join(fixtures, "tags") with open(tagfile, "w") as f: for x in tags: f.write("{}\n".format(x)) invList = inventory.InventoryList(db_basedir=inventory_dir, istag=True, hostfile=tagfile) entries = invList.entries() db = {host: sorted(tags) for host, tags in entries} expected_db = self._gen_expected_db_for_tags(tags) self.assertEqual(db, expected_db) os.remove(tagfile)
def test_inventory_host_del_hostfile(self): hosts = ( "loadbalancer3", "loadbalancer4", ) hostfile = op.join(fixtures, "hosts") with open(hostfile, "w") as f: for x in hosts: f.write("{}\n".format(x)) invHost = inventory.InventoryHost(db_basedir=inventory_dir, action="del", hostfile=hostfile) invHost.run() invList = inventory.InventoryList(db_basedir=inventory_dir, hosts=hosts) expected_hosts = tuple(invList.host_entries()) self.assertTupleEqual(expected_hosts, ()) os.remove(hostfile)
def test_inventory_host_add_hostfile(self): hosts = ( "spam-new", "eggs-new", "foo-new", ) hostfile = op.join(fixtures, "hosts") with open(hostfile, "w") as f: for x in hosts: f.write("{}\n".format(x)) invHost = inventory.InventoryHost(db_basedir=inventory_dir, action="add", hostfile=hostfile) invHost.run() invList = inventory.InventoryList(db_basedir=inventory_dir) expected_hosts = tuple(x for x in invList.host_entries() if x in hosts) self.assertEqual(sorted(hosts), sorted(expected_hosts)) os.remove(hostfile)
def test_inventory_tag_del_all_tags(self): hosts = ( "web3", "shell1", ) hostsf = ( "shell2", "loadbalancer1", ) hostfile = op.join(fixtures, "hosts") with open(hostfile, "w") as f: for x in hostsf: f.write("{}\n".format(x)) invHost = inventory.InventoryHost(db_basedir=inventory_dir, action="del", all=True, hosts=hosts, hostfile=hostfile) invHost.run() invList = inventory.InventoryList(db_basedir=inventory_dir, hosts=hosts + hostsf) for host, htags in invList.entries(): self.assertEqual(htags, ()) os.remove(hostfile)
def commandline(cls, args): """Configure remote system""" if (args.parallel and args.parallel != 1) or args.jobs: if args.timestamp: cdist.log.setupTimestampingParallelLogging() else: cdist.log.setupParallelLogging() elif args.timestamp: cdist.log.setupTimestampingLogging() log = logging.getLogger("config") # No new child process if only one host at a time. if args.parallel == 1: log.debug("Only 1 parallel process, doing it sequentially") args.parallel = 0 if args.parallel: import signal signal.signal(signal.SIGTERM, mp_sig_handler) signal.signal(signal.SIGHUP, mp_sig_handler) cls._check_and_prepare_args(args) failed_hosts = [] time_start = time.time() cls.construct_remote_exec_copy_patterns(args) base_root_path = cls.create_base_root_path(args.out_path) hostcnt = 0 cfg = cdist.configuration.Configuration(args) configuration = cfg.get_config(section='GLOBAL') if args.tag or args.all_tagged_hosts: inventory.determine_default_inventory_dir(args, configuration) if args.all_tagged_hosts: inv_list = inventory.InventoryList( hosts=None, istag=True, hostfile=None, db_basedir=args.inventory_dir) else: inv_list = inventory.InventoryList( hosts=args.host, istag=True, hostfile=args.hostfile, db_basedir=args.inventory_dir, has_all_tags=args.has_all_tags) it = inv_list.entries() else: it = itertools.chain(cls.hosts(args.host), cls.hosts(args.hostfile)) process_args = [] if args.parallel: log.trace("Processing hosts in parallel") else: log.trace("Processing hosts sequentially") for entry in it: if isinstance(entry, tuple): # if configuring by specified tags host = entry[0] host_tags = entry[1] else: # if configuring by host then check inventory for tags host = entry inventory.determine_default_inventory_dir(args, configuration) inv_list = inventory.InventoryList( hosts=(host, ), db_basedir=args.inventory_dir) inv = tuple(inv_list.entries()) if inv: # host is present in inventory and has tags host_tags = inv[0][1] else: # host is not present in inventory or has no tags host_tags = None host_base_path, hostdir = cls.create_host_base_dirs( host, base_root_path) log.debug("Base root path for target host \"{}\" is \"{}\"".format( host, host_base_path)) hostcnt += 1 if args.parallel: pargs = (host, host_tags, host_base_path, hostdir, args, True, configuration) log.trace(("Args for multiprocessing operation " "for host {}: {}".format(host, pargs))) process_args.append(pargs) else: try: cls.onehost(host, host_tags, host_base_path, hostdir, args, parallel=False, configuration=configuration) except cdist.Error as e: failed_hosts.append(host) if args.parallel and len(process_args) == 1: log.debug("Only 1 host for parallel processing, doing it " "sequentially") try: cls.onehost(*process_args[0]) except cdist.Error as e: failed_hosts.append(host) elif args.parallel: log.trace("Multiprocessing start method is {}".format( multiprocessing.get_start_method())) log.trace(("Starting multiprocessing Pool for {} " "parallel host operation".format(args.parallel))) results = mp_pool_run(cls.onehost, process_args, jobs=args.parallel) log.trace(("Multiprocessing for parallel host operation " "finished")) log.trace( "Multiprocessing for parallel host operation " "results: %s", results) failed_hosts = [host for host, result in results if not result] time_end = time.time() log.verbose("Total processing time for %s host(s): %s", hostcnt, (time_end - time_start)) if len(failed_hosts) > 0: raise cdist.Error("Failed to configure the following hosts: " + " ".join(failed_hosts)) elif not args.out_path: # If tmp out path created then remove it, but only if no failed # hosts. shutil.rmtree(base_root_path)
def test_inventory_list_all(self): invList = inventory.InventoryList(db_basedir=inventory_dir) entries = invList.entries() db = {host: sorted(tags) for host, tags in entries} self.assertEqual(db, self.db)
def test_inventory_list_print_host_only(self): invList = inventory.InventoryList(db_basedir=inventory_dir, list_only_host=True) invList.run() output = self._get_output() self.assertFalse(' ' in output)
def test_inventory_list_print(self): invList = inventory.InventoryList(db_basedir=inventory_dir) invList.run() output = self._get_output() self.assertTrue(' ' in output)