diff --git a/.taskfiles/_scripts/build_import_and_push.sh b/.taskfiles/_scripts/build_import.sh similarity index 73% rename from .taskfiles/_scripts/build_import_and_push.sh rename to .taskfiles/_scripts/build_import.sh index 4443f34..53e0211 100755 --- a/.taskfiles/_scripts/build_import_and_push.sh +++ b/.taskfiles/_scripts/build_import.sh @@ -21,13 +21,7 @@ MBP="${metadataBuildPath}/tarball/nixos-system-x86_64-linux.tar.xz" QBP="${qemuImageBuildPath}/nixos.qcow2" # Import the image to Incus and capture the instance name -instance_name=$(incus image import --alias nixos-gen/custom/fj-lxc-vm-x86_64 "$MBP" "$QBP" | grep -oP '(?<=Instance ).*(?= created)') +incus image import --alias nixos-gen/custom/fj-lxc-vm-x86_64 "$MBP" "$QBP" check_command "Importing image to Incus" -echo "Instance created: $instance_name" - -# Call the push_token.sh script with the new instance name -./push_token.sh "$instance_name" -check_command "Pushing token to instance" - echo "Process completed successfully" diff --git a/.taskfiles/_scripts/launch.sh b/.taskfiles/_scripts/launch.sh new file mode 100644 index 0000000..5bf11e1 --- /dev/null +++ b/.taskfiles/_scripts/launch.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +instance_name=$(incus launch nixos-gen/custom/fj-lxc-vm-x86_64 | grep -oP 'Instance name is: \K\S+') +echo "The captured instance name is: $instance_name" + +# Call the push_token.sh script with the new instance name +./push_token.sh "$instance_name" +echo "Pushing token to instance" + +echo "Process completed successfully" diff --git a/.taskfiles/_scripts/push_token.sh b/.taskfiles/_scripts/push_token.sh index a116a77..8f12626 100755 --- a/.taskfiles/_scripts/push_token.sh +++ b/.taskfiles/_scripts/push_token.sh @@ -18,7 +18,7 @@ INCUS_INSTANCE="$1" # Use the provided instance name OP_ITEM_NAME="forgejo-runner" # Name of the 1Password item containing the runner token OP_VAULT_NAME="forgejo-runner" # Name of the 1Password vault TOKEN_FILE="tokenfile" # Name of the temporary file to store the token -INCUS_PATH="$INCUS_INSTANCE/var/lib/gitea-runner/default/$TOKEN_FILE" +INCUS_PATH="/var/lib/gitea-runner/default/$TOKEN_FILE" # Check if OP_SESSION environment variable exists, if not, sign in if [ -z "${OP_SESSION:-}" ]; then @@ -29,10 +29,6 @@ if [ -z "${OP_SESSION:-}" ]; then fi fi -# Ensure the target directory exists in the Incus instance -#incus exec "$INCUS_INSTANCE" -- mkdir -p /var/lib/gitea-runner/default -#incus exec "$INCUS_INSTANCE" -- chown gitea-runner:gitea-runner /var/lib/gitea-runner/default - # Retrieve the token from 1Password TOKEN=$(op item get "$OP_ITEM_NAME" --vault "$OP_VAULT_NAME" --fields runner_token) @@ -44,12 +40,31 @@ fi # Create the token file echo "TOKEN=$TOKEN" > "$TOKEN_FILE" -# Push the file to Incus -echo "Running: incus file push $INCUS_PATH" -if incus file push "$TOKEN_FILE" "$INCUS_PATH"; then - echo "Token file successfully pushed to Incus instance $INCUS_INSTANCE" +# Function to push file and check existence with retries +push_and_check_file() { + local retries=5 + local count=0 + while [ $count -lt $retries ]; do + echo "Attempt $((count+1)) of $retries: Pushing file to Incus instance..." + if incus file push "$TOKEN_FILE" "$INCUS_INSTANCE$INCUS_PATH"; then + if incus exec "$INCUS_INSTANCE" -- test -f "$INCUS_PATH"; then + echo "File successfully verified in Incus instance." + return 0 + fi + fi + ((count++)) + echo "File not found or push failed. Retrying in 5 seconds..." + sleep 5 + done + echo "Failed to push and verify file after $retries attempts." >&2 + return 1 +} + +# Push the file to Incus and verify its existence +if push_and_check_file; then + echo "Token file successfully pushed and verified in Incus instance $INCUS_INSTANCE" else - echo "Failed to push token file to Incus instance $INCUS_INSTANCE" >&2 + echo "Failed to push or verify token file in Incus instance $INCUS_INSTANCE" >&2 rm "$TOKEN_FILE" exit 1 fi