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.

254 lines
5.6 KiB

  1. #!/bin/bash
  2. exname=$(basename $0)
  3. prefix_cmd="
  4. . /etc/shlib
  5. include common
  6. include parse
  7. . ../lib/common
  8. "
  9. ##
  10. ## Mocks
  11. ##
  12. relation-get() {
  13. local key="$1"
  14. echo "$CFG" | shyaml get-value "$key" 2>/dev/null
  15. }
  16. export -f relation-get
  17. relation-set() {
  18. local key="$1" value="$2"
  19. echo "relation-set $key:" >&2
  20. echo "$value" | prefix " | " >&2
  21. }
  22. export -f relation-set
  23. get_service_relations() {
  24. printf "%s\0" "${RELATIONS[@]}"
  25. }
  26. export -f get_service_relations
  27. file_put() {
  28. echo "file_put $1"
  29. cat - | prefix " | "
  30. }
  31. export -f file_put
  32. docker() {
  33. echo "docker" "$@"
  34. echo stdin:
  35. cat - | prefix " | "
  36. }
  37. export -f docker
  38. config-add() {
  39. echo "config-add"
  40. echo "$1" | prefix " | "
  41. }
  42. export -f config-add
  43. mkdir() {
  44. echo "called: $FUNCNAME $@" >&2
  45. }
  46. export -f mkdir
  47. setfacl() {
  48. echo "called: $FUNCNAME $@" >&2
  49. }
  50. export -f setfacl
  51. chgrp() {
  52. echo "called: $FUNCNAME $@" >&2
  53. }
  54. export -f chgrp
  55. chmod() {
  56. echo "called: $FUNCNAME $@" >&2
  57. }
  58. export -f chmod
  59. cached_cmd_on_base_image() {
  60. echo "called: $FUNCNAME $@" >&2
  61. echo "stdout:" >&2
  62. echo "<GID>" | prefix " | " >&2
  63. echo "<GID>"
  64. }
  65. export -f cached_cmd_on_base_image
  66. ##
  67. ## apache_vhost_create
  68. ##
  69. try "
  70. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  71. DOMAIN=www.example.com
  72. DOCKER_SITE_PATH=/var/www/\$DOMAIN
  73. apache_vhost_create"
  74. is errlvl 0
  75. is err part "\
  76. relation-set protocol:
  77. | http"
  78. is out reg '^file_put \$SERVICE_CONFIGSTORE/.*/www.example.com.conf'
  79. try "
  80. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  81. DOMAIN=www.example.com
  82. DOCKER_SITE_PATH=/var/www/\$DOMAIN
  83. CFG='
  84. ssl: true
  85. '
  86. apache_vhost_create"
  87. is errlvl 0
  88. is err part "## Auto-redirection from http to https"
  89. is err part "\
  90. relation-set protocol:
  91. | https"
  92. is out reg '^file_put \$SERVICE_CONFIGSTORE/.*/www.example.com.conf'
  93. try "
  94. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  95. export CONFIGSTORE='\$CONFIGSTORE'
  96. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  97. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  98. DOMAIN=www.example.com
  99. DOCKER_SITE_PATH=/var/www/\$DOMAIN
  100. CFG='
  101. ssl:
  102. key: |
  103. a
  104. b
  105. cert: c
  106. '
  107. apache_vhost_create"
  108. is errlvl 0
  109. is err part "## Auto-redirection from http to https"
  110. is err part "\
  111. relation-set protocol:
  112. | https"
  113. is out part 'file_put $CONFIGSTORE/$BASE_SERVICE_NAME/etc/ssl/certs/www.example.com.pem
  114. | c'
  115. is out part 'file_put $CONFIGSTORE/$BASE_SERVICE_NAME/etc/ssl/private/www.example.com.key
  116. | a
  117. | b'
  118. is out reg 'config-add'
  119. is out reg ' - \$CONFIGSTORE/\$BASE_SERVICE_NAME/etc/ssl/certs/www.example.com.pem:/etc/ssl/certs/www.example.com.pem:ro'
  120. is out reg ' - \$CONFIGSTORE/\$BASE_SERVICE_NAME/etc/ssl/private/www.example.com.key:/etc/ssl/private/www.example.com.key:ro'
  121. try "
  122. export DOCKER_BASE_IMAGE=docker/apache
  123. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  124. export CONFIGSTORE='\$CONFIGSTORE'
  125. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  126. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  127. DOMAIN=www.example.com
  128. DOCKER_SITE_PATH=/var/www/\$DOMAIN
  129. export CFG='
  130. creds:
  131. toto: xxx
  132. '
  133. apache_vhost_create"
  134. is errlvl 0
  135. is err part "\
  136. relation-set protocol:
  137. | http"
  138. is out reg "htpasswd -bc '/etc/apache2/sites-enabled/www.example.com.passwd' 'toto' 'xxx'"
  139. is out reg 'docker run -i --entrypoint /bin/bash .* docker/apache'
  140. ##
  141. ## apache_proxy_dir
  142. ##
  143. try "
  144. export DOCKER_BASE_IMAGE=docker/apache
  145. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  146. export CONFIGSTORE='\$CONFIGSTORE'
  147. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  148. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  149. export CFG='
  150. creds:
  151. toto: xxx
  152. '
  153. apache_publish_dir"
  154. is errlvl 1 ## no domain
  155. try "
  156. export DATASTORE='\$DATASTORE'
  157. export DOCKER_BASE_IMAGE=docker/apache
  158. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  159. export CONFIGSTORE='\$CONFIGSTORE'
  160. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  161. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  162. export CFG='
  163. domain: www.example.com
  164. creds:
  165. toto: xxx
  166. '
  167. apache_publish_dir"
  168. is errlvl 0
  169. is err reg 'setfacl -R -m g:<GID>:rx \$DATASTORE/\$BASE_SERVICE_NAME/var/www/www.example.com'
  170. is err reg 'cached_cmd_on_base_image apache id -g www-data'
  171. try "
  172. export DATASTORE='\$DATASTORE'
  173. export DOCKER_BASE_IMAGE=docker/apache
  174. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  175. export CONFIGSTORE='\$CONFIGSTORE'
  176. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  177. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  178. export CFG='
  179. domain: www.example.com
  180. creds:
  181. toto: xxx
  182. data-dirs:
  183. - a
  184. - b
  185. - c
  186. '
  187. apache_publish_dir"
  188. is errlvl 0
  189. is err reg 'setfacl -R -m g:<GID>:rwx \$DATASTORE/\$BASE_SERVICE_NAME/var/www/www.example.com/a \$DATASTORE/\$BASE_SERVICE_NAME/var/www/www.example.com/b \$DATASTORE/\$BASE_SERVICE_NAME/var/www/www.example.com/c'
  190. is err reg 'setfacl -R -d -m g:<GID>:rwx \$DATASTORE/\$BASE_SERVICE_NAME/var/www/www.example.com/a \$DATASTORE/\$BASE_SERVICE_NAME/var/www/www.example.com/b \$DATASTORE/\$BASE_SERVICE_NAME/var/www/www.example.com/c'
  191. try "
  192. export DATASTORE='\$DATASTORE'
  193. export DOCKER_BASE_IMAGE=docker/apache
  194. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  195. export CONFIGSTORE='\$CONFIGSTORE'
  196. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  197. export MASTER_BASE_SERVICE_NAME='\$MASTER_BASE_SERVICE_NAME'
  198. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  199. export CFG='
  200. domain: www.example.com
  201. location: /opt/apps/newlocation
  202. creds:
  203. toto: xxx
  204. data-dirs:
  205. - a
  206. - b
  207. - c
  208. '
  209. apache_publish_dir"
  210. is errlvl 0
  211. is err reg 'mkdir -p /opt/apps/newlocation'
  212. is err reg 'setfacl -R -m g:<GID>:rx /opt/apps/newlocation'
  213. is out part ' | $MASTER_BASE_SERVICE_NAME:
  214. | volumes:
  215. | - /opt/apps/newlocation:/var/www/www.example.com'