-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-and-deploy-network.sh
executable file
·1059 lines (959 loc) · 39.2 KB
/
build-and-deploy-network.sh
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Function to wait for a job to complete
wait_for_job() {
local job_prefix=$1
echo "Waiting for job $job_prefix to complete..."
while true; do
job_name=$(kubectl get jobs --selector=job-name=$job_prefix -o jsonpath='{.items[*].metadata.name}')
if [ -n "$job_name" ]; then
status=$(kubectl get job $job_name -o jsonpath='{.status.conditions[?(@.type=="Complete")].status}')
if [ "$status" == "True" ]; then
echo "Job $job_name completed successfully."
break
fi
fi
sleep 5
done
}
# Function to check if a node exists in the cluster
check_node_exists() {
local node_name=$1
kubectl get nodes | grep -q "$node_name"
return $?
}
generate_network_key_zmq() {
local key_dir="kubernetes-manifests/generated/network_keys"
mkdir -p "$key_dir"
# Install dependencies and compile curve_keygen
sudo apt-get update && sudo apt-get install -y g++ libzmq3-dev
wget https://raw.githubusercontent.com/zeromq/libzmq/master/tools/curve_keygen.cpp
g++ curve_keygen.cpp -o curve_keygen -lzmq
# Generate keys
key_output=$(./curve_keygen)
public_key=$(echo "$key_output" | grep "CURVE PUBLIC KEY" -A 1 | tail -n 1)
private_key=$(echo "$key_output" | grep "CURVE SECRET KEY" -A 1 | tail -n 1)
# Save keys to files (for reference, consider removing in production)
echo "$public_key" > "$key_dir/network_public.key"
echo "$private_key" > "$key_dir/network_private.key"
# Create a Kubernetes Secret YAML
cat << EOF > kubernetes-manifests/generated/network-key-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: network-key-secret
type: Opaque
stringData:
network_private_key: "${private_key}"
network_public_key: "${public_key}"
EOF
echo "Network key Secret YAML has been created at kubernetes-manifests/generated/network-key-secret.yaml"
kubectl apply -f kubernetes-manifests/generated/network-key-secret.yaml
# Clean up
rm curve_keygen curve_keygen.cpp
}
generate_ssl_certificates() {
local num_nodes=$1
local cert_dir="kubernetes-manifests/generated/certs"
mkdir -p "$cert_dir"
# Generate CA key and certificate
openssl genrsa -out "$cert_dir/ca.key" 4096
openssl req -new -x509 -key "$cert_dir/ca.key" -out "$cert_dir/ca.crt" -days 365 -subj "/CN=CouchDB CA"
chmod 600 "$cert_dir/ca.key" "$cert_dir/ca.crt"
# Generate certificates for each node
for ((i=0; i<num_nodes; i++)); do
openssl genrsa -out "$cert_dir/node$i.key" 2048
openssl req -new -key "$cert_dir/node$i.key" -out "$cert_dir/node$i.csr" \
-subj "/CN=couchdb-$i.default.svc.cluster.local"
cat > "$cert_dir/node$i.ext" << EOF
subjectAltName = DNS:couchdb-$i.default.svc.cluster.local, \
DNS:couchdb@couchdb-$i.default.svc.cluster.local, \
DNS:couchdb-$i, \
DNS:localhost
EOF
openssl x509 -req -in "$cert_dir/node$i.csr" -CA "$cert_dir/ca.crt" -CAkey "$cert_dir/ca.key" \
-CAcreateserial -out "$cert_dir/node$i.crt" -days 365 -extfile "$cert_dir/node$i.ext"
chmod 600 "$cert_dir/node$i.key" "$cert_dir/node$i.csr" "$cert_dir/node$i.crt" "$cert_dir/node$i.ext"
done
# Create Kubernetes secret for certificates
kubectl create secret generic couchdb-certs \
--from-file="$cert_dir/ca.crt" \
$(for ((i=0; i<num_nodes; i++)); do echo "--from-file=node${i}_crt=$cert_dir/node$i.crt --from-file=node${i}_key=$cert_dir/node$i.key"; done)
}
generate_couchdb_yaml() {
local num_compute_nodes=$1
local yaml_content="apiVersion: v1
kind: List
items:"
# Generate PVCs
for ((i=0; i<num_compute_nodes; i++)); do
yaml_content+="
- apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: couchdb${i}-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi"
done
# Generate Deployments
for ((i=0; i<num_compute_nodes; i++)); do
yaml_content+="
- apiVersion: apps/v1
kind: Deployment
metadata:
name: couchdb-${i}
spec:
selector:
matchLabels:
app: couchdb-${i}
replicas: 1
template:
metadata:
labels:
app: couchdb-${i}
spec:
nodeSelector:
kubernetes.io/hostname: compute-node-$((i+1))
initContainers:
- name: init-config
image: busybox
command: ['sh', '-c', 'cp /tmp/couchdb-config/* /opt/couchdb/etc/local.d/ && echo \"Config files copied\" && ls -la /opt/couchdb/etc/local.d']
volumeMounts:
- name: couchdb-config
mountPath: /tmp/couchdb-config
- name: config-storage
mountPath: /opt/couchdb/etc/local.d
containers:
- name: couchdb
image: couchdb:latest
ports:
- containerPort: 5984
- containerPort: 6984
env:
- name: COUCHDB_USER
valueFrom:
secretKeyRef:
name: couchdb-secrets
key: COUCHDB_USER
- name: COUCHDB_PASSWORD
valueFrom:
secretKeyRef:
name: couchdb-secrets
key: COUCHDB_PASSWORD
- name: COUCHDB_SECRET
valueFrom:
secretKeyRef:
name: couchdb-secrets
key: COUCHDB_SECRET
- name: ERL_FLAGS
value: \"-setcookie \\\"jT7egojgnPLzOncq9MQUzqwqHm6ZiPUU7xJfFLA8MA\\\" -couch_log level debug -kernel inet_dist_listen_min 9100 -kernel inet_dist_listen_max 9200 -proto_dist inet_tls -ssl_dist_opt server_certfile /home/couchdb-certs/node${i}_crt -ssl_dist_opt server_keyfile /home/couchdb-certs/node${i}_key -ssl_dist_opt server_cacertfile /home/couchdb-certs/ca.crt\"
- name: NODENAME
value: \"couchdb-${i}.default.svc.cluster.local\"
volumeMounts:
- name: config-storage
mountPath: /opt/couchdb/etc/local.d
- name: couchdb-data
mountPath: /opt/couchdb/data
- name: couchdb-certs
mountPath: /home/couchdb-certs
volumes:
- name: couchdb-data
persistentVolumeClaim:
claimName: couchdb${i}-data
- name: couchdb-config
configMap:
name: couchdb-config-${i}
- name: config-storage
emptyDir: {}
- name: couchdb-certs
secret:
secretName: couchdb-certs"
done
# Generate Services
for ((i=0; i<num_compute_nodes; i++)); do
yaml_content+="
- apiVersion: v1
kind: Service
metadata:
name: couchdb-${i}
spec:
clusterIP: None
selector:
app: couchdb-${i}
ports:
- name: http
port: 5984
targetPort: 5984
- name: https
port: 6984
targetPort: 6984"
done
for ((i=0; i<num_compute_nodes; i++)); do
# Generate ConfigMap for CouchDB configuration
yaml_content+="
- apiVersion: v1
kind: ConfigMap
metadata:
name: couchdb-config-${i}
data:
local.ini: |
[couchdb]
uuid = couch
[chttpd]
port = 5984
bind_address = 0.0.0.0
enable_cors = true
[cors]
origins = *
credentials = true
methods = GET, PUT, POST, HEAD, DELETE
headers = accept, authorization, content-type, origin, referer
[cluster]
enable_ssl = true
[log]
level = debug
[ssl]
enable = true
port = 6984
cert_file = /home/couchdb-certs/node${i}_crt
key_file = /home/couchdb-certs/node${i}_key
cacert_file = /home/couchdb-certs/ca.crt
certfile = /home/couchdb-certs/node${i}_crt
keyfile = /home/couchdb-certs/node${i}_key
cacertfile = /home/couchdb-certs/ca.crt
verify_ssl = true
tls_versions = [tlsv1, 'tlsv1.1', 'tlsv1.2', 'tlsv1.3']
ssl_log_level = debug"
done
# Generate CouchDB Cluster Setup Job
yaml_content+="
- apiVersion: batch/v1
kind: Job
metadata:
name: couchdb-setup
spec:
template:
metadata:
name: couchdb-setup
spec:
restartPolicy: OnFailure
containers:
- name: couchdb-setup
image: curlimages/curl:latest
command:
- /bin/sh
args:
- -c
- |
echo \"Starting CouchDB cluster setup\" &&
for i in \$(seq 0 $((num_compute_nodes-1))); do
echo \"https://\${COUCHDB_USER}:\${COUCHDB_PASSWORD}@couchdb-\${i}.default.svc.cluster.local:6984\"
until curl --cacert /certs/ca.crt --cert /certs/node\${i}_crt --key /certs/node\${i}_key -s \"https://\${COUCHDB_USER}:\${COUCHDB_PASSWORD}@couchdb-\${i}.default.svc.cluster.local:6984\" > /dev/null; do
echo \"Waiting for CouchDB on couchdb-\${i} to be ready...\"
sleep 5
done
echo \"CouchDB on couchdb-\${i} is ready\"
done &&
echo \"Adding nodes to the cluster\" &&
for num in \$(seq 1 $((num_compute_nodes-1))); do
response=\$(curl --cacert /certs/ca.crt --cert /certs/node0_crt --key /certs/node0_key -X POST -H 'Content-Type: application/json' \"https://\${COUCHDB_USER}:\${COUCHDB_PASSWORD}@couchdb-0.default.svc.cluster.local:6984/_cluster_setup\" -d \"{\\\"action\\\": \\\"enable_cluster\\\", \\\"bind_address\\\":\\\"0.0.0.0\\\", \\\"username\\\": \\\"\${COUCHDB_USER}\\\", \\\"password\\\":\\\"\${COUCHDB_PASSWORD}\\\", \\\"port\\\": 6984, \\\"node_count\\\": \\\"$num_compute_nodes\\\", \\\"remote_node\\\": \\\"couchdb-\${num}.default.svc.cluster.local\\\", \\\"remote_current_user\\\": \\\"\${COUCHDB_USER}\\\", \\\"remote_current_password\\\": \\\"\${COUCHDB_PASSWORD}\\\" }\")
echo \"Enable cluster on couchdb-\${num} response: \${response}\"
response=\$(curl -s -X POST -H 'Content-Type: application/json' \"http://\${COUCHDB_USER}:\${COUCHDB_PASSWORD}@couchdb-0.default.svc.cluster.local:5984/_cluster_setup\" -d \"{\\\"action\\\": \\\"add_node\\\", \\\"host\\\":\\\"couchdb-\${num}.default.svc.cluster.local\\\", \\\"port\\\": 5984, \\\"username\\\": \\\"\${COUCHDB_USER}\\\", \\\"password\\\":\\\"\${COUCHDB_PASSWORD}\\\"}\")
echo \"Adding node couchdb-\${num} response: \${response}\"
done &&
echo \"Finishing cluster setup\" &&
response=\$(curl --cacert /certs/ca.crt --cert /certs/node0_crt --key /certs/node0_key -s -X POST -H 'Content-Type: application/json' \"https://\${COUCHDB_USER}:\${COUCHDB_PASSWORD}@couchdb-0.default.svc.cluster.local:6984/_cluster_setup\" -d \"{\\\"action\\\": \\\"finish_cluster\\\"}\") &&
echo \"Finish cluster response: \${response}\" &&
echo \"Checking cluster membership\" &&
membership=\$(curl --cacert /certs/ca.crt --cert /certs/node0_crt --key /certs/node0_key -s -X GET \"https://\${COUCHDB_USER}:\${COUCHDB_PASSWORD}@couchdb-0.default.svc.cluster.local:6984/_membership\") &&
echo \"Cluster membership: \${membership}\" &&
echo \"Creating \${RESOURCE_REGISTRY_DB} and \${TASK_DATA_DB} database on all nodes\" &&
response=\$(curl --cacert /certs/ca.crt --cert /certs/node0_crt --key /certs/node0_key -s -X PUT \"https://\${COUCHDB_USER}:\${COUCHDB_PASSWORD}@couchdb-0.default.svc.cluster.local:6984/\${RESOURCE_REGISTRY_DB}\") &&
echo \"Creating \${RESOURCE_REGISTRY_DB} on couchdb-0 response: \${response}\" &&
response=\$(curl --cacert /certs/ca.crt --cert /certs/node0_crt --key /certs/node0_key -s -X PUT \"https://\${COUCHDB_USER}:\${COUCHDB_PASSWORD}@couchdb-0.default.svc.cluster.local:6984/\${TASK_DATA_DB}\") &&
echo \"Creating \${TASK_DATA_DB} on couchdb-0 response: \${response}\" &&
echo \"Waiting for \${RESOURCE_REGISTRY_DB} & \${TASK_DATA_DB} to be available on all nodes\" &&
for db in \${RESOURCE_REGISTRY_DB} \${TASK_DATA_DB}; do
for i in \$(seq 0 $((num_compute_nodes-1))); do
until curl --cacert /certs/ca.crt --cert /certs/node\${i}_crt --key /certs/node\${i}_key -s \"https://\${COUCHDB_USER}:\${COUCHDB_PASSWORD}@couchdb-\${i}.default.svc.cluster.local:6984/\${db}\" | grep -q \"\${db}\"; do
echo \"Waiting for \${db} on couchdb-\${i}...\"
sleep 5
done
echo \"\${db} is available on couchdb-\${i}\"
done
done &&
echo \"CouchDB cluster setup completed and \${RESOURCE_REGISTRY_DB}, \${TASK_DATA_DB} databases are available on all nodes\"
env:
- name: RESOURCE_REGISTRY_DB
value: \"resource_registry\"
- name: TASK_DATA_DB
value: \"task_data\"
- name: COUCHDB_USER
valueFrom:
secretKeyRef:
name: couchdb-secrets
key: COUCHDB_USER
- name: COUCHDB_PASSWORD
valueFrom:
secretKeyRef:
name: couchdb-secrets
key: COUCHDB_PASSWORD
volumeMounts:
- name: couchdb-certs
mountPath: /certs
volumes:
- name: couchdb-certs
secret:
secretName: couchdb-certs"
echo "$yaml_content"
}
# Function to generate blockchain network deployment YAML
generate_blockchain_network_yaml() {
local num_compute_nodes=$1
local num_iot_nodes=$2
local yaml_content="apiVersion: v1
kind: List
items:"
# Generate compute Node Deployments and Services
for ((i=0; i<num_compute_nodes; i++)); do
local hostname="compute-node-$((i+1))"
local deployment_name="pbft-$i"
local service_name="sawtooth-$i"
yaml_content+="
# --------------------------=== $hostname ===--------------------------
- apiVersion: apps/v1
kind: Deployment
metadata:
name: $deployment_name
spec:
selector:
matchLabels:
name: $deployment_name
replicas: 1
template:
metadata:
labels:
name: $deployment_name
spec:
nodeSelector:
kubernetes.io/hostname: $hostname
volumes:
- name: proc
hostPath:
path: /proc
- name: sys
hostPath:
path: /sys
- name: couchdb-certs
secret:
secretName: couchdb-certs
initContainers:
- name: wait-for-registry
image: busybox
command: [ 'sh', '-c', 'until nc -z sawtooth-registry 5000; do echo waiting for sawtooth-registry; sleep 2; done;' ]
- name: wait-for-couchdb-setup
image: curlimages/curl:latest
command:
- 'sh'
- '-c'
- |
for db in \${RESOURCE_REGISTRY_DB} \${TASK_DATA_DB}; do
for i in \$(seq 0 $((num_compute_nodes-1))); do
until curl --cacert /certs/ca.crt --cert /certs/node\${i}_crt --key /certs/node\${i}_key -s \"https://\${COUCHDB_USER}:\${COUCHDB_PASSWORD}@couchdb-\${i}.default.svc.cluster.local:6984/\${db}\" | grep -q \"\${db}\"; do
echo \"Waiting for \${db} on couchdb-\${i}...\"
sleep 5
done
echo \"\${db} is available on couchdb-\${i}\"
done
done &&
echo \"CouchDB cluster setup completed and \${RESOURCE_REGISTRY_DB} & \${TASK_DATA_DB} is available on all nodes\"
env:
- name: RESOURCE_REGISTRY_DB
value: \"resource_registry\"
- name: TASK_DATA_DB
value: \"task_data\"
- name: COUCHDB_USER
valueFrom:
secretKeyRef:
name: couchdb-secrets
key: COUCHDB_USER
- name: COUCHDB_PASSWORD
valueFrom:
secretKeyRef:
name: couchdb-secrets
key: COUCHDB_PASSWORD
volumeMounts:
- name: couchdb-certs
mountPath: /certs
containers:
- name: peer-registry-tp
image: murtazahr/peer-registry-tp:latest
env:
- name: MAX_UPDATES_PER_NODE
value: \"100\"
- name: VALIDATOR_URL
value: \"tcp://$service_name:4004\"
- name: docker-image-tp
image: murtazahr/docker-image-tp:latest
env:
- name: VALIDATOR_URL
value: \"tcp://$service_name:4004\"
- name: dependency-management-tp
image: murtazahr/dependency-management-tp:latest
env:
- name: VALIDATOR_URL
value: \"tcp://$service_name:4004\"
- name: schedule-status-update-tp
image: murtazahr/schedule-status-update-tp:latest
env:
- name: VALIDATOR_URL
value: \"tcp://$service_name:4004\"
- name: iot-data-tp
image: murtazahr/iot-data-tp:latest
env:
- name: VALIDATOR_URL
value: \"tcp://$service_name:4004\"
- name: COUCHDB_HOST
value: \"couchdb-$i.default.svc.cluster.local:6984\"
- name: COUCHDB_USER
valueFrom:
secretKeyRef:
name: couchdb-secrets
key: COUCHDB_USER
- name: COUCHDB_PASSWORD
valueFrom:
secretKeyRef:
name: couchdb-secrets
key: COUCHDB_PASSWORD
- name: COUCHDB_SSL_CERT
valueFrom:
secretKeyRef:
name: couchdb-certs
key: node${i}_crt
- name: COUCHDB_SSL_KEY
valueFrom:
secretKeyRef:
name: couchdb-certs
key: node${i}_key
- name: COUCHDB_SSL_CA
valueFrom:
secretKeyRef:
name: couchdb-certs
key: ca.crt
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: redis-password
key: password
- name: REDIS_SSL_CERT
valueFrom:
secretKeyRef:
name: redis-certificates
key: redis.crt
- name: REDIS_SSL_KEY
valueFrom:
secretKeyRef:
name: redis-certificates
key: redis.key
- name: REDIS_SSL_CA
valueFrom:
secretKeyRef:
name: redis-certificates
key: ca.crt
- name: scheduling-request-tp
image: murtazahr/scheduling-request-tp:latest
env:
- name: VALIDATOR_URL
value: \"tcp://$service_name:4004\"
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: redis-password
key: password
- name: REDIS_SSL_CERT
valueFrom:
secretKeyRef:
name: redis-certificates
key: redis.crt
- name: REDIS_SSL_KEY
valueFrom:
secretKeyRef:
name: redis-certificates
key: redis.key
- name: REDIS_SSL_CA
valueFrom:
secretKeyRef:
name: redis-certificates
key: ca.crt
- name: schedule-confirmation-tp
image: murtazahr/schedule-confirmation-tp:latest
env:
- name: VALIDATOR_URL
value: \"tcp://$service_name:4004\"
- name: sawtooth-pbft-engine
image: hyperledger/sawtooth-pbft-engine:chime
command:
- bash
args:
- -c
- \"pbft-engine -vv --connect tcp://\$HOSTNAME:5050\"
- name: sawtooth-settings-tp
image: hyperledger/sawtooth-settings-tp:chime
command:
- bash
args:
- -c
- \"settings-tp -vv -C tcp://\$HOSTNAME:4004\"
- name: sawtooth-rest-api
image: hyperledger/sawtooth-rest-api:chime
ports:
- name: api
containerPort: 8008
command:
- bash
args:
- -c
- \"sawtooth-rest-api -vv -C tcp://\$HOSTNAME:4004 -B 0.0.0.0:8008\"
readinessProbe:
httpGet:
path: /status
port: 8008
initialDelaySeconds: 15 # Time to wait after container starts before checking
periodSeconds: 30 # How often to check
timeoutSeconds: 30 # How long to wait for a response
failureThreshold: 3 # How many failures before marking unready
successThreshold: 1 # How many successes before marking ready
- name: sawtooth-shell
image: hyperledger/sawtooth-shell:chime
command:
- bash
args:
- -c
- \"sawtooth keygen && tail -f /dev/null\"
- name: compute-node
resources: {}
image: murtazahr/compute-node:latest
securityContext:
privileged: true
volumeMounts:
- name: proc
mountPath: /host/proc
readOnly: true
- name: sys
mountPath: /host/sys
readOnly: true
env:
- name: REGISTRY_URL
value: \"sawtooth-registry:5000\"
- name: VALIDATOR_URL
value: \"tcp://$service_name:4004\"
- name: NODE_ID
value: \"sawtooth-compute-node-$i\"
- name: COUCHDB_HOST
value: \"couchdb-$i.default.svc.cluster.local:6984\"
- name: RESOURCE_UPDATE_INTERVAL
value: \"30\"
- name: RESOURCE_UPDATE_BATCH_SIZE
value: \"500\"
- name: COUCHDB_USER
valueFrom:
secretKeyRef:
name: couchdb-secrets
key: COUCHDB_USER
- name: COUCHDB_PASSWORD
valueFrom:
secretKeyRef:
name: couchdb-secrets
key: COUCHDB_PASSWORD
- name: COUCHDB_SSL_CERT
valueFrom:
secretKeyRef:
name: couchdb-certs
key: node${i}_crt
- name: COUCHDB_SSL_KEY
valueFrom:
secretKeyRef:
name: couchdb-certs
key: node${i}_key
- name: COUCHDB_SSL_CA
valueFrom:
secretKeyRef:
name: couchdb-certs
key: ca.crt
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: redis-password
key: password
- name: REDIS_SSL_CERT
valueFrom:
secretKeyRef:
name: redis-certificates
key: redis.crt
- name: REDIS_SSL_KEY
valueFrom:
secretKeyRef:
name: redis-certificates
key: redis.key
- name: REDIS_SSL_CA
valueFrom:
secretKeyRef:
name: redis-certificates
key: ca.crt
- name: IS_NEW_ADDITION
value: \"false\"
- name: sawtooth-validator
image: hyperledger/sawtooth-validator:chime
ports:
- name: tp
containerPort: 4004
- name: consensus
containerPort: 5050
- name: validators
containerPort: 8800"
if [ "$i" -eq 0 ]; then
local pbft_members=$(for ((j=0; j<num_compute_nodes; j++)); do
echo -n "\\\"\$pbft${j}pub\\\""
if [ $j -lt $((num_compute_nodes-1)) ]; then
echo -n ","
fi
done)
yaml_content+="
envFrom:
- configMapRef:
name: keys-config
env:
- name: NETWORK_PRIVATE_KEY
valueFrom:
secretKeyRef:
name: network-key-secret
key: network_private_key
- name: NETWORK_PUBLIC_KEY
valueFrom:
secretKeyRef:
name: network-key-secret
key: network_public_key
command:
- bash
args:
- -c
- |
if [ ! -e /etc/sawtooth/keys/validator.priv ]; then
echo \$pbft0priv > /etc/sawtooth/keys/validator.priv
echo \$pbft0pub > /etc/sawtooth/keys/validator.pub
fi &&
if [ ! -e /root/.sawtooth/keys/my_key.priv ]; then
sawtooth keygen my_key
fi &&
if [ ! -e config-genesis.batch ]; then
sawset genesis -k /root/.sawtooth/keys/my_key.priv -o config-genesis.batch
fi &&
sleep 30 &&
echo sawtooth.consensus.pbft.members=[\"${pbft_members}\"] &&
if [ ! -e config.batch ]; then
sawset proposal create -k /root/.sawtooth/keys/my_key.priv sawtooth.consensus.algorithm.name=pbft sawtooth.consensus.algorithm.version=1.0 sawtooth.consensus.pbft.members=[\"${pbft_members}\"] sawtooth.publisher.max_batches_per_block=100 sawtooth.validator.state_pruning_enabled=false sawtooth.validator.max_database_size_mb=15000 sawtooth.validation.block_validation_rules=NofX:1,block_info sawtooth.consensus.pbft.block_publishing_delay=2000 sawtooth.validator.transaction_queue_max_length=10000 -o config.batch
fi && if [ ! -e /var/lib/sawtooth/genesis.batch ]; then
sawadm genesis config-genesis.batch config.batch
fi &&
echo \"network_public_key = '\${NETWORK_PUBLIC_KEY}'\" > /etc/sawtooth/validator.toml &&
echo \"network_private_key = '\${NETWORK_PRIVATE_KEY}'\" >> /etc/sawtooth/validator.toml &&
chown root:sawtooth /etc/sawtooth/validator.toml &&
chmod 640 /etc/sawtooth/validator.toml &&
sawtooth-validator -vv --endpoint tcp://\$SAWTOOTH_0_SERVICE_HOST:8800 --bind component:tcp://eth0:4004 --bind consensus:tcp://eth0:5050 --bind network:tcp://eth0:8800 --network-auth trust --scheduler parallel --peering static --maximum-peer-connectivity 10000"
else
yaml_content+="
env:
- name: pbft${i}priv
valueFrom:
configMapKeyRef:
name: keys-config
key: pbft${i}priv
- name: pbft${i}pub
valueFrom:
configMapKeyRef:
name: keys-config
key: pbft${i}pub
- name: NETWORK_PRIVATE_KEY
valueFrom:
secretKeyRef:
name: network-key-secret
key: network_private_key
- name: NETWORK_PUBLIC_KEY
valueFrom:
secretKeyRef:
name: network-key-secret
key: network_public_key
command:
- bash
args:
- -c
- |
if [ ! -e /etc/sawtooth/keys/validator.priv ]; then
echo \$pbft${i}priv > /etc/sawtooth/keys/validator.priv
echo \$pbft${i}pub > /etc/sawtooth/keys/validator.pub
fi &&
sawtooth keygen my_key &&
echo \"network_public_key = '\${NETWORK_PUBLIC_KEY}'\" > /etc/sawtooth/validator.toml &&
echo \"network_private_key = '\${NETWORK_PRIVATE_KEY}'\" >> /etc/sawtooth/validator.toml &&
chown root:sawtooth /etc/sawtooth/validator.toml &&
chmod 640 /etc/sawtooth/validator.toml &&
sawtooth-validator -vv --endpoint tcp://\$SAWTOOTH_${i}_SERVICE_HOST:8800 --bind component:tcp://eth0:4004 --bind consensus:tcp://eth0:5050 --bind network:tcp://eth0:8800 --network-auth trust --scheduler parallel --peering static --maximum-peer-connectivity 10000 $(for ((j=0; j<i; j++)); do echo -n "--peers tcp://\$SAWTOOTH_${j}_SERVICE_HOST:8800 "; done)"
fi
yaml_content+="
- apiVersion: v1
kind: Service
metadata:
name: $service_name
spec:
type: ClusterIP
selector:
name: $deployment_name
ports:
- name: \"4004\"
protocol: TCP
port: 4004
targetPort: 4004
- name: \"5050\"
protocol: TCP
port: 5050
targetPort: 5050
- name: \"8008\"
protocol: TCP
port: 8008
targetPort: 8008
- name: \"8080\"
protocol: TCP
port: 8080
targetPort: 8080
- name: \"5000\"
protocol: TCP
port: 5000
targetPort: 5000
- name: \"8800\"
protocol: TCP
port: 8800
targetPort: 8800"
done
# Generate IoT Node Deployments
for ((i=0; i<num_iot_nodes; i++)); do
local hostname="iot-node-$((i+1))"
local deployment_name="iot-$i"
local service_name="iot-$i"
yaml_content+="
# -------------------------=== iot-node-$((i+1)) ===------------------
- apiVersion: apps/v1
kind: Deployment
metadata:
name: $deployment_name
spec:
selector:
matchLabels:
name: $deployment_name
replicas: 1
template:
metadata:
labels:
name: $deployment_name
spec:
nodeSelector:
kubernetes.io/hostname: iot-node-$((i+1))
containers:
- name: iot-node
image: murtazahr/iot-node:latest
env:
- name: VALIDATOR_URLS
value: \"$(for ((j=0; j<num_compute_nodes; j++)); do echo -n "tcp://sawtooth-$j:4004"; if [ $j -lt $((num_compute_nodes-1)) ]; then echo -n ,; fi; done)\"
- name: IOT_URL
value: \"tcp://$service_name\"
- apiVersion: v1
kind: Service
metadata:
name: $service_name
spec:
type: ClusterIP
selector:
name: $deployment_name
ports:
- name: \"5555\"
protocol: TCP
port: 5555
targetPort: 5555"
done
# Add Client Console Deployment
yaml_content+="
# -------------------------=== client-console ===------------------
- apiVersion: apps/v1
kind: Deployment
metadata:
name: network-management-console
spec:
selector:
matchLabels:
name: network-management-console
replicas: 1
template:
metadata:
labels:
name: network-management-console
spec:
nodeSelector:
kubernetes.io/hostname: client-console
containers:
- name: application-deployment-client
image: murtazahr/docker-image-client:latest
securityContext:
privileged: true
env:
- name: REGISTRY_URL
value: \"sawtooth-registry:5000\"
- name: VALIDATOR_URL
value: \"tcp://sawtooth-0:4004\"
- name: workflow-creation-client
image: murtazahr/workflow-creation-client:latest
env:
- name: VALIDATOR_URL
value: \"tcp://sawtooth-0:4004\"
- name: scheduling-client
image: murtazahr/scheduling-client:latest
env:
- name: VALIDATOR_URL
value: \"tcp://sawtooth-0:4004\""
echo "$yaml_content"
}
# Main script starts here
echo "Enter the number of compute nodes:"
read num_compute_nodes
echo "Enter the number of IoT nodes:"
read num_iot_nodes
# Part 1: Verify inputs and check node existence
if [ "$num_compute_nodes" -lt 3 ]; then
echo "Error: The number of compute nodes must be at least 3."
exit 1
fi
echo "Checking for compute nodes..."
for ((i=1; i<=num_compute_nodes; i++)); do
if ! check_node_exists "compute-node-$i"; then
echo "Error: compute-node-$i does not exist in the cluster."
exit 1
fi
done
echo "Checking for IoT nodes..."
for ((i=1; i<=num_iot_nodes; i++)); do
if ! check_node_exists "iot-node-$i"; then
echo "Error: iot-node-$i does not exist in the cluster."
exit 1
fi
done
echo "All required nodes are present in the cluster."
# Part 1: Generate SSL/TLS certificates and Build sample application images
generate_ssl_certificates "$num_compute_nodes"
generate_network_key_zmq
WORK_DIR=$(pwd)
TEST_APP_DIR=$(pwd)/sample-apps
# Building docker image for test docker applications
cd "$TEST_APP_DIR/cold-chain-monitoring/task1_process_sensor_data" || exit
docker build -t process-sensor-data:latest -f Dockerfile .
cd "$TEST_APP_DIR/cold-chain-monitoring/task2_detect_anomalies" || exit
docker build -t anomaly-detection:latest -f Dockerfile .
cd "$TEST_APP_DIR/cold-chain-monitoring/task3_generate_alerts" || exit
docker build -t generate-alerts:latest -f Dockerfile .
# Make sure user is in the correct working directory
cd "$WORK_DIR" || exit
# Export test docker application
docker save -o auto-docker-deployment/docker-image-client/process-sensor-data.tar process-sensor-data
docker save -o auto-docker-deployment/docker-image-client/anomaly-detection.tar anomaly-detection
docker save -o auto-docker-deployment/docker-image-client/generate-alerts.tar generate-alerts
# Part 2: Create redis cluster
mkdir -p kubernetes-manifests/generated
/bin/bash ./redis-setup.sh "$num_compute_nodes"
# Part 3: Generate YAML file for config and secrets
# Create the PBFT key generation job YAML
cat << EOF > kubernetes-manifests/generated/pbft-key-generation-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pbft-keys
spec:
template:
metadata:
labels:
job-name: pbft-keys
spec:
containers:
- name: pbft-keys-generator
image: hyperledger/sawtooth-shell
command:
- bash
args:
- -c
- "for i in {0..$(($num_compute_nodes-1))}; do sawadm keygen -q pbft\${i}; done && cd /etc/sawtooth/keys/ && grep '' * | sed 's/\\\\.//' | sed 's/:/:\ /'"
restartPolicy: Never
backoffLimit: 4
EOF
echo "Generated PBFT key generation job YAML has been saved to kubernetes-manifests/generated/pbft-key-generation-job.yaml"
# Apply the job YAML
kubectl apply -f kubernetes-manifests/generated/pbft-key-generation-job.yaml
# Wait for the job to complete
wait_for_job pbft-keys
# Get the pod name
pod_name=$(kubectl get pods --selector=job-name=pbft-keys --output=jsonpath='{.items[*].metadata.name}')
# Fetch the keys from the pod logs
generated_keys=$(kubectl logs "$pod_name")
# Delete the job YAML
kubectl delete -f kubernetes-manifests/generated/pbft-key-generation-job.yaml
echo "PBFT key generation job has been deleted."
# Process the generated keys to add proper indentation
indented_keys=$(echo "$generated_keys" | sed 's/^/ /')
# Create the config and secrets YAML
cat << EOF > kubernetes-manifests/generated/config-and-secrets.yaml