localcopy.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env sh
  2. # Deploy-hook to very simply copy files to set directories and then
  3. # execute whatever reloadcmd the admin needs afterwards. This can be
  4. # useful for configurations where the "multideploy" hook (in development)
  5. # is used or when an admin wants ACME.SH to renew certs but needs to
  6. # manually configure deployment via an external script
  7. # (e.g. The deploy-freenas script for TrueNAS Core/Scale
  8. # https://github.com/danb35/deploy-freenas/ )
  9. #
  10. # If the same file is configured for the certificate key
  11. # and the certificate and/or full chain, a combined PEM file will
  12. # be output instead.
  13. #
  14. # Environment variables to be utilized are as follows:
  15. #
  16. # DEPLOY_LOCALCOPY_CERTKEY - /path/to/target/cert.key
  17. # DEPLOY_LOCALCOPY_CERTIFICATE - /path/to/target/cert.cer
  18. # DEPLOY_LOCALCOPY_FULLCHAIN - /path/to/target/fullchain.cer
  19. # DEPLOY_LOCALCOPY_CA - /path/to/target/ca.cer
  20. # DEPLOY_LOCALCOPY_PFX - /path/to/target/cert.pfx
  21. # DEPLOY_LOCALCOPY_RELOADCMD - "echo 'this is my cmd'"
  22. ######## Public functions #####################
  23. #domain keyfile certfile cafile fullchain
  24. localcopy_deploy() {
  25. _cdomain="$1"
  26. _ckey="$2"
  27. _ccert="$3"
  28. _cca="$4"
  29. _cfullchain="$5"
  30. _cpfx="$6"
  31. _debug _cdomain "$_cdomain"
  32. _debug _ckey "$_ckey"
  33. _debug _ccert "$_ccert"
  34. _debug _cca "$_cca"
  35. _debug _cfullchain "$_cfullchain"
  36. _debug _cpfx "$_cpfx"
  37. _getdeployconf DEPLOY_LOCALCOPY_CERTIFICATE
  38. _getdeployconf DEPLOY_LOCALCOPY_CERTKEY
  39. _getdeployconf DEPLOY_LOCALCOPY_FULLCHAIN
  40. _getdeployconf DEPLOY_LOCALCOPY_CA
  41. _getdeployconf DEPLOY_LOCALCOPY_RELOADCMD
  42. _getdeployconf DEPLOY_LOCALCOPY_PFX
  43. _combined_target=""
  44. _combined_srccert=""
  45. # Create PEM file
  46. if [ "$DEPLOY_LOCALCOPY_CERTKEY" ] &&
  47. { [ "$DEPLOY_LOCALCOPY_CERTKEY" = "$DEPLOY_LOCALCOPY_FULLCHAIN" ] ||
  48. [ "$DEPLOY_LOCALCOPY_CERTKEY" = "$DEPLOY_LOCALCOPY_CERTIFICATE" ]; }; then
  49. _combined_target="$DEPLOY_LOCALCOPY_CERTKEY"
  50. _savedeployconf DEPLOY_LOCALCOPY_CERTKEY "$DEPLOY_LOCALCOPY_CERTKEY"
  51. if [ "$DEPLOY_LOCALCOPY_CERTKEY" = "$DEPLOY_LOCALCOPY_CERTIFICATE" ]; then
  52. _combined_srccert="$_ccert"
  53. _savedeployconf DEPLOY_LOCALCOPY_CERTIFICATE "$DEPLOY_LOCALCOPY_CERTIFICATE"
  54. DEPLOY_LOCALCOPY_CERTIFICATE=""
  55. fi
  56. if [ "$DEPLOY_LOCALCOPY_CERTKEY" = "$DEPLOY_LOCALCOPY_FULLCHAIN" ]; then
  57. _combined_srccert="$_cfullchain"
  58. _savedeployconf DEPLOY_LOCALCOPY_FULLCHAIN "$DEPLOY_LOCALCOPY_FULLCHAIN"
  59. DEPLOY_LOCALCOPY_FULLCHAIN=""
  60. fi
  61. DEPLOY_LOCALCOPY_CERTKEY=""
  62. _info "Creating combined PEM"
  63. _debug "Creating combined PEM at $_combined_target"
  64. if ! [ -f "$_combined_target" ]; then
  65. touch "$_combined_target" || return 1
  66. chmod 600 "$_combined_target"
  67. fi
  68. if ! cat "$_combined_srccert" "$_ckey" >"$_combined_target"; then
  69. _err "Failed to create PEM file"
  70. return 1
  71. fi
  72. fi
  73. if [ "$DEPLOY_LOCALCOPY_CERTIFICATE" ]; then
  74. _info "Copying certificate"
  75. _debug "Copying $_ccert to $DEPLOY_LOCALCOPY_CERTIFICATE"
  76. if ! cat "$_ccert" >"$DEPLOY_LOCALCOPY_CERTIFICATE"; then
  77. _err "Failed to copy certificate, aborting."
  78. return 1
  79. fi
  80. _savedeployconf DEPLOY_LOCALCOPY_CERTIFICATE "$DEPLOY_LOCALCOPY_CERTIFICATE"
  81. fi
  82. if [ "$DEPLOY_LOCALCOPY_CERTKEY" ]; then
  83. _info "Copying certificate key"
  84. _debug "Copying $_ckey to $DEPLOY_LOCALCOPY_CERTKEY"
  85. if ! [ -f "$DEPLOY_LOCALCOPY_CERTKEY" ]; then
  86. touch "$DEPLOY_LOCALCOPY_CERTKEY" || return 1
  87. chmod 600 "$DEPLOY_LOCALCOPY_CERTKEY"
  88. fi
  89. if ! cat "$_ckey" >"$DEPLOY_LOCALCOPY_CERTKEY"; then
  90. _err "Failed to copy certificate key, aborting."
  91. return 1
  92. fi
  93. _savedeployconf DEPLOY_LOCALCOPY_CERTKEY "$DEPLOY_LOCALCOPY_CERTKEY"
  94. fi
  95. if [ "$DEPLOY_LOCALCOPY_FULLCHAIN" ]; then
  96. _info "Copying fullchain"
  97. _debug "Copying $_cfullchain to $DEPLOY_LOCALCOPY_FULLCHAIN"
  98. if ! cat "$_cfullchain" >"$DEPLOY_LOCALCOPY_FULLCHAIN"; then
  99. _err "Failed to copy fullchain, aborting."
  100. return 1
  101. fi
  102. _savedeployconf DEPLOY_LOCALCOPY_FULLCHAIN "$DEPLOY_LOCALCOPY_FULLCHAIN"
  103. fi
  104. if [ "$DEPLOY_LOCALCOPY_CA" ]; then
  105. _info "Copying CA"
  106. _debug "Copying $_cca to $DEPLOY_LOCALCOPY_CA"
  107. if ! cat "$_cca" >"$DEPLOY_LOCALCOPY_CA"; then
  108. _err "Failed to copy CA, aborting."
  109. return 1
  110. fi
  111. _savedeployconf DEPLOY_LOCALCOPY_CA "$DEPLOY_LOCALCOPY_CA"
  112. fi
  113. if [ "$DEPLOY_LOCALCOPY_PFX" ]; then
  114. _info "Copying PFX"
  115. _debug "Copying $_cpfx to $DEPLOY_LOCALCOPY_PFX"
  116. if ! [ -f "$DEPLOY_LOCALCOPY_PFX" ]; then
  117. touch "$DEPLOY_LOCALCOPY_PFX" || return 1
  118. chmod 600 "$DEPLOY_LOCALCOPY_PFX"
  119. fi
  120. if ! cat "$_cpfx" >"$DEPLOY_LOCALCOPY_PFX"; then
  121. _err "Failed to copy PFX, aborting."
  122. return 1
  123. fi
  124. _savedeployconf DEPLOY_LOCALCOPY_PFX "$DEPLOY_LOCALCOPY_PFX"
  125. fi
  126. _reload=$DEPLOY_LOCALCOPY_RELOADCMD
  127. _debug "Running reloadcmd $_reload"
  128. if [ -z "$_reload" ]; then
  129. _info "Reloadcmd not provided, skipping."
  130. else
  131. _info "Reloading"
  132. if eval "$_reload"; then
  133. _info "Reload successful."
  134. _savedeployconf DEPLOY_LOCALCOPY_RELOADCMD "$DEPLOY_LOCALCOPY_RELOADCMD" "base64"
  135. else
  136. _err "Reload failed."
  137. return 1
  138. fi
  139. fi
  140. _info "$(__green "'localcopy' deploy success")"
  141. return 0
  142. }