Change step 4 to step 3 in healtcheck
[functest.git] / cli / commands / cli_os.py
1 #!/usr/bin/env python
2 #
3 # jose.lausuch@ericsson.com
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 import click
11 import os
12 import yaml
13
14 import functest.utils.clean_openstack as clean_os
15 import functest.utils.functest_utils as ft_utils
16 import functest.utils.generate_defaults as gen_def
17
18 """ global variables """
19 with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
20     functest_yaml = yaml.safe_load(f)
21
22 REPOS_DIR = os.getenv('repos_dir')
23 FUNCTEST_REPO = ("%s/functest/" % REPOS_DIR)
24 FUNCTEST_CONF_DIR = functest_yaml.get("general").get(
25     "directories").get("dir_functest_conf")
26 RC_FILE = os.getenv('creds')
27 OS_DEFAULTS_FILE = FUNCTEST_CONF_DIR + '/os_defaults.yaml'
28
29
30 class CliOpenStack:
31     def __init__(self):
32         self.os_auth_url = os.getenv('OS_AUTH_URL')
33         self.endpoint_ip = None
34         self.endpoint_port = None
35         if self.os_auth_url is not None:
36             self.endpoint_ip = self.os_auth_url.rsplit("/")[2].rsplit(":")[0]
37             self.endpoint_port = self.os_auth_url.rsplit("/")[2].rsplit(":")[1]
38
39     def ping_endpoint(self):
40         if self.os_auth_url is None:
41             click.echo("Source the OpenStack credentials first '. $creds'")
42             exit(0)
43         response = os.system("ping -c 1 " + self.endpoint_ip + ">/dev/null")
44         if response == 0:
45             return 0
46         else:
47             click.echo("Cannot talk to the endpoint %s\n" % self.endpoint_ip)
48             exit(0)
49
50     def show_credentials(self):
51         cmd = "env|grep OS_"
52         ft_utils.execute_command(cmd, exit_on_error=False, verbose=False)
53         click.echo("")
54
55     def fetch_credentials(self):
56         if os.path.isfile(RC_FILE):
57             answer = raw_input("It seems the RC file is already present. "
58                                "Do you want to overwrite it? [y|n]\n")
59             while True:
60                 if answer.lower() in ["y", "yes"]:
61                     break
62                 elif answer.lower() in ["n", "no"]:
63                     return
64                 else:
65                     answer = raw_input("Invalid answer. Please type [y|n]\n")
66
67         CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
68         if CI_INSTALLER_TYPE is None:
69             click.echo("The environment variable 'INSTALLER_TYPE' is not"
70                        "defined. Please export it")
71         CI_INSTALLER_IP = os.getenv('INSTALLER_IP')
72         if CI_INSTALLER_IP is None:
73             click.echo("The environment variable 'INSTALLER_IP' is not"
74                        "defined. Please export it")
75         cmd = ("/home/opnfv/repos/releng/utils/fetch_os_creds.sh "
76                "-d %s -i %s -a %s"
77                % (RC_FILE, CI_INSTALLER_TYPE, CI_INSTALLER_IP))
78         click.echo("Fetching credentials from installer node '%s' with IP=%s.."
79                    % (CI_INSTALLER_TYPE, CI_INSTALLER_IP))
80         ft_utils.execute_command(cmd, verbose=False)
81
82     def check(self):
83         self.ping_endpoint()
84         cmd = FUNCTEST_REPO + "ci/check_os.sh"
85         ft_utils.execute_command(cmd, verbose=False)
86
87     def snapshot_create(self):
88         self.ping_endpoint()
89         if os.path.isfile(OS_DEFAULTS_FILE):
90             answer = raw_input("It seems there is already an OpenStack "
91                                "snapshot. Do you want to overwrite it with "
92                                "the current OpenStack status? [y|n]\n")
93             while True:
94                 if answer.lower() in ["y", "yes"]:
95                     break
96                 elif answer.lower() in ["n", "no"]:
97                     return
98                 else:
99                     answer = raw_input("Invalid answer. Please type [y|n]\n")
100
101         click.echo("Generating Openstack snapshot...")
102         gen_def.main()
103
104     def snapshot_show(self):
105         if not os.path.isfile(OS_DEFAULTS_FILE):
106             click.echo("There is no OpenStack snapshot created. To create "
107                        "one run the command 'functest env os-create-snapshot'")
108             return
109         with open(OS_DEFAULTS_FILE, 'r') as yaml_file:
110             click.echo("\n%s"
111                        % yaml_file.read())
112
113     def clean(self):
114         self.ping_endpoint()
115         if not os.path.isfile(OS_DEFAULTS_FILE):
116             click.echo("Not possible to clean OpenStack without a snapshot. "
117                        "This could cause problems. "
118                        "Run first the command 'os-create-shapshot'.")
119             return
120         clean_os.main()