def dump(dirpath): """Dumps configuration data for debugging Dumps most files in /etc/kolla and /usr/share/kolla into a tar file so be given to support / development to help with debugging problems. """ kolla_home = get_kolla_ansible_home() kolla_logs = get_kolla_ansible_log_dir() kolla_ansible = os.path.join(kolla_home, 'ansible') kollacli_etc = get_kolla_cli_etc().rstrip('/') ketc = 'kolla/etc/' kshare = 'kolla/share/' fd, dump_path = tempfile.mkstemp(dir=dirpath, prefix='kollacli_dump_', suffix='.tgz') os.close(fd) # avoid fd leak with tarfile.open(dump_path, 'w:gz') as tar: # Can't blanket add kolla_home because the .ssh dir is # accessible by the kolla user only (not kolla group) tar.add(kolla_ansible, arcname=kshare + os.path.basename(kolla_ansible)) # Can't blanket add kolla_etc because the passwords.yml # file is accessible by the kolla user only (not kolla group) tar.add(kollacli_etc, arcname=ketc + os.path.basename(kollacli_etc)) # add kolla log files if os.path.isdir(kolla_logs): tar.add(kolla_logs) # add output of various commands _add_cmd_info(tar) return dump_path
def save(inventory): """Save the inventory in a pickle file""" inventory_path = os.path.join(get_kolla_cli_etc(), INVENTORY_PATH) try: # multiple trips thru json to render a readable inventory file data = jsonpickle.encode(inventory) data_str = json.loads(data) pretty_data = json.dumps(data_str, indent=4) sync_write_file(inventory_path, pretty_data) except Exception as e: raise FailedOperation( u._('Saving inventory failed. : {error}').format(error=str(e)))
def setUp(self): super(KollaCliTest, self).setUp() logging.basicConfig(stream=sys.stderr) self.log.setLevel(logging.DEBUG) self.log.info( '\nStarting test: %s ***********************************' % self._testMethodName) # switch to test path etc_path = utils.get_kolla_cli_etc() self.log.debug('etc for tests: %s' % etc_path) self._save_config()
def _config_reset_cmd(): """config_reset command args for config_reset command - none """ kolla_etc = get_kolla_etc() kolla_home = get_kolla_ansible_home() kollacli_etc = get_kolla_cli_etc() group_vars_path = os.path.join(kolla_home, 'ansible/group_vars') host_vars_path = os.path.join(kolla_home, 'ansible/host_vars') globals_path = os.path.join(group_vars_path, '__GLOBAL__') inventory_path = os.path.join(kollacli_etc, 'ansible/inventory.json') # truncate global property and inventory files with open(globals_path, 'w') as globals_file: globals_file.truncate() with open(inventory_path, 'w') as inventory_file: inventory_file.truncate() # clear all passwords clear_all_passwords() # nuke all files under the kolla etc base, skipping everything # in the kolla-cli directory and the globals.yml and passwords.yml files for dir_path, dir_names, file_names in os.walk(kolla_etc, topdown=False): if 'kolla-cli' not in dir_path: for dir_name in dir_names: if dir_name != 'kolla-cli': os.rmdir(os.path.join(dir_path, dir_name)) for file_name in file_names: if file_name == 'passwords.yml' or file_name == 'globals.yml': continue os.remove(os.path.join(dir_path, file_name)) # nuke all property files under the kolla-ansible base other than # all.yml and the global property file which we truncate above for dir_path, _, file_names in os.walk(group_vars_path): for file_name in file_names: if (file_name != '__GLOBAL__' and file_name != 'all.yml'): os.remove(os.path.join(dir_path, file_name)) for dir_path, _, file_names in os.walk(host_vars_path): for file_name in file_names: os.remove(os.path.join(dir_path, file_name))
def _restore_config(self): """restore config""" # restore inventory dst_path = os.path.join(utils.get_kolla_cli_etc(), 'ansible', 'inventory.json') src_path = os.path.join('/tmp', 'inventory.json.utest.save') copyfile(src_path, dst_path) # restore group vars ansible_dir = os.path.join(utils.get_kolla_ansible_home(), 'ansible') groupdir = os.path.join(ansible_dir, 'group_vars') self._restore_dir(groupdir) # restore host vars hostdir = os.path.join(ansible_dir, 'host_vars') self._restore_dir(hostdir)
def load(): """load the inventory from a pickle file""" inventory_path = os.path.join(get_kolla_cli_etc(), INVENTORY_PATH) data = '' try: if os.path.exists(inventory_path): data = sync_read_file(inventory_path) if data.strip(): inventory = jsonpickle.decode(data) # upgrade version handling if inventory.version != inventory.class_version: inventory.upgrade() else: inventory = Inventory() Inventory.save(inventory) except Exception: raise FailedOperation( u._('Loading inventory failed. : {error}').format( error=traceback.format_exc())) return inventory
def __init__(self): super(KollaCli, self).__init__( description=u._('Command-Line Client for OpenStack Kolla'), version=VERSION, command_manager=CommandManager('kolla.cli'), ) inventory_path = os.path.join(get_kolla_cli_etc(), INVENTORY_PATH) if not self._is_inventory_present(inventory_path): err_string = u._( 'Required file ({inventory}) does not exist.\n' 'Please re-install the kollacli to ' 'recreate the file.').format(inventory=inventory_path) raise CommandError(err_string) # set up logging and test that user running shell is part # of kolla group ClientApi() # paramiko log is very chatty, tune it down logging.getLogger('paramiko').setLevel(logging.WARNING) self.dump_stack_trace = False
def setUp(self): super(KollaCliTest, self).setUp() logging.basicConfig(stream=sys.stderr) self.log.setLevel(logging.DEBUG) self.log.info('\nStarting test: %s ***********************************' % self._testMethodName) # switch to test path self.log.info('running python: %s/%s' % (sys.executable, sys.version)) etc_path = utils.get_kolla_cli_etc() self.log.debug('etc for tests: %s' % etc_path) self._set_cmd_prefix() self._save_config() # make sure inventory dirs exists and remove inventory file etc_ansible_path = os.path.join(etc_path, 'ansible/') inv_path = os.path.join(etc_ansible_path, 'inventory.json') self._init_dir(etc_path) self._init_dir(etc_ansible_path) self._init_file(inv_path)
def ssh_get_public_key(): keyfile_path = os.path.join(get_kolla_cli_etc(), 'id_rsa.pub') with open(keyfile_path, "r") as public_key_file: public_key = public_key_file.read() return public_key
def _replace_inventory(self, old_version_inv_path): inv_path = os.path.join(get_kolla_cli_etc(), 'ansible', 'inventory.json') shutil.copyfile(old_version_inv_path, inv_path)