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