annotate extra/soundsoftware/extract-docs.sh @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents 76f8f0814e1e
children
rev   line source
chris@203 1 #!/bin/bash
chris@203 2
chris@203 3 # Run this script from anywhere
chris@203 4
chris@203 5 # Enumerate Hg repos; make sure they're up to date; extract docs for
chris@203 6 # each
chris@203 7
chris@203 8 hgdir="/var/hg"
chris@203 9 docdir="/var/doc"
Chris@1590 10 logfile="/var/www/code/log/extract-docs.log"
chris@223 11
Chris@1590 12 redgrp="code"
chris@203 13
Chris@1593 14 apikey="INSERT_API_KEY_HERE"
Chris@1593 15 apischeme="INSERT_API_SCHEME_HERE"
Chris@1593 16 apihost="INSERT_API_HOST_HERE"
Chris@1593 17
Chris@1593 18 # HTTP auth username/password for /sys api calls
Chris@1593 19 apiuser="INSERT_API_USER_HERE"
Chris@1593 20 apipass="INSERT_API_PASSWORD_HERE"
chris@218 21
chris@203 22 progdir=$(dirname $0)
chris@203 23 case "$progdir" in
chris@203 24 /*) ;;
chris@203 25 *) progdir="$(pwd)/$progdir" ;;
chris@203 26 esac
chris@203 27
chris@411 28 types="doxygen javadoc matlabdocs" # Do Doxygen first (it can be used for Java too)
chris@203 29
chris@203 30 for x in $types; do
chris@203 31 if [ ! -x "$progdir/extract-$x.sh" ]; then
chris@203 32 echo "Helper script not available: $progdir/extract-$x.sh"
chris@203 33 exit 1
chris@203 34 fi
chris@203 35 done
chris@203 36
chris@218 37 enable_embedded()
chris@218 38 {
chris@218 39 p="$1"
chris@228 40 if [ -n "$apikey" ]; then
chris@228 41 if [ -n "$apiuser" ]; then
chris@339 42 sudo -u docgen curl -u "$apiuser":"$apipass" "$apischeme://$apihost/sys/projects/$p/embedded.xml?enable=1&key=$apikey" -d ""
chris@228 43 else
chris@339 44 sudo -u docgen curl "$apischeme://$apihost/sys/projects/$p/embedded.xml?enable=1&key=$apikey" -d ""
chris@228 45 fi
chris@228 46 else
chris@228 47 echo "Can't enable Embedded, API not configured" 1>&2
chris@218 48 fi
chris@218 49 }
chris@218 50
chris@223 51 # We want to ensure the doc extraction is done by the unprivileged
chris@223 52 # user docgen, which is not a member of any interesting group
chris@223 53 #
chris@223 54 # To this end, we create the tmpdir with user docgen and group
chris@223 55 # www-data, and use the www-data user to pull out an archive of the Hg
chris@223 56 # repo tip into a location beneath that, before using the docgen user
chris@223 57 # to extract docs from that location and write them into the tmpdir
chris@223 58
chris@223 59 # Same tmpdir for each project: we delete and recreate to avoid
chris@223 60 # cleanup duty from lots of directories being created
chris@223 61 #
chris@223 62 tmpdir=$(mktemp -d "$docdir/tmp_XXXXXX")
chris@223 63
chris@223 64 fail()
chris@223 65 {
chris@223 66 message="$1"
chris@223 67 echo "$message" 1>&2
chris@223 68 case "$tmpdir" in
chris@223 69 */tmp*) rm -rf "$tmpdir";;
chris@223 70 *);;
chris@223 71 esac
chris@223 72 exit 1
chris@223 73 }
chris@223 74
chris@223 75 case "$tmpdir" in
chris@223 76 /*) ;;
chris@223 77 *) fail "Temporary directory creation failed";;
chris@223 78 esac
chris@223 79
chris@223 80 chown docgen.www-data "$tmpdir" || fail "Temporary directory ownership change failed"
chris@223 81 chmod g+rwx "$tmpdir" || fail "Temporary directory permissions change failed"
chris@223 82
chris@203 83 for projectdir in "$hgdir"/* ; do
chris@203 84
chris@203 85 if [ -d "$projectdir" ] && [ -d "$projectdir/.hg" ]; then
chris@203 86
Chris@1628 87 if ! sudo -u www-data hg -R "$projectdir" -q update --check >> "$logfile" 2>&1; then
Chris@1628 88 echo "Failed to update Hg in $projectdir, skipping" >> "$logfile"
chris@223 89 continue
chris@223 90 fi
chris@223 91
chris@203 92 project=$(basename "$projectdir")
chris@203 93
chris@223 94 tmptargetdir="$tmpdir/doc"
chris@223 95 snapshotdir="$tmpdir/hgsnapshot"
chris@203 96
chris@223 97 rm -rf "$tmptargetdir" "$snapshotdir"
chris@223 98
chris@226 99 mkdir -m 770 "$tmptargetdir" || fail "Temporary target directory creation failed"
chris@226 100 chown docgen.www-data "$tmptargetdir" || fail "Temporary target directory ownership change failed"
chris@223 101
chris@223 102 mkdir -m 770 "$snapshotdir" || fail "Snapshot directory creation failed"
chris@223 103 chown docgen.www-data "$snapshotdir" || fail "Snapshot directory ownership change failed"
chris@223 104
Chris@1628 105 hgparents=$(sudo -u www-data hg -R "$projectdir" parents 2>> "$logfile")
chris@223 106 if [ -z "$hgparents" ]; then
Chris@1628 107 echo "Hg repo at $projectdir has no working copy (empty repo?), skipping" >> "$logfile"
chris@223 108 continue
chris@223 109 else
Chris@1628 110 echo "Found non-empty Hg repo: $projectdir for project $project" >> "$logfile"
chris@223 111 fi
chris@223 112
Chris@1628 113 if ! sudo -u www-data hg -R "$projectdir" archive -r tip -t files "$snapshotdir" >> "$logfile" 2>&1; then
Chris@1628 114 echo "Failed to pack archive from $projectdir, skipping" >> "$logfile"
chris@223 115 continue
chris@223 116 fi
chris@203 117
chris@203 118 targetdir="$docdir/$project"
chris@203 119
Chris@1628 120 echo "Temporary dir is $tmpdir, temporary doc dir is $tmptargetdir, snapshot dir is $snapshotdir, eventual target is $targetdir" >> "$logfile"
chris@203 121
chris@203 122 for x in $types; do
chris@226 123 if sudo -u docgen "$progdir/extract-$x.sh" "$project" "$snapshotdir" "$tmptargetdir" >> "$logfile" 2>&1; then
chris@226 124 break
chris@226 125 else
Chris@1628 126 echo "Failed to extract via type $x" >> "$logfile"
chris@203 127 fi
chris@203 128 done
chris@203 129
chris@223 130 if [ -f "$tmptargetdir/index.html" ]; then
Chris@1628 131 echo "Processing resulted in an index.html being created, looks good!" >> "$logfile"
chris@203 132 if [ ! -d "$targetdir" ] || [ ! -f "$targetdir/index.html" ]; then
Chris@1628 133 echo "This project hasn't had doc extracted before: enabling Embedded" >> "$logfile"
chris@218 134 enable_embedded "$project"
chris@203 135 fi
chris@203 136
chris@203 137 if [ -d "$targetdir" ]; then
chris@203 138 mv "$targetdir" "$targetdir"_"$$" && \
chris@223 139 mv "$tmptargetdir" "$targetdir" && \
chris@203 140 rm -rf "$targetdir"_"$$"
chris@223 141 chgrp -R "$redgrp" "$targetdir"
chris@203 142 else
chris@223 143 mv "$tmptargetdir" "$targetdir"
chris@223 144 chgrp -R "$redgrp" "$targetdir"
chris@203 145 fi
chris@228 146 else
Chris@1628 147 echo "Processing did not result in an index.html being created" >> "$logfile"
chris@203 148 fi
chris@203 149 fi
chris@203 150 done
chris@203 151
chris@223 152 rm -rf "$tmpdir"