one command builder deploy

This commit is contained in:
Joseph Hanson 2024-09-13 19:49:40 -05:00
parent 6421c46571
commit 2162517b12
Signed by: jahanson
SSH key fingerprint: SHA256:vy6dKBECV522aPAwklFM3ReKAVB086rT3oWwiuiFG7o
3 changed files with 36 additions and 17 deletions

View file

@ -21,13 +21,7 @@ MBP="${metadataBuildPath}/tarball/nixos-system-x86_64-linux.tar.xz"
QBP="${qemuImageBuildPath}/nixos.qcow2" QBP="${qemuImageBuildPath}/nixos.qcow2"
# Import the image to Incus and capture the instance name # 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" 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" echo "Process completed successfully"

View file

@ -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"

View file

@ -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_ITEM_NAME="forgejo-runner" # Name of the 1Password item containing the runner token
OP_VAULT_NAME="forgejo-runner" # Name of the 1Password vault OP_VAULT_NAME="forgejo-runner" # Name of the 1Password vault
TOKEN_FILE="tokenfile" # Name of the temporary file to store the token 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 # Check if OP_SESSION environment variable exists, if not, sign in
if [ -z "${OP_SESSION:-}" ]; then if [ -z "${OP_SESSION:-}" ]; then
@ -29,10 +29,6 @@ if [ -z "${OP_SESSION:-}" ]; then
fi fi
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 # Retrieve the token from 1Password
TOKEN=$(op item get "$OP_ITEM_NAME" --vault "$OP_VAULT_NAME" --fields runner_token) TOKEN=$(op item get "$OP_ITEM_NAME" --vault "$OP_VAULT_NAME" --fields runner_token)
@ -44,12 +40,31 @@ fi
# Create the token file # Create the token file
echo "TOKEN=$TOKEN" > "$TOKEN_FILE" echo "TOKEN=$TOKEN" > "$TOKEN_FILE"
# Push the file to Incus # Function to push file and check existence with retries
echo "Running: incus file push $INCUS_PATH" push_and_check_file() {
if incus file push "$TOKEN_FILE" "$INCUS_PATH"; then local retries=5
echo "Token file successfully pushed to Incus instance $INCUS_INSTANCE" 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 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" rm "$TOKEN_FILE"
exit 1 exit 1
fi fi