-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_webserver_deploy_changes.py
More file actions
84 lines (62 loc) · 2.46 KB
/
Copy pathhello_webserver_deploy_changes.py
File metadata and controls
84 lines (62 loc) · 2.46 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
"""
This script pulls new changes from origin in hello-webserver.git repository, and if there's a new commit it runs
the hello-webserver-deploy.yml playbook.
That process is repeated every 30 seconds.
Prerequisites:
* pip install GitPython
Examples executing this script:
python hello_webserver_deploy_changes.py
python3 hello_webserver_deploy_changes.py --git-repo-root /path/to/hello-webserver --environment test
"""
import git
import time
import argparse
import subprocess
from pathlib import Path
PLAYBOOK_NAME = "hello-webserver-deploy.yml"
SCRIPT_ROOT = Path(__file__).parent.as_posix()
def pull_changes(git_dir: str) -> git.Commit:
"""
Pull changes from repository
params:
git_dir (str): path to the git repository
returns: git.Commit - latest commit
"""
repo = git.Repo(git_dir)
repo.remote("origin").pull()
return next(repo.iter_commits())
def push_changes_to_backend(git_dir: str, environment: str):
"""
Run ansible playbook to push changes to backend
params:
git_dir (str): path to the git repository
environment (str): identifier for the ansible host inventory file
"""
command = f'ansible-playbook {PLAYBOOK_NAME} -i hosts/{environment}.ini -e "hello_webserver_root={git_dir}" -vv'
subprocess.run(command, check=True, shell=True)
def deploy(git_dir: str, environment: str):
"""
Main function. Goes over an infinite loop looking for new commits every 30 seconds
params:
git_dir (str): path to the git repository
environment (str): identifier for the ansible host inventory file
"""
latest_commit_epoch = 0
while True:
last_commit = pull_changes(git_dir)
if last_commit.committed_date > latest_commit_epoch:
latest_commit_epoch = last_commit.committed_date
push_changes_to_backend(git_dir, environment)
else:
print("No new commits")
time.sleep(30)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--git-repo-root", type=str,
help="Path for git repository. Default: <script_dir>/hello-webserver",
default=Path(SCRIPT_ROOT, "hello-webserver").as_posix())
parser.add_argument("--environment", type=str,
help="Targeting environment (test, prod)",
default="test")
args = parser.parse_args()
deploy(args.git_repo_root, args.environment)