-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
90 lines (70 loc) · 2.33 KB
/
Copy pathmain.py
File metadata and controls
90 lines (70 loc) · 2.33 KB
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
import sys
import time
import logging
from pathlib import Path
from config import STACKS_ROOT, setup_logging, load_config
from git import (
ensure_git_safe_directory,
ensure_repo,
update_repo,
repo_changed,
get_current_head,
get_changed_files_between,
)
from stacks import find_stacks, get_changed_stacks
from deploy import deploy_stack
from config import setup_logging, setup_notifications
def deploy_all_stacks(stacks_root: Path, branch: str):
update_repo(branch)
stacks = find_stacks(stacks_root)
logging.info("Found %d stacks", len(stacks))
for stack in stacks:
try:
deploy_stack(stack)
except Exception:
logging.exception("Stack %s failed to deploy", stack.name)
def deploy_changed_stacks(branch, stacks_root: Path):
old_head = get_current_head()
update_repo(branch)
new_head = get_current_head()
changed_files = get_changed_files_between(old_head, new_head)
stacks = get_changed_stacks(changed_files, stacks_root)
if not stacks:
logging.info("No stack changes detected")
return
logging.info("Stacks to deploy: %s", ", ".join(s.name for s in stacks))
for stack in stacks:
try:
deploy_stack(stack)
except Exception:
logging.exception("Deploy failed for stack: %s", stack.name)
def main():
setup_logging()
setup_notifications()
config = load_config()
if not config["repo"]:
logging.error("GIT_REPO is required")
sys.exit(1)
ensure_git_safe_directory()
ensure_repo(config["repo"], config["branch"])
stacks_root = (
STACKS_ROOT / config["compose_dir"]
if config["compose_dir"]
else STACKS_ROOT
)
deploy_all_stacks(stacks_root, config["branch"])
while True:
try:
if repo_changed(config["branch"]):
logging.info("Repository changes detected")
if config["force_full_deploy"]:
deploy_all_stacks(stacks_root, config["branch"])
else:
deploy_changed_stacks(config["branch"], stacks_root)
else:
logging.info("No repository changes")
except Exception:
logging.exception("Deploy cycle failed")
time.sleep(config["interval"])
if __name__ == "__main__":
main()