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.

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