-
Notifications
You must be signed in to change notification settings - Fork 1
/
copy-router-and-networks.sh
executable file
·753 lines (565 loc) · 35.7 KB
/
copy-router-and-networks.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
#!/bin/bash
# TODO add external ip assignment to copied sprut routers
echo "
██████╗ ██████╗ ██╗ ██╗████████╗███████╗██████╗
██╔══██╗██╔═══██╗██║ ██║╚══██╔══╝██╔════╝██╔══██╗
██████╔╝██║ ██║██║ ██║ ██║ █████╗ ██████╔╝
██╔══██╗██║ ██║██║ ██║ ██║ ██╔══╝ ██╔══██╗
██║ ██║╚██████╔╝╚██████╔╝ ██║ ███████╗██║ ██║
╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
███╗ ██╗███████╗████████╗██╗ ██╗ ██████╗ ██████╗ ██╗ ██╗
████╗ ██║██╔════╝╚══██╔══╝██║ ██║██╔═══██╗██╔══██╗██║ ██╔╝
██╔██╗ ██║█████╗ ██║ ██║ █╗ ██║██║ ██║██████╔╝█████╔╝
██║╚██╗██║██╔══╝ ██║ ██║███╗██║██║ ██║██╔══██╗██╔═██╗
██║ ╚████║███████╗ ██║ ╚███╔███╔╝╚██████╔╝██║ ██║██║ ██╗
╚═╝ ╚═══╝╚══════╝ ╚═╝ ╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝
███████╗██╗ ██╗██████╗ ███╗ ██╗███████╗████████╗██╗ ██╗ ██████╗ ██████╗ ██╗ ██╗
██╔════╝██║ ██║██╔══██╗████╗ ██║██╔════╝╚══██╔══╝██║ ██║██╔═══██╗██╔══██╗██║ ██╔╝
███████╗██║ ██║██████╔╝██╔██╗ ██║█████╗ ██║ ██║ █╗ ██║██║ ██║██████╔╝█████╔╝
╚════██║██║ ██║██╔══██╗██║╚██╗██║██╔══╝ ██║ ██║███╗██║██║ ██║██╔══██╗██╔═██╗
███████║╚██████╔╝██████╔╝██║ ╚████║███████╗ ██║ ╚███╔███╔╝╚██████╔╝██║ ██║██║ ██╗
╚══════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝╚══════╝ ╚═╝ ╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝
███╗ ███╗██╗ ██████╗ ██████╗ █████╗ ████████╗██╗ ██████╗ ███╗ ██╗ ███████╗ ██████╗██████╗ ██╗██████╗ ████████╗
████╗ ████║██║██╔════╝ ██╔══██╗██╔══██╗╚══██╔══╝██║██╔═══██╗████╗ ██║ ██╔════╝██╔════╝██╔══██╗██║██╔══██╗╚══██╔══╝
██╔████╔██║██║██║ ███╗██████╔╝███████║ ██║ ██║██║ ██║██╔██╗ ██║ ███████╗██║ ██████╔╝██║██████╔╝ ██║
██║╚██╔╝██║██║██║ ██║██╔══██╗██╔══██║ ██║ ██║██║ ██║██║╚██╗██║ ╚════██║██║ ██╔══██╗██║██╔═══╝ ██║
██║ ╚═╝ ██║██║╚██████╔╝██║ ██║██║ ██║ ██║ ██║╚██████╔╝██║ ╚████║ ███████║╚██████╗██║ ██║██║██║ ██║
╚═╝ ╚═╝╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚══════╝ ╚═════╝╚═╝ ╚═╝╚═╝╚═╝ ╚═╝
Copies routers, networks, subnets from neutron to sprut.
All resources will be created with -sprut postfix.
Input file format:
neutron router1 UUID,<std|adv|transit>
neutron router2 UUID,<std|adv|transit>
...
"
echo "{====================STAGE 1: Collecting information====================}"
echo ""
echo ""
#!/bin/bash
# STEP 1: Reading the config file and building map
if [ -z "$1" ]; then
echo "Error: No input file provided."
exit 1
fi
start_time=$(date +%s)
echo "Executing STEP 1: Reading config file $1"
declare -A neutron_router_to_transform_format
while IFS=, read -r neutron_router transform_format || [ -n "$neutron_router" ]
do
if [[ "$transform_format" != "std" && "$transform_format" != "adv" && "$transform_format" != "transit" ]]; then
echo "Error: transform format $transform_format is not in <std|adv|transit>"
exit 1
fi
neutron_router_to_transform_format["$neutron_router"]="$transform_format"
done < "$1"
echo "Reading values from config:"
echo ""
echo "_____________________________________________________"
echo "|Neutron router |Advanced Router|"
echo " ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅"
for key in "${!neutron_router_to_transform_format[@]}"; do
echo "|$key|${neutron_router_to_transform_format[$key]} |"
done
echo " ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ "
echo ""
echo "STEP 1 complete (config read)"
echo "**********************************************************************************"
# STEP 2: Checking if Neutron routers exist in OpenStack tenant
neutron_router_list=$(openstack router list -f json)
for neutron_router_id in "${!neutron_router_to_transform_format[@]}"; do
if ! echo "$neutron_router_list" | jq -e --arg id "$neutron_router_id" '.[] | select(.ID == $id)' > /dev/null; then
echo "Error: Neutron router ID $neutron_router_id not found in OpenStack tenant."
exit 1
else
echo "Neutron router ID $neutron_router_id exists in OpenStack tenant."
fi
done
echo "STEP 2 complete (Neutron routers checked)"
echo "**********************************************************************************"
# STEP 3: Collecting info about Openstack routers
echo "Executing STEP 3: Collecting info about Openstack routers"
declare -A neutron_router_ID_to_json_details
for neutron_router_id in "${!neutron_router_to_transform_format[@]}"; do
neutron_router_show_cmd="openstack router show $neutron_router_id -f json"
echo "Running command: $neutron_router_show_cmd"
neutron_router_show_details=$($neutron_router_show_cmd)
neutron_router_ID_to_json_details["$neutron_router_id"]=$neutron_router_show_details
done
echo "Stored neutron routers: "
for neutron_router_id in "${!neutron_router_ID_to_json_details[@]}"; do
echo "Router ID: $neutron_router_id"
echo "Details: ${neutron_router_ID_to_json_details[$neutron_router_id]}"
echo ""
done
echo "STEP 3 complete: Info about neutron routers stored"
echo "**********************************************************************************"
# STEP 4: Collecting info about Openstack subnets per router
echo "Executing STEP 4: Collecting info about Openstack subnets per router"
declare -A neutron_router_id_to_subnet_ids
declare -A neutron_subnet_id_to_json_details
declare -A neutron_network_id_to_json_details
echo ""
echo "Listing subnets per router and showing details for each subnet:"
for neutron_router_id in "${!neutron_router_ID_to_json_details[@]}"; do
subnet_ids=$(echo "${neutron_router_ID_to_json_details[$neutron_router_id]}" | jq -r '.interfaces_info[].subnet_id')
neutron_router_id_to_subnet_ids["$neutron_router_id"]="$subnet_ids"
echo "Router ID: $neutron_router_id"
echo "Subnet IDs:"
echo "${neutron_router_id_to_subnet_ids[$neutron_router_id]}"
echo ""
for subnet_id in ${neutron_router_id_to_subnet_ids[$neutron_router_id]}; do
echo "Processing subnet ID: $subnet_id"
neutron_subnet_show_cmd="openstack subnet show $subnet_id -f json"
echo "Running command: $neutron_subnet_show_cmd"
neutron_subnet_show_details=$($neutron_subnet_show_cmd)
neutron_subnet_id_to_json_details["$subnet_id"]="$neutron_subnet_show_details"
echo "$subnet_id Subnet details:"
echo "${neutron_subnet_id_to_json_details["$subnet_id"]}"
neutron_network_id=$(echo $neutron_subnet_show_details | jq -r '.network_id')
neutron_network_id_to_json_details["$neutron_network_id"]=" "
done
done
echo ""
echo "STEP 4 Complete: Info about Openstack subnets per router collected"
# STEP 5: Collecting info about Openstack networks
echo "Executing STEP 5: Collecting info about Openstack networks"
echo ""
for network_id in "${!neutron_network_id_to_json_details[@]}"; do
echo "Processing network ID: $network_id"
neutron_network_show_cmd="openstack network show $network_id -f json"
echo "Running command: $neutron_network_show_cmd"
neutron_network_show_details=$($neutron_network_show_cmd)
neutron_network_id_to_json_details["$network_id"]=$neutron_network_show_details
echo "Network details: "
echo "${neutron_network_id_to_json_details["$network_id"]}"
done
echo "STEP 5 Complete: Info about Openstack networks collected"
echo "{====================STAGE 1: COMPLETE====================}"
echo ""
# Stage 2: Collecting Existing Sprut Objects
echo "{====================STAGE 2: Collecting Existing Sprut Objects====================}"
sprut_api_base="https://infra.mail.ru:9696/v2.0"
token=$(openstack token issue -c id -f value)
declare -A sprut_router_ports
declare -A advanced_router_ports
# Function to collect Sprut objects and store them in dictionaries
collect_sprut_objects() {
echo "Collecting Standard routers from Sprut..."
sprut_standard_routers=$(curl -s -X GET "${sprut_api_base}/routers" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT")
echo "Standard routers collected:"
echo "$sprut_standard_routers" | jq .
echo ""
echo "Collecting Advanced routers from Sprut..."
advanced_routers=$(curl -s -X GET "${sprut_api_base}/direct_connect/dc_routers" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT")
echo "Advanced routers collected:"
echo "$advanced_routers" | jq .
echo ""
echo "Collecting Networks from Sprut..."
sprut_networks=$(curl -s -X GET "${sprut_api_base}/networks" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT")
echo "Networks collected:"
echo "$sprut_networks" | jq .
echo ""
echo "Collecting Subnetworks from Sprut..."
sprut_subnetworks=$(curl -s -X GET "${sprut_api_base}/subnets" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT")
echo "Subnetworks collected:"
echo "$sprut_subnetworks" | jq .
echo ""
# Extract router IDs
sprut_router_ids=$(echo "$sprut_standard_routers" | jq -r '.routers[].id')
for router_id in $sprut_router_ids; do
echo "Collecting ports for router ID: $router_id"
# Fetch the ports associated with this router
ports=$(curl -s -X GET "${sprut_api_base}/ports?device_id=${router_id}" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT")
# Store the result in the associative array
sprut_router_ports["$router_id"]="$ports"
echo "Ports for router ID $router_id:"
echo "${sprut_router_ports["$router_id"]}" | jq .
echo ""
done
advanced_router_ids=$(echo "$advanced_routers" | jq -r '.dc_routers[].id')
for router_id in $advanced_router_ids; do
echo "Collecting ports for advanced router ID: $router_id"
# Fetch the ports associated with this router
ports=$(curl -s -X GET "${sprut_api_base}/direct_connect/dc_interfaces?device_id=${router_id}" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT")
# Store the result in the associative array
advanced_router_ports["$router_id"]="$ports"
echo "Ports for advanced router ID $router_id"
echo "${advanced_router_ports["$router_id"]}"
echo ""
done
}
# Execute the function
collect_sprut_objects
echo "STAGE 2 complete: Sprut objects collected"
echo "**********************************************************************************"
# Stage 3: Comparing and Creating Missing Objects in Sprut
echo "{====================STAGE 3: Comparing and Creating Missing Objects in Sprut====================}"
print_map_as_table() {
local -n map=$1
local title=$2
local column1=$3
local column2=$4
echo "=================================================================================="
echo "$title"
echo "=================================================================================="
printf "| %-36s | %-36s |\n" "$column1" "$column2"
echo " ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅ ̅"
for key in "${!map[@]}"; do
echo "$key" "${map[$key]}"
done
echo "=================================================================================="
echo ""
}
internet_network_uuid=$(openstack network show internet -f json | jq -r .id)
# Declare maps to store the correspondence between Neutron and Sprut IDs
declare -A neutron_to_sprut_router
compare_and_create_routers() {
for neutron_router_id in "${!neutron_router_ID_to_json_details[@]}"; do
neutron_router_transform_format="${neutron_router_to_transform_format[$neutron_router_id]}"
# check in which form copy should be created
if [ $neutron_router_transform_format != "std" ]
then
echo "neutron router $neutron_router_id with transform $neutron_router_transform_format will be created as advanced router in another loop"
continue
fi
neutron_router=$(echo "${neutron_router_ID_to_json_details[$neutron_router_id]}" | jq -r)
neutron_router_name=$(echo "$neutron_router" | jq -r '.name')
neutron_external_gateway_info=$(echo "$neutron_router" | jq '.external_gateway_info')
sprut_router_name=$neutron_router_name
sprut_router_name+="-sprut"
# basic case for std
sprut_router_id=$(echo "$sprut_standard_routers" | jq -r --arg name "$sprut_router_name" '.routers[] | select(.name == $name) | .id')
if [ -z "$sprut_router_id" ]; then
echo "Creating Router '$sprut_router_name' in Sprut"
if [ "$neutron_external_gateway_info" != "null" ]; then
request_body=$(jq -n --arg name "$sprut_router_name" --arg network_id "$internet_network_uuid" '{
router: {
name: $name,
external_gateway_info: {
network_id: $network_id
}
}
}')
else
request_body=$(jq -n --arg name "$sprut_router_name" '{
router: {
name: $name
}
}')
fi
curl_response=$(curl -s -X POST "${sprut_api_base}/routers" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT" \
-d "$request_body")
echo "$curl_response" | jq .
sprut_router_id=$(echo "$curl_response" | jq -r '.router.id')
neutron_to_sprut_router["$neutron_router_id"]="$sprut_router_id"
else
echo "Router '$sprut_router_name' already exists in Sprut"
neutron_to_sprut_router["$neutron_router_id"]="$sprut_router_id"
fi
done
}
compare_and_create_routers
print_map_as_table neutron_to_sprut_router "Neutron to Sprut Routers" "Neutron Router ID" "Sprut Router ID"
# COMPARE AND CREATE ADVANCED ROUTERS
declare -A neutron_to_advanced_router
compare_and_create_advanced_routers() {
for neutron_router_id in "${!neutron_router_ID_to_json_details[@]}"; do
neutron_router_transform_format="${neutron_router_to_transform_format[$neutron_router_id]}"
# check in which form copy should be created
if [ $neutron_router_transform_format != "adv" ]
then
echo "neutron router $neutron_router_id with transform $neutron_router_transform_format was created as standard router in previous loop"
continue
fi
neutron_router=$(echo "${neutron_router_ID_to_json_details[$neutron_router_id]}" | jq -r)
neutron_router_name=$(echo "$neutron_router" | jq -r '.name')
neutron_external_gateway_info=$(echo "$neutron_router" | jq '.external_gateway_info')
advanced_router_name=$neutron_router_name
advanced_router_name+="-adv-sprut"
advanced_router_id=$(echo "$advanced_routers" | jq -r --arg name "$advanced_router_name" '.dc_routers[] | select(.name == $name) | .id')
if [ -z "$advanced_router_id" ]; then
echo "Creating advanced router '$advanced_router_name'"
request_body=$(jq -n --arg name "$advanced_router_name" '{
dc_router: {
name: $name
}
}')
curl_response=$(curl -s -X POST "${sprut_api_base}/direct_connect/dc_routers" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT" \
-d "$request_body")
echo "$curl_response" | jq .
advanced_router_id=$(echo "$curl_response" | jq -r '.dc_router.id')
neutron_to_advanced_router["$neutron_router_id"]="$advanced_router_id"
external_ip_name=$advanced_router_name
external_ip_name+="-ext-interface"
if [ "$neutron_external_gateway_info" != "null" ]; then
request_body=$(jq -n --arg name $external_ip_name \
--arg dc_router_id $advanced_router_id \
--arg network_id $internet_network_uuid \
'{
dc_interface : {
name: $name,
dc_router_id: $dc_router_id,
network_id: $network_id
}
}')
curl_response=$(curl -s -X POST "${sprut_api_base}/direct_connect/dc_interfaces" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT" \
-d "$request_body")
echo "$curl_response" | jq .
fi
else
echo "Advanced router '$advanced_router_name' already exists"
neutron_to_advanced_router["$neutron_router_id"]="$advanced_router_id"
fi
done
}
compare_and_create_advanced_routers
print_map_as_table neutron_to_advanced_router "Neutron to Advanced Routers" "Neutron Router ID" "Advanced Router ID"
declare -A neutron_to_sprut_network
compare_and_create_networks() {
for neutron_network_id in "${!neutron_network_id_to_json_details[@]}"; do
neutron_network=$(echo "${neutron_network_id_to_json_details[$neutron_network_id]}" | jq -r)
neutron_network_name=$(echo "$neutron_network" | jq -r '.name')
sprut_network_name=$neutron_network_name
sprut_network_name+="-sprut"
sprut_network_id=$(echo "$sprut_networks" | jq -r --arg name "$sprut_network_name" '.networks[] | select(.name == $name) | .id')
if [ -z "$sprut_network_id" ]; then
echo "Creating Network '$sprut_network_name' in Sprut"
request_body=$(jq -n --arg name "$sprut_network_name" '{
network: {
name: $name
}
}')
curl_response=$(curl -s -X POST "${sprut_api_base}/networks" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT" \
-d "$request_body")
echo "$curl_response" | jq .
sprut_network_id=$(echo "$curl_response" | jq -r '.network.id')
neutron_to_sprut_network["$neutron_network_id"]="$sprut_network_id"
else
echo "Router '$sprut_router_name' already exists in Sprut"
neutron_to_sprut_network["$neutron_network_id"]="$sprut_network_id"
fi
done
}
compare_and_create_networks
print_map_as_table neutron_to_sprut_network "Neutron to Sprut Networks" "Neutron Network ID" "Sprut Network ID"
# TODO: add transformation for static routes via SNAT interfaces
declare -A neutron_to_sprut_subnet
compare_and_create_subnetworks() {
for neutron_subnetwork_id in "${!neutron_subnet_id_to_json_details[@]}"; do
neutron_subnetwork=$(echo "${neutron_subnet_id_to_json_details[$neutron_subnetwork_id]}" | jq -r)
neutron_subnetwork_name=$(echo "$neutron_subnetwork" | jq -r '.name')
sprut_subnetwork_name=$neutron_subnetwork_name
sprut_subnetwork_name+="-sprut"
sprut_subnetwork_id=$(echo "$sprut_subnetworks" | jq -r --arg name "$sprut_subnetwork_name" '.subnets[] | select(.name == $name) | .id')
if [ -z "$sprut_subnetwork_id" ]; then
echo "Creating Subnetwork '$sprut_subnetwork_name' in Sprut"
neutron_network_id=$(echo "$neutron_subnetwork" | jq -r '.network_id')
sprut_network_id="${neutron_to_sprut_network[$neutron_network_id]}"
request_body=$(jq -n --arg name "$sprut_subnetwork_name" \
--arg cidr "$(echo "$neutron_subnetwork" | jq -r '.cidr')" \
--arg description "$(echo "$neutron_subnetwork" | jq -r '.description')" \
--argjson dns_nameservers "$(echo "$neutron_subnetwork" | jq '.dns_nameservers')" \
--argjson enable_dhcp "$(echo "$neutron_subnetwork" | jq '.enable_dhcp')" \
--arg gateway_ip "$(echo "$neutron_subnetwork" | jq -r '.gateway_ip')" \
--argjson host_routes "$(echo "$neutron_subnetwork" | jq '.host_routes')" \
--argjson allocation_pools "$(echo "$neutron_subnetwork" | jq '.allocation_pools')" \
--arg network_id "$sprut_network_id" \
'{
subnet: {
name: $name,
cidr: $cidr,
description: $description,
dns_nameservers: $dns_nameservers,
enable_dhcp: $enable_dhcp,
gateway_ip: $gateway_ip,
host_routes: $host_routes,
allocation_pools: $allocation_pools,
network_id: $network_id,
ip_version: 4
}
}')
echo "Subnetwork creating request body: $request_body"
curl_response=$(curl -s -X POST "${sprut_api_base}/subnets" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT" \
-d "$request_body")
echo "$curl_response" | jq .
sprut_subnetwork_id=$(echo "$curl_response" | jq -r '.subnet.id')
neutron_to_sprut_subnet["$neutron_subnetwork_id"]="$sprut_subnetwork_id"
else
echo "Subnet '$sprut_subnetwork_name' already exists in Sprut"
neutron_to_sprut_subnet["$neutron_subnetwork_id"]="$sprut_subnetwork_id"
fi
done
}
compare_and_create_subnetworks
print_map_as_table neutron_to_sprut_subnet "Neutron to Sprut Subnets" "Neutron Subnetwork ID" "Sprut Subnetwork ID"
collect_sprut_objects
declare -A neutron_to_sprut_router_ports
compare_and_create_router_to_network_interfaces() {
neutron_and_sprut_ports_json=$(openstack port list -f json)
for neutron_router_id in "${!neutron_router_ID_to_json_details[@]}"; do
neutron_router=$(echo "${neutron_router_ID_to_json_details[$neutron_router_id]}" | jq -r)
neutron_router_name=$(echo "$neutron_router" | jq -r '.name')
router_type="${neutron_router_to_transform_format[$neutron_router_id]}"
for neutron_interface in $(echo "$neutron_router" | jq -c '.interfaces_info[]'); do
neutron_interface_id=$(echo "$neutron_interface" | jq -r '.port_id')
neutron_port_show_cmd="openstack port show $neutron_interface_id -f json"
echo "Running command: $neutron_port_show_cmd"
neutron_port_show_cmd_details=$($neutron_port_show_cmd)
neutron_port_device_owner=$(echo "$neutron_port_show_cmd_details" | jq -r '.device_owner')
if [ "$neutron_port_device_owner" = "network:router_centralized_snat" ]; then
echo "neutron port $neutron_interface_id is SNAT port. There are no SNAT ports in sprut, ignoring..."
continue
fi
neutron_ip=$(echo "$neutron_interface" | jq -r '.ip_address')
neutron_port_mac_address=$(echo "$neutron_port_show_cmd_details" | jq -r '.mac_address')
neutron_subnet_id=$(echo "$neutron_interface" | jq -r '.subnet_id')
sprut_subnet_id="${neutron_to_sprut_subnet[$neutron_subnet_id]}"
# sprut_subnet=$(echo "$sprut_subnetworks" | jq -r --arg id "$sprut_subnet_id" '.subnets[] | select(.id == $id) | .name')
sprut_subnet=$(echo "$sprut_subnetworks" | jq -r --arg id "$sprut_subnet_id" '.subnets[] | select(.id == $id)')
sprut_subnet_name=$(echo "$sprut_subnet" | jq -r '.name')
sprut_network_id=$(echo "$sprut_subnet" | jq -r '.network_id')
# Assign the port name
port_name="${neutron_router_name}-${sprut_subnet_name}-${neutron_ip}"
# check if router STANDARD
if [ "$router_type" = "std" ]; then
# Check if the port already exists in Sprut based on IP and MAC address
echo "Check if port $port_name already exists"
sprut_router_id=${neutron_to_sprut_router[$neutron_router_id]}
# port can exist but not be attached
sprut_port_id=$(echo "$neutron_and_sprut_ports_json" | jq -r --arg name $port_name --arg ip $neutron_ip '.[] | select(.Name == $name and ."Fixed IP Addresses"[].ip_address == $ip) | .ID')
if [ -z "$sprut_port_id" ]; then
echo "Creating Port '$port_name' in Sprut"
request_body=$(jq -n --arg name "$port_name" \
--arg ip_address "$neutron_ip" \
--arg mac_address "$neutron_port_mac_address" \
--arg network_id "$sprut_network_id" \
--argjson fixed_ips "$(jq -n --arg subnet_id "$sprut_subnet_id" --arg ip_address "$neutron_ip" '[{subnet_id: $subnet_id, ip_address: $ip_address}]')" \
'{
port: {
name: $name,
mac_address: $mac_address,
fixed_ips: $fixed_ips,
network_id: $network_id
}
}')
curl_response=$(curl -s -X POST "${sprut_api_base}/ports" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT" \
-d "$request_body")
echo "$curl_response"
sprut_port_id=$(echo "$curl_response" | jq -r '.port.id')
# attaching ports to routers
echo "Attaching created port $sprut_port_id to router $sprut_router_id"
request_body=$(jq -n --arg sprut_port_id "$sprut_port_id" \
'{
port_id: $sprut_port_id
}')
curl_response=$(curl -s -X PUT "${sprut_api_base}/routers/${sprut_router_id}/add_router_interface" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT" \
-d "$request_body")
echo "$curl_response"
else
echo "Port '$port_name' already exists in Sprut"
# port exists, check that it is attached to a router
# check if router advanced or standard
sprut_routers_json_details="${sprut_router_ports["$sprut_router_id"]}"
sprut_port_id_on_router=$(echo "$sprut_routers_json_details" | jq --arg name "$port_name" --arg mac "$neutron_port_mac_address" --arg ip "$neutron_ip" -c '
.ports[] |
select(.NAME == $name and ."MAC Address" == $mac and ."Fixed IP Addresses"[].ip_address == $ip) | .ID
')
if [[ -n "$sprut_port_id_on_router" ]]; then
echo "Port $sprut_port_id_on_router is attached to router $sprut_router_id"
else
echo "Attaching existing port $sprut_port_id to router $sprut_router_id"
request_body=$(jq -n --arg sprut_port_id "$sprut_port_id" \
'{
port_id: $sprut_port_id
}')
curl_response=$(curl -s -X PUT "${sprut_api_base}/routers/${sprut_router_id}/add_router_interface" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT" \
-d "$request_body")
echo "$curl_response"
fi
fi
fi
# Check if router ADVANCED
if [ "$router_type" = "adv" ]; then
port_name+="-adv"
echo "Check if port $port_name already exists"
adv_router_id=${neutron_to_advanced_router[$neutron_router_id]}
adv_port_id=$(echo "$neutron_and_sprut_ports_json" | jq -r --arg name $port_name --arg ip $neutron_ip '.[] | select(.name == $name and .ip_address == $ip) | .port_id')
sprut_port_id=$adv_port_id
if [ -z "$adv_port_id" ]; then
echo "Creating Port '$port_name' in Advanced router $adv_router_id"
request_body=$(jq -n --arg name $port_name \
--arg dc_router_id $adv_router_id \
--arg subnet_id $sprut_subnet_id \
--arg network_id $sprut_network_id \
--arg mac_address $neutron_port_mac_address \
--arg ip_address $neutron_ip \
'{
dc_interface : {
name: $name,
dc_router_id: $dc_router_id,
subnet_id: $subnet_id,
network_id: $network_id,
ip_address: $ip_address
}
}')
curl_response=$(curl -s -X POST "${sprut_api_base}/direct_connect/dc_interfaces" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-SDN:SPRUT" \
-d "$request_body")
echo "$curl_response" | jq .
sprut_port_id=$(echo $curl_response | jq -r '.dc_interface.id')
else
echo "Port with name $port_name and ip $ip already exists on advanced router $adv_router_id"
fi
fi
neutron_to_sprut_router_ports["$neutron_interface_id"]="$sprut_port_id"
done
done
}
compare_and_create_router_to_network_interfaces
print_map_as_table neutron_to_sprut_router_ports "Neutron to Sprut Router Ports" "Neutron Router Port ID" "Sprut Router Port ID"
echo "STAGE 3 complete: Missing Sprut objects created"
echo "**********************************************************************************"