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.

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