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.

315 lines
7.0 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. ##
  103. ## apache_vhost_create
  104. ##
  105. try "
  106. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  107. apache_vhost_create publish_dir '
  108. domain: www.example.com
  109. '"
  110. is errlvl 0
  111. is err part "\
  112. relation-set protocol:
  113. | http"
  114. is out reg '^file_put \$SERVICE_CONFIGSTORE/.*/www.example.com.conf'
  115. try "
  116. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  117. CFG='
  118. domain: www.example.com
  119. ssl: true
  120. '
  121. ADDITION='
  122. apache-custom-rules:
  123. - |
  124. ## Auto-redirection from http to https
  125. RewriteEngine On
  126. RewriteCond %{HTTPS} off
  127. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]'
  128. MERGE_YAML_STR=\"
  129. 6b92a84e9d93e4a1: |
  130. \$(echo \"\$CFG\" | prefix ' ')
  131. \$(echo \"\$ADDITION\" | prefix ' ')
  132. \"
  133. apache_vhost_create publish_dir \"\$CFG\"
  134. "
  135. is errlvl 0
  136. is err part "## Auto-redirection from http to https"
  137. is err part "\
  138. relation-set protocol:
  139. | https"
  140. is out reg '^file_put \$SERVICE_CONFIGSTORE/.*/www.example.com.conf'
  141. try "
  142. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  143. export CONFIGSTORE='\$CONFIGSTORE'
  144. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  145. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  146. CFG='
  147. domain: www.example.com
  148. ssl:
  149. key: |
  150. a
  151. b
  152. cert: c
  153. '
  154. ADDITION='
  155. apache-custom-rules:
  156. - |
  157. ## Auto-redirection from http to https
  158. RewriteEngine On
  159. RewriteCond %{HTTPS} off
  160. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]'
  161. MERGE_YAML_STR=\"
  162. 3b76349cfba9d3f2: |
  163. \$(echo \"\$CFG\" | prefix ' ')
  164. \$(echo \"\$ADDITION\" | prefix ' ')
  165. \"
  166. apache_vhost_create publish_dir \"\$CFG\"
  167. "
  168. is errlvl 0
  169. is err part "## Auto-redirection from http to https"
  170. is err part "\
  171. relation-set protocol:
  172. | https"
  173. is out part 'file_put $CONFIGSTORE/$BASE_SERVICE_NAME/etc/ssl/certs/www.example.com.pem
  174. | c'
  175. is out part 'file_put $CONFIGSTORE/$BASE_SERVICE_NAME/etc/ssl/private/www.example.com.key
  176. | a
  177. | b'
  178. is out reg 'config-add'
  179. is out reg ' - \$CONFIGSTORE/\$BASE_SERVICE_NAME/etc/ssl/certs/www.example.com.pem:/etc/ssl/certs/www.example.com.pem:ro'
  180. is out reg ' - \$CONFIGSTORE/\$BASE_SERVICE_NAME/etc/ssl/private/www.example.com.key:/etc/ssl/private/www.example.com.key:ro'
  181. try "
  182. export DOCKER_BASE_IMAGE=docker/apache
  183. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  184. export CONFIGSTORE='\$CONFIGSTORE'
  185. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  186. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  187. CFG='
  188. domain: www.example.com
  189. creds:
  190. toto: xxx
  191. '
  192. apache_vhost_create publish_dir \"\$CFG\"
  193. "
  194. is errlvl 0
  195. is err part "\
  196. relation-set protocol:
  197. | http"
  198. is out reg "htpasswd -bc '/etc/apache2/sites-enabled/www.example.com.passwd' 'toto' 'xxx'"
  199. is out reg 'docker run -i --entrypoint /bin/bash .* docker/apache'
  200. ##
  201. ## apache_publish_dir
  202. ##
  203. try "
  204. export DOCKER_BASE_IMAGE=docker/apache
  205. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  206. export CONFIGSTORE='\$CONFIGSTORE'
  207. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  208. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  209. apache_publish_dir '
  210. creds:
  211. toto: xxx
  212. '" "missing domain"
  213. is errlvl 1 ## no domain
  214. try "
  215. export DATASTORE='\$DATASTORE'
  216. export DOCKER_BASE_IMAGE=docker/apache
  217. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  218. export CONFIGSTORE='\$CONFIGSTORE'
  219. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  220. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  221. apache_publish_dir '
  222. domain: www.example.com
  223. creds:
  224. toto: xxx
  225. '
  226. "
  227. is errlvl 0
  228. is err reg 'setfacl -R -m g:<GID>:rx \$DATASTORE/\$BASE_SERVICE_NAME/var/www/www.example.com'
  229. is err reg 'cached_cmd_on_base_image apache id -g www-data'
  230. try "
  231. export DATASTORE='\$DATASTORE'
  232. export DOCKER_BASE_IMAGE=docker/apache
  233. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  234. export CONFIGSTORE='\$CONFIGSTORE'
  235. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  236. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  237. apache_publish_dir '
  238. domain: www.example.com
  239. creds:
  240. toto: xxx
  241. data-dirs:
  242. - a
  243. - b
  244. - c
  245. '
  246. "
  247. is errlvl 0
  248. 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'
  249. 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'
  250. try "
  251. export DATASTORE='\$DATASTORE'
  252. export DOCKER_BASE_IMAGE=docker/apache
  253. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  254. export CONFIGSTORE='\$CONFIGSTORE'
  255. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  256. export MASTER_BASE_SERVICE_NAME='\$MASTER_BASE_SERVICE_NAME'
  257. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  258. apache_publish_dir '
  259. domain: www.example.com
  260. location: /opt/apps/newlocation
  261. creds:
  262. toto: xxx
  263. data-dirs:
  264. - a
  265. - b
  266. - c
  267. '
  268. "
  269. is errlvl 0
  270. is err reg 'mkdir -p /opt/apps/newlocation'
  271. is err reg 'setfacl -R -m g:<GID>:rx /opt/apps/newlocation'
  272. is out part ' | $MASTER_BASE_SERVICE_NAME:
  273. | volumes:
  274. | - /opt/apps/newlocation:/var/www/www.example.com'