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.

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