Skip to content
Snippets Groups Projects
Commit c027acba authored by jurgenhaas's avatar jurgenhaas
Browse files

Change script framework from Python to Yaml

parent 8c53931d
Branches
Tags
No related merge requests found
Showing
with 220 additions and 147 deletions
......@@ -11,19 +11,24 @@ to be executed as the first argument.
import argparse
import sys
import os
import importlib
import yaml
from tabulate import tabulate
from lib import general
path = os.path.dirname(os.path.realpath(__file__)) + os.path.sep + 'scripts' + os.path.sep
def list():
scripts = []
for file in sorted(os.listdir(os.path.dirname(os.path.realpath(__file__)) + os.path.sep + 'scripts')):
if file != '__init__.py' and '.pyc' not in file:
scriptname = file.split('.')[0]
script = importlib.import_module('.' + scriptname, package='scripts')
scripts.append([scriptname, script.__doc__.splitlines()[0]])
for file in sorted(os.listdir(path)):
name, ext = os.path.splitext(file)
if ext == '.yml':
with open(path + file, 'r') as config:
try:
script = yaml.load(config)
scripts.append([name, script['description']])
except yaml.YAMLError:
pass
print(tabulate(scripts, headers=['Script', 'Description']))
exit(0)
......@@ -40,12 +45,12 @@ def script():
if arg == 'list':
list()
try:
script = importlib.import_module('.' + arg, package='scripts')
except Exception:
raise Exception('Script %s does not exist.' % (arg))
return script
with open(path + arg + '.yml', 'r') as config:
try:
return yaml.load(config)
except Exception:
pass
raise Exception('Script %s does not exist.' % (arg))
parser = argparse.ArgumentParser(description='Ansible script by Paragon',
......
......@@ -4,6 +4,7 @@ import yaml
import uservars
import threading
import Queue
import types
import window
from subprocess import Popen, PIPE, STDOUT
......@@ -25,16 +26,24 @@ def run(parser, script=None):
help='Disable GUI')
if not os.path.exists(path + 'inventory/group_vars'):
parser.add_argument('--company', default=os.environ.get('ANSIBLE_COMPANY', None),
required=(not isGitlabUser(env)),
required=companyRequired(env),
help='Only useful if inventories from multiple companies are installed')
parser.add_argument('--include-local', action='store_true', default=False,
help='If company=all, this would also include the local inventory, default to False')
if script:
parser.description = script.__doc__
parser.description = script['description']
parser.add_argument('--custom', action='store_true', default=False,
help='Only run a custom playbook and ignore the global one')
script.extend(parser)
if 'cli' in script:
for type in ['arguments', 'options']:
prefix = '--' if type == 'options' else ''
if type in script['cli']:
for item in script['cli'][type]:
parser.add_argument(prefix + item, **script['cli'][type][item])
if 'defaults' in script['cli']:
for item in script['cli']['defaults']:
parser.set_defaults(**{item: script['cli']['defaults'][item]})
args, extras = parser.parse_known_args()
......@@ -42,7 +51,12 @@ def run(parser, script=None):
window.guiAvailable = False
if script:
script.append(args)
if 'arguments' in script:
for item in script['arguments']:
value = script['arguments'][item]
if not isinstance(value, types.StringTypes):
value = ','.join(value)
setattr(args, item, value)
if 'playbook' in args and 'limit' in args:
extras.append('--limit=' + args.limit)
......@@ -110,8 +124,8 @@ def followUp(script, args):
if error_seen:
exit(1)
if script and hasattr(script, 'follower'):
cmd = ['ascr', script.follower(), '--limit=' + args.limit]
if 'follower' in script:
cmd = ['ascr', script['follower'], '--limit=' + args.limit]
if hasattr(args, 'company'):
cmd.append('--company=' + args.company)
if not window.guiAvailable:
......@@ -144,6 +158,12 @@ def startThreads(company, args, files, path, extras, pathSecrets, script):
thread.join()
def companyRequired(env):
if 'ANSIBLE_COMPANY' in env:
return False
return not isGitlabUser(env)
def isGitlabUser(env):
return ('USER' in env and env['USER'] == 'gitlab-runner')
......@@ -209,7 +229,21 @@ def main(args, extras, path, pathSecrets, company = None, script = None):
cmd.append('--extra-vars={excluded_roles: ' + json.dumps(excluded_roles) + '}')
if script:
script.build(cmd, args)
if 'command' in script:
for item in script['command']:
condition = True
if 'condition' in item:
condition = bool(getattr(args, item['condition']))
if condition:
value = item['value']
if not isinstance(value, types.StringTypes):
value = ','.join(value)
if 'args' in item:
commandargs = []
for commandarg in item['args']:
commandargs.append(getattr(args, commandarg))
value = value % tuple(commandargs)
cmd.append('--' + item['name'] + '=' + value)
# Append more CLI options
for extra in extras:
......
description: 'Quickly configure Apache'
cli:
options:
application:
default: 'apache'
help: 'The application to configure Apache for, e.g. apache, drupal, piwk, nextcloud'
arguments:
playbook: 'role'
roles_exclude:
- 'commonauth'
- 'commonconnect'
- 'common'
- 'php'
- 'jailkit'
command:
- name: 'tags'
value: 'ApacheConfig'
- name: 'extra-vars'
value: 'hosts=%s role=%s'
args:
- 'limit'
- 'application'
"""Quickly configure Apache"""
def extend(parser):
parser.add_argument('--application', default='apache',
help='The application to configure Apache for, e.g. apache, drupal, piwk, nextcloud')
def append(args):
args.playbook = 'role'
args.roles_exclude = 'commonauth,commonconnect,common,php,jailkit'
def build(cmd, args):
cmd.append('--tags=ApacheConfig')
cmd.append('--extra-vars=hosts=' + args.limit + ' role=' + args.application)
"""Find big files / directories"""
def extend(parser):
parser.add_argument('--depth', default=5,
help='How many directories deep this should parse')
parser.add_argument('--threshold', default='1G',
help='Only show directories bigger than this')
def append(args):
pass
def build(cmd, args):
cmd.append('-m')
cmd.append('command')
cmd.append('-a')
cmd.append('du -xchd ' + str(args.depth) + ' --threshold=' + args.threshold + ' /')
description: 'Find big files / directories'
cli:
options:
depth:
default: 5
help: 'How many directories deep this should parse'
threshold:
default: '1G'
help: 'Only show directories bigger than this'
command:
- name: 'module-name'
value: 'command'
- name: 'args'
value: 'du -xchd %s --threshold=%s /'
args:
- 'depth'
- 'threshold'
"""Update Composer itself and global packages"""
def extend(parser):
parser.set_defaults(limit='webserver')
def append(args):
args.playbook = 'role'
def build(cmd, args):
cmd.append('--extra-vars=hosts=' + args.limit + ' role=composer')
cmd.append('--tags=update')
def follower():
return 'jailkitupgrade'
description: 'Update Composer itself and global packages'
cli:
defaults:
limit: 'webserver'
arguments:
playbook: 'role'
command:
- name: 'extra-vars'
value: 'hosts=%s role=composer'
args:
- 'limit'
- name: 'tags'
value: 'update'
follower: 'jailkitupgrade'
"""Update hosts and ssh config"""
def extend(parser):
parser.set_defaults(limit='all')
parser.set_defaults(include_local=True)
def append(args):
args.playbook = 'role'
def build(cmd, args):
cmd.append('--extra-vars=hosts=' + args.limit + ' role=commonconnect')
description: 'Update hosts and ssh config'
cli:
defaults:
limit: 'all'
include_local: True
arguments:
playbook: 'role'
command:
- name: 'extra-vars'
value: 'hosts=%s role=commonconnect'
args:
- 'limit'
"""Update Cron Jobs"""
def extend(parser):
pass
def append(args):
args.playbook = 'farm'
def build(cmd, args):
cmd.append('--tags=cron')
description: 'Update Cron Jobs'
arguments:
playbook: 'farm'
command:
- name: 'tags'
value: 'cron'
description: 'Quickly configure Drupal sites'
cli:
options:
site:
default: None
help: 'The id of a site if only one of the defined sites should be updated'
quick:
action: 'store_true'
default: False
help: 'Only Drupal itself, not the CLI or JailKit'
defaults:
limit: 'webserver-drupal'
arguments:
playbook: 'role'
roles_exclude:
- 'commonauth'
- 'commonconnect'
- 'common'
- 'php'
- 'apache'
command:
- name: 'extra-vars'
value: 'hosts=%s role=drupal'
args:
- 'limit'
- name: 'extra-vars'
condition: 'site'
value: 'limit_drupal_site=%s'
args:
- 'site'
- name: 'start-at-task'
condition: 'quick'
value: 'Drupal Role'
description: 'Quickly re-deploy a Drupal site'
cli:
arguments:
host:
default: None
help: 'The hostname on which to re-reploy Drupal'
site:
default: None
help: 'The id of a site'
arguments:
playbook: 'role'
roles_exclude:
- 'commonauth'
- 'commonconnect'
- 'common'
- 'php'
- 'apache'
- 'jailkit'
command:
- name: 'tags'
value: 'deploy'
- name: 'extra-vars'
value: 'hosts=%s role=drupal'
args:
- 'host'
- name: 'extra-vars'
value: 'limit_drupal_site=%s'
args:
- 'site'
- name: 'start-at-task'
value: 'Install Drupal'
"""Quickly configure Drupal sites"""
def extend(parser):
parser.add_argument('--site', default=None,
help='The id of a site if only one of the defined sites should be updated')
parser.add_argument('--quick', action='store_true', default=False,
help='Only Drupal itself, not the CLI or JailKit')
parser.set_defaults(limit='webserver-drupal')
def append(args):
args.playbook = 'role'
args.roles_exclude = 'commonauth,commonconnect,common,php,apache'
def build(cmd, args):
cmd.append('--extra-vars=hosts=' + args.limit + ' role=drupal')
if args.site:
cmd.append('--extra-vars=limit_drupal_site=' + args.site)
if args.quick:
cmd.append('--start-at-task=Drupal Role')
"""Quickly re-deploy a Drupal site"""
def extend(parser):
parser.add_argument('host', default=None,
help='The hostname on which to re-reploy Drupal')
parser.add_argument('site', default=None,
help='The id of a site')
def append(args):
args.playbook = 'role'
args.roles_exclude = 'commonauth,commonconnect,common,php,apache,jailkit'
def build(cmd, args):
cmd.append('--tags=deploy')
cmd.append('--extra-vars=hosts=' + args.host + ' role=drupal')
cmd.append('--extra-vars=limit_drupal_site=' + args.site)
cmd.append('--start-at-task=Install Drupal')
description: 'Retrieve and adjust Drush aliases from remote host(s)'
cli:
defaults:
limit: 'webserver-drupal'
arguments:
playbook: 'role'
command:
- name: 'tags'
value: 'DrushUpdateAliases'
- name: 'extra-vars'
value: 'hosts=%s role=drush'
args:
- 'limit'
"""Update rules on ElastAlert hosts"""
def extend(parser):
parser.set_defaults(limit='logserver')
def append(args):
args.playbook = 'role'
def build(cmd, args):
cmd.append('--tags=Rules')
cmd.append('--extra-vars=hosts=' + args.limit + ' role=elasticsearch')
description: 'Update rules on ElastAlert hosts'
cli:
defaults:
limit: 'logserver'
arguments:
playbook: 'role'
command:
- name: 'tags'
value: 'Rules'
- name: 'extra-vars'
value: 'hosts=%s role=elasticsearch'
args:
- 'limit'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment