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.

765 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/$prefix$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
  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. if _get_ssl_option_value "$cfg" >/dev/null 2>&1; then
  140. protocol="http,https"
  141. else
  142. protocol="http"
  143. fi
  144. ;;
  145. both)
  146. protocol="https,http"
  147. ;;
  148. ssl|https)
  149. protocol="https"
  150. ;;
  151. http)
  152. protocol="http"
  153. ;;
  154. *)
  155. err "Invalid value '$protocol' for ${WHITE}protocol$NORMAL option (use one of: http, https, both, auto)."
  156. return 1
  157. esac
  158. echo ",$protocol,"
  159. #| tee "$cache_file"
  160. }
  161. ## ssl_plugin_* and ssl_fallback should :
  162. ## - do anything to ensure that
  163. ## - issue config-add to add volumes if necessary
  164. ## - output 3 vars of where to find the 3 files from within the docker apache
  165. ssl_get_plugin_fun() {
  166. # from ssl conf, return the function that should manage SSL code creation
  167. local master_cfg="$1" cfg type keys
  168. cfg=$(_get_ssl_option_value "$master_cfg")
  169. [ "$cfg" ] || return 0
  170. type="$(echo "$cfg" | shyaml -y get-type 2>/dev/null)" || return 1
  171. if [[ "$type" == "bool" ]]; then
  172. printf "%s\0" "ssl_fallback" "" "$cfg"
  173. echo ssl_fallback
  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. echo ssl_fallback
  214. }
  215. ssl_fallback_vars() {
  216. local cfg="$1" ssl_cfg="$2" cert key ca_cert domain
  217. domain=$(get_domain "$cfg") || return 1
  218. if __vhost_cfg_ssl_cert=$(echo "$ssl_cfg" | shyaml get-value cert 2>/dev/null); then
  219. __vhost_cfg_SSL_CERT_LOCATION=/etc/ssl/certs/${domain}.pem
  220. fi
  221. if __vhost_cfg_ssl_key=$(echo "$ssl_cfg" | shyaml get-value key 2>/dev/null); then
  222. __vhost_cfg_SSL_KEY_LOCATION=/etc/ssl/private/${domain}.key
  223. fi
  224. if __vhost_cfg_ssl_ca_cert=$(echo "$ssl_cfg" | shyaml get-value ca-cert 2>/dev/null); then
  225. __vhost_cfg_SSL_CA_CERT_LOCATION=/etc/ssl/certs/${domain}-ca.pem
  226. fi
  227. }
  228. ssl_fallback_prepare() {
  229. local cfg="$1" cert key ca_cert
  230. dst="$CONFIGSTORE/$BASE_SERVICE_NAME"
  231. volumes=""
  232. for label in cert key ca_cert; do
  233. content="$(eval echo "\"\$__vhost_cfg_ssl_$label\"")"
  234. if [ "$content" ]; then
  235. location="$(eval echo "\$__vhost_cfg_SSL_${label^^}_LOCATION")"
  236. echo "$content" | file_put "$dst$location"
  237. config_hash=$(printf "%s\0" "$config_hash" "$label" "$content" | md5_compat)
  238. volumes="$volumes
  239. - $dst$location:$location:ro"
  240. fi
  241. done
  242. if [ "$volumes" ]; then
  243. config-add "\
  244. services:
  245. $MASTER_TARGET_SERVICE_NAME:
  246. volumes:
  247. $volumes
  248. "
  249. fi
  250. }
  251. ssl_plugin_cert-provider_vars() {
  252. local cfg="$1" ssl_cfg="$2"
  253. domain=$(get_domain "$cfg") || return 1
  254. __vhost_cfg_SSL_CERT_LOCATION=/etc/letsencrypt/live/${domain}/cert.pem
  255. __vhost_cfg_SSL_KEY_LOCATION=/etc/letsencrypt/live/${domain}/privkey.pem
  256. __vhost_cfg_SSL_CHAIN=/etc/letsencrypt/live/${domain}/chain.pem
  257. }
  258. ssl_plugin_cert-provider_prepare() {
  259. local cfg="$1" ssl_cfg="$2" service="$3" options domain server_aliases
  260. domain=$(get_domain "$cfg") || return 1
  261. options=$(yaml_key_val_str "options" "$ssl_cfg") || return 1
  262. service_config=$(yaml_key_val_str "$service" "$options")
  263. server_aliases=$(e "$cfg" | cfg-get-value server-aliases 2>/dev/null) || true
  264. [ "$server_aliases" == None ] && server_aliases=""
  265. if [ "$server_aliases" ]; then
  266. server_aliases=($(echo "$server_aliases" | shyaml get-values)) || return 1
  267. else
  268. server_aliases=()
  269. fi
  270. compose --debug --add-compose-content "$service_config" run --rm --service-ports "$service" \
  271. crt create "$domain" "${server_aliases[@]}" || {
  272. err "Failed to launch letsencrypt for certificate creation."
  273. return 1
  274. }
  275. config-add "\
  276. services:
  277. $MASTER_TARGET_SERVICE_NAME:
  278. volumes:
  279. - $DATASTORE/$service/etc/letsencrypt:/etc/letsencrypt:ro
  280. " || return 1
  281. }
  282. apache_passwd_file() {
  283. local cfg="$1" creds
  284. include parse || true
  285. ## XXXvlab: called twice... no better way to do this ?
  286. creds=$(e "$cfg" | cfg-get-value creds 2>/dev/null) || true
  287. password_path=$(password-path-get "$cfg")
  288. first=
  289. if ! [ -e "$CONFIGSTORE/$MASTER_TARGET_SERVICE_NAME$password_path" ]; then
  290. debug "No file $CONFIGSTORE/$MASTER_TARGET_SERVICE_NAME$password_path, creating password file." || true
  291. first=c
  292. fi
  293. while read-0 login password; do
  294. debug "htpasswd -b$first '${password_path}' '$login' '$password'"
  295. echo "htpasswd -b$first '${password_path}' '$login' '$password'"
  296. if [ "$first" ]; then
  297. first=
  298. fi
  299. done < <(e "$creds" | shyaml key-values-0 2>/dev/null) |
  300. docker run -i --entrypoint "/bin/bash" \
  301. -v "$APACHE_CONFIG_LOCATION:/etc/apache2/sites-enabled" \
  302. "$DOCKER_BASE_IMAGE" || return 1
  303. }
  304. ## Produce the full statements depending on relation-get informations
  305. apache_vhost_statement() {
  306. local type="$1" protocols="$2" cfg="$3" \
  307. vhost_statement
  308. if is_protocol_enabled http "$protocols"; then
  309. __vhost_full_vhost_statement "$type" http "$cfg" || return 1
  310. fi
  311. if is_protocol_enabled https "$protocols"; then
  312. read-0 ssl_plugin_fun ssl_cfg_value ssl_cfg_options < <(ssl_get_plugin_fun "$cfg") || return 1
  313. "$ssl_plugin_fun"_vars "$cfg" "$ssl_cfg_options" "$ssl_cfg_value" || return 1
  314. vhost_statement=$(__vhost_full_vhost_statement "$type" https "$cfg") || return 1
  315. cat <<EOF
  316. <IfModule mod_ssl.c>
  317. $(echo "$vhost_statement" | prefix " ")
  318. </IfModule>
  319. EOF
  320. fi
  321. }
  322. export -f apache_vhost_statement
  323. apache_code_dir() {
  324. local cfg="$1" www_data_gid local_path
  325. www_data_gid=$(cached_cmd_on_base_image apache 'id -g www-data') || {
  326. debug "Failed to query for www-data gid in ${DARKYELLOW}apache${NORMAL} base image."
  327. return 1
  328. }
  329. domain=$(get_domain "$cfg") || return 1
  330. local_path="/var/www/${domain}"
  331. host_path=$(e "$cfg" | cfg-get-value location 2>/dev/null) ||
  332. host_path="$DATASTORE/$BASE_SERVICE_NAME${local_path}"
  333. mkdir -p "$host_path" || return 1
  334. setfacl -R -m g:"$www_data_gid":rx "$host_path"
  335. info "Set permission for read and traversal on '$host_path'."
  336. config-add "
  337. $MASTER_BASE_SERVICE_NAME:
  338. volumes:
  339. - $host_path:$local_path
  340. "
  341. }
  342. apache_data_dirs() {
  343. local cfg="$1" data_dirs dst data dirs
  344. data_dirs=$(e "$cfg" | cfg-get-value data-dirs 2>/dev/null | shyaml get-values 2>/dev/null) || true
  345. if [ -z "$data_dirs" ]; then
  346. return 0
  347. fi
  348. domain=$(get_domain "$cfg") || return 1
  349. local_path="/var/www/${domain}"
  350. dst=$DATASTORE/$BASE_SERVICE_NAME$local_path
  351. data=()
  352. while IFS="," read -ra addr; do
  353. for dir in "${addr[@]}"; do
  354. data+=($dir)
  355. done
  356. done <<< "$data_dirs"
  357. www_data_gid=$(cached_cmd_on_base_image apache 'id -g www-data') || {
  358. debug "Failed to query for www-data gid in ${DARKYELLOW}apache${NORMAL} base image."
  359. return 1
  360. }
  361. info "www-data gid from ${DARKYELLOW}apache${NORMAL} is '$www_data_gid'"
  362. dirs=()
  363. for d in "${data[@]}"; do
  364. dirs+=("$dst/$d")
  365. done
  366. mkdir -p "${dirs[@]}"
  367. setfacl -R -m g:"$www_data_gid":rwx "${dirs[@]}"
  368. setfacl -R -d -m g:"$www_data_gid":rwx "${dirs[@]}"
  369. config-add "
  370. $MASTER_BASE_SERVICE_NAME:
  371. volumes:
  372. $(
  373. for d in "${data[@]}"; do
  374. echo " - $dst/$d:$local_path/$d"
  375. done
  376. )"
  377. }
  378. deploy_files() {
  379. local src="$1" dst="$2"
  380. if ! [ -d "$dst" ]; then
  381. err "Destination '$dst' does not exist or is not a directory"
  382. return 1
  383. fi
  384. (
  385. cd "$dst" && info "In $dst:" &&
  386. get_file "$src" | tar xv
  387. )
  388. }
  389. export -f deploy_files
  390. apache_core_rules_add() {
  391. local conf="$1" dst="/etc/apache2/conf-enabled/$BASE_SERVICE_NAME.conf"
  392. debug "Adding core rule."
  393. echo "$conf" | file_put "$CONFIGSTORE/$BASE_SERVICE_NAME$dst"
  394. config_hash=$(printf "%s\0" "$config_hash" "$conf" | md5_compat)
  395. config-add "
  396. $MASTER_BASE_SERVICE_NAME:
  397. volumes:
  398. - $CONFIGSTORE/$BASE_SERVICE_NAME$dst:$dst:ro
  399. "
  400. }
  401. __vhost_ssl_statement() {
  402. ## defaults
  403. __vhost_cfg_SSL_CERT_LOCATION=${__vhost_cfg_SSL_CERT_LOCATION:-/etc/ssl/certs/ssl-cert-snakeoil.pem}
  404. __vhost_cfg_SSL_KEY_LOCATION=${__vhost_cfg_SSL_KEY_LOCATION:-/etc/ssl/private/ssl-cert-snakeoil.key}
  405. cat <<EOF
  406. ##
  407. ## SSL Configuration
  408. ##
  409. SSLEngine On
  410. SSLCertificateFile $__vhost_cfg_SSL_CERT_LOCATION
  411. SSLCertificateKeyFile $__vhost_cfg_SSL_KEY_LOCATION
  412. $([ -z "$__vhost_cfg_SSL_CA_CERT_LOCATION" ] || echo "SSLCACertificateFile $__vhost_cfg_SSL_CA_CERT_LOCATION")
  413. $([ -z "$__vhost_cfg_SSL_CHAIN" ] || echo "SSLCertificateChainFile $__vhost_cfg_SSL_CHAIN")
  414. SSLVerifyClient None
  415. EOF
  416. }
  417. password-path-get() {
  418. local cfg="$1" domain
  419. domain=$(get_domain "$cfg") || return 1
  420. echo /etc/apache2/sites-enabled/${domain}.passwd
  421. }
  422. __vhost_creds_statement() {
  423. local cfg="$1" password_path
  424. password_path=$(password-path-get "$cfg") || return 1
  425. if ! e "$cfg" | cfg-get-value creds >/dev/null 2>&1; then
  426. echo "Allow from all"
  427. return 0
  428. fi
  429. cat <<EOF
  430. AuthType basic
  431. AuthName "private"
  432. AuthUserFile ${password_path}
  433. Require valid-user
  434. EOF
  435. }
  436. __vhost_head_statement() {
  437. local cfg="$1" protocol="$2" server_aliases admin_mail
  438. domain=$(get_domain "$cfg") || return 1
  439. admin_mail=$(e "$1" | cfg-get-value "admin-mail" 2>/dev/null) || true
  440. server_aliases=$(e "$cfg" | cfg-get-value server-aliases 2>/dev/null) || true
  441. [ "$server_aliases" == None ] && server_aliases=""
  442. if [ "$server_aliases" ]; then
  443. server_aliases=($(e "$server_aliases" | shyaml get-values)) || return 1
  444. if [ -z "$domain" ]; then
  445. err "You can't specify server aliases if you don't have a domain."
  446. return 1
  447. fi
  448. else
  449. server_aliases=()
  450. fi
  451. if [ "$protocol" == "https" ]; then
  452. prefix="s-"
  453. else
  454. prefix=
  455. fi
  456. cat <<EOF
  457. ServerAdmin ${admin_mail:-contact@$domain}
  458. ServerName ${domain}
  459. $(
  460. for alias in "${server_aliases[@]}"; do
  461. [ "$alias" ] || continue
  462. echo "ServerAlias $alias"
  463. done
  464. )
  465. ServerSignature Off
  466. CustomLog /var/log/apache2/${prefix}${domain}_access.log combined
  467. ErrorLog /var/log/apache2/${prefix}${domain}_error.log
  468. ErrorLog syslog:local2
  469. EOF
  470. }
  471. _get_custom_rules() {
  472. local cfg="$1" custom_rules type elt value first
  473. custom_rules=$(e "$cfg" | cfg-get-value apache-custom-rules 2>/dev/null) || true
  474. if [ -z "$custom_rules" ]; then
  475. return 0
  476. fi
  477. type=$(echo "$custom_rules" | shyaml get-type)
  478. value=
  479. case "$type" in
  480. "sequence")
  481. first=1
  482. while read-0 elt; do
  483. elt="$(echo "$elt" | yaml_get_interpret)" || return 1
  484. [ "$elt" ] || continue
  485. if [ "$first" ]; then
  486. first=
  487. else
  488. value+=$'\n'$'\n'
  489. fi
  490. first=
  491. value+="$elt"
  492. done < <(echo "$custom_rules" | shyaml -y get-values-0)
  493. ;;
  494. "struct")
  495. while read-0 _key val; do
  496. value+=$'\n'"$(echo "$val" | yaml_get_interpret)" || return 1
  497. done < <(echo "$custom_rules" | shyaml -y key-values-0)
  498. ;;
  499. "str")
  500. value+=$(echo "$custom_rules")
  501. ;;
  502. *)
  503. value+=$(echo "$custom_rules")
  504. ;;
  505. esac
  506. printf "%s" "$value"
  507. }
  508. __vhost_custom_rules() {
  509. local cfg="$1" custom_rules
  510. custom_rules=$(_get_custom_rules "$cfg") || return 1
  511. if [ "$custom_rules" ]; then
  512. cat <<EOF
  513. ##
  514. ## Custom rules
  515. ##
  516. $custom_rules
  517. EOF
  518. fi
  519. }
  520. __vhost_content_statement() {
  521. local type="$1"
  522. shift
  523. case "$type" in
  524. "web_proxy")
  525. __vhost_proxy_statement "$@" || return 1
  526. ;;
  527. "publish_dir")
  528. __vhost_publish_dir_statement "$@" || return 1
  529. ;;
  530. esac
  531. }
  532. target-get() {
  533. local cfg="$1" target first_exposed_port base_image
  534. target=$(e "$cfg" | cfg-get-value target 2>/dev/null) || true
  535. if [ -z "$target" ]; then
  536. ## First exposed port:
  537. base_image=$(service_base_docker_image "$BASE_SERVICE_NAME") || return 1
  538. if ! docker_has_image "$base_image"; then
  539. docker pull "$base_image" >&2
  540. fi
  541. first_exposed_port=$(image_exposed_ports_0 "$base_image" | tr '\0' '\n' | head -n 1 | cut -f 1 -d /) || return 1
  542. if [ -z "$first_exposed_port" ]; then
  543. err "Failed to get first exposed port of image '$base_image'."
  544. return 1
  545. fi
  546. target=$MASTER_BASE_SERVICE_NAME:$first_exposed_port
  547. info "No target was specified, introspection found: $target"
  548. fi
  549. echo "$target"
  550. }
  551. __vhost_proxy_statement() {
  552. local protocol="$1" cfg="$2"
  553. target=$(target-get "$cfg") || return 1
  554. cat <<EOF
  555. ##
  556. ## Proxy declaration towards $target
  557. ##
  558. <IfModule mod_proxy.c>
  559. ProxyRequests Off
  560. <Proxy *>
  561. Order deny,allow
  562. Allow from all
  563. </Proxy>
  564. ProxyVia On
  565. ProxyPass / http://$target/ retry=0
  566. <Location / >
  567. $(__vhost_creds_statement "$cfg" | prefix " ")
  568. ProxyPassReverse /
  569. </Location>
  570. $([ "$protocol" == "https" ] && echo " SSLProxyEngine On")
  571. </IfModule>
  572. RequestHeader set "X-Forwarded-Proto" "$protocol"
  573. ## Fix IE problem (httpapache proxy dav error 408/409)
  574. SetEnv proxy-nokeepalive 1
  575. EOF
  576. }
  577. __vhost_full_vhost_statement() {
  578. local type="$1" protocol="$2" cfg="$3" head_statement custom_rules content_statement
  579. head_statement=$(__vhost_head_statement "$cfg" "$protocol") || return 1
  580. custom_rules=$(__vhost_custom_rules "$cfg") || return 1
  581. content_statement=$(__vhost_content_statement "$type" "$protocol" "$cfg") || return 1
  582. case "$protocol" in
  583. https)
  584. PORT=443
  585. ;;
  586. http)
  587. PORT=80
  588. ;;
  589. esac
  590. cat <<EOF
  591. <VirtualHost *:$PORT>
  592. $(echo "$head_statement" | prefix " ")
  593. $(echo "$custom_rules" | prefix " ")
  594. $(echo "$content_statement" | prefix " ")
  595. ## Forbid any cache, this is only usefull on dev server.
  596. #Header set Cache-Control "no-cache"
  597. #Header set Access-Control-Allow-Origin "*"
  598. #Header set Access-Control-Allow-Methods "POST, GET, OPTIONS"
  599. #Header set Access-Control-Allow-Headers "origin, content-type, accept"
  600. $([ "$protocol" == "https" ] && __vhost_ssl_statement | prefix " " && echo )
  601. </VirtualHost>
  602. EOF
  603. }
  604. __vhost_publish_dir_statement() {
  605. local protocol="$1" cfg="$2"
  606. domain=$(get_domain "$cfg") || return 1
  607. local_path="/var/www/${domain}"
  608. cat <<EOF
  609. ##
  610. ## Publish directory $local_path
  611. ##
  612. DocumentRoot $local_path
  613. <Directory />
  614. Options FollowSymLinks
  615. AllowOverride None
  616. </Directory>
  617. <Directory $local_path>
  618. Options Indexes FollowSymLinks MultiViews
  619. AllowOverride all
  620. $(__vhost_creds_statement "$cfg" | prefix " ")
  621. </Directory>
  622. EOF
  623. }
  624. apache_config_hash() {
  625. debug "Adding config hash to enable recreating upon config change."
  626. config_hash=$({
  627. printf "%s\0" "$config_hash"
  628. find "$SERVICE_CONFIGSTORE/etc/apache2/sites-enabled" \
  629. -name \*.conf -exec md5sum {} \;
  630. } | md5_compat) || exit 1
  631. init-config-add "
  632. $MASTER_BASE_SERVICE_NAME:
  633. labels:
  634. - compose.config_hash=$config_hash
  635. "
  636. }