def setup_logging(): """ Setup logging """ logging_config = configuration.settings.logging_conf pfx = configuration.settings.log_path if os.path.exists(logging_config): try: config = yaml.safe_load(fread(logging_config)) except yaml.YAMLError as _e: print("ERROR: logging configuration error: {}".format(_e)) sys.exit(12) fname = config.get('handlers').get('file_handler')['filename'] full_path = os.path.join(pfx, fname) config.get('handlers').get('file_handler')['filename'] = full_path logging.config.dictConfig(config) logging.info("Loaded logging configuration from " "{}".format(logging_config)) else: logging.basicConfig(level=logging.DEBUG) logging.warning("Logging configuration file ({}) not found, using " "basic logging".format(logging_config))
def test_remove_host(self): """- remove a host from all groups""" response = self.app.post('api/v1/groups/removehost1') self.assertEqual(response.status_code, 200) response = self.app.post('api/v1/groups/removehost2') self.assertEqual(response.status_code, 200) response = self.app.post('api/v1/hosts/localhost/groups/removehost1?others=removehost2') # noqa self.assertEqual(response.status_code, 200) response = self.app.delete('api/v1/hosts/localhost') self.assertEqual(response.status_code, 200) root_dir = os.getcwd() inv_filename = os.path.join(root_dir, 'samples/inventory/hosts') inv_data = yaml.safe_load(fread(inv_filename)) groups = inv_data['all']['children'].keys() for group in groups: hosts_in_group = inv_data['all']['children'][group]['hosts'] if isinstance(hosts_in_group, dict): self.assertNotIn("localhost", hosts_in_group.keys())
def setup_logging(): """ Setup logging """ logging_config = configuration.settings.logging_conf pfx = configuration.settings.log_path if os.path.exists(logging_config): try: config = yaml.safe_load(fread(logging_config)) except yaml.YAMLError as _e: print("ERROR: Invalid logging configuration file...aborting") sys.exit(12) fname = config.get('handlers').get('file_handler')['filename'] full_path = os.path.join(pfx, fname) config.get('handlers').get('file_handler')['filename'] = full_path logging.config.dictConfig(config) logging.info("Loaded logging configuration from {}".format(logging_config)) else: logging.basicConfig(level=logging.DEBUG) logging.warning("Logging configuration file ({}) not found, using " "basic logging".format(logging_config))
def test_group_add(self): """- Add a group to the inventory""" response = self.app.post('api/v1/groups/group1') self.assertEqual(response.status_code, 200) self.assertEqual(response.headers['Content-Type'], 'application/json') root_dir = os.getcwd() inv_filename = os.path.join(root_dir, 'samples/inventory/hosts') inv_data = yaml.safe_load(fread(inv_filename)) self.assertIn("group1", inv_data['all']['children'].keys())
def test_add_groupvars_inventory(self): """ - add a group and groupvars (inventory - hosts)""" payload = {"var1": "value1"} response = self.app.post('api/v1/groups/gvaddinv') response = self.app.post('api/v1/groupvars/gvaddinv?type=inventory', data=json.dumps(payload), content_type="application/json") self.assertEqual(response.status_code, 200) cwd = os.getcwd() inv = yaml.safe_load(fread(os.path.join(cwd, 'samples/inventory/hosts'))) # noqa E501 self.assertIn('var1', inv['all']['children']['gvaddinv']['vars'])
def test_group_remove(self): """- Remove a group from the inventory""" # first, setup the group we're going to remove response = self.app.post('api/v1/groups/group2') self.assertEqual(response.status_code, 200) response = self.app.delete('api/v1/groups/group2') self.assertEqual(response.status_code, 200) root_dir = os.getcwd() inv_filename = os.path.join(root_dir, 'samples/inventory/hosts') inv_data = yaml.safe_load(fread(inv_filename)) self.assertNotIn("group2", inv_data['all']['children'].keys())
def test_host_add_localhost(self): """- Add a localhost to a group""" response = self.app.post('api/v1/groups/local') self.assertEqual(response.status_code, 200) response = self.app.post('/api/v1/hosts/localhost/groups/local') self.assertEqual(response.status_code, 200) root_dir = os.getcwd() inv_filename = os.path.join(root_dir, 'samples/inventory/hosts') inv_data = yaml.safe_load(fread(inv_filename)) self.assertIn("localhost", inv_data['all']['children']['local']['hosts'].keys()) # noqa
def test_delete_groupvars_inv(self): """- delete groupvars (from group_vars dir)""" payload = {"var1": "value1"} response = self.app.post("api/v1/groups/gvinventory") self.assertEqual(response.status_code, 200) response = self.app.post('api/v1/groupvars/gvinventory?type=inventory', data=json.dumps(payload), content_type="application/json") self.assertEqual(response.status_code, 200) response = self.app.delete('api/v1/groupvars/gvinventory') self.assertEqual(response.status_code, 200) cwd = os.getcwd() inv = yaml.safe_load(fread(os.path.join(cwd, 'samples/inventory/hosts'))) # noqa E501 self.assertNotIn('vars', inv['all']['children']['gvinventory'])
def test_add_hostvars_inventory(self): """- create hostvars (file), group and host valid""" response = self.app.post('api/v1/groups/tahi') self.assertEqual(response.status_code, 200) response = self.app.post('api/v1/hosts/localhost/groups/tahi') self.assertEqual(response.status_code, 200) payload = {"var1": "value1"} response = self.app.post( 'api/v1/hostvars/localhost/groups/tahi?type=inventory', # noqa E501 data=json.dumps(payload), content_type="application/json") self.assertEqual(response.status_code, 200) cwd = os.getcwd() inv = yaml.safe_load( fread(os.path.join(cwd, 'samples/inventory/hosts'))) # noqa E501 self.assertIn( 'var1', inv['all']['children']['tahi']['hosts']['localhost']) # noqa
def setup_localhost_ssh(): ssh_home = os.path.join(os.path.expanduser('~'), '.ssh') if not os.path.exists(ssh_home): os.mkdir(ssh_home) os.chmod(ssh_home, 0o700) authorized_keys = os.path.join(ssh_home, "authorized_keys") root_dir = configuration.settings.playbooks_root_dir app_pub_key = fread(os.path.join(root_dir, "env", "ssh_key.pub")) if not os.path.exists(authorized_keys): with open(authorized_keys, "w") as auth_file: auth_file.write(app_pub_key) os.chmod(authorized_keys, 0o644) else: # it does exist, so see if our key should be appended with open(authorized_keys, "r") as auth_file: auth_data = auth_file.read().splitlines() if app_pub_key not in auth_data: with open(authorized_keys, "a") as auth_file: auth_file.write("{}\n".format(app_pub_key)) else: pass