From a7b953ea094f3f4372b57b764f6a6c9d5dfd7286 Mon Sep 17 00:00:00 2001 From: "Jessica B. Hamrick" Date: Wed, 21 Jan 2015 19:05:04 -0800 Subject: [PATCH] Add ability to download assignments --- download.yml | 26 +++++++++++++++++ library/collect_submissions.py | 52 ++++++++++++++++++++++++++++++++++ script/download | 17 +++++++++++ 3 files changed, 95 insertions(+) create mode 100644 download.yml create mode 100644 library/collect_submissions.py create mode 100755 script/download diff --git a/download.yml b/download.yml new file mode 100644 index 0000000..dbf3a82 --- /dev/null +++ b/download.yml @@ -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 diff --git a/library/collect_submissions.py b/library/collect_submissions.py new file mode 100644 index 0000000..0dc085e --- /dev/null +++ b/library/collect_submissions.py @@ -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() diff --git a/script/download b/script/download new file mode 100755 index 0000000..a93b996 --- /dev/null +++ b/script/download @@ -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} $@