chris@203: #!/bin/bash chris@203: chris@203: # Run this script from anywhere chris@203: chris@203: # Enumerate Hg repos; make sure they're up to date; extract docs for chris@203: # each chris@203: chris@203: hgdir="/var/hg" chris@203: docdir="/var/doc" Chris@1590: logfile="/var/www/code/log/extract-docs.log" chris@223: Chris@1590: redgrp="code" chris@203: Chris@1593: apikey="INSERT_API_KEY_HERE" Chris@1593: apischeme="INSERT_API_SCHEME_HERE" Chris@1593: apihost="INSERT_API_HOST_HERE" Chris@1593: Chris@1593: # HTTP auth username/password for /sys api calls Chris@1593: apiuser="INSERT_API_USER_HERE" Chris@1593: apipass="INSERT_API_PASSWORD_HERE" chris@218: chris@203: progdir=$(dirname $0) chris@203: case "$progdir" in chris@203: /*) ;; chris@203: *) progdir="$(pwd)/$progdir" ;; chris@203: esac chris@203: chris@411: types="doxygen javadoc matlabdocs" # Do Doxygen first (it can be used for Java too) chris@203: chris@203: for x in $types; do chris@203: if [ ! -x "$progdir/extract-$x.sh" ]; then chris@203: echo "Helper script not available: $progdir/extract-$x.sh" chris@203: exit 1 chris@203: fi chris@203: done chris@203: chris@218: enable_embedded() chris@218: { chris@218: p="$1" chris@228: if [ -n "$apikey" ]; then chris@228: if [ -n "$apiuser" ]; then chris@339: sudo -u docgen curl -u "$apiuser":"$apipass" "$apischeme://$apihost/sys/projects/$p/embedded.xml?enable=1&key=$apikey" -d "" chris@228: else chris@339: sudo -u docgen curl "$apischeme://$apihost/sys/projects/$p/embedded.xml?enable=1&key=$apikey" -d "" chris@228: fi chris@228: else chris@228: echo "Can't enable Embedded, API not configured" 1>&2 chris@218: fi chris@218: } chris@218: chris@223: # We want to ensure the doc extraction is done by the unprivileged chris@223: # user docgen, which is not a member of any interesting group chris@223: # chris@223: # To this end, we create the tmpdir with user docgen and group chris@223: # www-data, and use the www-data user to pull out an archive of the Hg chris@223: # repo tip into a location beneath that, before using the docgen user chris@223: # to extract docs from that location and write them into the tmpdir chris@223: chris@223: # Same tmpdir for each project: we delete and recreate to avoid chris@223: # cleanup duty from lots of directories being created chris@223: # chris@223: tmpdir=$(mktemp -d "$docdir/tmp_XXXXXX") chris@223: chris@223: fail() chris@223: { chris@223: message="$1" chris@223: echo "$message" 1>&2 chris@223: case "$tmpdir" in chris@223: */tmp*) rm -rf "$tmpdir";; chris@223: *);; chris@223: esac chris@223: exit 1 chris@223: } chris@223: chris@223: case "$tmpdir" in chris@223: /*) ;; chris@223: *) fail "Temporary directory creation failed";; chris@223: esac chris@223: chris@223: chown docgen.www-data "$tmpdir" || fail "Temporary directory ownership change failed" chris@223: chmod g+rwx "$tmpdir" || fail "Temporary directory permissions change failed" chris@223: chris@203: for projectdir in "$hgdir"/* ; do chris@203: chris@203: if [ -d "$projectdir" ] && [ -d "$projectdir/.hg" ]; then chris@203: chris@223: if ! sudo -u www-data hg -R "$projectdir" -q update; then chris@223: echo "Failed to update Hg in $projectdir, skipping" 1>&2 chris@223: continue chris@223: fi chris@223: chris@203: project=$(basename "$projectdir") chris@203: chris@223: tmptargetdir="$tmpdir/doc" chris@223: snapshotdir="$tmpdir/hgsnapshot" chris@203: chris@223: rm -rf "$tmptargetdir" "$snapshotdir" chris@223: chris@226: mkdir -m 770 "$tmptargetdir" || fail "Temporary target directory creation failed" chris@226: chown docgen.www-data "$tmptargetdir" || fail "Temporary target directory ownership change failed" chris@223: chris@223: mkdir -m 770 "$snapshotdir" || fail "Snapshot directory creation failed" chris@223: chown docgen.www-data "$snapshotdir" || fail "Snapshot directory ownership change failed" chris@223: chris@223: hgparents=$(sudo -u www-data hg -R "$projectdir" parents) chris@223: if [ -z "$hgparents" ]; then chris@223: echo "Hg repo at $projectdir has no working copy (empty repo?), skipping" chris@223: continue chris@223: else chris@223: echo "Found non-empty Hg repo: $projectdir for project $project" chris@223: fi chris@223: chris@223: if ! sudo -u www-data hg -R "$projectdir" archive -r tip -t files "$snapshotdir"; then chris@223: echo "Failed to pick archive from $projectdir, skipping" 1>&2 chris@223: continue chris@223: fi chris@203: chris@203: targetdir="$docdir/$project" chris@203: chris@223: echo "Temporary dir is $tmpdir, temporary doc dir is $tmptargetdir, snapshot dir is $snapshotdir, eventual target is $targetdir" chris@203: chris@203: for x in $types; do chris@226: if sudo -u docgen "$progdir/extract-$x.sh" "$project" "$snapshotdir" "$tmptargetdir" >> "$logfile" 2>&1; then chris@226: break chris@226: else chris@203: echo "Failed to extract via type $x" chris@203: fi chris@203: done chris@203: chris@223: if [ -f "$tmptargetdir/index.html" ]; then chris@203: echo "Processing resulted in an index.html being created, looks good!" chris@203: if [ ! -d "$targetdir" ] || [ ! -f "$targetdir/index.html" ]; then chris@223: echo "This project hasn't had doc extracted before: enabling Embedded" chris@218: enable_embedded "$project" chris@203: fi chris@203: chris@203: if [ -d "$targetdir" ]; then chris@203: mv "$targetdir" "$targetdir"_"$$" && \ chris@223: mv "$tmptargetdir" "$targetdir" && \ chris@203: rm -rf "$targetdir"_"$$" chris@223: chgrp -R "$redgrp" "$targetdir" chris@203: else chris@223: mv "$tmptargetdir" "$targetdir" chris@223: chgrp -R "$redgrp" "$targetdir" chris@203: fi chris@228: else chris@228: echo "Processing did not result in an index.html being created" chris@203: fi chris@203: fi chris@203: done chris@203: chris@223: rm -rf "$tmpdir"