simplify deploy script
This commit is contained in:
@@ -2,6 +2,16 @@ import sys
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
vpn_containers = [ # containers that need to be recreated re: vpn
|
||||||
|
"tasks/qbittorrent.yml",
|
||||||
|
"tasks/jackett.yml"
|
||||||
|
]
|
||||||
|
ignore_deploys_for = [ # don't auto-deploy these
|
||||||
|
"tasks/runner",
|
||||||
|
"templates/runner",
|
||||||
|
"roles/docker"
|
||||||
|
]
|
||||||
|
|
||||||
def git_diff():
|
def git_diff():
|
||||||
args = sys.argv
|
args = sys.argv
|
||||||
res = subprocess.run(f"git diff --name-only {args[1]} {args[2]}", capture_output=True, shell=True, text=True)
|
res = subprocess.run(f"git diff --name-only {args[1]} {args[2]}", capture_output=True, shell=True, text=True)
|
||||||
@@ -18,66 +28,51 @@ def deploy(tags):
|
|||||||
res = subprocess.run(command, shell=True)
|
res = subprocess.run(command, shell=True)
|
||||||
return res.returncode == 0
|
return res.returncode == 0
|
||||||
|
|
||||||
|
def get_normalized_task_name(container):
|
||||||
|
if "tasks/" in container:
|
||||||
|
task_name = container.split("/")[1].split(".")[0]
|
||||||
|
elif "roles/" or "templates/" in container:
|
||||||
|
task_name = container.split("/")[1]
|
||||||
|
else:
|
||||||
|
task_name = False
|
||||||
|
|
||||||
|
return task_name
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
dir_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../')
|
dir_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../')
|
||||||
diff = git_diff()
|
diff = git_diff()
|
||||||
|
deployable_containers = []
|
||||||
|
removable_containers = []
|
||||||
|
|
||||||
# containers that need special treatment
|
|
||||||
removed_containers = [] # containers queued for removal
|
|
||||||
vpn_containers = [ # containers that need to be recreated re: vpn
|
|
||||||
"tasks/qbittorrent.yml",
|
|
||||||
"tasks/jackett.yml"
|
|
||||||
]
|
|
||||||
ignore_deploys_for = [ # don't auto-deploy these
|
|
||||||
"tasks/runner",
|
|
||||||
"templates/runner",
|
|
||||||
"roles/docker"
|
|
||||||
]
|
|
||||||
|
|
||||||
# special actions
|
|
||||||
if "tasks/gluetun.yml" in diff:
|
|
||||||
print("[MAIN] Detected Gluetun in diff, recreating dependent containers..")
|
|
||||||
for container in vpn_containers:
|
|
||||||
if container not in diff:
|
|
||||||
diff.append(container)
|
|
||||||
|
|
||||||
# clean up the diff
|
|
||||||
new_diff = []
|
|
||||||
for file in diff:
|
for file in diff:
|
||||||
split_string = file.split("/")
|
if os.path.exists(os.path.join(dir_path, file)):
|
||||||
service_name = split_string[1].split(".")[0] + "_deploy" if "." in split_string[1] else split_string[1] + "_deploy"
|
deployable_containers.append(file)
|
||||||
|
|
||||||
# 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, service_name)):
|
|
||||||
print(f"[MAIN] '{service_name}' role removed, marking for cleanup..")
|
|
||||||
removed_containers.append(service_name)
|
|
||||||
elif "tasks" in file:
|
|
||||||
print(f"[MAIN] '{service_name}' non-existent, marking for cleanup..")
|
|
||||||
removed_containers.append(service_name)
|
|
||||||
else:
|
else:
|
||||||
if service_name not in ignore_deploys_for and service_name not in new_diff:
|
removable_containers.append(file)
|
||||||
new_diff.append(service_name)
|
|
||||||
|
|
||||||
if len(new_diff) > 0:
|
print(f"[MAIN] Deployable: {deployable_containers}")
|
||||||
deployed = deploy(new_diff)
|
print(f"[MAIN] Removable: {removable_containers}")
|
||||||
else:
|
|
||||||
# success, nothing deployed
|
|
||||||
deployed = True
|
|
||||||
|
|
||||||
for task in removed_containers:
|
if len(deployable_containers) > 0:
|
||||||
print(f"[MAIN] Attempting to remove containers related to '{task}'...")
|
to_deploy = []
|
||||||
if "_deploy" in task:
|
for container in deployable_containers:
|
||||||
task_name = task.split("_")[0]
|
task_name = get_normalized_task_name(container)
|
||||||
else:
|
if task_name:
|
||||||
task_name = task
|
to_deploy.append(task_name + "_deploy")
|
||||||
|
|
||||||
result = subprocess.run(
|
if len(to_deploy) > 0:
|
||||||
|
result = deploy(to_deploy)
|
||||||
|
|
||||||
|
if len(removable_containers) > 0:
|
||||||
|
for container in removable_containers:
|
||||||
|
task_name = get_normalized_task_name(container)
|
||||||
|
result = subprocess.run(
|
||||||
f'/usr/bin/docker ps --filter "name={task_name}" -q',
|
f'/usr/bin/docker ps --filter "name={task_name}" -q',
|
||||||
shell=True,
|
shell=True,
|
||||||
capture_output=True
|
capture_output=True
|
||||||
)
|
)
|
||||||
for line in result.stdout.splitlines():
|
for line in result.stdout.splitlines():
|
||||||
container_id = line.strip().decode("utf8")
|
container_id = line.strip().decode("utf8")
|
||||||
if not container_id:
|
if not container_id:
|
||||||
continue
|
continue
|
||||||
@@ -87,21 +82,5 @@ def main():
|
|||||||
subprocess.run("/usr/bin/docker image prune -f", shell=True)
|
subprocess.run("/usr/bin/docker image prune -f", shell=True)
|
||||||
subprocess.run("/usr/bin/docker container prune -f", shell=True)
|
subprocess.run("/usr/bin/docker container prune -f", shell=True)
|
||||||
|
|
||||||
if deployed and len(new_diff) > 0:
|
|
||||||
print("\n---------------------")
|
|
||||||
print(" Deployment succeeded!")
|
|
||||||
print(f" All tasks: {', '.join(new_diff)}")
|
|
||||||
print("---------------------\n")
|
|
||||||
sys.exit(0)
|
|
||||||
elif not deployed and len(new_diff) > 0:
|
|
||||||
print("\n---------------------")
|
|
||||||
print(" Deployment failed!")
|
|
||||||
print(f" All tasks: {', '.join(new_diff)}")
|
|
||||||
print("---------------------\n")
|
|
||||||
sys.exit(1)
|
|
||||||
elif len(new_diff) <= 0:
|
|
||||||
print("[MAIN] Successfully executed, no tasks required execution")
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user