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
15 from functest.ci import check_deployment
16 from functest.utils.constants import CONST
17 import functest.utils.openstack_clean as os_clean
18 import functest.utils.openstack_snapshot as os_snapshot
21 class OpenStack(object):
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')
30 self.endpoint_ip = self.os_auth_url.rsplit("/")[2].rsplit(":")[0]
31 self.endpoint_port = self.os_auth_url.rsplit("/")[2].rsplit(":")[1]
33 def ping_endpoint(self):
34 if self.os_auth_url is None:
35 click.echo("Source the OpenStack credentials first '. $creds'")
37 response = os.system("ping -c 1 " + self.endpoint_ip + ">/dev/null")
41 click.echo("Cannot talk to the endpoint %s\n" % self.endpoint_ip)
45 def show_credentials():
47 for key, value in os.environ.items():
48 if key.startswith('OS_'):
49 dic_credentials.update({key: value})
50 return dic_credentials
54 deployment = check_deployment.CheckDeployment()
55 deployment.check_all()
57 def snapshot_create(self):
59 if os.path.isfile(self.snapshot_file):
60 answer = raw_input("It seems there is already an OpenStack "
61 "snapshot. Do you want to overwrite it with "
62 "the current OpenStack status? [y|n]\n")
64 if answer.lower() in ["y", "yes"]:
66 elif answer.lower() in ["n", "no"]:
69 answer = raw_input("Invalid answer. Please type [y|n]\n")
71 click.echo("Generating Openstack snapshot...")
74 def snapshot_show(self):
75 if not os.path.isfile(self.snapshot_file):
76 click.echo("There is no OpenStack snapshot created. To create "
77 "one run the command "
78 "'functest openstack snapshot-create'")
80 with open(self.snapshot_file, 'r') as yaml_file:
86 if not os.path.isfile(self.snapshot_file):
87 click.echo("Not possible to clean OpenStack without a snapshot. "
88 "This could cause problems. "
89 "Run first the command "
90 "'functest openstack snapshot-create'")
95 class CliOpenStack(OpenStack):
98 super(CliOpenStack, self).__init__()
101 def show_credentials():
102 dic_credentials = OpenStack.show_credentials()
103 for key, value in dic_credentials.items():
104 if key.startswith('OS_'):
105 click.echo("{}={}".format(key, value))