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