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.

413 lines
9.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. export RELATION_DATA_FILE=x
  18. relation-set() {
  19. local key="$1" value="$2"
  20. echo "relation-set $key:" >&2
  21. echo "$value" | prefix " | " >&2
  22. }
  23. export -f relation-set
  24. cfg-get-value() {
  25. local key="$1"
  26. shyaml get-value "$key" 2>/dev/null
  27. }
  28. export -f cfg-get-value
  29. get_service_relations() {
  30. printf "%s\0" "${RELATIONS[@]}"
  31. }
  32. export -f get_service_relations
  33. file_put() {
  34. echo "file_put $1"
  35. cat - | prefix " | "
  36. }
  37. export -f file_put
  38. docker() {
  39. echo "docker" "$@"
  40. echo stdin:
  41. cat - | prefix " | "
  42. }
  43. export -f docker
  44. config-add() {
  45. echo "config-add"
  46. echo "$1" | prefix " | "
  47. }
  48. export -f config-add
  49. init-config-add() {
  50. echo "init-config-add"
  51. echo "$1" | prefix " | "
  52. }
  53. export -f init-config-add
  54. mkdir() {
  55. echo "called: $FUNCNAME $@" >&2
  56. }
  57. export -f mkdir
  58. setfacl() {
  59. echo "called: $FUNCNAME $@" >&2
  60. }
  61. export -f setfacl
  62. chgrp() {
  63. echo "called: $FUNCNAME $@" >&2
  64. }
  65. export -f chgrp
  66. chmod() {
  67. echo "called: $FUNCNAME $@" >&2
  68. }
  69. export -f chmod
  70. merge_yaml_str() {
  71. local arg_hash="$(H "$@" | cut -c -16)"
  72. local i
  73. echo "Calling: merge_yaml_str" >&2
  74. ((i=0))
  75. for arg in "$@"; do
  76. echo " arg$((i++)):"
  77. echo "$arg" | prefix " | "
  78. done >&2
  79. echo " H> $arg_hash" >&2
  80. while read-0 h res; do
  81. if [[ "$arg_hash" == "$h" ]]; then
  82. echo "Mock hash matched, returning:" >&2
  83. echo "$res" | prefix " | " >&2
  84. echo "$res"
  85. return 0
  86. fi
  87. done < <(e "$MERGE_YAML_STR" | shyaml key-values-0)
  88. printf "<merge_yaml_str("
  89. printf "'%s', " "$@"
  90. printf ")>"
  91. }
  92. export -f merge_yaml_str
  93. yaml_get_interpret() {
  94. shyaml get-value
  95. }
  96. export -f yaml_get_interpret
  97. yaml_key_val_str() {
  98. printf "%s: %s" "$1" "$2"
  99. }
  100. export -f yaml_key_val_str
  101. cached_cmd_on_base_image() {
  102. echo "called: $FUNCNAME $@" >&2
  103. echo "stdout:" >&2
  104. echo "<GID>" | prefix " | " >&2
  105. echo "<GID>"
  106. }
  107. export -f cached_cmd_on_base_image
  108. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  109. trap "rm -rf \"$state_tmpdir\"" EXIT
  110. ##
  111. ## apache_vhost_create
  112. ##
  113. try "
  114. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  115. apache_vhost_create publish_dir '
  116. domain: www.example.com
  117. '"
  118. is errlvl 0
  119. is err part "\
  120. relation-set url:
  121. | http://www.example.com"
  122. is out reg '^file_put \$SERVICE_CONFIGSTORE/.*/www.example.com.conf'
  123. try "
  124. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  125. CFG='
  126. domain: www.example.com
  127. ssl: true
  128. '
  129. ADDITION='
  130. apache-custom-rules:
  131. - |
  132. ## Auto-redirection from http to https
  133. RewriteEngine On
  134. RewriteCond %{HTTPS} off
  135. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]'
  136. MERGE_YAML_STR=\"
  137. 6b92a84e9d93e4a1: |
  138. \$(echo \"\$CFG\" | prefix ' ')
  139. \$(echo \"\$ADDITION\" | prefix ' ')
  140. \"
  141. apache_vhost_create publish_dir \"\$CFG\"
  142. "
  143. is errlvl 0
  144. is err part "## Auto-redirection from http to https"
  145. is err part "\
  146. relation-set url:
  147. | https://www.example.com"
  148. is out reg '^file_put \$SERVICE_CONFIGSTORE/.*/www.example.com.conf'
  149. try "
  150. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  151. export CONFIGSTORE='\$CONFIGSTORE'
  152. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  153. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  154. CFG='
  155. domain: www.example.com
  156. ssl:
  157. key: |
  158. a
  159. b
  160. cert: c
  161. '
  162. ADDITION='
  163. apache-custom-rules:
  164. - |
  165. ## Auto-redirection from http to https
  166. RewriteEngine On
  167. RewriteCond %{HTTPS} off
  168. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]'
  169. MERGE_YAML_STR=\"
  170. 3b76349cfba9d3f2: |
  171. \$(echo \"\$CFG\" | prefix ' ')
  172. \$(echo \"\$ADDITION\" | prefix ' ')
  173. \"
  174. apache_vhost_create publish_dir \"\$CFG\"
  175. "
  176. is errlvl 0
  177. is err part "## Auto-redirection from http to https"
  178. is err part "\
  179. relation-set url:
  180. | https://www.example.com"
  181. is out part 'file_put $CONFIGSTORE/$BASE_SERVICE_NAME/etc/ssl/certs/www.example.com.pem
  182. | c'
  183. is out part 'file_put $CONFIGSTORE/$BASE_SERVICE_NAME/etc/ssl/private/www.example.com.key
  184. | a
  185. | b'
  186. is out reg 'init-config-add'
  187. is out reg ' - \$CONFIGSTORE/\$BASE_SERVICE_NAME/etc/ssl/certs/www.example.com.pem:/etc/ssl/certs/www.example.com.pem:ro'
  188. is out reg ' - \$CONFIGSTORE/\$BASE_SERVICE_NAME/etc/ssl/private/www.example.com.key:/etc/ssl/private/www.example.com.key:ro'
  189. try "
  190. export DOCKER_BASE_IMAGE=docker/apache
  191. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  192. export CONFIGSTORE='\$CONFIGSTORE'
  193. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  194. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  195. CFG='
  196. domain: www.example.com
  197. creds:
  198. toto: xxx
  199. '
  200. apache_vhost_create publish_dir \"\$CFG\"
  201. "
  202. is errlvl 0
  203. is err part "\
  204. relation-set url:
  205. | http://www.example.com"
  206. is out reg "htpasswd -bc '/etc/apache2/sites-enabled/www.example.com.passwd' 'toto' 'xxx'"
  207. is out reg 'docker run -i --entrypoint /bin/bash .* docker/apache'
  208. ## XXXvlab: we don't need this feature anymore it seems
  209. # try "
  210. # export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  211. # apache_vhost_create publish_dir '' ',http,' '000-default'"
  212. # is errlvl 0
  213. # is err part "\
  214. # relation-set url:
  215. # | http://" ## XXXvlab: this isn't right, is it ?
  216. # is out reg '^file_put \$SERVICE_CONFIGSTORE/.*/000-default.conf'
  217. ##
  218. ## apache_publish_dir
  219. ##
  220. try "
  221. export DOCKER_BASE_IMAGE=docker/apache
  222. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  223. export CONFIGSTORE='\$CONFIGSTORE'
  224. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  225. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  226. apache_publish_dir '
  227. creds:
  228. toto: xxx
  229. '" "missing domain"
  230. is errlvl 1 ## no domain
  231. try "
  232. export DATASTORE='\$DATASTORE'
  233. export DOCKER_BASE_IMAGE=docker/apache
  234. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  235. export CONFIGSTORE='\$CONFIGSTORE'
  236. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  237. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  238. apache_publish_dir '
  239. domain: www.example.com
  240. creds:
  241. toto: xxx
  242. '
  243. "
  244. is errlvl 0
  245. is err reg 'setfacl -R -m g:<GID>:rx \$DATASTORE/\$BASE_SERVICE_NAME/var/www/www.example.com'
  246. is err reg 'cached_cmd_on_base_image apache id -g www-data'
  247. try "
  248. export DATASTORE='\$DATASTORE'
  249. export DOCKER_BASE_IMAGE=docker/apache
  250. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  251. export CONFIGSTORE='\$CONFIGSTORE'
  252. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  253. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  254. apache_publish_dir '
  255. domain: www.example.com
  256. creds:
  257. toto: xxx
  258. data-dirs:
  259. - a
  260. - b
  261. - c
  262. '
  263. "
  264. is errlvl 0
  265. 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'
  266. 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'
  267. try "
  268. export DATASTORE='\$DATASTORE'
  269. export DOCKER_BASE_IMAGE=docker/apache
  270. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  271. export CONFIGSTORE='\$CONFIGSTORE'
  272. export SERVICE_NAME='\$SERVICE_NAME'
  273. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  274. export MASTER_BASE_SERVICE_NAME='\$MASTER_BASE_SERVICE_NAME'
  275. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  276. apache_publish_dir '
  277. domain: www.example.com
  278. location: /opt/apps/newlocation
  279. creds:
  280. toto: xxx
  281. data-dirs:
  282. - a
  283. - b
  284. - c
  285. '
  286. "
  287. is errlvl 0
  288. is err reg 'mkdir -p /opt/apps/newlocation'
  289. is err reg 'setfacl -R -m g:<GID>:rx /opt/apps/newlocation'
  290. is out part '
  291. init-config-add
  292. |
  293. | $SERVICE_NAME:
  294. | volumes:
  295. | - /opt/apps/newlocation:/var/www/www.example.com' RTRIM
  296. try "
  297. export DATASTORE='\$DATASTORE'
  298. export DOCKER_BASE_IMAGE=docker/apache
  299. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  300. export CONFIGSTORE='\$CONFIGSTORE'
  301. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  302. export MASTER_BASE_SERVICE_NAME='\$MASTER_BASE_SERVICE_NAME'
  303. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  304. apache_ssh_tunnel '
  305. domain: www.example.com
  306. creds:
  307. toto: xxx
  308. '
  309. " "ssh tunnel without ssl"
  310. is errlvl 1
  311. is err 'Error: ssl must be valued in ssh-tunnel config.
  312. '
  313. is out ''
  314. try "
  315. export DATASTORE='\$DATASTORE'
  316. export DOCKER_BASE_IMAGE=docker/apache
  317. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  318. export CONFIGSTORE='\$CONFIGSTORE'
  319. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  320. export MASTER_BASE_SERVICE_NAME='\$MASTER_BASE_SERVICE_NAME'
  321. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  322. apache_ssh_tunnel '
  323. ssl: true
  324. creds:
  325. toto: xxx
  326. '
  327. " "ssh tunnel without domain"
  328. is errlvl 1
  329. is err 'Error: domain must be valued in ssh-tunnel config.
  330. '
  331. is out ''
  332. try "
  333. export DATASTORE='\$DATASTORE'
  334. export DOCKER_BASE_IMAGE=docker/apache
  335. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  336. export CONFIGSTORE='\$CONFIGSTORE'
  337. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  338. export MASTER_BASE_SERVICE_NAME='\$MASTER_BASE_SERVICE_NAME'
  339. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  340. apache_ssh_tunnel '
  341. domain: ssh.example.com
  342. ssl:
  343. key: a
  344. ca-cert: b
  345. creds:
  346. toto: xxx
  347. '
  348. " "ssh tunnel"
  349. is errlvl 0
  350. is err reg 'relation-set domain:
  351. | ssh.example.com'
  352. is out reg 'file_put \$SERVICE_CONFIGSTORE/etc/apache2/sites-enabled/000-ssh.example.com.conf'
  353. is out reg 'file_put \$CONFIGSTORE/\$BASE_SERVICE_NAME/etc/ssl/private/ssh.example.com.key'
  354. is out reg 'file_put \$CONFIGSTORE/\$BASE_SERVICE_NAME/etc/ssl/certs/ssh.example.com-ca.pem'
  355. is out reg 'AuthUserFile /etc/apache2/sites-enabled/ssh.example.com.passwd'
  356. is out reg "htpasswd -bc '/etc/apache2/sites-enabled/000-ssh.example.com.passwd' 'toto' 'xxx'"