install.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/bin/bash
  2. #
  3. # WyOJ install script -- runs *inside* the web container.
  4. #
  5. # install.sh -p | --prep prepare the container: apache + php config, web root
  6. # symlink, runtime directories. No database access.
  7. # Called once from the Dockerfile.
  8. # install.sh -i | --init first-boot init: write app/.config.php from the
  9. # environment, wait for the database, prepare the
  10. # data volume, mark the install as done.
  11. # Called from docker-entrypoint.sh.
  12. #
  13. # Package installation used to live here too (getAptPackage); it now lives in
  14. # the Dockerfile so that there is exactly one place that installs software.
  15. set -eu
  16. _web_root_="/opt/uoj/web"
  17. _data_root_="${UOJ_DATA:-/var/uoj_data}"
  18. usage() {
  19. cat <<'EOF'
  20. Usage: install.sh [-p|--prep] [-i|--init]
  21. -p, --prep prepare apache/php configuration and the web root symlink
  22. -i, --init first-boot initialisation (needs the database to be up)
  23. EOF
  24. }
  25. # ---------------------------------------------------------------------------
  26. # apache + php
  27. # ---------------------------------------------------------------------------
  28. setLAMPConf() {
  29. echo "==> configuring apache and php"
  30. # AllowOverride All is set here, inside the vhost, instead of patching
  31. # apache2.conf by line number (`sed -i '172s/...'`).
  32. # Logs go to the container's stdout/stderr so that `docker logs` works.
  33. cat >/etc/apache2/sites-available/000-uoj.conf <<UOJEOF
  34. <VirtualHost *:80>
  35. ServerAdmin ${SERVER_ADMIN:-admin@local_uoj.ac}
  36. DocumentRoot /var/www/uoj
  37. <Directory /var/www/uoj>
  38. Options -Indexes +FollowSymLinks
  39. AllowOverride All
  40. Require all granted
  41. </Directory>
  42. SetEnvIf Request_URI "^/judge/.*\$" judgelog
  43. ErrorLog /proc/self/fd/2
  44. CustomLog /proc/self/fd/1 combined env=!judgelog
  45. CustomLog /proc/self/fd/1 common env=judgelog
  46. XSendFile On
  47. XSendFilePath ${_data_root_}
  48. XSendFilePath ${_web_root_}/app/storage
  49. </VirtualHost>
  50. UOJEOF
  51. a2ensite 000-uoj.conf
  52. a2dissite 000-default.conf 2>/dev/null || true
  53. a2enmod rewrite headers
  54. # PHP session store used by .htaccess (session.save_path).
  55. install -d -m 1733 /var/lib/php/uoj_sessions
  56. }
  57. # ---------------------------------------------------------------------------
  58. # web root
  59. # ---------------------------------------------------------------------------
  60. setWebConf() {
  61. echo "==> preparing web root and runtime directories"
  62. ln -sfn "${_web_root_}" /var/www/uoj
  63. mkdir -p "${_data_root_}/upload" \
  64. "${_web_root_}/app/storage/submission" \
  65. "${_web_root_}/app/storage/tmp" \
  66. "${_web_root_}/app/storage/paste"
  67. chown -R www-data:www-data "${_data_root_}" "${_web_root_}/app/storage"
  68. # storage is bind-mounted from the host; it must stay group-writable.
  69. chmod -R g+ws "${_web_root_}/app/storage"
  70. }
  71. # ---------------------------------------------------------------------------
  72. # config file
  73. #
  74. # The old version piped a heredoc into `php -a` and wrote
  75. # str_replace(\'_httpHost_\', ...)
  76. # Bash does not strip the backslash in an unquoted heredoc, so that produced a
  77. # PHP parse error and no config file at all. This uses a *quoted* heredoc plus
  78. # getenv() instead, which needs no escaping and honours the variables that
  79. # docker-compose.yml already passes in.
  80. # ---------------------------------------------------------------------------
  81. writeConfig() {
  82. echo "==> writing app/.config.php from the environment"
  83. php <<'PHPEOF'
  84. <?php
  85. $config = include '/opt/uoj/web/app/.default-config.php';
  86. $env = function ($key, $default) {
  87. $v = getenv($key);
  88. return ($v === false || $v === '') ? $default : $v;
  89. };
  90. $config['database']['host'] = $env('DATABASE_HOST', $config['database']['host']);
  91. $config['database']['database'] = $env('DATABASE_NAME', $config['database']['database']);
  92. $config['database']['username'] = $env('DATABASE_USER', $config['database']['username']);
  93. $config['database']['password'] = $env('DATABASE_PASSWORD', $config['database']['password']);
  94. // NOTE: there is deliberately no port setting -- DB::init() hardcodes :3306.
  95. $config['judger']['socket']['port'] = $env('JUDGER_SOCKET_PORT', $config['judger']['socket']['port']);
  96. $config['judger']['socket']['password'] = $env('JUDGER_SOCKET_PASSWORD', $config['judger']['socket']['password']);
  97. $config['security']['user']['client_salt'] = $env('SALT_0', $config['security']['user']['client_salt']);
  98. $config['security']['cookie']['checksum_salt'] = [
  99. $env('SALT_1', $config['security']['cookie']['checksum_salt'][0]),
  100. $env('SALT_2', $config['security']['cookie']['checksum_salt'][1]),
  101. $env('SALT_3', $config['security']['cookie']['checksum_salt'][2]),
  102. ];
  103. $protocol = $env('UOJ_PROTOCOL', 'http');
  104. $config['web']['main']['protocol'] = $protocol;
  105. $config['web']['blog']['protocol'] = $protocol;
  106. if ($host = $env('UOJ_HOST', null)) {
  107. $config['web']['main']['host'] = $host;
  108. $config['web']['blog']['host'] = $host;
  109. }
  110. $out = "<?php\nreturn "
  111. . str_replace("'_httpHost_'", 'UOJContext::httpHost()', var_export($config, true))
  112. . ";\n";
  113. file_put_contents('/opt/uoj/web/app/.config.php', $out);
  114. // The config is bind-mounted from the host, where it may be root-owned 0600;
  115. // apache runs as www-data and has to be able to *read* it.
  116. @chmod('/opt/uoj/web/app/.config.php', 0644);
  117. echo "wrote /opt/uoj/web/app/.config.php\n";
  118. PHPEOF
  119. }
  120. # ---------------------------------------------------------------------------
  121. # database
  122. # ---------------------------------------------------------------------------
  123. waitForDatabase() {
  124. echo "==> waiting for the database"
  125. # The web image has no mysql client; use mysqli from the config environment.
  126. php -r '
  127. $host = getenv("DATABASE_HOST") ?: "uoj-db";
  128. $user = getenv("DATABASE_USER") ?: "root";
  129. $pass = getenv("DATABASE_PASSWORD") ?: "root";
  130. $db = getenv("DATABASE_NAME") ?: "app_uoj233";
  131. for ($i = 0; $i < 60; $i++) {
  132. $m = @mysqli_connect($host, $user, $pass, $db);
  133. if ($m) { echo "database $host is up\n"; exit(0); }
  134. sleep(2);
  135. }
  136. fwrite(STDERR, "ERROR: database $host unreachable after 120s\n");
  137. exit(1);
  138. '
  139. }
  140. # ---------------------------------------------------------------------------
  141. initProgress() {
  142. writeConfig
  143. waitForDatabase
  144. echo "==> preparing the data volume"
  145. mkdir -p "${_data_root_}/upload" \
  146. "${_web_root_}/app/storage/submission" \
  147. "${_web_root_}/app/storage/tmp" \
  148. "${_web_root_}/app/storage/paste"
  149. chown -R www-data:www-data "${_data_root_}" "${_web_root_}/app/storage"
  150. chmod -R g+ws "${_web_root_}/app/storage"
  151. # /judge/download/judger serves this to judge clients (self-update).
  152. # It is built into the image because app/storage is a bind mount and would
  153. # otherwise shadow whatever the image put there.
  154. if [ -f /opt/uoj/judge_client.zip ]; then
  155. cp /opt/uoj/judge_client.zip "${_web_root_}/app/storage/judge_client.zip"
  156. chown www-data:www-data "${_web_root_}/app/storage/judge_client.zip"
  157. echo "==> published judge_client.zip to app/storage"
  158. else
  159. echo "WARN: /opt/uoj/judge_client.zip is missing; /judge/download/judger will 404"
  160. fi
  161. # The schema comes from the db image (db/app_uoj233.sql), so this is
  162. # normally a no-op. It is kept so that app/upgrade/* still works later.
  163. echo "==> running upgrade:latest"
  164. php /var/www/uoj/app/cli.php upgrade:latest \
  165. || echo "WARN: upgrade:latest failed; the schema is expected to come from the db image"
  166. touch "${_data_root_}/.UOJSetupDone"
  167. echo "*** WyOJ is ready ***"
  168. }
  169. if [ $# -le 0 ]; then
  170. usage
  171. exit 1
  172. fi
  173. while [ $# -gt 0 ]; do
  174. case "$1" in
  175. -p | --prep)
  176. echo 'Preparing WyOJ web container...'
  177. setLAMPConf
  178. setWebConf
  179. ;;
  180. -i | --init)
  181. echo 'Initialising WyOJ...'
  182. initProgress
  183. ;;
  184. -h | --help)
  185. usage
  186. ;;
  187. *)
  188. echo "Illegal option $1" >&2
  189. usage >&2
  190. exit 1
  191. ;;
  192. esac
  193. shift
  194. done