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.9 KiB

  1. APP_NAME=monujo
  2. VERSION=0.0.2
  3. SOURCE_URL="https://docker.0k.io/downloads/$APP_NAME-$VERSION.tar.bz2"
  4. LOCATION="$SERVICE_DATASTORE/opt/apps/$APP_NAME"
  5. CONFIGFILE="$LOCATION/config.json"
  6. monujo:code_init() {
  7. mkdir -p "$LOCATION"
  8. if dir_is_empty "$LOCATION"; then
  9. cd "$LOCATION"
  10. info "First install, downloading '$SOURCE_URL'."
  11. wget -q "$SOURCE_URL" -O file.tar.bz2 || {
  12. err "Couldn't download '$SOURCE_URL'."
  13. rm file.tar.bz2
  14. return 1
  15. }
  16. tar xjf file.tar.bz2 &&
  17. rm file.tar.bz2 &&
  18. chown root:root "$LOCATION" -R
  19. fi
  20. }
  21. export MONUJO_OPTIONS=(
  22. lokapi-host:string
  23. lokapi-db:string
  24. map-url:string
  25. local-password-retention-time:numeric
  26. theme:struct\*
  27. )
  28. export MONUJO_OPTIONS_CONCAT=" ${MONUJO_OPTIONS[*]} "
  29. monujo:code_config_base() {
  30. local service_def
  31. service_def=$(get_compose_service_def "$SERVICE_NAME") || return 1
  32. options=$(e "$service_def" | shyaml get-value -y options) || true
  33. e "$options" |
  34. monujo:json-make > "$CONFIGFILE" || {
  35. err "Failed to make 'config.json'."
  36. return 1
  37. }
  38. }
  39. monujo:json-make() {
  40. local conv="$1" key val
  41. ## XXXvlab: Should probably offer some lib to do this
  42. local sep=
  43. while read-0 key val; do
  44. key=$(e "$key" | shyaml get-value)
  45. case "$MONUJO_OPTIONS_CONCAT" in
  46. *" ${key}:bool "*)
  47. case "${val,,}" in
  48. true|ok|yes|y)
  49. val=true
  50. ;;
  51. false|ko|nok|no|n)
  52. val=false
  53. ;;
  54. *)
  55. die "Invalid value for ${WHITE}$key$NORMAL, please use a boolean value."
  56. ;;
  57. esac
  58. ;;
  59. *" ${key}:numeric "*)
  60. val=$(e "$val" | shyaml get-value)
  61. if ! is_int "$val"; then
  62. err "Invalid value for ${WHITE}$key$NORMAL, please use numeric value."
  63. return 1
  64. fi
  65. ;;
  66. *" ${key}:struct* "*)
  67. val=$(e "$val" | monujo:json-make noconv) || return 1
  68. ;;
  69. *" ${key}:struct "*)
  70. val=$(e "$val" | monujo:json-make) || return 1
  71. ;;
  72. *" ${key}:string "*|*)
  73. val=$(e "$val" | shyaml get-value | jq -Rr tojson)
  74. ;;
  75. esac
  76. if [ -z "$conv" ]; then
  77. key=$(echo "${key//-/_}" | sed 's/_\([a-z0-9]\)/\U\1/g')
  78. fi
  79. printf "$sep%s\0%s" "$key" "$val"
  80. sep="\0\0"
  81. done < <(shyaml key-values-0 -y) |
  82. jq -sR 'split("\u0000\u0000") | map(split("\u0000") | {key: .[0], value: .[1] | fromjson}) | from_entries'
  83. }
  84. monujo:config_merge() {
  85. local old_config new_config="$1"
  86. old_config=$(cat "$CONFIGFILE")
  87. e "$old_config" "$new_config" | jq -s 'reduce .[] as $x ({}; . * $x)' > "$CONFIGFILE"
  88. }