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.

265 lines
7.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/env sh
  2. #DEPLOY_DOCKER_CONTAINER_LABEL="xxxxxxx"
  3. #DEPLOY_DOCKER_CONTAINER_KEY_FILE="/path/to/key.pem"
  4. #DEPLOY_DOCKER_CONTAINER_CERT_FILE="/path/to/cert.pem"
  5. #DEPLOY_DOCKER_CONTAINER_CA_FILE="/path/to/ca.pem"
  6. #DEPLOY_DOCKER_CONTAINER_FULLCHAIN_FILE="/path/to/fullchain.pem"
  7. #DEPLOY_DOCKER_CONTAINER_RELOAD_CMD="service nginx force-reload"
  8. _DEPLOY_DOCKER_WIKI="https://github.com/Neilpang/acme.sh/wiki/deploy-to-docker-containers"
  9. _DOCKER_HOST_DEFAULT="/var/run/docker.sock"
  10. docker_deploy() {
  11. _cdomain="$1"
  12. _ckey="$2"
  13. _ccert="$3"
  14. _cca="$4"
  15. _cfullchain="$5"
  16. _debug _cdomain "$_cdomain"
  17. if [ -z "$DEPLOY_DOCKER_CONTAINER_LABEL" ]; then
  18. _err "The DEPLOY_DOCKER_CONTAINER_LABEL variable is not defined, we use this label to find the container."
  19. _err "See: $_DEPLOY_DOCKER_WIKI"
  20. fi
  21. _savedomainconf DEPLOY_DOCKER_CONTAINER_LABEL "$DEPLOY_DOCKER_CONTAINER_LABEL"
  22. if [ "$DOCKER_HOST" ]; then
  23. _saveaccountconf DOCKER_HOST "$DOCKER_HOST"
  24. fi
  25. if _exists docker && docker version | grep -i docker >/dev/null; then
  26. _info "Using docker command"
  27. export _USE_DOCKER_COMMAND=1
  28. else
  29. export _USE_DOCKER_COMMAND=
  30. fi
  31. export _USE_UNIX_SOCKET=
  32. if [ -z "$_USE_DOCKER_COMMAND" ]; then
  33. export _USE_REST=
  34. if [ "$DOCKER_HOST" ]; then
  35. _debug "Try use docker host: $DOCKER_HOST"
  36. export _USE_REST=1
  37. else
  38. export _DOCKER_SOCK="$_DOCKER_HOST_DEFAULT"
  39. _debug "Try use $_DOCKER_SOCK"
  40. if [ ! -e "$_DOCKER_SOCK" ] || [ ! -w "$_DOCKER_SOCK" ]; then
  41. _err "$_DOCKER_SOCK is not available"
  42. return 1
  43. fi
  44. export _USE_UNIX_SOCKET=1
  45. if ! _exists "curl"; then
  46. _err "Please install curl first."
  47. _err "We need curl to work."
  48. return 1
  49. fi
  50. if ! _check_curl_version; then
  51. return 1
  52. fi
  53. fi
  54. fi
  55. if [ "$DEPLOY_DOCKER_CONTAINER_KEY_FILE" ]; then
  56. _savedomainconf DEPLOY_DOCKER_CONTAINER_KEY_FILE "$DEPLOY_DOCKER_CONTAINER_KEY_FILE"
  57. fi
  58. if [ "$DEPLOY_DOCKER_CONTAINER_CERT_FILE" ]; then
  59. _savedomainconf DEPLOY_DOCKER_CONTAINER_CERT_FILE "$DEPLOY_DOCKER_CONTAINER_CERT_FILE"
  60. fi
  61. if [ "$DEPLOY_DOCKER_CONTAINER_CA_FILE" ]; then
  62. _savedomainconf DEPLOY_DOCKER_CONTAINER_CA_FILE "$DEPLOY_DOCKER_CONTAINER_CA_FILE"
  63. fi
  64. if [ "$DEPLOY_DOCKER_CONTAINER_FULLCHAIN_FILE" ]; then
  65. _savedomainconf DEPLOY_DOCKER_CONTAINER_FULLCHAIN_FILE "$DEPLOY_DOCKER_CONTAINER_FULLCHAIN_FILE"
  66. fi
  67. if [ "$DEPLOY_DOCKER_CONTAINER_RELOAD_CMD" ]; then
  68. _savedomainconf DEPLOY_DOCKER_CONTAINER_RELOAD_CMD "$DEPLOY_DOCKER_CONTAINER_RELOAD_CMD"
  69. fi
  70. _cid="$(_get_id "$DEPLOY_DOCKER_CONTAINER_LABEL")"
  71. _info "Container id: $_cid"
  72. if [ -z "$_cid" ]; then
  73. _err "can not find container id"
  74. return 1
  75. fi
  76. if [ "$DEPLOY_DOCKER_CONTAINER_KEY_FILE" ]; then
  77. if ! _docker_cp "$_cid" "$_ckey" "$DEPLOY_DOCKER_CONTAINER_KEY_FILE"; then
  78. return 1
  79. fi
  80. fi
  81. if [ "$DEPLOY_DOCKER_CONTAINER_CERT_FILE" ]; then
  82. if ! _docker_cp "$_cid" "$_ccert" "$DEPLOY_DOCKER_CONTAINER_CERT_FILE"; then
  83. return 1
  84. fi
  85. fi
  86. if [ "$DEPLOY_DOCKER_CONTAINER_CA_FILE" ]; then
  87. if ! _docker_cp "$_cid" "$_cca" "$DEPLOY_DOCKER_CONTAINER_CA_FILE"; then
  88. return 1
  89. fi
  90. fi
  91. if [ "$DEPLOY_DOCKER_CONTAINER_FULLCHAIN_FILE" ]; then
  92. if ! _docker_cp "$_cid" "$_cfullchain" "$DEPLOY_DOCKER_CONTAINER_FULLCHAIN_FILE"; then
  93. return 1
  94. fi
  95. fi
  96. if [ "$DEPLOY_DOCKER_CONTAINER_RELOAD_CMD" ]; then
  97. if ! _docker_exec "$_cid" "$DEPLOY_DOCKER_CONTAINER_RELOAD_CMD"; then
  98. return 1
  99. fi
  100. fi
  101. return 0
  102. }
  103. #label
  104. _get_id() {
  105. _label="$1"
  106. if [ "$_USE_DOCKER_COMMAND" ]; then
  107. docker ps -f label="$_label" --format "{{.ID}}"
  108. elif [ "$_USE_REST" ]; then
  109. _err "Not implemented yet."
  110. return 1
  111. elif [ "$_USE_UNIX_SOCKET" ]; then
  112. _req="{\"label\":[\"$_label\"]}"
  113. _debug2 _req "$_req"
  114. _req="$(printf "%s" "$_req" | _url_encode)"
  115. _debug2 _req "$_req"
  116. listjson="$(_curl_unix_sock "${_DOCKER_SOCK:-$_DOCKER_HOST_DEFAULT}" GET "/containers/json?filters=$_req")"
  117. _debug2 "listjson" "$listjson"
  118. echo "$listjson" | tr '{,' '\n' | grep -i '"id":' | _head_n 1 | cut -d '"' -f 4
  119. else
  120. _err "Not implemented yet."
  121. return 1
  122. fi
  123. }
  124. #id cmd
  125. _docker_exec() {
  126. _eargs="$*"
  127. _debug2 "_docker_exec $_eargs"
  128. _dcid="$1"
  129. shift
  130. if [ "$_USE_DOCKER_COMMAND" ]; then
  131. docker exec -i "$_dcid" "$@"
  132. elif [ "$_USE_REST" ]; then
  133. _err "Not implemented yet."
  134. return 1
  135. elif [ "$_USE_UNIX_SOCKET" ]; then
  136. _cmd="$*"
  137. _cmd="$(printf "%s" "$_cmd" | sed 's/ /","/g')"
  138. _debug2 _cmd "$_cmd"
  139. #create exec instance:
  140. cjson="$(_curl_unix_sock "$_DOCKER_SOCK" POST "/containers/$_dcid/exec" "{\"Cmd\": [\"$_cmd\"]}")"
  141. _debug2 cjson "$cjson"
  142. execid="$(echo "$cjson" | cut -d '"' -f 4)"
  143. _debug execid "$execid"
  144. ejson="$(_curl_unix_sock "$_DOCKER_SOCK" POST "/exec/$execid/start" "{\"Detach\": false,\"Tty\": false}")"
  145. _debug2 ejson "$ejson"
  146. else
  147. _err "Not implemented yet."
  148. return 1
  149. fi
  150. }
  151. #id from to
  152. _docker_cp() {
  153. _dcid="$1"
  154. _from="$2"
  155. _to="$3"
  156. _info "Copying file from $_from to $_to"
  157. _dir="$(dirname "$_to")"
  158. _docker_exec "$_dcid" mkdir -p "$_dir"
  159. if [ "$_USE_DOCKER_COMMAND" ]; then
  160. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  161. _docker_exec "$_dcid" tee "$_to" <"$_from"
  162. else
  163. _docker_exec "$_dcid" tee "$_to" <"$_from" >/dev/null
  164. fi
  165. if [ "$?" = "0" ]; then
  166. _info "Success"
  167. return 0
  168. else
  169. _info "Error"
  170. return 1
  171. fi
  172. elif [ "$_USE_REST" ]; then
  173. _err "Not implemented yet."
  174. return 1
  175. elif [ "$_USE_UNIX_SOCKET" ]; then
  176. _frompath="$_from"
  177. if _startswith "$_frompath" '/'; then
  178. _frompath="$(echo "$_from" | cut -b 2-)" #remove the first '/' char
  179. fi
  180. _debug2 "_frompath" "$_frompath"
  181. _toname="$(basename "$_to")"
  182. _debug2 "_toname" "$_toname"
  183. if ! tar --transform="s,$_frompath,$_toname," -cz "$_from" 2>/dev/null | _curl_unix_sock "$_DOCKER_SOCK" PUT "/containers/$_dcid/archive?noOverwriteDirNonDir=1&path=$(printf "%s" "$_dir" | _url_encode)" '@-' "Content-Type: application/octet-stream"; then
  184. _err "copy error"
  185. return 1
  186. fi
  187. return 0
  188. else
  189. _err "Not implemented yet."
  190. return 1
  191. fi
  192. }
  193. #sock method endpoint data content-type
  194. _curl_unix_sock() {
  195. _socket="$1"
  196. _method="$2"
  197. _endpoint="$3"
  198. _data="$4"
  199. _ctype="$5"
  200. if [ -z "$_ctype" ]; then
  201. _ctype="Content-Type: application/json"
  202. fi
  203. _debug _data "$_data"
  204. _debug2 "url" "http://localhost$_endpoint"
  205. if [ "$_CURL_NO_HOST" ]; then
  206. _cux_url="http:$_endpoint"
  207. else
  208. _cux_url="http://localhost$_endpoint"
  209. fi
  210. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  211. curl -vvv --silent --unix-socket "$_socket" -X "$_method" --data-binary "$_data" --header "$_ctype" "$_cux_url"
  212. else
  213. curl --silent --unix-socket "$_socket" -X "$_method" --data-binary "$_data" --header "$_ctype" "$_cux_url"
  214. fi
  215. }
  216. _check_curl_version() {
  217. _cversion="$(curl -V | grep '^curl ' | cut -d ' ' -f 2)"
  218. _debug2 "_cversion" "$_cversion"
  219. _major="$(_getfield "$_cversion" 1 '.')"
  220. _debug2 "_major" "$_major"
  221. _minor="$(_getfield "$_cversion" 2 '.')"
  222. _debug2 "_minor" "$_minor"
  223. if [ "$_major$_minor" -lt "740" ]; then
  224. _err "curl v$_cversion doesn't support unit socket"
  225. return 1
  226. fi
  227. if [ "$_major$_minor" -lt "750" ]; then
  228. _debug "Use short host name"
  229. export _CURL_NO_HOST=1
  230. else
  231. export _CURL_NO_HOST=
  232. fi
  233. return 0
  234. }