Merge "Re-Enable Promise testcases"
[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 from functest.utils.constants import CONST
16 import functest.utils.functest_utils as ft_utils
17 import functest.utils.openstack_clean as os_clean
18 import functest.utils.openstack_snapshot as os_snapshot
19
20
21 class CliOpenStack(object):
22
23     def __init__(self):
24         self.os_auth_url = CONST.__getattribute__('OS_AUTH_URL')
25         self.endpoint_ip = None
26         self.endpoint_port = None
27         self.openstack_creds = CONST.__getattribute__('openstack_creds')
28         self.snapshot_file = CONST.__getattribute__('openstack_snapshot_file')
29         if self.os_auth_url:
30             self.endpoint_ip = self.os_auth_url.rsplit("/")[2].rsplit(":")[0]
31             self.endpoint_port = self.os_auth_url.rsplit("/")[2].rsplit(":")[1]
32
33     def ping_endpoint(self):
34         if self.os_auth_url is None:
35             click.echo("Source the OpenStack credentials first '. $creds'")
36             exit(0)
37         response = os.system("ping -c 1 " + self.endpoint_ip + ">/dev/null")
38         if response == 0:
39             return 0
40         else:
41             click.echo("Cannot talk to the endpoint %s\n" % self.endpoint_ip)
42             exit(0)
43
44     @staticmethod
45     def show_credentials():
46         for key, value in os.environ.items():
47             if key.startswith('OS_'):
48                 click.echo("{}={}".format(key, value))
49
50     def fetch_credentials(self):
51         if os.path.isfile(self.openstack_creds):
52             answer = raw_input("It seems the RC file is already present. "
53                                "Do you want to overwrite it? [y|n]\n")
54             while True:
55                 if answer.lower() in ["y", "yes"]:
56                     break
57                 elif answer.lower() in ["n", "no"]:
58                     return
59                 else:
60                     answer = raw_input("Invalid answer. Please type [y|n]\n")
61
62         installer_type = CONST.__getattribute__('INSTALLER_TYPE')
63         if installer_type is None:
64             click.echo("The environment variable 'INSTALLER_TYPE' is not"
65                        "defined. Please export it")
66         installer_ip = CONST.__getattribute__('INSTALLER_IP')
67         if installer_ip is None:
68             click.echo("The environment variable 'INSTALLER_IP' is not"
69                        "defined. Please export it")
70         cmd = ("fetch_os_creds.sh -d %s -i %s -a %s"
71                % (self.openstack_creds,
72                   installer_type,
73                   installer_ip))
74         click.echo("Fetching credentials from installer node '%s' with IP=%s.."
75                    % (installer_type, installer_ip))
76         ft_utils.execute_command(cmd, verbose=False)
77
78     def check(self):
79         self.ping_endpoint()
80         cmd = os.path.join(CONST.__getattribute__('dir_repo_functest'),
81                            "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(self.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(self.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(self.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(self.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()