-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
159 lines (132 loc) · 4.21 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
variable "storage_ip" {
description = "The ip address of the storage server"
}
variable "storage_ssh_user" {
description = "Username to connect to the remote server"
}
variable "storage_ssh_private_key_path" {
description = "Path for the key of the user to connect to the remote server"
}
variable "root_tftp_folder" {
description = "the folder in the remote server that is the root of the tftp server"
}
variable "source_images_folder" {
description = "the folder in the remote server that is the root of the tftp server"
}
variable "source_pi_image" {
description = "the image of the root disk for the ISO"
default = "/home/jose/src/mylab/03.pi-k8s-image/packer-output/temp-packer-pi"
}
variable "pis" {
description = "dictionary with the PIs, keys are pi name"
# //
# "pi01" : {
# "serial": "serial",
# "mac_addr": "mac"
# },
}
locals {
any_pi= element(keys(var.pis),1)
}
locals {
storage_ssh_private_key = file(var.storage_ssh_private_key_path)
}
resource "null_resource" "discover_iscsi_targets" {
provisioner "local-exec" {
command = "sudo iscsiadm -m discovery -t st -p ${var.storage_ip}"
}
}
resource "null_resource" "connect_discs" {
for_each = var.pis
provisioner "local-exec" {
command = "sudo iscsiadm -m node -T iqn.2004-04.com.qnap:tvs-882:iscsi.${each.key}.04a272 -p ${var.storage_ip} -l; sleep 5"
}
depends_on = [
null_resource.discover_iscsi_targets
]
}
locals {
temp_dir = "/tmp/mounted"
}
resource "null_resource" "copy_firmware" {
connection {
type = "ssh"
host = var.storage_ip
user = var.storage_ssh_user
private_key = local.storage_ssh_private_key
timeout = "20s"
}
provisioner "local-exec" {
command = <<EOF
mkdir ${local.temp_dir}
sudo losetup --partscan --find ${var.source_images_folder}/${local.any_pi}
sync
sleep 10
sudo mount -t ext4 /dev/disk/by-label/cloudimg-rootfs ${local.temp_dir}
EOF
}
provisioner "file" {
destination = var.root_tftp_folder
source = "${local.temp_dir}/boot/firmware"
}
provisioner "file" {
destination = "${var.root_tftp_folder}/firmware/config.txt"
source = "config.txt"
}
}
resource "null_resource" "render_bootcmd" {
for_each = var.pis
connection {
type = "ssh"
host = var.storage_ip
user = var.storage_ssh_user
private_key = local.storage_ssh_private_key
timeout = "20s"
}
provisioner "file" {
destination = "${var.root_tftp_folder}/${each.key}/cmdline.txt"
content = "console=serial0,115200 dwc_otg.lpm_enable=0 cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory console=tty1 root=/dev/sda1 rootfstype=ext4 ISCSI_INITIATOR=iqn.2019-09.us.tswn.us:${each.key} ISCSI_TARGET_NAME=iqn.2004-04.com.qnap:tvs-882:iscsi.${each.key}.04a272 ISCSI_TARGET_IP=${var.storage_ip} rootwait fixrtc nosplash"
}
}
resource "null_resource" "prepare_pi_boot_filesystem" {
for_each = var.pis
connection {
type = "ssh"
host = var.storage_ip
user = var.storage_ssh_user
private_key = local.storage_ssh_private_key
timeout = "20s"
}
provisioner "remote-exec" {
inline = [
# "mkdir ${var.root_tftp_folder}/${each.key} ${var.root_tftp_folder}/${each.value.serial}",
"sudo mount -t overlay overlay -o lowerdir=${var.root_tftp_folder}/firmware:${var.root_tftp_folder}/${each.key} ${var.root_tftp_folder}/${each.value.serial}"
]
}
depends_on = [
null_resource.render_bootcmd,
null_resource.copy_firmware
]
}
resource "null_resource" "transfer_root_volumes" {
for_each = var.pis
provisioner "local-exec" {
command = <<EOT
sudo dd bs=4M if=${var.source_pi_image} of=/dev/disk/by-path/ip-${var.storage_ip}:3260-iscsi-iqn.2004-04.com.qnap:tvs-882:iscsi.${each.key}.04a272-lun-0 status=progress &&
sudo sync
EOT
}
depends_on = [
null_resource.connect_discs
]
}
resource "null_resource" "disconnect_resources" {
provisioner "local-exec" {
command = "sudo iscsiadm -m session -u ; sudo iscsiadm -m node --op delete; sudo umount ${local.temp_dir}; rm -rf ${local.temp_dir}"
}
depends_on = [
null_resource.prepare_pi_boot_filesystem,
null_resource.transfer_root_volumes,
null_resource.render_bootcmd
]
}