-
Notifications
You must be signed in to change notification settings - Fork 8
/
ezjail.py
executable file
·91 lines (75 loc) · 2.6 KB
/
ezjail.py
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
#!/usr/bin/python
DOCUMENTATION = '''
---
module: ezjail
author: Tom Lazar
short_description: Manage FreeBSD jails
requirements: [ zfs ]
description:
- Manage FreeBSD jails
'''
class Ezjail(object):
platform = 'FreeBSD'
def __init__(self, module):
self.module = module
self.changed = False
self.state = self.module.params.pop('state')
self.name = self.module.params.pop('name')
self.cmd = self.module.get_bin_path('ezjail-admin', required=True)
def ezjail_admin(self, command, *params):
return self.module.run_command(' '.join(['sudo', self.cmd, command]
+ list(params)))
def exists(self):
(rc, out, err) = self.ezjail_admin('config', '-r', 'test', self.name)
return rc==0
def create(self):
result = dict()
if self.module.check_mode:
self.changed = True
return result
(rc, out, err) = self.ezjail_admin('create', '-c',
self.module.params['disktype'], self.name, self.module.params['ip_addr'])
if rc == 0:
self.changed = True
if self.state == 'running':
(rc, out, err) = self.ezjail_admin('start', 'webserver')
if rc != 0:
result['failed'] = True
result['msg'] = "Could not start jail. %s%s" % (out, err)
else:
self.changed = False
result['failed'] = True
result['msg'] = "Could not create jail. %s%s" % (out, err)
return result
def destroy(self):
raise NotImplemented
def __call__(self):
result = dict(name=self.name, state=self.state)
if self.state in ['present', 'running']:
if not self.exists():
result.update(self.create())
elif self.state == 'absent':
if self.exists():
self.destroy()
result['changed'] = self.changed
return result
MODULE_SPECS = dict(
argument_spec=dict(
name=dict(required=True, type='str'),
state=dict(default='present', choices=['present', 'absent', 'running'], type='str'),
disktype=dict(default='simple', choices=['simple', 'bde', 'eli', 'zfs'], type='str'),
ip_addr=dict(required=True, type='str'),
),
supports_check_mode=True
)
def main():
module = AnsibleModule(**MODULE_SPECS)
result = Ezjail(module)()
if 'failed' in result:
module.fail_json(**result)
else:
module.exit_json(**result)
# include magic from lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
if __name__ == "__main__":
main()