Fixes relative import for utils
[apex.git] / apex / utils.py
1 ##############################################################################
2 # Copyright (c) 2017 Tim Rozet (trozet@redhat.com) and others.
3 #
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 ##############################################################################
9
10 # TODO(trozet) migrate rest of utils.sh here
11
12 import argparse
13 import datetime
14 import logging
15 import os
16 import sys
17 import tempfile
18
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
23
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))
27
28
29 def fetch_logs(args):
30     uc_ip = uc_lib.Undercloud.get_ip()
31     if not uc_ip:
32         raise Exception('No Undercloud IP found')
33     logging.info("Undercloud IP is: {}".format(uc_ip))
34     fetch_vars = dict()
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')
39     try:
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")
43     except Exception:
44         logging.error("Failed to retrieve overcloud nodes.  Please check log")
45         raise
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 ' \
51                                 'LogLevel=error'
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))
58         try:
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))
62         except Exception:
63             logging.error("Log retrieval failed "
64                           "for node {}. Please check log".format(node))
65             raise
66     logging.info("Log retrieval complete and stored in {}".format(
67         APEX_TEMP_DIR))
68
69
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))
76             func(args)
77
78
79 def main():
80     util_parser = argparse.ArgumentParser()
81     util_parser.add_argument('-f', '--fetch-logs',
82                              dest='fetch_logs',
83                              required=False,
84                              default=False,
85                              action='store_true',
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',
95                         format=formatter,
96                         datefmt='%m/%d/%Y %I:%M:%S %p',
97                         level=logging.DEBUG)
98     console = logging.StreamHandler()
99     console.setLevel(logging.DEBUG)
100     console.setFormatter(logging.Formatter(formatter))
101     logging.getLogger('').addHandler(console)
102
103     execute_actions(args)
104
105
106 if __name__ == '__main__':
107     main()