def __init__(self): super(KollaCli, self).__init__( description='Command-Line Client for OpenStack Kolla', version='0.1', command_manager=CommandManager('kolla.cli'), ) # check that current user is in the kolla group inventory_path = os.path.join(get_kollacli_etc(), INVENTORY_PATH) errString = 'Required file %s does not exist.\n' + \ 'Please re-install the kollacli to recreate the file.' if os.path.isfile(inventory_path) is False: raise CommandError(errString % inventory_path) inventory_file = None try: inventory_file = open(inventory_path, 'r+') except Exception: raise CommandError('Permission denied to run the kollacli.' + '\nPlease add user to the kolla group and ' + 'then log out and back in.') finally: if inventory_file and inventory_file.close is False: inventory_file.close() self.rotating_log_dir = get_kolla_log_dir() self.max_bytes = get_kolla_log_file_size() self.backup_count = 4 self.dump_stack_trace = False self.add_rotational_log()
def save(inventory): """Save the inventory in a pickle file""" inventory_path = os.path.join(utils.get_kollacli_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) utils.sync_write_file(inventory_path, pretty_data) except Exception as e: raise CommandError('saving inventory failed: %s' % 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_kollacli_etc() self.log.debug('etc for tests: %s' % etc_path) self._set_cmd_prefix() # make sure inventory dirs exists and remove inventory file self._init_dir(etc_path) etc_ansible_path = os.path.join(etc_path, 'ansible/') self._init_dir(etc_ansible_path) self._init_file(os.path.join(etc_ansible_path, 'inventory.json'))
def take_action(self, parsed_args): try: kolla_home = get_kolla_home() kolla_logs = get_kolla_log_dir() kolla_ansible = os.path.join(kolla_home, 'ansible') kolla_docs = os.path.join(kolla_home, 'docs') kolla_etc = get_kolla_etc() kolla_config = os.path.join(kolla_etc, 'config') kolla_globals = os.path.join(kolla_etc, 'globals.yml') kollacli_etc = get_kollacli_etc().rstrip('/') ketc = 'kolla/etc/' kshare = 'kolla/share/' fd, dump_path = tempfile.mkstemp(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)) tar.add(kolla_docs, arcname=kshare + os.path.basename(kolla_docs)) # Can't blanket add kolla_etc because the passwords.yml # file is accessible by the kolla user only (not kolla group) tar.add(kolla_config, arcname=ketc + os.path.basename(kolla_config)) tar.add(kolla_globals, arcname=ketc + os.path.basename(kolla_globals)) 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 self._add_cmd_info(tar) self.log.info('dump successful to %s' % dump_path) except Exception: raise Exception(traceback.format_exc())
def load(): """load the inventory from a pickle file""" inventory_path = os.path.join(utils.get_kollacli_etc(), INVENTORY_PATH) data = '' try: if os.path.exists(inventory_path): data = utils.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() except Exception: raise CommandError('loading inventory failed: %s' % traceback.format_exc()) return inventory
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_kollacli_etc() self.log.debug('etc for tests: %s' % etc_path) self._set_cmd_prefix() # make sure inventory dirs exists and remove inventory file self._init_dir(etc_path) etc_ansible_path = os.path.join(etc_path, 'ansible/') self._init_dir(etc_ansible_path) self._init_file(os.path.join(etc_ansible_path, 'inventory.json'))
def ssh_get_public_key(): keyfile_path = os.path.join(get_kollacli_etc(), 'id_rsa.pub') with open(keyfile_path, "r") as public_key_file: public_key = public_key_file.read() return public_key return None