-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
97 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/bin/bash | ||
|
||
LOCK_FILE="/tmp/update_ssh_config.lock" | ||
|
||
while [ -e $LOCK_FILE ]; do | ||
sleep 1 | ||
done | ||
|
||
touch $LOCK_FILE | ||
|
||
SSH_DIR="$HOME/.ssh" | ||
SSH_CONFIG="$SSH_DIR/config" | ||
ALIAS=$1 | ||
HOSTNAME=$2 | ||
SSH_KEY_PATH=$3 | ||
|
||
# Check if the .ssh directory exists | ||
if [ ! -d "$SSH_DIR" ]; then | ||
mkdir -p $SSH_DIR | ||
chmod 700 $SSH_DIR | ||
fi | ||
|
||
# Create the .ssh/config file if it doesn't exist | ||
touch $SSH_CONFIG | ||
chmod 600 $SSH_CONFIG | ||
|
||
# Prepare the updated entry with sub-parameters | ||
ENTRY="Host $ALIAS | ||
HostName $HOSTNAME | ||
HostKeyAlgorithms=+ssh-rsa | ||
StrictHostKeyChecking no | ||
UserKnownHostsFile=/dev/null | ||
Port 22 | ||
User ec2-user | ||
ServerAliveInterval 300 | ||
ServerAliveCountMax 2 | ||
IdentityFile $SSH_KEY_PATH" | ||
|
||
# If the entry exists, delete the full entry and its sub-parameters | ||
if grep -q "Host $ALIAS" $SSH_CONFIG; then | ||
awk -v alias="$ALIAS" ' | ||
$1 == "Host" && $2 == alias { skip = 1; next } | ||
$1 == "Host" && $2 != alias { skip = 0 } | ||
skip { next } | ||
1' $SSH_CONFIG > ${SSH_CONFIG}.tmp && mv ${SSH_CONFIG}.tmp $SSH_CONFIG | ||
fi | ||
|
||
# Append the new (or updated) entry with an additional newline for separation | ||
echo -e "\n$ENTRY\n" >> $SSH_CONFIG | ||
|
||
rm -f $LOCK_FILE | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
resource "null_resource" "update_ssh_config" { | ||
count = length(var.vpc_secondary_subnets) | ||
|
||
# This ensures that the provisioner will run again if the instance or key path changes | ||
triggers = { | ||
# always_run = "${timestamp()}" | ||
instance_public_dns = aws_instance.ce_instance[count.index].public_dns | ||
ssh_key_path = local_sensitive_file.my_private_key_file.filename | ||
hostname_alias = var.vpc_secondary_subnets[count.index].hostname | ||
} | ||
|
||
provisioner "local-exec" { | ||
command = <<-EOT | ||
./ssh_config_update.sh \ | ||
${var.vpc_secondary_subnets[count.index].hostname} \ | ||
${aws_instance.ce_instance[count.index].public_dns} \ | ||
${local_sensitive_file.my_private_key_file.filename} | ||
EOT | ||
on_failure = continue | ||
} | ||
|
||
# Ensure this runs after the EC2 instance is fully created. | ||
depends_on = [aws_instance.ce_instance] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters