Skip to content

Commit

Permalink
fix(terraform): better cloud-init, hostname setup
Browse files Browse the repository at this point in the history
  • Loading branch information
raisedadead committed Jul 16, 2023
1 parent b4d6587 commit adea2f2
Show file tree
Hide file tree
Showing 20 changed files with 258 additions and 79 deletions.
9 changes: 6 additions & 3 deletions terraform/ops-cluster-o11y/cloud-init--userdata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ runcmd:
- usermod -aG docker freecodecamp
# This sections should be the last in the list because it involves restarting the ssh service,
# which may prevent commands from running after it in certain scenarios.
- sed -i -e '/^PermitRootLogin/s/^.*$/PermitRootLogin no/' /etc/ssh/sshd_config
- sed -i -e '/^PasswordAuthentication/s/^.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
- sed -i -e '$aAllowUsers freecodecamp' /etc/ssh/sshd_config
- |
sed -i -e '/^PermitRootLogin/s/^.*$/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i -e '/^PasswordAuthentication/s/^.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
sed -i -e '$aAllowUsers freecodecamp' /etc/ssh/sshd_config
sed -i '/preserve_hostname: false/c\preserve_hostname: true' /etc/cloud/cloud.cfg
# restart ssh service
- restart ssh"}
final_message: 'Setup complete'
30 changes: 24 additions & 6 deletions terraform/ops-cluster-o11y/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,22 @@ resource "linode_instance_config" "ops_o11y_leaders_config" {
host = linode_instance.ops_o11y_leaders[count.index].ip_address
}

# All of the provisioning should be done via cloud-init.
# This is just to setup the reboot.
provisioner "remote-exec" {
inline = [
# Wait for cloud-init to finish.
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
# Set the hostname.
"hostnamectl set-hostname ldr-${count.index + 1}.o11y.${data.linode_domain.ops_dns_domain.domain}",
"echo \"ldr-${count.index + 1}.o11y.${data.linode_domain.ops_dns_domain.domain}\" > /etc/hostname",
"echo Current hostname...; hostname",
"shutdown -r +1 'Terraform: Rebooting to apply hostname change in 1 min.'"
]
}

# This run is a hack to trigger the reboot,
# which may fail otherwise in the previous step.
provisioner "remote-exec" {
inline = [
"uptime"
]
}

Expand Down Expand Up @@ -183,13 +192,22 @@ resource "linode_instance_config" "ops_o11y_workers_config" {
host = linode_instance.ops_o11y_workers[count.index].ip_address
}

# All of the provisioning should be done via cloud-init.
# This is just to setup the reboot.
provisioner "remote-exec" {
inline = [
# Wait for cloud-init to finish.
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
# Set the hostname.
"hostnamectl set-hostname wkr-${count.index + 1}.o11y.${data.linode_domain.ops_dns_domain.domain}",
"echo \"wkr-${count.index + 1}.o11y.${data.linode_domain.ops_dns_domain.domain}\" > /etc/hostname",
"echo Current hostname...; hostname",
"shutdown -r +1 'Terraform: Rebooting to apply hostname change in 1 min.'"
]
}

# This run is a hack to trigger the reboot,
# which may fail otherwise in the previous step.
provisioner "remote-exec" {
inline = [
"uptime"
]
}

Expand Down
18 changes: 0 additions & 18 deletions terraform/ops-test/cloud-init--userdata.yml

This file was deleted.

31 changes: 31 additions & 0 deletions terraform/ops-test/cloud-init--userdata.yml.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#cloud-config
users:
- name: freecodecamp
groups: sudo
shell: /bin/bash
sudo: ['ALL=(ALL) NOPASSWD:ALL']
ssh_import_id:
- gh:camperbot
- raisedadead
runcmd:
- usermod -aG docker freecodecamp
# Configure sshd
- |
sed -i -e '/^PermitRootLogin/s/^.*$/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i -e '/^PasswordAuthentication/s/^.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
sed -i -e '/^PubkeyAuthentication/s/^.*$/PubkeyAuthentication yes/' /etc/ssh/sshd_config
sed -i -e '$aAllowUsers freecodecamp' /etc/ssh/sshd_config
# Configure hostname
- |
sed -i '/preserve_hostname: false/c\preserve_hostname: true' /etc/cloud/cloud.cfg
hostnamectl set-hostname ${tf_hostname}
echo \"${tf_hostname}\" > /etc/hostname
#
# :-----------------------: WARNING :-----------------------:
#
# This next line should be the last command in the list,
# because it involves restarting the ssh service.
#
# :-----------------------: WARNING :-----------------------:
- systemctl restart sshd
final_message: 'Setup complete'
21 changes: 17 additions & 4 deletions terraform/ops-test/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ resource "linode_instance_disk" "ops_test_disk__boot" {

stackscript_id = data.linode_stackscripts.cloudinit_scripts.stackscripts.0.id
stackscript_data = {
userdata = filebase64("${path.root}/cloud-init--userdata.yml")
userdata = base64encode(
templatefile("${path.root}/cloud-init--userdata.yml.tftpl", {
tf_hostname = "test.${data.linode_domain.ops_dns_domain.domain}"
})
)
}
}

Expand Down Expand Up @@ -73,13 +77,22 @@ resource "linode_instance_config" "ops_test_config" {
host = linode_instance.ops_test.ip_address
}

# All of the provisioning should be done via cloud-init.
# This is just to setup the reboot.
provisioner "remote-exec" {
inline = [
# Wait for cloud-init to finish.
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
# Set the hostname.
"hostnamectl set-hostname test.${data.linode_domain.ops_dns_domain.domain}",
"echo \"test.${data.linode_domain.ops_dns_domain.domain}\" > /etc/hostname",
"echo Current hostname...; hostname",
"shutdown -r +1 'Terraform: Rebooting to apply hostname change in 1 min.'"
]
}

# This run is a hack to trigger the reboot,
# which may fail otherwise in the previous step.
provisioner "remote-exec" {
inline = [
"uptime"
]
}

Expand Down
9 changes: 6 additions & 3 deletions terraform/prd-cluster-publish/cloud-init--userdata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ runcmd:
- usermod -aG docker freecodecamp
# This sections should be the last in the list because it involves restarting the ssh service,
# which may prevent commands from running after it in certain scenarios.
- sed -i -e '/^PermitRootLogin/s/^.*$/PermitRootLogin no/' /etc/ssh/sshd_config
- sed -i -e '/^PasswordAuthentication/s/^.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
- sed -i -e '$aAllowUsers freecodecamp' /etc/ssh/sshd_config
- |
sed -i -e '/^PermitRootLogin/s/^.*$/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i -e '/^PasswordAuthentication/s/^.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
sed -i -e '$aAllowUsers freecodecamp' /etc/ssh/sshd_config
sed -i '/preserve_hostname: false/c\preserve_hostname: true' /etc/cloud/cloud.cfg
# restart ssh service
- restart ssh"}
final_message: 'Setup complete'
30 changes: 24 additions & 6 deletions terraform/prd-cluster-publish/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,22 @@ resource "linode_instance_config" "prd_publish_leaders_config" {
host = linode_instance.prd_publish_leaders[count.index].ip_address
}

# All of the provisioning should be done via cloud-init.
# This is just to setup the reboot.
provisioner "remote-exec" {
inline = [
# Wait for cloud-init to finish.
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
# Set the hostname.
"hostnamectl set-hostname ldr-${count.index + 1}.publish.prd.${data.linode_domain.ops_dns_domain.domain}",
"echo \"ldr-${count.index + 1}.publish.prd.${data.linode_domain.ops_dns_domain.domain}\" > /etc/hostname",
"echo Current hostname...; hostname",
"shutdown -r +1 'Terraform: Rebooting to apply hostname change in 1 min.'"
]
}

# This run is a hack to trigger the reboot,
# which may fail otherwise in the previous step.
provisioner "remote-exec" {
inline = [
"uptime"
]
}

Expand Down Expand Up @@ -183,13 +192,22 @@ resource "linode_instance_config" "prd_publish_workers_config" {
host = linode_instance.prd_publish_workers[count.index].ip_address
}

# All of the provisioning should be done via cloud-init.
# This is just to setup the reboot.
provisioner "remote-exec" {
inline = [
# Wait for cloud-init to finish.
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
# Set the hostname.
"hostnamectl set-hostname wkr-${count.index + 1}.publish.prd.${data.linode_domain.ops_dns_domain.domain}",
"echo \"wkr-${count.index + 1}.publish.prd.${data.linode_domain.ops_dns_domain.domain}\" > /etc/hostname",
"echo Current hostname...; hostname",
"shutdown -r +1 'Terraform: Rebooting to apply hostname change in 1 min.'"
]
}

# This run is a hack to trigger the reboot,
# which may fail otherwise in the previous step.
provisioner "remote-exec" {
inline = [
"uptime"
]
}

Expand Down
9 changes: 6 additions & 3 deletions terraform/stg-cluster-oldeworld/cloud-init--userdata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ runcmd:
- usermod -aG docker freecodecamp
# This sections should be the last in the list because it involves restarting the ssh service,
# which may prevent commands from running after it in certain scenarios.
- sed -i -e '/^PermitRootLogin/s/^.*$/PermitRootLogin no/' /etc/ssh/sshd_config
- sed -i -e '/^PasswordAuthentication/s/^.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
- sed -i -e '$aAllowUsers freecodecamp' /etc/ssh/sshd_config
- |
sed -i -e '/^PermitRootLogin/s/^.*$/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i -e '/^PasswordAuthentication/s/^.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
sed -i -e '$aAllowUsers freecodecamp' /etc/ssh/sshd_config
sed -i '/preserve_hostname: false/c\preserve_hostname: true' /etc/cloud/cloud.cfg
# restart ssh service
- restart ssh"}
final_message: 'Setup complete'
15 changes: 12 additions & 3 deletions terraform/stg-cluster-oldeworld/next-01-nginx.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,22 @@ resource "linode_instance_config" "stg_oldeworld_pxy_config" {
host = linode_instance.stg_oldeworld_pxy[count.index].ip_address
}

# All of the provisioning should be done via cloud-init.
# This is just to setup the reboot.
provisioner "remote-exec" {
inline = [
# Wait for cloud-init to finish.
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
# Set the hostname.
"hostnamectl set-hostname pxy-${count.index + 1}.oldeworld.stg.${data.linode_domain.ops_dns_domain.domain}",
"echo \"pxy-${count.index + 1}.oldeworld.stg.${data.linode_domain.ops_dns_domain.domain}\" > /etc/hostname",
"echo Current hostname...; hostname",
"shutdown -r +1 'Terraform: Rebooting to apply hostname change in 1 min.'"
]
}

# This run is a hack to trigger the reboot,
# which may fail otherwise in the previous step.
provisioner "remote-exec" {
inline = [
"uptime"
]
}

Expand Down
15 changes: 12 additions & 3 deletions terraform/stg-cluster-oldeworld/next-11-client-eng.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,22 @@ resource "linode_instance_config" "stg_oldeworld_clteng_config" {
host = linode_instance.stg_oldeworld_clteng[count.index].ip_address
}

# All of the provisioning should be done via cloud-init.
# This is just to setup the reboot.
provisioner "remote-exec" {
inline = [
# Wait for cloud-init to finish.
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
# Set the hostname.
"hostnamectl set-hostname clteng-${count.index + 1}.oldeworld.stg.${data.linode_domain.ops_dns_domain.domain}",
"echo \"clteng-${count.index + 1}.oldeworld.stg.${data.linode_domain.ops_dns_domain.domain}\" > /etc/hostname",
"echo Current hostname...; hostname",
"shutdown -r +1 'Terraform: Rebooting to apply hostname change in 1 min.'"
]
}

# This run is a hack to trigger the reboot,
# which may fail otherwise in the previous step.
provisioner "remote-exec" {
inline = [
"uptime"
]
}

Expand Down
15 changes: 12 additions & 3 deletions terraform/stg-cluster-oldeworld/next-12-client-chn.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,22 @@ resource "linode_instance_config" "stg_oldeworld_cltchn_config" {
host = linode_instance.stg_oldeworld_cltchn[count.index].ip_address
}

# All of the provisioning should be done via cloud-init.
# This is just to setup the reboot.
provisioner "remote-exec" {
inline = [
# Wait for cloud-init to finish.
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
# Set the hostname.
"hostnamectl set-hostname cltchn-${count.index + 1}.oldeworld.stg.${data.linode_domain.ops_dns_domain.domain}",
"echo \"cltchn-${count.index + 1}.oldeworld.stg.${data.linode_domain.ops_dns_domain.domain}\" > /etc/hostname",
"echo Current hostname...; hostname",
"shutdown -r +1 'Terraform: Rebooting to apply hostname change in 1 min.'"
]
}

# This run is a hack to trigger the reboot,
# which may fail otherwise in the previous step.
provisioner "remote-exec" {
inline = [
"uptime"
]
}

Expand Down
15 changes: 12 additions & 3 deletions terraform/stg-cluster-oldeworld/next-13-client-cnt.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,22 @@ resource "linode_instance_config" "stg_oldeworld_cltcnt_config" {
host = linode_instance.stg_oldeworld_cltcnt[count.index].ip_address
}

# All of the provisioning should be done via cloud-init.
# This is just to setup the reboot.
provisioner "remote-exec" {
inline = [
# Wait for cloud-init to finish.
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
# Set the hostname.
"hostnamectl set-hostname cltcnt-${count.index + 1}.oldeworld.stg.${data.linode_domain.ops_dns_domain.domain}",
"echo \"cltcnt-${count.index + 1}.oldeworld.stg.${data.linode_domain.ops_dns_domain.domain}\" > /etc/hostname",
"echo Current hostname...; hostname",
"shutdown -r +1 'Terraform: Rebooting to apply hostname change in 1 min.'"
]
}

# This run is a hack to trigger the reboot,
# which may fail otherwise in the previous step.
provisioner "remote-exec" {
inline = [
"uptime"
]
}

Expand Down
15 changes: 12 additions & 3 deletions terraform/stg-cluster-oldeworld/next-14-client-esp.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,22 @@ resource "linode_instance_config" "stg_oldeworld_cltesp_config" {
host = linode_instance.stg_oldeworld_cltesp[count.index].ip_address
}

# All of the provisioning should be done via cloud-init.
# This is just to setup the reboot.
provisioner "remote-exec" {
inline = [
# Wait for cloud-init to finish.
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
# Set the hostname.
"hostnamectl set-hostname cltesp-${count.index + 1}.oldeworld.stg.${data.linode_domain.ops_dns_domain.domain}",
"echo \"cltesp-${count.index + 1}.oldeworld.stg.${data.linode_domain.ops_dns_domain.domain}\" > /etc/hostname",
"echo Current hostname...; hostname",
"shutdown -r +1 'Terraform: Rebooting to apply hostname change in 1 min.'"
]
}

# This run is a hack to trigger the reboot,
# which may fail otherwise in the previous step.
provisioner "remote-exec" {
inline = [
"uptime"
]
}

Expand Down
15 changes: 12 additions & 3 deletions terraform/stg-cluster-oldeworld/next-15-client-ger.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,22 @@ resource "linode_instance_config" "stg_oldeworld_cltger_config" {
host = linode_instance.stg_oldeworld_cltger[count.index].ip_address
}

# All of the provisioning should be done via cloud-init.
# This is just to setup the reboot.
provisioner "remote-exec" {
inline = [
# Wait for cloud-init to finish.
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
# Set the hostname.
"hostnamectl set-hostname cltger-${count.index + 1}.oldeworld.stg.${data.linode_domain.ops_dns_domain.domain}",
"echo \"cltger-${count.index + 1}.oldeworld.stg.${data.linode_domain.ops_dns_domain.domain}\" > /etc/hostname",
"echo Current hostname...; hostname",
"shutdown -r +1 'Terraform: Rebooting to apply hostname change in 1 min.'"
]
}

# This run is a hack to trigger the reboot,
# which may fail otherwise in the previous step.
provisioner "remote-exec" {
inline = [
"uptime"
]
}

Expand Down
Loading

0 comments on commit adea2f2

Please sign in to comment.