diff --git a/docker-compose.yml b/docker-compose.yml index 1427f54..b9ce706 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,11 +8,13 @@ services: volumes: - ./src:/app - ./tmp/deploy-keys:/deploy-keys:ro + - .:/local-repos/asset-manager:ro environment: - | AGENT_CONFIG={ "repos": { - "git@github.com:WATonomous/infra-config.git": {"deploy_key_path": "/deploy-keys/infra-config"} + "git@github.com:WATonomous/infra-config.git": {"deploy_key_path": "/deploy-keys/infra-config"}, + "asset-manager": {"path": "/local-repos/asset-manager"} }, "buckets": { "temp": { diff --git a/src/utils.py b/src/utils.py index 0cee020..fe02c1a 100644 --- a/src/utils.py +++ b/src/utils.py @@ -49,37 +49,41 @@ def callback(log_level: LogLevel = LogLevel.INFO): @app.command() def clone_repos(): for repo_url, repo_config in AGENT_CONFIG["repos"].items(): - # Temporary file is required to handle ssh key permissions. - # NamedTemporaryFile is always created with mode 0600: - # https://stackoverflow.com/a/10541972 - with NamedTemporaryFile() as deploy_key_file: - logging.debug( - f"Copying deploy key from {repo_config['deploy_key_path']} to {deploy_key_file.name}" - ) - deploy_key_file.write(Path(repo_config["deploy_key_path"]).read_bytes()) - deploy_key_file.flush() - - repo_path = WORKSPACE / repo_url - - if repo_path.exists(): + if "path" in repo_config: + repo = Repo(repo_config["path"]) + logging.info(f"Using existing repo at {repo.working_dir}") + elif "deploy_key_path" in repo_config: + # Temporary file is required to handle ssh key permissions. + # NamedTemporaryFile is always created with mode 0600: + # https://stackoverflow.com/a/10541972 + with NamedTemporaryFile() as deploy_key_file: logging.debug( - f"Path {repo_path} already exists. Pulling latest changes." + f"Copying deploy key from {repo_config['deploy_key_path']} to {deploy_key_file.name}" ) - repo = Repo(repo_path) - repo.remote().pull( - env={"GIT_SSH_COMMAND": f"ssh -i {deploy_key_file.name}"} - ) - logging.info(f"Pulled latest changes to {repo.working_dir}") - else: - logging.debug(f"Path {repo_path} does not exist. Cloning repo.") - repo = Repo.clone_from( - repo_url, - repo_path, - env={"GIT_SSH_COMMAND": f"ssh -i {deploy_key_file.name}"}, - ) - logging.info(f"Cloned {repo_url} to {repo.working_dir}") - - yield repo + deploy_key_file.write(Path(repo_config["deploy_key_path"]).read_bytes()) + deploy_key_file.flush() + + repo_path = WORKSPACE / repo_url + + if repo_path.exists(): + logging.debug( + f"Path {repo_path} already exists. Pulling latest changes." + ) + repo = Repo(repo_path) + repo.remote().pull( + env={"GIT_SSH_COMMAND": f"ssh -i {deploy_key_file.name}"} + ) + logging.info(f"Pulled latest changes to {repo.working_dir}") + else: + logging.debug(f"Path {repo_path} does not exist. Cloning repo.") + repo = Repo.clone_from( + repo_url, + repo_path, + env={"GIT_SSH_COMMAND": f"ssh -i {deploy_key_file.name}"}, + ) + logging.info(f"Cloned {repo_url} to {repo.working_dir}") + + yield repo @app.command()