forked from admin/brencher
docker check by version (#50)
* docker check by version * docker check by version * docker check by version * docker check by version * docker check by version * docker check by version * docker check by version * docker check by version * docker check by version * docker check by version
This commit is contained in:
+2
-1
@@ -5,4 +5,5 @@ temp
|
||||
venv
|
||||
.venv
|
||||
LOCAL.md
|
||||
.idea
|
||||
.idea
|
||||
docker-compose.yml.tmp
|
||||
@@ -16,11 +16,15 @@ Generate docker compose file to deploy to docker swarm.
|
||||
|
||||
# Local start
|
||||
|
||||
python ./backend/app.py brencher_local1 noweb
|
||||
python ./backend/app.py brencher_local1:docker_swarm_check_by_version noweb
|
||||
|
||||
# Local start
|
||||
# Prod recovery
|
||||
|
||||
python ./backend/app.py brencher_local1 noweb
|
||||
uv venv --python 3.12.3 .venv
|
||||
|
||||
uv pip install -r requirements.txt
|
||||
|
||||
python ./backend/app.py brencher:docker_swarm_check_by_version noweb
|
||||
|
||||
# Update docker secret
|
||||
|
||||
|
||||
+24
-10
@@ -110,6 +110,8 @@ if slave_url:
|
||||
# Handlers for events from master -> re-emit locally (marked so we don't loop)
|
||||
@remote_sio.on('branches', namespace='/ws/branches')
|
||||
def _remote_branches(data: Any) -> None:
|
||||
global branches_slaves
|
||||
branches_slaves = data
|
||||
try:
|
||||
socketio.emit('branches', get_global_branches_to_emit(), namespace='/ws/branches')
|
||||
except Exception as e:
|
||||
@@ -118,7 +120,7 @@ if slave_url:
|
||||
|
||||
@remote_sio.on('environments', namespace='/ws/environment')
|
||||
def _remote_environments(data: Any) -> None:
|
||||
global environments, environments_slaves
|
||||
global environments_slaves
|
||||
environments_slaves = data
|
||||
try:
|
||||
socketio.emit('environments', get_global_envs_to_emit(), namespace='/ws/environment')
|
||||
@@ -200,7 +202,7 @@ def get_global_envs_to_emit() -> Any:
|
||||
return merge_result
|
||||
|
||||
def get_local_branches_to_emit() -> Dict[str, Dict[str, List[Any]]]:
|
||||
global branches_slaves, environments
|
||||
global environments
|
||||
branches: Dict[str, Dict[str, List[Any]]] = {}
|
||||
for k, env in environments.items():
|
||||
branches[k] = {}
|
||||
@@ -216,7 +218,7 @@ def get_local_branches_to_emit() -> Dict[str, Dict[str, List[Any]]]:
|
||||
return branches
|
||||
|
||||
def get_global_branches_to_emit() -> Dict[str, Dict[str, List[Any]]]:
|
||||
global branches, branches_slaves
|
||||
global branches_slaves
|
||||
local_branches: Dict[str, Dict[str, List[Any]]] = get_local_branches_to_emit()
|
||||
return merge_dicts(local_branches, branches_slaves)
|
||||
|
||||
@@ -318,12 +320,24 @@ class App:
|
||||
if cli_env_ids and len(cli_env_ids) > 0:
|
||||
environments = {k: e for k, e in environments.items() if k not in cli_env_ids}
|
||||
else:
|
||||
cli_env_ids = cli_env_ids_str.split(',')
|
||||
cli_env_ids = [x for x in cli_env_ids if len(x) > 0]
|
||||
logger.info(f"cli_env_ids {cli_env_ids}")
|
||||
if cli_env_ids and len(cli_env_ids) > 0:
|
||||
environments = {k: e for k, e in environments.items() if k in cli_env_ids}
|
||||
|
||||
cli_env_pairs = {x[0]: x[1] if len(x) > 1 else None for x in [x.split(":")
|
||||
for x in cli_env_ids_str.split(',')
|
||||
if len(x) > 0
|
||||
]
|
||||
}
|
||||
logger.info(f"cli_env_ids {cli_env_pairs}")
|
||||
if cli_env_pairs and len(cli_env_pairs) > 0:
|
||||
environments = {k: e for k, e in environments.items() if k in cli_env_pairs.keys()}
|
||||
for k, v in cli_env_pairs.items():
|
||||
if v is not None:
|
||||
if k in environments:
|
||||
env = environments[k]
|
||||
env.branches = [(v, 'HEAD')]
|
||||
logger.info(f"Overriding environment {k} branches to {env.branches}")
|
||||
else:
|
||||
logger.warning(f"Environment {k} not found to override branches")
|
||||
else:
|
||||
logger.info(f"No branch override for environment {k}")
|
||||
if dry_run:
|
||||
for id, e in environments.items():
|
||||
e.dry = True
|
||||
@@ -390,4 +404,4 @@ if __name__ == '__main__':
|
||||
cli_env_ids_str = cli_env_ids_list[0]
|
||||
|
||||
app1 = App(cli_env_ids_str, 'dry' in sys.argv[1:])
|
||||
app1.runWeb(port=(5000 if 'noweb' in sys.argv[1:] else 5001))
|
||||
app1.runWeb(port=(5007 if 'noweb' in sys.argv[1:] else 5001))
|
||||
|
||||
+45
-17
@@ -1,9 +1,11 @@
|
||||
import hashlib
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Dict, Callable, Any, Mapping, Protocol
|
||||
from typing import List, Dict, Callable, Any, Mapping, Protocol, Union, runtime_checkable
|
||||
|
||||
import docker
|
||||
import yaml
|
||||
@@ -11,7 +13,7 @@ from docker import errors as docker_errors
|
||||
from dotenv import dotenv_values
|
||||
|
||||
from enironment import AbstractStep
|
||||
from steps.git import GitClone
|
||||
from steps.git import GitClone, HasVersion
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -130,6 +132,7 @@ class DockerSwarmCheck(AbstractStep[Dict[str, DockerSwarmCheckResult]]):
|
||||
return current_services
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class HasImage(Protocol):
|
||||
image: str
|
||||
|
||||
@@ -138,7 +141,7 @@ class DockerSwarmDeploy(AbstractStep[str]):
|
||||
def __init__(self,
|
||||
wd: GitClone,
|
||||
buildDocker: DockerComposeBuild | None,
|
||||
stackChecker: AbstractStep[Mapping[str, HasImage]],
|
||||
stackChecker: AbstractStep[Mapping[str, Union[HasImage, HasVersion]]],
|
||||
docker_compose_path: str,
|
||||
envs: Callable[[], Dict[str, Any]],
|
||||
stack_name: str,
|
||||
@@ -158,7 +161,7 @@ class DockerSwarmDeploy(AbstractStep[str]):
|
||||
self.wd.progress()
|
||||
if self.buildDocker is not None:
|
||||
self.buildDocker.progress()
|
||||
current_services = self.stackChecker.progress()
|
||||
current_services: Mapping[str, HasImage | HasVersion] = self.stackChecker.progress()
|
||||
|
||||
def merge_dicts(a: Dict[str, Any], b: Dict[str, Any]) -> None:
|
||||
for k, v in b.items():
|
||||
@@ -192,21 +195,44 @@ class DockerSwarmDeploy(AbstractStep[str]):
|
||||
diffs = []
|
||||
ok = []
|
||||
for svc_name, svc in expected_services.items():
|
||||
expected_image = svc.get("image")
|
||||
running_service = current_services.get(svc_name)
|
||||
running_image = running_service.image if running_service is not None else None
|
||||
l = {
|
||||
"service": svc_name,
|
||||
"expected_image": expected_image,
|
||||
"actual_image": running_image,
|
||||
"stack": self.stack_name,
|
||||
}
|
||||
logger.info(l)
|
||||
if running_image and expected_image and running_image == expected_image:
|
||||
ok.append(l)
|
||||
if isinstance(running_service, HasVersion):
|
||||
expected_version = svc.get("labels", {}).get("org.brencher.version")
|
||||
running_version = running_service.version if running_service is not None else None
|
||||
l = {
|
||||
"service": svc_name,
|
||||
"expected_version": expected_version,
|
||||
"actual_version": running_version,
|
||||
"stack": self.stack_name,
|
||||
}
|
||||
logger.info(l)
|
||||
if not running_version or not expected_version:
|
||||
diffs.append(l)
|
||||
elif running_version != expected_version:
|
||||
diffs.append(l)
|
||||
else:
|
||||
ok.append(l)
|
||||
elif isinstance(running_service, HasImage):
|
||||
expected_image = svc.get("image")
|
||||
running_image = running_service.image if running_service is not None else None
|
||||
l = {
|
||||
"service": svc_name,
|
||||
"expected_image": expected_image,
|
||||
"actual_image": running_image,
|
||||
"stack": self.stack_name,
|
||||
}
|
||||
logger.info(l)
|
||||
if not running_image or not expected_image:
|
||||
diffs.append(l)
|
||||
elif running_image != expected_image:
|
||||
diffs.append(l)
|
||||
else:
|
||||
ok.append(l)
|
||||
else:
|
||||
diffs.append(l)
|
||||
|
||||
diffs.append({
|
||||
"service": svc_name,
|
||||
"stack": self.stack_name,
|
||||
})
|
||||
if len(diffs) == 0:
|
||||
logger.info(f"No diff found, stack is already up-to-date.")
|
||||
return ok
|
||||
@@ -217,6 +243,7 @@ class DockerSwarmDeploy(AbstractStep[str]):
|
||||
"diffs": diffs,
|
||||
}
|
||||
|
||||
# tmp_compose_path = os.path.join(tempfile.gettempdir(), f"{hashlib.sha1(content.encode()).hexdigest()[:5]}")
|
||||
tmp_compose_path = docker_compose_absolute_path + ".tmp"
|
||||
with open(tmp_compose_path, 'w') as f:
|
||||
f.write(content)
|
||||
@@ -238,6 +265,7 @@ class DockerSwarmDeploy(AbstractStep[str]):
|
||||
# merge_dicts(swarmEnv, env)
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
# , cwd=os.path.dirname(tmp_compose_path), env=swarmEnv)
|
||||
os.remove(tmp_compose_path)
|
||||
if result.returncode != 0:
|
||||
logger.error(f"Stack deploy failed: {result.stderr}")
|
||||
raise RuntimeError(f"Stack deploy failed: {result.stderr}")
|
||||
|
||||
@@ -3,7 +3,7 @@ import logging
|
||||
import os
|
||||
import tempfile
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Tuple, Set, Dict, Any, Mapping
|
||||
from typing import List, Tuple, Set, Dict, Any, Mapping, runtime_checkable
|
||||
import shutil
|
||||
|
||||
import git
|
||||
@@ -104,7 +104,7 @@ def _commits_childs(repo: git.Repo) -> Dict[Commit, List[Commit]]:
|
||||
|
||||
def ensure_clean(repo: git.Repo) -> None:
|
||||
if repo.is_dirty() or len(repo.untracked_files) > 0:
|
||||
raise BaseException(f"Changes in repo: I{repo.index.entries}, U{repo.untracked_files}")
|
||||
raise BaseException(f"Changes in repo: U{repo.untracked_files}")
|
||||
|
||||
|
||||
class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
@@ -239,7 +239,7 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
|
||||
from typing import Protocol, Any
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class HasVersion(Protocol):
|
||||
version: str
|
||||
|
||||
|
||||
Reference in New Issue
Block a user