Skip to content

Commit

Permalink
Merge pull request #12 from berttejeda/github/pypackage_missing_lib
Browse files Browse the repository at this point in the history
Mistakenly ignored lib folder!
  • Loading branch information
berttejeda authored Jul 23, 2019
2 parents 96faffa + 28639ad commit 66b5d84
Show file tree
Hide file tree
Showing 17 changed files with 869 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
Expand Down
Empty file.
72 changes: 72 additions & 0 deletions ansible_taskrunner/lib/bash/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Imports
import logging
import sys

provider_name = 'bash'

# Logging
logger = logging.getLogger('logger')
logger.setLevel(logging.INFO)

# Import third-party and custom modules
try:
import click
from .. import reindent
from .. import YamlCLIInvocation
except ImportError as e:
print('Failed to import at least one required module')
print('Error was %s' % e)
print('Please install/update the required modules:')
print('pip install -U -r requirements.txt')
sys.exit(1)


class ProviderCLI:
def __init__(self, parameter_set=None, vars_input={}):
self.vars = vars_input
self.parameter_set = parameter_set
self.logger = logger
pass

@staticmethod
def options(func):
"""Add provider-specific click options"""
return func

@staticmethod
def invocation(yaml_input_file=None,
string_vars=[],
default_vars={},
paramset_var=None,
bash_functions=[],
cli_vars='',
yaml_vars={},
list_vars=[],
debug=False,
args=None,
prefix='',
raw_args='',
kwargs={}):
"""Invoke commands according to provider"""
logger.info('Bash Command Provider')
command = '''
{dsv}
{psv}
{dlv}
{clv}
{bfn}
'''.format(
dlv='\n'.join(list_vars),
dsv='\n'.join(string_vars),
psv=paramset_var,
clv=cli_vars,
bfn='\n'.join(bash_functions),
deb=debug
)
command = reindent(command, 0)
# Command invocation
if prefix == 'echo':
logger.info("ECHO MODE ON")
print(command)
else:
YamlCLIInvocation().call(command)
115 changes: 115 additions & 0 deletions ansible_taskrunner/lib/click_extras/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Imports
import logging
import sys

# Logging
logger = logging.getLogger('logger')
logger.setLevel(logging.INFO)

# Import third-party and custom modules
try:
import click
except ImportError as e:
print('Failed to import at least one required module')
print('Error was %s' % e)
print('pip install -U -r requirements.txt')
sys.exit(1)


class ExtendedEpilog(click.Group):
def format_epilog(self, ctx, formatter):
"""Format click epilog to honor newline characters"""
if self.epilog:
formatter.write_paragraph()
for line in self.epilog.split('\n'):
formatter.write_text(line)


class ExtendedHelp(click.Command):
def format_help(self, ctx, formatter):
"""Format click help to honor newline characters"""
if self.help:
formatter.write_paragraph()
for line in self.help.split('\n'):
formatter.write_text(line)
opts = []
for param in self.get_params(ctx):
rv = param.get_help_record(ctx)
if rv is not None:
opts.append(rv)
if opts:
with formatter.section('Options'):
formatter.write_dl(opts)
if self.epilog:
formatter.write_paragraph()
for line in self.epilog.split('\n'):
formatter.write_text(line)


class ExtendCLI():
def __init__(self, parameter_set=None, vars_input={}):
self.vars = vars_input
self.parameter_set = parameter_set
self.logger = logger
pass

def process_options(self, parameters, func, is_required=False):
"""Read dictionary of parameters and translate to click options"""
if not parameters:
parameters = {}
numargs = 1
numargs_unlimited_is_set = False
numargs_unlimited_count = 0
numargs_unlimited_count_max = 1
vanilla_parameters = dict(
[(k, v) for k, v in parameters.items() if not isinstance(parameters[k], dict)])
if self.parameter_set:
_parameters = parameters.get(self.parameter_set, {})
if _parameters:
parameters = {}
parameters.update(vanilla_parameters)
parameters.update(_parameters)
else:
parameters = vanilla_parameters
for cli_option, value in parameters.items():
if isinstance(value, list):
if len(value) == 1:
numargs_unlimited_is_set = True
numargs_unlimited_count += 1
value = str(value[0])
numargs = -1
elif len(value) > 1:
numargs = value[1]
value = str(value[0])
option_help = value
if '|' in cli_option:
option_string = cli_option.split('|')
first_option = option_string[0].strip()
second_option = option_string[1].strip()
numargs = 1 if numargs < 1 else numargs
option = click.option(first_option, second_option, value, type=str,
nargs=numargs, help=option_help, required=is_required)
else:
if numargs_unlimited_is_set and not numargs_unlimited_count > numargs_unlimited_count_max:
option = click.argument(
cli_option, nargs=numargs, required=is_required)
else:
numargs = 1 if numargs < 1 else numargs
option = click.option(
cli_option, value, is_flag=True, default=False, help=option_help, required=is_required)
func = option(func)
numargs_unlimited_is_set = False
numargs = 1
return func

def options(self, func):
"""Read dictionary of parameters, append these as additional options to incoming click function"""
required_parameters = self.vars.get('required_parameters', {})
extended_cli_func_required = self.process_options(
required_parameters, func, is_required=True)
optional_parameters = self.vars.get('optional_parameters', {})
extended_cli_func = self.process_options(
optional_parameters, extended_cli_func_required)
# if required_parameters:
# self.logger.warning("The value for 'required_parameters' is invalid")
return extended_cli_func
18 changes: 18 additions & 0 deletions ansible_taskrunner/lib/errorhandler/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ERR_ARGS_TASKF_OVERRIDE = '''
When specifying a task file override, you must do so like this:
{script} -f /path/to/mytasks.yaml
{script} --tasks-file /path/to/mytasks.yaml
Additionally, only task file overrides ending in .yml or .yaml are accepted
'''

# see https://stackoverflow.com/questions/6234405/logging-uncaught-exceptions-in-python


def catchException(logger, script_name, typ, value, traceback):
"""Log uncaught exception to python logger"""
logger.critical("Program Crash")
logger.critical("Type: %s" % typ)
logger.critical("Value: %s" % value)
logger.critical("Traceback: %s" % traceback)
logger.info(
"Run same command but with %s --debug to see full stack trace!" % script_name)
22 changes: 22 additions & 0 deletions ansible_taskrunner/lib/formatting/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Defaults
ansi_colors = """
RESTORE=$(echo -en '\033[0m')
RED=$(echo -en '\033[00;31m')
GREEN=$(echo -en '\033[00;32m')
YELLOW=$(echo -en '\033[00;33m')
BLUE=$(echo -en '\033[00;34m')
MAGENTA=$(echo -en '\033[00;35m')
PURPLE=$(echo -en '\033[00;35m')
CYAN=$(echo -en '\033[00;36m')
LIGHTGRAY=$(echo -en '\033[00;37m')
"""

logging_format = "[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s"


def reindent(s, numSpaces):
"""Remove leading spaces from string see: Python Cookbook by David Ascher, Alex Martelli"""
s = s.split('\n')
s = [(numSpaces * ' ') + line.lstrip() for line in s]
s = '\n'.join(s)
return s
75 changes: 75 additions & 0 deletions ansible_taskrunner/lib/help/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
SAMPLE_CONFIG = '''
---
cli:
providers:
default: ansible
...
'''

SAMPLE_TASKS_MANIFEST = '''
- hosts: myhosts
gather_facts: true
become: true
vars:
myvar1: myvalue1
myvar2: myvalue2
myvar3: myvalue3
myvar4: |
This is a multiline value
of type string
myvar5:
- mylistvalue1
- mylistvalue2
- mylistvalue3
- mylistvalue4
required_parameters:
aws:
-d|--db-hosts: dbhosts_aws
-a|--some-special-aws-flag: aws_flag
gcp:
-d|--db-hosts: dbhosts_gcp
-g|--some-special-gcp-flag: gcp_flag
-d|--db-hosts: dbhosts
-w|--web-hosts: webhosts
-t|--some-parameter: some_value
optional_parameters:
-l|--another-parameter: another_value
-A: hello
-PR: preflight_and_run
--debug-mode: debug_mode
help:
message: |
Do something against db and web hosts
epilog: |
This line will be displayed at the end of the help text readme
examples:
- example1: |
Usage example 1
- example2: |
Usage example 2
inventory: |
[web-hosts]
$(echo ${webhosts} | tr ',' '\\n')
[db-hosts]
$(echo ${dbhosts} | tr ',' '\\n')
[myhosts:children]
deployment-hosts
web-hosts
db-hosts
functions:
hello:
shell: bash
source: |-
echo hello
preflight_and_run:
shell: bash
source: |-
echo 'Running Preflight Tasks!'
tasks run -d dbhost1 -w webhost1 -t value1
tasks:
- debug:
msg: |
Hello from Ansible!
You specified: {{ some_value }}
You specified: {{ another_value }}
'''
21 changes: 21 additions & 0 deletions ansible_taskrunner/lib/language/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Import builtins
import os
import sys

# Import third-party and custom modules
try:
from superduperconfig import SuperDuperConfig
except ImportError as e:
print('Failed to import at least one required module')
print('Error was %s' % e)
print('Please install/update the required modules:')
print('pip install -U -r requirements.txt')
sys.exit(1)


def get_strings():
self_path = os.path.dirname(os.path.abspath(__file__))
__program_name__ = 'tasks'
superconf = SuperDuperConfig(__program_name__)
language_file = '%s/en.yaml' % self_path
return superconf.load_config(language_file)
2 changes: 2 additions & 0 deletions ansible_taskrunner/lib/language/en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
strings:
info:
Empty file.
Loading

0 comments on commit 66b5d84

Please sign in to comment.