fork 0k-charms
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.

278 lines
7.4 KiB

  1. #!/bin/bash
  2. set -eux
  3. ## 0k git remote path
  4. GIT_0K_BASE=${GIT_0K_BASE:-"0k-ro:/var/git"}
  5. ## 0k git remote options
  6. GIT_0K_CLONE_OPTIONS=${GIT_0K_CLONE_OPTIONS:-""}
  7. NO_DOCKER_RESTART=${NO_DOCKER_RESTART:-}
  8. ##
  9. ## Install 0k-manage
  10. ##
  11. mkdir -p /opt/apps
  12. (
  13. if [ -d "/opt/apps/0k-manage" ]; then
  14. cd /opt/apps/0k-manage &&
  15. git pull -r
  16. else
  17. cd /opt/apps &&
  18. git clone $GIT_0K_CLONE_OPTIONS "$GIT_0K_BASE/0k/0k-manage.git" &&
  19. cd /opt/apps/0k-manage &&
  20. git checkout 0k/prod/master
  21. fi
  22. ## Debian 9 did not have setuptool
  23. if [ "$(python -c 'import setuptools' 2>&1 | tail -n 1)" == "ImportError: No module named setuptools" ]; then
  24. pip install setuptools
  25. fi
  26. pip install sact.epoch
  27. if [ "$(python -c 'import sact.epoch' 2>&1 | tail -n 1)" == "ImportError: No module named interface" ]; then
  28. echo "Error: conflicting installation of zope.interface detected. Trying workaround."
  29. (
  30. cd /usr/local/lib/python2.7/dist-packages
  31. mv zope zope-bad
  32. ) &&
  33. pip install zope.interface --upgrade &&
  34. pip install zope.component --upgrade
  35. if [ "$(python -c 'import sact.epoch' 2>&1 | tail -n 1)" == "" ]; then
  36. echo "Workaround worked."
  37. else
  38. echo "Failed work around."
  39. exit 1
  40. fi
  41. fi
  42. ln -sf /opt/apps/0k-manage/src/bin/pick2del_backups /usr/local/bin/
  43. # ln -sf /opt/apps/0k-manage/src/bin/* /usr/local/bin/
  44. )
  45. ##
  46. ## Remove possible previous docker-compose related stuff
  47. ##
  48. if [ -f /etc/compose/local.conf ]; then
  49. sed -ri 's%^(. /opt/venv/docker-compose/bin/activate)$%# \1 ## docker-compose not needed anymore%g' \
  50. /etc/compose/local.conf
  51. fi
  52. ##
  53. ## Install 0k-charm
  54. ##
  55. (
  56. apt-get install -y kal-shlib-{common,charm,cache,cmdline,config} </dev/null
  57. if [ -d "/opt/apps/0k-charm" ]; then
  58. cd /opt/apps/0k-charm &&
  59. git checkout master &&
  60. git pull -r
  61. else
  62. cd /opt/apps &&
  63. git clone $GIT_0K_CLONE_OPTIONS "$GIT_0K_BASE/0k/0k-charm.git"
  64. fi
  65. ln -sfn /opt/apps/0k-charm/bin/charm /usr/local/sbin/charm
  66. )
  67. ##
  68. ## Install 0k-charms
  69. ##
  70. (
  71. if [ -d "/opt/apps/0k-charms" ]; then
  72. cd /opt/apps/0k-charms &&
  73. git checkout master &&
  74. git pull -r
  75. else
  76. cd /opt/apps &&
  77. git clone $GIT_0K_CLONE_OPTIONS "$GIT_0K_BASE/0k/0k-charms.git"
  78. fi
  79. if [ -d "/srv/charm-store" ]; then
  80. if [ -L "/srv/charm-store" ]; then
  81. echo "Already have a valid /srv/charm-store"
  82. elif [ -L "/srv/charm-store/0k-charms" ]; then
  83. mv /srv/charm-store{,.old} &&
  84. mv /srv/charm-store.old/0k-charms /srv/charm-store &&
  85. rmdir /srv/charm-store.old
  86. else
  87. die "Unexpected layout of '/srv/charm-store'. Bailing out."
  88. fi
  89. else
  90. ln -sfn /opt/apps/0k-charms /srv/charm-store
  91. fi
  92. )
  93. ##
  94. ## Install lxc-scripts
  95. ##
  96. (
  97. if ! [ -d "/opt/apps/lxc-scripts" ]; then
  98. cd /opt/apps &&
  99. git clone $GIT_0K_CLONE_OPTIONS "$GIT_0K_BASE/0k/lxc-scripts.git"
  100. fi
  101. cd /opt/apps/lxc-scripts &&
  102. git checkout master &&
  103. git pull -r &&
  104. ln -sfn /opt/apps/lxc-scripts/bin/lxc-* /usr/local/sbin/ &&
  105. if [ -f /etc/default/lxc ]; then
  106. if [ -f /etc/default/lxc.pre-install ]; then
  107. cp /etc/default/lxc.pre-install /etc/default/lxc
  108. else
  109. cp /etc/default/lxc /etc/default/lxc.pre-install
  110. fi
  111. [ -d /usr/lib/lxc/templates ] && {
  112. ln -sfn /opt/apps/lxc-scripts/usr/lib/lxc/templates/lxc-0k-ubuntu-cloud /usr/lib/lxc/templates/
  113. echo TEMPLATE_PATH=/usr/lib/lxc/templates >> /etc/default/lxc
  114. }
  115. [ -d /usr/share/lxc/templates ] && {
  116. ln -sfn /opt/apps/lxc-scripts/usr/lib/lxc/templates/lxc-0k-ubuntu-cloud /usr/share/lxc/templates
  117. echo TEMPLATE_PATH=/usr/share/lxc/templates >> /etc/default/lxc
  118. }
  119. fi
  120. )
  121. ##
  122. ## Install 0k-docker
  123. ##
  124. (
  125. apt-get install -y kal-shlib-docker jq </dev/null
  126. if [ -d "/opt/apps/0k-docker" ]; then
  127. cd /opt/apps/0k-docker &&
  128. git checkout master &&
  129. git pull -r
  130. else
  131. cd /opt/apps &&
  132. git clone $GIT_0K_CLONE_OPTIONS "$GIT_0K_BASE/0k/0k-docker"
  133. fi
  134. ln -sfn /opt/apps/0k-docker/src/bin/* /usr/local/sbin/
  135. ## Probably dangerous for docker only solution that do not
  136. ## rely on the same choices
  137. #ln -sfn /opt/apps/0k-docker/src/bin/docker-clean /etc/cron.daily/docker-clean
  138. ## in update, will remove broken links to binaries that were removed
  139. find -L /usr/local/sbin -maxdepth 1 -type l -ilname /opt/apps/0k-docker/\* -delete
  140. )
  141. ##
  142. ## Install 0k.io certificate authority
  143. ##
  144. ## Note that docker should be installed after (or be restarted).
  145. apt-get install -y curl </dev/null
  146. ## This does not seem to work anymore (docker v1.12, ubutnu 14.04 on gani)
  147. ca=/etc/ssl/ca.0k.io.pem
  148. need_restart=
  149. oldmd5=
  150. if [ -f "$ca" ]; then
  151. oldmd5=$(md5sum "$ca")
  152. fi
  153. echo "Fetching 0k.io CA certificate..."
  154. curl http://docker.0k.io/get/ca.0k.io.pem > "$ca"
  155. if [[ "$(md5sum "$ca")" != "$oldmd5" ]]; then
  156. need_restart=1
  157. cat "$ca" >> /etc/ssl/certs/ca-certificates.crt
  158. fi
  159. ## This is the new way: https://docs.docker.com/engine/security/certificates/
  160. ca_ln="/etc/docker/certs.d/docker.0k.io/ca.crt"
  161. mkdir -p "$(dirname "$ca_ln")"
  162. if ! [ -L "$ca_ln" ] || [ "$(realpath "$ca_ln")" != "$ca" ] ; then
  163. ln -sfn "$ca" "$ca_ln"
  164. need_restart=1
  165. fi
  166. if [ -n "$need_restart" ] && [ -z "$NO_DOCKER_RESTART" ]; then
  167. service docker restart
  168. fi
  169. echo "Login into our server."
  170. docker login -u vm -p iamavm https://docker.0k.io
  171. sed -ri 's/^#(net\.ipv4\.ip_forward=1)$/\1/g' /etc/sysctl.conf
  172. sysctl -w net.ipv4.ip_forward=1
  173. ##
  174. ## Installation of compose
  175. ##
  176. docker pull docker.0k.io/compose
  177. if [ -d "/opt/apps/0k-compose" ]; then
  178. cd "/opt/apps/0k-compose" &&
  179. git pull -r
  180. else
  181. mkdir -p /opt/apps && cd /opt/apps
  182. git clone "$GIT_0K_BASE"/0k/0k-compose.git
  183. fi
  184. [ -e /usr/local/bin/compose ] || ln -sfnv /opt/apps/0k-compose/bin/compose /usr/local/bin/
  185. rm -rf /var/cache/compose
  186. cat <<EOF > /etc/default/datastore
  187. DATASTORE=/srv/datastore
  188. EOF
  189. cat <<EOF > /etc/default/compose
  190. ## if not provided, this will be the default service launched.
  191. export DEFAULT_SERVICES=""
  192. ## In new compose (running in docker) this is not yet really supported,
  193. ## and is it wanted ?
  194. #export DEFAULT_PROJECT_NAME=$(hostname | cut -f 2 -d . )
  195. #DEFAULT_PROJECT_NAME=\${DEFAULT_PROJECT_NAME,,} ## to lowercase
  196. export DOCKER_DATASTORE=\$([ -e /etc/default/datastore ] && . /etc/default/datastore && echo \$DATASTORE)
  197. export DATASTORE=\$DOCKER_DATASTORE/data
  198. export CONFIGSTORE=\$DOCKER_DATASTORE/config
  199. EOF
  200. if ! egrep "^DEFAULT_COMPOSE_FILE=" /etc/compose/local.conf >/dev/null 2>&1; then
  201. mkdir /etc/compose -p
  202. touch /etc/compose/local.conf
  203. echo "DEFAULT_COMPOSE_FILE=/etc/compose/compose.yml" >> /etc/compose/local.conf
  204. fi
  205. ##
  206. ## Install pgm
  207. ##
  208. (
  209. if [ -d "/opt/apps/0k-pgm" ]; then
  210. cd /opt/apps/0k-pgm &&
  211. git pull -r
  212. else
  213. cd /opt/apps &&
  214. git clone $GIT_0K_CLONE_OPTIONS "$GIT_0K_BASE/0k/0k-pgm.git" &&
  215. cd /opt/apps/0k-pgm &&
  216. git checkout master
  217. fi
  218. ln -sfnv /opt/apps/0k-pgm/bin/* /usr/local/bin/
  219. find -L /usr/local/bin -maxdepth 1 -type l -ilname /opt/apps/0k-pgm/bin/\* -delete
  220. apt-get install -y pv buffer < /dev/null
  221. apt-get install -y postgresql-client </dev/null
  222. )