aboutsummaryrefslogtreecommitdiff
path: root/terraform/mount_volume/main.tf
blob: aed5324e0dbe2b598899631a87c060cddfe16ee2 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
variable "volume_name" {
  type = "string"
}

variable "host" {
  type = "string"
}

variable "server_id" {
  type = "string"
}

# volumes contain persistent storage and thus need to be initialized
# manually
data "hcloud_volume" "master" {
  name = "${var.volume_name}"
}

resource "hcloud_volume_attachment" "master_attachment" {
  volume_id = "${data.hcloud_volume.master.id}"
  server_id = "${var.server_id}"
}

resource "null_resource" "volume_mount" {
  triggers = {
    attachement_id = "${hcloud_volume_attachment.master_attachment.id}"
  }

  connection {
    host = "${var.host}"
  }

  provisioner "remote-exec" {
    inline = ["mkdir -p /mnt/storage"]
  }

  provisioner "file" {
    content = <<EOF
[Unit]
Description=Mount /mnt/storage directory

[Mount]
What=${data.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 mnt-storage.mount",
      "systemctl enable srv.mount",
      "systemctl enable home.mount",
      "systemctl reboot",
    ]
  }
}