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