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.

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