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.

173 lines
5.1 KiB

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