From 873a27c4909e468a67eac04035183972b40f5246 Mon Sep 17 00:00:00 2001 From: Khue Doan Date: Fri, 14 Oct 2022 19:02:10 +0700 Subject: [PATCH] refactor(bootstrap)!: create a temporary git server for the seed repo Instead of relying on GitHub --- Makefile | 1 + bootstrap/root/apply.sh | 36 ++++++++++++++++++++++--- bootstrap/root/seed-repo/.gitignore | 2 ++ bootstrap/root/values-seed.yaml | 2 -- platform/gitea/files/config/config.yaml | 2 +- scripts/configure | 11 -------- 6 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 bootstrap/root/seed-repo/.gitignore delete mode 100644 bootstrap/root/values-seed.yaml diff --git a/Makefile b/Makefile index ebf6b730..9df1d8a0 100644 --- a/Makefile +++ b/Makefile @@ -47,6 +47,7 @@ test: clean: docker compose --project-directory ./metal/roles/pxe_server/files down + docker rm --force homelab-seed-repo dev: make -C metal cluster env=dev diff --git a/bootstrap/root/apply.sh b/bootstrap/root/apply.sh index 2dd4465f..b5e5dbc0 100755 --- a/bootstrap/root/apply.sh +++ b/bootstrap/root/apply.sh @@ -1,13 +1,41 @@ #!/bin/sh -VALUES="values.yaml" +set -eu -kubectl get ingress gitea --namespace gitea \ - || VALUES="values-seed.yaml" +ADDITIONAL_VALUES="" + +# If the Gitea service does not exist, it means that this is the first installation, clone from a seed repo instead +# ArgoCD will automatically switch to Gitea once the repository is clonable +if ! kubectl get service gitea-http --namespace gitea; then + # Use Ansible to get a consistent output with ../metal + CONTROLLER_IP="$(\ + ANSIBLE_LOAD_CALLBACK_PLUGINS=true \ + ANSIBLE_STDOUT_CALLBACK=json \ + ansible localhost -m ansible.builtin.setup -a 'filter=ansible_default_ipv4' \ + | jq --raw-output '.plays[0].tasks[0].hosts.localhost.ansible_facts.ansible_default_ipv4.address' \ + )" + + # Create a temporary git server + SEED_REPO="${PWD}/seed-repo/homelab.git" + [ -d "${SEED_REPO}" ] || git clone --bare ../.. "${SEED_REPO}" + git -C "${SEED_REPO}" update-server-info + git -C "${SEED_REPO}" fetch --all + docker start homelab-seed-repo || docker run \ + --volume "${PWD}/seed-repo:/usr/share/nginx/html:ro" \ + --publish 8000:80 \ + --rm \ + --detach \ + --name homelab-seed-repo \ + nginx:latest + + ADDITIONAL_VALUES="gitops.repo=http://${CONTROLLER_IP}:8000/homelab.git" +fi + +echo "${ADDITIONAL_VALUES}" helm template \ --include-crds \ --namespace argocd \ - --values "${VALUES}" \ + --set "${ADDITIONAL_VALUES}" \ argocd . \ | kubectl apply -n argocd -f - diff --git a/bootstrap/root/seed-repo/.gitignore b/bootstrap/root/seed-repo/.gitignore new file mode 100644 index 00000000..d6b7ef32 --- /dev/null +++ b/bootstrap/root/seed-repo/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/bootstrap/root/values-seed.yaml b/bootstrap/root/values-seed.yaml deleted file mode 100644 index ae729830..00000000 --- a/bootstrap/root/values-seed.yaml +++ /dev/null @@ -1,2 +0,0 @@ -gitops: - repo: https://github.com/khuedoan/homelab diff --git a/platform/gitea/files/config/config.yaml b/platform/gitea/files/config/config.yaml index b093dbff..295e0db1 100644 --- a/platform/gitea/files/config/config.yaml +++ b/platform/gitea/files/config/config.yaml @@ -16,7 +16,7 @@ repositories: owner: ops private: false migrate: - source: https://github.com/khuedoan/homelab + source: http://192.168.1.24:8000/homelab.git # TODO mirror: false # TODO create webhook (use a global one?) # webhooks: diff --git a/scripts/configure b/scripts/configure index 982aa7b9..db2d74e7 100755 --- a/scripts/configure +++ b/scripts/configure @@ -13,7 +13,6 @@ import sys from rich.prompt import Confirm, Prompt -default_seed_repo = "https://github.com/khuedoan/homelab" default_domain = "khuedoan.com" default_timezone = "Asia/Ho_Chi_Minh" default_terraform_workspace = "khuedoan" @@ -44,7 +43,6 @@ def main() -> None: editor = Prompt.ask("Select text editor", default='nvim') domain = Prompt.ask("Enter your domain", default=default_domain) - seed_repo = Prompt.ask("Enter seed repo", default=default_seed_repo) timezone = Prompt.ask("Enter time zone", default=default_timezone) find_and_replace( @@ -60,15 +58,6 @@ def main() -> None: ] ) - find_and_replace( - pattern=default_seed_repo, - replacement=seed_repo, - paths=[ - "bootstrap", - "platform" - ] - ) - find_and_replace( pattern=default_timezone, replacement=timezone,