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.

764 lines
22 KiB

  1. # -*- mode: shell-script -*-
  2. get_domain() {
  3. local cfg="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(H "$SERVICE_NAME" "$MASTER_BASE_SERVICE_NAME" "$@")" \
  4. domain
  5. if [ -e "$cache_file" ]; then
  6. cat "$cache_file"
  7. return 0
  8. fi
  9. domain=$(e "$cfg" | cfg-get-value domain 2>/dev/null) || true
  10. if [ "$domain" ]; then
  11. echo "$domain" | tee "$cache_file"
  12. elif [[ "$BASE_SERVICE_NAME" =~ ^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$ ]]; then
  13. echo "$BASE_SERVICE_NAME" | tee "$cache_file"
  14. else
  15. err "You must specify a ${WHITE}domain$NORMAL option in relation. (${FUNCNAME[@]})"
  16. return 1
  17. fi
  18. }
  19. ##
  20. ## Master entrypoints
  21. ##
  22. apache_proxy_dir() {
  23. local cfg="$1" domain
  24. apache_vhost_create web_proxy "$cfg" || return 1
  25. }
  26. export -f apache_proxy_dir
  27. apache_publish_dir() {
  28. local cfg="$1" domain
  29. apache_vhost_create publish_dir "$cfg" || return 1
  30. apache_code_dir "$cfg" || return 1
  31. apache_data_dirs "$cfg"
  32. }
  33. export -f apache_publish_dir
  34. ##
  35. ## Simple functions
  36. ##
  37. apache_vhost_create() {
  38. local type="$1" cfg="$2" custom_rules vhost_statement creds
  39. export APACHE_CONFIG_LOCATION="$SERVICE_CONFIGSTORE/etc/apache2/sites-enabled"
  40. protocols=$(__vhost_cfg_normalize_protocol "$cfg") || return 1
  41. if is_protocol_enabled https "$protocols"; then
  42. read-0 ssl_plugin_fun ssl_cfg_value ssl_cfg_options < <(ssl_get_plugin_fun "$cfg") || return 1
  43. "$ssl_plugin_fun"_vars "$cfg" "$ssl_cfg_options" "$ssl_cfg_value" || return 1
  44. redirect=$(e "$cfg" | cfg-get-value 'redirect-to-ssl' 2>/dev/null) || true
  45. if is_protocol_enabled http "$protocols"; then
  46. redirect=${redirect:-true}
  47. else
  48. redirect=false
  49. fi
  50. if [ "$redirect" == "true" ]; then
  51. custom_rules=$(_get_custom_rules "$cfg") || return 1
  52. if [[ "$custom_rules" != *"## Auto-redirection from http to https"* ]]; then
  53. redirect_rule="- |
  54. ## Auto-redirection from http to https
  55. RewriteEngine On
  56. RewriteCond %{HTTPS} off
  57. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  58. "
  59. relation-set apache-custom-rules "$redirect_rule
  60. $(if [ "$custom_rules" ]; then
  61. echo "- |"$'\n'"$(echo "$custom_rules" | prefix " ")"
  62. fi)"
  63. cfg=$(merge_yaml_str "$cfg" "$(yaml_key_val_str "apache-custom-rules" "$redirect_rule
  64. $(if [ "$custom_rules" ]; then
  65. echo "- |"$'\n'"$(echo "$custom_rules" | prefix " ")"
  66. fi)")")
  67. fi
  68. relation-set protocol https
  69. else
  70. ## Both services are available and different, don't do anything then ?
  71. #relation-set protocol https
  72. :
  73. fi
  74. else
  75. relation-set protocol http
  76. fi
  77. vhost_statement=$(apache_vhost_statement "$type" "$protocols" "$cfg") || {
  78. err "Failed to get vhost statement for type $type on ${protocols:1:-1}"
  79. return 1
  80. }
  81. domain=$(get_domain "$cfg") || return 1
  82. relation-set domain "$domain"
  83. echo "$vhost_statement" | file_put "$APACHE_CONFIG_LOCATION/$domain.conf" || return 1
  84. creds=$(e "$cfg" | cfg-get-value creds 2>/dev/null) || true
  85. if [ "$creds" ]; then
  86. apache_passwd_file "$cfg" || return 1
  87. fi
  88. if is_protocol_enabled https "$protocols"; then
  89. "$ssl_plugin_fun"_prepare "$cfg" "$ssl_cfg_options" "$ssl_cfg_value" || return 1
  90. fi
  91. }
  92. is_protocol_enabled() {
  93. local protocol="$1" protocols="$2"
  94. [[ "$protocols" == *",$protocol,"* ]]
  95. }
  96. export -f is_protocol_enabled
  97. _get_ssl_option_value() {
  98. local cfg="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(H "$SERVICE_NAME" "$MASTER_BASE_SERVICE_NAME" "$@")" \
  99. target_relation rn ts rc td
  100. if [ -e "$cache_file" ]; then
  101. cat "$cache_file"
  102. return 0
  103. fi
  104. if ssl_cfg=$(e "$cfg" | cfg-get-value ssl 2>/dev/null); then
  105. if [[ "$ssl_cfg" =~ ^False|None$ ]]; then
  106. ssl_cfg=""
  107. fi
  108. echo "$ssl_cfg" | tee "$cache_file"
  109. return 0
  110. fi
  111. target_relation="cert-provider"
  112. while read-0 rn ts rc td; do
  113. [ "$rn" == "${target_relation}" ] || continue
  114. info "A cert-provider '$ts' declared as 'ssl' value"
  115. echo "$ts" | tee "$cache_file"
  116. return 0
  117. done < <(get_service_relations "$SERVICE_NAME")
  118. return 1
  119. }
  120. __vhost_cfg_normalize_protocol() {
  121. local cfg="$1" protocol ssl
  122. ## XXXvlab: can't cache if libcharm is not able to give me some checksums
  123. ## indeed, ``_get_ssl_option_value`` depends on relations calculations...
  124. # local cfg="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(p0 "$@" | md5_compat)" \
  125. # protocol
  126. # if [ -e "$cache_file" ]; then
  127. # #debug "$FUNCNAME: STATIC cache hit $1"
  128. # cat "$cache_file" &&
  129. # touch "$cache_file" || return 1
  130. # return 0
  131. # fi
  132. if protocol=$(e "$cfg" | cfg-get-value protocol 2>/dev/null); then
  133. protocol=${protocol:-auto}
  134. else
  135. protocol=auto
  136. fi
  137. case "$protocol" in
  138. auto)
  139. ssl=$(_get_ssl_option_value "$cfg" 2>/dev/null)
  140. if [ "$ssl" ] ; then
  141. protocol="http,https"
  142. else
  143. protocol="http"
  144. fi
  145. ;;
  146. both)
  147. protocol="http,https"
  148. ;;
  149. ssl|https)
  150. protocol="https"
  151. ;;
  152. http)
  153. protocol="http"
  154. ;;
  155. *)
  156. err "Invalid value '$protocol' for ${WHITE}protocol$NORMAL option (use one of: http, https, both, auto)."
  157. return 1
  158. esac
  159. echo -n ",$protocol,"
  160. #| tee "$cache_file"
  161. }
  162. ## ssl_plugin_* and ssl_fallback should :
  163. ## - do anything to ensure that
  164. ## - issue config-add to add volumes if necessary
  165. ## - output 3 vars of where to find the 3 files from within the docker apache
  166. ssl_get_plugin_fun() {
  167. # from ssl conf, return the function that should manage SSL code creation
  168. local master_cfg="$1" cfg type keys
  169. cfg=$(_get_ssl_option_value "$master_cfg")
  170. [ "$cfg" ] || return 0
  171. type="$(echo "$cfg" | shyaml -y get-type 2>/dev/null)" || return 1
  172. if [[ "$type" == "bool" ]]; then
  173. printf "%s\0" "ssl_fallback" "" "$cfg"
  174. return 0
  175. fi
  176. if ! [[ "$type" == "str" || "$type" == "struct" ]]; then
  177. err "Invalid ${WHITE}ssl${NORMAL} value type '$type': please provide a string or a struct."
  178. return 1
  179. fi
  180. if [ -z "$NO_CERT_PROVIDER" ]; then
  181. if [[ "$type" == "str" ]]; then
  182. keys=("$cfg")
  183. else
  184. keys=($(echo "$cfg" | shyaml keys 2>/dev/null))
  185. fi
  186. for key in "${keys[@]}"; do
  187. target_relation="cert-provider"
  188. fun="ssl_plugin_${target_relation}"
  189. while read-0 relation_name target_service relation_config tech_dep; do
  190. [ "$relation_name" == "${target_relation}" ] || continue
  191. [ "$target_service" == "$key" ] || continue
  192. verb "Corresponding plugin ${DARKGREEN}found${NORMAL}" \
  193. "in ${DARKBLUE}$relation_name${NORMAL}/${DARKYELLOW}$key${NORMAL}"
  194. ssl_cfg=$(printf "%s" "$cfg" | shyaml get-value "$key" 2>/dev/null) || true
  195. merged_config=$(merge_yaml_str "$relation_config" "$ssl_cfg") || return 1
  196. printf "%s\0" "$fun" "$key" "$merged_config"
  197. return 0
  198. done < <(get_service_relations "$SERVICE_NAME") || return 1
  199. case "$key" in
  200. cert|ca-cert|key)
  201. :
  202. ;;
  203. *)
  204. err "Invalid key '$key' in ${WHITE}ssl${NORMAL}:" \
  205. "no corresponding services declared in ${DARKBLUE}${target_relation}$NORMAL"
  206. return 1
  207. ;;
  208. esac
  209. done
  210. fi
  211. ## No key of the struct seem to be declared cert-provider, so fallback
  212. printf "%s\0" "ssl_fallback" "" "$cfg"
  213. }
  214. ssl_fallback_vars() {
  215. local cfg="$1" ssl_cfg="$2" cert key ca_cert domain
  216. domain=$(get_domain "$cfg") || return 1
  217. if __vhost_cfg_ssl_cert=$(echo "$ssl_cfg" | shyaml get-value cert 2>/dev/null); then
  218. __vhost_cfg_SSL_CERT_LOCATION=/etc/ssl/certs/${domain}.pem
  219. fi
  220. if __vhost_cfg_ssl_key=$(echo "$ssl_cfg" | shyaml get-value key 2>/dev/null); then
  221. __vhost_cfg_SSL_KEY_LOCATION=/etc/ssl/private/${domain}.key
  222. fi
  223. if __vhost_cfg_ssl_ca_cert=$(echo "$ssl_cfg" | shyaml get-value ca-cert 2>/dev/null); then
  224. __vhost_cfg_SSL_CA_CERT_LOCATION=/etc/ssl/certs/${domain}-ca.pem
  225. fi
  226. }
  227. ssl_fallback_prepare() {
  228. local cfg="$1" cert key ca_cert
  229. dst="$CONFIGSTORE/$BASE_SERVICE_NAME"
  230. volumes=""
  231. for label in cert key ca_cert; do
  232. content="$(eval echo "\"\$__vhost_cfg_ssl_$label\"")"
  233. if [ "$content" ]; then
  234. location="$(eval echo "\$__vhost_cfg_SSL_${label^^}_LOCATION")"
  235. echo "$content" | file_put "$dst$location"
  236. config_hash=$(printf "%s\0" "$config_hash" "$label" "$content" | md5_compat)
  237. volumes="$volumes
  238. - $dst$location:$location:ro"
  239. fi
  240. done
  241. if [ "$volumes" ]; then
  242. config-add "\
  243. services:
  244. $MASTER_TARGET_SERVICE_NAME:
  245. volumes:
  246. $volumes
  247. "
  248. fi
  249. }
  250. ssl_plugin_cert-provider_vars() {
  251. local cfg="$1" ssl_cfg="$2"
  252. domain=$(get_domain "$cfg") || return 1
  253. __vhost_cfg_SSL_CERT_LOCATION=/etc/letsencrypt/live/${domain}/cert.pem
  254. __vhost_cfg_SSL_KEY_LOCATION=/etc/letsencrypt/live/${domain}/privkey.pem
  255. __vhost_cfg_SSL_CHAIN=/etc/letsencrypt/live/${domain}/chain.pem
  256. }
  257. ssl_plugin_cert-provider_prepare() {
  258. local cfg="$1" ssl_cfg="$2" service="$3" options domain server_aliases
  259. domain=$(get_domain "$cfg") || return 1
  260. options=$(yaml_key_val_str "options" "$ssl_cfg") || return 1
  261. service_config=$(yaml_key_val_str "$service" "$options")
  262. server_aliases=$(e "$cfg" | cfg-get-value server-aliases 2>/dev/null) || true
  263. [ "$server_aliases" == None ] && server_aliases=""
  264. if [ "$server_aliases" ]; then
  265. server_aliases=($(echo "$server_aliases" | shyaml get-values)) || return 1
  266. else
  267. server_aliases=()
  268. fi
  269. compose --debug --add-compose-content "$service_config" run --rm --service-ports "$service" \
  270. crt create "$domain" "${server_aliases[@]}" || {
  271. err "Failed to launch letsencrypt for certificate creation."
  272. return 1
  273. }
  274. config-add "\
  275. services:
  276. $MASTER_TARGET_SERVICE_NAME:
  277. volumes:
  278. - $DATASTORE/$service/etc/letsencrypt:/etc/letsencrypt:ro
  279. " || return 1
  280. }
  281. apache_passwd_file() {
  282. local cfg="$1" creds
  283. include parse || true
  284. ## XXXvlab: called twice... no better way to do this ?
  285. creds=$(e "$cfg" | cfg-get-value creds 2>/dev/null) || true
  286. password_path=$(password-path-get "$cfg")
  287. first=
  288. if ! [ -e "$CONFIGSTORE/$MASTER_TARGET_SERVICE_NAME$password_path" ]; then
  289. debug "No file $CONFIGSTORE/$MASTER_TARGET_SERVICE_NAME$password_path, creating password file." || true
  290. first=c
  291. fi
  292. while read-0 login password; do
  293. debug "htpasswd -b$first '${password_path}' '$login' '$password'"
  294. echo "htpasswd -b$first '${password_path}' '$login' '$password'"
  295. if [ "$first" ]; then
  296. first=
  297. fi
  298. done < <(e "$creds" | shyaml key-values-0 2>/dev/null) |
  299. docker run -i --entrypoint "/bin/bash" \
  300. -v "$APACHE_CONFIG_LOCATION:/etc/apache2/sites-enabled" \
  301. "$DOCKER_BASE_IMAGE" || return 1
  302. }
  303. ## Produce the full statements depending on relation-get informations
  304. apache_vhost_statement() {
  305. local type="$1" protocols="$2" cfg="$3" \
  306. vhost_statement
  307. if is_protocol_enabled http "$protocols"; then
  308. __vhost_full_vhost_statement "$type" http "$cfg" || return 1
  309. fi
  310. if is_protocol_enabled https "$protocols"; then
  311. read-0 ssl_plugin_fun ssl_cfg_value ssl_cfg_options < <(ssl_get_plugin_fun "$cfg") || return 1
  312. "$ssl_plugin_fun"_vars "$cfg" "$ssl_cfg_options" "$ssl_cfg_value" || return 1
  313. vhost_statement=$(__vhost_full_vhost_statement "$type" https "$cfg") || return 1
  314. cat <<EOF
  315. <IfModule mod_ssl.c>
  316. $(echo "$vhost_statement" | prefix " ")
  317. </IfModule>
  318. EOF
  319. fi
  320. }
  321. export -f apache_vhost_statement
  322. apache_code_dir() {
  323. local cfg="$1" www_data_gid local_path
  324. www_data_gid=$(cached_cmd_on_base_image apache 'id -g www-data') || {
  325. debug "Failed to query for www-data gid in ${DARKYELLOW}apache${NORMAL} base image."
  326. return 1
  327. }
  328. domain=$(get_domain "$cfg") || return 1
  329. local_path="/var/www/${domain}"
  330. host_path=$(e "$cfg" | cfg-get-value location 2>/dev/null) ||
  331. host_path="$DATASTORE/$BASE_SERVICE_NAME${local_path}"
  332. mkdir -p "$host_path" || return 1
  333. setfacl -R -m g:"$www_data_gid":rx "$host_path"
  334. info "Set permission for read and traversal on '$host_path'."
  335. config-add "
  336. $MASTER_BASE_SERVICE_NAME:
  337. volumes:
  338. - $host_path:$local_path
  339. "
  340. }
  341. apache_data_dirs() {
  342. local cfg="$1" data_dirs dst data dirs
  343. data_dirs=$(e "$cfg" | cfg-get-value data-dirs 2>/dev/null | shyaml get-values 2>/dev/null) || true
  344. if [ -z "$data_dirs" ]; then
  345. return 0
  346. fi
  347. domain=$(get_domain "$cfg") || return 1
  348. local_path="/var/www/${domain}"
  349. dst=$DATASTORE/$BASE_SERVICE_NAME$local_path
  350. data=()
  351. while IFS="," read -ra addr; do
  352. for dir in "${addr[@]}"; do
  353. data+=($dir)
  354. done
  355. done <<< "$data_dirs"
  356. www_data_gid=$(cached_cmd_on_base_image apache 'id -g www-data') || {
  357. debug "Failed to query for www-data gid in ${DARKYELLOW}apache${NORMAL} base image."
  358. return 1
  359. }
  360. info "www-data gid from ${DARKYELLOW}apache${NORMAL} is '$www_data_gid'"
  361. dirs=()
  362. for d in "${data[@]}"; do
  363. dirs+=("$dst/$d")
  364. done
  365. mkdir -p "${dirs[@]}"
  366. setfacl -R -m g:"$www_data_gid":rwx "${dirs[@]}"
  367. setfacl -R -d -m g:"$www_data_gid":rwx "${dirs[@]}"
  368. config-add "
  369. $MASTER_BASE_SERVICE_NAME:
  370. volumes:
  371. $(
  372. for d in "${data[@]}"; do
  373. echo " - $dst/$d:$local_path/$d"
  374. done
  375. )"
  376. }
  377. deploy_files() {
  378. local src="$1" dst="$2"
  379. if ! [ -d "$dst" ]; then
  380. err "Destination '$dst' does not exist or is not a directory"
  381. return 1
  382. fi
  383. (
  384. cd "$dst" && info "In $dst:" &&
  385. get_file "$src" | tar xv
  386. )
  387. }
  388. export -f deploy_files
  389. apache_core_rules_add() {
  390. local conf="$1" dst="/etc/apache2/conf-enabled/$BASE_SERVICE_NAME.conf"
  391. debug "Adding core rule."
  392. echo "$conf" | file_put "$CONFIGSTORE/$BASE_SERVICE_NAME$dst"
  393. config_hash=$(printf "%s\0" "$config_hash" "$conf" | md5_compat)
  394. config-add "
  395. $MASTER_BASE_SERVICE_NAME:
  396. volumes:
  397. - $CONFIGSTORE/$BASE_SERVICE_NAME$dst:$dst:ro
  398. "
  399. }
  400. __vhost_ssl_statement() {
  401. ## defaults
  402. __vhost_cfg_SSL_CERT_LOCATION=${__vhost_cfg_SSL_CERT_LOCATION:-/etc/ssl/certs/ssl-cert-snakeoil.pem}
  403. __vhost_cfg_SSL_KEY_LOCATION=${__vhost_cfg_SSL_KEY_LOCATION:-/etc/ssl/private/ssl-cert-snakeoil.key}
  404. cat <<EOF
  405. ##
  406. ## SSL Configuration
  407. ##
  408. SSLEngine On
  409. SSLCertificateFile $__vhost_cfg_SSL_CERT_LOCATION
  410. SSLCertificateKeyFile $__vhost_cfg_SSL_KEY_LOCATION
  411. $([ -z "$__vhost_cfg_SSL_CA_CERT_LOCATION" ] || echo "SSLCACertificateFile $__vhost_cfg_SSL_CA_CERT_LOCATION")
  412. $([ -z "$__vhost_cfg_SSL_CHAIN" ] || echo "SSLCertificateChainFile $__vhost_cfg_SSL_CHAIN")
  413. SSLVerifyClient None
  414. EOF
  415. }
  416. password-path-get() {
  417. local cfg="$1" domain
  418. domain=$(get_domain "$cfg") || return 1
  419. echo /etc/apache2/sites-enabled/${domain}.passwd
  420. }
  421. __vhost_creds_statement() {
  422. local cfg="$1" password_path
  423. password_path=$(password-path-get "$cfg") || return 1
  424. if ! e "$cfg" | cfg-get-value creds >/dev/null 2>&1; then
  425. echo "Allow from all"
  426. return 0
  427. fi
  428. cat <<EOF
  429. AuthType basic
  430. AuthName "private"
  431. AuthUserFile ${password_path}
  432. Require valid-user
  433. EOF
  434. }
  435. __vhost_head_statement() {
  436. local cfg="$1" protocol="$2" server_aliases admin_mail
  437. domain=$(get_domain "$cfg") || return 1
  438. admin_mail=$(e "$1" | cfg-get-value "admin-mail" 2>/dev/null) || true
  439. server_aliases=$(e "$cfg" | cfg-get-value server-aliases 2>/dev/null) || true
  440. [ "$server_aliases" == None ] && server_aliases=""
  441. if [ "$server_aliases" ]; then
  442. server_aliases=($(e "$server_aliases" | shyaml get-values)) || return 1
  443. if [ -z "$domain" ]; then
  444. err "You can't specify server aliases if you don't have a domain."
  445. return 1
  446. fi
  447. else
  448. server_aliases=()
  449. fi
  450. if [ "$protocol" == "https" ]; then
  451. prefix="s-"
  452. else
  453. prefix=
  454. fi
  455. cat <<EOF
  456. ServerAdmin ${admin_mail:-contact@$domain}
  457. ServerName ${domain}
  458. $(
  459. for alias in "${server_aliases[@]}"; do
  460. [ "$alias" ] || continue
  461. echo "ServerAlias $alias"
  462. done
  463. )
  464. ServerSignature Off
  465. CustomLog /var/log/apache2/${prefix}${domain}_access.log combined
  466. ErrorLog /var/log/apache2/${prefix}${domain}_error.log
  467. ErrorLog syslog:local2
  468. EOF
  469. }
  470. _get_custom_rules() {
  471. local cfg="$1" custom_rules type elt value first
  472. custom_rules=$(e "$cfg" | cfg-get-value apache-custom-rules 2>/dev/null) || true
  473. if [ -z "$custom_rules" ]; then
  474. return 0
  475. fi
  476. type=$(echo "$custom_rules" | shyaml get-type)
  477. value=
  478. case "$type" in
  479. "sequence")
  480. first=1
  481. while read-0 elt; do
  482. elt="$(echo "$elt" | yaml_get_interpret)" || return 1
  483. [ "$elt" ] || continue
  484. if [ "$first" ]; then
  485. first=
  486. else
  487. value+=$'\n'$'\n'
  488. fi
  489. first=
  490. value+="$elt"
  491. done < <(echo "$custom_rules" | shyaml -y get-values-0)
  492. ;;
  493. "struct")
  494. while read-0 _key val; do
  495. value+=$'\n'"$(echo "$val" | yaml_get_interpret)" || return 1
  496. done < <(echo "$custom_rules" | shyaml -y key-values-0)
  497. ;;
  498. "str")
  499. value+=$(echo "$custom_rules")
  500. ;;
  501. *)
  502. value+=$(echo "$custom_rules")
  503. ;;
  504. esac
  505. printf "%s" "$value"
  506. }
  507. __vhost_custom_rules() {
  508. local cfg="$1" custom_rules
  509. custom_rules=$(_get_custom_rules "$cfg") || return 1
  510. if [ "$custom_rules" ]; then
  511. cat <<EOF
  512. ##
  513. ## Custom rules
  514. ##
  515. $custom_rules
  516. EOF
  517. fi
  518. }
  519. __vhost_content_statement() {
  520. local type="$1"
  521. shift
  522. case "$type" in
  523. "web_proxy")
  524. __vhost_proxy_statement "$@" || return 1
  525. ;;
  526. "publish_dir")
  527. __vhost_publish_dir_statement "$@" || return 1
  528. ;;
  529. esac
  530. }
  531. target-get() {
  532. local cfg="$1" target first_exposed_port base_image
  533. target=$(e "$cfg" | cfg-get-value target 2>/dev/null) || true
  534. if [ -z "$target" ]; then
  535. ## First exposed port:
  536. base_image=$(service_base_docker_image "$BASE_SERVICE_NAME") || return 1
  537. if ! docker_has_image "$base_image"; then
  538. docker pull "$base_image" >&2
  539. fi
  540. first_exposed_port=$(image_exposed_ports_0 "$base_image" | tr '\0' '\n' | head -n 1 | cut -f 1 -d /) || return 1
  541. if [ -z "$first_exposed_port" ]; then
  542. err "Failed to get first exposed port of image '$base_image'."
  543. return 1
  544. fi
  545. target=$MASTER_BASE_SERVICE_NAME:$first_exposed_port
  546. info "No target was specified, introspection found: $target"
  547. fi
  548. echo "$target"
  549. }
  550. __vhost_proxy_statement() {
  551. local protocol="$1" cfg="$2"
  552. target=$(target-get "$cfg") || return 1
  553. cat <<EOF
  554. ##
  555. ## Proxy declaration towards $target
  556. ##
  557. <IfModule mod_proxy.c>
  558. ProxyRequests Off
  559. <Proxy *>
  560. Order deny,allow
  561. Allow from all
  562. </Proxy>
  563. ProxyVia On
  564. ProxyPass / http://$target/ retry=0
  565. <Location / >
  566. $(__vhost_creds_statement "$cfg" | prefix " ")
  567. ProxyPassReverse /
  568. </Location>
  569. $([ "$protocol" == "https" ] && echo " SSLProxyEngine On")
  570. </IfModule>
  571. RequestHeader set "X-Forwarded-Proto" "$protocol"
  572. ## Fix IE problem (httpapache proxy dav error 408/409)
  573. SetEnv proxy-nokeepalive 1
  574. EOF
  575. }
  576. __vhost_full_vhost_statement() {
  577. local type="$1" protocol="$2" cfg="$3" head_statement custom_rules content_statement
  578. head_statement=$(__vhost_head_statement "$cfg" "$protocol") || return 1
  579. custom_rules=$(__vhost_custom_rules "$cfg") || return 1
  580. content_statement=$(__vhost_content_statement "$type" "$protocol" "$cfg") || return 1
  581. case "$protocol" in
  582. https)
  583. PORT=443
  584. ;;
  585. http)
  586. PORT=80
  587. ;;
  588. esac
  589. cat <<EOF
  590. <VirtualHost *:$PORT>
  591. $(echo "$head_statement" | prefix " ")
  592. $(echo "$custom_rules" | prefix " ")
  593. $(echo "$content_statement" | prefix " ")
  594. ## Forbid any cache, this is only usefull on dev server.
  595. #Header set Cache-Control "no-cache"
  596. #Header set Access-Control-Allow-Origin "*"
  597. #Header set Access-Control-Allow-Methods "POST, GET, OPTIONS"
  598. #Header set Access-Control-Allow-Headers "origin, content-type, accept"
  599. $([ "$protocol" == "https" ] && __vhost_ssl_statement | prefix " " && echo )
  600. </VirtualHost>
  601. EOF
  602. }
  603. __vhost_publish_dir_statement() {
  604. local protocol="$1" cfg="$2"
  605. domain=$(get_domain "$cfg") || return 1
  606. local_path="/var/www/${domain}"
  607. cat <<EOF
  608. ##
  609. ## Publish directory $local_path
  610. ##
  611. DocumentRoot $local_path
  612. <Directory />
  613. Options FollowSymLinks
  614. AllowOverride None
  615. </Directory>
  616. <Directory $local_path>
  617. Options Indexes FollowSymLinks MultiViews
  618. AllowOverride all
  619. $(__vhost_creds_statement "$cfg" | prefix " ")
  620. </Directory>
  621. EOF
  622. }
  623. apache_config_hash() {
  624. debug "Adding config hash to enable recreating upon config change."
  625. config_hash=$({
  626. printf "%s\0" "$config_hash"
  627. find "$SERVICE_CONFIGSTORE/etc/apache2/sites-enabled" \
  628. -name \*.conf -exec md5sum {} \;
  629. } | md5_compat) || exit 1
  630. init-config-add "
  631. $MASTER_BASE_SERVICE_NAME:
  632. labels:
  633. - compose.config_hash=$config_hash
  634. "
  635. }