Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
feat: added a check for ssh-agent's running state (#21)
Browse files Browse the repository at this point in the history
script added to check for ssh-agent listener
  • Loading branch information
IanEff authored Jan 20, 2024
1 parent 653a3ee commit ecbf5c0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
51 changes: 46 additions & 5 deletions kubernetes-cluster-kubeone/terraform/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,63 @@ else
fi

# Extract and check ssh_public_key_file value
ssh_key_file=$(grep -E "^ssh_public_key_file" terraform.tfvars | awk -F= '{gsub(/[ \047"]/, "", $2); print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')
ssh_public_key_file=$(grep -E "^ssh_public_key_file" terraform.tfvars | awk -F= '{gsub(/[ \047"]/, "", $2); print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')

ssh_key_file="${ssh_key_file/#\~/$HOME}"
ssh_public_key_file="${ssh_public_key_file/#\~/$HOME}"

# Extract worker volume size
worker_volume_size=$(grep -E "^worker_volume_size" terraform.tfvars.example | awk -F= '{gsub(/[ \047"]/, "", $2); print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')

if ! [ -f "$ssh_key_file" ]; then
echo "Error: SSH public key file '$ssh_key_file' not found."
if ! [ -f "$ssh_public_key_file" ]; then
echo "Error: SSH public key file '$ssh_public_key_file' not found."
echo "Please make sure the file exists and has appropriate permissions"
exit 1
else
echo -e "Using ssh: \033[32m$ssh_key_file\033[0m"
echo -e "Using ssh: \033[32m$ssh_public_key_file\033[0m"
echo -e "== Please make sure that this key is unique in your Hetzner Cloud account === \n"
fi

# Extract and check ssh_private_key_file value
ssh_private_key_file=$(grep -E "^ssh_private_key_file" terraform.tfvars | awk -F= '{gsub(/[ \047"]/, "", $2); print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')

ssh_private_key_file="${ssh_private_key_file/#\~/$HOME}"

# Check whether an ssh-agent is listening
if [ -z "$SSH_AUTH_SOCK" ]; then
echo "ssh-agent isn't running."

# Start ssh-agent
# NOTE/TODO: The agent sicks around, but only listens to the bash instance launched by the script.
# If further interactions with the agent are required (someone exiting the script and proceeding
# with the rest of the install on their own), the agent sticks around, but isn't listening to any-
# one unless they explicitly export SSH_AUTH_SOCK to the /tmp/ssh-..../agent.... value printed below.
eval "$(ssh-agent -s)"

echo "SSH_AGENT_PID: $SSH_AGENT_PID"
echo "SSH_AUTH_SOCK: $SSH_AUTH_SOCK"

# Check if the private key file exists
if [ ! -f "$ssh_private_key_file" ]; then
echo "Error: Private key file not found at $ssh_private_key_file"
exit 1
fi

# Add private key to ssh-agent
ssh-add -v "$ssh_private_key_file"

echo "Private key added to ssh-agent."
else
echo "SSH_AUTH_SOCK is already set."

# Ensure private key is added to ssh-agent
if ! ssh-add -l | grep -qF "$ssh_private_key_file"; then
echo "Private key is not in ssh-agent. Adding it now."
ssh-add "$ssh_private_key_file"
else
echo "Private key is already in ssh-agent."
fi
fi

# Show a summary of the Terraform variables
echo "Summary of Terraform variables (terraform.tfvars):"
awk -F' *= *' '/^[^#]/ {printf "| %-40s | %-20s |\n", $1, $2}' terraform.tfvars
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cluster_name = "koor-demo"
ssh_public_key_file = "~/.ssh/id_rsa.pub"
ssh_private_key_file = "~/.ssh/id_rsa"
control_plane_vm_count=3
initial_machinedeployment_replicas=3
worker_type="cpx41"
Expand Down

0 comments on commit ecbf5c0

Please sign in to comment.