-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.data-disk.tf
92 lines (80 loc) · 2.47 KB
/
main.data-disk.tf
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
resource "google_compute_attached_disk" "data" {
depends_on = [
google_compute_instance.vm_instance,
google_compute_disk.data
]
disk = google_compute_disk.data.id
instance = google_compute_instance.vm_instance.id
device_name = "mithril-data-disk"
}
resource "google_compute_disk" "data" {
depends_on = [
google_compute_instance.vm_instance
]
name = "${local.environment_name}-data"
type = var.google_compute_instance_data_disk_type
zone = var.google_zone
size = var.google_compute_instance_data_disk_size
snapshot = var.google_compute_instance_data_disk_snapshot
labels = {
environment = local.environment_name
type = "data"
}
}
resource "google_compute_resource_policy" "policy-data" {
name = "${local.environment_name}-policy-data"
region = var.google_region
snapshot_schedule_policy {
schedule {
daily_schedule {
days_in_cycle = var.google_compute_instance_data_disk_snapshot_pace_days
start_time = var.google_compute_instance_data_disk_snapshot_start_time
}
}
retention_policy {
max_retention_days = var.google_compute_instance_data_disk_snapshot_max_retention_days
on_source_disk_delete = "KEEP_AUTO_SNAPSHOTS"
}
}
}
resource "google_compute_disk_resource_policy_attachment" "policy-attachment-data" {
name = google_compute_resource_policy.policy-data.name
disk = google_compute_disk.data.name
zone = var.google_zone
}
resource "null_resource" "mithril_mount_data_disk" {
depends_on = [
google_compute_attached_disk.data
]
triggers = {
attached_disk = google_compute_attached_disk.data.id
}
connection {
type = "ssh"
user = "curry"
private_key = local.google_service_account_private_key
host = google_compute_address.mithril-external-address.address
}
provisioner "remote-exec" {
inline = [
<<-EOT
set -e
# Format data disk if necessary
if sudo blkid /dev/disk/by-id/google-mithril-data-disk; then
echo "Data disk already formatted"
else
# Format data disk
echo "Format data disk"
sudo mkfs.ext4 -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-mithril-data-disk
fi
# Mount data disk
echo "Mount data disk"
mkdir -p /home/curry/data
sudo mount -o discard,defaults /dev/disk/by-id/google-mithril-data-disk /home/curry/data
# Update rights of data directory
sudo chown "curry":"curry" /home/curry/data -R
echo "Data disk mounted!"
EOT
]
}
}