Merge "Use PrettyTable to show functest env"
[functest.git] / functest / cli / commands / cli_env.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 import os
11
12 import click
13 import git
14 import prettytable
15
16 from functest.utils.constants import CONST
17 import functest.utils.functest_utils as ft_utils
18
19
20 class CliEnv(object):
21
22     def __init__(self):
23         pass
24
25     def prepare(self):
26         if self.status(verbose=False) == 0:
27             answer = raw_input("It seems that the environment has been "
28                                "already prepared. Do you want to do "
29                                "it again? [y|n]\n")
30             while True:
31                 if answer.lower() in ["y", "yes"]:
32                     os.remove(CONST.__getattribute__('env_active'))
33                     break
34                 elif answer.lower() in ["n", "no"]:
35                     return
36                 else:
37                     answer = raw_input("Invalid answer. Please type [y|n]\n")
38
39         cmd = ("python %s/functest/ci/prepare_env.py start" %
40                CONST.__getattribute__('dir_repo_functest'))
41         ft_utils.execute_command(cmd)
42
43     def show(self):
44         def _get_value(attr, default='Unknown'):
45             return attr if attr else default
46
47         install_type = _get_value(CONST.__getattribute__('INSTALLER_TYPE'))
48         installer_ip = _get_value(CONST.__getattribute__('INSTALLER_IP'))
49         installer_info = ("%s, %s" % (install_type, installer_ip))
50         scenario = _get_value(CONST.__getattribute__('DEPLOY_SCENARIO'))
51         node = _get_value(CONST.__getattribute__('NODE_NAME'))
52         repo_h = git.Repo(CONST.__getattribute__('dir_repo_functest')).head
53         if repo_h.is_detached:
54             git_branch = 'detached from FETCH_HEAD'
55             git_hash = repo_h.commit.hexsha
56         else:
57             branch = repo_h.reference
58             git_branch = branch.name
59             git_hash = branch.commit.hexsha
60         is_debug = _get_value(CONST.__getattribute__('CI_DEBUG'), 'false')
61         build_tag = CONST.__getattribute__('BUILD_TAG')
62         if build_tag is not None:
63             build_tag = build_tag.lstrip(
64                 "jenkins-").lstrip("functest").lstrip("-")
65
66         STATUS = "not ready"
67         if self.status(verbose=False) == 0:
68             STATUS = "ready"
69
70         msg = prettytable.PrettyTable(
71             header_style='upper', padding_width=5,
72             field_names=['Functest Environment', 'value'])
73         msg.add_row(['INSTALLER', installer_info])
74         msg.add_row(['SCENARIO', scenario])
75         msg.add_row(['POD', node])
76         msg.add_row(['GIT BRANCH', git_branch])
77         msg.add_row(['GIT HASH', git_hash])
78         if build_tag:
79             msg.add_row(['BUILD TAG', build_tag])
80         msg.add_row(['DEBUG FLAG', is_debug])
81         msg.add_row(['STATUS', STATUS])
82         click.echo(msg.get_string())
83
84     def status(self, verbose=True):
85         ret_val = 0
86         if not os.path.isfile(CONST.__getattribute__('env_active')):
87             if verbose:
88                 click.echo("Functest environment is not installed.\n")
89             ret_val = 1
90         elif verbose:
91             click.echo("Functest environment ready to run tests.\n")
92
93         return ret_val