This commit is contained in:
@@ -7,31 +7,21 @@ def git_diff():
|
||||
res = subprocess.run(f"git diff --name-only {args[1]} {args[2]}", capture_output=True, shell=True, text=True)
|
||||
return [x for x in res.stdout.strip().split("\n") if "tasks/" in x or "roles/" in x]
|
||||
|
||||
def construct_command(tag = None, host = None):
|
||||
def construct_command(tags = []):
|
||||
command = f"ANSIBLE_CONFIG=ansible.cfg /usr/bin/ansible-playbook main.yml --vault-password-file ~/.vault_pass.txt"
|
||||
|
||||
if host:
|
||||
command += f" -l {host}"
|
||||
if tag:
|
||||
command += f" --tags {tag}_deploy"
|
||||
command += f" --tags {",".join(tags)}"
|
||||
|
||||
return command
|
||||
|
||||
def deploy(tag = None, host = None):
|
||||
command = construct_command(tag, host)
|
||||
|
||||
if tag:
|
||||
print(f"[MAIN] Deploying {tag}...")
|
||||
else:
|
||||
print(f"[MAIN] Deploying host {host}...")
|
||||
def deploy(tags):
|
||||
print(f"[MAIN] Deploying...")
|
||||
command = construct_command(tags)
|
||||
res = subprocess.run(command, shell=True)
|
||||
|
||||
return res.returncode == 0
|
||||
|
||||
def main():
|
||||
dir_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../')
|
||||
diff = git_diff()
|
||||
print(diff)
|
||||
|
||||
# containers that need special treatment
|
||||
removed_containers = []
|
||||
@@ -39,10 +29,10 @@ def main():
|
||||
"tasks/qbittorrent.yml",
|
||||
"tasks/jackett.yml"
|
||||
]
|
||||
managed_roles = [
|
||||
"roles/fivem",
|
||||
"roles/gitea-runner",
|
||||
"roles/traefik"
|
||||
ignore_deploys_for = [
|
||||
"tasks/runner",
|
||||
"templates/runner",
|
||||
"roles/docker"
|
||||
]
|
||||
|
||||
# special actions
|
||||
@@ -55,35 +45,22 @@ def main():
|
||||
# clean up the diff
|
||||
new_diff = []
|
||||
for file in diff:
|
||||
task_name = f"{file.split('/')[0]}/{file.split('/')[1]}"
|
||||
split_string = file.split("/")
|
||||
service_name = split_string[1].split(".")[0] + "_deploy" if "." in split_string[1] else split_string[1] + "_deploy"
|
||||
|
||||
# i'm not proud of this either
|
||||
if not os.path.exists(os.path.join(dir_path, file)):
|
||||
if "roles" in file and not os.path.exists(os.path.join(dir_path, task_name)) and task_name in managed_roles:
|
||||
if "roles" in file and not os.path.exists(os.path.join(dir_path, service_name)):
|
||||
print(f"[MAIN] '{task_name}' role removed, marking for cleanup..")
|
||||
removed_containers.append(task_name)
|
||||
removed_containers.append(service_name)
|
||||
elif "tasks" in task_name:
|
||||
print(f"[MAIN] '{task_name}' non-existent, marking for cleanup..")
|
||||
removed_containers.append(task_name)
|
||||
elif "roles" in file:
|
||||
if task_name in managed_roles:
|
||||
if task_name not in new_diff:
|
||||
new_diff.append(task_name)
|
||||
elif "tasks" in file:
|
||||
new_diff.append(file.split(".")[0])
|
||||
removed_containers.append(service_name)
|
||||
else:
|
||||
new_diff.append(file)
|
||||
if service_name not in ignore_deploys_for and service_name not in new_diff:
|
||||
new_diff.append(service_name)
|
||||
|
||||
deployed = []
|
||||
failed = []
|
||||
|
||||
for task in new_diff:
|
||||
deployment = deploy(tag=task.split("/")[1])
|
||||
|
||||
if not deployment:
|
||||
failed.append(task)
|
||||
else:
|
||||
deployed.append(task)
|
||||
deployed = deploy(tag=",".join(new_diff))
|
||||
|
||||
for task in removed_containers:
|
||||
print(f"[MAIN] Attempting to remove containers related to '{task}'...")
|
||||
@@ -101,20 +78,19 @@ def main():
|
||||
subprocess.run("/usr/bin/docker image prune -f", shell=True)
|
||||
subprocess.run("/usr/bin/docker container prune -f", shell=True)
|
||||
|
||||
if len(failed) <= 0 and len(deployed) > 0:
|
||||
if deployed:
|
||||
print("\n---------------------")
|
||||
print(" Deployment succeeded!")
|
||||
print(f" All tasks: {', '.join(deployed)}")
|
||||
print(f" All tasks: {', '.join(new_diff)}")
|
||||
print("---------------------\n")
|
||||
sys.exit(0)
|
||||
elif len(failed) > 0:
|
||||
elif not deployed:
|
||||
print("\n---------------------")
|
||||
print(" Deployment failed!")
|
||||
print(f" Failed tasks: {', '.join(failed)}")
|
||||
print(f" All tasks: {', '.join(deployed)}")
|
||||
print(f" All tasks: {', '.join(new_diff)}")
|
||||
print("---------------------\n")
|
||||
sys.exit(1)
|
||||
elif len(deployed) <= 0:
|
||||
elif len(new_diff) <= 0:
|
||||
print("[MAIN] Successfully executed, no tasks required execution")
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ scrape_configs:
|
||||
- targets: ["monitoring_prometheus:9090"]
|
||||
- job_name: "node-exporter"
|
||||
static_configs:
|
||||
- targets: ["172.16.0.25:9100"]
|
||||
- targets: ["172.16.0.44:9100"]
|
||||
- job_name: "cadvisor"
|
||||
static_configs:
|
||||
- targets: ["monitoring_cadvisor:8080"]
|
||||
Reference in New Issue
Block a user