-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
100 lines (73 loc) · 3.36 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from fastapi import FastAPI, Body, HTTPException, Depends
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from util import gunicorn_process, is_amarillo_cd, run_command
import signal
import logging
import docker
import uvicorn
from pydantic_settings import BaseSettings
class Config(BaseSettings):
dev_compose_folder: str
dev_compose_env_file: str = "dev.env"
config = Config(_env_file='.env', _env_file_encoding='utf-8')
logging.basicConfig(
format='[%(asctime)s] [%(levelname)s] %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
logger = logging.getLogger(__name__)
logger.info(f"CD started.")
docker_client = docker.from_env()
app = FastAPI()
security = HTTPBasic()
@app.post("/payload")
async def payload(payload: dict = Body(...)):
if await is_amarillo_cd(payload):
logger.warning(f"CD sending SIGTERM to itself.")
# https://docs.gunicorn.org/en/stable/signals.html
gunicorn_process().send_signal(signal.SIGTERM)
return
is_main_branch = payload.get('ref') == 'refs/heads/main'
if is_main_branch:
logger.warning(("CD trying to stpp the amarillo-dev container"))
container = docker_client.containers.get("amarillo-dev")
logger.warning((f"CD stopping container {container}"))
container.stop()
logger.warning((f"CD container stopped"))
#logger.warning(f"CD sending SIGTERM to amarillo-dev.")
#gunicorn_process(process_name="amarillo-dev").send_signal(signal.SIGTERM)
return
is_release_branch = payload.get('ref') == 'refs/heads/release'
if is_release_branch:
logger.warning(f"CD sending SIGTERM to amarillo-prod.")
gunicorn_process(process_name="amarillo-prod").send_signal(signal.SIGTERM)
return
print(payload)
def verify_secret(credentials: HTTPBasicCredentials = Depends(security)):
with open('secret.txt', 'r') as secret_file:
secret = secret_file.read().strip()
if credentials.password != secret:
raise HTTPException(status_code=401, detail="Incorrect deploy secret")
@app.post("/mitanand")
async def payload_mitanand(payload: dict = Body(...), security = Depends(verify_secret)):
logger.warning(("CD trying to stop the amarillo-mitanand container"))
container = docker_client.containers.get("amarillo-mitanand")
logger.warning((f"CD stopping container {container}"))
container.stop()
logger.warning((f"CD container stopped"))
@app.post("/dev")
async def payload_dev(payload: dict = Body(...), security = Depends(verify_secret)):
project_name = "dev"
compose_folder = config.dev_compose_folder
env_file_path = config.dev_compose_env_file
logger.info(f"Running git pull command for {project_name}")
run_command("pwd", compose_folder)
run_command("git pull", compose_folder)
logger.info(f"Running docker pull command for {project_name}")
pull_command = f"docker compose --env-file {env_file_path} --profile enhancer --profile generator pull"
run_command(pull_command, compose_folder)
logger.info(f"Running up command for {project_name}")
up_command = f"docker compose --env-file {env_file_path} --profile enhancer --profile generator -p {project_name} up -d"
run_command(up_command, compose_folder)
logger.info(f"CD ran for {project_name}")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8888)