file_ops.sh 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379
  1. # File operations for interactive setup.
  2. # Registry of temp files created during this session; cleaned up on exit.
  3. _FILE_OPS_CLEANUP_TMP=()
  4. declare -A _FILE_OPS_VOLUME_BLOCKS=()
  5. declare -a _FILE_OPS_VOLUME_ORDER=()
  6. _WIZARD_MANAGED_SERVICES_MARKER="# __WIZARD_MANAGED_SERVICES__"
  7. _file_ops_cleanup() {
  8. local f
  9. for f in "${_FILE_OPS_CLEANUP_TMP[@]:-}"; do
  10. rm -f "$f" 2>/dev/null || true
  11. done
  12. }
  13. trap '_file_ops_cleanup' EXIT INT TERM
  14. format_env_value() {
  15. local value="$1"
  16. local escaped
  17. if [[ -z "$value" ]]; then
  18. printf ''
  19. return
  20. fi
  21. if [[ "$value" =~ [[:space:]] || "$value" == *"\""* || "$value" == *"$"* || "$value" == *"#"* ]]; then
  22. # Prefer single quotes when quoting is required so generated .env values
  23. # match env.example style and remain Compose-friendly. Fall back to
  24. # double quotes only when the value itself contains a single quote.
  25. if [[ "$value" != *"'"* ]]; then
  26. printf "'%s'" "$value"
  27. return
  28. fi
  29. # Double-quoted .env values only need escaping for backslash and double quote.
  30. # Do not escape '$': python-dotenv preserves plain '$' literally, while '\$'
  31. # changes the loaded value.
  32. # '#' in unquoted values is treated as a comment by python-dotenv, so any
  33. # value containing '#' must be quoted.
  34. escaped="${value//\\/\\\\}"
  35. escaped="${escaped//\"/\\\"}"
  36. printf '"%s"' "$escaped"
  37. return
  38. fi
  39. printf '%s' "$value"
  40. }
  41. backup_env_file() {
  42. local env_file="${1:-${REPO_ROOT:-.}/.env}"
  43. local backup_file=""
  44. if [[ -f "$env_file" ]]; then
  45. backup_file="${env_file}.backup.$(date +%Y%m%d_%H%M%S)"
  46. if ! cp "$env_file" "$backup_file"; then
  47. format_error "Failed to back up ${env_file} to ${backup_file}." "Check disk space and file permissions."
  48. return 1
  49. fi
  50. printf '%s' "$backup_file"
  51. fi
  52. }
  53. backup_compose_file() {
  54. local compose_file="${1:-}"
  55. local repo_root="${REPO_ROOT:-.}"
  56. local backup_file=""
  57. if [[ -z "$compose_file" ]]; then
  58. compose_file="$(find_generated_compose_file)"
  59. fi
  60. if [[ -z "$compose_file" || ! -f "$compose_file" ]]; then
  61. return 0
  62. fi
  63. backup_file="${repo_root}/docker-compose.backup$(date +%Y%m%d_%H%M%S).yml"
  64. if ! cp "$compose_file" "$backup_file"; then
  65. format_error "Failed to back up ${compose_file} to ${backup_file}." \
  66. "Check disk space and file permissions."
  67. return 1
  68. fi
  69. printf '%s' "$backup_file"
  70. }
  71. stage_ssl_assets() {
  72. local cert_source="$1"
  73. local key_source="$2"
  74. local certs_dir="${REPO_ROOT:-.}/data/certs"
  75. local cert_target=""
  76. local key_target=""
  77. mkdir -p "$certs_dir"
  78. if [[ -n "$cert_source" ]]; then
  79. cert_target="${certs_dir}/$(resolve_staged_ssl_basename "cert" "$cert_source" "$key_source")"
  80. if [[ ! -e "$cert_target" || ! "$cert_source" -ef "$cert_target" ]]; then
  81. cp "$cert_source" "$cert_target"
  82. fi
  83. fi
  84. if [[ -n "$key_source" ]]; then
  85. key_target="${certs_dir}/$(resolve_staged_ssl_basename "key" "$key_source" "$cert_source")"
  86. if [[ ! -e "$key_target" || ! "$key_source" -ef "$key_target" ]]; then
  87. cp "$key_source" "$key_target"
  88. fi
  89. fi
  90. }
  91. stage_redis_config_asset() {
  92. local template_path="${TEMPLATES_DIR:-}/redis.conf.template"
  93. local config_dir="${REPO_ROOT:-.}/data/config"
  94. local config_target="${config_dir}/redis.conf"
  95. if [[ -z "$template_path" || ! -f "$template_path" ]]; then
  96. format_error "Missing Redis config template: ${template_path}" \
  97. "Restore scripts/setup/templates/redis.conf.template before rerunning setup."
  98. return 1
  99. fi
  100. mkdir -p "$config_dir"
  101. if [[ -e "$config_target" ]]; then
  102. log_info "Preserving existing Redis config at ${config_target}"
  103. return 0
  104. fi
  105. if ! cp "$template_path" "$config_target"; then
  106. format_error "Failed to stage Redis config at ${config_target}" \
  107. "Check file permissions and available disk space, then rerun setup."
  108. return 1
  109. fi
  110. log_success "Staged Redis config at ${config_target}"
  111. }
  112. resolve_staged_ssl_basename() {
  113. local asset_type="$1"
  114. local source_path="$2"
  115. local peer_path="${3:-}"
  116. local basename_value=""
  117. local peer_basename=""
  118. basename_value="$(basename "$source_path")"
  119. if [[ -n "$peer_path" ]]; then
  120. peer_basename="$(basename "$peer_path")"
  121. if [[ "$basename_value" == "$peer_basename" ]]; then
  122. printf '%s-%s' "$asset_type" "$basename_value"
  123. return 0
  124. fi
  125. fi
  126. printf '%s' "$basename_value"
  127. }
  128. append_preserved_non_template_env_lines() {
  129. local template_file="$1"
  130. local existing_env_file="$2"
  131. local output_file="$3"
  132. local line key
  133. local in_preserved_section="no"
  134. local line_is_commented_env="no"
  135. local template_in_preserved_section="no"
  136. local template_has_preserved_section="no"
  137. local old_has_preserved_section="no"
  138. local preserved_header="### ----- Preserved custom environment variables from previous .env -----"
  139. local preserved_notice="### ----- Comments in this session will persist across regenerations -----"
  140. local -a pending_lines=()
  141. local -a preserved_payload=()
  142. local -a discovered_payload=()
  143. local -a template_preserved_payload=()
  144. local -a effective_preserved_payload=()
  145. local -A ignored_keys=(
  146. ["LIGHTRAG_SETUP_PROFILE"]=1
  147. )
  148. local -A template_keys=()
  149. while IFS= read -r line || [[ -n "$line" ]]; do
  150. if [[ "$line" == "$preserved_header" ]]; then
  151. template_has_preserved_section="yes"
  152. template_in_preserved_section="yes"
  153. continue
  154. fi
  155. if [[ "$template_in_preserved_section" == "yes" && "$line" == "$preserved_notice" ]]; then
  156. continue
  157. fi
  158. if [[ "$template_in_preserved_section" == "yes" ]]; then
  159. template_preserved_payload+=("$line")
  160. fi
  161. if [[ "$line" =~ ^[A-Za-z0-9_]+= ]]; then
  162. template_keys["${line%%=*}"]=1
  163. elif [[ "$line" =~ ^#[[:space:]]*([A-Za-z0-9_]+)=(.*)$ ]]; then
  164. template_keys["${BASH_REMATCH[1]}"]=1
  165. fi
  166. done < "$template_file"
  167. if [[ -f "$existing_env_file" ]]; then
  168. while IFS= read -r line || [[ -n "$line" ]]; do
  169. if [[ "$line" == "$preserved_header" ]]; then
  170. in_preserved_section="yes"
  171. old_has_preserved_section="yes"
  172. pending_lines=()
  173. continue
  174. fi
  175. if [[ "$in_preserved_section" == "yes" && "$line" == "$preserved_notice" ]]; then
  176. continue
  177. fi
  178. key=""
  179. line_is_commented_env="no"
  180. if [[ "$line" =~ ^([A-Za-z0-9_]+)= ]]; then
  181. key="${BASH_REMATCH[1]}"
  182. elif [[ "$line" =~ ^#[[:space:]]*([A-Za-z0-9_]+)=(.*)$ ]]; then
  183. key="${BASH_REMATCH[1]}"
  184. line_is_commented_env="yes"
  185. fi
  186. if [[ -z "$key" ]]; then
  187. if [[ "$in_preserved_section" == "yes" ]]; then
  188. pending_lines+=("$line")
  189. fi
  190. continue
  191. fi
  192. if [[ -n "${ignored_keys[$key]+set}" ]]; then
  193. if [[ "$in_preserved_section" == "yes" ]] && ((${#pending_lines[@]} > 0)); then
  194. preserved_payload+=("${pending_lines[@]}")
  195. fi
  196. pending_lines=()
  197. continue
  198. fi
  199. if [[ -z "${template_keys[$key]+set}" || \
  200. ("$in_preserved_section" == "yes" && "$line_is_commented_env" == "yes") ]]; then
  201. if ((${#pending_lines[@]} > 0)); then
  202. if [[ "$in_preserved_section" == "yes" ]]; then
  203. preserved_payload+=("${pending_lines[@]}")
  204. fi
  205. fi
  206. if [[ "$in_preserved_section" == "yes" ]]; then
  207. preserved_payload+=("$line")
  208. else
  209. discovered_payload+=("$line")
  210. fi
  211. elif [[ "$in_preserved_section" == "yes" && ${#pending_lines[@]} -gt 0 ]]; then
  212. preserved_payload+=("${pending_lines[@]}")
  213. fi
  214. pending_lines=()
  215. done < "$existing_env_file"
  216. if ((${#pending_lines[@]} > 0)); then
  217. preserved_payload+=("${pending_lines[@]}")
  218. fi
  219. fi
  220. effective_preserved_payload=("${preserved_payload[@]}")
  221. if [[ "$template_has_preserved_section" == "yes" ]]; then
  222. printf '%s\n%s\n' "$preserved_header" "$preserved_notice" >> "$output_file"
  223. if [[ "$old_has_preserved_section" != "yes" ]] && ((${#template_preserved_payload[@]} > 0)); then
  224. printf '%s\n' "${template_preserved_payload[@]}" >> "$output_file"
  225. fi
  226. if ((${#effective_preserved_payload[@]} > 0)); then
  227. printf '%s\n' "${effective_preserved_payload[@]}" >> "$output_file"
  228. fi
  229. if ((${#discovered_payload[@]} > 0)); then
  230. printf '%s\n' "${discovered_payload[@]}" >> "$output_file"
  231. fi
  232. return 0
  233. fi
  234. if ((${#effective_preserved_payload[@]} == 0 && ${#discovered_payload[@]} == 0)); then
  235. return 0
  236. fi
  237. printf '\n%s\n%s\n' "$preserved_header" "$preserved_notice" >> "$output_file"
  238. if ((${#effective_preserved_payload[@]} > 0)); then
  239. printf '%s\n' "${effective_preserved_payload[@]}" >> "$output_file"
  240. fi
  241. if ((${#discovered_payload[@]} > 0)); then
  242. printf '%s\n' "${discovered_payload[@]}" >> "$output_file"
  243. fi
  244. }
  245. generate_env_file() {
  246. local template_file="${1:-${REPO_ROOT:-.}/env.example}"
  247. local output_file="${2:-${REPO_ROOT:-.}/.env}"
  248. local tmp_file="${output_file}.tmp"
  249. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  250. local line key value
  251. local preserved_header="### ----- Preserved custom environment variables from previous .env -----"
  252. local in_template_preserved_section="no"
  253. local -A written_keys=()
  254. local -A match_write_keys=()
  255. if [[ ! -f "$template_file" ]]; then
  256. echo "env.example not found at $template_file" >&2
  257. return 1
  258. fi
  259. # Pre-scan: identify commented keys whose value exactly matches the ENV_VALUE.
  260. # When a match exists, the active value is written only at that matching line,
  261. # leaving all other commented examples intact.
  262. local _prescan_key _prescan_val _prescan_env_val _prescan_fmt
  263. while IFS= read -r line || [[ -n "$line" ]]; do
  264. if [[ "$line" == "$preserved_header" ]]; then
  265. in_template_preserved_section="yes"
  266. continue
  267. fi
  268. if [[ "$in_template_preserved_section" == "yes" ]]; then
  269. continue
  270. fi
  271. if [[ "$line" =~ ^#[[:space:]]*([A-Za-z0-9_]+)=(.*)$ ]]; then
  272. _prescan_key="${BASH_REMATCH[1]}"
  273. _prescan_val="${BASH_REMATCH[2]}"
  274. if [[ -z "${match_write_keys[$_prescan_key]+set}" && -n "${ENV_VALUES[$_prescan_key]+set}" ]]; then
  275. _prescan_env_val="${ENV_VALUES[$_prescan_key]}"
  276. _prescan_fmt="$(format_env_value "$_prescan_env_val" "$_prescan_key")"
  277. if [[ "$_prescan_val" == "$_prescan_env_val" || "$_prescan_val" == "$_prescan_fmt" ]]; then
  278. match_write_keys["$_prescan_key"]=1
  279. fi
  280. fi
  281. fi
  282. done < "$template_file"
  283. : > "$tmp_file"
  284. in_template_preserved_section="no"
  285. while IFS= read -r line || [[ -n "$line" ]]; do
  286. if [[ "$line" == "$preserved_header" ]]; then
  287. in_template_preserved_section="yes"
  288. continue
  289. fi
  290. if [[ "$in_template_preserved_section" == "yes" ]]; then
  291. continue
  292. fi
  293. if [[ "$line" =~ ^[A-Za-z0-9_]+= ]]; then
  294. key="${line%%=*}"
  295. if [[ -z "${written_keys[$key]+set}" ]]; then
  296. if [[ -n "${ENV_VALUES[$key]+set}" ]]; then
  297. value="${ENV_VALUES[$key]}"
  298. local _fmt_active_val
  299. _fmt_active_val="$(format_env_value "$value" "$key")"
  300. printf '%s=%s\n' "$key" "$_fmt_active_val" >> "$tmp_file"
  301. local _orig_tmpl_val="${line#*=}"
  302. if [[ "$_orig_tmpl_val" != "$value" && "$_orig_tmpl_val" != "$_fmt_active_val" ]]; then
  303. printf '# %s\n' "$line" >> "$tmp_file"
  304. fi
  305. else
  306. printf '%s\n' "$line" >> "$tmp_file"
  307. fi
  308. written_keys["$key"]=1
  309. else
  310. if [[ -n "${ENV_VALUES[$key]+set}" ]]; then
  311. printf '# %s\n' "$line" >> "$tmp_file"
  312. else
  313. printf '%s\n' "$line" >> "$tmp_file"
  314. fi
  315. fi
  316. elif [[ "$line" =~ ^#[[:space:]]*([A-Za-z0-9_]+)=(.*)$ ]]; then
  317. key="${BASH_REMATCH[1]}"
  318. local _commented_val="${BASH_REMATCH[2]}"
  319. if [[ -z "${written_keys[$key]+set}" && -n "${ENV_VALUES[$key]+set}" ]]; then
  320. value="${ENV_VALUES[$key]}"
  321. if [[ -n "${match_write_keys[$key]+set}" ]]; then
  322. # A commented line matching the ENV value exists; only activate at that line.
  323. local _fmt_val
  324. _fmt_val="$(format_env_value "$value" "$key")"
  325. if [[ "$_commented_val" == "$value" || "$_commented_val" == "$_fmt_val" ]]; then
  326. printf '%s=%s\n' "$key" "$_fmt_val" >> "$tmp_file"
  327. written_keys["$key"]=1
  328. else
  329. printf '%s\n' "$line" >> "$tmp_file"
  330. fi
  331. else
  332. # No matching commented line; fall back to activating at first occurrence.
  333. printf '%s=%s\n' "$key" "$(format_env_value "$value" "$key")" >> "$tmp_file"
  334. written_keys["$key"]=1
  335. fi
  336. else
  337. printf '%s\n' "$line" >> "$tmp_file"
  338. fi
  339. else
  340. printf '%s\n' "$line" >> "$tmp_file"
  341. fi
  342. done < "$template_file"
  343. append_preserved_non_template_env_lines "$template_file" "$output_file" "$tmp_file"
  344. mv "$tmp_file" "$output_file"
  345. }
  346. # All environment keys the wizard may inject into the lightrag service via
  347. # COMPOSE_ENV_OVERRIDES. Used to remove stale entries before re-injection so
  348. # keys no longer needed are not left behind in the compose file.
  349. _WIZARD_COMPOSE_LIGHTRAG_KEYS=(
  350. "EMBEDDING_BINDING_HOST" "RERANK_BINDING_HOST" "LLM_BINDING_HOST"
  351. "REDIS_URI" "MONGO_URI" "NEO4J_URI" "MILVUS_URI" "QDRANT_URL" "MEMGRAPH_URI" "OPENSEARCH_HOSTS"
  352. "POSTGRES_HOST" "POSTGRES_PORT" "PORT" "HOST" "SSL_CERTFILE" "SSL_KEYFILE"
  353. "WORKING_DIR" "INPUT_DIR" "PROMPT_DIR"
  354. )
  355. _managed_service_root_name() {
  356. local service_name="$1"
  357. case "$service_name" in
  358. postgres|neo4j|mongodb|redis|qdrant|memgraph|vllm-embed|vllm-rerank)
  359. printf '%s' "$service_name"
  360. ;;
  361. opensearch|dashboards)
  362. printf 'opensearch'
  363. ;;
  364. milvus|milvus-etcd|milvus-minio)
  365. printf 'milvus'
  366. ;;
  367. *)
  368. printf ''
  369. ;;
  370. esac
  371. }
  372. _managed_volume_root_name() {
  373. local volume_name="$1"
  374. case "$volume_name" in
  375. postgres_data)
  376. printf 'postgres'
  377. ;;
  378. neo4j_data)
  379. printf 'neo4j'
  380. ;;
  381. mongo_data|mongo_config_data|mongo_mongot_data)
  382. printf 'mongodb'
  383. ;;
  384. redis_data)
  385. printf 'redis'
  386. ;;
  387. milvus_data|milvus-etcd_data|milvus-minio_data)
  388. printf 'milvus'
  389. ;;
  390. qdrant_data)
  391. printf 'qdrant'
  392. ;;
  393. memgraph_data)
  394. printf 'memgraph'
  395. ;;
  396. opensearch_data)
  397. printf 'opensearch'
  398. ;;
  399. vllm_rerank_cache)
  400. printf 'vllm-rerank'
  401. ;;
  402. vllm_embed_cache)
  403. printf 'vllm-embed'
  404. ;;
  405. *)
  406. printf ''
  407. ;;
  408. esac
  409. }
  410. _should_rewrite_wizard_managed_root_service() {
  411. local root_service="$1"
  412. if [[ -z "$root_service" ]]; then
  413. return 1
  414. fi
  415. if [[ "${FORCE_REWRITE_COMPOSE:-no}" == "yes" ]]; then
  416. return 0
  417. fi
  418. if [[ -z "${DOCKER_SERVICE_SET[$root_service]+set}" ]]; then
  419. return 0
  420. fi
  421. if [[ -n "${COMPOSE_REWRITE_SERVICE_SET[$root_service]+set}" ]]; then
  422. return 0
  423. fi
  424. return 1
  425. }
  426. _should_preserve_wizard_managed_root_service() {
  427. local root_service="$1"
  428. if [[ -z "$root_service" || "${FORCE_REWRITE_COMPOSE:-no}" == "yes" ]]; then
  429. return 1
  430. fi
  431. if [[ -z "${DOCKER_SERVICE_SET[$root_service]+set}" ]]; then
  432. return 1
  433. fi
  434. if [[ -n "${COMPOSE_REWRITE_SERVICE_SET[$root_service]+set}" ]]; then
  435. return 1
  436. fi
  437. return 0
  438. }
  439. _existing_managed_root_service_present() {
  440. local root_service="$1"
  441. [[ -n "$root_service" && -n "${EXISTING_MANAGED_ROOT_SERVICE_SET[$root_service]+set}" ]]
  442. }
  443. _refresh_existing_managed_root_service_set_from_compose() {
  444. local compose_file="$1"
  445. local service_name
  446. EXISTING_MANAGED_ROOT_SERVICE_SET=()
  447. if [[ -z "$compose_file" || ! -f "$compose_file" ]]; then
  448. return 0
  449. fi
  450. while IFS= read -r service_name; do
  451. EXISTING_MANAGED_ROOT_SERVICE_SET["$service_name"]=1
  452. done < <(detect_managed_root_services "$compose_file")
  453. }
  454. _is_wizard_managed_root_service_name() {
  455. local service_name="$1"
  456. [[ -n "$(_managed_service_root_name "$service_name")" ]]
  457. }
  458. _is_wizard_managed_service_name() {
  459. local service_name="$1"
  460. [[ -n "$(_managed_service_root_name "$service_name")" ]]
  461. }
  462. _is_wizard_managed_volume_name() {
  463. local volume_name="$1"
  464. [[ -n "$(_managed_volume_root_name "$volume_name")" ]]
  465. }
  466. # Remove wizard-managed keys from the lightrag service's environment block,
  467. # leaving any user-added keys intact.
  468. _strip_lightrag_wizard_environment_keys() {
  469. local compose_file="$1"
  470. local tmp_file="${compose_file}.strip-wizard-keys"
  471. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  472. local line key wk list_entry
  473. local in_lightrag="no"
  474. local in_environment="no"
  475. : > "$tmp_file"
  476. while IFS= read -r line || [[ -n "$line" ]]; do
  477. if [[ "$line" == " lightrag:" ]]; then
  478. in_lightrag="yes"
  479. in_environment="no"
  480. elif [[ "$in_lightrag" == "yes" && "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != " lightrag:" ]]; then
  481. in_lightrag="no"
  482. in_environment="no"
  483. fi
  484. if [[ "$in_lightrag" == "yes" && "$line" == " environment:" ]]; then
  485. in_environment="yes"
  486. printf '%s\n' "$line" >> "$tmp_file"
  487. continue
  488. fi
  489. if [[ "$in_lightrag" == "yes" && "$in_environment" == "yes" ]]; then
  490. if [[ "$line" =~ ^[[:space:]]{6}([A-Z0-9_]+): ]]; then
  491. key="${BASH_REMATCH[1]}"
  492. for wk in "${_WIZARD_COMPOSE_LIGHTRAG_KEYS[@]}"; do
  493. if [[ "$key" == "$wk" ]]; then
  494. continue 2 # skip this wizard-managed key
  495. fi
  496. done
  497. elif [[ "$line" =~ ^[[:space:]]{6}-[[:space:]](.+)$ ]]; then
  498. list_entry="$(_strip_wrapping_quotes "${BASH_REMATCH[1]}")"
  499. key="${list_entry%%=*}"
  500. if [[ "$key" =~ ^[A-Z0-9_]+$ ]]; then
  501. for wk in "${_WIZARD_COMPOSE_LIGHTRAG_KEYS[@]}"; do
  502. if [[ "$key" == "$wk" ]]; then
  503. continue 2 # skip this wizard-managed key
  504. fi
  505. done
  506. fi
  507. elif [[ -z "$line" ]]; then
  508. continue # skip blank lines inside the environment block
  509. elif [[ ! "$line" =~ ^[[:space:]]{6} ]]; then
  510. in_environment="no"
  511. fi
  512. fi
  513. printf '%s\n' "$line" >> "$tmp_file"
  514. done < "$compose_file"
  515. mv "$tmp_file" "$compose_file"
  516. }
  517. _write_service_environment_entries() {
  518. local tmp_file="$1"
  519. local style="$2"
  520. shift 2
  521. local entries=("$@")
  522. local key value
  523. for entry in "${entries[@]}"; do
  524. key="${entry%%=*}"
  525. value="${entry#*=}"
  526. if [[ "$style" == "list" ]]; then
  527. if [[ "$value" == "${_COMPOSE_RAW_VALUE_PREFIX}"* ]]; then
  528. printf ' - %s=%s\n' "$key" "${value#${_COMPOSE_RAW_VALUE_PREFIX}}" >> "$tmp_file"
  529. else
  530. printf ' - %s\n' "$(format_yaml_value "${key}=${value}")" >> "$tmp_file"
  531. fi
  532. else
  533. printf ' %s: %s\n' "$key" "$(format_compose_environment_value "$value")" >> "$tmp_file"
  534. fi
  535. done
  536. }
  537. # Capture top-level named volume blocks so user-managed definitions can be
  538. # re-emitted when still referenced by preserved services.
  539. _collect_top_level_volume_blocks() {
  540. local compose_file="$1"
  541. local line
  542. local in_top_volumes="no"
  543. local current_volume=""
  544. local current_block=""
  545. _FILE_OPS_VOLUME_BLOCKS=()
  546. _FILE_OPS_VOLUME_ORDER=()
  547. if [[ ! -f "$compose_file" ]]; then
  548. return 0
  549. fi
  550. while IFS= read -r line || [[ -n "$line" ]]; do
  551. if [[ "$line" =~ ^[A-Za-z] ]]; then
  552. if [[ "$in_top_volumes" == "yes" && -n "$current_volume" ]]; then
  553. _FILE_OPS_VOLUME_BLOCKS["$current_volume"]="$current_block"
  554. _FILE_OPS_VOLUME_ORDER+=("$current_volume")
  555. fi
  556. current_volume=""
  557. current_block=""
  558. if [[ "$line" == "volumes:" ]]; then
  559. in_top_volumes="yes"
  560. else
  561. in_top_volumes="no"
  562. fi
  563. continue
  564. fi
  565. if [[ "$in_top_volumes" != "yes" ]]; then
  566. continue
  567. fi
  568. if [[ "$line" =~ ^[[:space:]]{2}([A-Za-z0-9_.-]+):[[:space:]]*$ ]]; then
  569. if [[ -n "$current_volume" ]]; then
  570. _FILE_OPS_VOLUME_BLOCKS["$current_volume"]="$current_block"
  571. _FILE_OPS_VOLUME_ORDER+=("$current_volume")
  572. fi
  573. current_volume="${BASH_REMATCH[1]}"
  574. current_block="${line}"$'\n'
  575. continue
  576. fi
  577. if [[ -n "$current_volume" ]]; then
  578. current_block+="${line}"$'\n'
  579. fi
  580. done < "$compose_file"
  581. if [[ "$in_top_volumes" == "yes" && -n "$current_volume" ]]; then
  582. _FILE_OPS_VOLUME_BLOCKS["$current_volume"]="$current_block"
  583. _FILE_OPS_VOLUME_ORDER+=("$current_volume")
  584. fi
  585. }
  586. # Remove wizard-managed services and the top-level volumes block from a compose
  587. # file. Non-managed services are preserved verbatim.
  588. _strip_wizard_managed_services_and_top_level_volumes() {
  589. local compose_file="$1"
  590. local tmp_file="${compose_file}.strip-svc"
  591. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  592. local line current_service="" current_root_service=""
  593. local in_services="no"
  594. local in_top_volumes="no"
  595. local inserted_marker="no"
  596. : > "$tmp_file"
  597. while IFS= read -r line || [[ -n "$line" ]]; do
  598. if [[ "$line" == "$_WIZARD_MANAGED_SERVICES_MARKER" ]]; then
  599. continue
  600. fi
  601. # Detect top-level (non-indented) keys.
  602. if [[ "$line" =~ ^[A-Za-z] ]]; then
  603. if [[ "$in_services" == "yes" && "$line" != "services:" && "$inserted_marker" != "yes" ]]; then
  604. printf '%s\n' "$_WIZARD_MANAGED_SERVICES_MARKER" >> "$tmp_file"
  605. inserted_marker="yes"
  606. fi
  607. in_top_volumes="no"
  608. if [[ "$line" == "services:" ]]; then
  609. in_services="yes"
  610. current_service=""
  611. elif [[ "$line" =~ ^volumes:[[:space:]]*$ ]]; then
  612. in_top_volumes="yes"
  613. in_services="no"
  614. current_service=""
  615. continue # skip volumes: header; regenerated at end of generate_docker_compose
  616. else
  617. in_services="no"
  618. current_service=""
  619. fi
  620. printf '%s\n' "$line" >> "$tmp_file"
  621. continue
  622. fi
  623. # Skip top-level volumes block content.
  624. if [[ "$in_top_volumes" == "yes" ]]; then
  625. continue
  626. fi
  627. # Track current service inside the services: block.
  628. if [[ "$in_services" == "yes" && "$line" =~ ^[[:space:]]{2}([A-Za-z0-9_-]+):[[:space:]]*$ ]]; then
  629. current_service="${BASH_REMATCH[1]}"
  630. current_root_service="$(_managed_service_root_name "$current_service")"
  631. fi
  632. # Skip managed services that are being removed or regenerated. Preserve
  633. # lightrag, user-added services, and unchanged managed service groups.
  634. if [[ "$in_services" == "yes" && -n "$current_service" ]] && \
  635. [[ "$current_service" != "lightrag" ]] && \
  636. [[ -n "$current_root_service" ]] && \
  637. _should_rewrite_wizard_managed_root_service "$current_root_service"; then
  638. continue
  639. fi
  640. printf '%s\n' "$line" >> "$tmp_file"
  641. done < "$compose_file"
  642. if [[ "$in_services" == "yes" && "$inserted_marker" != "yes" ]]; then
  643. printf '%s\n' "$_WIZARD_MANAGED_SERVICES_MARKER" >> "$tmp_file"
  644. fi
  645. mv "$tmp_file" "$compose_file"
  646. }
  647. _merge_managed_service_blocks() {
  648. local compose_file="$1"
  649. local service_blocks_file="$2"
  650. local tmp_file="${compose_file}.merge-services"
  651. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  652. local line
  653. local inserted="no"
  654. : > "$tmp_file"
  655. while IFS= read -r line || [[ -n "$line" ]]; do
  656. if [[ "$line" == "$_WIZARD_MANAGED_SERVICES_MARKER" ]]; then
  657. if [[ -s "$service_blocks_file" ]]; then
  658. cat "$service_blocks_file" >> "$tmp_file"
  659. inserted="yes"
  660. fi
  661. continue
  662. fi
  663. printf '%s\n' "$line" >> "$tmp_file"
  664. done < "$compose_file"
  665. if [[ -s "$service_blocks_file" && "$inserted" != "yes" ]]; then
  666. cat "$service_blocks_file" >> "$tmp_file"
  667. fi
  668. mv "$tmp_file" "$compose_file"
  669. }
  670. _normalize_services_section_spacing() {
  671. local compose_file="$1"
  672. local tmp_file="${compose_file}.normalize-services"
  673. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  674. local line
  675. local in_services="no"
  676. local pending_blank="no"
  677. local saw_service_content="no"
  678. : > "$tmp_file"
  679. while IFS= read -r line || [[ -n "$line" ]]; do
  680. if [[ "$in_services" == "yes" ]]; then
  681. if [[ "$line" == "$_WIZARD_MANAGED_SERVICES_MARKER" ]]; then
  682. continue
  683. fi
  684. if [[ -z "$line" ]]; then
  685. pending_blank="yes"
  686. continue
  687. fi
  688. if [[ ! "$line" =~ ^[[:space:]] ]]; then
  689. pending_blank="no"
  690. if [[ "$saw_service_content" == "yes" ]]; then
  691. printf '\n' >> "$tmp_file"
  692. fi
  693. printf '%s\n' "$line" >> "$tmp_file"
  694. in_services="no"
  695. continue
  696. fi
  697. if [[ "$pending_blank" == "yes" && "$saw_service_content" == "yes" ]]; then
  698. printf '\n' >> "$tmp_file"
  699. fi
  700. printf '%s\n' "$line" >> "$tmp_file"
  701. pending_blank="no"
  702. saw_service_content="yes"
  703. continue
  704. fi
  705. printf '%s\n' "$line" >> "$tmp_file"
  706. if [[ "$line" == "services:" ]]; then
  707. in_services="yes"
  708. pending_blank="no"
  709. saw_service_content="no"
  710. fi
  711. done < "$compose_file"
  712. mv "$tmp_file" "$compose_file"
  713. }
  714. _strip_wrapping_quotes() {
  715. local value="$1"
  716. if [[ "$value" == \"*\" && "$value" == *\" ]]; then
  717. value="${value#\"}"
  718. value="${value%\"}"
  719. elif [[ "$value" == \'*\' ]]; then
  720. value="${value#\'}"
  721. value="${value%\'}"
  722. fi
  723. printf '%s' "$value"
  724. }
  725. read_service_environment_value() {
  726. local compose_file="$1"
  727. local service_name="$2"
  728. local wanted_key="$3"
  729. local line
  730. local entry_key=""
  731. local entry_value=""
  732. local service_header=" ${service_name}:"
  733. local in_service="no"
  734. local in_environment="no"
  735. if [[ ! -f "$compose_file" ]]; then
  736. return 1
  737. fi
  738. while IFS= read -r line || [[ -n "$line" ]]; do
  739. if [[ "$in_service" == "yes" && "$in_environment" == "yes" ]]; then
  740. if [[ "$line" =~ ^[[:space:]]{6}([A-Z0-9_]+):[[:space:]]*(.+)$ ]]; then
  741. entry_key="${BASH_REMATCH[1]}"
  742. if [[ "$entry_key" == "$wanted_key" ]]; then
  743. printf '%s' "$(_strip_wrapping_quotes "${BASH_REMATCH[2]}")"
  744. return 0
  745. fi
  746. elif [[ "$line" =~ ^[[:space:]]{6}-[[:space:]](.+)$ ]]; then
  747. entry_value="$(_strip_wrapping_quotes "${BASH_REMATCH[1]}")"
  748. entry_key="${entry_value%%=*}"
  749. if [[ "$entry_key" == "$wanted_key" && "$entry_value" == *=* ]]; then
  750. printf '%s' "${entry_value#*=}"
  751. return 0
  752. fi
  753. elif [[ ! "$line" =~ ^[[:space:]]{6} ]]; then
  754. in_environment="no"
  755. fi
  756. elif [[ "$in_service" == "yes" && "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != "$service_header" ]]; then
  757. in_service="no"
  758. fi
  759. if [[ "$line" == "$service_header" ]]; then
  760. in_service="yes"
  761. in_environment="no"
  762. elif [[ "$in_service" == "yes" && "$line" == " environment:" ]]; then
  763. in_environment="yes"
  764. fi
  765. done < "$compose_file"
  766. return 1
  767. }
  768. # Parse the image: value for a named service from a compose file.
  769. # Assumes wizard-standard indentation: services at 2 spaces, properties at 4 spaces.
  770. read_service_image_value() {
  771. local compose_file="$1"
  772. local service_name="$2"
  773. local line
  774. local service_header=" ${service_name}:"
  775. local in_service="no"
  776. if [[ ! -f "$compose_file" ]]; then
  777. return 1
  778. fi
  779. while IFS= read -r line || [[ -n "$line" ]]; do
  780. if [[ "$in_service" == "yes" ]]; then
  781. if [[ "$line" =~ ^[[:space:]]{4}image:[[:space:]]*(.+)$ ]]; then
  782. printf '%s' "${BASH_REMATCH[1]}"
  783. return 0
  784. fi
  785. if [[ "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != "$service_header" ]]; then
  786. in_service="no"
  787. elif [[ "$line" =~ ^[^[:space:]] ]]; then
  788. in_service="no"
  789. fi
  790. fi
  791. if [[ "$line" == "$service_header" ]]; then
  792. in_service="yes"
  793. fi
  794. done < "$compose_file"
  795. return 1
  796. }
  797. _extract_named_volume_name() {
  798. local mount_spec="$1"
  799. local source=""
  800. mount_spec="$(_strip_wrapping_quotes "$mount_spec")"
  801. source="${mount_spec%%:*}"
  802. if [[ "$source" == "$mount_spec" || -z "$source" ]]; then
  803. return 1
  804. fi
  805. if [[ "$source" == .* || "$source" == /* || "$source" == "~"* ]]; then
  806. return 1
  807. fi
  808. if [[ "$source" == *"/"* || "$source" == *'$'* ]]; then
  809. return 1
  810. fi
  811. printf '%s' "$source"
  812. }
  813. _collect_referenced_named_volumes() {
  814. local compose_file="$1"
  815. local line
  816. local in_services="no"
  817. local current_service=""
  818. local in_volumes="no"
  819. local in_long_volume_entry="no"
  820. local long_volume_type=""
  821. local long_volume_source=""
  822. local volume_name=""
  823. local long_entry_key=""
  824. local long_entry_value=""
  825. local -A seen=()
  826. if [[ ! -f "$compose_file" ]]; then
  827. return 0
  828. fi
  829. while IFS= read -r line || [[ -n "$line" ]]; do
  830. if [[ "$line" == "services:" ]]; then
  831. in_services="yes"
  832. current_service=""
  833. in_volumes="no"
  834. in_long_volume_entry="no"
  835. long_volume_type=""
  836. continue
  837. fi
  838. if [[ "$in_services" == "yes" && "$line" =~ ^[A-Za-z] && "$line" != "services:" ]]; then
  839. in_services="no"
  840. in_volumes="no"
  841. in_long_volume_entry="no"
  842. continue
  843. fi
  844. if [[ "$in_services" != "yes" ]]; then
  845. continue
  846. fi
  847. if [[ "$line" =~ ^[[:space:]]{2}([A-Za-z0-9_-]+):[[:space:]]*$ ]]; then
  848. current_service="${BASH_REMATCH[1]}"
  849. in_volumes="no"
  850. in_long_volume_entry="no"
  851. long_volume_type=""
  852. long_volume_source=""
  853. continue
  854. fi
  855. if [[ -z "$current_service" ]]; then
  856. continue
  857. fi
  858. if [[ "$line" == " volumes:" ]]; then
  859. in_volumes="yes"
  860. in_long_volume_entry="no"
  861. long_volume_type=""
  862. long_volume_source=""
  863. continue
  864. fi
  865. if [[ "$in_volumes" != "yes" ]]; then
  866. continue
  867. fi
  868. if [[ "$line" =~ ^[[:space:]]{4}[^[:space:]-] || "$line" =~ ^[[:space:]]{2}[^[:space:]] ]]; then
  869. in_volumes="no"
  870. in_long_volume_entry="no"
  871. long_volume_type=""
  872. long_volume_source=""
  873. continue
  874. fi
  875. if [[ "$line" =~ ^[[:space:]]{6}-[[:space:]](.+)$ ]]; then
  876. local volume_entry="${BASH_REMATCH[1]}"
  877. volume_entry="$(_strip_wrapping_quotes "$volume_entry")"
  878. in_long_volume_entry="no"
  879. long_volume_type=""
  880. long_volume_source=""
  881. if [[ "$volume_entry" =~ ^([A-Za-z_][A-Za-z0-9_-]*):[[:space:]]*(.*)$ ]]; then
  882. long_entry_key="${BASH_REMATCH[1]}"
  883. long_entry_value="$(_strip_wrapping_quotes "${BASH_REMATCH[2]}")"
  884. case "$long_entry_key" in
  885. type|source|target|read_only|bind|volume|tmpfs|consistency|nocopy|subpath)
  886. in_long_volume_entry="yes"
  887. case "$long_entry_key" in
  888. type)
  889. if [[ "$long_entry_value" == "volume" ]]; then
  890. long_volume_type="volume"
  891. else
  892. long_volume_type="other"
  893. fi
  894. ;;
  895. source)
  896. long_volume_source="$long_entry_value"
  897. ;;
  898. esac
  899. if [[ "$long_volume_type" == "volume" && -n "$long_volume_source" && -z "${seen[$long_volume_source]+set}" ]]; then
  900. seen["$long_volume_source"]=1
  901. printf '%s\n' "$long_volume_source"
  902. fi
  903. continue
  904. ;;
  905. esac
  906. fi
  907. volume_name="$(_extract_named_volume_name "$volume_entry")" || continue
  908. if [[ -z "${seen[$volume_name]+set}" ]]; then
  909. seen["$volume_name"]=1
  910. printf '%s\n' "$volume_name"
  911. fi
  912. continue
  913. fi
  914. if [[ "$in_long_volume_entry" == "yes" ]] && \
  915. [[ "$line" =~ ^[[:space:]]{8}([A-Za-z_][A-Za-z0-9_-]*):[[:space:]]*(.+)$ ]]; then
  916. long_entry_key="${BASH_REMATCH[1]}"
  917. long_entry_value="$(_strip_wrapping_quotes "${BASH_REMATCH[2]}")"
  918. case "$long_entry_key" in
  919. type)
  920. if [[ "$long_entry_value" == "volume" ]]; then
  921. long_volume_type="volume"
  922. else
  923. long_volume_type="other"
  924. fi
  925. ;;
  926. source)
  927. long_volume_source="$long_entry_value"
  928. ;;
  929. esac
  930. if [[ "$long_volume_type" == "volume" && -n "$long_volume_source" && -z "${seen[$long_volume_source]+set}" ]]; then
  931. seen["$long_volume_source"]=1
  932. printf '%s\n' "$long_volume_source"
  933. fi
  934. fi
  935. done < "$compose_file"
  936. }
  937. _trim_trailing_blank_lines_in_file() {
  938. local file="$1"
  939. local trim_file="${file}.trim-tail"
  940. _FILE_OPS_CLEANUP_TMP+=("$trim_file")
  941. awk '
  942. { lines[NR] = $0 }
  943. END {
  944. last = NR
  945. while (last > 0 && lines[last] == "") {
  946. last--
  947. }
  948. for (i = 1; i <= last; i++) {
  949. print lines[i]
  950. }
  951. }
  952. ' "$file" > "$trim_file"
  953. mv "$trim_file" "$file"
  954. }
  955. _append_referenced_volume_blocks() {
  956. local compose_file="$1"
  957. local -a referenced_volumes=()
  958. local volume_name
  959. local root_service
  960. while IFS= read -r volume_name; do
  961. if [[ -n "$volume_name" ]]; then
  962. referenced_volumes+=("$volume_name")
  963. fi
  964. done < <(_collect_referenced_named_volumes "$compose_file")
  965. if ((${#referenced_volumes[@]} == 0)); then
  966. return 0
  967. fi
  968. _trim_trailing_blank_lines_in_file "$compose_file"
  969. printf '\nvolumes:\n' >> "$compose_file"
  970. for volume_name in "${referenced_volumes[@]}"; do
  971. if _is_wizard_managed_volume_name "$volume_name"; then
  972. root_service="$(_managed_volume_root_name "$volume_name")"
  973. if [[ -n "${_FILE_OPS_VOLUME_BLOCKS[$volume_name]+set}" ]] && \
  974. _should_preserve_wizard_managed_root_service "$root_service"; then
  975. printf '%s' "${_FILE_OPS_VOLUME_BLOCKS[$volume_name]}" >> "$compose_file"
  976. else
  977. printf ' %s:\n' "$volume_name" >> "$compose_file"
  978. fi
  979. elif [[ -n "${_FILE_OPS_VOLUME_BLOCKS[$volume_name]+set}" ]]; then
  980. printf '%s' "${_FILE_OPS_VOLUME_BLOCKS[$volume_name]}" >> "$compose_file"
  981. else
  982. printf ' %s:\n' "$volume_name" >> "$compose_file"
  983. fi
  984. done
  985. }
  986. generate_docker_compose() {
  987. local output_file="${1:-${REPO_ROOT:-.}/docker-compose.yml}"
  988. local base_file="${REPO_ROOT:-.}/docker-compose.yml"
  989. local tmp_file="${output_file}.tmp"
  990. local service_blocks_file="${output_file}.services"
  991. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  992. _FILE_OPS_CLEANUP_TMP+=("$service_blocks_file")
  993. local template_file
  994. local lightrag_mounts=()
  995. local lightrag_env_entries=()
  996. local key
  997. local root_service
  998. # Prefer the existing generated compose as the starting point to preserve
  999. # any user customisations to the lightrag service. Fall back to the base
  1000. # docker-compose.yml when the output file doesn't exist yet.
  1001. if [[ -f "$output_file" && "$output_file" != "$base_file" ]]; then
  1002. _refresh_existing_managed_root_service_set_from_compose "$output_file"
  1003. _collect_top_level_volume_blocks "$output_file"
  1004. cp "$output_file" "$tmp_file"
  1005. # Strip wizard-managed services and top-level volumes. User-managed
  1006. # services are preserved, while volumes are rebuilt from final service
  1007. # references after managed templates are appended.
  1008. _strip_wizard_managed_services_and_top_level_volumes "$tmp_file"
  1009. elif [[ -f "$base_file" ]]; then
  1010. _refresh_existing_managed_root_service_set_from_compose "$base_file"
  1011. _collect_top_level_volume_blocks "$base_file"
  1012. cp "$base_file" "$tmp_file"
  1013. _strip_wizard_managed_services_and_top_level_volumes "$tmp_file"
  1014. else
  1015. EXISTING_MANAGED_ROOT_SERVICE_SET=()
  1016. _FILE_OPS_VOLUME_BLOCKS=()
  1017. _FILE_OPS_VOLUME_ORDER=()
  1018. printf 'services:\n' > "$tmp_file"
  1019. fi
  1020. prepare_lightrag_service_for_generated_compose "$tmp_file"
  1021. normalize_lightrag_restart_policy "$tmp_file"
  1022. # Remove stale wizard-managed keys from lightrag's environment so that
  1023. # keys no longer in COMPOSE_ENV_OVERRIDES are not left behind.
  1024. _strip_lightrag_wizard_environment_keys "$tmp_file"
  1025. # Remove stale wizard-managed bind mounts from lightrag's volumes so that
  1026. # mounts no longer needed (e.g. after SSL removal) are not left behind.
  1027. _strip_lightrag_wizard_bind_mounts "$tmp_file"
  1028. if [[ -n "${LIGHTRAG_COMPOSE_SERVER_PORT_MAPPING:-}" ]]; then
  1029. _strip_lightrag_wizard_ports "$tmp_file"
  1030. inject_lightrag_port_mapping "$tmp_file" "$LIGHTRAG_COMPOSE_SERVER_PORT_MAPPING"
  1031. fi
  1032. # Ensure the optional prompts directory mount exists so users can supply
  1033. # custom entity-type prompt profiles without rebuilding the image. Mirrors
  1034. # the ./data/inputs and ./data/rag_storage bind layout.
  1035. if ! _lightrag_volumes_have_container_target "$tmp_file" "/app/data/prompts"; then
  1036. lightrag_mounts+=("./data/prompts:/app/data/prompts")
  1037. fi
  1038. append_lightrag_ssl_mount lightrag_mounts "${COMPOSE_ENV_OVERRIDES[SSL_CERTFILE]:-}" || return 1
  1039. append_lightrag_ssl_mount lightrag_mounts "${COMPOSE_ENV_OVERRIDES[SSL_KEYFILE]:-}" || return 1
  1040. if ((${#lightrag_mounts[@]} > 0)); then
  1041. inject_lightrag_bind_mounts "$tmp_file" "${lightrag_mounts[@]}"
  1042. fi
  1043. for key in "${!COMPOSE_ENV_OVERRIDES[@]}"; do
  1044. lightrag_env_entries+=("${key}=${COMPOSE_ENV_OVERRIDES[$key]}")
  1045. done
  1046. if ((${#lightrag_env_entries[@]} > 0)); then
  1047. inject_lightrag_environment_overrides "$tmp_file" "${lightrag_env_entries[@]}"
  1048. fi
  1049. repair_misplaced_lightrag_depends_on "$tmp_file"
  1050. inject_lightrag_depends_on "$tmp_file" "${DOCKER_SERVICES[@]}"
  1051. : > "$service_blocks_file"
  1052. for service in "${DOCKER_SERVICES[@]}"; do
  1053. root_service="$(_managed_service_root_name "$service")"
  1054. if _should_preserve_wizard_managed_root_service "$root_service" && \
  1055. _existing_managed_root_service_present "$root_service"; then
  1056. continue
  1057. fi
  1058. template_file="$TEMPLATES_DIR/${service}.yml"
  1059. if [[ "$service" == "milvus" ]]; then
  1060. if [[ "${ENV_VALUES[MILVUS_DEVICE]:-cpu}" == "cuda" ]]; then
  1061. if [[ -f "$TEMPLATES_DIR/${service}-gpu.yml" ]]; then
  1062. template_file="$TEMPLATES_DIR/${service}-gpu.yml"
  1063. fi
  1064. fi
  1065. fi
  1066. if [[ "$service" == "qdrant" ]]; then
  1067. if [[ "${ENV_VALUES[QDRANT_DEVICE]:-cpu}" == "cuda" ]]; then
  1068. if [[ -f "$TEMPLATES_DIR/${service}-gpu.yml" ]]; then
  1069. template_file="$TEMPLATES_DIR/${service}-gpu.yml"
  1070. fi
  1071. fi
  1072. fi
  1073. if [[ "$service" == "vllm-rerank" ]]; then
  1074. if [[ "${ENV_VALUES[VLLM_RERANK_DEVICE]:-cpu}" == "cuda" ]]; then
  1075. if [[ -f "$TEMPLATES_DIR/${service}-gpu.yml" ]]; then
  1076. template_file="$TEMPLATES_DIR/${service}-gpu.yml"
  1077. fi
  1078. fi
  1079. fi
  1080. if [[ "$service" == "vllm-embed" ]]; then
  1081. if [[ "${ENV_VALUES[VLLM_EMBED_DEVICE]:-cpu}" == "cuda" ]]; then
  1082. if [[ -f "$TEMPLATES_DIR/${service}-gpu.yml" ]]; then
  1083. template_file="$TEMPLATES_DIR/${service}-gpu.yml"
  1084. fi
  1085. fi
  1086. fi
  1087. if [[ ! -f "$template_file" ]]; then
  1088. format_error "Missing docker template: $template_file" "Reinstall the setup scripts."
  1089. return 1
  1090. fi
  1091. printf '\n' >> "$service_blocks_file"
  1092. cat "$template_file" >> "$service_blocks_file"
  1093. case "$service" in
  1094. postgres)
  1095. inject_service_environment_overrides "$service_blocks_file" "postgres" \
  1096. "POSTGRES_USER=${ENV_VALUES[POSTGRES_USER]:-}" \
  1097. "POSTGRES_PASSWORD=${ENV_VALUES[POSTGRES_PASSWORD]:-}" \
  1098. "POSTGRES_DB=${ENV_VALUES[POSTGRES_DATABASE]:-}"
  1099. ;;
  1100. neo4j)
  1101. inject_service_environment_overrides "$service_blocks_file" "neo4j" \
  1102. "NEO4J_AUTH=${_COMPOSE_RAW_VALUE_PREFIX}\${NEO4J_USERNAME:?missing}/\${NEO4J_PASSWORD:?missing}" \
  1103. "NEO4J_dbms_default__database=${ENV_VALUES[NEO4J_DATABASE]:-neo4j}"
  1104. ;;
  1105. mongodb)
  1106. ;;
  1107. redis)
  1108. ;;
  1109. milvus)
  1110. ;;
  1111. qdrant)
  1112. ;;
  1113. memgraph)
  1114. ;;
  1115. opensearch)
  1116. ;;
  1117. vllm-rerank)
  1118. ;;
  1119. vllm-embed)
  1120. ;;
  1121. esac
  1122. if [[ -n "${COMPOSE_SERVICE_IMAGE_OVERRIDES[$service]+set}" ]]; then
  1123. inject_service_image_override \
  1124. "$service_blocks_file" \
  1125. "$service" \
  1126. "${COMPOSE_SERVICE_IMAGE_OVERRIDES[$service]}"
  1127. fi
  1128. done
  1129. _merge_managed_service_blocks "$tmp_file" "$service_blocks_file"
  1130. _normalize_services_section_spacing "$tmp_file"
  1131. _append_referenced_volume_blocks "$tmp_file"
  1132. mv "$tmp_file" "$output_file"
  1133. }
  1134. prepare_lightrag_service_for_generated_compose() {
  1135. # Let the containerized app read the mounted .env itself. Keeping env_file
  1136. # here would make Docker Compose re-parse the same secrets and expand '$'.
  1137. local compose_file="$1"
  1138. local tmp_file="${compose_file}.strip-env-file"
  1139. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  1140. local line
  1141. local in_lightrag="no"
  1142. local in_env_file="no"
  1143. : > "$tmp_file"
  1144. while IFS= read -r line || [[ -n "$line" ]]; do
  1145. if [[ "$in_env_file" == "yes" ]]; then
  1146. if [[ "$line" =~ ^[[:space:]]{6}-[[:space:]] ]]; then
  1147. continue
  1148. fi
  1149. in_env_file="no"
  1150. fi
  1151. if [[ "$in_lightrag" == "yes" && "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != " lightrag:" ]]; then
  1152. in_lightrag="no"
  1153. fi
  1154. if [[ "$in_lightrag" == "yes" && "$line" == " env_file:" ]]; then
  1155. in_env_file="yes"
  1156. continue
  1157. fi
  1158. if [[ "$in_lightrag" == "yes" && "$line" =~ ^[[:space:]]{4}container_name: ]]; then
  1159. continue
  1160. fi
  1161. printf '%s\n' "$line" >> "$tmp_file"
  1162. if [[ "$line" == " lightrag:" ]]; then
  1163. in_lightrag="yes"
  1164. in_env_file="no"
  1165. fi
  1166. done < "$compose_file"
  1167. mv "$tmp_file" "$compose_file"
  1168. }
  1169. normalize_lightrag_restart_policy() {
  1170. local compose_file="$1"
  1171. local tmp_file="${compose_file}.normalize-lightrag-restart"
  1172. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  1173. local line
  1174. local in_lightrag="no"
  1175. local in_deploy="no"
  1176. local deploy_seen="no"
  1177. local insert_blank_after_deploy="no"
  1178. local skip_blank_after_removed_restart="no"
  1179. local -a deploy_lines=()
  1180. _trim_trailing_blank_lines() {
  1181. local file="$1"
  1182. local trim_file="${file}.trim"
  1183. _FILE_OPS_CLEANUP_TMP+=("$trim_file")
  1184. awk '
  1185. { lines[NR] = $0 }
  1186. END {
  1187. last = NR
  1188. while (last > 0 && lines[last] == "") {
  1189. last--
  1190. }
  1191. for (i = 1; i <= last; i++) {
  1192. print lines[i]
  1193. }
  1194. }
  1195. ' "$file" > "$trim_file"
  1196. mv "$trim_file" "$file"
  1197. }
  1198. _write_normalized_lightrag_deploy_block() {
  1199. local deploy_line
  1200. local skipping_restart_policy="no"
  1201. printf ' deploy:\n' >> "$tmp_file"
  1202. for deploy_line in "${deploy_lines[@]}"; do
  1203. if [[ -z "$deploy_line" ]]; then
  1204. continue
  1205. fi
  1206. if [[ "$skipping_restart_policy" == "yes" ]]; then
  1207. if [[ "$deploy_line" =~ ^[[:space:]]{8} ]]; then
  1208. continue
  1209. fi
  1210. skipping_restart_policy="no"
  1211. fi
  1212. if [[ "$deploy_line" == " restart_policy:" ]]; then
  1213. skipping_restart_policy="yes"
  1214. continue
  1215. fi
  1216. printf '%s\n' "$deploy_line" >> "$tmp_file"
  1217. done
  1218. printf ' restart_policy:\n' >> "$tmp_file"
  1219. printf ' condition: on-failure\n' >> "$tmp_file"
  1220. printf ' max_attempts: 10\n' >> "$tmp_file"
  1221. }
  1222. : > "$tmp_file"
  1223. while IFS= read -r line || [[ -n "$line" ]]; do
  1224. if [[ "$in_deploy" == "yes" ]]; then
  1225. if [[ "$line" =~ ^[[:space:]]{6} || -z "$line" ]]; then
  1226. deploy_lines+=("$line")
  1227. continue
  1228. fi
  1229. _trim_trailing_blank_lines "$tmp_file"
  1230. _write_normalized_lightrag_deploy_block
  1231. deploy_lines=()
  1232. in_deploy="no"
  1233. if [[ "$line" =~ ^[[:space:]]{2}[^[:space:]] || "$line" =~ ^[^[:space:]] ]]; then
  1234. insert_blank_after_deploy="yes"
  1235. fi
  1236. fi
  1237. if [[ "$in_lightrag" == "yes" && "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != " lightrag:" ]] || \
  1238. [[ "$in_lightrag" == "yes" && "$line" =~ ^[^[:space:]] ]]; then
  1239. if [[ "$deploy_seen" != "yes" ]]; then
  1240. _trim_trailing_blank_lines "$tmp_file"
  1241. _write_normalized_lightrag_deploy_block
  1242. insert_blank_after_deploy="yes"
  1243. fi
  1244. in_lightrag="no"
  1245. deploy_seen="no"
  1246. skip_blank_after_removed_restart="no"
  1247. fi
  1248. if [[ "$in_lightrag" == "yes" && "$line" == " deploy:" ]]; then
  1249. in_deploy="yes"
  1250. deploy_seen="yes"
  1251. deploy_lines=()
  1252. continue
  1253. fi
  1254. if [[ "$in_lightrag" == "yes" && "$line" =~ ^[[:space:]]{4}restart: ]]; then
  1255. skip_blank_after_removed_restart="yes"
  1256. continue
  1257. fi
  1258. if [[ "$skip_blank_after_removed_restart" == "yes" && "$in_lightrag" == "yes" ]]; then
  1259. if [[ -z "$line" ]]; then
  1260. continue
  1261. fi
  1262. skip_blank_after_removed_restart="no"
  1263. fi
  1264. if [[ "$insert_blank_after_deploy" == "yes" ]]; then
  1265. printf '\n' >> "$tmp_file"
  1266. insert_blank_after_deploy="no"
  1267. fi
  1268. printf '%s\n' "$line" >> "$tmp_file"
  1269. if [[ "$line" == " lightrag:" ]]; then
  1270. in_lightrag="yes"
  1271. in_deploy="no"
  1272. deploy_seen="no"
  1273. insert_blank_after_deploy="no"
  1274. skip_blank_after_removed_restart="no"
  1275. deploy_lines=()
  1276. fi
  1277. done < "$compose_file"
  1278. if [[ "$in_deploy" == "yes" ]]; then
  1279. _trim_trailing_blank_lines "$tmp_file"
  1280. _write_normalized_lightrag_deploy_block
  1281. deploy_seen="yes"
  1282. fi
  1283. if [[ "$in_lightrag" == "yes" && "$deploy_seen" != "yes" ]]; then
  1284. _trim_trailing_blank_lines "$tmp_file"
  1285. _write_normalized_lightrag_deploy_block
  1286. fi
  1287. mv "$tmp_file" "$compose_file"
  1288. }
  1289. append_lightrag_ssl_mount() {
  1290. local array_name="$1"
  1291. local container_path="$2"
  1292. local relative_host_path=""
  1293. local mount_entry=""
  1294. if [[ -z "$container_path" ]]; then
  1295. return 0
  1296. fi
  1297. if [[ "$container_path" != /app/data/* ]]; then
  1298. format_error "Unsupported SSL path: ${container_path}" "Use paths staged under /app/data."
  1299. return 1
  1300. fi
  1301. relative_host_path="./data/${container_path#/app/data/}"
  1302. mount_entry="${relative_host_path}:${container_path}:ro"
  1303. local -n _arr_ref="$array_name"
  1304. _arr_ref+=("$mount_entry")
  1305. }
  1306. format_yaml_value() {
  1307. local value="$1"
  1308. local escaped="${value//\\/\\\\}"
  1309. escaped="${escaped//\"/\\\"}"
  1310. escaped="${escaped//\$/\$\$}"
  1311. printf '"%s"' "$escaped"
  1312. }
  1313. _COMPOSE_RAW_VALUE_PREFIX="__LIGHTRAG_RAW_COMPOSE__:"
  1314. format_compose_environment_value() {
  1315. local value="$1"
  1316. if [[ "$value" == "${_COMPOSE_RAW_VALUE_PREFIX}"* ]]; then
  1317. printf '%s' "${value#${_COMPOSE_RAW_VALUE_PREFIX}}"
  1318. return 0
  1319. fi
  1320. format_yaml_value "$value"
  1321. }
  1322. inject_service_environment_overrides() {
  1323. local compose_file="$1"
  1324. local service_name="$2"
  1325. shift 2
  1326. local entries=("$@")
  1327. local tmp_file="${compose_file}.${service_name}.env"
  1328. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  1329. local line key value
  1330. local in_service="no"
  1331. local in_environment="no"
  1332. local environment_style="mapping"
  1333. local inserted="no"
  1334. local service_header=" ${service_name}:"
  1335. if ((${#entries[@]} == 0)); then
  1336. return 0
  1337. fi
  1338. : > "$tmp_file"
  1339. while IFS= read -r line || [[ -n "$line" ]]; do
  1340. if [[ "$in_service" == "yes" && "$in_environment" == "yes" ]]; then
  1341. if [[ "$line" =~ ^[[:space:]]{6}-[[:space:]] ]]; then
  1342. environment_style="list"
  1343. elif [[ "$line" =~ ^[[:space:]]{6}[A-Z0-9_]+: ]]; then
  1344. environment_style="mapping"
  1345. fi
  1346. if [[ "$line" =~ ^[[:space:]]{4}[^[:space:]] || "$line" =~ ^[[:space:]]{2}[^[:space:]] || "$line" =~ ^[^[:space:]] ]]; then
  1347. if [[ "$inserted" == "no" ]]; then
  1348. _write_service_environment_entries "$tmp_file" "$environment_style" "${entries[@]}"
  1349. inserted="yes"
  1350. fi
  1351. in_environment="no"
  1352. fi
  1353. elif [[ "$in_service" == "yes" && \
  1354. ( "$line" =~ ^[[:space:]]{2}[^[:space:]] || "$line" =~ ^[^[:space:]] ) && \
  1355. "$line" != "$service_header" ]]; then
  1356. if [[ "$inserted" == "no" ]]; then
  1357. printf ' environment:\n' >> "$tmp_file"
  1358. _write_service_environment_entries "$tmp_file" "mapping" "${entries[@]}"
  1359. inserted="yes"
  1360. fi
  1361. in_service="no"
  1362. fi
  1363. printf '%s\n' "$line" >> "$tmp_file"
  1364. if [[ "$line" == "$service_header" ]]; then
  1365. in_service="yes"
  1366. in_environment="no"
  1367. environment_style="mapping"
  1368. elif [[ "$in_service" == "yes" && "$line" == " environment:" ]]; then
  1369. in_environment="yes"
  1370. environment_style="mapping"
  1371. fi
  1372. done < "$compose_file"
  1373. if [[ "$in_service" == "yes" && "$inserted" == "no" ]]; then
  1374. if [[ "$in_environment" != "yes" ]]; then
  1375. printf ' environment:\n' >> "$tmp_file"
  1376. fi
  1377. _write_service_environment_entries "$tmp_file" "$environment_style" "${entries[@]}"
  1378. fi
  1379. mv "$tmp_file" "$compose_file"
  1380. }
  1381. inject_service_image_override() {
  1382. local compose_file="$1"
  1383. local service_name="$2"
  1384. local image_value="$3"
  1385. local tmp_file="${compose_file}.${service_name}.image"
  1386. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  1387. local line
  1388. local in_service="no"
  1389. local inserted="no"
  1390. local service_header=" ${service_name}:"
  1391. if [[ -z "$image_value" || ! -f "$compose_file" ]]; then
  1392. return 0
  1393. fi
  1394. : > "$tmp_file"
  1395. while IFS= read -r line || [[ -n "$line" ]]; do
  1396. if [[ "$in_service" == "yes" ]]; then
  1397. if [[ "$line" =~ ^[[:space:]]{4}image:[[:space:]]*.+$ ]]; then
  1398. printf ' image: %s\n' "$image_value" >> "$tmp_file"
  1399. inserted="yes"
  1400. continue
  1401. fi
  1402. if [[ "$inserted" == "no" && "$line" =~ ^[[:space:]]{4}[^[:space:]] ]]; then
  1403. printf ' image: %s\n' "$image_value" >> "$tmp_file"
  1404. inserted="yes"
  1405. fi
  1406. if [[ "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != "$service_header" ]]; then
  1407. if [[ "$inserted" == "no" ]]; then
  1408. printf ' image: %s\n' "$image_value" >> "$tmp_file"
  1409. inserted="yes"
  1410. fi
  1411. in_service="no"
  1412. elif [[ "$line" =~ ^[^[:space:]] ]]; then
  1413. if [[ "$inserted" == "no" ]]; then
  1414. printf ' image: %s\n' "$image_value" >> "$tmp_file"
  1415. inserted="yes"
  1416. fi
  1417. in_service="no"
  1418. fi
  1419. fi
  1420. printf '%s\n' "$line" >> "$tmp_file"
  1421. if [[ "$line" == "$service_header" ]]; then
  1422. in_service="yes"
  1423. fi
  1424. done < "$compose_file"
  1425. if [[ "$in_service" == "yes" && "$inserted" == "no" ]]; then
  1426. printf ' image: %s\n' "$image_value" >> "$tmp_file"
  1427. fi
  1428. mv "$tmp_file" "$compose_file"
  1429. }
  1430. # Return success when the lightrag service already has a volume mount whose
  1431. # container target matches the supplied path. Used to make idempotent mount
  1432. # injections that should not duplicate existing user/wizard entries.
  1433. _lightrag_volumes_have_container_target() {
  1434. local compose_file="$1"
  1435. local target_path="$2"
  1436. local line
  1437. local in_lightrag="no"
  1438. local in_volumes="no"
  1439. local mount_spec=""
  1440. local remainder=""
  1441. local container_path=""
  1442. if [[ ! -f "$compose_file" || -z "$target_path" ]]; then
  1443. return 1
  1444. fi
  1445. while IFS= read -r line || [[ -n "$line" ]]; do
  1446. if [[ "$in_lightrag" == "yes" ]]; then
  1447. if [[ "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != " lightrag:" ]] || \
  1448. [[ "$line" =~ ^[^[:space:]] ]]; then
  1449. in_lightrag="no"
  1450. in_volumes="no"
  1451. elif [[ "$line" == " volumes:" ]]; then
  1452. in_volumes="yes"
  1453. elif [[ "$in_volumes" == "yes" && "$line" =~ ^[[:space:]]{4}[^[:space:]-] ]]; then
  1454. in_volumes="no"
  1455. elif [[ "$in_volumes" == "yes" && "$line" =~ ^[[:space:]]{6}-[[:space:]](.+)$ ]]; then
  1456. mount_spec="$(_strip_wrapping_quotes "${BASH_REMATCH[1]}")"
  1457. remainder="${mount_spec#*:}"
  1458. if [[ "$remainder" == "$mount_spec" ]]; then
  1459. continue
  1460. fi
  1461. container_path="${remainder%%:*}"
  1462. if [[ "$container_path" == "$target_path" ]]; then
  1463. return 0
  1464. fi
  1465. fi
  1466. fi
  1467. if [[ "$line" == " lightrag:" ]]; then
  1468. in_lightrag="yes"
  1469. in_volumes="no"
  1470. fi
  1471. done < "$compose_file"
  1472. return 1
  1473. }
  1474. # Return success when a volume mount entry is a wizard-managed SSL cert/key
  1475. # bind mount (./data/certs/* -> /app/data/certs/*, optional :ro suffix).
  1476. _is_wizard_ssl_bind_mount() {
  1477. local mount_spec="$1"
  1478. local host_path=""
  1479. local remainder=""
  1480. local container_path=""
  1481. local mode=""
  1482. local host_suffix=""
  1483. local container_suffix=""
  1484. host_path="${mount_spec%%:*}"
  1485. remainder="${mount_spec#*:}"
  1486. if [[ "$remainder" == "$mount_spec" ]]; then
  1487. return 1
  1488. fi
  1489. container_path="${remainder%%:*}"
  1490. if [[ "$container_path" == "$remainder" ]]; then
  1491. mode=""
  1492. else
  1493. mode="${remainder#${container_path}:}"
  1494. fi
  1495. if [[ "$host_path" != ./data/certs/* ]]; then
  1496. return 1
  1497. fi
  1498. if [[ "$container_path" != /app/data/certs/* ]]; then
  1499. return 1
  1500. fi
  1501. # Wizard-generated SSL mounts are read-only. Keep non-read-only mounts so
  1502. # user-defined overrides under /app/data/certs are not stripped.
  1503. if [[ -n "$mode" && "$mode" != "ro" ]]; then
  1504. return 1
  1505. fi
  1506. host_suffix="${host_path#./data/certs/}"
  1507. container_suffix="${container_path#/app/data/certs/}"
  1508. [[ "$host_suffix" == "$container_suffix" ]]
  1509. }
  1510. # Remove wizard-managed SSL bind mounts from the lightrag service's volumes
  1511. # block, leaving persistent and user-added /app/data/* mounts intact.
  1512. _strip_lightrag_wizard_bind_mounts() {
  1513. local compose_file="$1"
  1514. local tmp_file="${compose_file}.strip-mounts"
  1515. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  1516. local line
  1517. local mount_spec
  1518. local in_lightrag="no"
  1519. local in_volumes="no"
  1520. : > "$tmp_file"
  1521. while IFS= read -r line || [[ -n "$line" ]]; do
  1522. if [[ "$in_lightrag" == "yes" ]]; then
  1523. if [[ "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != " lightrag:" ]]; then
  1524. in_lightrag="no"
  1525. in_volumes="no"
  1526. elif [[ "$line" == " volumes:" ]]; then
  1527. in_volumes="yes"
  1528. elif [[ "$in_volumes" == "yes" ]]; then
  1529. if [[ "$line" =~ ^[[:space:]]{4}[^[:space:]] ]]; then
  1530. in_volumes="no"
  1531. elif [[ "$line" =~ ^[[:space:]]{6}-[[:space:]] ]]; then
  1532. mount_spec="${line# - }"
  1533. mount_spec="${mount_spec%\"}"
  1534. mount_spec="${mount_spec#\"}"
  1535. mount_spec="${mount_spec%\'}"
  1536. mount_spec="${mount_spec#\'}"
  1537. if _is_wizard_ssl_bind_mount "$mount_spec"; then
  1538. continue
  1539. fi
  1540. fi
  1541. fi
  1542. fi
  1543. printf '%s\n' "$line" >> "$tmp_file"
  1544. if [[ "$line" == " lightrag:" ]]; then
  1545. in_lightrag="yes"
  1546. in_volumes="no"
  1547. fi
  1548. done < "$compose_file"
  1549. mv "$tmp_file" "$compose_file"
  1550. }
  1551. _is_wizard_lightrag_port_mapping() {
  1552. local port_spec="$(_strip_wrapping_quotes "$1")"
  1553. if [[ "$port_spec" == '${HOST:-0.0.0.0}:${PORT:-9621}:9621' || \
  1554. "$port_spec" == '${PORT:-9621}:9621' ]]; then
  1555. return 0
  1556. fi
  1557. case "$port_spec" in
  1558. 9621|9621/tcp|*:9621|*:9621/tcp)
  1559. return 0
  1560. ;;
  1561. esac
  1562. return 1
  1563. }
  1564. _strip_lightrag_wizard_ports() {
  1565. local compose_file="$1"
  1566. local tmp_file="${compose_file}.strip-lightrag-ports"
  1567. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  1568. local line
  1569. local port_spec=""
  1570. local in_lightrag="no"
  1571. local in_ports="no"
  1572. : > "$tmp_file"
  1573. while IFS= read -r line || [[ -n "$line" ]]; do
  1574. if [[ "$in_lightrag" == "yes" && "$in_ports" == "yes" ]]; then
  1575. if [[ "$line" =~ ^[[:space:]]{6}-[[:space:]](.+)$ ]]; then
  1576. port_spec="${BASH_REMATCH[1]}"
  1577. if _is_wizard_lightrag_port_mapping "$port_spec"; then
  1578. continue
  1579. fi
  1580. elif [[ ! "$line" =~ ^[[:space:]]{6} ]]; then
  1581. in_ports="no"
  1582. fi
  1583. elif [[ "$in_lightrag" == "yes" && "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != " lightrag:" ]]; then
  1584. in_lightrag="no"
  1585. fi
  1586. printf '%s\n' "$line" >> "$tmp_file"
  1587. if [[ "$line" == " lightrag:" ]]; then
  1588. in_lightrag="yes"
  1589. in_ports="no"
  1590. elif [[ "$in_lightrag" == "yes" && "$line" == " ports:" ]]; then
  1591. in_ports="yes"
  1592. fi
  1593. done < "$compose_file"
  1594. mv "$tmp_file" "$compose_file"
  1595. }
  1596. inject_lightrag_bind_mounts() {
  1597. local compose_file="$1"
  1598. shift
  1599. local mounts=("$@")
  1600. local tmp_file="${compose_file}.mounts"
  1601. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  1602. local line
  1603. local in_lightrag="no"
  1604. local in_volumes="no"
  1605. local inserted="no"
  1606. if ((${#mounts[@]} == 0)); then
  1607. return 0
  1608. fi
  1609. : > "$tmp_file"
  1610. while IFS= read -r line || [[ -n "$line" ]]; do
  1611. if [[ "$in_lightrag" == "yes" && "$in_volumes" == "yes" ]]; then
  1612. if [[ "$line" =~ ^[[:space:]]{4}[^[:space:]-] || "$line" =~ ^[[:space:]]{2}[^[:space:]] || "$line" =~ ^(volumes|networks): ]]; then
  1613. if [[ "$inserted" == "no" ]]; then
  1614. for mount in "${mounts[@]}"; do
  1615. printf ' - %s\n' "$mount" >> "$tmp_file"
  1616. done
  1617. inserted="yes"
  1618. fi
  1619. in_volumes="no"
  1620. fi
  1621. elif [[ "$in_lightrag" == "yes" && "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != " lightrag:" ]]; then
  1622. if [[ "$inserted" == "no" ]]; then
  1623. printf ' volumes:\n' >> "$tmp_file"
  1624. for mount in "${mounts[@]}"; do
  1625. printf ' - %s\n' "$mount" >> "$tmp_file"
  1626. done
  1627. inserted="yes"
  1628. fi
  1629. in_lightrag="no"
  1630. fi
  1631. printf '%s\n' "$line" >> "$tmp_file"
  1632. if [[ "$line" == " lightrag:" ]]; then
  1633. in_lightrag="yes"
  1634. in_volumes="no"
  1635. elif [[ "$in_lightrag" == "yes" && "$line" == " volumes:" ]]; then
  1636. in_volumes="yes"
  1637. fi
  1638. done < "$compose_file"
  1639. if [[ "$in_lightrag" == "yes" && "$inserted" == "no" ]]; then
  1640. if [[ "$in_volumes" != "yes" ]]; then
  1641. printf ' volumes:\n' >> "$tmp_file"
  1642. fi
  1643. for mount in "${mounts[@]}"; do
  1644. printf ' - %s\n' "$mount" >> "$tmp_file"
  1645. done
  1646. fi
  1647. mv "$tmp_file" "$compose_file"
  1648. }
  1649. inject_lightrag_port_mapping() {
  1650. local compose_file="$1"
  1651. local port_mapping="$2"
  1652. local tmp_file="${compose_file}.ports"
  1653. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  1654. local line
  1655. local in_lightrag="no"
  1656. local in_ports="no"
  1657. local inserted="no"
  1658. if [[ -z "$port_mapping" ]]; then
  1659. return 0
  1660. fi
  1661. : > "$tmp_file"
  1662. while IFS= read -r line || [[ -n "$line" ]]; do
  1663. if [[ "$in_lightrag" == "yes" && "$in_ports" == "yes" ]]; then
  1664. if [[ "$line" =~ ^[[:space:]]{4}[^[:space:]-] || "$line" =~ ^[[:space:]]{2}[^[:space:]] || "$line" =~ ^(volumes|networks): ]]; then
  1665. if [[ "$inserted" == "no" ]]; then
  1666. printf ' - "%s"\n' "$port_mapping" >> "$tmp_file"
  1667. inserted="yes"
  1668. fi
  1669. in_ports="no"
  1670. fi
  1671. elif [[ "$in_lightrag" == "yes" && "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != " lightrag:" ]]; then
  1672. if [[ "$inserted" == "no" ]]; then
  1673. printf ' ports:\n' >> "$tmp_file"
  1674. printf ' - "%s"\n' "$port_mapping" >> "$tmp_file"
  1675. inserted="yes"
  1676. fi
  1677. in_lightrag="no"
  1678. fi
  1679. printf '%s\n' "$line" >> "$tmp_file"
  1680. if [[ "$line" == " lightrag:" ]]; then
  1681. in_lightrag="yes"
  1682. in_ports="no"
  1683. elif [[ "$in_lightrag" == "yes" && "$line" == " ports:" ]]; then
  1684. in_ports="yes"
  1685. fi
  1686. done < "$compose_file"
  1687. if [[ "$in_lightrag" == "yes" && "$inserted" == "no" ]]; then
  1688. if [[ "$in_ports" != "yes" ]]; then
  1689. printf ' ports:\n' >> "$tmp_file"
  1690. fi
  1691. printf ' - "%s"\n' "$port_mapping" >> "$tmp_file"
  1692. fi
  1693. mv "$tmp_file" "$compose_file"
  1694. }
  1695. repair_misplaced_lightrag_depends_on() {
  1696. local compose_file="$1"
  1697. local tmp_file="${compose_file}.repair-lightrag-depends-on"
  1698. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  1699. local line
  1700. local in_services="no"
  1701. local in_lightrag="no"
  1702. local lightrag_has_depends_on="no"
  1703. local candidate_service=""
  1704. local candidate_root_service=""
  1705. local captured_block=""
  1706. local candidate_header=""
  1707. local inserted="no"
  1708. local in_candidate_service="no"
  1709. local skipping_candidate_depends_on="no"
  1710. while IFS= read -r line || [[ -n "$line" ]]; do
  1711. if [[ "$line" == "services:" ]]; then
  1712. in_services="yes"
  1713. continue
  1714. fi
  1715. if [[ "$in_services" == "yes" && "$line" =~ ^[^[:space:]] && "$line" != "services:" ]]; then
  1716. break
  1717. fi
  1718. if [[ "$in_services" != "yes" ]]; then
  1719. continue
  1720. fi
  1721. if [[ "$in_lightrag" == "yes" ]]; then
  1722. if [[ "$line" == " depends_on:" ]]; then
  1723. lightrag_has_depends_on="yes"
  1724. break
  1725. fi
  1726. if [[ "$line" =~ ^[[:space:]]{2}([A-Za-z0-9_-]+):[[:space:]]*$ ]] && \
  1727. [[ "${BASH_REMATCH[1]}" != "lightrag" ]]; then
  1728. candidate_service="${BASH_REMATCH[1]}"
  1729. candidate_root_service="$(_managed_service_root_name "$candidate_service")"
  1730. if [[ -z "$candidate_root_service" || "$candidate_root_service" == "milvus" ]]; then
  1731. break
  1732. fi
  1733. in_lightrag="no"
  1734. fi
  1735. continue
  1736. fi
  1737. if [[ "$line" == " lightrag:" ]]; then
  1738. in_lightrag="yes"
  1739. continue
  1740. fi
  1741. if [[ -n "$candidate_service" ]]; then
  1742. if [[ "$line" == " depends_on:" ]]; then
  1743. captured_block=" depends_on:"$'\n'
  1744. continue
  1745. fi
  1746. if [[ -n "$captured_block" ]]; then
  1747. if [[ "$line" =~ ^[[:space:]]{6} ]]; then
  1748. captured_block+="${line}"$'\n'
  1749. continue
  1750. fi
  1751. break
  1752. fi
  1753. if [[ "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != " ${candidate_service}:" ]]; then
  1754. break
  1755. fi
  1756. if [[ "$line" =~ ^[^[:space:]] ]]; then
  1757. break
  1758. fi
  1759. fi
  1760. done < "$compose_file"
  1761. if [[ "$lightrag_has_depends_on" == "yes" || -z "$captured_block" || -z "$candidate_service" ]]; then
  1762. return 0
  1763. fi
  1764. candidate_header=" ${candidate_service}:"
  1765. : > "$tmp_file"
  1766. in_lightrag="no"
  1767. while IFS= read -r line || [[ -n "$line" ]]; do
  1768. if [[ "$skipping_candidate_depends_on" == "yes" ]]; then
  1769. if [[ "$line" =~ ^[[:space:]]{6} ]]; then
  1770. continue
  1771. fi
  1772. skipping_candidate_depends_on="no"
  1773. fi
  1774. if [[ "$in_lightrag" == "yes" && "$inserted" == "no" ]] && \
  1775. [[ ( "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != " lightrag:" ) || "$line" =~ ^[^[:space:]] ]]; then
  1776. printf '%s' "$captured_block" >> "$tmp_file"
  1777. inserted="yes"
  1778. in_lightrag="no"
  1779. fi
  1780. if [[ "$line" == "$candidate_header" ]]; then
  1781. in_candidate_service="yes"
  1782. elif [[ "$in_candidate_service" == "yes" ]] && \
  1783. [[ ( "$line" =~ ^[[:space:]]{2}[^[:space:]] && "$line" != "$candidate_header" ) || "$line" =~ ^[^[:space:]] ]]; then
  1784. in_candidate_service="no"
  1785. fi
  1786. if [[ "$in_candidate_service" == "yes" && "$line" == " depends_on:" ]]; then
  1787. skipping_candidate_depends_on="yes"
  1788. continue
  1789. fi
  1790. printf '%s\n' "$line" >> "$tmp_file"
  1791. if [[ "$line" == " lightrag:" ]]; then
  1792. in_lightrag="yes"
  1793. fi
  1794. done < "$compose_file"
  1795. if [[ "$in_lightrag" == "yes" && "$inserted" == "no" ]]; then
  1796. printf '%s' "$captured_block" >> "$tmp_file"
  1797. fi
  1798. mv "$tmp_file" "$compose_file"
  1799. }
  1800. inject_lightrag_environment_overrides() {
  1801. local compose_file="$1"
  1802. shift
  1803. inject_service_environment_overrides "$compose_file" "lightrag" "$@"
  1804. }
  1805. inject_lightrag_depends_on() {
  1806. local compose_file="$1"
  1807. shift
  1808. local candidate_service
  1809. local managed_services=()
  1810. local tmp_file="${compose_file}.depends-on"
  1811. _FILE_OPS_CLEANUP_TMP+=("$tmp_file")
  1812. local line
  1813. local in_lightrag="no"
  1814. local in_depends_on="no"
  1815. local inserted="no"
  1816. local insert_blank_after_depends_on="no"
  1817. local current_dep_name=""
  1818. local current_dep_block=""
  1819. local dep_name=""
  1820. local dep_tail=""
  1821. local dep_service=""
  1822. declare -A preserved_dep_blocks=()
  1823. declare -A preserved_dep_seen=()
  1824. local -a preserved_dep_order=()
  1825. for candidate_service in "$@"; do
  1826. if _is_wizard_managed_root_service_name "$candidate_service"; then
  1827. managed_services+=("$candidate_service")
  1828. fi
  1829. done
  1830. _record_preserved_depends_on_entry() {
  1831. local service_name="$1"
  1832. local block="$2"
  1833. if [[ -z "$service_name" ]] || _is_wizard_managed_root_service_name "$service_name"; then
  1834. return 0
  1835. fi
  1836. if [[ -n "${preserved_dep_seen[$service_name]+set}" ]]; then
  1837. return 0
  1838. fi
  1839. preserved_dep_seen["$service_name"]=1
  1840. preserved_dep_order+=("$service_name")
  1841. preserved_dep_blocks["$service_name"]="$block"
  1842. }
  1843. _flush_current_depends_on_entry() {
  1844. if [[ -z "$current_dep_name" ]]; then
  1845. return 0
  1846. fi
  1847. _record_preserved_depends_on_entry "$current_dep_name" "$current_dep_block"
  1848. current_dep_name=""
  1849. current_dep_block=""
  1850. }
  1851. _trim_trailing_blank_lines() {
  1852. local file="$1"
  1853. local trim_file="${file}.trim"
  1854. _FILE_OPS_CLEANUP_TMP+=("$trim_file")
  1855. awk '
  1856. { lines[NR] = $0 }
  1857. END {
  1858. last = NR
  1859. while (last > 0 && lines[last] == "") {
  1860. last--
  1861. }
  1862. for (i = 1; i <= last; i++) {
  1863. print lines[i]
  1864. }
  1865. }
  1866. ' "$file" > "$trim_file"
  1867. mv "$trim_file" "$file"
  1868. }
  1869. _write_lightrag_depends_on_block() {
  1870. local managed_service
  1871. local preserved_service
  1872. if ((${#preserved_dep_order[@]} == 0 && ${#managed_services[@]} == 0)); then
  1873. inserted="yes"
  1874. return 0
  1875. fi
  1876. printf ' depends_on:\n' >> "$tmp_file"
  1877. for preserved_service in "${preserved_dep_order[@]}"; do
  1878. printf '%s' "${preserved_dep_blocks[$preserved_service]}" >> "$tmp_file"
  1879. done
  1880. for managed_service in "${managed_services[@]}"; do
  1881. printf ' %s:\n' "$managed_service" >> "$tmp_file"
  1882. printf ' condition: service_healthy\n' >> "$tmp_file"
  1883. done
  1884. inserted="yes"
  1885. }
  1886. : > "$tmp_file"
  1887. while IFS= read -r line || [[ -n "$line" ]]; do
  1888. if [[ "$in_depends_on" == "yes" ]]; then
  1889. if [[ -n "$current_dep_name" && "$line" =~ ^[[:space:]]{8} ]]; then
  1890. current_dep_block+="${line}"$'\n'
  1891. continue
  1892. fi
  1893. if [[ "$line" =~ ^[[:space:]]{6}-[[:space:]](.+)$ ]]; then
  1894. _flush_current_depends_on_entry
  1895. dep_service="$(_strip_wrapping_quotes "${BASH_REMATCH[1]}")"
  1896. _record_preserved_depends_on_entry \
  1897. "$dep_service" \
  1898. "$(printf ' %s:\n condition: service_started\n' "$dep_service")"
  1899. continue
  1900. fi
  1901. if [[ "$line" =~ ^[[:space:]]{6}([A-Za-z0-9_.-]+):[[:space:]]*(.*)$ ]]; then
  1902. _flush_current_depends_on_entry
  1903. dep_name="${BASH_REMATCH[1]}"
  1904. dep_tail="${BASH_REMATCH[2]}"
  1905. current_dep_name="$(_strip_wrapping_quotes "$dep_name")"
  1906. if [[ -n "$dep_tail" ]]; then
  1907. current_dep_block=" ${current_dep_name}: ${dep_tail}"$'\n'
  1908. else
  1909. current_dep_block=" ${current_dep_name}:"$'\n'
  1910. fi
  1911. continue
  1912. fi
  1913. _flush_current_depends_on_entry
  1914. if [[ "$inserted" == "no" ]]; then
  1915. _trim_trailing_blank_lines "$tmp_file"
  1916. _write_lightrag_depends_on_block
  1917. fi
  1918. in_depends_on="no"
  1919. fi
  1920. if [[ "$in_lightrag" == "yes" && "$line" == " depends_on:" ]]; then
  1921. in_depends_on="yes"
  1922. continue
  1923. fi
  1924. if [[ "$in_lightrag" == "yes" && \
  1925. ( "$line" =~ ^[[:space:]]{2}[^[:space:]] || "$line" =~ ^[^[:space:]] ) && \
  1926. "$line" != " lightrag:" ]]; then
  1927. if [[ "$inserted" == "no" ]]; then
  1928. _trim_trailing_blank_lines "$tmp_file"
  1929. _write_lightrag_depends_on_block
  1930. insert_blank_after_depends_on="yes"
  1931. fi
  1932. in_lightrag="no"
  1933. fi
  1934. if [[ "$insert_blank_after_depends_on" == "yes" ]]; then
  1935. printf '\n' >> "$tmp_file"
  1936. insert_blank_after_depends_on="no"
  1937. fi
  1938. printf '%s\n' "$line" >> "$tmp_file"
  1939. if [[ "$line" == " lightrag:" ]]; then
  1940. in_lightrag="yes"
  1941. inserted="no"
  1942. insert_blank_after_depends_on="no"
  1943. in_depends_on="no"
  1944. current_dep_name=""
  1945. current_dep_block=""
  1946. preserved_dep_blocks=()
  1947. preserved_dep_seen=()
  1948. preserved_dep_order=()
  1949. fi
  1950. done < "$compose_file"
  1951. if [[ "$in_depends_on" == "yes" ]]; then
  1952. _flush_current_depends_on_entry
  1953. if [[ "$inserted" == "no" ]]; then
  1954. _trim_trailing_blank_lines "$tmp_file"
  1955. _write_lightrag_depends_on_block
  1956. fi
  1957. elif [[ "$in_lightrag" == "yes" && "$inserted" == "no" ]]; then
  1958. _trim_trailing_blank_lines "$tmp_file"
  1959. _write_lightrag_depends_on_block
  1960. fi
  1961. mv "$tmp_file" "$compose_file"
  1962. }
  1963. # Find the first generated compose file in priority order.
  1964. # Prints the path if found, empty string if not.
  1965. find_generated_compose_file() {
  1966. local repo_root="${REPO_ROOT:-.}"
  1967. local candidates=(
  1968. "final:$repo_root/docker-compose.final.yml"
  1969. "development:$repo_root/docker-compose.development.yml"
  1970. "production:$repo_root/docker-compose.production.yml"
  1971. "custom:$repo_root/docker-compose.custom.yml"
  1972. "local:$repo_root/docker-compose.local.yml"
  1973. )
  1974. local candidate f
  1975. for candidate in "${candidates[@]}"; do
  1976. f="${candidate#*:}"
  1977. if [[ -f "$f" ]]; then
  1978. printf '%s' "$f"
  1979. return 0
  1980. fi
  1981. done
  1982. printf ''
  1983. }
  1984. # Detect service names in a compose file's services: block (excluding lightrag).
  1985. # Prints one service name per line.
  1986. detect_compose_services() {
  1987. local compose_file="$1"
  1988. local in_services="no"
  1989. local line
  1990. if [[ ! -f "$compose_file" ]]; then
  1991. return 0
  1992. fi
  1993. while IFS= read -r line || [[ -n "$line" ]]; do
  1994. if [[ "$line" == "services:" ]]; then
  1995. in_services="yes"
  1996. continue
  1997. fi
  1998. if [[ "$in_services" == "yes" ]]; then
  1999. if [[ "$line" =~ ^[^[:space:]] && "$line" != "services:" ]]; then
  2000. in_services="no"
  2001. continue
  2002. fi
  2003. if [[ "$line" =~ ^[[:space:]]{2}([A-Za-z0-9_-]+):[[:space:]]*$ ]]; then
  2004. local svc_name="${BASH_REMATCH[1]}"
  2005. if [[ "$svc_name" != "lightrag" ]]; then
  2006. printf '%s\n' "$svc_name"
  2007. fi
  2008. fi
  2009. fi
  2010. done < "$compose_file"
  2011. }
  2012. detect_managed_root_services() {
  2013. local compose_file="$1"
  2014. local service_name
  2015. local root_service
  2016. declare -A seen_roots=()
  2017. while IFS= read -r service_name; do
  2018. root_service="$(_managed_service_root_name "$service_name")"
  2019. if [[ -n "$root_service" && -z "${seen_roots[$root_service]+set}" ]]; then
  2020. seen_roots["$root_service"]=1
  2021. printf '%s\n' "$root_service"
  2022. fi
  2023. done < <(detect_compose_services "$compose_file")
  2024. }