From a901f66499fa01619cebe15d48763484baa844b0 Mon Sep 17 00:00:00 2001 From: Luuk van Venrooij Date: Tue, 10 Mar 2020 21:24:00 +0100 Subject: [PATCH] Added better debug options: - Added verbosity levels for Terraform and Ansible (#987) - Added vim to Epicli container and devcontainer (#986) --- core/src/epicli/.devcontainer/Dockerfile | 2 +- core/src/epicli/Dockerfile-debian | 2 +- .../cli/engine/ansible/AnsibleCommand.py | 5 +-- .../cli/engine/terraform/TerraformCommand.py | 5 +-- core/src/epicli/cli/epicli.py | 32 +++++++++++++++---- core/src/epicli/cli/helpers/Config.py | 2 +- 6 files changed, 35 insertions(+), 13 deletions(-) diff --git a/core/src/epicli/.devcontainer/Dockerfile b/core/src/epicli/.devcontainer/Dockerfile index d028d91f82..0783a76a8c 100644 --- a/core/src/epicli/.devcontainer/Dockerfile +++ b/core/src/epicli/.devcontainer/Dockerfile @@ -17,7 +17,7 @@ RUN chmod +x /config-pre.sh \ && apt-get update \ && apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \ - && apt-get -y install git procps lsb-release gcc make musl-dev libffi-dev tar unzip \ + && apt-get -y install git procps lsb-release gcc make musl-dev libffi-dev tar unzip vim \ && apt-get -y install ruby-full \ diff --git a/core/src/epicli/Dockerfile-debian b/core/src/epicli/Dockerfile-debian index c2c1a51f46..45d196d70e 100644 --- a/core/src/epicli/Dockerfile-debian +++ b/core/src/epicli/Dockerfile-debian @@ -8,7 +8,7 @@ COPY /dist/ /epicli WORKDIR /epicli RUN apt-get update \ - && apt-get -y install gcc make musl-dev libffi-dev tar unzip openssh-client + && apt-get -y install gcc make musl-dev libffi-dev tar unzip openssh-client vim RUN pip install epicli-${EPICLI_VERSION}-py3-none-any.whl diff --git a/core/src/epicli/cli/engine/ansible/AnsibleCommand.py b/core/src/epicli/cli/engine/ansible/AnsibleCommand.py index 6cfe19f2a7..aae5f9d9b8 100644 --- a/core/src/epicli/cli/engine/ansible/AnsibleCommand.py +++ b/core/src/epicli/cli/engine/ansible/AnsibleCommand.py @@ -4,6 +4,7 @@ from cli.helpers.Config import Config import time +ansible_verbosity = ['NONE','-v','-vv','-vvv','-vvvv'] class AnsibleCommand: @@ -62,8 +63,8 @@ def run_playbook(self, inventory, playbook_path, vault_file=None): cmd.append(playbook_path) - if Config().debug: - cmd.append('-vvv') + if Config().debug > 0: + cmd.append(ansible_verbosity[Config().debug]) self.logger.info('Running: "' + ' '.join(playbook_path) + '"') diff --git a/core/src/epicli/cli/engine/terraform/TerraformCommand.py b/core/src/epicli/cli/engine/terraform/TerraformCommand.py index d04fb09b73..b325fcc85b 100644 --- a/core/src/epicli/cli/engine/terraform/TerraformCommand.py +++ b/core/src/epicli/cli/engine/terraform/TerraformCommand.py @@ -3,6 +3,7 @@ from cli.helpers.Log import LogPipe, Log from cli.helpers.Config import Config +terraform_verbosity = ['ERROR','WARN','INFO','DEBUG','TRACE'] class TerraformCommand: @@ -41,8 +42,8 @@ def run(self, command, env, auto_approve=False): cmd = ' '.join(cmd) self.logger.info(f'Running: "{cmd}"') - if Config().debug: - env['TF_LOG'] = 'TRACE' + if Config().debug > 0: + env['TF_LOG'] = terraform_verbosity[Config().debug] logpipe = LogPipe(__name__) with subprocess.Popen(cmd, stdout=logpipe, stderr=logpipe, env=env, shell=True) as sp: diff --git a/core/src/epicli/cli/epicli.py b/core/src/epicli/cli/epicli.py index 7b36c5bf73..481ad7c300 100644 --- a/core/src/epicli/cli/epicli.py +++ b/core/src/epicli/cli/epicli.py @@ -25,7 +25,7 @@ def main(): parser = argparse.ArgumentParser( description=__doc__, usage='''epicli []''', - formatter_class=argparse.RawDescriptionHelpFormatter) + formatter_class=argparse.RawTextHelpFormatter) # setup some root arguments parser.add_argument('--version', action='version', help='Shows the CLI version', version=VERSION) @@ -45,12 +45,32 @@ def main(): parser.add_argument('--validate-certs', choices=['true', 'false'], default='true', action='store', dest='validate_certs', help='''[Experimental]: Disables certificate checks for certain Ansible operations - which might have issues behind proxies (https://github.com/ansible/ansible/issues/32750). - Should NOT be used in production for security reasons.''') - parser.add_argument('--debug', dest='debug', action="store_true", - help='Set this to output extensive debug information. Carries over to Ansible and Terraform.') +which might have issues behind proxies (https://github.com/ansible/ansible/issues/32750). +Should NOT be used in production for security reasons.''') parser.add_argument('--auto-approve', dest='auto_approve', action="store_true", help='Auto approve any user input queries asked by Epicli') + + # set debug verbosity level. + def debug_level(x): + x = int(x) + if x < 0 or x > 4: + raise argparse.ArgumentTypeError("--debug value should be between 0 and 4") + return x + parser.add_argument('--debug', dest='debug', type=debug_level, + help='''Set this flag (0..4) to enable debug output where 0 is no +debug output and 1..4 is debug output with different verbosity levels: +Python : Anything heigher then 0 enables printing of Python stacktraces +Ansible : 1..4 map to following Ansible verbosity levels: + 1: -v + 2: -vv + 3: -vvv + 4: -vvvv +Terraform : 1..4 map to the following Terraform verbosity levels: + 1: WARN + 2: INFO + 3: DEBUG + 4: TRACE''') + # some arguments we don't want available when running from the docker image. if not config.docker_cli: parser.add_argument('-o', '--output', dest='output_dir', type=str, @@ -95,7 +115,7 @@ def main(): return args.func(args) except Exception as e: logger = Log('epicli') - logger.error(e, exc_info=config.debug) + logger.error(e, exc_info=(config.debug > 0)) return 1 diff --git a/core/src/epicli/cli/helpers/Config.py b/core/src/epicli/cli/helpers/Config.py index fe62fcb98a..11a4e59bac 100644 --- a/core/src/epicli/cli/helpers/Config.py +++ b/core/src/epicli/cli/helpers/Config.py @@ -17,7 +17,7 @@ def __init__(self): self._log_type = 'plain' self._validate_certs = True - self._debug = False + self._debug = 0 self._auto_approve = False self._offline_requirements = '' self._wait_for_pods = False