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.

131 lines
3.6 KiB

  1. #!/bin/bash
  2. . /etc/shlib
  3. [[ "${BASH_SOURCE[0]}" != "${0}" ]] && SOURCED=true
  4. include common
  5. include pretty
  6. include cmdline
  7. desc='Adds YAML key quite crudely in given compose.yml.'
  8. help="\
  9. $WHITE$exname$NORMAL will find a add or replace SSH key to
  10. given identifier for rsync-backup-target charm.
  11. For now, it only use detection of '## INSERTION-POINT' to manage
  12. edition of 'compose.yml', and verification it didn't break anything
  13. before overwriting.
  14. "
  15. [ "$SOURCED" ] && return 0
  16. ##
  17. ## Command line processing
  18. ##
  19. # remove all lines from regex to regex (not included).
  20. remove_lines_from_to() {
  21. local from="$1" to="$2"
  22. sed -r "/$from/,/$to/{/$to/"'!'"d};/$from/d"
  23. }
  24. check_valid_yaml() {
  25. shyaml get-value >/dev/null 2>&1;
  26. }
  27. cmdline.spec.gnu
  28. cmdline.spec.reporting
  29. service_name=${SERVICE_NAME:-rsync-backup-target}
  30. compose_file=${COMPOSE_FILE:-/etc/compose/compose.yml}
  31. cmdline.spec::valued:--compose-file,-f:usage() {
  32. echo "Compose file location. Defaults to '/etc/compose/compose.yml'"; }
  33. cmdline.spec::valued:--compose-file,-f:run() { compose_file="$1"; }
  34. cmdline.spec::valued:--service-name,-s:usage() {
  35. echo "YAML service name in compose file to check for existence of key. Defaults to 'rsync-backup-target'"; }
  36. cmdline.spec::valued:--service-name,-s:run() { service_name="$1"; }
  37. cmdline.spec::cmd:__main__:run() {
  38. : :posarg: DOMAIN 'domain identifier'
  39. : :posarg: SSH_PUBLIC_KEY 'ssh public key'
  40. if ! existing_domains=$(shyaml keys "${service_name//./\\.}.options.keys" < "$compose_file"); then
  41. err "Couldn't query file '$compose_file' for keys of" \
  42. "service ${DARKYELLOW}${service_name}${NORMAL}."
  43. exit 1
  44. fi
  45. content=$(cat "$compose_file")
  46. if echo "$existing_domains" | grep "^${DOMAIN}$" >/dev/null 2>&1; then
  47. content=$(echo "$content" | remove_lines_from_to '^ '"${DOMAIN//./\\.}"': ".*\\$' \
  48. '^ ([A-Za-z0-9.-]+): "')
  49. if [ -z "$content" ]; then
  50. err "Didn't manage to remove key to compose file '$DOMAIN' in '$compose_file'."
  51. exit 1
  52. fi
  53. if [ "$content" == "$(cat "$compose_file")" ]; then
  54. err "Couldn't remove previous key for '$DOMAIN' in '$compose_file'."
  55. exit 1
  56. fi
  57. ## check we didn't break yaml
  58. if ! echo "$content" | check_valid_yaml; then
  59. err "Couldn't safely remove previous key for '$DOMAIN' in '$compose_file'."
  60. exit 1
  61. fi
  62. fi
  63. excerpt=$(cat <<EOF
  64. $DOMAIN: "$(
  65. echo "$SSH_PUBLIC_KEY" |
  66. fold -w 67 | sed -r 's/$/\\/g;2,$ s/^/ /g;$,$ s/\\$//g')"
  67. EOF
  68. )
  69. excerpt="${excerpt//\\/\\\\\\}"
  70. content="$(echo "$content" | sed -r '/^ ## INSERTION-POINT/a\'"${excerpt}")"
  71. if [ -z "$content" ]; then
  72. err "Didn't manage to add key to compose file '$DOMAIN' in '$compose_file'."
  73. exit 1
  74. fi
  75. if ! echo "$content" | check_valid_yaml; then
  76. err "Couldn't safely add key to compose file '$DOMAIN' in '$compose_file'."
  77. exit 1
  78. fi
  79. if diff=$(diff -u "$compose_file" <(echo "$content")); then
  80. echo "Key was already setup."
  81. exit 0
  82. fi
  83. echo "${WHITE}Applying these changes:${NORMAL}"
  84. echo "$diff"
  85. echo "$content" > "$compose_file"
  86. ## reloading (could be much faster)
  87. compose --debug down && compose --debug up
  88. if [ "$?" == 0 ]; then
  89. echo "Added key, and restarted service ${DARKYELLOW}$service_name${NORMAL}."
  90. else
  91. echo "something went wrong ! Should check the state of '$DOMAIN' !!"
  92. exit 1
  93. fi
  94. }
  95. cmdline::parse "$@"