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.

319 lines
7.1 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. cfg-get-value() {
  24. local key="$1"
  25. shyaml get-value "$key" 2>/dev/null
  26. }
  27. export -f cfg-get-value
  28. get_service_relations() {
  29. printf "%s\0" "${RELATIONS[@]}"
  30. }
  31. export -f get_service_relations
  32. file_put() {
  33. echo "file_put $1"
  34. cat - | prefix " | "
  35. }
  36. export -f file_put
  37. docker() {
  38. echo "docker" "$@"
  39. echo stdin:
  40. cat - | prefix " | "
  41. }
  42. export -f docker
  43. config-add() {
  44. echo "config-add"
  45. echo "$1" | prefix " | "
  46. }
  47. export -f config-add
  48. mkdir() {
  49. echo "called: $FUNCNAME $@" >&2
  50. }
  51. export -f mkdir
  52. setfacl() {
  53. echo "called: $FUNCNAME $@" >&2
  54. }
  55. export -f setfacl
  56. chgrp() {
  57. echo "called: $FUNCNAME $@" >&2
  58. }
  59. export -f chgrp
  60. chmod() {
  61. echo "called: $FUNCNAME $@" >&2
  62. }
  63. export -f chmod
  64. merge_yaml_str() {
  65. local arg_hash="$(H "$@" | cut -c -16)"
  66. local i
  67. echo "Calling: merge_yaml_str" >&2
  68. ((i=0))
  69. for arg in "$@"; do
  70. echo " arg$((i++)):"
  71. echo "$arg" | prefix " | "
  72. done >&2
  73. echo " H> $arg_hash" >&2
  74. while read-0 h res; do
  75. if [[ "$arg_hash" == "$h" ]]; then
  76. echo "Mock hash matched, returning:" >&2
  77. echo "$res" | prefix " | " >&2
  78. echo "$res"
  79. return 0
  80. fi
  81. done < <(e "$MERGE_YAML_STR" | shyaml key-values-0)
  82. printf "<merge_yaml_str("
  83. printf "'%s', " "$@"
  84. printf ")>"
  85. }
  86. export -f merge_yaml_str
  87. yaml_get_interpret() {
  88. shyaml get-value
  89. }
  90. export -f yaml_get_interpret
  91. yaml_key_val_str() {
  92. printf "%s: %s" "$1" "$2"
  93. }
  94. export -f yaml_key_val_str
  95. cached_cmd_on_base_image() {
  96. echo "called: $FUNCNAME $@" >&2
  97. echo "stdout:" >&2
  98. echo "<GID>" | prefix " | " >&2
  99. echo "<GID>"
  100. }
  101. export -f cached_cmd_on_base_image
  102. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  103. trap "rm -rf \"$state_tmpdir\"" EXIT
  104. ##
  105. ## apache_vhost_create
  106. ##
  107. try "
  108. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  109. apache_vhost_create publish_dir '
  110. domain: www.example.com
  111. '"
  112. is errlvl 0
  113. is err part "\
  114. relation-set protocol:
  115. | http"
  116. is out reg '^file_put \$SERVICE_CONFIGSTORE/.*/www.example.com.conf'
  117. try "
  118. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  119. CFG='
  120. domain: www.example.com
  121. ssl: true
  122. '
  123. ADDITION='
  124. apache-custom-rules:
  125. - |
  126. ## Auto-redirection from http to https
  127. RewriteEngine On
  128. RewriteCond %{HTTPS} off
  129. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]'
  130. MERGE_YAML_STR=\"
  131. 6b92a84e9d93e4a1: |
  132. \$(echo \"\$CFG\" | prefix ' ')
  133. \$(echo \"\$ADDITION\" | prefix ' ')
  134. \"
  135. apache_vhost_create publish_dir \"\$CFG\"
  136. "
  137. is errlvl 0
  138. is err part "## Auto-redirection from http to https"
  139. is err part "\
  140. relation-set protocol:
  141. | https"
  142. is out reg '^file_put \$SERVICE_CONFIGSTORE/.*/www.example.com.conf'
  143. try "
  144. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  145. export CONFIGSTORE='\$CONFIGSTORE'
  146. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  147. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  148. CFG='
  149. domain: www.example.com
  150. ssl:
  151. key: |
  152. a
  153. b
  154. cert: c
  155. '
  156. ADDITION='
  157. apache-custom-rules:
  158. - |
  159. ## Auto-redirection from http to https
  160. RewriteEngine On
  161. RewriteCond %{HTTPS} off
  162. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]'
  163. MERGE_YAML_STR=\"
  164. 3b76349cfba9d3f2: |
  165. \$(echo \"\$CFG\" | prefix ' ')
  166. \$(echo \"\$ADDITION\" | prefix ' ')
  167. \"
  168. apache_vhost_create publish_dir \"\$CFG\"
  169. "
  170. is errlvl 0
  171. is err part "## Auto-redirection from http to https"
  172. is err part "\
  173. relation-set protocol:
  174. | https"
  175. is out part 'file_put $CONFIGSTORE/$BASE_SERVICE_NAME/etc/ssl/certs/www.example.com.pem
  176. | c'
  177. is out part 'file_put $CONFIGSTORE/$BASE_SERVICE_NAME/etc/ssl/private/www.example.com.key
  178. | a
  179. | b'
  180. is out reg 'config-add'
  181. is out reg ' - \$CONFIGSTORE/\$BASE_SERVICE_NAME/etc/ssl/certs/www.example.com.pem:/etc/ssl/certs/www.example.com.pem:ro'
  182. is out reg ' - \$CONFIGSTORE/\$BASE_SERVICE_NAME/etc/ssl/private/www.example.com.key:/etc/ssl/private/www.example.com.key:ro'
  183. try "
  184. export DOCKER_BASE_IMAGE=docker/apache
  185. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  186. export CONFIGSTORE='\$CONFIGSTORE'
  187. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  188. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  189. CFG='
  190. domain: www.example.com
  191. creds:
  192. toto: xxx
  193. '
  194. apache_vhost_create publish_dir \"\$CFG\"
  195. "
  196. is errlvl 0
  197. is err part "\
  198. relation-set protocol:
  199. | http"
  200. is out reg "htpasswd -bc '/etc/apache2/sites-enabled/www.example.com.passwd' 'toto' 'xxx'"
  201. is out reg 'docker run -i --entrypoint /bin/bash .* docker/apache'
  202. ##
  203. ## apache_publish_dir
  204. ##
  205. try "
  206. export DOCKER_BASE_IMAGE=docker/apache
  207. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  208. export CONFIGSTORE='\$CONFIGSTORE'
  209. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  210. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  211. apache_publish_dir '
  212. creds:
  213. toto: xxx
  214. '" "missing domain"
  215. is errlvl 1 ## no domain
  216. try "
  217. export DATASTORE='\$DATASTORE'
  218. export DOCKER_BASE_IMAGE=docker/apache
  219. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  220. export CONFIGSTORE='\$CONFIGSTORE'
  221. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  222. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  223. apache_publish_dir '
  224. domain: www.example.com
  225. creds:
  226. toto: xxx
  227. '
  228. "
  229. is errlvl 0
  230. is err reg 'setfacl -R -m g:<GID>:rx \$DATASTORE/\$BASE_SERVICE_NAME/var/www/www.example.com'
  231. is err reg 'cached_cmd_on_base_image apache id -g www-data'
  232. try "
  233. export DATASTORE='\$DATASTORE'
  234. export DOCKER_BASE_IMAGE=docker/apache
  235. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  236. export CONFIGSTORE='\$CONFIGSTORE'
  237. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  238. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  239. apache_publish_dir '
  240. domain: www.example.com
  241. creds:
  242. toto: xxx
  243. data-dirs:
  244. - a
  245. - b
  246. - c
  247. '
  248. "
  249. is errlvl 0
  250. 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'
  251. 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'
  252. try "
  253. export DATASTORE='\$DATASTORE'
  254. export DOCKER_BASE_IMAGE=docker/apache
  255. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  256. export CONFIGSTORE='\$CONFIGSTORE'
  257. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  258. export MASTER_BASE_SERVICE_NAME='\$MASTER_BASE_SERVICE_NAME'
  259. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  260. apache_publish_dir '
  261. domain: www.example.com
  262. location: /opt/apps/newlocation
  263. creds:
  264. toto: xxx
  265. data-dirs:
  266. - a
  267. - b
  268. - c
  269. '
  270. "
  271. is errlvl 0
  272. is err reg 'mkdir -p /opt/apps/newlocation'
  273. is err reg 'setfacl -R -m g:<GID>:rx /opt/apps/newlocation'
  274. is out part ' | $MASTER_BASE_SERVICE_NAME:
  275. | volumes:
  276. | - /opt/apps/newlocation:/var/www/www.example.com'