From dfa4b6000d122df76a44c1981accd0e11dde7a22 Mon Sep 17 00:00:00 2001 From: ThomasCardin Date: Wed, 30 Oct 2024 12:57:25 -0400 Subject: [PATCH] issue #61: create pull request to howard repo --- webtop-template/main.py | 47 ++++++++++++++++++- webtop-template/requirements.txt | 4 +- .../templates/kustomization.yaml.j2 | 8 ---- .../templates/webtop-deployment.yaml.j2 | 24 +++++++++- 4 files changed, 71 insertions(+), 12 deletions(-) delete mode 100644 webtop-template/templates/kustomization.yaml.j2 diff --git a/webtop-template/main.py b/webtop-template/main.py index 0be1beb..0b92af6 100644 --- a/webtop-template/main.py +++ b/webtop-template/main.py @@ -1,9 +1,14 @@ import sys import os +from dotenv import load_dotenv + from jinja2 import Environment, FileSystemLoader +from github import Github +REPO_NAME = 'ai-cfia/howard' FOLDER_PATH = 'templates' -TEMPLATES_NAMES = ['webtop-deployment.yaml.j2', 'webtop-ingress.yaml.j2', 'webtop-secrets.yaml.j2', 'kustomization.yaml.j2'] +HOWARD_DUMB_FILE_FOLDER = 'kubernetes/aks/apps/webtop' +TEMPLATES_NAMES = ['webtop-deployment.yaml.j2', 'webtop-ingress.yaml.j2', 'webtop-secrets.yaml.j2'] def render_template(username): file_loader = FileSystemLoader(FOLDER_PATH) @@ -23,6 +28,44 @@ def render_template(username): with open(output_filename, 'w') as f: f.write(rendered_content) +def create_github_pr(username, gh_access_token): + g = Github(gh_access_token) + repo = g.get_repo(REPO_NAME) + + branch_name = f"{username}-webtop-instance" + + source = repo.get_branch('main') + repo.create_git_ref(ref=f'refs/heads/{branch_name}', sha=source.commit.sha) + + for root, _, files in os.walk(username): + for file in files: + file_path = os.path.join(root, file) + with open(file_path, 'r') as file_content: + content = file_content.read() + repo_file_path = os.path.relpath(file_path, username) + repo_file_path = os.path.join(HOWARD_DUMB_FILE_FOLDER, file) + repo.create_file( + path=repo_file_path, + message=f'Adding {repo_file_path}', + content=content, + branch=branch_name + ) + + pr = repo.create_pull( + title=f'Adding new webtop instance for {username}', + body=f'Adding new webtop instance for {username}', + head=branch_name, + base='main' + ) + + print(f"Pull request created: {pr.html_url}") + if __name__ == '__main__': username = sys.argv[1] - render_template(username) \ No newline at end of file + + load_dotenv() + + gh_access_token = os.getenv("GITHUB_ACCESS_TOKEN") + + render_template(username) + create_github_pr(username, gh_access_token) \ No newline at end of file diff --git a/webtop-template/requirements.txt b/webtop-template/requirements.txt index 1c579e7..b627d4f 100644 --- a/webtop-template/requirements.txt +++ b/webtop-template/requirements.txt @@ -1 +1,3 @@ -jinja2 \ No newline at end of file +jinja2 +PyGithub +python-dotenv \ No newline at end of file diff --git a/webtop-template/templates/kustomization.yaml.j2 b/webtop-template/templates/kustomization.yaml.j2 deleted file mode 100644 index e64510c..0000000 --- a/webtop-template/templates/kustomization.yaml.j2 +++ /dev/null @@ -1,8 +0,0 @@ ---- -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization - -resources: - - {{ username }}-webtop-deployment.yaml - - {{ username }}-webtop-ingress.yaml - - {{ username }}-webtop-secrets.yaml diff --git a/webtop-template/templates/webtop-deployment.yaml.j2 b/webtop-template/templates/webtop-deployment.yaml.j2 index ab18e32..8428c4a 100644 --- a/webtop-template/templates/webtop-deployment.yaml.j2 +++ b/webtop-template/templates/webtop-deployment.yaml.j2 @@ -14,19 +14,41 @@ spec: spec: containers: - name: webtop - image: linuxserver/webtop:ubuntu-kde + image: ghcr.io/ai-cfia/webtop-ubuntu-kde:main ports: - containerPort: 3000 + env: + - name: DOCKER_HOST + value: "tcp://localhost:2375" + - name: DOCKER_TLS_CERTDIR + value: "" envFrom: - secretRef: name: {{ username }}-webtop-secrets volumeMounts: - name: config mountPath: /config + - name: docker-dind + image: docker:dind + securityContext: + privileged: true + ports: + - containerPort: 2375 + env: + - name: DOCKER_TLS_CERTDIR + value: "" + args: + - "--host=tcp://0.0.0.0:2375" + - "--tls=false" + volumeMounts: + - name: docker-graph-storage + mountPath: /var/lib/docker volumes: - name: config persistentVolumeClaim: claimName: {{ username }}-webtop-config-pvc + - name: docker-graph-storage + emptyDir: {} --- apiVersion: v1