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