Skip to content

Commit

Permalink
改善环境变量管理方式
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryJi529 committed Apr 26, 2024
1 parent 8c39903 commit f2f1a9a
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,36 @@ class Tool:


class Env:
GHCR_USERANME = os.getenv("GHCR_USERANME")
GHCR_PAT = os.getenv("GHCR_PAT")
CLOUD_USERNAME = os.getenv("CLOUD_USERNAME")
CLOUD_PASSWORD = os.getenv("CLOUD_PASSWORD")
PUBLIC_IP = os.getenv("PUBLIC_IP")
DEV_PASSWORD = os.getenv("DEV_PASSWORD")
DJANGO_SECRET_KEY = os.getenv("DJANGO_SECRET_KEY")
EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD")
TENCENT_SMS_APP_KEY = os.getenv("TENCENT_SMS_APP_KEY")
RECAPTCHA_PUBLIC_KEY = os.getenv("RECAPTCHA_PUBLIC_KEY")
RECAPTCHA_PRIVATE_KEY = os.getenv("RECAPTCHA_PRIVATE_KEY")
REDIS_PASSWORD = os.getenv("REDIS_PASSWORD")
MYSQL_ROOT_PASSWORD = os.getenv("MYSQL_ROOT_PASSWORD")
MORNINGSTAR_USERNAME = os.getenv("MORNINGSTAR_USERNAME")
MORNINGSTAR_PASSWORD = os.getenv("MORNINGSTAR_PASSWORD")


ENV_DICT = {
key: value for key, value in Env.__dict__.items() if not key.startswith("__")
}


class MorningstarConnection(Connection):
def __init__(self):
super().__init__(
host=Env.PUBLIC_IP,
user=Env.CLOUD_USERNAME,
connect_kwargs={"password": Env.CLOUD_PASSWORD},
connect_kwargs={
"password": Env.CLOUD_PASSWORD,
},
)


Expand Down Expand Up @@ -438,13 +456,13 @@ def updateDocker(c: Connection):
"git clone https://github.com/HenryJi529/OpenMorningstar.git ~/morningstar"
)

def update_file_with_secret():
c.run(
"source ~/.zshrc && echo \"\nenvironment=DJANGO_SECRET_KEY='${DJANGO_SECRET_KEY}',EMAIL_HOST_PASSWORD='${EMAIL_HOST_PASSWORD}',TENCENT_SMS_APP_KEY='${TENCENT_SMS_APP_KEY}',RECAPTCHA_PUBLIC_KEY='${RECAPTCHA_PUBLIC_KEY}',RECAPTCHA_PRIVATE_KEY='${RECAPTCHA_PRIVATE_KEY}',MYSQL_ROOT_PASSWORD='${MYSQL_ROOT_PASSWORD}',REDIS_PASSWORD='${REDIS_PASSWORD}'\" >> ~/morningstar/scripts/deploy/django/supervise.conf"
)

colored_print("添加密钥...")
update_file_with_secret()
envCommaRow = ",".join(
[f"{key}='{value}'" for key, value in ENV_DICT.items()]
)
c.run(
f'echo "\n{envCommaRow}" >> ~/morningstar/scripts/deploy/django/supervise.conf'
)

colored_print("清理docker(除volume)...")
try:
Expand All @@ -454,8 +472,11 @@ def update_file_with_secret():
c.run("docker system prune -af")

colored_print("部署容器...")
envDoubleAndRow = " && ".join(
[f"{key}='{value}'" for key, value in ENV_DICT.items()]
)
c.run(
"source ~/.zshrc && cd ~/morningstar/scripts/deploy; docker-compose build && docker-compose up -d"
f"{envDoubleAndRow} && cd ~/morningstar/scripts/deploy; docker-compose build && docker-compose up -d"
)

colored_print("转移媒体文件...")
Expand Down Expand Up @@ -573,9 +594,13 @@ def publicPackage():
def checkCert():
conn = MorningstarConnection()
colored_print("Let's Encrypt证书剩余时间: ")
conn.run(
"source ~/.zshrc && docker exec morningstar_nginx certbot certificates"
)
conn.run("docker exec morningstar_nginx certbot certificates")

@staticmethod
def updateCert():
conn = MorningstarConnection()
colored_print("更新Let's Encrypt证书: ")
conn.run("docker exec morningstar_nginx certbot renew")

@staticmethod
def syncLedger():
Expand All @@ -593,6 +618,11 @@ def syncLedger():
"docker cp ~/morningstar/scripts/deploy/beancount morningstar_beancount:/root/"
)

@staticmethod
def loginGHCR():
conn = MorningstarConnection()
conn.run(f"docker login ghcr.io -u {Env.GHCR_USERANME} -p {Env.GHCR_PAT}")

@staticmethod
def _test():
runcmd('echo "\033[33mThis is a test...\033[0m"')
Expand All @@ -618,7 +648,9 @@ def _test():
"publicArchive",
"publicPackage",
"checkCert",
"updateCert",
"syncLedger",
"loginGHCR",
"_test",
],
)
Expand Down

0 comments on commit f2f1a9a

Please sign in to comment.