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.

210 lines
7.7 KiB

  1. #!/bin/bash
  2. ## Installing base docker-host
  3. wget http://docker.0k.io/get/ -qO - | bash || exit 1
  4. ## shlib is now available
  5. . /etc/shlib || {
  6. echo "shlib is not available. Bailing out." >&2
  7. exit 1
  8. }
  9. clone_or_update() {
  10. local pkg="$1" branch=${2:-master} \
  11. DEPLOY_PATH="/opt/apps" \
  12. GIT_BASE="https://git.myceliandre.fr/Myceliandre" \
  13. current_branch
  14. if [ -d "$DEPLOY_PATH/$pkg" ]; then
  15. cd "$DEPLOY_PATH/$pkg"
  16. current_branch=$(git rev-parse --abbrev-ref HEAD)
  17. if [ "$current_branch" != "$branch" ]; then
  18. echo "Fatal: $DEPLOY_PATH/$pkg git repos has unexpected branch checkouted."
  19. echo " To avoid changing thing, we prefer to bailout."
  20. return 1
  21. fi
  22. git pull -r || return 1
  23. else
  24. mkdir -p "$DEPLOY_PATH" && cd "$DEPLOY_PATH"
  25. git clone "$GIT_BASE/${pkg}" -b "$branch" --depth=1
  26. fi
  27. }
  28. install_bin() {
  29. local path="$1" DEST_PATH=/usr/local/bin
  30. ln -sfnv "$path"/* "$DEST_PATH" || return 1
  31. find -L "$DEST_PATH" -maxdepth 1 -type l -ilname "$path"/\* -exec rm -v {} \; || return 1
  32. }
  33. set -e
  34. clone_or_update myc-manage || exit 1
  35. install_bin /opt/apps/myc-manage/bin
  36. clone_or_update myc-deploy || exit 1
  37. #[ -e /etc/compose.conf ] || ln -sfv /opt/apps/myc-deploy/etc/compose.conf /etc/compose.conf
  38. ## XXXvlab: should get rid of this file in some future
  39. cd /opt/apps/myc-deploy
  40. if ! grep "^DEFAULT_COMPOSE_FILE=$PWD/compose.yml$" /etc/compose/local.conf >/dev/null 2>&1; then
  41. echo "Adding CWD=$PWD to docker-compose."
  42. cat <<EOF >> /etc/compose/local.conf
  43. DEFAULT_COMPOSE_FILE=$PWD/compose.yml
  44. EOF
  45. fi
  46. type -t docker-clean || ln -sfv /opt/apps/0k-docker/src/bin/docker-clean /usr/local/bin
  47. if [ -z "$WITHOUT_DOCKER_CLEAN" ]; then
  48. ln -sfn /opt/apps/0k-docker/src/bin/docker-clean /etc/cron.daily/docker-clean
  49. fi
  50. cd /opt/apps/myc-deploy
  51. cat <<EOF > /root/.pgm.rc
  52. prefix_pg_local_command=" " ## otherwise, will default to sudo -u postgres
  53. pgpass="/srv/datastore/data/postgres/var/lib/postgresql/data/pgpass"
  54. [ -f "\$pgpass" ] || {
  55. echo "No '\$pgpass' found. Postgres database doesn't seem to be setup." >&2
  56. exit 1
  57. }
  58. cp "\$pgpass" /root/.pgpass
  59. PGUSER=\${PGUSER:-postgres}
  60. if [ -z "\$PGHOST" ]; then
  61. PGHOST=\$(docker-ip | grep postgres | xargs echo | cut -f 3 -d " ") || {
  62. echo "No local running postgres docker found." >&2
  63. exit 1
  64. }
  65. fi
  66. export PGHOST PGUSER
  67. EOF
  68. # if ! [ -s /etc/ssh/vm_git_myceliandre_access_id_rsa ]; then
  69. # curl -L --fail https://docker.0k.io/get/vm_git_myceliandre_access_id_rsa > /etc/ssh/vm_git_myceliandre_access_id_rsa || {
  70. # echo "Fatal: Could not retrieve http://docker.0k.io/get/vm_git_myceliandre_access_id_rsa ..." >&2
  71. # rm -f /etc/ssh/vm_git_myceliandre_access_id_rsa
  72. # exit 1
  73. # }
  74. # ## Not so usefull as it is public !
  75. # chmod 0600 /etc/ssh/vm_git_myceliandre_access_id_rsa
  76. # fi
  77. mkdir -p /root/.ssh
  78. cat <<EOF >> /root/.ssh/config
  79. Host git.myceliandre.fr
  80. User git
  81. IdentityFile /etc/ssh/vm_git_myceliandre_access_id_rsa
  82. UserKnownHostsFile /dev/null
  83. StrictHostKeyChecking no
  84. Port 5022
  85. EOF
  86. ##
  87. ## We could need some docker-compose for some quick hacks
  88. ##
  89. version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
  90. if type -p python3 >/dev/null 2>&1 &&
  91. ! version_gt $(python3 --version | cut -f 2 -d " ") 3.9 ; then
  92. if ! type -p docker-compose >/dev/null; then
  93. # seems to require a C compiler
  94. apt-get install -y build-essential libffi-dev </dev/null &&
  95. pip install wheel==0.33.6 &&
  96. pip install pip==19.3.1 cffi==1.12.3 subprocess32==3.5.4 texttable==1.6.2 \
  97. pyrsistent==0.15.7 \
  98. git+https://github.com/0k/compose@run_ignore_orphans || exit 1
  99. ## Bug after updating pip
  100. hash -d pip || exit 1
  101. fi
  102. fi
  103. if [ "$DOMAIN" ]; then
  104. sed -ri "s/^(\s+domain:\s+).*$/\1$DOMAIN/g" compose.yml
  105. fi
  106. if [ "$MAIN_PASSWORD" ]; then
  107. sed -ri "s/^(\s+admin-password:\s+).*$/\1$MAIN_PASSWORD/g" compose.yml
  108. fi
  109. pip install ovh ||
  110. pip install ovh --break-system-packages || exit 1
  111. #compose --debug up odoo apache
  112. ## Temporary work around for old images that don't want to build
  113. docker pull docker.0k.io/php:7.4-myc && docker tag docker.0k.io/php:7.4-myc myc_frontend &&
  114. docker pull docker.0k.io/cron:jessie && docker tag docker.0k.io/cron:jessie myc_cron &&
  115. ## Creation of an account to send notification to ntfy server
  116. echo "-----BEGIN OPENSSH PRIVATE KEY-----
  117. b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn
  118. NhAAAAAwEAAQAAAQEApGXqKYEJbv0xu/wKl1mXtiz90kZbqq7FALTZYyYqWZfsp4RtiHXi
  119. NC7WKFiabQ1j1s8WuE0I2xJNSpzjHuWouduLQ5WtTl0PIWausMYaHam5T1I3KLVBg1QNi8
  120. 0wL5LVMD3mMoxVstQmlvYOuODZSaCS6j6ND33IS5IG11M9xwR6IcUKLKnfF5h5OQbTSiQ0
  121. ANgw5KmYdGBQ8PUIQO0ELz0rhjJVZLADZspXLoWikNURmlYozfcSFcfOVA7AkqeMKMZd64
  122. 72WDGTd9NrAOq+hmLMKDfJXuHlKrNuqmK1jVGs/5YcSArrFyuvKOabT8AJfjBDEVtbsSeu
  123. mN44MoH1bwAAA8hI4f+cSOH/nAAAAAdzc2gtcnNhAAABAQCkZeopgQlu/TG7/AqXWZe2LP
  124. 3SRluqrsUAtNljJipZl+ynhG2IdeI0LtYoWJptDWPWzxa4TQjbEk1KnOMe5ai524tDla1O
  125. XQ8hZq6wxhodqblPUjcotUGDVA2LzTAvktUwPeYyjFWy1CaW9g644NlJoJLqPo0PfchLkg
  126. bXUz3HBHohxQosqd8XmHk5BtNKJDQA2DDkqZh0YFDw9QhA7QQvPSuGMlVksANmylcuhaKQ
  127. 1RGaVijN9xIVx85UDsCSp4woxl3rjvZYMZN302sA6r6GYswoN8le4eUqs26qYrWNUaz/lh
  128. xICusXK68o5ptPwAl+MEMRW1uxJ66Y3jgygfVvAAAAAwEAAQAAAQEAnzpm1tQ4QtvRc/Xm
  129. fDk2jCh/n06uMl8cSFbhxvqMQkK34HiPboBfG5PRsTpAOCej78acht12Gllbq0zRXneqOH
  130. nAJTGvrhrMMNm3kVgOq3RcG8vRyQfl8EFU7XdLmIhrHFKXx5XM22xIBCdGkyKU0o9IPMFg
  131. 9wQpH6jMH3psd3j9M7x9QwPZKujv5XMF7DrMdtwAsU/XPTHrOedxdmnVpy9hwTpygTP6Xs
  132. TRL9CgdoIo1arZAu8M5/h8xS37IKEe4lUNr/j5tJe3d0HQ+aXCtVrD1WDyZnslPnrDr0MQ
  133. XCbx957Kh6VJ11t8el7x21Yr0iuF+S/RSKxsiyqC3J4EAQAAAIAQabySOpcNGk/kR3A7KH
  134. Szz3uft+c9qmt9+b5Sth+GmRKEoOO51hi3K+WrzArMJ3AyO8QGodjBAStcbFMDW5DkWxFH
  135. 0BuuXL2JTNJdn/2iBQH2bjLI68zTCrqHapI4l3kwTFUDybZP9hcdN9QrsY10rh+WiUILt7
  136. gIB69cxQKeuAAAAIEAz7W5MrUL50A5wi7EMalR9+dIVDTvpyub7Ip6dczRyXt9Xz35mv4S
  137. pBaK0mabJPgNP23fGoDhsXhZoDxJpGaBMCciLujVt/wJCX+vXbXwBXwMi0DC9AF/W6FGYb
  138. GAusBeJzziXuEmQlirbKFTwkBMVOpMWvsX/DQgDjsTVKjm9u8AAACBAMqeZ17+r7602t9P
  139. 8Gie1YXde/T1vMeQAHNCOCBoiuERdM/xDyQE3EXk9Pj2LOvhEu9CskQUCkuZS4JcMO+GQz
  140. zfqty7nmi/sAQTwPe+Gl9dnvS+dixeDmS3g+rP+hEAYhVPQgQhm0zzMvzlYk437WB+9BYk
  141. JUr3Zp+T2t4WoOmBAAAAEm50ZnlAY29yZS0wMS4way5pbw==
  142. -----END OPENSSH PRIVATE KEY-----" > /root/.ssh/ntfy-key
  143. echo "|1|e3yYRMYJg0EpbOeTplTgtI+KbY4=|PotgCF8Rwt2OZFKr1CGYWpJ6FRA= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBH8axkuXlI2zowRvL3Vyg/qgkKK57cqX7+9WRaLm9ECWkLvaVPGunR1zVJUZdTO3gjlSkqtblTcI00BBLt+zQvE=" \
  144. >> /root/.ssh/known_hosts
  145. chmod 600 /root/.ssh/ntfy-key
  146. ## Request token to ntfy server and add to config file
  147. cred=$(ssh -i /root/.ssh/ntfy-key ntfy@core-01.0k.io request-token) || >&2 echo "Error while requesting token to ntfy server"
  148. login_ntfy=$(printf "%s" "${cred%$'\n'*}")
  149. password_ntfy=$(printf "%s" "${cred#$'\n'*}")
  150. ## if the config file exist and LOGIN PASSWORD ARE already in we do nothing
  151. if [ -z "$login_ntfy"] || [[ "$login_ntfy" == *$'\n'*]]; then
  152. echo "Error: couldn’t infer credential from ntfy server" >&2;
  153. printf "%s" "$cred" | sed -r 's/^ |/g' >&2;
  154. exit
  155. fi
  156. config_file="/etc/ntfy/ntfy.conf"
  157. mkdir -p "${config_file%/*}"
  158. if [ -f "$config_file" ] || touch $config_file || {
  159. echo "Error: couldn’t create config file $config_file" >&2;
  160. exit 1
  161. }; then
  162. if grep -qE '^LOGIN=' "$config_file"; then
  163. sed -i "s/^LOGIN=.*/LOGIN='$login'/" "$config_file"
  164. else
  165. echo "LOGIN='$login'" >> "$config_file"
  166. fi
  167. if grep -qE '^PASSWORD=' "$config_file"; then
  168. sed -i "s/^PASSWORD=.*/PASSWORD='$password'/" "$config_file"
  169. else
  170. echo "PASSWORD='$password'" >> "$config_file"
  171. fi
  172. fi
  173. ## Marker to probe if this script finished it's job
  174. echo "done" > /var/run/myc-installer.0k.io.state