diff --git a/scripttease/library/snippets/__init__.py b/scripttease/library/snippets/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scripttease/library/snippets/centos.py b/scripttease/library/snippets/centos.py new file mode 100644 index 0000000..1c74c73 --- /dev/null +++ b/scripttease/library/snippets/centos.py @@ -0,0 +1,28 @@ +centos = { + 'apache': { + 'reload': "apachectl -k reload", + 'restart': "apachectl -k restart", + 'start': "apachectl -k start", + 'stop': "apachectl -k stop", + 'test': "apachectl configtest", + }, + 'install': "yum install -y {{ args[0] }}", + 'reload': "systemctl reload {{ args[0] }}", + 'restart': "systemctl restart {{ args[0] }}", + 'start': "systemctl start {{ args[0] }}", + 'stop': "systemctl stop {{ args[0] }}", + 'system': { + 'reboot': "reboot", + 'update': "yum check-update", + 'upgrade': "yum update -y", + }, + 'uninstall': "yum remove -y {{ args[0] }}", + 'user': { + 'create': [ + "adduser {{ args[0] }}", + "{% if home %}--home {{ home }}{% endif %}", + "{% if groups %}&& {% for group in groups %}gpasswd -a {{ args[0] }} {{ group }};{% endfor %}{% endif %}" + ], + 'remove': "userdel -r {{ args[0] }}", + } +} diff --git a/scripttease/library/snippets/django.py b/scripttease/library/snippets/django.py new file mode 100644 index 0000000..54eeae6 --- /dev/null +++ b/scripttease/library/snippets/django.py @@ -0,0 +1,113 @@ +from commonkit import parse_jinja_string + + +def django_command_parser(snippet, args=None): + + # We need to remove the common options so any remaining keyword arguments are converted to switches for the + # management command. + _kwargs = snippet.kwargs.copy() + _kwargs.pop("comment") + _kwargs.pop("environments", None) + _kwargs.pop("prefix", None) + _kwargs.pop("cd", None) + _kwargs.pop("register", None) + _kwargs.pop("shell", None) + _kwargs.pop("stop", None) + _kwargs.pop("tags", None) + _kwargs.pop("venv", None) + + # We need to remove some parameters for dumpdata and loaddata. Otherwise they end up as switches. + if snippet.name in ("django.dumpdata", "django.loaddata"): + app_name = _kwargs.pop("app", None) + model_name = _kwargs.pop("model", None) + + default_path = "fixtures/%s/initial.json" % app_name + if model_name: + default_path = "fixtures/%s/%s.json" % (app_name, model_name.lower()) + + path = _kwargs.pop("path", default_path) + if 'path' not in snippet.kwargs: + snippet.kwargs['path'] = path + + a = list() + command_name = None + for key, value in _kwargs.items(): + if key == "_name": + command_name = value + continue + + key = key.replace("_", "-") + if type(value) is bool: + if value is True: + a.append("--%s" % key) + else: + a.append("--%s=%s" % (key, value)) + + context = snippet.context.copy() + context['args'] = args or snippet.args + context['command_name'] = command_name + context['switches'] = " ".join(a) + context.update(snippet.kwargs) + + if type(snippet.content) is list: + b = list() + for i in snippet.content: + b.append(parse_jinja_string(i, context)) + + return " ".join(b) + + return parse_jinja_string(snippet.content, context) + + +# def django_command_builder(tokens, *args, **kwargs): +# a = list() +# +# command_name = tokens.pop(0) +# if command_name == "command": +# command_name = tokens.pop(0) +# +# params = django_convert_params(*args, **kwargs) +# +# a.append("./manage.py %s" % command_name) +# if len(list(params)) > 0: +# a.append(" ".join(list(params))) +# +# return a +# +# +# def django_convert_params(*args, **kwargs): +# a = list() +# for key, value in kwargs.items(): +# key = key.replace("_", "-") +# if type(value) is bool: +# if value is True: +# a.append("--%s" % key) +# else: +# a.append("--%s=%s" % (key, value)) +# +# return " ".join(list(args)), " ".join(a) + + +django = { + 'django': { + 'check': "./manage.py check {{ switches }}", + 'command': "./manage.py {{ command_name }} {% if args %}{{ ' '.join(args) }}{% endif %} {{ switches }}", + 'dumpdata': [ + "./manage.py dumpdata {{ app }}{% if model %}.{{ model }}{% endif %}", + "--indent=4", + "{{ switches }}", + '> {{ path }}', + ], + 'loaddata': [ + "./manage.py loaddata", + "{{ switches }}" + '{{ path }}', + ], + 'migrate': "./manage.py migrate {{ switches }}", + 'static': "./manage.py collectstatic {{ switches }}", + '_default': "command", + '_parser': django_command_parser, + '_prefix': "source {{ virtualenv }}/bin/activate", + '_register': ["check", "migrate"] + } +} diff --git a/scripttease/library/snippets/messages.py b/scripttease/library/snippets/messages.py new file mode 100644 index 0000000..65ad7c4 --- /dev/null +++ b/scripttease/library/snippets/messages.py @@ -0,0 +1,20 @@ +messages = { + 'dialog': [ + "dialog", + "--clear", + '--backtitle "{{ title|default("Message") }}', + '--msgbox "{{ args[0] }}" {{ height|default("15") }} {{ width|default("100") }};' + 'clear;' + ], + 'echo': 'echo "{{ args[0] }}"', + 'slack': [ + "curl -X POST -H 'Content-type: application/json' --data", + '{"text": "{{ args[0] }}"}', + "{{ url }}", + ], + 'twist': [ + "curl -X POST -H 'Content-type: application/json' --data", + '{"content": "{{ args[0] }}", "title": "{{ title|default("Notice") }}"}', + "{{ url }}", + ], +} \ No newline at end of file diff --git a/scripttease/library/snippets/mysql.py b/scripttease/library/snippets/mysql.py new file mode 100644 index 0000000..c841dfe --- /dev/null +++ b/scripttease/library/snippets/mysql.py @@ -0,0 +1,87 @@ +mysql = { + 'mysql': { + 'create': [ + "mysqladmin create", + '--user={{ admin_user|default("root") }}', + '{% if admin_pass %}--password="{{ admin_pass }}"{% endif %}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("3306") }}', + "{{ args[0] }}", + '{% if owner %}&& mysql --user {{ admin_user|default("root") }} ' + '{% if admin_pass %}--password="{{ admin_pass }}"{% endif %} ' + '--host={{ host|default("localhost") }} ' + '--port={{ port|default("3306") }} ' + '--execute="GRANT ALL ON {{ args[0] }}.* TO \'{{ owner }}\'@\'{{ host|default("localhost") }}\'"' + '{% endif %}' + ], + 'drop': [ + "mysqladmin drop", + '--user={{ admin_user|default("root") }}', + '{% if admin_pass %}--password="{{ admin_pass }}"{% endif %}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("3306") }}', + "{{ args[0] }}", + ], + 'dump': [ + "mysqldump", + '--user={{ admin_user|default("root") }}', + '{% if admin_pass %}--password="{{ admin_pass }}"{% endif %}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("3306") }}', + '--complete-inserts', + '{{ args[0] }} > {{ path|default("dump.sql") }}', + ], + 'exec': [ + "mysql", + '--user={{ admin_user|default("root") }}', + '{% if admin_pass %}--password="{{ admin_pass }}"{% endif %}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("3306") }}', + '--execute="{{ args[0] }}"', + '{{ database|default("default") }}', + ], + 'exists': [ + "mysql", + '--user={{ admin_user|default("root") }}', + '{% if admin_pass %}--password="{{ admin_pass }}"{% endif %}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("3306") }}', + '--execute="SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = \'{{ args[0] }}\'"', + ], + 'grant': [ + "mysql", + '--user={{ admin_user|default("root") }}', + '{% if admin_pass %}--password="{{ admin_pass }}"{% endif %}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("3306") }}', + '--execute="GRANT {{ args[0] }} ON {{ database }}.* TO \'{{ user }}\'@\'{{ host|default("localhost") }}\'"' + ], + 'user': { + 'create': [ + "mysql", + '--user={{ admin_user|default("root") }}', + '{% if admin_pass %}--password="{{ admin_pass }}"{% endif %}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("3306") }}', + '--execute="CREATE USER IF NOT EXISTS \'{{ args[0] }}\'@\'{{ host|default("localhost") }}\'" ' + '{% if password %}IDENTIFIED BY PASSWORD(\'{{ password }}\'{% endif %}' + ], + 'drop': [ + "mysql", + '--user={{ admin_user|default("root") }}', + '{% if admin_pass %}--password="{{ admin_pass }}"{% endif %}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("3306") }}', + '--execute="DROP USER IF EXISTS \'{{ args[0] }}\'@\'{{ host|default("localhost") }}\'"' + ], + 'exists': [ + "mysql", + '--user={{ admin_user|default("root") }}', + '{% if admin_pass %}--password="{{ admin_pass }}"{% endif %}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("3306") }}', + '--execute="SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = \'{{ args[0] }}\')"' + ], + }, + } +} diff --git a/scripttease/library/snippets/pgsql.py b/scripttease/library/snippets/pgsql.py new file mode 100644 index 0000000..73a4900 --- /dev/null +++ b/scripttease/library/snippets/pgsql.py @@ -0,0 +1,115 @@ +from commonkit import parse_jinja_string + + +def pgsql_command_parser(snippet, args=None): + a = list() + + if snippet.admin_pass: + a.append('export PGPASSWORD="%s" &&' % snippet.admin_pass) + + if snippet.admin_user: + a.append("-U %s" % snippet.admin_user) + + if snippet.host: + a.append("--host=%s" % snippet.host) + + if snippet.port: + a.append("--port=%s" % snippet.port) + + context = snippet.context.copy() + context['args'] = args or snippet.args + context.update(snippet.kwargs) + + if type(snippet.content) is list: + b = list() + for i in snippet.content: + b.append(parse_jinja_string(i, context)) + + return " ".join(b) + + return parse_jinja_string(snippet.content, context) + + +pgsql = { + 'pgsql': { + 'create': [ + '{% if admin_pass %}export PGPASSWORD="{{ admin_pass }}" &&{% endif %}', + "createdb", + '-U {{ admin_user|default("postgres") }}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("5432") }}', + "{% if owner %}--owner={{ owner }}{% endif %}", + "{% if template %}--template={{ template }}{% endif %}", + "{{ args[0] }}", + ], + 'drop': [ + '{% if admin_pass %}export PGPASSWORD="{{ admin_pass }}" &&{% endif %}', + "dropdb", + '-U {{ admin_user|default("postgres") }}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("5432") }}', + "{{ args[0] }}", + ], + 'dump': [ + '{% if admin_pass %}export PGPASSWORD="{{ admin_pass }}" &&{% endif %}', + "pd_dump", + '-U {{ admin_user|default("postgres") }}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("5432") }}', + "--column-inserts", + '--file={{ file_name|default("dump.sql") }}', + "{{ args[0] }}" + ], + 'exec': [ + '{% if admin_pass %}export PGPASSWORD="{{ admin_pass }}" &&{% endif %}', + "psql", + '-U {{ admin_user|default("postgres") }}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("5432") }}', + "--dbname={{ database }}", + '-c "{{ args[0] }}"', + ], + 'exists': [ + '{% if admin_pass %}export PGPASSWORD="{{ admin_pass }}" &&{% endif %}', + "psql", + '-U {{ admin_user|default("postgres") }}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("5432") }}', + r"-lqt | cut -d \| -f 1 | grep -qw {{ args[0] }}", + ], + 'user': { + 'create': [ + '{% if admin_pass %}export PGPASSWORD="{{ admin_pass }}" &&{% endif %}', + "createuser", + '-U {{ admin_user|default("postgres") }}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("5432") }}', + "-DRS {{ args[0] }}", + '{% if password %}&& psql -U {{ admin_user|default("postgres") }} ' + '--host={{ host|default("localhost") }} ' + '--port={{ port|default("5432") }} ' + ' -c "ALTER USER {{ args[0] }} WITH ENCRYPTED PASSWORD \'{{ password }}\';"' + '{% endif %}', + ], + 'drop': [ + '{% if admin_pass %}export PGPASSWORD="{{ admin_pass }}" &&{% endif %}', + "dropuser", + '-U {{ admin_user|default("postgres") }}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("5432") }}', + "{{ args[0] }}" + ], + 'exists': [ + '{% if admin_pass %}export PGPASSWORD="{{ admin_pass }}" &&{% endif %}', + "psql", + '-U {{ admin_user|default("postgres") }}', + '--host={{ host|default("localhost") }}', + '--port={{ port|default("5432") }}', + '-c "SELECT 1 FROM pgsql_roles WHERE rolnamme={{ args[0] }};"' + ], + }, + # '_parser': pgsql_command_parser, + '_register': ["exists", 'user.exists'], + } +} + diff --git a/scripttease/library/snippets/posix.py b/scripttease/library/snippets/posix.py new file mode 100644 index 0000000..c8e0dfc --- /dev/null +++ b/scripttease/library/snippets/posix.py @@ -0,0 +1,111 @@ +posix = { + 'append': 'echo "{{ content }}" >> {{ args[0] }}', + 'archive': [ + "tar -cz", + "{% if absolute %}-P{% endif %}", + "{% if view %}-v{% endif %}", + "{% if exclude %}--exclude {{ exclude }}{% endif %}", + "{% if strip %}--strip-components {{ strip }}{% endif %}", + "-f {{ args[0] }} {{ to }}", + ], + 'copy': [ + "cp", + "{% if not overwrite %}-n{% endif %}", + "{% if recursive %}-R{% endif %}", + "{{ args[0] }} {{ args[1] }}" + ], + 'extract': [ + "tar", + "-xz", + "{% if absolute %}-P{% endif %}", + "{% if view %}-v{% endif %}", + "{% if exclude %}--exclude {{ exclude }}{% endif %}", + "{% if strip %}--script-components {{ strip }}{% endif %}", + '-f {{ args[0] }} {{ to|default("./") }}', + ], + 'link': [ + "ln -s", + "{% if force %}-f{% endif %}", + '{{ args[0] }} {{ args[1] }}', + ], + 'mkdir': [ + "mkdir", + "{% if recursive %}-p{% endif %}", + "{% if mode %}-m {{ mode }}{% endif %}", + "{{ args[0] }}", + "{% if group %}&& chgrp -R {{ group }} {{ args[0] }}{% endif %}", + "{% if owner %}&& chown -R {{ owner }} {{ args[0] }}{% endif %}" + ], + 'move': "mv {{ args[0] }} {{ args[1] }}", + 'perms': [ + "{% if group %}chgrp {% if recursive %}-R {% endif %}{{ group }} {{ args[0] }};{% endif %}", + "{% if mode %}chmod {% if recursive %}-R {% endif %}{{ mode }} {{ args[0] }};{% endif %}", + "{% if owner %}chown {% if recursive %}-R {% endif %}{{ owner }} {{ args[0] }}{% endif %}", + ], + 'push': [ + "rsync", + "--csv-exclude", + "--checksum", + "--compress", + "{% if delete %}--delete{% endif %}", + "{% if links %}--copy-links{% endif %}", + "{% if exclude %}--exclude-from={{ exclude }}{% endif %}", + # --partial and --progress + "-P", + "{% if recursive %}--recursive{% endif %}", + "{{ args[0] }}", + '-e "ssh -i {{ key_file }} -p {{ port|default("22") }}', + "{{ user }}@{{ host }}:{{ args[1] }}", + ], + 'remove': [ + "rm", + "{% if force %}-f{% endif %}", + "{% if recursive %}-r{% endif %}", + "{{ args[0] }}" + ], + 'rename': "mv {{ args[0] }} {{ args[1] }}", + 'replace': [ + 'sed -i {{ backup|default(".b") }}', + '"s{{ delimiter|default("/") }}{{ find }}{{ delimiter|default("/") }}{{ sub }}{{ delimiter|default("/") }}g"', + "{{ args[0] }}" + ], + 'scopy': [ + "scp", + "{% if key_file %}-i {{ key_file }}{% endif %}", + '-P {{ port|default("22") }}', + "{{ args[0] }}", + "{{ user }}@{{ host }}:{{ args[1] }}" + ], + 'ssl': [ + "certbot certonly", + "--agree-tos", + '--email {{ email|default("webmaster@" + args[0]) }}', + "-n --webroot", + '-w {{ webroot|default("/var/www/maint/www") }}', + "-d {{ args[0] }}" + ], + 'sync': [ + "rsync", + "--csv-exclude", + "--checksum", + "--compress", + "{% if delete %}--delete{% endif %}", + "{% if links %}--copy-links{% endif %}", + "{% if exclude %}--exclude-from={{ exclude }}{% endif %}", + # --partial and --progress + "-P", + "{% if recursive %}--rescursive{% endif %}", + "{{ args[0] }}" + "{{ args[1] }}" + ], + 'touch': "touch {{ args[0] }}", + 'wait': "sleep {{ args[0] }}", + # 'write': [ + # "cat > {{ args[0] }} << EOF", + # "\n", + # "{{ content }}", + # "\n", + # "EOF" + # ], + 'write': "cat > {{ args[0] }} << EOF\n{{ content }}\nEOF", +} \ No newline at end of file diff --git a/scripttease/library/snippets/python.py b/scripttease/library/snippets/python.py new file mode 100644 index 0000000..5428c55 --- /dev/null +++ b/scripttease/library/snippets/python.py @@ -0,0 +1,27 @@ +python = { + 'pip': [ + "{% if venv %}source {{ venv }}/bin/activate &&{%- endif %}", + "pip{% if version %}{{ version }}{% endif %}", + '{{ op|default("install") }}', + '{% if op == "upgrade" %}--upgrade{% endif %}', + "{{ args[0] }}", + ], + # 'pip': { + # 'install': [ + # "{% if venv %}source {{ venv }} &&{% endif %}", + # "{% if version %}pip{{ version }}{% else %}pip{% endif %}", + # "install {{ args[0] }}", + # ], + # 'remove': [ + # "{% if version %}pip{{ version }}{% else %}pip{% endif %}", + # "uninstall {{ args[0] }}", + # ], + # 'upgrade': [ + # "{% if version %}pip{{ version }}{% else %}pip{% endif %}", + # "install --upgrade {{ args[0] }}", + # ], + # '_default': "install", + # '_dotted': True, + # }, + 'virtualenv': "virtualenv {{ args[0] }}", +} \ No newline at end of file diff --git a/scripttease/library/snippets/ubuntu.py b/scripttease/library/snippets/ubuntu.py new file mode 100644 index 0000000..969ac8f --- /dev/null +++ b/scripttease/library/snippets/ubuntu.py @@ -0,0 +1,41 @@ +ubuntu = { + 'apache': { + 'disable': '{% if args[0].startswith("mod_") %}a2dismod{% else %}a2dissite{% endif %} {{ args[0] }}', + 'disable_module': "a2dissite {{ args[0] }}", + 'disable_site': "a2dismod {{ args[0] }}", + 'enable': '{% if args[0].startswith("mod_") %}a2denmod{% else %}a2ensite{% endif %} {{ args[0] }}', + 'enable_module': "a2enmod {{ args[0] }}", + 'enable_site': "a2ensite {{ args[0] }}", + 'reload': "service apache2 reload", + 'restart': "service apache2 restart", + 'start': "service apache2 start", + 'stop': "service apache2 stop", + 'test': "apachectl configtest", + }, + 'install': "apt-get install -y {{ args[0] }}", + 'run': "{{ args[0] }}", + 'service': { + 'reload': "service {{ args[0] }} reload", + 'restart': "service {{ args[0] }} restart", + 'start': "service {{ args[0] }} start", + 'stop': "service {{ args[0] }} stop", + }, + 'system': { + 'reboot': "reboot", + 'update': "apt-get update -y", + 'upgrade': "apt-get upgrade -y", + }, + 'uninstall': "apt-get uninstall -y {{ args[0] }}", + 'upgrade': "apt-get install -y --only-upgrade {{ args[0] }}", + 'user': { + # The gecos switch eliminates the prompts. + # TODO: Deal with user password when creating a user in ubuntu. + 'create': [ + "adduser {{ args[0] }} --gecos --disabled-password", + "{% if home %}--home {{ home }}{% endif %}", + "{% if groups %}&& {% for group in groups %}adduser {{ args[0] }} {{ group }};{% endfor %}{% endif %}" + + ], + 'remove': "deluser {{ args[0] }}", + }, +}