Merge "Fix rally db issue"
[functest.git] / functest / ci / prepare_env.py
1 #!/usr/bin/env python
2 #
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8
9 import argparse
10 import logging
11 import logging.config
12 import os
13 import pkg_resources
14 import re
15 import sys
16
17 import yaml
18
19 from functest.ci import check_deployment
20 import functest.utils.functest_utils as ft_utils
21 import functest.utils.openstack_utils as os_utils
22 from functest.utils.constants import CONST
23
24 actions = ['start', 'check']
25
26 """ logging configuration """
27 logger = logging.getLogger('functest.ci.prepare_env')
28 handler = None
29 # set the architecture to default
30 pod_arch = os.getenv("POD_ARCH", None)
31 arch_filter = ['aarch64']
32
33 CONFIG_FUNCTEST_PATH = pkg_resources.resource_filename(
34     'functest', 'ci/config_functest.yaml')
35 CONFIG_PATCH_PATH = pkg_resources.resource_filename(
36     'functest', 'ci/config_patch.yaml')
37 CONFIG_AARCH64_PATCH_PATH = pkg_resources.resource_filename(
38     'functest', 'ci/config_aarch64_patch.yaml')
39
40
41 class PrepareEnvParser(object):
42
43     def __init__(self):
44         self.parser = argparse.ArgumentParser()
45         self.parser.add_argument("action", help="Possible actions are: "
46                                  "'{d[0]}|{d[1]}' ".format(d=actions),
47                                  choices=actions)
48         self.parser.add_argument("-d", "--debug", help="Debug mode",
49                                  action="store_true")
50
51     def parse_args(self, argv=[]):
52         return vars(self.parser.parse_args(argv))
53
54
55 def print_separator():
56     logger.info("==============================================")
57
58
59 def source_rc_file():
60     print_separator()
61
62     logger.info("Sourcing the OpenStack RC file...")
63     os_utils.source_credentials(CONST.__getattribute__('openstack_creds'))
64     for key, value in os.environ.iteritems():
65         if re.search("OS_", key):
66             if key == 'OS_AUTH_URL':
67                 CONST.__setattr__('OS_AUTH_URL', value)
68             elif key == 'OS_USERNAME':
69                 CONST.__setattr__('OS_USERNAME', value)
70             elif key == 'OS_TENANT_NAME':
71                 CONST.__setattr__('OS_TENANT_NAME', value)
72             elif key == 'OS_PASSWORD':
73                 CONST.__setattr__('OS_PASSWORD', value)
74
75
76 def update_config_file():
77     patch_file(CONFIG_PATCH_PATH)
78
79     if pod_arch and pod_arch in arch_filter:
80         patch_file(CONFIG_AARCH64_PATCH_PATH)
81
82     if "TEST_DB_URL" in os.environ:
83         update_db_url()
84
85
86 def patch_file(patch_file_path):
87     logger.debug('Updating file: %s', patch_file_path)
88     with open(patch_file_path) as f:
89         patch_file = yaml.safe_load(f)
90
91     updated = False
92     for key in patch_file:
93         if key in CONST.__getattribute__('DEPLOY_SCENARIO'):
94             new_functest_yaml = dict(ft_utils.merge_dicts(
95                 ft_utils.get_functest_yaml(), patch_file[key]))
96             updated = True
97
98     if updated:
99         os.remove(CONFIG_FUNCTEST_PATH)
100         with open(CONFIG_FUNCTEST_PATH, "w") as f:
101             f.write(yaml.dump(new_functest_yaml, default_style='"'))
102
103
104 def update_db_url():
105     with open(CONFIG_FUNCTEST_PATH) as f:
106         functest_yaml = yaml.safe_load(f)
107
108     with open(CONFIG_FUNCTEST_PATH, "w") as f:
109         functest_yaml["results"]["test_db_url"] = os.environ.get('TEST_DB_URL')
110         f.write(yaml.dump(functest_yaml, default_style='"'))
111
112
113 def verify_deployment():
114     print_separator()
115     logger.info("Verifying OpenStack deployment...")
116     deployment = check_deployment.CheckDeployment()
117     deployment.check_all()
118
119
120 def create_flavor():
121     _, flavor_id = os_utils.get_or_create_flavor('m1.tiny',
122                                                  '512',
123                                                  '1',
124                                                  '1',
125                                                  public=True)
126     if flavor_id is None:
127         raise Exception('Failed to create flavor')
128
129
130 def check_environment():
131     msg_not_active = "The Functest environment is not installed."
132     if not os.path.isfile(CONST.__getattribute__('env_active')):
133         raise Exception(msg_not_active)
134
135     with open(CONST.__getattribute__('env_active'), "r") as env_file:
136         s = env_file.read()
137         if not re.search("1", s):
138             raise Exception(msg_not_active)
139
140     logger.info("Functest environment is installed.")
141
142
143 def prepare_env(**kwargs):
144     try:
145         if not (kwargs['action'] in actions):
146             logger.error('Argument not valid.')
147             return -1
148         elif kwargs['action'] == "start":
149             logger.info("######### Preparing Functest environment #########\n")
150             verify_deployment()
151             source_rc_file()
152             update_config_file()
153             create_flavor()
154             with open(CONST.__getattribute__('env_active'), "w") as env_file:
155                 env_file.write("1")
156             check_environment()
157         elif kwargs['action'] == "check":
158             check_environment()
159     except Exception as e:
160         logger.error(e)
161         return -1
162     return 0
163
164
165 def main():
166     logging.config.fileConfig(pkg_resources.resource_filename(
167         'functest', 'ci/logging.ini'))
168     logging.captureWarnings(True)
169     parser = PrepareEnvParser()
170     args = parser.parse_args(sys.argv[1:])
171     return prepare_env(**args)