forked from compmodels/jupyterhub-deploy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollect_submissions.py
52 lines (40 loc) · 1.33 KB
/
collect_submissions.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import tarfile
def main():
module = AnsibleModule(
argument_spec={
'src': dict(required=True),
'dest': dict(required=True),
'users': dict(required=True, type='list'),
'overwrite': dict(default=False, type='bool')
}
)
src = module.params["src"]
dest = module.params["dest"]
users = module.params["users"]
overwrite = module.params["overwrite"]
if os.path.exists(dest) and not overwrite:
module.exit_json(changed=False)
elif os.path.exists(dest):
os.remove(dest)
tf = tarfile.open(dest, 'w:gz')
for user in users:
homedir = os.path.abspath('/home/{}'.format(user))
if not homedir.startswith('/home/'):
tf.close()
os.remove(dest)
module.fail_json(msg="Home directory is invalid: {}".format(homedir))
if not os.path.exists(homedir):
tf.close()
os.remove(dest)
module.fail_json(msg="Home directory does not exist: {}".format(homedir))
pth = os.path.join(homedir, src)
if not os.path.exists(pth):
continue
tf.add(pth, arcname="{}.tar.gz".format(user))
tf.close()
module.exit_json(changed=True)
from ansible.module_utils.basic import *
main()