Skip to content

Commit

Permalink
Add ability to download assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamrick committed Jan 22, 2015
1 parent 9e915a8 commit a7b953e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
26 changes: 26 additions & 0 deletions download.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
- hosts: jupyterhub_host
vars_files:
- ['secrets.yml', 'secrets.vault.yml']
- ['users.yml', 'users.vault.yml']
- ['vars.local.yml', 'vars.yml']

vars_prompt:
assignment: "Which assignment?"

tasks:
- name: collect submissions
collect_submissions:
src: '.nbgrader/submissions/{{ assignment }}.tar.gz'
dest: '/srv/assignments/submissions.tar.gz'
users: "{{ instructors }}"
overwrite: yes
sudo: yes

- name: download submissions
fetch:
src: '/srv/assignments/submissions.tar.gz'
dest: '{{ assignment_path }}/{{ assignment }}/submissions.tar.gz'
flat: yes
fail_on_missing: yes
sudo: yes
52 changes: 52 additions & 0 deletions library/collect_submissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
17 changes: 17 additions & 0 deletions script/download
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

ROOT=$(dirname $0)/..

if [ ! -e ${ROOT}//inventory ]; then
echo "Please create an inventory file with your hosts."
echo " cp inventory.example inventory"
exit 1
fi

if [ -e ${ROOT}/secrets.yml ]; then
VAULT_ARG=
else
VAULT_ARG=--ask-vault-pass
fi

exec ansible-playbook ${ROOT}/download.yml -i ${ROOT}/inventory ${VAULT_ARG} $@

0 comments on commit a7b953e

Please sign in to comment.