Reverted the file permission
[functest-xtesting.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
15 import functest.utils.functest_utils as ft_utils
16 import functest.utils.functest_constants as ft_constants
17
18
19 class CliEnv:
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(ft_constants.ENV_FILE)
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         cmd = ("python %s/functest/ci/prepare_env.py start" %
39                ft_constants.FUNCTEST_REPO_DIR)
40         ft_utils.execute_command(cmd)
41
42     def show(self):
43         CI_INSTALLER_TYPE = ft_constants.CI_INSTALLER_TYPE
44         if CI_INSTALLER_TYPE is None:
45             CI_INSTALLER_TYPE = "Unknown"
46         CI_INSTALLER_IP = ft_constants.CI_INSTALLER_IP
47         if CI_INSTALLER_IP is None:
48             CI_INSTALLER_IP = "Unknown"
49         CI_INSTALLER = ("%s, %s" % (CI_INSTALLER_TYPE, CI_INSTALLER_IP))
50
51         CI_SCENARIO = ft_constants.CI_SCENARIO
52         if CI_SCENARIO is None:
53             CI_SCENARIO = "Unknown"
54
55         CI_NODE = ft_constants.CI_NODE
56         if CI_NODE is None:
57             CI_NODE = "Unknown"
58
59         repo = git.Repo(ft_constants.FUNCTEST_REPO_DIR)
60         branch = repo.head.reference
61         GIT_BRANCH = branch.name
62         GIT_HASH = branch.commit.hexsha
63
64         CI_BUILD_TAG = ft_constants.CI_BUILD_TAG
65         if CI_BUILD_TAG is not None:
66             CI_BUILD_TAG = CI_BUILD_TAG.lstrip(
67                 "jenkins-").lstrip("functest").lstrip("-")
68
69         CI_DEBUG = ft_constants.CI_DEBUG
70         if CI_DEBUG is None:
71             CI_DEBUG = "false"
72
73         STATUS = "not ready"
74         if self.status(verbose=False) == 0:
75             STATUS = "ready"
76
77         click.echo("+======================================================+")
78         click.echo("| Functest Environment info                            |")
79         click.echo("+======================================================+")
80         click.echo("|  INSTALLER: %s|" % CI_INSTALLER.ljust(41))
81         click.echo("|   SCENARIO: %s|" % CI_SCENARIO.ljust(41))
82         click.echo("|        POD: %s|" % CI_NODE.ljust(41))
83         click.echo("| GIT BRACNH: %s|" % GIT_BRANCH.ljust(41))
84         click.echo("|   GIT HASH: %s|" % GIT_HASH.ljust(41))
85         if CI_BUILD_TAG:
86             click.echo("|  BUILD TAG: %s|" % CI_BUILD_TAG.ljust(41))
87         click.echo("| DEBUG FLAG: %s|" % CI_DEBUG.ljust(41))
88         click.echo("+------------------------------------------------------+")
89         click.echo("|     STATUS: %s|" % STATUS.ljust(41))
90         click.echo("+------------------------------------------------------+")
91         click.echo("")
92
93     def status(self, verbose=True):
94         ret_val = 0
95         if not os.path.isfile(ft_constants.ENV_FILE):
96             if verbose:
97                 click.echo("Functest environment is not installed.\n")
98             ret_val = 1
99         elif verbose:
100             click.echo("Functest environment ready to run tests.\n")
101
102         return ret_val