Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持使用环境变量设置部分 django settings #1543

Merged
merged 20 commits into from
Jun 11, 2022

Conversation

LeoQuote
Copy link
Collaborator

@LeoQuote LeoQuote commented May 25, 2022

支持使用 环境变量, 以及本地 local_settings.py 覆盖 settings.py 内的配置, 优先级从高到低如下:

  1. local_settings.py
  2. 环境变量
  3. 取环境变量时设置的默认值
  4. 定义环境变量时设置的默认值

这个 pr 可以减少一些重复代码, 比如 docker 版本的 archery 可能仅需更改部分配置, helm 版本的也仅需更改部分配置. 都可以通过 local_settings.py 来实现

local_settings.py 应该放在程序启动的根目录, 也就是 manage.py 的同级目录.

fix #1545

@LeoQuote LeoQuote marked this pull request as draft May 25, 2022 07:01
@codecov
Copy link

codecov bot commented May 25, 2022

Codecov Report

Merging #1543 (f8fcb46) into master (ba1fdb6) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head f8fcb46 differs from pull request most recent head b119fe9. Consider uploading reports for the commit b119fe9 to get more accurate results

@@            Coverage Diff             @@
##           master    #1543      +/-   ##
==========================================
- Coverage   76.81%   76.81%   -0.01%     
==========================================
  Files          92       92              
  Lines       14325    14323       -2     
==========================================
- Hits        11004    11002       -2     
  Misses       3321     3321              
Impacted Files Coverage Δ
sql/models.py 96.56% <100.00%> (-0.02%) ⬇️
sql/utils/sql_review.py 89.88% <100.00%> (ø)
sql/views.py 67.69% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d1ce601...b119fe9. Read the comment docs.

@nick2wang
Copy link
Collaborator

这样确实安全方便好多,上次改settings的api配置漏改了docker导致一堆issue...

@LeoQuote
Copy link
Collaborator Author

LeoQuote commented May 25, 2022

@AceAttorney 你这边能帮忙我交叉验证下吗? 我这里环境还没搭起来, 不过初步看配置项都是可以拿到的

archery/settings.py Show resolved Hide resolved
archery/settings.py Outdated Show resolved Hide resolved
archery/settings.py Show resolved Hide resolved
sql/models.py Show resolved Hide resolved
@AceAttorney
Copy link
Contributor

晚点我测试一下,应该问题不大,不知道Django里有没有类似于 spring cloud kubernetes里可以动态watch comfipmap的组件,如果有的话,后面这些变量可以直接通过在configmap在集群里修改后实现热加载

@LeoQuote
Copy link
Collaborator Author

LeoQuote commented May 27, 2022

晚点我测试一下,应该问题不大,不知道Django里有没有类似于 spring cloud kubernetes里可以动态watch comfipmap的组件,如果有的话,后面这些变量可以直接通过在configmap在集群里修改后实现热加载

k8s 环境里这个没有必要, 直接重启就完了, 而且以 subpath 形式挂载的文件也不会更新

@LeoQuote
Copy link
Collaborator Author

@AceAttorney 我把这个 helm chart 传到 douban/charts 了, 你可以直接添加这个 repo 安装

helm repo add douban https://douban.github.io/charts/
helm install xxx douban/archery -f xxx.yaml

@hhyo
Copy link
Owner

hhyo commented Jun 3, 2022

更新中?

@LeoQuote
Copy link
Collaborator Author

LeoQuote commented Jun 5, 2022

更新中?

这个pr 内容上差不多了, 还夹带了几个我写的 fix, 如果需要的话我分成两三个 pr, 我还在内部运行试验, 估计下周我会把他 ready

@LeoQuote
Copy link
Collaborator Author

LeoQuote commented Jun 7, 2022

@hhyo helm chart 这部分我暂时保留, 其实我比较推荐删掉, 我已经搬到 douban/charts 里面去了, 后续能删掉, 然后用 douban/charts 是最好

@LeoQuote LeoQuote marked this pull request as ready for review June 7, 2022 04:04
@LeoQuote LeoQuote requested a review from hhyo June 7, 2022 04:11
@hhyo
Copy link
Owner

hhyo commented Jun 10, 2022

@hhyo helm chart 这部分我暂时保留, 其实我比较推荐删掉, 我已经搬到 douban/charts 里面去了, 后续能删掉, 然后用 douban/charts 是最好

可以和@AceAttorney 确认下,这块以前是他维护,如果能够直接使用douban的,可以删除,文档也可以再增加这个部署方式

PS: 后面可以帮看下dockerfile以及compose是否可以改进,worker和server以及ng是不是分开更好,这块我不是很熟悉😊

hhyo
hhyo previously approved these changes Jun 10, 2022
@LeoQuote
Copy link
Collaborator Author

老实讲 dockerfile 是有点烂, 不过也是勉强能用有空我再另提pr

@hhyo
Copy link
Owner

hhyo commented Jun 10, 2022

4.0需要新增一个参数 CSRF_TRUSTED_ORIGINS = ['https://demo.archerydms.com'] 也一并处理一下

#1565

@LeoQuote
Copy link
Collaborator Author

看来要等 CI 修了再merge

@hhyo
Copy link
Owner

hhyo commented Jun 11, 2022

看来要等 CI 修了再merge

合了再修了,昨天看了是过了才合的,可能看劈叉了

@hhyo hhyo merged commit 8aeda2d into hhyo:master Jun 11, 2022
@hhyo
Copy link
Owner

hhyo commented Jun 11, 2022

这块需要补个基础的升级文档,demo直接就起不来了😄

@LeoQuote
Copy link
Collaborator Author

@hhyo 不应该啊, 报什么错?

@LeoQuote LeoQuote deleted the support_env branch June 11, 2022 04:58
@sugniii
Copy link

sugniii commented Aug 3, 2022

支持使用环境变量,以及本地local_settings.py覆盖settings.py内部的配置,优先级从高到低变:

  1. local_settings.py
  2. 环境变量
  3. 取环境变量时设置的默认值
  4. 定义环境变量时设置的默认值

这个更改版本的代码可能会减少一些重复配置,例如 docker 的可能选项部分,可以控制部分 pr 的一些可能更改配置。都可以通过local_settings.py来实现

local_settings.py应该是非法程序启动的根目录,也就是manage.py的同级目录。

修复 #1545

docker image version: 1.8.5
环境变量
DATABASE_URL: mysql://ip:3306/archery
执行命令:python3 manage.py makemigrations sql
报错连接数据失败: 127.0.0.1不是我所填的ip地址
我没有其他配置,只配置了三个环境变量(组件都没有密码):
NGINX_PORT: 9123
DATABASE_URL: mysql://mysql:3306/archery
CACHE_URL: redis://redis:6379/0
请问配置了环境变量后还需要配置你上面的local_settings.py嘛.
还是需要配置/opt/archery/archery/settings.py
因为我之前使用1.8.3版本的时候是直接映射volume将/opt/archery/archery/settings.py文件映射到主机进行配置.
我希望可以使用环境变量来配置这些东西.
如果可以的话帮忙解答一下,万分感谢

@FSerKevin
Copy link

/docker-compose/archery/settings.py 文件怎么用 方便解答下给个示例吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ bug ] 新建实例建表的时候报错, primary key 太长
6 participants