diff --git a/gcloud/tf/doit.tf b/gcloud/tf/doit.tf deleted file mode 100644 index e69de29..0000000 diff --git a/gcloud/tf/k3s/forgejo/deployment.yaml b/gcloud/tf/k3s/forgejo/deployment.yaml index cd3afdc..445ec2d 100644 --- a/gcloud/tf/k3s/forgejo/deployment.yaml +++ b/gcloud/tf/k3s/forgejo/deployment.yaml @@ -26,7 +26,7 @@ spec: - name: FORGEJO__repository__ENABLE_PUSH_CREATE_USER value: "true" - name: FORGEJO__server__ROOT_URL - value: "https://frg.headshed.dev/" + value: "https://frgdr.headshed.dev/" - name: FORGEJO__repository__DEFAULT_BRANCH value: "main" - name: FORGEJO__server__LFS_START_SERVER diff --git a/gcloud/tf/scripts/build-gcloud-k3s-pipeline-wait-dns.json b/gcloud/tf/scripts/build-gcloud-k3s-pipeline-wait-dns.json new file mode 100644 index 0000000..eeb5a5f --- /dev/null +++ b/gcloud/tf/scripts/build-gcloud-k3s-pipeline-wait-dns.json @@ -0,0 +1,56 @@ +[ + { + "name": "run pre-flight checks", + "function": "RunCommand", + "params": [ + "./gcloud/tf/scripts/pre-flight-checks.sh" + ], + "retryCount": 0, + "shouldAbort": true + }, + { + "name": "list gcloud infrastructure", + "function": "RunCommand", + "params": [ + "./gcloud/tf/scripts/list_gloud_infra.sh" + ], + "retryCount": 0, + "shouldAbort": true + }, + { + "name": "create tfvars", + "function": "RunCommand", + "params": [ + "./gcloud/tf/scripts/create_tfvars.sh" + ], + "retryCount": 0, + "shouldAbort": true + }, + { + "name": "run tofu", + "function": "RunCommand", + "params": [ + "./gcloud/tf/scripts/run_tofu.sh" + ], + "retryCount": 0, + "shouldAbort": true + }, + { + "name": "wait for user input to continue", + "function": "RunCommand", + "params": [ + "./gcloud/tf/scripts/wait_for_user_input_dns.sh" + ], + "retryCount": 0, + "shouldAbort": true + }, + { + "name": "copy .env to k3s-vm-1", + "function": "RunCommand", + "params": [ + "gcloud/tf/scripts/copy_env_to_first_node.sh" + ], + "retryCount": 0, + "shouldAbort": true + } +] \ No newline at end of file diff --git a/gcloud/tf/scripts/build-gcloud-k3s-pipeline.json b/gcloud/tf/scripts/build-gcloud-k3s-pipeline.json index 179282f..d1fd61b 100644 --- a/gcloud/tf/scripts/build-gcloud-k3s-pipeline.json +++ b/gcloud/tf/scripts/build-gcloud-k3s-pipeline.json @@ -34,5 +34,14 @@ ], "retryCount": 0, "shouldAbort": true + }, + { + "name": "copy .env to k3s-vm-1", + "function": "RunCommand", + "params": [ + "gcloud/tf/scripts/copy_env_to_first_node.sh" + ], + "retryCount": 0, + "shouldAbort": true } ] \ No newline at end of file diff --git a/gcloud/tf/scripts/copy_env_to_first_node.sh b/gcloud/tf/scripts/copy_env_to_first_node.sh new file mode 100755 index 0000000..5bce28a --- /dev/null +++ b/gcloud/tf/scripts/copy_env_to_first_node.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +source .env + +for i in {1..10}; do + # Check if the instance is running + INSTANCE_STATUS=$(gcloud compute instances describe k3s-vm-1 --zone=us-central1-a --project="$PROJECT_NAME" --format='get(status)') + if [[ "$INSTANCE_STATUS" != "RUNNING" ]]; then + echo "Instance k3s-vm-1 is not running. Attempt $i/10. Waiting 5 seconds..." + sleep 5 + continue + fi + + # Check if the directory exists on the remote host + if gcloud compute ssh k3s-vm-1 --zone=us-central1-a --project="$PROJECT_NAME" --command="test -d /opt/src/infctl-cli/"; then + echo "/opt/src/infctl-cli/ exists on k3s-vm-1." + break + else + echo "/opt/src/infctl-cli/ does not exist yet. Attempt $i/10. Waiting 5 seconds..." + sleep 5 + fi +done + +# Final check after loop +if ! gcloud compute ssh k3s-vm-1 --zone=us-central1-a --project="$PROJECT_NAME" --command="test -d /opt/src/infctl-cli/"; then + echo "ERROR: /opt/src/infctl-cli/ does not exist on k3s-vm-1 after 10 attempts. Exiting." + exit 1 +fi + +gcloud compute scp .env k3s-vm-1:/opt/src/infctl-cli/.env --zone=us-central1-a --project=$PROJECT_NAME + diff --git a/gcloud/tf/scripts/k3s-vm-startup.sh b/gcloud/tf/scripts/k3s-vm-startup.sh index dd875fd..6e2fcf9 100644 --- a/gcloud/tf/scripts/k3s-vm-startup.sh +++ b/gcloud/tf/scripts/k3s-vm-startup.sh @@ -1,5 +1,8 @@ #!/bin/bash +# Redirect all output to a log file for reliability +exec > /tmp/startup.log 2>&1 + INFCTL_GIT_REPO="https://codeberg.org/headshed/infctl-cli.git" INFCTL_GIT_REPO_BRANCH="main" INFCTL_INSTALL_DIR="/opt/src" @@ -100,3 +103,31 @@ if [[ ! -d "$INFCTL_INSTALL_DIR" ]]; then chown -R user:user "$INFCTL_INSTALL_DIR" fi +for i in {1..100}; do + if [[ -f /opt/src/infctl-cli/.env ]]; then + echo ".env file found." + break + else + echo ".env file not found. Attempt $i/100. Waiting 5 seconds..." + sleep 5 + fi +done + +# Final check after loop +if [[ ! -f /opt/src/infctl-cli/.env ]]; then + echo "ERROR: .env file not found after 10 attempts. Exiting." + exit 1 +fi + +# load .env file +source /opt/src/infctl-cli/.env + +# check to see if INSTALL_FORGEJO is set to "true" +if [[ "$INSTALL_FORGEJO" == "true" ]]; then + # install forgejo using infctl + # .... + export KUBECONFIG=/etc/rancher/k3s/k3s.yaml + LOG_FORMAT=none infctl -f "${INFCTL_INSTALL_DIR}/infctl-cli/gcloud/tf/scripts/install-forgejo-pipeline.json" + touch /etc/forgejo_was_installed + +fi diff --git a/gcloud/tf/scripts/wait_for_user_input_dns.sh b/gcloud/tf/scripts/wait_for_user_input_dns.sh new file mode 100755 index 0000000..a356571 --- /dev/null +++ b/gcloud/tf/scripts/wait_for_user_input_dns.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +echo "Please configure DNS using the IP address from the previous stage." +echo "you have 120 seconds." +for i in {120..1}; do + echo -ne "Time remaining: $i seconds\r" + sleep 1 +done +echo "" + +exit 0 \ No newline at end of file