Skip to content

Commit 34ddb46

Browse files
committed
Use script_server config of ubergeek42
1 parent e1733ce commit 34ddb46

File tree

7 files changed

+239
-0
lines changed

7 files changed

+239
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
script_server_url: https://github.com/bugy/script-server/releases/download/1.18.0/script-server.zip
3+
script_server_port: 5000
4+
5+
script_server_admin_users: &admin_users
6+
# hostnames or ips
7+
- backup
8+
- 127.0.0.1
9+
10+
# ansible hostgroups to ignore
11+
script_server_ignored_groups:
12+
- 'all'
13+
- 'ungrouped'
14+
- 'contestants'
15+
- 'contestants_wf46'
16+
- 'contestants_wf47'
17+
18+
19+
script_server_commands:
20+
- name: run-lastminute
21+
description: Runs the ansible playbook lastminute.yml
22+
group: ansible # for organization in script-server
23+
allowed_users:
24+
- backup # or 10.3.3.210 (the ansible template will resolve names to IPs)
25+
content: |
26+
#!/usr/bin/bash
27+
echo "hello world $HOSTPATTERN"
28+
parameters:
29+
- name: host_pattern
30+
# pass_as: env_variable # this is default
31+
# env_var: host_pattern # default is same as name
32+
type: list
33+
values:
34+
- all
35+
- backup
36+
- packages
37+
- scoreboard
38+
- cds
39+
40+
script_server_command_defaults:
41+
output_format: terminal
42+
# scheduling: # Don't allow scheduling, the ui for it is not good...
43+
# enabled: false
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- name: Restart script-server
3+
ansible.builtin.service:
4+
name: script-server
5+
state: restarted
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import ansible.utils as utils
2+
import ansible.errors as errors
3+
from ansible.plugins.lookup import LookupBase
4+
import socket
5+
import ipaddress
6+
7+
class LookupModule(LookupBase):
8+
9+
def __init__(self, basedir=None, **kwargs):
10+
self.basedir = basedir
11+
12+
def run(self, terms, variables=None, **kwargs):
13+
hostname = terms[0]
14+
15+
try:
16+
# If it's a valid ip address already, just return it directly
17+
ipaddress.ip_address(hostname)
18+
return [hostname]
19+
except Exception:
20+
pass
21+
22+
if not isinstance(hostname, str):
23+
raise errors.AnsibleError("ip lookup expects a string (hostname)")
24+
25+
return [socket.gethostbyname(hostname)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
- name: Install script-server dependencies
3+
ansible.builtin.package:
4+
pkg: "{{ item }}"
5+
state: present
6+
loop:
7+
- python3-tornado
8+
- apache2-utils # for htpasswd auth
9+
10+
- name: Create directory for script-server to live
11+
ansible.builtin.file:
12+
path: /opt/script-server
13+
mode: "0755"
14+
owner: root
15+
group: root
16+
state: directory
17+
18+
- name: Download script-server
19+
ansible.builtin.unarchive:
20+
src: "{{ script_server_url }}"
21+
dest: /opt/script-server
22+
remote_src: "{{ true if script_server_url.startswith('http') else false }}"
23+
creates: /opt/script-server/launcher.py
24+
25+
- name: Configure the server
26+
ansible.builtin.template:
27+
src: conf.json.j2
28+
dest: /opt/script-server/conf/conf.json
29+
mode: "0644"
30+
31+
- name: Create systemd service for script-server
32+
ansible.builtin.copy:
33+
mode: "0644"
34+
dest: /etc/systemd/system/script-server.service
35+
content: |
36+
[Unit]
37+
Description=Script Server
38+
After=network.target
39+
StartLimitIntervalSec=0
40+
41+
[Service]
42+
Type=simple
43+
Restart=always
44+
RestartSec=1
45+
ExecStart=/usr/bin/python3 /opt/script-server/launcher.py
46+
47+
[Install]
48+
WantedBy=multi-user.target
49+
notify: Restart script-server
50+
51+
- name: Start + enable script-server
52+
ansible.builtin.service:
53+
name: script-server
54+
state: started
55+
enabled: true
56+
57+
- name: Ensure required directories exist
58+
ansible.builtin.file:
59+
state: directory
60+
mode: "0755"
61+
path: /opt/script-server/conf/{{ item }}
62+
loop:
63+
- scripts
64+
- runners
65+
66+
- name: Create scripts
67+
ansible.builtin.copy:
68+
content: "{{ item.content }}"
69+
dest: /opt/script-server/conf/scripts/{{ item.name }}
70+
mode: "0755"
71+
with_items: "{{ script_server_commands }}"
72+
73+
- name: Create script config definitions
74+
ansible.builtin.template:
75+
src: command_template.yaml.j2
76+
dest: /opt/script-server/conf/runners/{{ item.name }}.yaml
77+
mode: "0644"
78+
with_items: "{{ script_server_commands }}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
{% set _unused_content = item.pop('content') %}
3+
{% set parameters = item.pop('parameters') %}
4+
{% set admin_users = item.pop('admin_users', []) + script_server_admin_users %}
5+
{% set allowed_users = item.pop('allowed_users', []) %}
6+
{# set admin_users = (admin_users if ( admin_users | type_debug == "list" ) else [admin_users]) #}
7+
{# set allowed_users = (allowed_users if ( allowed_users | type_debug == "list" ) else [allowed_users]) #}
8+
{{
9+
script_server_command_defaults |
10+
combine(item) |
11+
to_nice_yaml
12+
}}
13+
{% if admin_users %}
14+
admin_users:
15+
{% for u in admin_users %}
16+
- {{ lookup('ip', u) }}
17+
{% endfor %}
18+
{% endif %}
19+
{% if allowed_users %}
20+
allowed_users:
21+
{% for u in (allowed_users + admin_users) %}
22+
- {{ lookup('ip', u) }}
23+
{% endfor %}
24+
{% endif %}
25+
26+
{% if parameters %}
27+
parameters:
28+
{% for param in parameters %}
29+
- name: {{ param.name }}
30+
pass_as: {{ param.pass_as | default('env_variable') }}
31+
{% if param.pass_as|default('env_variable') == 'env_variable' %}
32+
env_var: {{ param.env_var | default(param.name) }}
33+
{% endif %}
34+
{% for k,v in param.items() if k not in ['pass_as','env_var','name'] %}
35+
{{ k}}: {{ v|to_json }}
36+
{% endfor %}
37+
{% endfor %}
38+
{% endif %}
39+
40+
41+
# assume default path for the script file
42+
script_path: conf/scripts/{{ item.name }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"title": "sysops script server",
3+
"port": {{ script_server_port }},
4+
"access": {
5+
"trusted_ips": [
6+
{% for group in groups if group not in script_server_ignored_groups %}
7+
{%- for host in groups[group] -%}
8+
"{{ hostvars[host].ansible_host}}",
9+
{% endfor %}
10+
{%- endfor -%}
11+
12+
{% for u in script_server_admin_users %}"{{ lookup('ip', u)}}", {% endfor %}"127.0.0.1"
13+
],
14+
"allowed_users": [
15+
{% for group in groups if group not in script_server_ignored_groups %}
16+
{%- for host in groups[group] -%}
17+
"{{ hostvars[host].ansible_host}}",
18+
{% endfor -%}
19+
{%- endfor -%}
20+
"127.0.0.1"
21+
],
22+
"admin_users": [{% for u in script_server_admin_users %}"{{ lookup('ip', u)}}", {% endfor %}"127.0.0.1"],
23+
"groups": {
24+
{% for group in groups if group not in script_server_ignored_groups -%}
25+
"{{group}}": [
26+
{% for host in groups[group] -%}
27+
"{{ hostvars[host].ansible_host}}" {{ ", " if not loop.last else "" }}
28+
{% endfor -%}
29+
],
30+
{% endfor -%}
31+
"all": [
32+
{% for group in groups if group not in script_server_ignored_groups -%}
33+
"{{ group }}",
34+
{% endfor -%}
35+
"@admin_users"
36+
]
37+
}
38+
},
39+
"logging": {
40+
"execution_file": "$DATE-$ID.log",
41+
"execution_date_format": "%y-%m-%d_%H-%M"
42+
},
43+
"security": {
44+
"xsrf_protection": "token"
45+
}
46+
}

0 commit comments

Comments
 (0)