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.

187 lines
6.5 KiB

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