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.

151 lines
4.5 KiB

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