From 202d1ef7f3bb1d445c94fbabe69963bf79f3b45a Mon Sep 17 00:00:00 2001 From: Johannes Faltermeier Date: Thu, 30 Mar 2023 15:12:25 +0200 Subject: [PATCH] Git Init Operation #69 --- .vscode/extensions.json | 5 ++-- doc/docs/Building-Internal.md | 7 ++++++ dockerfiles/git-init/Dockerfile | 16 ++++++++++++ python/git-init/README.md | 44 +++++++++++++++++++++++++++++++++ python/git-init/git-askpw.py | 24 ++++++++++++++++++ python/git-init/git-init.py | 42 +++++++++++++++++++++++++++++++ 6 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 dockerfiles/git-init/Dockerfile create mode 100644 python/git-init/README.md create mode 100755 python/git-init/git-askpw.py create mode 100755 python/git-init/git-init.py diff --git a/.vscode/extensions.json b/.vscode/extensions.json index cad1a218..91ec209a 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,5 +1,6 @@ { "recommendations": [ - "hashicorp.terraform" + "hashicorp.terraform", + "ms-python.python" ] -} \ No newline at end of file +} diff --git a/doc/docs/Building-Internal.md b/doc/docs/Building-Internal.md index 2ea90906..6531a41c 100644 --- a/doc/docs/Building-Internal.md +++ b/doc/docs/Building-Internal.md @@ -56,3 +56,10 @@ Build and push the operator with: docker build -t theiacloud/theia-cloud-operator:latest -f dockerfiles/operator/Dockerfile . docker push theiacloud/theia-cloud-operator:latest ``` + +Build and pish the git-init container: + +```bash +docker build -t theiacloud/theia-cloud-git-init:latest -f dockerfiles/git-init/Dockerfile . +docker push theiacloud/theia-cloud-git-init:latest +``` diff --git a/dockerfiles/git-init/Dockerfile b/dockerfiles/git-init/Dockerfile new file mode 100644 index 00000000..246f426f --- /dev/null +++ b/dockerfiles/git-init/Dockerfile @@ -0,0 +1,16 @@ +FROM debian:11-slim + +RUN apt update && \ + apt install python git -y && \ + apt clean + +WORKDIR /tmp +COPY python/git-init/git-init.py . +COPY python/git-init/git-askpw.py . + +ENV GIT_PROMPT1 "foo" +ENV GIT_PROMPT2 "bar" +ENV GIT_ASKPASS=/tmp/git-askpw.py + +ENTRYPOINT [ "python", "git-init.py" ] +CMD ["-h"] \ No newline at end of file diff --git a/python/git-init/README.md b/python/git-init/README.md new file mode 100644 index 00000000..4eb6303c --- /dev/null +++ b/python/git-init/README.md @@ -0,0 +1,44 @@ +# Git Init Container + +## Scenarios + +- HTTP(S) + - No Auth + - Ask for password only + - Ask for username and password +- SSH + - No Auth + - Ask for password + +## Testing + +Build init container + +```bash +docker build -t theiacloud/theia-cloud-git-init:local -f dockerfiles/git-init/Dockerfile . +``` + +Test Checkout + +```bash +# Adjust URLs and Password/PATs below +# keep spaces in front to avoid command being added to bash history + export HTTP_PUBLIC=https://github.com/eclipsesource/theia-cloud.git + export HTTP_PRIVATE=https://gitlab.eclipse.org/jfaltermeier/org.eclipse.epp.mpc.git + export HTTP_PRIVATE_WITH_USERNAME=https://jfaltermeier@gitlab.eclipse.org/jfaltermeier/org.eclipse.epp.mpc.git + export HTTP_PRIVATE_WITH_USERNAME_AND_PASSWORD=https://jfaltermeier:pat@gitlab.eclipse.org/jfaltermeier/org.eclipse.epp.mpc.git + export HTTP_USERNAME=jfaltermeier + export HTTP_PASSWORD=pat + +# HTTPS Public +docker run --rm theiacloud/theia-cloud-git-init:local "$HTTP_PUBLIC" "/tmp/my-repo" + +# HTTPS Private +docker run --env GIT_PROMPT1=$HTTP_USERNAME --env GIT_PROMPT2=$HTTP_PASSWORD --rm theiacloud/theia-cloud-git-init:local "$HTTP_PRIVATE" "/tmp/my-repo" + +# HTTPS Private with Username +docker run --env GIT_PROMPT1=$HTTP_PASSWORD --rm theiacloud/theia-cloud-git-init:local "$HTTP_PRIVATE_WITH_USERNAME" "/tmp/my-repo" + +# HTTPS Private with Username and Password +docker run --rm theiacloud/theia-cloud-git-init:local "$HTTP_PRIVATE_WITH_USERNAME_AND_PASSWORD" "/tmp/my-repo" +``` diff --git a/python/git-init/git-askpw.py b/python/git-init/git-askpw.py new file mode 100755 index 00000000..09e83adf --- /dev/null +++ b/python/git-init/git-askpw.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +import os +import logging + +debugLogging = False +path = "/tmp/theia-cloud-askpw" + +logging.basicConfig() + +logger = logging.getLogger('ask-pw') +if debugLogging: + logger.setLevel(logging.DEBUG) +else: + logger.setLevel(logging.INFO) + +if os.path.isfile(path): + logger.debug("Prompt 2") + prompt2 = os.environ['GIT_PROMPT2'] + print(prompt2) +else: + logger.debug("Prompt 1") + prompt1 = os.environ['GIT_PROMPT1'] + print(prompt1) + os.mknod(path) diff --git a/python/git-init/git-init.py b/python/git-init/git-init.py new file mode 100755 index 00000000..777069df --- /dev/null +++ b/python/git-init/git-init.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +import argparse +import subprocess +import logging + +debugLogging = True + +logging.basicConfig() + +logger = logging.getLogger('git-init') +if debugLogging: + logger.setLevel(logging.DEBUG) +else: + logger.setLevel(logging.INFO) + +def runProcess(args): + process = subprocess.Popen( + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + out = stdout.decode('ascii') + if len(out) > 0: + logger.info(out) + if process.returncode != 0: + logger.error(stderr.decode('ascii')) + return process.returncode + +parser = argparse.ArgumentParser() +parser.add_argument("repository", help="The repository URL", type=str) +parser.add_argument("directory", help="The directory to clone into", type=str) +args = parser.parse_args() + +code = runProcess(["git", "config", "--global", "credential.helper", "store"]) +if code != 0: + exit(code) + +code = runProcess(["git", "clone", args.repository, args.directory]) +if code != 0: + exit(code) + +if debugLogging: + runProcess(["ls", "-al", args.directory])