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.

85 lines
2.8 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_USER
  21. if [ -z "$DEPLOY_OMV_USER" ]; then
  22. DEPLOY_OMV_USER="admin"
  23. fi
  24. _savedeployconf DEPLOY_OMV_HOST "$DEPLOY_OMV_HOST"
  25. _savedeployconf DEPLOY_OMV_USER "$DEPLOY_OMV_USER"
  26. _command="omv-rpc -u $DEPLOY_OMV_USER 'CertificateMgmt' 'getList' '{\"start\": 0, \"limit\": -1}' | jq -r '.data[] | select(.name==\"/CN='$_cdomain'\") | .uuid'"
  27. # shellcheck disable=SC2086
  28. _uuid=$(ssh "root@$DEPLOY_OMV_HOST" "$_command")
  29. _debug _command "$_command"
  30. if [ -z "$_uuid" ]; then
  31. _info "[OMV deploy-hook] Domain $_cdomain has no certificate in openmediavault, creating it!"
  32. _command="omv-rpc -u $DEPLOY_OMV_USER 'CertificateMgmt' 'create' '{\"cn\": \"test.example.com\", \"size\": 4096, \"days\": 3650, \"c\": \"\", \"st\": \"\", \"l\": \"\", \"o\": \"\", \"ou\": \"\", \"email\": \"\"}' | jq -r '.uuid'"
  33. # shellcheck disable=SC2086
  34. _uuid=$(ssh "root@$DEPLOY_OMV_HOST" "$_command")
  35. _debug _command "$_command"
  36. if [ -z "$_uuid" ]; then
  37. _err "[OMB deploy-hook] An error occured while creating the certificate"
  38. return 1
  39. fi
  40. fi
  41. _info "[OMV deploy-hook] Domain $_cdomain has uuid: $_uuid"
  42. _fullchain=$(jq <"$_cfullchain" -aRs .)
  43. _key=$(jq <"$_ckey" -aRs .)
  44. _debug _fullchain "$_fullchain"
  45. _debug _key "$_key"
  46. _info "[OMV deploy-hook] Updating key and certificate in openmediavault"
  47. _command="omv-rpc -u $DEPLOY_OMV_USER 'CertificateMgmt' 'set' '{\"uuid\":\"$_uuid\", \"certificate\":$_fullchain, \"privatekey\":$_key, \"comment\":\"acme.sh deployed $(date)\"}'"
  48. # shellcheck disable=SC2029
  49. _result=$(ssh "root@$DEPLOY_OMV_HOST" "$_command")
  50. _debug _result "$_result"
  51. _debug _command "$_command"
  52. _info "[OMV deploy-hook] Asking openmediavault to apply changes... (this could take some time, hang in there)"
  53. _command="omv-rpc -u $DEPLOY_OMV_USER 'Config' 'applyChanges' '{\"modules\":[], \"force\": false}'"
  54. # shellcheck disable=SC2029
  55. _result=$(ssh "root@$DEPLOY_OMV_HOST" "$_command")
  56. _debug _command "$_command"
  57. _debug _result "$_result"
  58. _info "[OMV deploy-hook] Asking nginx to reload"
  59. _command="nginx -s reload"
  60. # shellcheck disable=SC2029
  61. _result=$(ssh "root@$DEPLOY_OMV_HOST" "$_command")
  62. _debug _command "$_command"
  63. _debug _result "$_result"
  64. return 0
  65. }