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.

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