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.

148 lines
5.6 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_WEBUI_ADMIN
  16. if [ -z "$DEPLOY_OMV_WEBUI_ADMIN" ]; then
  17. DEPLOY_OMV_WEBUI_ADMIN="admin"
  18. fi
  19. _savedeployconf DEPLOY_OMV_WEBUI_ADMIN "$DEPLOY_OMV_WEBUI_ADMIN"
  20. _getdeployconf DEPLOY_OMV_HOST
  21. _getdeployconf DEPLOY_OMV_SSH_USER
  22. if [ -n "$DEPLOY_OMV_HOST" ] && [ -n "$DEPLOY_OMV_SSH_USER" ]; then
  23. _info "[OMV deploy-hook] Deploy certificate remotely through ssh."
  24. _savedeployconf DEPLOY_OMV_HOST "$DEPLOY_OMV_HOST"
  25. _savedeployconf DEPLOY_OMV_SSH_USER "$DEPLOY_OMV_SSH_USER"
  26. else
  27. _info "[OMV deploy-hook] Deploy certificate locally."
  28. fi
  29. if [ -n "$DEPLOY_OMV_HOST" ] && [ -n "$DEPLOY_OMV_SSH_USER" ]; then
  30. _command="omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'CertificateMgmt' 'getList' '{\"start\": 0, \"limit\": -1}' | jq -r '.data[] | select(.name==\"/CN='$_cdomain'\") | .uuid'"
  31. # shellcheck disable=SC2029
  32. _uuid=$(ssh "$DEPLOY_OMV_SSH_USER@$DEPLOY_OMV_HOST" "$_command")
  33. _debug _command "$_command"
  34. if [ -z "$_uuid" ]; then
  35. _info "[OMV deploy-hook] Domain $_cdomain has no certificate in openmediavault, creating it!"
  36. _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'"
  37. # shellcheck disable=SC2029
  38. _uuid=$(ssh "$DEPLOY_OMV_SSH_USER@$DEPLOY_OMV_HOST" "$_command")
  39. _debug _command "$_command"
  40. if [ -z "$_uuid" ]; then
  41. _err "[OMV deploy-hook] An error occured while creating the certificate"
  42. return 1
  43. fi
  44. fi
  45. _info "[OMV deploy-hook] Domain $_cdomain has uuid: $_uuid"
  46. _fullchain=$(jq <"$_cfullchain" -aRs .)
  47. _key=$(jq <"$_ckey" -aRs .)
  48. _debug _fullchain "$_fullchain"
  49. _debug _key "$_key"
  50. _info "[OMV deploy-hook] Updating key and certificate in openmediavault"
  51. _command="omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'CertificateMgmt' 'set' '{\"uuid\":\"$_uuid\", \"certificate\":$_fullchain, \"privatekey\":$_key, \"comment\":\"acme.sh deployed $(date)\"}'"
  52. # shellcheck disable=SC2029
  53. _result=$(ssh "$DEPLOY_OMV_SSH_USER@$DEPLOY_OMV_HOST" "$_command")
  54. _debug _command "$_command"
  55. _debug _result "$_result"
  56. _command="omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'WebGui' 'setSettings' \$(omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'WebGui' 'getSettings' | jq -c '.sslcertificateref=\"$_uuid\"')"
  57. # shellcheck disable=SC2029
  58. _result=$(ssh "$DEPLOY_OMV_SSH_USER@$DEPLOY_OMV_HOST" "$_command")
  59. _debug _command "$_command"
  60. _debug _result "$_result"
  61. _info "[OMV deploy-hook] Asking openmediavault to apply changes... (this could take some time, hang in there)"
  62. _command="omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'Config' 'applyChanges' '{\"modules\":[], \"force\": false}'"
  63. # shellcheck disable=SC2029
  64. _result=$(ssh "$DEPLOY_OMV_SSH_USER@$DEPLOY_OMV_HOST" "$_command")
  65. _debug _command "$_command"
  66. _debug _result "$_result"
  67. _info "[OMV deploy-hook] Asking nginx to reload"
  68. _command="nginx -s reload"
  69. # shellcheck disable=SC2029
  70. _result=$(ssh "$DEPLOY_OMV_SSH_USER@$DEPLOY_OMV_HOST" "$_command")
  71. _debug _command "$_command"
  72. _debug _result "$_result"
  73. else
  74. # shellcheck disable=SC2086
  75. _uuid=$(omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'CertificateMgmt' 'getList' '{"start": 0, "limit": -1}' | jq -r '.data[] | select(.name=="/CN='$_cdomain'") | .uuid')
  76. if [ -z "$_uuid" ]; then
  77. _info "[OMV deploy-hook] Domain $_cdomain has no certificate in openmediavault, creating it!"
  78. # shellcheck disable=SC2086
  79. _uuid=$(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')
  80. if [ -z "$_uuid" ]; then
  81. _err "[OMB deploy-hook] An error occured while creating the certificate"
  82. return 1
  83. fi
  84. fi
  85. _info "[OMV deploy-hook] Domain $_cdomain has uuid: $_uuid"
  86. _fullchain=$(jq <"$_cfullchain" -aRs .)
  87. _key=$(jq <"$_ckey" -aRs .)
  88. _debug _fullchain "$_fullchain"
  89. _debug _key "$_key"
  90. _info "[OMV deploy-hook] Updating key and certificate in openmediavault"
  91. _command="omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'CertificateMgmt' 'set' '{\"uuid\":\"$_uuid\", \"certificate\":$_fullchain, \"privatekey\":$_key, \"comment\":\"acme.sh deployed $(date)\"}'"
  92. _result=$(eval "$_command")
  93. _debug _command "$_command"
  94. _debug _result "$_result"
  95. _command="omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'WebGui' 'setSettings' \$(omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'WebGui' 'getSettings' | jq -c '.sslcertificateref=\"$_uuid\"')"
  96. _result=$(eval "$_command")
  97. _debug _command "$_command"
  98. _debug _result "$_result"
  99. _info "[OMV deploy-hook] Asking openmediavault to apply changes... (this could take some time, hang in there)"
  100. _command="omv-rpc -u $DEPLOY_OMV_WEBUI_ADMIN 'Config' 'applyChanges' '{\"modules\":[], \"force\": false}'"
  101. _result=$(eval "$_command")
  102. _debug _command "$_command"
  103. _debug _result "$_result"
  104. _info "[OMV deploy-hook] Asking nginx to reload"
  105. _command="nginx -s reload"
  106. _result=$(eval "$_command")
  107. _debug _command "$_command"
  108. _debug _result "$_result"
  109. fi
  110. return 0
  111. }