aboutsummaryrefslogtreecommitdiffstats
path: root/functest/cli/commands/cli_os.py
blob: a543e845750453e092cc16677e7834aa6c41a1a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
#
# jose.lausuch@ericsson.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

# pylint: disable=missing-docstring

import os

import click
from six.moves import urllib

from functest.ci import check_deployment
from functest.utils.constants import CONST


class OpenStack(object):

    def __init__(self):
        self.os_auth_url = CONST.__getattribute__('OS_AUTH_URL')
        self.endpoint_ip = None
        self.endpoint_port = None
        self.openstack_creds = CONST.__getattribute__('openstack_creds')
        if self.os_auth_url:
            self.endpoint_ip = urllib.parse.urlparse(self.os_auth_url).hostname
            self.endpoint_port = urllib.parse.urlparse(self.os_auth_url).port

    def ping_endpoint(self):
        if self.os_auth_url is None:
            click.echo("Source the OpenStack credentials first '. $creds'")
            exit(0)
        response = os.system("ping -c 1 " + self.endpoint_ip + ">/dev/null")
        if response == 0:
            return 0
        else:
            click.echo("Cannot talk to the endpoint %s\n" % self.endpoint_ip)
            exit(0)

    @staticmethod
    def show_credentials():
        dic_credentials = {}
        for key, value in os.environ.items():
            if key.startswith('OS_'):
                dic_credentials.update({key: value})
        return dic_credentials

    def check(self):
        self.ping_endpoint()
        deployment = check_deployment.CheckDeployment()
        deployment.check_all()


class CliOpenStack(OpenStack):

    @staticmethod
    def show_credentials():
        dic_credentials = OpenStack.show_credentials()
        for key, value in dic_credentials.items():
            if key.startswith('OS_'):
                click.echo("{}={}".format(key, value))