Get auth token when checking deployment
[functest-xtesting.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 import six
15 from six.moves.urllib.parse import urlparse
16
17 from functest.ci import check_deployment
18 from functest.utils.constants import CONST
19 import functest.utils.openstack_clean as os_clean
20 import functest.utils.openstack_snapshot as os_snapshot
21
22
23 class OpenStack(object):
24
25     def __init__(self):
26         self.os_auth_url = CONST.__getattribute__('OS_AUTH_URL')
27         self.endpoint_ip = None
28         self.endpoint_port = None
29         self.openstack_creds = CONST.__getattribute__('openstack_creds')
30         self.snapshot_file = CONST.__getattribute__('openstack_snapshot_file')
31         if self.os_auth_url:
32             self.endpoint_ip = urlparse(self.os_auth_url).hostname
33             self.endpoint_port = urlparse(self.os_auth_url).port
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     @staticmethod
47     def show_credentials():
48         dic_credentials = {}
49         for key, value in os.environ.items():
50             if key.startswith('OS_'):
51                 dic_credentials.update({key: value})
52         return dic_credentials
53
54     def check(self):
55         self.ping_endpoint()
56         deployment = check_deployment.CheckDeployment()
57         deployment.check_all()
58
59     def snapshot_create(self):
60         self.ping_endpoint()
61         if os.path.isfile(self.snapshot_file):
62             answer = six.moves.input(
63                 "It seems there is already an OpenStack "
64                 "snapshot. Do you want to overwrite it with "
65                 "the current OpenStack status? [y|n]\n")
66             while True:
67                 if answer.lower() in ["y", "yes"]:
68                     break
69                 elif answer.lower() in ["n", "no"]:
70                     return
71                 else:
72                     answer = six.moves.input(
73                         "Invalid answer. Please type [y|n]\n")
74
75         click.echo("Generating Openstack snapshot...")
76         os_snapshot.main()
77
78     def snapshot_show(self):
79         if not os.path.isfile(self.snapshot_file):
80             click.echo("There is no OpenStack snapshot created. To create "
81                        "one run the command "
82                        "'functest openstack snapshot-create'")
83             return
84         with open(self.snapshot_file, 'r') as yaml_file:
85             click.echo("\n%s"
86                        % yaml_file.read())
87
88     def clean(self):
89         self.ping_endpoint()
90         if not os.path.isfile(self.snapshot_file):
91             click.echo("Not possible to clean OpenStack without a snapshot. "
92                        "This could cause problems. "
93                        "Run first the command "
94                        "'functest openstack snapshot-create'")
95             return
96         os_clean.main()
97
98
99 class CliOpenStack(OpenStack):
100
101     def __init__(self):
102         super(CliOpenStack, self).__init__()
103
104     @staticmethod
105     def show_credentials():
106         dic_credentials = OpenStack.show_credentials()
107         for key, value in dic_credentials.items():
108                 if key.startswith('OS_'):
109                     click.echo("{}={}".format(key, value))