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.

96 lines
3.2 KiB

  1. YML_CONFIG_PATH=/conf.yml
  2. LOCAL_YML_CONFIG_PATH="$SERVICE_CONFIGSTORE$YML_CONFIG_PATH"
  3. HOST_YML_CONFIG_PATH="$HOST_CONFIGSTORE/$SERVICE_NAME$YML_CONFIG_PATH"
  4. CONFIG_PATH=/setup.ts
  5. LOCAL_CONFIG_PATH="$SERVICE_CONFIGSTORE$CONFIG_PATH"
  6. HOST_CONFIG_PATH="$HOST_CONFIGSTORE/$SERVICE_NAME$CONFIG_PATH"
  7. DEST_PATH=/var/www/cyclos-ui
  8. LOCAL_DEST_PATH="$SERVICE_DATASTORE$DEST_PATH"
  9. HOST_DEST_PATH="$HOST_DATASTORE/$SERVICE_NAME$DEST_PATH"
  10. cyclos_ui.build_builder_image() {
  11. local last_line
  12. if out=$(cd "$CHARM_PATH/misc/builder" && docker build . 2>&1); then
  13. last_line="${out##*$'\n'}"
  14. if [[ "${last_line}" =~ ^"Successfully built "[a-f0-9]+$ ]]; then
  15. echo "${last_line##* }"
  16. return 0
  17. else
  18. err "Couldn't find image id:"
  19. fi
  20. else
  21. err "Failed to build the builder image:"
  22. fi
  23. e "$out" | prefix " ${DARKGRAY}|${NORMAL} " >&2
  24. return 1
  25. }
  26. cyclos_ui.make_base_config_file() {
  27. local cfg_file_content builder_image_id
  28. # builder_image_id=$(cyclos_ui.build_builder_image) || return 1
  29. # cfg_file_content=$(docker run --rm "$builder_image_id" cat src/app/setup.ts) || {
  30. # err "Couldn't access 'src/app/setup.ts' in given image."
  31. # return 1
  32. # }
  33. service_def=$(get_compose_service_def "$SERVICE_NAME") || return 1
  34. cfg_file_content=$(echo "$service_def" | shyaml -y get-value "options" 2>/dev/null) || true
  35. mkdir -p "${LOCAL_YML_CONFIG_PATH%/*}" &&
  36. e "$cfg_file_content" > "$LOCAL_YML_CONFIG_PATH"
  37. }
  38. cyclos_ui.generate_website() {
  39. local builder_image_id hash_file="$LOCAL_DEST_PATH/.hash"
  40. hash=$(hash_get < "$LOCAL_YML_CONFIG_PATH")
  41. if [ -f "${hash_file}" ]; then
  42. if [[ "$(cat "${hash_file}")" == "$hash" ]]; then
  43. return 0
  44. else
  45. [ -d "$LOCAL_DEST_PATH" ] && echo rm -rf "${LOCAL_DEST_PATH}"
  46. fi
  47. fi
  48. builder_image_id=$(cyclos_ui.build_builder_image) || return 1
  49. cfg_file_content=$(docker run --rm "$builder_image_id" cat src/app/setup.ts) || {
  50. err "Couldn't access 'src/app/setup.ts' in given image."
  51. return 1
  52. }
  53. ## remove last '}'
  54. cfg_file_content="${cfg_file_content%\}*}"
  55. while read-0 k v; do
  56. json=$(e "$v" | yaml2json) || {
  57. err "Failed to correct yaml to json"
  58. return 1
  59. }
  60. cfg_file_content+=" Configuration.$(e "$k" | shyaml get-value) = $json;"$'\n'""
  61. done < <(cat "$LOCAL_YML_CONFIG_PATH" | shyaml -y key-values-0)
  62. cfg_file_content+="}"
  63. mkdir -p "${LOCAL_CONFIG_PATH%/*}" &&
  64. e "$cfg_file_content" > "$LOCAL_CONFIG_PATH"
  65. docker run --rm \
  66. -v "$HOST_CONFIG_PATH:/opt/apps/cyclos-ui/src/app/setup.ts:ro" \
  67. -v "$HOST_DEST_PATH:/opt/apps/cyclos-ui/dist:rw" \
  68. "${builder_image_id}" \
  69. npm run build >&2 || {
  70. err "Failed to build new sources."
  71. return 1
  72. }
  73. e "$hash" > "$hash_file"
  74. }
  75. cyclos_ui.conf_add() {
  76. local yaml="$1"
  77. prev=$([ -f "$LOCAL_YML_CONFIG_PATH" ] && cat "$LOCAL_YML_CONFIG_PATH")
  78. if ! out=$(merge_yaml_str "$prev" "$yaml"); then
  79. err "Couldn't merge new configuration."
  80. exit 1
  81. fi
  82. mkdir -p "${LOCAL_CONFIG_PATH%/*}" &&
  83. e "$out" > "$LOCAL_YML_CONFIG_PATH"
  84. }