fork 0k-charms
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.

171 lines
5.0 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. )
  66. export MONUJO_OPTIONS_CONCAT=" ${MONUJO_OPTIONS[*]} "
  67. monujo:code_config_base() {
  68. local enforce_config="$1" service_def
  69. if [[ "${enforce_config,,}" =~ ^(false|0|no)$ ]]; then
  70. if [ -e "$CONFIGFILE" ]; then
  71. return 0
  72. fi
  73. fi
  74. service_def=$(get_compose_service_def "$SERVICE_NAME") || return 1
  75. options=$(e "$service_def" | shyaml get-value -y options) || true
  76. e "$options" |
  77. monujo:json-make > "$CONFIGFILE" || {
  78. err "Failed to make 'config.json'."
  79. return 1
  80. }
  81. }
  82. monujo:json-make() {
  83. local conv="$1" key val
  84. ## XXXvlab: Should probably offer some lib to do this
  85. local sep=
  86. while read-0 key val; do
  87. key=$(e "$key" | shyaml get-value)
  88. ytype=$(e "$val" | shyaml get-type)
  89. case "$MONUJO_OPTIONS_CONCAT" in
  90. *" ${key}:ignore "*)
  91. continue
  92. ;;
  93. *" ${key}:bool "*)
  94. val=$(e "$val" | shyaml get-value)
  95. case "${val,,}" in
  96. true|ok|yes|y)
  97. val=true
  98. ;;
  99. false|ko|nok|no|n)
  100. val=false
  101. ;;
  102. *)
  103. die "Invalid value for ${WHITE}$key$NORMAL, please use a boolean value."
  104. ;;
  105. esac
  106. ;;
  107. *" ${key}:numeric "*)
  108. val=$(e "$val" | shyaml get-value)
  109. if ! is_int "$val"; then
  110. err "Invalid value for ${WHITE}$key$NORMAL, please use numeric value."
  111. return 1
  112. fi
  113. ;;
  114. *" ${key}:struct* "*)
  115. val=$(e "$val" | monujo:json-make noconv) || return 1
  116. ;;
  117. *" ${key}:struct "*)
  118. val=$(e "$val" | monujo:json-make) || return 1
  119. ;;
  120. *" ${key}:string "*|*)
  121. :
  122. ;;
  123. esac
  124. case "$ytype" in
  125. struct|sequence)
  126. val=$(e "$val" | monujo:json-make noconv) || return 1
  127. ;;
  128. bool)
  129. val=$(e "$val" | shyaml get-value)
  130. ## shyaml outputs actually python's True/False,
  131. ## json need the lowercase version.
  132. val=${val,,}
  133. ;;
  134. str)
  135. val=$(e "$val" | shyaml get-value | jq -Rr tojson)
  136. ;;
  137. *)
  138. echo "YTYPE: $ytype" >&2
  139. echo "VAL: $val" >&2
  140. val=$(e "$val" | shyaml get-value | jq -r tojson)
  141. ;;
  142. esac
  143. if [ -z "$conv" ]; then
  144. key=$(echo "${key//-/_}" | sed 's/_\([a-z0-9]\)/\U\1/g')
  145. fi
  146. printf "$sep%s\0%s" "$key" "$val"
  147. sep="\0\0"
  148. done < <(shyaml key-values-0 -y) |
  149. jq -sR 'split("\u0000\u0000") | map(split("\u0000") | {key: .[0], value: .[1] | fromjson}) | from_entries'
  150. }
  151. monujo:config_merge() {
  152. local old_config new_config="$1"
  153. old_config=$(cat "$CONFIGFILE")
  154. e "$old_config" "$new_config" | jq -s 'reduce .[] as $x ({}; . * $x)' > "$CONFIGFILE"
  155. }