docker.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. 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
  204. _err "copy error"
  205. return 1
  206. fi
  207. return 0
  208. else
  209. _err "Not implemented yet."
  210. return 1
  211. fi
  212. }
  213. #sock method endpoint data content-type
  214. _curl_unix_sock() {
  215. _socket="$1"
  216. _method="$2"
  217. _endpoint="$3"
  218. _data="$4"
  219. _ctype="$5"
  220. if [ -z "$_ctype" ]; then
  221. _ctype="Content-Type: application/json"
  222. fi
  223. _debug _data "$_data"
  224. _debug2 "url" "http://localhost$_endpoint"
  225. if [ "$_CURL_NO_HOST" ]; then
  226. _cux_url="http:$_endpoint"
  227. else
  228. _cux_url="http://localhost$_endpoint"
  229. fi
  230. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  231. curl -vvv --silent --unix-socket "$_socket" -X "$_method" --data-binary "$_data" --header "$_ctype" "$_cux_url"
  232. else
  233. curl --silent --unix-socket "$_socket" -X "$_method" --data-binary "$_data" --header "$_ctype" "$_cux_url"
  234. fi
  235. }
  236. _check_curl_version() {
  237. _cversion="$(curl -V | grep '^curl ' | cut -d ' ' -f 2)"
  238. _debug2 "_cversion" "$_cversion"
  239. _major="$(_getfield "$_cversion" 1 '.')"
  240. _debug2 "_major" "$_major"
  241. _minor="$(_getfield "$_cversion" 2 '.')"
  242. _debug2 "_minor" "$_minor"
  243. if [ "$_major$_minor" -lt "740" ]; then
  244. _err "curl v$_cversion doesn't support unit socket"
  245. return 1
  246. fi
  247. if [ "$_major$_minor" -lt "750" ]; then
  248. _debug "Use short host name"
  249. export _CURL_NO_HOST=1
  250. else
  251. export _CURL_NO_HOST=
  252. fi
  253. return 0
  254. }