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.

124 lines
3.7 KiB

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