Switch to cloud-init for host initialization

This commit is contained in:
Marco Nenciarini
2022-02-23 19:36:03 +01:00
parent c309a1b290
commit 5f7d47783b
6 changed files with 88 additions and 70 deletions

View File

@@ -8,10 +8,20 @@ resource "hcloud_server" "server" {
ssh_keys = var.ssh_keys
firewall_ids = var.firewall_ids
placement_group_id = var.placement_group_id
user_data = data.template_cloudinit_config.config.rendered
labels = var.labels
# Prevent destroying the whole cluster if the user changes
# any of the attributes that force to recreate the servers.
lifecycle {
ignore_changes = [
location,
ssh_keys,
user_data,
]
}
connection {
user = "root"
private_key = local.ssh_private_key
@@ -19,17 +29,6 @@ resource "hcloud_server" "server" {
host = self.ipv4_address
}
provisioner "file" {
content = local.ignition_config
destination = "/root/config.ign"
}
# Combustion script file to install k3s-selinux
provisioner "file" {
content = local.combustion_script
destination = "/root/script"
}
# Install MicroOS
provisioner "remote-exec" {
inline = local.microOS_install_commands
@@ -57,8 +56,6 @@ resource "hcloud_server" "server" {
"set -ex",
"rebootmgrctl set-strategy off",
"echo 'REBOOT_METHOD=kured' > /etc/transactional-update.conf",
# set the hostname
"hostnamectl set-hostname ${self.name}"
]
}
}
@@ -68,3 +65,33 @@ resource "hcloud_server_network" "server" {
server_id = hcloud_server.server.id
subnet_id = var.ipv4_subnet_id
}
data "template_cloudinit_config" "config" {
gzip = true
base64_encode = true
# Main cloud-config configuration file.
part {
filename = "init.cfg"
content_type = "text/cloud-config"
content = templatefile(
"${path.module}/templates/userdata.yaml.tpl",
{
hostname = var.name
sshAuthorizedKeys = concat([local.ssh_public_key], var.additional_public_keys)
}
)
}
# Initialization script (runs at every reboot)
part {
content_type = "text/cloud-boothook"
filename = "boothook.sh"
content = templatefile(
"${path.module}/templates/boothook.sh.tpl",
{
hostname = var.name
}
)
}
}