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