diff --git a/kollacli/common.py b/kollacli/common.py index ad56884..027190b 100644 --- a/kollacli/common.py +++ b/kollacli/common.py @@ -26,7 +26,6 @@ from kollacli.utils import get_kolla_etc from kollacli.utils import get_kolla_home from kollacli.utils import get_kolla_log_dir from kollacli.utils import get_kollacli_etc -from kollacli.utils import get_kollacli_home from kollacli.utils import run_cmd from cliff.command import Command @@ -122,11 +121,10 @@ class Dump(Command): kolla_logs = get_kolla_log_dir() kolla_ansible = os.path.join(kolla_home, 'ansible') kolla_docs = os.path.join(kolla_home, 'docs') - kolla_templates = os.path.join(kolla_home, 'templates') 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() + kollacli_etc = get_kollacli_etc().rstrip('/') ketc = 'kolla/etc/' kshare = 'kolla/share/' fd, dump_path = tempfile.mkstemp(prefix='kollacli_dump_', @@ -136,21 +134,18 @@ class Dump(Command): # 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=ketc + os.path.basename(kolla_ansible)) + arcname=kshare + os.path.basename(kolla_ansible)) tar.add(kolla_docs, - arcname=ketc + os.path.basename(kolla_docs)) - if os.path.isdir(kolla_templates): - tar.add(kolla_templates, - arcname=ketc + os.path.basename(kolla_templates)) + 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=kshare + os.path.basename(kolla_config)) + arcname=ketc + os.path.basename(kolla_config)) tar.add(kolla_globals, - arcname=kshare + os.path.basename(kolla_globals)) + arcname=ketc + os.path.basename(kolla_globals)) tar.add(kollacli_etc, - arcname=kshare + os.path.basename(kollacli_etc)) + arcname=ketc + os.path.basename(kollacli_etc)) # add kolla log files if os.path.isdir(kolla_logs): @@ -174,22 +169,31 @@ class Dump(Command): 'kollacli password list'] # collect the json inventory output - json_cmd = os.path.join(get_kollacli_home(), 'tools', - 'json_generator.py') - cmds.append(json_cmd) - fd, path = tempfile.mkstemp(suffix='.tmp') - os.close(fd) - with open(path, 'w') as tmp_file: - for cmd in cmds: - err_msg, output = run_cmd(cmd, False) - tmp_file.write('\n\n$ %s\n' % cmd) - if err_msg: - tmp_file.write('Error message: %s\n' % err_msg) - for line in output: - tmp_file.write(line + '\n') + inventory = Inventory.load() + inv_path = inventory.create_json_gen_file() + cmds.append(inv_path) - tar.add(path, arcname=os.path.join('kolla', 'cmds_output')) - os.remove(path) + try: + fd, path = tempfile.mkstemp(suffix='.tmp') + os.close(fd) + with open(path, 'w') as tmp_file: + for cmd in cmds: + err_msg, output = run_cmd(cmd, False) + tmp_file.write('\n\n$ %s\n' % cmd) + if err_msg: + tmp_file.write('Error message: %s\n' % err_msg) + lines = output.split('\n') + for line in lines: + tmp_file.write(line + '\n') + tar.add(path, arcname=os.path.join('kolla', 'cmds_output')) + + except Exception as e: + raise e + finally: + if path: + os.remove(path) + if inv_path: + os.remove(inv_path) return diff --git a/tests/deploy.py b/tests/deploy.py index 1b9caa0..f08ff49 100644 --- a/tests/deploy.py +++ b/tests/deploy.py @@ -19,6 +19,7 @@ from kollacli.ansible.inventory import SERVICES import json import os +import tarfile import unittest @@ -136,16 +137,39 @@ class TestFunctional(KollaCliTest): self.run_cli_cmd('deploy --groups=control') def test_dump(self): - # quick check of kollacli dump + check_files = [ + 'var/log/kolla/kolla.log', + 'kolla/etc/globals.yml', + 'kolla/etc/config/nova/nova-api.conf', + 'kolla/etc/kollacli/ansible/inventory.json', + 'kolla/share/ansible/site.yml', + 'kolla/share/docs/ansible-deployment.rst', + ] + # dump success output is: # dump successful to /tmp/kollacli_dump_Umxu6d.tgz - msg = self.run_cli_cmd('dump') - self.assertIn('/', msg, 'path not found in dump output: %s' % msg) + dump_path = None + try: + msg = self.run_cli_cmd('dump') + self.assertIn('/', msg, 'path not found in dump output: %s' % msg) - dump_path = '/' + msg.strip().split('/', 1)[1] - is_file = os.path.isfile(dump_path) - self.assertTrue(is_file, - 'dump file not found at %s' % dump_path) - os.remove(dump_path) + dump_path = '/' + msg.strip().split('/', 1)[1] + is_file = os.path.isfile(dump_path) + self.assertTrue(is_file, + 'dump file not found at %s' % dump_path) + + file_paths = [] + with tarfile.open(dump_path, 'r') as tar: + file_paths = tar.getnames() + + for check_file in check_files: + self.assertIn(check_file, file_paths, + 'dump: check file: %s, not in files:\n%s' + % (check_file, file_paths)) + except Exception as e: + raise e + finally: + if dump_path and os.path.exists(dump_path): + os.remove(dump_path) def check_json(self, msg, groups, hosts, included_groups, included_hosts): err_msg = ('included groups: %s\n' % included_groups + diff --git a/tools/log_collector.py b/tools/log_collector.py index 197e9bd..a950d35 100755 --- a/tools/log_collector.py +++ b/tools/log_collector.py @@ -22,31 +22,31 @@ import tempfile from kollacli.ansible.inventory import Inventory from kollacli.ansible import properties from kollacli.utils import get_admin_user -from kollacli.utils import get_kollacli_home tar_file_descr = None def run_ansible_cmd(cmd, host): - # sudo -u kolla ansible ol7-c4 -i - # /usr/share/kolla/kollacli/tools/json_generator.py -a "cmd" + # sudo -u kolla ansible ol7-c4 -i inv_path -a "cmd" out = None user = get_admin_user() - kollacli_home = get_kollacli_home() - inv_path = os.path.join(kollacli_home, 'tools', 'json_generator.py') + inventory = Inventory.load() + inv_path = inventory.create_json_gen_file() acmd = ('/usr/bin/sudo -u %s ansible %s -i %s -a "%s"' % (user, host, inv_path, cmd)) try: - (out, _) = subprocess.Popen(acmd, shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE).communicate() + (out, err) = subprocess.Popen(acmd, shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE).communicate() except Exception as e: print('%s\nCannot communicate with host: %s, skipping' % (e, host)) + finally: + os.remove(inv_path) if not out: - print('Host %s is not accessible, skipping' % host) + print('Host %s is not accessible: %s, skipping' % (host, err)) elif '>>' not in out: print('Ansible command: %s' % acmd) print('Host: %s. \nInvalid ansible return data: [%s]. skipping'