fork 0k-charms
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.

120 lines
3.6 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. ytype=$(e "$val" | shyaml get-type)
  46. case "$MONUJO_OPTIONS_CONCAT" in
  47. *" ${key}:bool "*)
  48. val=$(e "$val" | shyaml get-value)
  49. case "${val,,}" in
  50. true|ok|yes|y)
  51. val=true
  52. ;;
  53. false|ko|nok|no|n)
  54. val=false
  55. ;;
  56. *)
  57. die "Invalid value for ${WHITE}$key$NORMAL, please use a boolean value."
  58. ;;
  59. esac
  60. ;;
  61. *" ${key}:numeric "*)
  62. val=$(e "$val" | shyaml get-value)
  63. if ! is_int "$val"; then
  64. err "Invalid value for ${WHITE}$key$NORMAL, please use numeric value."
  65. return 1
  66. fi
  67. ;;
  68. *" ${key}:struct* "*)
  69. val=$(e "$val" | monujo:json-make noconv) || return 1
  70. ;;
  71. *" ${key}:struct "*)
  72. val=$(e "$val" | monujo:json-make) || return 1
  73. ;;
  74. *" ${key}:string "*|*)
  75. :
  76. ;;
  77. esac
  78. case "$ytype" in
  79. struct|sequence)
  80. :
  81. ;;
  82. bool)
  83. val=$(e "$val" | shyaml get-value)
  84. ## shyaml outputs actually python's True/False,
  85. ## json need the lowercase version.
  86. val=${val,,}
  87. ;;
  88. str)
  89. val=$(e "$val" | shyaml get-value | jq -Rr tojson)
  90. ;;
  91. *)
  92. echo "YTYPE: $ytype" >&2
  93. echo "VAL: $val" >&2
  94. val=$(e "$val" | shyaml get-value | jq -r tojson)
  95. ;;
  96. esac
  97. if [ -z "$conv" ]; then
  98. key=$(echo "${key//-/_}" | sed 's/_\([a-z0-9]\)/\U\1/g')
  99. fi
  100. printf "$sep%s\0%s" "$key" "$val"
  101. sep="\0\0"
  102. done < <(shyaml key-values-0 -y) |
  103. jq -sR 'split("\u0000\u0000") | map(split("\u0000") | {key: .[0], value: .[1] | fromjson}) | from_entries'
  104. }
  105. monujo:config_merge() {
  106. local old_config new_config="$1"
  107. old_config=$(cat "$CONFIGFILE")
  108. e "$old_config" "$new_config" | jq -s 'reduce .[] as $x ({}; . * $x)' > "$CONFIGFILE"
  109. }