diff --git a/augur/application/cli/backend.py b/augur/application/cli/backend.py index 46761084a6..84c91eaf03 100644 --- a/augur/application/cli/backend.py +++ b/augur/application/cli/backend.py @@ -13,6 +13,7 @@ import uuid import traceback import requests +import tempfile from redis.exceptions import ConnectionError as RedisConnectionError from augur.tasks.start_tasks import augur_collection_monitor, create_collection_status_records @@ -23,6 +24,8 @@ from augur.tasks.data_analysis.contributor_breadth_worker.contributor_breadth_worker import contributor_breadth_model from augur.application.db.models import UserRepo from augur.application.db.session import DatabaseSession +from augur.application.config import AugurConfig +from augur.application.db import get_engine from augur.application.logs import AugurLogger from augur.application.service_manager import AugurServiceManager, cleanup_collection_status_and_rabbit, clean_collection_status from augur.application.db.lib import get_value @@ -35,6 +38,48 @@ logger = AugurLogger("augur", reset_logfiles=reset_logs).get_logger() +def create_git_credential_file(github_key, gitlab_key): + if 'AUGUR_GITHUB_USERNAME' in os.environ: + print("Found AUGUR_GITHUB_USERNAME env variable") + github_username = os.environ.get("AUGUR_GITHUB_USERNAME") + else: + github_username = input("Please input your GitHub username") + + if 'AUGUR_GITLAB_USERNAME' in os.environ: + print("Found AUGUR_GITLAB_USERNAME env variable") + gitlab_username = os.environ.get("AUGUR_GITLAB_USERNAME") + else: + gitlab_username = input("Please input your GitLab username") + + + engine = get_engine() + with DatabaseSession(logger, engine) as session: + config = AugurConfig(logger, session) + + worker_options = config.get_section("Facade") + repo_base_directory = worker_options['repo_directory'] + + credential_file_text = f""" + https://{github_username}:{github_key}@github.com + https://{gitlab_username}:{gitlab_key}@gitlab.com + """ + + link_path = f"{repo_base_directory}/.git-credentials" + fd, temp_path = tempfile.mkstemp(prefix="myapp_", suffix=".tmp") + with os.fdopen(fd, "w") as f: + f.write(credential_file_text) + + # Create a temporary symlink + tmp_link = link_path + ".new" + os.symlink(temp_path, tmp_link) + # Atomically swap it into place + os.replace(tmp_link, link_path) + + facade_credential_store = f"git config --global credential.helper \"store --file {repo_base_directory}/.git-credentials\"" + subprocess.Popen(facade_credential_store.split(" ")) + + + @click.group('server', short_help='Commands for controlling the backend API server & data collection workers') @click.pass_context def cli(ctx): @@ -168,7 +213,10 @@ def start(ctx, disable_collection, development, pidfile, port): clean_collection_status(session) assign_orphan_repos_to_default_user(session) - + + #Create git credentials file and store git credentials + create_git_credential_file(ghkeyman.keys[0], glkeyman.keys[0]) + create_collection_status_records.si().apply_async() time.sleep(3) diff --git a/scripts/install/config.sh b/scripts/install/config.sh index 0408b0aa09..c634366d4f 100755 --- a/scripts/install/config.sh +++ b/scripts/install/config.sh @@ -226,13 +226,13 @@ function create_config() { fi #Create and cache credentials for github and gitlab - touch $facade_repo_directory/.git-credentials + #touch $facade_repo_directory/.git-credentials - echo "https://$github_username:$github_api_key@github.com" > $facade_repo_directory/.git-credentials - echo "https://$gitlab_username:$gitlab_api_key@gitlab.com" >> $facade_repo_directory/.git-credentials + #echo "https://$github_username:$github_api_key@github.com" > $facade_repo_directory/.git-credentials + #echo "https://$gitlab_username:$gitlab_api_key@gitlab.com" >> $facade_repo_directory/.git-credentials - git config --global credential.helper "store --file $facade_repo_directory/.git-credentials" - "${cmd[@]}" + #git config --global credential.helper "store --file $facade_repo_directory/.git-credentials" + #"${cmd[@]}" } echo echo "Collecting data for config..."