provisioning tool for building opinionated architecture
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
6.4 KiB

10 months ago
  1. #!/bin/bash
  2. readonly UNWANTED_PACKAGES_STRING="nginx node python haxe"
  3. readonly MANDATORY_PACKAGES_STRING="wget apache2 make git imagemagick gettext libapache2-mod-neko mariadb-client sendemail libio-socket-ssl-perl libnet-ssleay-perl"
  4. ### CHECK
  5. function check() {
  6. PREFIX="recipe:cagettepei:check"
  7. check_unwanted_packages || return 21
  8. check_mandatory_packages || return 22
  9. check_apache_modules || return 23
  10. check_node8 || return 24
  11. check_python2 || return 25
  12. check_haxe3 || return 26
  13. check_cagettepei_batch || return 35
  14. check_cagettepei_timers || return 36
  15. }
  16. function check_apache_modules() {
  17. lxc exec "$CONTAINER" -- bash <<EOF
  18. set -Eeuo pipefail
  19. test -L /etc/apache2/mods-enabled/neko.load
  20. test -L /etc/apache2/mods-enabled/rewrite.load
  21. true
  22. EOF
  23. }
  24. function check_cagettepei_batch() {
  25. lxc exec "$CONTAINER" -- bash <<EOF
  26. set -Eeuo pipefail
  27. test -f /var/www/cagettepei/cagettepei-batch
  28. true
  29. EOF
  30. }
  31. function check_cagettepei_timers() {
  32. lxc exec "$CONTAINER" -- bash <<EOF
  33. set -Eeuo pipefail
  34. test -f /etc/systemd/system/cagettepei-batch-minute.service
  35. test -f /etc/systemd/system/cagettepei-batch-minute.timer
  36. test -f /etc/systemd/system/cagettepei-batch-day.service
  37. test -f /etc/systemd/system/cagettepei-batch-day.timer
  38. true
  39. EOF
  40. }
  41. function check_node8() {
  42. lxc exec "$CONTAINER" -- bash <<EOF
  43. set -Eeuo pipefail
  44. node --version | grep -q 'v8.17.0'
  45. EOF
  46. }
  47. function check_python2() {
  48. lxc exec "$CONTAINER" -- bash <<EOF
  49. set -Eeuo pipefail
  50. python --version 2>&1 | grep -q 'Python 2.7.18'
  51. EOF
  52. }
  53. function check_haxe3() {
  54. lxc exec "$CONTAINER" -- bash <<EOF
  55. set -Eeuo pipefail
  56. haxe -version 2>&1 | grep -q '3.4.7'
  57. EOF
  58. }
  59. function check_unwanted_packages() {
  60. lxc exec "$CONTAINER" -- bash <<EOF
  61. set -Eeuo pipefail
  62. mapfile -t PACKAGES <<< "$UNWANTED_PACKAGES_STRING"
  63. for package in \${PACKAGES[@]}; do
  64. ! (dpkg -l "\$package" 2>/dev/null | grep -q ^ii)
  65. done
  66. true # useful because for might return last inexistant package
  67. EOF
  68. }
  69. function check_mandatory_packages() {
  70. lxc exec "$CONTAINER" -- bash <<EOF
  71. set -Eeuo pipefail
  72. mapfile -t PACKAGES <<< "$MANDATORY_PACKAGES_STRING"
  73. for package in \${PACKAGES[@]}; do
  74. dpkg-query -l "\$package" 2>/dev/null | grep -q ^ii
  75. done
  76. EOF
  77. }
  78. ### INSTALL
  79. function install() {
  80. PREFIX="recipe:cagettepei:install"
  81. : $PREFIX
  82. launch_container "$CONTAINER"
  83. echo "initializing CagettePéi ... "
  84. echo -n "check unwanted packages..."
  85. check_unwanted_packages
  86. PREFIX="" echo "OK"
  87. lxc exec "$CONTAINER" -- bash <<EOF
  88. set -Eeuo pipefail
  89. echo installing CagettePéi...
  90. apt-get update
  91. apt-get install -y $MANDATORY_PACKAGES_STRING
  92. echo installing custom Node8...
  93. wget https://nodejs.org/download/release/v8.17.0/node-v8.17.0-linux-x64.tar.gz -O /tmp/node-v8.17.0-linux-x64.tar.gz
  94. tar -xzvf /tmp/node-v8.17.0-linux-x64.tar.gz -C /opt
  95. chown root:root -R /opt/node-v8.17.0-linux-x64
  96. ln -sf /opt/node-v8.17.0-linux-x64/bin/node /usr/local/bin/
  97. ln -sf /opt/node-v8.17.0-linux-x64/bin/npm /usr/local/bin/
  98. echo -n DONE
  99. echo installing custom Python2 with pypy...
  100. wget https://downloads.python.org/pypy/pypy2.7-v7.3.13-linux64.tar.bz2 -O /tmp/pypy2.7-v7.3.13-linux64.tar.bz2
  101. apt install -y bzip2
  102. bunzip2 -f /tmp/pypy2.7-v7.3.13-linux64.tar.bz2
  103. tar -xvf /tmp/pypy2.7-v7.3.13-linux64.tar -C /opt
  104. ln -sf /opt/pypy2.7-v7.3.13-linux64/bin/python /usr/local/bin/
  105. ln -sf /opt/pypy2.7-v7.3.13-linux64/bin/python2 /usr/local/bin/
  106. echo -n DONE
  107. echo installing custom Haxe3...
  108. wget https://github.com/HaxeFoundation/haxe/releases/download/3.4.7/haxe-3.4.7-linux64.tar.gz -O /tmp/haxe-3.4.7-linux64.tar.gz
  109. tar -xzvf /tmp/haxe-3.4.7-linux64.tar.gz -C /opt
  110. ln -sf /opt/haxe_20180221160843_bb7b827/haxe /usr/local/bin/
  111. ln -sf /opt/haxe_20180221160843_bb7b827/haxelib /usr/local/bin/
  112. echo -n DONE
  113. systemctl stop apache2
  114. rm -f /etc/apache2/sites-available/{000-default,default-ssl}.conf
  115. rm -f /etc/apache2/sites-enabled/000-default.conf
  116. rm -rf /var/www/html
  117. sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
  118. sed -i 's/^Listen 80$//' /etc/apache2/ports.conf
  119. echo "prepare folder for cagettepei instances"
  120. mkdir -p /var/www/cagettepei
  121. echo "enable neko and rewrite apache2 modules"
  122. a2enmod neko
  123. a2enmod rewrite
  124. EOF
  125. echo -n "copy cagettepei-batch..."
  126. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/apps/cagettepei/cagettepei-batch" "$CONTAINER/var/www/cagettepei/cagettepei-batch"
  127. lxc exec "$CONTAINER" -- chmod +x /var/www/cagettepei/cagettepei-batch
  128. PREFIX="" echo "OK"
  129. echo -n "copy cagettepei timers in systemd..."
  130. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/apps/cagettepei/systemd/cagettepei-batch-minute.service" "$CONTAINER/etc/systemd/system/cagettepei-batch-minute.service"
  131. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/apps/cagettepei/systemd/cagettepei-batch-minute.timer" "$CONTAINER/etc/systemd/system/cagettepei-batch-minute.timer"
  132. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/apps/cagettepei/systemd/cagettepei-batch-day.service" "$CONTAINER/etc/systemd/system/cagettepei-batch-day.service"
  133. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/apps/cagettepei/systemd/cagettepei-batch-day.timer" "$CONTAINER/etc/systemd/system/cagettepei-batch-day.timer"
  134. PREFIX="" echo "OK"
  135. echo -n "override apache2 service to launch cagettepei timers..."
  136. lxc exec "$CONTAINER" -- bash -c "SYSTEMD_EDITOR=tee systemctl edit apache2 <<EOT
  137. [Unit]
  138. BindsTo = cagettepei-batch-minute.timer cagettepei-batch-day.timer
  139. EOT"
  140. PREFIX="" echo "OK"
  141. echo "enable and start cagettepei timers in systemd..."
  142. lxc exec "$CONTAINER" -- bash <<EOF
  143. systemctl enable cagettepei-batch-minute.timer cagettepei-batch-day.timer
  144. systemctl start cagettepei-batch-minute.timer cagettepei-batch-day.timer
  145. EOF
  146. PREFIX="" echo "OK"
  147. }
  148. ### MAIN
  149. . "$MIAOU_BASEDIR/lib/init.sh"
  150. arg1_required "$@"
  151. readonly CONTAINER="$1"
  152. check || (
  153. install
  154. check
  155. )