mirror of
https://github.com/langgenius/dify.git
synced 2026-03-02 22:36:41 +08:00
Signed-off-by: majiayu000 <1835304752@qq.com> Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: yihong0618 <zouzou0208@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: 盐粒 Yanli <yanli@dify.ai> Co-authored-by: wangxiaolei <fatelei@gmail.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Cursx <33718736+Cursx@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: lif <1835304752@qq.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: fenglin <790872612@qq.com> Co-authored-by: qiaofenglin <qiaofenglin@baidu.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: TomoOkuyama <49631611+TomoOkuyama@users.noreply.github.com> Co-authored-by: Tomo Okuyama <tomo.okuyama@intersystems.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: zyssyz123 <916125788@qq.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: CodingOnStar <hanxujiang@dify.ai> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: Xiangxuan Qu <fghpdf@outlook.com> Co-authored-by: fghpdf <fghpdf@users.noreply.github.com> Co-authored-by: coopercoder <whitetiger0127@163.com> Co-authored-by: zhaiguangpeng <zhaiguangpeng@didiglobal.com> Co-authored-by: Junyan Qin (Chin) <rockchinq@gmail.com> Co-authored-by: E.G <146701565+GlobalStar117@users.noreply.github.com> Co-authored-by: GlobalStar117 <GlobalStar117@users.noreply.github.com> Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: heyszt <270985384@qq.com> Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: moonpanda <chuanzegao@163.com> Co-authored-by: warlocgao <warlocgao@tencent.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: KVOJJJin <jzongcode@gmail.com> Co-authored-by: eux <euxx@users.noreply.github.com> Co-authored-by: bangjiehan <bangjiehan@gmail.com> Co-authored-by: FFXN <31929997+FFXN@users.noreply.github.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: Nie Ronghua <nieronghua@sf-express.com> Co-authored-by: JQSevenMiao <141806521+JQSevenMiao@users.noreply.github.com> Co-authored-by: jiasiqi <jiasiqi3@tal.com> Co-authored-by: Seokrin Taron Sung <sungsjade@gmail.com> Co-authored-by: CrabSAMA <40541269+CrabSAMA@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: yessenia <yessenia.contact@gmail.com> Co-authored-by: Jax <anobaka@qq.com> Co-authored-by: niveshdandyan <155956228+niveshdandyan@users.noreply.github.com> Co-authored-by: OSS Contributor <oss-contributor@example.com> Co-authored-by: niveshdandyan <niveshdandyan@users.noreply.github.com> Co-authored-by: Sean Kenneth Doherty <Smaster7772@gmail.com>
113 lines
4.1 KiB
Python
Executable File
113 lines
4.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
|
|
def parse_env_example(file_path):
|
|
"""
|
|
Parses the .env.example file and returns a dictionary with variable names as keys and default values as values.
|
|
"""
|
|
env_vars = {}
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
for line_number, line in enumerate(f, 1):
|
|
line = line.strip()
|
|
# Ignore empty lines and comments
|
|
if not line or line.startswith("#"):
|
|
continue
|
|
# Use regex to parse KEY=VALUE
|
|
match = re.match(r"^([^=]+)=(.*)$", line)
|
|
if match:
|
|
key = match.group(1).strip()
|
|
value = match.group(2).strip()
|
|
# Remove possible quotes around the value
|
|
if (value.startswith('"') and value.endswith('"')) or (
|
|
value.startswith("'") and value.endswith("'")
|
|
):
|
|
value = value[1:-1]
|
|
env_vars[key] = value
|
|
else:
|
|
print(f"Warning: Unable to parse line {line_number}: {line}")
|
|
return env_vars
|
|
|
|
|
|
def generate_shared_env_block(env_vars, anchor_name="shared-api-worker-env"):
|
|
"""
|
|
Generates a shared environment variables block as a YAML string.
|
|
"""
|
|
lines = [f"x-shared-env: &{anchor_name}"]
|
|
for key, default in env_vars.items():
|
|
if key == "COMPOSE_PROFILES":
|
|
continue
|
|
# If default value is empty, use ${KEY:-}
|
|
if default == "":
|
|
lines.append(f" {key}: ${{{key}:-}}")
|
|
else:
|
|
# If default value contains special characters, wrap it in quotes
|
|
if re.search(r"[:\s]", default):
|
|
default = f"{default}"
|
|
lines.append(f" {key}: ${{{key}:-{default}}}")
|
|
return "\n".join(lines)
|
|
|
|
|
|
def insert_shared_env(template_path, output_path, shared_env_block, header_comments):
|
|
"""
|
|
Inserts the shared environment variables block and header comments into the template file,
|
|
removing any existing x-shared-env anchors, and generates the final docker-compose.yaml file.
|
|
"""
|
|
with open(template_path, "r", encoding="utf-8") as f:
|
|
template_content = f.read()
|
|
|
|
# Remove existing x-shared-env: &shared-api-worker-env lines
|
|
template_content = re.sub(
|
|
r"^x-shared-env: &shared-api-worker-env\s*\n?",
|
|
"",
|
|
template_content,
|
|
flags=re.MULTILINE,
|
|
)
|
|
|
|
# Prepare the final content with header comments and shared env block
|
|
final_content = f"{header_comments}\n{shared_env_block}\n\n{template_content}"
|
|
|
|
with open(output_path, "w", encoding="utf-8") as f:
|
|
f.write(final_content)
|
|
print(f"Generated {output_path}")
|
|
|
|
|
|
def main():
|
|
env_example_path = ".env.example"
|
|
template_path = "docker-compose-template.yaml"
|
|
output_path = "docker-compose.yaml"
|
|
anchor_name = "shared-api-worker-env" # Can be modified as needed
|
|
|
|
# Define header comments to be added at the top of docker-compose.yaml
|
|
header_comments = (
|
|
"# ==================================================================\n"
|
|
"# WARNING: This file is auto-generated by generate_docker_compose\n"
|
|
"# Do not modify this file directly. Instead, update the .env.example\n"
|
|
"# or docker-compose-template.yaml and regenerate this file.\n"
|
|
"# ==================================================================\n"
|
|
)
|
|
|
|
# Check if required files exist
|
|
for path in [env_example_path, template_path]:
|
|
if not os.path.isfile(path):
|
|
print(f"Error: File {path} does not exist.")
|
|
sys.exit(1)
|
|
|
|
# Parse .env.example file
|
|
env_vars = parse_env_example(env_example_path)
|
|
|
|
if not env_vars:
|
|
print("Warning: No environment variables found in .env.example.")
|
|
|
|
# Generate shared environment variables block
|
|
shared_env_block = generate_shared_env_block(env_vars, anchor_name)
|
|
|
|
# Insert shared environment variables block and header comments into the template
|
|
insert_shared_env(template_path, output_path, shared_env_block, header_comments)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|