aboutsummaryrefslogtreecommitdiff
path: root/terraform/volume/main.tf
blob: 7f4a8b6873c72383934a52e42a3188b20e25aa16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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}"
}