-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloud_init.tf
143 lines (131 loc) · 5.59 KB
/
cloud_init.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
data "template_file" "moodle_script1" {
template = file("moodle1_cloud_init.sh")
vars = {
"ssh_public_key" = file("id_rsa.pub")
"ssh_private_key" = file("id_rsa")
"node1" = oci_core_instance.moodle_main1.private_ip
"node2" = oci_core_instance.moodle_main2.private_ip
}
}
resource "null_resource" "moodle1_cloud_init" {
depends_on = [oci_core_instance.bastion,oci_core_volume_attachment.moodlehtml_attach1]
provisioner "file" {
connection {
type = "ssh"
user = "opc"
host = oci_core_instance.moodle_main1.private_ip
private_key = file("id_rsa")
script_path = "/home/opc/moodle1.sh"
agent = false
timeout = "12m"
bastion_host = oci_core_instance.bastion.public_ip
bastion_port = "22"
bastion_user = "opc"
bastion_private_key = file("id_rsa")
}
content = data.template_file.moodle_script1.rendered
destination = "/home/opc/moodle1_cloud_init.sh"
}
provisioner "remote-exec" {
connection {
type = "ssh"
user = "opc"
host = oci_core_instance.moodle_main1.private_ip
private_key = file("id_rsa")
script_path = "/home/opc/moodle1.sh"
agent = false
timeout = "12m"
bastion_host = oci_core_instance.bastion.public_ip
bastion_port = "22"
bastion_user = "opc"
bastion_private_key = file("id_rsa")
}
inline = [
"sudo yum install dos2unix -y",
"dos2unix ~/moodle1_cloud_init.sh",
"sudo iscsiadm -m node -o new -T ${oci_core_volume_attachment.moodlehtml_attach1.iqn} -p ${oci_core_volume_attachment.moodlehtml_attach1.ipv4}:${oci_core_volume_attachment.moodlehtml_attach1.port}",
"sudo iscsiadm -m node -o update -T ${oci_core_volume_attachment.moodlehtml_attach1.iqn} -n node.startup -v automatic",
"sudo iscsiadm -m node -T ${oci_core_volume_attachment.moodlehtml_attach1.iqn} -p ${oci_core_volume_attachment.moodlehtml_attach1.ipv4}:${oci_core_volume_attachment.moodlehtml_attach1.port} -l",
"sudo iscsiadm -m node -o new -T ${oci_core_volume_attachment.moodledata_attach1.iqn} -p ${oci_core_volume_attachment.moodledata_attach1.ipv4}:${oci_core_volume_attachment.moodledata_attach1.port}",
"sudo iscsiadm -m node -o update -T ${oci_core_volume_attachment.moodledata_attach1.iqn} -n node.startup -v automatic",
"sudo iscsiadm -m node -T ${oci_core_volume_attachment.moodledata_attach1.iqn} -p ${oci_core_volume_attachment.moodledata_attach1.ipv4}:${oci_core_volume_attachment.moodledata_attach1.port} -l",
"chmod +x ~/moodle1_cloud_init.sh",
"sudo ~/moodle1_cloud_init.sh"
]
}
}
data "template_file" "moodle_script2" {
template = file("moodle2_cloud_init.sh")
vars = {
"ssh_public_key" = file("id_rsa.pub")
"ssh_private_key" = file("id_rsa")
"node1" = oci_core_instance.moodle_main1.private_ip
"node2" = oci_core_instance.moodle_main2.private_ip
}
}
resource "null_resource" "moodle2_cloud_init" {
depends_on = [oci_core_instance.bastion,oci_core_volume_attachment.moodlehtml_attach2]
provisioner "file" {
connection {
type = "ssh"
user = "opc"
host = oci_core_instance.moodle_main2.private_ip
private_key = file("id_rsa")
script_path = "/home/opc/moodle2.sh"
agent = false
timeout = "12m"
bastion_host = oci_core_instance.bastion.public_ip
bastion_port = "22"
bastion_user = "opc"
bastion_private_key = file("id_rsa")
}
content = data.template_file.moodle_script2.rendered
destination = "/home/opc/moodle2_cloud_init.sh"
}
provisioner "remote-exec" {
connection {
type = "ssh"
user = "opc"
host = oci_core_instance.moodle_main2.private_ip
private_key = file("id_rsa")
script_path = "/home/opc/moodle2.sh"
agent = false
timeout = "12m"
bastion_host = oci_core_instance.bastion.public_ip
bastion_port = "22"
bastion_user = "opc"
bastion_private_key = file("id_rsa")
}
inline = [
"sudo yum install dos2unix -y",
"dos2unix ~/moodle2_cloud_init.sh",
"sudo iscsiadm -m node -o new -T ${oci_core_volume_attachment.moodlehtml_attach2.iqn} -p ${oci_core_volume_attachment.moodlehtml_attach2.ipv4}:${oci_core_volume_attachment.moodlehtml_attach2.port}",
"sudo iscsiadm -m node -o update -T ${oci_core_volume_attachment.moodlehtml_attach2.iqn} -n node.startup -v automatic",
"sudo iscsiadm -m node -T ${oci_core_volume_attachment.moodlehtml_attach2.iqn} -p ${oci_core_volume_attachment.moodlehtml_attach2.ipv4}:${oci_core_volume_attachment.moodlehtml_attach2.port} -l",
"sudo iscsiadm -m node -o new -T ${oci_core_volume_attachment.moodledata_attach2.iqn} -p ${oci_core_volume_attachment.moodledata_attach2.ipv4}:${oci_core_volume_attachment.moodledata_attach2.port}",
"sudo iscsiadm -m node -o update -T ${oci_core_volume_attachment.moodledata_attach2.iqn} -n node.startup -v automatic",
"sudo iscsiadm -m node -T ${oci_core_volume_attachment.moodledata_attach2.iqn} -p ${oci_core_volume_attachment.moodledata_attach2.ipv4}:${oci_core_volume_attachment.moodledata_attach2.port} -l",
"chmod +x ~/moodle2_cloud_init.sh",
"sudo ~/moodle2_cloud_init.sh"
]
}
}
data "template_file" "bastion_script" {
template = file("bastion.tpl")
vars = {
"ssh_public_key" = file("id_rsa.pub")
"ssh_private_key" = file("id_rsa")
"mysql_admin_user" = "${var.mysql_admin_user}"
"mysql_admin_password" = "${var.mysql_admin_password}"
"mysql_host" = "${oci_mysql_mysql_db_system.MoodleDb.ip_address}"
}
}
data "template_cloudinit_config" "bastion_cloud_init" {
gzip = true
base64_encode = true
part {
filename = "cloud-init.sh"
content_type = "text/x-shellscript"
content = data.template_file.bastion_script.rendered
}
}