Skip to content

Commit a65adf6

Browse files
authored
Generate ssh config file for digital ocean (#126)
having hosts in your .ssh/config allows for easy tab completion. This helps to easier connect to hosts created by terraform. Usage: Include this file in your ~/.ssh/config with: Include /path/to/this/ssh_config Or copy entries to your main SSH config file
1 parent eaa1864 commit a65adf6

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

terraform/devnet-0/digitalocean.tf

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ locals {
6868
can(regex("mev-relay", vm_group.name)) ? "mev-relay:${var.ethereum_network}" : null
6969
]))
7070
region = try(vm_group.region, var.digitalocean_regions[i % length(var.digitalocean_regions)])
71-
size = try(vm_group.size, can(regex("(super|bootnode)", vm_group.name)) ? var.digitalocean_supernode_size : var.digitalocean_fullnode_size)
72-
ipv6 = try(vm_group.ipv6, true)
71+
size = try(vm_group.size, can(regex("(super|bootnode)", vm_group.name)) ? var.digitalocean_supernode_size : var.digitalocean_fullnode_size)
72+
ipv6 = try(vm_group.ipv6, true)
7373
}
7474
}
7575
}
@@ -190,3 +190,52 @@ resource "local_file" "ansible_inventory" {
190190
)
191191
filename = "../../ansible/inventories/devnet-0/inventory.ini"
192192
}
193+
194+
locals {
195+
ssh_config_path = pathexpand("~/.ssh/config.d/ssh_config.${var.ethereum_network}")
196+
}
197+
198+
resource "local_file" "ssh_config" {
199+
content = templatefile("${path.module}/ssh_config.tmpl",
200+
{
201+
ethereum_network = var.ethereum_network
202+
hosts = merge(
203+
{
204+
for key, server in digitalocean_droplet.main : "${var.ethereum_network}-${key}" => {
205+
hostname = server.ipv4_address
206+
private_ip = server.ipv4_address_private
207+
name = key
208+
user = "devops"
209+
}
210+
}
211+
)
212+
}
213+
)
214+
filename = local.ssh_config_path
215+
216+
depends_on = [digitalocean_droplet.main]
217+
218+
lifecycle {
219+
create_before_destroy = true
220+
}
221+
}
222+
223+
# Ensure cleanup on destroy
224+
resource "null_resource" "ssh_config_cleanup" {
225+
triggers = {
226+
ssh_config_path = local.ssh_config_path
227+
}
228+
229+
# This provisioner runs on destroy
230+
provisioner "local-exec" {
231+
when = destroy
232+
command = "rm -f ${self.triggers.ssh_config_path} || true"
233+
}
234+
235+
depends_on = [local_file.ssh_config]
236+
}
237+
238+
output "ssh_config_file" {
239+
value = "SSH config generated at: ${local.ssh_config_path}"
240+
description = "Path to the generated SSH config file"
241+
}

terraform/devnet-0/ssh_config.tmpl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SSH Config for ${ethereum_network} devnet
2+
# Generated by Terraform
3+
# Usage: Include this file in your ~/.ssh/config with:
4+
# Include /path/to/this/ssh_config
5+
# Or copy entries to your main SSH config file
6+
7+
%{ for host_key, host in hosts ~}
8+
Host ${host_key}
9+
HostName ${host.hostname}
10+
User ${host.user}
11+
Port 22
12+
StrictHostKeyChecking no
13+
UserKnownHostsFile /dev/null
14+
LogLevel ERROR
15+
16+
%{ endfor ~}

0 commit comments

Comments
 (0)