-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
316 lines (275 loc) · 9.15 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
provider "google" {
project = "adsp-302713"
region = "europe-west3"
zone = "europe-west3-a"
}
resource "random_id" "id" {
byte_length = 4
}
resource "google_compute_instance" "node_red_cloud" {
name = "node-red-cloud-server"
machine_type = "n2-standard-2"
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
# A default network is created for all GCP projects
network = google_compute_network.vpc_network.self_link
access_config {
}
}
metadata = {
ssh-keys = "peng:${file("~/.ssh/id_rsa.pub")}"
}
connection {
type = "ssh"
host = self.network_interface.0.access_config.0.nat_ip
user = "peng"
port = 22
# agent = true
private_key = "${file("~/.ssh/id_rsa")}"
}
provisioner "remote-exec" {
# interpreter = ["/bin/bash" ,"-c"]
#inline = [
# "curl -fsSL https://get.docker.com -o get-docker.sh",
# "sudo sh get-docker.sh",
# "sudo docker run -d -ti -p 1883:1883 -p 1880:1880 niklasamslgruber/node-red-castle"
#]
# Run without docker on dev branche
inline = [
"sudo apt update && sudo apt upgrade -y",
"sudo apt install git nodejs npm python3-pip screen -y",
"sudo npm install npm@latest -g",
"pip3 install --upgrade pip",
"sudo npm install -g pm2",
"sudo npm install -g --unsafe-perm node-red",
"git clone https://github.com/niklasamslgruber/RedCASTLE.git",
"cd RedCASTLE",
"git checkout dev_advanced_benchmark", # change branche accordingly to needs
"chmod +x setup.sh",
"pip3 install -r requirements.txt",
# inject broker ip in the redcastle config file
# TODO: only work if the config is not changed to something else then localhost
"sed -i 's/localhost/${google_compute_instance.mqtt_broker_cloud.network_interface.0.access_config.0.nat_ip}/g' CASTLE/src/config.json",
# inject broker ip in the flow file directly changing the mqtt node config
# TODO: only work if the config is not changed to something else then localhost
"sed -i 's/localhost/${google_compute_instance.mqtt_broker_cloud.network_interface.0.access_config.0.nat_ip}/g' flow.json",
"pm2 start /usr/local/bin/node-red -- ~/RedCASTLE/flow.json",
"sleep 10s",
# install RedCastle npm dependencies
"cd ~/.node-red/",
"sudo npm install node-red-dashboard",
"sudo npm install node-red-contrib-zeromq",
"sudo npm install node-red-contrib-msg-speed",
"pm2 restart node-red",
"sleep 10s",
"pm2 logs node-red --nostream --lines 50"
]
}
}
resource "google_compute_instance" "mqtt_broker_cloud" {
name = "mqtt-broker-cloud-server"
machine_type = "n2-standard-2"
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
# A default network is created for all GCP projects
network = google_compute_network.vpc_network.self_link
access_config {
}
}
metadata = {
ssh-keys = "peng:${file("~/.ssh/id_rsa.pub")}"
}
connection {
type = "ssh"
host = self.network_interface.0.access_config.0.nat_ip
user = "peng"
port = 22
# agent = true
private_key = "${file("~/.ssh/id_rsa")}"
}
provisioner "file" {
source = "scripts/installRabbitMQ.sh"
destination = "~/installRabbitMQ.sh"
}
provisioner "remote-exec" {
# interpreter = ["/bin/bash" ,"-c"]
inline = [
"sudo apt install libncurses5 -y",
"chmod +x installRabbitMQ.sh",
"sudo ./installRabbitMQ.sh",
"sudo rabbitmq-plugins enable rabbitmq_mqtt",
"sudo rabbitmq-plugins enable rabbitmq_management",
"sudo service rabbitmq-server restart",
# TODO: create correct user with the right credentials
"sudo rabbitmqctl add_user peng peng",
"sudo rabbitmqctl set_user_tags peng administrator",
"sudo rabbitmqctl set_permissions 'peng' '.*' '.*' '.*'",
"echo 'Management UI should now be available via http://${self.network_interface.0.access_config.0.nat_ip}:15672'"
]
}
}
resource "google_compute_instance" "emulator_cloud" {
name = "emulator-cloud-server"
machine_type = "n2-standard-2"
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
# A default network is created for all GCP projects
network = google_compute_network.vpc_network.self_link
access_config {
}
}
metadata = {
ssh-keys = "peng:${file("~/.ssh/id_rsa.pub")}"
}
connection {
type = "ssh"
host = self.network_interface.0.access_config.0.nat_ip
user = "peng"
port = 22
# agent = true
private_key = "${file("~/.ssh/id_rsa")}"
}
provisioner "remote-exec" {
# interpreter = ["/bin/bash" ,"-c"]
inline = [
"sudo apt update && sudo apt upgrade -y",
"sudo apt install git nodejs npm python3-pip screen -y",
"sudo npm install npm@latest -g",
"pip3 install --upgrade pip",
"sudo npm install -g pm2",
"sudo npm install -g --unsafe-perm node-red",
"git clone https://github.com/niklasamslgruber/RedCASTLE.git",
"cd RedCASTLE",
"git checkout dev_advanced_benchmark", # change branche accordingly to needs
"chmod +x setup.sh",
"pip3 install -r requirements.txt",
# inject broker ip in the redcastle config file
# TODO: only work if the config is not changed to something else then localhost
"sed -i 's/localhost/${google_compute_instance.mqtt_broker_cloud.network_interface.0.access_config.0.nat_ip}/g' CASTLE/src/config.json",
# inject broker ip in the flow file directly changing the mqtt node config
# TODO: only work if the config is not changed to something else then localhost
"sed -i 's/localhost/${google_compute_instance.mqtt_broker_cloud.network_interface.0.access_config.0.nat_ip}/g' flow.json",
"pm2 start /usr/local/bin/node-red -- ~/RedCASTLE/flow.json",
"sleep 10s",
# install RedCastle npm dependencies
"cd ~/.node-red/",
"sudo npm install node-red-dashboard",
"sudo npm install node-red-contrib-zeromq",
"sudo npm install node-red-contrib-msg-speed",
"pm2 restart node-red",
"sleep 10s",
"pm2 logs node-red --nostream --lines 50"
]
}
}
resource "google_compute_network" "vpc_network" {
name = "cloud-network"
auto_create_subnetworks = "true"
}
#################################################################
# Firewall
#################################################################
resource "google_compute_firewall" "allow_ssh" {
name = "cloud-network-internal-allow-ssh-${random_id.id.hex}"
network = google_compute_network.vpc_network.self_link
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["0.0.0.0/0"]
}
resource "google_compute_firewall" "allow_internet" {
name = "cloud-network-internal-allow-http-${random_id.id.hex}"
network = google_compute_network.vpc_network.self_link
allow {
protocol = "tcp"
ports = ["80", "443"]
}
source_ranges = ["0.0.0.0/0"]
}
resource "google_compute_firewall" "allow_node_red" {
name = "cloud-network-internal-allow-node-red-${random_id.id.hex}"
network = google_compute_network.vpc_network.self_link
allow {
protocol = "tcp"
ports = ["1880"]
}
source_ranges = ["0.0.0.0/0"]
}
resource "google_compute_firewall" "allow_mqtt" {
name = "cloud-network-internal-allow-mqtt-${random_id.id.hex}"
network = google_compute_network.vpc_network.self_link
allow {
protocol = "tcp"
ports = ["1883"]
}
source_ranges = ["0.0.0.0/0"]
}
resource "google_compute_firewall" "allow_rabbitmq" {
name = "cloud-network-internal-allow-rabbitmq-${random_id.id.hex}"
network = google_compute_network.vpc_network.self_link
allow {
protocol = "tcp"
ports = ["15672", "5672", "5671", "4369", "25672"]
}
source_ranges = ["0.0.0.0/0"]
}
# Backup MOSQUITTO
#resource "google_compute_instance" "mqtt_broker_cloud" {
# name = "mqtt-broker-cloud-server"
# machine_type = "n2-standard-2"
#
# boot_disk {
# initialize_params {
# image = "debian-cloud/debian-10"
# }
# }
#
# network_interface {
# # A default network is created for all GCP projects
# network = google_compute_network.vpc_network.self_link
# access_config {
# }
# }
# metadata = {
# ssh-keys = "peng:${file("~/.ssh/id_rsa.pub")}"
# }
#
# connection {
# type = "ssh"
# host = self.network_interface.0.access_config.0.nat_ip
# user = "peng"
# port = 22
# agent = true
# private_key = "${file("~/.ssh/id_rsa")}"
# }
# provisioner "file" {
# source = "configs/mosquitto.conf"
# destination = "~/mosquitto.conf"
# }
#
# provisioner "remote-exec" {
# interpreter = ["/bin/bash" ,"-c"]
# inline = [
# #"sudo apt update && sudo apt upgrade -y",
# #"sudo apt install git python3-pip python3-zmq apt-transport-https ca-certificates gnupg -y",
# #"pip3 install --upgrade pip",
# "sudo mkdir -p /etc/mosquitto/conf.d",
# "sudo mv ~/mosquitto.conf /etc/mosquitto/conf.d/mosquitto.conf",
# "sudo apt update",
# "sudo apt install mosquitto mosquitto-clients -y",
# ]
# }
#}