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