diff options
author | Jakob Odersky <jakob@odersky.com> | 2018-11-01 14:57:19 -0700 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2018-11-01 14:57:19 -0700 |
commit | db27247dd7d7209ab93419eb33d2ecb21e74c1ec (patch) | |
tree | 4e9209398b9055c0c91f819200d878a6633b59a0 | |
parent | 802e1268022d8398c565601fd324994e5eab5ca8 (diff) | |
download | infra-db27247dd7d7209ab93419eb33d2ecb21e74c1ec.tar.gz infra-db27247dd7d7209ab93419eb33d2ecb21e74c1ec.tar.bz2 infra-db27247dd7d7209ab93419eb33d2ecb21e74c1ec.zip |
Move volume config to vps module
-rwxr-xr-x | terraform/main.tf | 8 | ||||
-rw-r--r-- | terraform/stdvps/main.tf | 94 | ||||
-rw-r--r-- | terraform/volume/main.tf | 68 |
3 files changed, 95 insertions, 75 deletions
diff --git a/terraform/main.tf b/terraform/main.tf index 54ed333..b25598e 100755 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -22,7 +22,6 @@ provider "acme" { ################################################################################ -# Main ssh key resource "hcloud_ssh_key" "root" { name = "root" public_key = "${file("~/.ssh/id_rsa.pub")}" @@ -32,12 +31,7 @@ module "vps" { source = "./stdvps" location = "nbg1" ssh_key_name = "${hcloud_ssh_key.root.name}" -} - -module "volume" { - source = "./volume" - server_id = "${module.vps.id}" - server_fqdn = "${module.vps.fqdn}" + volume_name = "master" } module "roles" { diff --git a/terraform/stdvps/main.tf b/terraform/stdvps/main.tf index 2328bb1..2e94f1d 100644 --- a/terraform/stdvps/main.tf +++ b/terraform/stdvps/main.tf @@ -6,6 +6,11 @@ variable "location" { type = "string" } +variable "volume_name" { + type = "string" + default = "" +} + resource "random_id" "server" { prefix = "peter-" byte_length = 2 @@ -33,6 +38,95 @@ resource "cloudflare_record" "record_aaaa" { type = "AAAA" } +resource "hcloud_volume" "master" { + count = "${var.volume_name == "" ? 0 : 1}" + name = "${var.volume_name}" + size = 50 + server_id = "${hcloud_server.server.id}" +} + +# volumes contain persistent storage and thus need to be initialized manually +resource "null_resource" "volume_mount" { + count = "${var.volume_name == "" ? 0 : 1}" + + triggers = { + server_id = "${hcloud_server.server.id}" + volume_id = "${hcloud_volume.master.id}" + } + + connection { + host = "${hcloud_server.server.ipv4_address}" + } + + provisioner "remote-exec" { + inline = ["mkdir -p /mnt/storage"] + } + + provisioner "file" { + content = <<EOF +[Unit] +Description=Mount /mnt/storage directory + +[Mount] +What=${hcloud_volume.master.linux_device} +Where=/mnt/storage +Type=ext4 +Options=defaults + +[Install] +WantedBy=multi-user.target +EOF + destination = "/etc/systemd/system/mnt-storage.mount" + } + + provisioner "file" { + content = <<EOF +[Unit] +Description=Mount /srv to persistent volume storage +After=mnt-storage.mount +BindsTo=mnt-storage.mount + +[Mount] +What=/mnt/storage/srv +Where=/srv +Type=ext4 +Options=bind + +[Install] +WantedBy=multi-user.target +EOF + destination = "/etc/systemd/system/srv.mount" + } + + provisioner "file" { + content = <<EOF +[Unit] +Description=Mount /home to persistent volume storage +After=mnt-storage.mount +BindsTo=mnt-storage.mount + +[Mount] +What=/mnt/storage/home +Where=/home +Type=ext4 +Options=bind + +[Install] +WantedBy=multi-user.target +EOF + destination = "/etc/systemd/system/home.mount" + } + + provisioner "remote-exec" { + inline = [ + "systemctl daemon-reload", + "systemctl enable --now mnt-storage.mount", + "systemctl enable --now srv.mount", + "systemctl enable --now home.mount" + ] + } +} + output "ipv4" { value = "${hcloud_server.server.ipv4_address}" } diff --git a/terraform/volume/main.tf b/terraform/volume/main.tf deleted file mode 100644 index 7f4a8b6..0000000 --- a/terraform/volume/main.tf +++ /dev/null @@ -1,68 +0,0 @@ -variable "server_fqdn" { - type = "string" -} - -variable "server_id" { - type = "string" -} - -resource "hcloud_volume" "master" { - name = "master" - size = 50 - server_id = "${var.server_id}" -} - -# this is only run once if the volume id changes -resource "null_resource" "volume_format" { - triggers = { - volume_id = "${hcloud_volume.master.id}" - } - - connection { - host = "${var.server_fqdn}" - } - - provisioner "remote-exec" { - inline = [ - "mkfs.ext4 ${hcloud_volume.master.linux_device}", - ] - } -} - -resource "null_resource" "volume_mount" { - triggers = { - server_id = "${var.server_id}" - volume_id = "${hcloud_volume.master.id}" - } - - connection { - host = "${var.server_fqdn}" - } - - provisioner "file" { - content = <<EOF -[Unit] -Description=Mount /srv directory - -[Mount] -What=${hcloud_volume.master.linux_device} -Where=/srv -Type=ext4 -Options=defaults -EOF - - destination = "/etc/systemd/system/srv.mount" - } - - provisioner "remote-exec" { - inline = [ - "systemctl daemon-reload", - "systemctl enable srv.mount", - "systemctl start srv.mount", - ] - } -} - -output "server_id" { - value = "${null_resource.volume_mount.triggers.server_id}" -} |