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.

99 lines
2.8 KiB

  1. #!/bin/bash
  2. #Here is a script to deploy cert to opengear operations manager.
  3. #returns 0 means success, otherwise error.
  4. # Note that SSH must be able to login to remote host without a password...
  5. # The user must have sudo-access without password
  6. #
  7. # SSH Keys must have been exchanged with the remote host. Validate and
  8. # test that you can login to USER@SERVER from the host running acme.sh before
  9. # using this script.
  10. # export OPENGEAR_USER="" # required
  11. # export OPENGEAR_HOST="om1234" # defaults to domain name
  12. Le_Deploy_ssh_cmd="ssh"
  13. #domain keyfile certfile cafile fullchain
  14. opengear_deploy() {
  15. _cdomain="$1"
  16. _ckey="$2"
  17. _ccert="$3"
  18. _cca="$4"
  19. _cfullchain="$5"
  20. _debug _cdomain "$_cdomain"
  21. _debug _ckey "$_ckey"
  22. _debug _ccert "$_ccert"
  23. _debug _cca "$_cca"
  24. _debug _cfullchain "$_cfullchain"
  25. Le_Deploy_og_keyfile="/tmp/$(basename $_ckey)"
  26. Le_Deploy_og_fullchain="/tmp/$(basename $_cfullchain)"
  27. # OPENGEAR ENV VAR check
  28. if [ -z "$OPENGEAR_HOST" ]; then
  29. # HOST is not set in environment, check for saved variable
  30. _getdeployconf OPENGEAR_HOST
  31. _opengear_host=$OPENGEAR_HOST
  32. fi
  33. if [ -z "$_opengear_host" ]; then
  34. _info "No host found in saved vars. Defaulting to domain: $_cdomain"
  35. _opengear_host="$_cdomain"
  36. fi
  37. if [ -z "$OPENGEAR_USER" ]; then
  38. _debug "USER not found in ENV variables lets check for saved variables"
  39. _getdeployconf OPENGEAR_USER
  40. _opengear_user=$OPENGEAR_USER
  41. if [ -z "$_opengear_user" ]; then
  42. _err "No user found.. If this is the first time deploying please set OPENGEAR_USER in environment variables. Delete them after you have succesfully deployed certs."
  43. return 1
  44. else
  45. _debug "Using saved env variables."
  46. fi
  47. else
  48. _debug "Detected ENV variables to be saved to the deploy conf."
  49. _opengear_user="$OPENGEAR_USER"
  50. # Encrypt and save user
  51. _savedeployconf OPENGEAR_USER "$_opengear_user" 1
  52. _savedeployconf OPENGEAR_HOST "$_opengear_host" 1
  53. fi
  54. _info "Deploying to $_opengear_host"
  55. _cmdstr="sudo echo -e \"set services.https.certificate =$(cat $_cfullchain | base64 -w0)\nset services.https.private_key =$(cat $_ckey | base64 -w0)\npush\" | /usr/unsupported/bin/ogconfig-cli"
  56. _info "will deploy new certificate"
  57. if ! _ssh_remote_cmd "$_cmdstr"; then
  58. return $_err_code
  59. fi
  60. return $_err_code
  61. }
  62. #cmd
  63. _ssh_remote_cmd() {
  64. _cmd="$1"
  65. _secure_debug "Remote commands to execute: $_cmd"
  66. _info "Submitting sequence of commands to remote server by ssh"
  67. # quotations in bash cmd below intended. Squash travis spellcheck error
  68. # shellcheck disable=SC2029
  69. _debug $Le_Deploy_ssh_cmd "$_opengear_user@$_opengear_host" sh -c "'$_cmd'"
  70. $Le_Deploy_ssh_cmd "$_opengear_user@$_opengear_host" sh -c "'$_cmd'"
  71. _err_code="$?"
  72. if [ "$_err_code" != "0" ]; then
  73. _err "Error code $_err_code returned from ssh"
  74. fi
  75. return $_err_code
  76. }