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.

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