1 ##############################################################################
2 # Copyright (c) 2017 Tim Rozet (trozet@redhat.com) and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 # TODO(trozet) migrate rest of utils.sh here
19 from apex.common import constants
20 from apex.common import parsers
21 from apex.undercloud import undercloud as uc_lib
22 from apex.common import utils
24 VALID_UTILS = ['fetch_logs']
25 START_TIME = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M")
26 APEX_TEMP_DIR = tempfile.mkdtemp(prefix="apex-logs-{}-".format(START_TIME))
30 uc_ip = uc_lib.Undercloud.get_ip()
32 raise Exception('No Undercloud IP found')
33 logging.info("Undercloud IP is: {}".format(uc_ip))
35 fetch_vars['stackrc'] = 'source /home/stack/stackrc'
36 fetch_vars['apex_temp_dir'] = APEX_TEMP_DIR
37 fetch_playbook = os.path.join(args.lib_dir, constants.ANSIBLE_PATH,
38 'fetch_overcloud_nodes.yml')
40 utils.run_ansible(fetch_vars, fetch_playbook, host=uc_ip,
41 user='stack', tmp_dir=APEX_TEMP_DIR)
42 logging.info("Retrieved overcloud nodes info")
44 logging.error("Failed to retrieve overcloud nodes. Please check log")
46 nova_output = os.path.join(APEX_TEMP_DIR, 'nova_output')
47 fetch_vars['overcloud_nodes'] = parsers.parse_nova_output(nova_output)
48 fetch_vars['SSH_OPTIONS'] = '-o StrictHostKeyChecking=no -o ' \
49 'GlobalKnownHostsFile=/dev/null -o ' \
50 'UserKnownHostsFile=/dev/null -o ' \
52 fetch_playbook = os.path.join(args.lib_dir, constants.ANSIBLE_PATH,
53 'fetch_overcloud_logs.yml')
54 # Run per overcloud node
55 for node, ip in fetch_vars['overcloud_nodes'].items():
56 logging.info("Executing fetch logs overcloud playbook on "
57 "node {}".format(node))
59 utils.run_ansible(fetch_vars, fetch_playbook, host=ip,
60 user='heat-admin', tmp_dir=APEX_TEMP_DIR)
61 logging.info("Logs retrieved for node {}".format(node))
63 logging.error("Log retrieval failed "
64 "for node {}. Please check log".format(node))
66 logging.info("Log retrieval complete and stored in {}".format(
70 def execute_actions(args):
71 for action in VALID_UTILS:
72 if hasattr(args, action) and getattr(args, action):
73 util_module = __import__('apex').utils
74 func = getattr(util_module, action)
75 logging.info("Executing action: {}".format(action))
80 util_parser = argparse.ArgumentParser()
81 util_parser.add_argument('-f', '--fetch-logs',
86 help='Fetch all overcloud logs')
87 util_parser.add_argument('--lib-dir',
88 default='/usr/share/opnfv-apex',
89 help='Directory path for apex ansible '
90 'and third party libs')
91 args = util_parser.parse_args(sys.argv[1:])
92 os.makedirs(os.path.dirname('./apex_util.log'), exist_ok=True)
93 formatter = '%(asctime)s %(levelname)s: %(message)s'
94 logging.basicConfig(filename='./apex_util.log',
96 datefmt='%m/%d/%Y %I:%M:%S %p',
98 console = logging.StreamHandler()
99 console.setLevel(logging.DEBUG)
100 console.setFormatter(logging.Formatter(formatter))
101 logging.getLogger('').addHandler(console)
103 execute_actions(args)
106 if __name__ == '__main__':