git clone or fetch
This commit is contained in:
+2
-2
@@ -133,7 +133,7 @@ if __name__ == '__main__':
|
||||
def branch_refresh_thread():
|
||||
while True:
|
||||
fetch_branches()
|
||||
socketio.emit('branches', branches)
|
||||
socketio.emit('branches', branches, namespace='/ws/branches')
|
||||
time.sleep(300) # 5 minutes
|
||||
|
||||
# Start branch refresh thread
|
||||
@@ -144,7 +144,7 @@ if __name__ == '__main__':
|
||||
def processing_thread():
|
||||
while True:
|
||||
import processing
|
||||
processing.do_job(environments, lambda: socketio.emit('environments', get_envs_to_emit()))
|
||||
processing.do_job(environments, lambda: socketio.emit('environments', get_envs_to_emit(), namespace='/ws/environment'))
|
||||
time.sleep(10)
|
||||
processing = threading.Thread(target=processing_thread)
|
||||
processing.daemon = True
|
||||
|
||||
@@ -14,9 +14,8 @@ def do_job(
|
||||
if env.state != 'Active':
|
||||
continue
|
||||
for step in pipe:
|
||||
if isinstance(step, AbstractStep):
|
||||
step.do_job()
|
||||
onupdate()
|
||||
step.do_job()
|
||||
onupdate()
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Error processing release {env.id}: {str(e)}"
|
||||
|
||||
@@ -92,17 +92,20 @@ class DockerSwarmDeploy(AbstractStep):
|
||||
super().__init__(**kwargs)
|
||||
self.wd = wd
|
||||
self.envs = envs
|
||||
self.docker_compose_path = os.path.join(self.wd.result, self.docker_compose_path)
|
||||
self.docker_compose_path = docker_compose_path
|
||||
self.stack_name = stack_name
|
||||
|
||||
def progress(self) -> None:
|
||||
"""
|
||||
Deploys to Docker Swarm using the specified docker-compose.yaml.
|
||||
"""
|
||||
if isinstance(self.wd.result, BaseException):
|
||||
raise self.wd.result
|
||||
try:
|
||||
env = self.envs()
|
||||
# Prepare docker-compose file with env substitution
|
||||
with open(self.docker_compose_path, 'r') as f:
|
||||
docker_compose_absolute_path = os.path.join(self.wd.result, self.docker_compose_path)
|
||||
with open(docker_compose_absolute_path, 'r') as f:
|
||||
content = f.read()
|
||||
content = re.sub(r'\$\{([^}]+)\}', lambda m: env.get(m.group(1), ""), content)
|
||||
compose = yaml.safe_load(content)
|
||||
@@ -111,7 +114,7 @@ class DockerSwarmDeploy(AbstractStep):
|
||||
if "build" in svc:
|
||||
del svc["build"]
|
||||
content = yaml.safe_dump(compose)
|
||||
tmp_compose_path = self.docker_compose_path + ".tmp"
|
||||
tmp_compose_path = docker_compose_absolute_path + ".tmp"
|
||||
with open(tmp_compose_path, 'w') as f:
|
||||
f.write(content)
|
||||
|
||||
|
||||
+69
-74
@@ -5,6 +5,7 @@ from dataclasses import dataclass, asdict, field
|
||||
from typing import List, Optional, Union, Tuple
|
||||
from steps.step import AbstractStep
|
||||
import tempfile
|
||||
import os
|
||||
from enironment import Environment
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -12,22 +13,21 @@ logger = logging.getLogger(__name__)
|
||||
class GitClone(AbstractStep[str]):
|
||||
def __init__(self, env: Environment, **kwargs):
|
||||
super().__init__(env, **kwargs)
|
||||
self.temp_dir = os.path.join(tempfile.gettempdir(), f"{self.env.id}_{hashlib.sha1(self.env.repo.encode()).hexdigest()[:5]}")
|
||||
os.makedirs(self.temp_dir, exist_ok=True)
|
||||
|
||||
def progress(self) -> str | BaseException:
|
||||
try:
|
||||
# Create temporary directory for the clone
|
||||
def progress(self) -> str:
|
||||
|
||||
temp_dir = tempfile.mkdtemp(prefix=f"{self.env.id}_")
|
||||
logger.info(f"Cloning repository {self.env.repo} to {self.temp_dir}")
|
||||
if os.path.exists(os.path.join(self.temp_dir, ".git")):
|
||||
logger.info(f"Repository already cloned at {self.temp_dir}, fetching updates.")
|
||||
repo = git.Repo(self.temp_dir)
|
||||
repo.remotes.origin.fetch()
|
||||
else:
|
||||
git.Repo.clone_from(self.env.repo, self.temp_dir)
|
||||
|
||||
return self.temp_dir
|
||||
|
||||
# Clone the repository
|
||||
logger.info(f"Cloning repository {self.env.repo} to {temp_dir}")
|
||||
git.Repo.clone_from(self.env.repo, temp_dir)
|
||||
|
||||
return temp_dir
|
||||
|
||||
except Exception as e:
|
||||
return e
|
||||
|
||||
import hashlib
|
||||
|
||||
class CheckoutMerged(AbstractStep[Tuple[str, str]]):
|
||||
@@ -40,72 +40,67 @@ class CheckoutMerged(AbstractStep[Tuple[str, str]]):
|
||||
self.branches = branches
|
||||
|
||||
|
||||
def progress(self) -> Tuple[str, str] | BaseException:
|
||||
def progress(self) -> Tuple[str, str]:
|
||||
if self.wd.result_obj is BaseException:
|
||||
return self.wd.result_obj
|
||||
raise self.wd.result_obj
|
||||
|
||||
repo_path = self.wd.result_obj
|
||||
if not isinstance(repo_path, str):
|
||||
return BaseException(f"Unknown repo path {repo_path}")
|
||||
raise BaseException(f"Unknown repo path {repo_path}")
|
||||
|
||||
if len(self.branches) == 0:
|
||||
return BaseException(f"Empty branches set")
|
||||
raise BaseException(f"Empty branches set")
|
||||
|
||||
try:
|
||||
# Open the repository
|
||||
repo = git.Repo(repo_path)
|
||||
|
||||
# Generate hash from branch names
|
||||
sorted_branches = sorted(self.branches)
|
||||
branch_hash = hashlib.sha1(''.join(sorted_branches).encode()).hexdigest()
|
||||
auto_branch_name = f"auto/{branch_hash}"
|
||||
|
||||
logger.info(f"Looking for existing auto branch: {auto_branch_name}")
|
||||
|
||||
# Check if the auto branch already exists
|
||||
for remote_ref in repo.remotes.origin.refs:
|
||||
if remote_ref.name == f"origin/{auto_branch_name}":
|
||||
# Auto branch exists, fetch it
|
||||
logger.info(f"Found existing auto branch: {auto_branch_name}")
|
||||
repo.git.checkout(auto_branch_name)
|
||||
commit_hash = repo.head.commit.hexsha
|
||||
|
||||
return (auto_branch_name, commit_hash)
|
||||
# Open the repository
|
||||
repo = git.Repo(repo_path)
|
||||
|
||||
# Auto branch doesn't exist, create it
|
||||
logger.info(f"Creating new auto branch: {auto_branch_name}")
|
||||
|
||||
# Start with the first branch
|
||||
base_branch = self.branches[0]
|
||||
repo.git.checkout(base_branch)
|
||||
|
||||
# Create temporary branch for merging
|
||||
temp_branch = f"temp-merge-{branch_hash}"
|
||||
repo.git.checkout('-b', temp_branch)
|
||||
|
||||
# Merge the rest of the branches
|
||||
for branch in self.branches[1:]:
|
||||
try:
|
||||
logger.info(f"Merging branch: {branch}")
|
||||
repo.git.merge(branch, '--no-ff')
|
||||
except git.GitCommandError as e:
|
||||
# Handle merge conflicts according to predefined rules
|
||||
# For now, we'll abort the merge and report failure
|
||||
repo.git.merge('--abort')
|
||||
error_message = f"Merge conflict when merging {branch}: {str(e)}"
|
||||
logger.error(error_message)
|
||||
|
||||
return BaseException(error_message)
|
||||
|
||||
# Create and push the auto branch
|
||||
repo.git.branch('-f', auto_branch_name)
|
||||
repo.git.checkout(auto_branch_name)
|
||||
commit_hash = repo.head.commit.hexsha
|
||||
repo.git.push('-f', 'origin', auto_branch_name)
|
||||
|
||||
return (auto_branch_name,commit_hash)
|
||||
|
||||
except Exception as e:
|
||||
error_message = f"Failed to create merged branch: {str(e)}"
|
||||
logger.error(error_message)
|
||||
return BaseException(error_message)
|
||||
logger.info(f"Selected branches: {self.branches}")
|
||||
# Generate hash from branch names
|
||||
sorted_branches = sorted(self.branches)
|
||||
branch_hash = hashlib.sha1(''.join(sorted_branches).encode()).hexdigest()
|
||||
auto_branch_name = f"auto/{branch_hash}"
|
||||
|
||||
logger.info(f"Looking for existing auto branch: {auto_branch_name}")
|
||||
|
||||
# Check if the auto branch already exists
|
||||
for remote_ref in repo.remotes.origin.refs:
|
||||
if remote_ref.name == f"origin/{auto_branch_name}":
|
||||
# Auto branch exists, fetch it
|
||||
logger.info(f"Found existing auto branch: {auto_branch_name}")
|
||||
repo.git.checkout(auto_branch_name)
|
||||
commit_hash = repo.head.commit.hexsha
|
||||
|
||||
return (auto_branch_name, commit_hash)
|
||||
|
||||
# Auto branch doesn't exist, create it
|
||||
logger.info(f"Creating new auto branch: {auto_branch_name}")
|
||||
|
||||
# Start with the first branch
|
||||
base_branch = self.branches[0]
|
||||
repo.git.checkout(base_branch)
|
||||
|
||||
# Create temporary branch for merging
|
||||
temp_branch = f"temp-merge-{branch_hash}"
|
||||
repo.git.checkout('-b', temp_branch)
|
||||
|
||||
# Merge the rest of the branches
|
||||
for branch in self.branches[1:]:
|
||||
try:
|
||||
logger.info(f"Merging branch: {branch}")
|
||||
repo.git.merge(branch, '--no-ff')
|
||||
except git.GitCommandError as e:
|
||||
# Handle merge conflicts according to predefined rules
|
||||
# For now, we'll abort the merge and report failure
|
||||
repo.git.merge('--abort')
|
||||
error_message = f"Merge conflict when merging {branch}: {str(e)}"
|
||||
logger.error(error_message)
|
||||
|
||||
raise BaseException(error_message)
|
||||
|
||||
# Create and push the auto branch
|
||||
repo.git.branch('-f', auto_branch_name)
|
||||
repo.git.checkout(auto_branch_name)
|
||||
commit_hash = repo.head.commit.hexsha
|
||||
repo.git.push('-f', 'origin', auto_branch_name)
|
||||
|
||||
return (auto_branch_name,commit_hash)
|
||||
|
||||
@@ -38,5 +38,5 @@ class AbstractStep(ABC, Generic[T]):
|
||||
self._result = e
|
||||
|
||||
@abstractmethod
|
||||
def progress(self) -> T | BaseException:
|
||||
def progress(self) -> T:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user