blob: d15d02e8e2492889bf3730446afbb5381bd174e4 [file] [log] [blame]
#!/bin/bash
ACCESS_TOKEN=''
USER_NAME=''
PROJECT=''
TOOLS=''
HOST='sourceforge.net'
OUTPUT=.
MAX_MINUTES=10
HEADERS=/tmp/project_export_response_headers
BODY=/tmp/project_export_response
function error() {
echo "$1"
exit 1
}
while getopts "ht:u:o:H:c:" opt; do
case $opt in
h)
echo "Usage: $0 [OPTION]... PROJECT TOOLS..."
echo "Schedule an export of PROJECT and retrieve the file when ready"
echo
echo " -c CONFIG_FILE Load config (optionally including PROJECT and TOOLs) from file"
echo " -H HOST Host to use (default sourceforge.net)"
echo " -o OUTPUT Directory and/or filename for downloaded export file"
echo " -t ACCESS_TOKEN Access (bearer) token for the API"
echo " -u USER_NAME Username associated with access token (to download file)"
echo " -h Show this help message"
echo
echo "The TOOLS should be one or more tool mount-point names, separated by spaces"
;;
t)
C_ACCESS_TOKEN="$OPTARG"
;;
u)
C_USER_NAME="$OPTARG"
;;
H)
C_HOST="$OPTARG"
;;
o)
C_OUTPUT="$OPTARG"
;;
c)
source "$OPTARG"
;;
*)
error "Invalid option: -$opt"
esac
done
# command-line overrides config
[ -n "$C_ACCESS_TOKEN" ] && ACCESS_TOKEN="$C_ACCESS_TOKEN"
[ -n "$C_USER_NAME" ] && USER_NAME="$C_USER_NAME"
[ -n "$C_HOST" ] && HOST="$C_HOST"
[ -n "$C_OUTPUT" ] && OUTPUT="$C_OUTPUT"
shift $(($OPTIND - 1))
if [[ $# -ge 1 ]]; then
PROJECT="$1"
shift
fi
[ $# -ge 1 ] && TOOLS="$@"
# fall back to prompt
[ -z "$ACCESS_TOKEN" ] && read -p "Access (bearer) token: " ACCESS_TOKEN
[ -z "$USER_NAME" ] && read -p "Username: " USER_NAME
[ -z "$PROJECT" ] && read -p "Project: " PROJECT
[ -z "$TOOLS" ] && read -p "Tools: " TOOLS
URL="https://$HOST/rest/p/$PROJECT/admin/export"
function json() {
python -c "import sys, json; print json.load(sys.stdin)['$1']"
}
curl -s -D $HEADERS -o $BODY --data "access_token=$ACCESS_TOKEN&tools=$TOOLS" "$URL"
head -n1 $HEADERS | grep 400 && error "Invalid or missing tool"
head -n1 $HEADERS | grep 503 && error "Export already in progress"
head -n1 $HEADERS | grep -v 200 && error "Error: $(head -n1 $HEADERS)"
filename=`cat $BODY | json filename`
minutes=0
while true; do
curl -s -D $HEADERS -o $BODY "${URL}_status?access_token=$ACCESS_TOKEN"
head -n1 $HEADERS | grep -v 200 && error "Error: $(head -n1 $HEADERS)"
status=`cat $BODY | json status`
[ "$status" == "ready" ] && break
sleep 60
let minutes+=1
[ "$minutes" -ge "$MAX_MINUTES" ] && error "Timeout waiting for export"
done
scp $USER_NAME@web.$HOST:/home/project-exports/$PROJECT/$filename $OUTPUT