-
Notifications
You must be signed in to change notification settings - Fork 0
/
memset
executable file
·139 lines (108 loc) · 4.34 KB
/
memset
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
"""
Replace your /etc/ansible/hosts (/usr/local/etc/ansible/hosts) file with this,
to automatically import all your Memset servers into the Ansible inventory.
Alternatively, convert /etc/ansible/hosts to a folder - this allows you to
create local inventory files as well, importing all on each run.
This creates the Ansible host groups:
[memset-linux], [memset-windows], [memset-reading], [memset-dunsfold]
ANSIBLE_MEMSET_KEY must be exported before running:
export ANSIBLE_MEMSET_KEY=XXXXXX
ansible memset-linux -m ping
ansible memset-reading -l memset-linux -m ping
API key must have access to at least:
method:server.list, method:server.info & method:service.info
"""
from jsonrpclib import Server
import os
import sys
import getopt
try:
import json
except ImportError:
import simplejson as json
try:
memkey = os.environ['ANSIBLE_MEMSET_KEY']
except KeyError, e:
sys.stderr.write('Unable to locate Memset credentials in environment!')
sys.stderr.flush()
exit(1)
uri = "https://%s:@api.memset.com/v1/jsonrpc/" % memkey
s = Server(uri)
def _get_servers():
return s.server.list()
def _get_service(name):
return s.service.info(name=name)
def _simple_os(server):
return 'windows' if server["os"].startswith("win") else 'linux'
def _get_zone(service):
return service['network_zones'][0]
def _build_vars(server):
var = {}
service = _get_service(server["name"])
var["memset_nickname"] = service["nickname"]
var["memset_start_date"] = service["start_date"]
var["memset_expiry_date"] = service["expiry_date"]
var["memset_network_zone"] = service["network_zones"][0]
var["memset_data_zone"] = service["data_zone"]
var["memset_renewal_price_amount"] = service["renewal_price_amount"]
var["memset_renewal_price_currency"] = service["renewal_price_currency"]
var["memset_renewal_price_vat"] = service["renewal_price_vat"]
var["memset_type"] = service["type"]
var["memset_os"] = server["os"]
var["memset_backups"] = server["backups"]
var["memset_control_panel"] = server["control_panel"]
var["memset_firewall_type"] = server["firewall_type"]
var["memset_firewall_rule_group_name"] = server["firewall_rule_group"]["name"] if server["firewall_type"] != 'none' else ''
var["memset_firewall_rule_group_nickname"] = server["firewall_rule_group"]["nickname"] if server["firewall_type"] != 'none' else ''
var["memset_monitoring_level"] = server["monitoring_level"]
var["memset_monitor"] = server["monitor"]
var["memset_ignore_monitoring_off"] = server["ignore_monitoring_off"]
var["memset_no_auto_reboot"] = server["no_auto_reboot"]
var["memset_support_level"] = server["support_level"]
var["memset_vulnscan"] = server["vulnscan"]
var["memset_penetration_patrol"] = server["penetration_patrol"]
var["memset_penetration_patrol_alert_level"] = server["penetration_patrol_alert_level"]
return var
def get_single(hostname):
servers = _get_servers()
server = filter(lambda s: s["host_name"] == hostname, servers)
output = {}
if len(server) == 1:
output = _build_vars(server[0])
return output
def get_all():
osses = {}
osses["windows"] = []
osses["linux"] = []
output = {
'memset-windows': {'hosts': [], 'vars': {}},
'memset-linux': {'hosts': [], 'vars': {}},
'memset-reading': {'hosts': [], 'vars': {}},
'memset-dunsfold': {'hosts': [], 'vars': {}},
'_meta': {'hostvars': {}}
}
for server in _get_servers():
os_group = 'memset-%s' % _simple_os(server)
zone_group = 'memset-%s' % _get_zone(server)
hn = server['host_name']
output[os_group]['hosts'].append(hn)
output[zone_group]['hosts'].append(hn)
output['_meta']['hostvars'][hn] = _build_vars(server)
output['memset-linux']['vars']["ansible_ssh_user"] = 'root'
return output
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "", ["list", "host="])
except getopt.GetoptError as err:
print str(err)
sys.exit(2)
output = {}
for o, a in opts:
if o == "--list":
output = get_all()
elif o == "--host":
output = get_single(a)
print json.dumps(output, sort_keys=True, indent=4, separators=(',', ': '))
if __name__ == '__main__':
main()