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.

92 lines
3.1 KiB

  1. #!/usr/bin/env sh
  2. ######## Public functions #####################
  3. #domain keyfile certfile cafile fullchain
  4. openmediavault_deploy() {
  5. _cdomain="$1"
  6. _ckey="$2"
  7. _ccert="$3"
  8. _cca="$4"
  9. _cfullchain="$5"
  10. _debug _cdomain "$_cdomain"
  11. _debug _ckey "$_ckey"
  12. _debug _ccert "$_ccert"
  13. _debug _cca "$_cca"
  14. _debug _cfullchain "$_cfullchain"
  15. _getdeployconf DEPLOY_OMV_HOST
  16. if [ -z "$DEPLOY_OMV_HOST" ]; then
  17. _debug "Using _cdomain as DEPLOY_OMV_HOST, please set if not correct."
  18. DEPLOY_OMV_HOST="$_cdomain"
  19. fi
  20. _getdeployconf DEPLOY_OMV_WEBUI_ADMIN
  21. if [ -z "$DEPLOY_OMV_WEBUI_ADMIN" ]; then
  22. DEPLOY_OMV_WEBUI_ADMIN="admin"
  23. fi
  24. _getdeployconf DEPLOY_OMV_SSH_USER
  25. if [ -z "$DEPLOY_OMV_SSH_USER" ]; then
  26. DEPLOY_OMV_SSH_USER="root"
  27. fi
  28. _savedeployconf DEPLOY_OMV_HOST "$DEPLOY_OMV_HOST"
  29. _savedeployconf DEPLOY_OMV_WEBUI_ADMIN "$DEPLOY_OMV_WEBUI_ADMIN"
  30. _savedeployconf DEPLOY_OMV_SSH_USER "$DEPLOY_OMV_SSH_USER"
  31. _command="omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'CertificateMgmt' 'getList' '{\"start\": 0, \"limit\": -1}' | jq -r '.data[] | select(.name==\"/CN='$_cdomain'\") | .uuid'"
  32. # shellcheck disable=SC2086
  33. _uuid=$(ssh "$DEPLOY_OMV_SSH_USER@$DEPLOY_OMV_HOST" "$_command")
  34. _debug _command "$_command"
  35. if [ -z "$_uuid" ]; then
  36. _info "[OMV deploy-hook] Domain $_cdomain has no certificate in openmediavault, creating it!"
  37. _command="omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'CertificateMgmt' 'create' '{\"cn\": \"test.example.com\", \"size\": 4096, \"days\": 3650, \"c\": \"\", \"st\": \"\", \"l\": \"\", \"o\": \"\", \"ou\": \"\", \"email\": \"\"}' | jq -r '.uuid'"
  38. # shellcheck disable=SC2086
  39. _uuid=$(ssh "$DEPLOY_OMV_SSH_USER@$DEPLOY_OMV_HOST" "$_command")
  40. _debug _command "$_command"
  41. if [ -z "$_uuid" ]; then
  42. _err "[OMB deploy-hook] An error occured while creating the certificate"
  43. return 1
  44. fi
  45. fi
  46. _info "[OMV deploy-hook] Domain $_cdomain has uuid: $_uuid"
  47. _fullchain=$(jq <"$_cfullchain" -aRs .)
  48. _key=$(jq <"$_ckey" -aRs .)
  49. _debug _fullchain "$_fullchain"
  50. _debug _key "$_key"
  51. _info "[OMV deploy-hook] Updating key and certificate in openmediavault"
  52. _command="omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'CertificateMgmt' 'set' '{\"uuid\":\"$_uuid\", \"certificate\":$_fullchain, \"privatekey\":$_key, \"comment\":\"acme.sh deployed $(date)\"}'"
  53. # shellcheck disable=SC2029
  54. _result=$(ssh "$DEPLOY_OMV_SSH_USER@$DEPLOY_OMV_HOST" "$_command")
  55. _debug _command "$_command"
  56. _debug _result "$_result"
  57. _info "[OMV deploy-hook] Asking openmediavault to apply changes... (this could take some time, hang in there)"
  58. _command="omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'Config' 'applyChanges' '{\"modules\":[], \"force\": false}'"
  59. # shellcheck disable=SC2029
  60. _result=$(ssh "$DEPLOY_OMV_SSH_USER@$DEPLOY_OMV_HOST" "$_command")
  61. _debug _command "$_command"
  62. _debug _result "$_result"
  63. _info "[OMV deploy-hook] Asking nginx to reload"
  64. _command="nginx -s reload"
  65. # shellcheck disable=SC2029
  66. _result=$(ssh "$DEPLOY_OMV_SSH_USER@$DEPLOY_OMV_HOST" "$_command")
  67. _debug _command "$_command"
  68. _debug _result "$_result"
  69. return 0
  70. }