forked from compmodels/jupyterhub-deploy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelease_assignment.py
104 lines (86 loc) · 3.12 KB
/
release_assignment.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
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python
# -*- coding: utf-8 -*-
import tarfile
import os
import shutil
def extract_to(module, src, pth, prefix, user, overwrite=False):
root = os.path.abspath(os.path.join(pth, prefix))
ro_root = os.path.abspath(os.path.join(pth, prefix + " (read only)"))
if not root.startswith(pth):
module.fail_json(msg="Root path is invalid: {}".format(root))
# Skip existing directories, if we're not overwriting
if os.path.exists(root) and not overwrite:
return False
# Remove directory, if it already exists
if os.path.exists(root):
shutil.rmtree(root)
if os.path.exists(ro_root):
shutil.rmtree(ro_root)
# Extract the archive
tf = tarfile.open(src, 'r:gz')
tf.extractall(pth)
tf.close()
# Set permissions
module.run_command(
'chown -R {}:{} "{}"'.format(user, user, root), check_rc=True)
module.run_command(
'chmod -R u+rwX,og-rwx "{}"'.format(root), check_rc=True)
# Create a read-only version
shutil.copytree(root, ro_root)
module.run_command(
'chown -R {}:{} "{}"'.format(user, user, ro_root), check_rc=True)
module.run_command(
'chmod -R u+rX,u-w,og-rwx "{}"'.format(ro_root), check_rc=True)
return True
def main():
module = AnsibleModule(
argument_spec={
'src': dict(required=True),
'users': dict(required=True, type='list'),
'skeldir': dict(default=''),
'overwrite': dict(default=False, type='bool')
}
)
src = module.params["src"]
users = module.params["users"]
skeldir = module.params["skeldir"]
overwrite = module.params["overwrite"]
# Open the tarfile, figure out the prefix
tf = tarfile.open(src, 'r:gz')
prefix = os.path.commonprefix(tf.getnames()).rstrip("/")
if prefix != '':
try:
member = tf.getmember(prefix)
except KeyError:
pass
else:
if not member.isdir():
prefix = os.path.dirname(prefix)
tf.close()
if prefix == '':
module.fail_json(msg="Archive has no common prefix")
# Copy to the skeleton directory
if skeldir != "":
skeldir = os.path.abspath(skeldir)
if skeldir in ("/", "/root", "/home"):
module.fail_json(msg="Skeleton directory is {}, danger!".format(skeldir))
extract_to(module, src, skeldir, prefix, "root", overwrite=True)
changed = False
changed_users = []
# Extract the archive into the home directory of each user
for user in users:
homedir = os.path.abspath('/home/{}'.format(user))
if not homedir.startswith('/home/'):
module.fail_json(msg="Home directory is invalid: {}".format(homedir))
if not os.path.exists(homedir):
module.fail_json(msg="Home directory does not exist: {}".format(homedir))
if extract_to(module, src, homedir, prefix, user, overwrite=overwrite):
changed = True
changed_users.append(user)
module.exit_json(
changed=changed,
prefix=prefix,
users=changed_users
)
from ansible.module_utils.basic import *
main()