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.

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