summaryrefslogtreecommitdiff
path: root/nova/cmd/manage.py
blob: 08b8ebb310476e78a1f2d356780b1bfd7320e459 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
# Copyright (c) 2011 X.commerce, a business unit of eBay Inc.
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
# Copyright 2013 Red Hat, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.


"""
  CLI interface for nova management.
"""

import collections
import functools
import os
import re
import sys
import time
import traceback
import typing as ty
from urllib import parse as urlparse

from dateutil import parser as dateutil_parser
from keystoneauth1 import exceptions as ks_exc
from neutronclient.common import exceptions as neutron_client_exc
from os_brick.initiator import connector
import os_resource_classes as orc
from oslo_config import cfg
from oslo_db import exception as db_exc
from oslo_log import log as logging
import oslo_messaging as messaging
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
from oslo_utils import uuidutils
import prettytable
from sqlalchemy.engine import url as sqla_url

from nova.cmd import common as cmd_common
from nova.compute import api
from nova.compute import instance_actions
from nova.compute import rpcapi
import nova.conf
from nova import config
from nova import context
from nova.db import constants as db_const
from nova.db.main import api as db
from nova.db import migration
from nova import exception
from nova.i18n import _
from nova.network import constants
from nova.network import neutron as neutron_api
from nova import objects
from nova.objects import block_device as block_device_obj
from nova.objects import compute_node as compute_node_obj
from nova.objects import fields as obj_fields
from nova.objects import host_mapping as host_mapping_obj
from nova.objects import instance as instance_obj
from nova.objects import instance_mapping as instance_mapping_obj
from nova.objects import pci_device as pci_device_obj
from nova.objects import quotas as quotas_obj
from nova.objects import virtual_interface as virtual_interface_obj
from nova import rpc
from nova.scheduler.client import report
from nova.scheduler import utils as scheduler_utils
from nova import utils
from nova import version
from nova.virt.libvirt import machine_type_utils
from nova.volume import cinder

CONF = nova.conf.CONF
LOG = logging.getLogger(__name__)

# Keep this list sorted and one entry per line for readability.
_EXTRA_DEFAULT_LOG_LEVELS = [
    'nova=ERROR',
    'oslo_concurrency=INFO',
    'oslo_db=INFO',
    'oslo_policy=INFO',
    'oslo.privsep=ERROR',
    'os_brick=ERROR',
]

# Consts indicating whether allocations need to be healed by creating them or
# by updating existing allocations.
_CREATE = 'create'
_UPDATE = 'update'

# Decorators for actions
args = cmd_common.args
action_description = cmd_common.action_description


def mask_passwd_in_url(url):
    parsed = urlparse.urlparse(url)
    safe_netloc = re.sub(':.*@', ':****@', parsed.netloc)
    new_parsed = urlparse.ParseResult(
        parsed.scheme, safe_netloc,
        parsed.path, parsed.params,
        parsed.query, parsed.fragment)
    return urlparse.urlunparse(new_parsed)


def format_dict(dct, dict_property="Property", dict_value='Value',
                sort_key=None):
    """Print a `dict` as a table of two columns.

    :param dct: `dict` to print
    :param dict_property: name of the first column
    :param dict_value: header label for the value (second) column
    :param sort_key: key used for sorting the dict
    """
    pt = prettytable.PrettyTable([dict_property, dict_value])
    pt.align = 'l'
    for k, v in sorted(dct.items(), key=sort_key):
        # convert dict to str to check length
        if isinstance(v, dict):
            v = str(v)
        # if value has a newline, add in multiple rows
        # e.g. fault with stacktrace
        if v and isinstance(v, str) and r'\n' in v:
            lines = v.strip().split(r'\n')
            col1 = k
            for line in lines:
                pt.add_row([col1, line])
                col1 = ''
        else:
            pt.add_row([k, v])

    return encodeutils.safe_encode(pt.get_string()).decode()


class DbCommands(object):
    """Class for managing the main database."""

    # NOTE(danms): These functions are called with a DB context and a
    # count, which is the maximum batch size requested by the
    # user. They must be idempotent. At most $count records should be
    # migrated. The function must return a tuple of (found, done). The
    # found value indicates how many unmigrated/candidate records existed in
    # the database prior to the migration (either total, or up to the
    # $count limit provided), and a nonzero found value may tell the user
    # that there is still work to do. The done value indicates whether
    # or not any records were actually migrated by the function. Thus
    # if both (found, done) are nonzero, work was done and some work
    # remains. If found is nonzero and done is zero, some records are
    # not migratable (or don't need migrating), but all migrations that can
    # complete have finished.
    # NOTE(stephenfin): These names must be unique
    online_migrations = (
        # Added in Pike
        quotas_obj.migrate_quota_limits_to_api_db,
        # Added in Pike
        quotas_obj.migrate_quota_classes_to_api_db,
        # Added in Queens
        db.migration_migrate_to_uuid,
        # Added in Queens
        block_device_obj.BlockDeviceMapping.populate_uuids,
        # Added in Rocky
        # NOTE(tssurya): This online migration is going to be backported to
        # Queens and Pike since instance.avz of instances before Pike
        # need to be populated if it was not specified during boot time.
        instance_obj.populate_missing_availability_zones,
        # Added in Rocky
        instance_mapping_obj.populate_queued_for_delete,
        # Added in Stein
        compute_node_obj.migrate_empty_ratio,
        # Added in Stein
        virtual_interface_obj.fill_virtual_interface_list,
        # Added in Stein
        instance_mapping_obj.populate_user_id,
        # Added in Victoria
        pci_device_obj.PciDevice.populate_dev_uuids,
    )

    @args('--local_cell', action='store_true',
          help='Only sync db in the local cell: do not attempt to fan-out '
               'to all cells')
    @args('version', metavar='VERSION', nargs='?', help='Database version')
    def sync(self, version=None, local_cell=False):
        """Sync the database up to the most recent version."""
        if not local_cell:
            ctxt = context.RequestContext()
            # NOTE(mdoff): Multiple cells not yet implemented. Currently
            # fanout only looks for cell0.
            try:
                cell_mapping = objects.CellMapping.get_by_uuid(
                    ctxt, objects.CellMapping.CELL0_UUID,
                )
                with context.target_cell(ctxt, cell_mapping) as cctxt:
                    migration.db_sync(version, context=cctxt)
            except exception.CellMappingNotFound:
                msg = _(
                    'WARNING: cell0 mapping not found - not syncing cell0.'
                )
                print(msg)
            except Exception as e:
                msg = _(
                    'ERROR: Could not access cell0.\n'
                    'Has the nova_api database been created?\n'
                    'Has the nova_cell0 database been created?\n'
                    'Has "nova-manage api_db sync" been run?\n'
                    'Has "nova-manage cell_v2 map_cell0" been run?\n'
                    'Is [api_database]/connection set in nova.conf?\n'
                    'Is the cell0 database connection URL correct?\n'
                    'Error: %s'
                )
                print(msg % str(e))
                return 1

        return migration.db_sync(version)

    def version(self):
        """Print the current database version."""
        print(migration.db_version())

    @args('--max_rows', type=int, metavar='<number>', dest='max_rows',
          help='Maximum number of deleted rows to archive. Defaults to 1000. '
               'Note that this number does not include the corresponding '
               'rows, if any, that are removed from the API database for '
               'deleted instances.')
    @args('--before', metavar='<date>',
          help=('Archive rows that have been deleted before this date. '
                'Accepts date strings in the default format output by the '
                '``date`` command, as well as ``YYYY-MM-DD [HH:mm:ss]``.'))
    @args('--verbose', action='store_true', dest='verbose', default=False,
          help='Print how many rows were archived per table.')
    @args('--until-complete', action='store_true', dest='until_complete',
          default=False,
          help=('Run continuously until all deleted rows are archived. Use '
                'max_rows as a batch size for each iteration.'))
    @args('--purge', action='store_true', dest='purge', default=False,
          help='Purge all data from shadow tables after archive completes')
    @args('--all-cells', action='store_true', dest='all_cells',
          default=False, help='Run command across all cells.')
    @args('--task-log', action='store_true', dest='task_log', default=False,
          help=('Also archive ``task_log`` table records. Note that '
                '``task_log`` records are never deleted, so archiving them '
                'will move all of the ``task_log`` records up to now into the '
                'shadow tables. It is recommended to also specify the '
                '``--before`` option to avoid races for those consuming '
                '``task_log`` record data via the '
                '``/os-instance_usage_audit_log`` API (example: Telemetry).'))
    @args('--sleep', type=int, metavar='<seconds>', dest='sleep',
          help='The amount of time in seconds to sleep between batches when '
               '``--until-complete`` is used. Defaults to 0.')
    def archive_deleted_rows(
        self, max_rows=1000, verbose=False,
        until_complete=False, purge=False,
        before=None, all_cells=False, task_log=False, sleep=0,
    ):
        """Move deleted rows from production tables to shadow tables.

        Returns 0 if nothing was archived, 1 if some number of rows were
        archived, 2 if max_rows is invalid, 3 if no connection could be
        established to the API DB, 4 if before date is invalid. If automating,
        this should be run continuously while the result
        is 1, stopping at 0.
        """
        max_rows = int(max_rows)
        if max_rows < 0:
            print(_("Must supply a positive value for max_rows"))
            return 2
        if max_rows > db_const.MAX_INT:
            print(_('max rows must be <= %(max_value)d') %
                  {'max_value': db_const.MAX_INT})
            return 2

        ctxt = context.get_admin_context()
        try:
            # NOTE(tssurya): This check has been added to validate if the API
            # DB is reachable or not as this is essential for purging the
            # related API database records of the deleted instances.
            cell_mappings = objects.CellMappingList.get_all(ctxt)
        except db_exc.CantStartEngineError:
            print(_('Failed to connect to API DB so aborting this archival '
                    'attempt. Please check your config file to make sure that '
                    '[api_database]/connection is set and run this '
                    'command again.'))
            return 3

        if before:
            try:
                before_date = dateutil_parser.parse(before, fuzzy=True)
            except ValueError as e:
                print(_('Invalid value for --before: %s') % e)
                return 4
        else:
            before_date = None

        table_to_rows_archived = {}
        if until_complete and verbose:
            sys.stdout.write(_('Archiving') + '..')  # noqa

        interrupt = False

        if all_cells:
            # Sort first by cell name, then by table:
            # +--------------------------------+-------------------------+
            # | Table                          | Number of Rows Archived |
            # +--------------------------------+-------------------------+
            # | cell0.block_device_mapping     | 1                       |
            # | cell1.block_device_mapping     | 1                       |
            # | cell1.instance_actions         | 2                       |
            # | cell1.instance_actions_events  | 2                       |
            # | cell2.block_device_mapping     | 1                       |
            # | cell2.instance_actions         | 2                       |
            # | cell2.instance_actions_events  | 2                       |
            # ...
            def sort_func(item):
                cell_name, table = item[0].split('.')
                return cell_name, table
            print_sort_func = sort_func
        else:
            cell_mappings = [None]
            print_sort_func = None
        total_rows_archived = 0
        for cell_mapping in cell_mappings:
            # NOTE(Kevin_Zheng): No need to calculate limit for each
            # cell if until_complete=True.
            # We need not adjust max rows to avoid exceeding a specified total
            # limit because with until_complete=True, we have no total limit.
            if until_complete:
                max_rows_to_archive = max_rows
            elif max_rows > total_rows_archived:
                # We reduce the max rows to archive based on what we've
                # archived so far to avoid potentially exceeding the specified
                # total limit.
                max_rows_to_archive = max_rows - total_rows_archived
            else:
                break
            # If all_cells=False, cell_mapping is None
            with context.target_cell(ctxt, cell_mapping) as cctxt:
                cell_name = cell_mapping.name if cell_mapping else None
                try:
                    rows_archived = self._do_archive(
                        table_to_rows_archived,
                        cctxt,
                        max_rows_to_archive,
                        until_complete,
                        verbose,
                        before_date,
                        cell_name,
                        task_log,
                        sleep)
                except KeyboardInterrupt:
                    interrupt = True
                    break
                # TODO(melwitt): Handle skip/warn for unreachable cells. Note
                # that cell_mappings = [None] if not --all-cells
                total_rows_archived += rows_archived

        if until_complete and verbose:
            if interrupt:
                print('.' + _('stopped'))  # noqa
            else:
                print('.' + _('complete'))  # noqa

        if verbose:
            if table_to_rows_archived:
                print(format_dict(
                    table_to_rows_archived,
                    dict_property=_('Table'),
                    dict_value=_('Number of Rows Archived'),
                    sort_key=print_sort_func,
                ))
            else:
                print(_('Nothing was archived.'))

        if table_to_rows_archived and purge:
            if verbose:
                print(_('Rows were archived, running purge...'))
            self.purge(purge_all=True, verbose=verbose, all_cells=all_cells)

        # NOTE(danms): Return nonzero if we archived something
        return int(bool(table_to_rows_archived))

    def _do_archive(
        self, table_to_rows_archived, cctxt, max_rows,
        until_complete, verbose, before_date, cell_name, task_log, sleep,
    ):
        """Helper function for archiving deleted rows for a cell.

        This will archive deleted rows for a cell database and remove the
        associated API database records for deleted instances.

        :param table_to_rows_archived: Dict tracking the number of rows
            archived by <cell_name>.<table name>. Example:
            {'cell0.instances': 2,
             'cell1.instances': 5}
        :param cctxt: Cell-targeted nova.context.RequestContext if archiving
            across all cells
        :param max_rows: Maximum number of deleted rows to archive
        :param until_complete: Whether to run continuously until all deleted
            rows are archived
        :param verbose: Whether to print how many rows were archived per table
        :param before_date: Archive rows that were deleted before this date
        :param cell_name: Name of the cell or None if not archiving across all
            cells
        :param task_log: Whether to archive task_log table rows
        :param sleep: The amount of time in seconds to sleep between batches
            when ``until_complete`` is True.
        """
        ctxt = context.get_admin_context()
        while True:
            run, deleted_instance_uuids, total_rows_archived = \
                db.archive_deleted_rows(
                    cctxt, max_rows, before=before_date, task_log=task_log)
            for table_name, rows_archived in run.items():
                if cell_name:
                    table_name = cell_name + '.' + table_name
                table_to_rows_archived.setdefault(table_name, 0)
                table_to_rows_archived[table_name] += rows_archived
            if deleted_instance_uuids:
                table_to_rows_archived.setdefault(
                    'API_DB.instance_mappings', 0)
                table_to_rows_archived.setdefault(
                    'API_DB.request_specs', 0)
                table_to_rows_archived.setdefault(
                    'API_DB.instance_group_member', 0)
                deleted_mappings = objects.InstanceMappingList.destroy_bulk(
                            ctxt, deleted_instance_uuids)
                table_to_rows_archived[
                    'API_DB.instance_mappings'] += deleted_mappings
                deleted_specs = objects.RequestSpec.destroy_bulk(
                    ctxt, deleted_instance_uuids)
                table_to_rows_archived[
                    'API_DB.request_specs'] += deleted_specs
                deleted_group_members = (
                    objects.InstanceGroup.destroy_members_bulk(
                        ctxt, deleted_instance_uuids))
                table_to_rows_archived[
                    'API_DB.instance_group_member'] += deleted_group_members

            # If we're not archiving until there is nothing more to archive, we
            # have reached max_rows in this cell DB or there was nothing to
            # archive.
            if not until_complete or not run:
                break
            if verbose:
                sys.stdout.write('.')
            # Optionally sleep between batches to throttle the archiving.
            time.sleep(sleep)
        return total_rows_archived

    @args('--before', metavar='<before>', dest='before',
          help='If specified, purge rows from shadow tables that are older '
               'than this. Accepts date strings in the default format output '
               'by the ``date`` command, as well as ``YYYY-MM-DD '
               '[HH:mm:ss]``.')
    @args('--all', dest='purge_all', action='store_true',
          help='Purge all rows in the shadow tables')
    @args('--verbose', dest='verbose', action='store_true', default=False,
          help='Print information about purged records')
    @args('--all-cells', dest='all_cells', action='store_true', default=False,
          help='Run against all cell databases')
    def purge(self, before=None, purge_all=False, verbose=False,
              all_cells=False):
        if before is None and purge_all is False:
            print(_('Either --before or --all is required'))
            return 1
        if before:
            try:
                before_date = dateutil_parser.parse(before, fuzzy=True)
            except ValueError as e:
                print(_('Invalid value for --before: %s') % e)
                return 2
        else:
            before_date = None

        def status(msg):
            if verbose:
                print('%s: %s' % (identity, msg))

        deleted = 0
        admin_ctxt = context.get_admin_context()

        if all_cells:
            try:
                cells = objects.CellMappingList.get_all(admin_ctxt)
            except db_exc.DBError:
                print(_('Unable to get cell list from API DB. '
                        'Is it configured?'))
                return 4
            for cell in cells:
                identity = _('Cell %s') % cell.identity
                with context.target_cell(admin_ctxt, cell) as cctxt:
                    deleted += db.purge_shadow_tables(
                        cctxt, before_date, status_fn=status)
        else:
            identity = _('DB')
            deleted = db.purge_shadow_tables(
                admin_ctxt, before_date, status_fn=status)
        if deleted:
            return 0
        else:
            return 3

    def _run_migration(self, ctxt, max_count):
        ran = 0
        exceptions = False
        migrations = {}
        for migration_meth in self.online_migrations:
            count = max_count - ran
            try:
                found, done = migration_meth(ctxt, count)
            except Exception:
                msg = (_("Error attempting to run %(method)s") % dict(
                       method=migration_meth))
                print(msg)
                LOG.exception(msg)
                exceptions = True
                found = done = 0

            name = migration_meth.__name__
            if found:
                print(_('%(total)i rows matched query %(meth)s, %(done)i '
                        'migrated') % {'total': found,
                                       'meth': name,
                                       'done': done})
            # This is the per-migration method result for this batch, and
            # _run_migration will either continue on to the next migration,
            # or stop if up to this point we've processed max_count of
            # records across all migration methods.
            migrations[name] = found, done
            if max_count is not None:
                ran += done
                if ran >= max_count:
                    break
        return migrations, exceptions

    @args('--max-count', metavar='<number>', dest='max_count',
          help='Maximum number of objects to consider')
    def online_data_migrations(self, max_count=None):
        ctxt = context.get_admin_context()
        if max_count is not None:
            try:
                max_count = int(max_count)
            except ValueError:
                max_count = -1
            unlimited = False
            if max_count < 1:
                print(_('Must supply a positive value for max_number'))
                return 127
        else:
            unlimited = True
            max_count = 50
            print(_('Running batches of %i until complete') % max_count)

        ran = None
        migration_info = {}
        exceptions = False
        while ran is None or ran != 0:
            migrations, exceptions = self._run_migration(ctxt, max_count)
            ran = 0
            # For each batch of migration method results, build the cumulative
            # set of results.
            for name in migrations:
                migration_info.setdefault(name, (0, 0))
                migration_info[name] = (
                    migration_info[name][0] + migrations[name][0],
                    migration_info[name][1] + migrations[name][1],
                )
                ran += migrations[name][1]
            if not unlimited:
                break

        t = prettytable.PrettyTable([_('Migration'),
                                     _('Total Needed'),  # Really: Total Found
                                     _('Completed')])
        for name in sorted(migration_info.keys()):
            info = migration_info[name]
            t.add_row([name, info[0], info[1]])
        print(t)

        # NOTE(imacdonn): In the "unlimited" case, the loop above will only
        # terminate when all possible migrations have been effected. If we're
        # still getting exceptions, there's a problem that requires
        # intervention. In the max-count case, exceptions are only considered
        # fatal if no work was done by any other migrations ("not ran"),
        # because otherwise work may still remain to be done, and that work
        # may resolve dependencies for the failing migrations.
        if exceptions and (unlimited or not ran):
            print(_("Some migrations failed unexpectedly. Check log for "
                    "details."))
            return 2

        # TODO(mriedem): Potentially add another return code for
        # "there are more migrations, but not completable right now"
        return ran and 1 or 0


class ApiDbCommands(object):
    """Class for managing the api database."""

    def __init__(self):
        pass

    @args('version', metavar='VERSION', nargs='?', help='Database version')
    def sync(self, version=None):
        """Sync the database up to the most recent version."""
        return migration.db_sync(version, database='api')

    def version(self):
        """Print the current database version."""
        print(migration.db_version(database='api'))


class CellV2Commands(object):
    """Commands for managing cells v2."""

    def _validate_transport_url(self, transport_url, warn_about_none=True):
        if not transport_url:
            if not CONF.transport_url:
                if warn_about_none:
                    print(_(
                        'Must specify --transport-url if '
                        '[DEFAULT]/transport_url is not set in the '
                        'configuration file.'))
                return None
            print(_('--transport-url not provided in the command line, '
                    'using the value [DEFAULT]/transport_url from the '
                    'configuration file'))
            transport_url = CONF.transport_url

        try:
            messaging.TransportURL.parse(conf=CONF,
                                         url=objects.CellMapping.format_mq_url(
                                             transport_url))
        except (messaging.InvalidTransportURL, ValueError) as e:
            print(_('Invalid transport URL: %s') % str(e))
            return None

        return transport_url

    def _validate_database_connection(
            self, database_connection, warn_about_none=True):
        if not database_connection:
            if not CONF.database.connection:
                if warn_about_none:
                    print(_(
                        'Must specify --database_connection if '
                        '[database]/connection is not set in the '
                        'configuration file.'))
                return None
            print(_('--database_connection not provided in the command line, '
                    'using the value [database]/connection from the '
                    'configuration file'))
            return CONF.database.connection
        return database_connection

    def _non_unique_transport_url_database_connection_checker(self, ctxt,
                            cell_mapping, transport_url, database_connection):
        for cell in objects.CellMappingList.get_all(ctxt):
            if cell_mapping and cell.uuid == cell_mapping.uuid:
                # If we're looking for a specific cell, then don't check
                # that one for same-ness to allow idempotent updates
                continue
            if (cell.database_connection == database_connection or
                cell.transport_url == transport_url):
                print(_('The specified transport_url and/or '
                        'database_connection combination already exists '
                        'for another cell with uuid %s.') % cell.uuid)
                return True
        return False

    @args('--transport-url', metavar='<transport_url>', dest='transport_url',
          help='The transport url for the cell message queue')
    def simple_cell_setup(self, transport_url=None):
        """Simple cellsv2 setup.

        This simplified command is for use by existing non-cells users to
        configure the default environment. Returns 0 if setup is completed (or
        has already been done) and 1 if no hosts are reporting (and this cannot
        be mapped).
        """
        transport_url = self._validate_transport_url(transport_url)
        if not transport_url:
            return 1
        ctxt = context.RequestContext()
        try:
            cell0_mapping = self._map_cell0()
        except db_exc.DBDuplicateEntry:
            print(_('Cell0 is already setup'))
            cell0_mapping = objects.CellMapping.get_by_uuid(
                ctxt, objects.CellMapping.CELL0_UUID)

        # Run migrations so cell0 is usable
        with context.target_cell(ctxt, cell0_mapping) as cctxt:
            try:
                migration.db_sync(None, context=cctxt)
            except db_exc.DBError as ex:
                print(_('Unable to sync cell0 schema: %s') % ex)

        cell_uuid = self._map_cell_and_hosts(transport_url)
        if cell_uuid is None:
            # There are no compute hosts which means no cell_mapping was
            # created. This should also mean that there are no instances.
            return 1
        self.map_instances(cell_uuid)
        return 0

    @args('--database_connection',
          metavar='<database_connection>',
          help='The database connection url for cell0. '
               'This is optional. If not provided, a standard database '
               'connection will be used based on the main database connection '
               'from the Nova configuration.'
         )
    def map_cell0(self, database_connection=None):
        """Create a cell mapping for cell0.

        cell0 is used for instances that have not been scheduled to any cell.
        This generally applies to instances that have encountered an error
        before they have been scheduled.

        This command creates a cell mapping for this special cell which
        requires a database to store the instance data.

        Returns 0 if cell0 created successfully or already setup.
        """
        try:
            self._map_cell0(database_connection=database_connection)
        except db_exc.DBDuplicateEntry:
            print(_('Cell0 is already setup'))
        return 0

    def _map_cell0(self, database_connection=None):
        """Faciliate creation of a cell mapping for cell0.
        See map_cell0 for more.
        """
        def cell0_default_connection():
            # If no database connection is provided one is generated
            # based on the database connection url.
            # The cell0 database will use the same database scheme and
            # netloc as the main database, with a related path.
            # NOTE(sbauza): The URL has to be RFC1738 compliant in order to
            # be usable by sqlalchemy.
            connection = CONF.database.connection
            # sqlalchemy has a nice utility for parsing database connection
            # URLs so we use that here to get the db name so we don't have to
            # worry about parsing and splitting a URL which could have special
            # characters in the password, which makes parsing a nightmare.
            url = sqla_url.make_url(connection)
            url = url.set(database=url.database + '_cell0')

            return urlparse.unquote(str(url))

        dbc = database_connection or cell0_default_connection()
        ctxt = context.RequestContext()
        # A transport url of 'none://' is provided for cell0. RPC should not
        # be used to access cell0 objects. Cells transport switching will
        # ignore any 'none' transport type.
        cell_mapping = objects.CellMapping(
                ctxt, uuid=objects.CellMapping.CELL0_UUID, name="cell0",
                transport_url="none:///",
                database_connection=dbc)
        cell_mapping.create()
        return cell_mapping

    def _get_and_map_instances(self, ctxt, cell_mapping, limit, marker):
        filters = {}
        with context.target_cell(ctxt, cell_mapping) as cctxt:
            instances = objects.InstanceList.get_by_filters(
                    cctxt.elevated(read_deleted='yes'), filters,
                    sort_key='created_at', sort_dir='asc', limit=limit,
                    marker=marker)

        for instance in instances:
            try:
                mapping = objects.InstanceMapping(ctxt)
                mapping.instance_uuid = instance.uuid
                mapping.cell_mapping = cell_mapping
                mapping.project_id = instance.project_id
                mapping.user_id = instance.user_id
                mapping.create()
            except db_exc.DBDuplicateEntry:
                continue

        if len(instances) == 0 or len(instances) < limit:
            # We've hit the end of the instances table
            marker = None
        else:
            marker = instances[-1].uuid
        return marker

    @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
          required=True,
          help='Unmigrated instances will be mapped to the cell with the '
               'uuid provided.')
    @args('--max-count', metavar='<max_count>', dest='max_count',
          help='Maximum number of instances to map. If not set, all instances '
               'in the cell will be mapped in batches of 50. If you have a '
               'large number of instances, consider specifying a custom value '
               'and run the command until it exits with 0.')
    @args('--reset', action='store_true', dest='reset_marker',
          help='The command will start from the beginning as opposed to the '
               'default behavior of starting from where the last run '
               'finished')
    def map_instances(self, cell_uuid, max_count=None, reset_marker=None):
        """Map instances into the provided cell.

        Instances in the nova database of the provided cell (nova database
        info is obtained from the nova-api database) will be queried from
        oldest to newest and if unmapped, will be mapped to the provided cell.
        A max-count can be set on the number of instance to map in a single
        run. Repeated runs of the command will start from where the last run
        finished so it is not necessary to increase max-count to finish. A
        reset option can be passed which will reset the marker, thus making the
        command start from the beginning as opposed to the default behavior of
        starting from where the last run finished. An exit code of 0 indicates
        that all instances have been mapped.
        """

        # NOTE(stephenfin): The support for batching in this command relies on
        # a bit of a hack. We initially process N instance-cell mappings, where
        # N is the value of '--max-count' if provided else 50. To ensure we
        # can continue from N on the next iteration, we store a instance-cell
        # mapping object with a special name and the UUID of the last
        # instance-cell mapping processed (N - 1) in munged form. On the next
        # iteration, we search for the special name and unmunge the UUID to
        # pick up where we left off. This is done until all mappings are
        # processed. The munging is necessary as there's a unique constraint on
        # the UUID field and we need something reversable. For more
        # information, see commit 9038738d0.

        if max_count is not None:
            try:
                max_count = int(max_count)
            except ValueError:
                max_count = -1
            map_all = False
            if max_count < 1:
                print(_('Must supply a positive value for max-count'))
                return 127
        else:
            map_all = True
            max_count = 50

        ctxt = context.RequestContext()
        marker_project_id = 'INSTANCE_MIGRATION_MARKER'

        # Validate the cell exists, this will raise if not
        cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)

        # Check for a marker from a previous run
        marker_mapping = objects.InstanceMappingList.get_by_project_id(ctxt,
                marker_project_id)
        if len(marker_mapping) == 0:
            marker = None
        else:
            # There should be only one here
            marker = marker_mapping[0].instance_uuid.replace(' ', '-')
            if reset_marker:
                marker = None
            marker_mapping[0].destroy()

        next_marker = True
        while next_marker is not None:
            next_marker = self._get_and_map_instances(ctxt, cell_mapping,
                    max_count, marker)
            marker = next_marker
            if not map_all:
                break

        if next_marker:
            # Don't judge me. There's already an InstanceMapping with this UUID
            # so the marker needs to be non destructively modified.
            next_marker = next_marker.replace('-', ' ')
            # This is just the marker record, so set user_id to the special
            # marker name as well.
            objects.InstanceMapping(ctxt, instance_uuid=next_marker,
                    project_id=marker_project_id,
                    user_id=marker_project_id).create()
            return 1
        return 0

    def _map_cell_and_hosts(self, transport_url, name=None, verbose=False):
        ctxt = context.RequestContext()
        cell_mapping_uuid = cell_mapping = None
        # First, try to detect if a CellMapping has already been created
        compute_nodes = objects.ComputeNodeList.get_all(ctxt)
        if not compute_nodes:
            print(_('No hosts found to map to cell, exiting.'))
            return None
        missing_nodes = set()
        for compute_node in compute_nodes:
            try:
                host_mapping = objects.HostMapping.get_by_host(
                    ctxt, compute_node.host)
            except exception.HostMappingNotFound:
                missing_nodes.add(compute_node.host)
            else:
                if verbose:
                    print(_(
                        'Host %(host)s is already mapped to cell %(uuid)s'
                        ) % {'host': host_mapping.host,
                             'uuid': host_mapping.cell_mapping.uuid})
                # Re-using the existing UUID in case there is already a mapping
                # NOTE(sbauza): There could be possibly multiple CellMappings
                # if the operator provides another configuration file and moves
                # the hosts to another cell v2, but that's not really something
                # we should support.
                cell_mapping_uuid = host_mapping.cell_mapping.uuid
        if not missing_nodes:
            print(_('All hosts are already mapped to cell(s).'))
            return cell_mapping_uuid
        # Create the cell mapping in the API database
        if cell_mapping_uuid is not None:
            cell_mapping = objects.CellMapping.get_by_uuid(
                ctxt, cell_mapping_uuid)
        if cell_mapping is None:
            cell_mapping_uuid = uuidutils.generate_uuid()
            cell_mapping = objects.CellMapping(
                ctxt, uuid=cell_mapping_uuid, name=name,
                transport_url=transport_url,
                database_connection=CONF.database.connection)
            cell_mapping.create()
        # Pull the hosts from the cell database and create the host mappings
        for compute_host in missing_nodes:
            host_mapping = objects.HostMapping(
                ctxt, host=compute_host, cell_mapping=cell_mapping)
            host_mapping.create()
        if verbose:
            print(cell_mapping_uuid)
        return cell_mapping_uuid

    @args('--transport-url', metavar='<transport_url>', dest='transport_url',
          help='The transport url for the cell message queue')
    @args('--name', metavar='<cell_name>', help='The name of the cell')
    @args('--verbose', action='store_true',
          help='Output the cell mapping uuid for any newly mapped hosts.')
    def map_cell_and_hosts(self, transport_url=None, name=None, verbose=False):
        """EXPERIMENTAL. Create a cell mapping and host mappings for a cell.

        Users not dividing their cloud into multiple cells will be a single
        cell v2 deployment and should specify:

          nova-manage cell_v2 map_cell_and_hosts --config-file <nova.conf>

        Users running multiple cells can add a cell v2 by specifying:

          nova-manage cell_v2 map_cell_and_hosts --config-file <cell nova.conf>
        """
        transport_url = self._validate_transport_url(transport_url)
        if not transport_url:
            return 1
        self._map_cell_and_hosts(transport_url, name, verbose)
        # online_data_migrations established a pattern of 0 meaning everything
        # is done, 1 means run again to do more work. This command doesn't do
        # partial work so 0 is appropriate.
        return 0

    @args('--uuid', metavar='<instance_uuid>', dest='uuid', required=True,
          help=_('The instance UUID to verify'))
    @args('--quiet', action='store_true', dest='quiet',
          help=_('Do not print anything'))
    def verify_instance(self, uuid, quiet=False):
        """Verify instance mapping to a cell.

        This command is useful to determine if the cellsv2 environment is
        properly setup, specifically in terms of the cell, host, and instance
        mapping records required.

        This prints one of three strings (and exits with a code) indicating
        whether the instance is successfully mapped to a cell (0), is unmapped
        due to an incomplete upgrade (1), unmapped due to normally transient
        state (2), it is a deleted instance which has instance mapping (3),
        or it is an archived instance which still has an instance mapping (4).
        """
        def say(string):
            if not quiet:
                print(string)

        ctxt = context.get_admin_context()
        try:
            mapping = objects.InstanceMapping.get_by_instance_uuid(
                ctxt, uuid)
        except exception.InstanceMappingNotFound:
            say('Instance %s is not mapped to a cell '
                '(upgrade is incomplete) or instance '
                'does not exist' % uuid)
            return 1
        if mapping.cell_mapping is None:
            say('Instance %s is not mapped to a cell' % uuid)
            return 2
        else:
            with context.target_cell(ctxt, mapping.cell_mapping) as cctxt:
                try:
                    instance = objects.Instance.get_by_uuid(cctxt, uuid)
                except exception.InstanceNotFound:
                    try:
                        el_ctx = cctxt.elevated(read_deleted='yes')
                        instance = objects.Instance.get_by_uuid(el_ctx, uuid)
                        # instance is deleted
                        if instance:
                            say('The instance with uuid %s has been deleted.'
                                % uuid)
                            say('Execute '
                                '`nova-manage db archive_deleted_rows` '
                                'command to archive this deleted '
                                'instance and remove its instance_mapping.')
                            return 3
                    except exception.InstanceNotFound:
                        # instance is archived
                        say('The instance with uuid %s has been archived.'
                            % uuid)
                        say('However its instance_mapping remains.')
                        return 4
            # instance is alive and mapped to a cell
            say('Instance %s is in cell: %s (%s)' % (
                uuid,
                mapping.cell_mapping.name,
                mapping.cell_mapping.uuid))
            return 0

    @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
          help='If provided only this cell will be searched for new hosts to '
               'map.')
    @args('--verbose', action='store_true',
          help=_('Provide detailed output when discovering hosts.'))
    @args('--strict', action='store_true',
          help=_('Considered successful (exit code 0) only when an unmapped '
                 'host is discovered. Any other outcome will be considered a '
                 'failure (non-zero exit code).'))
    @args('--by-service', action='store_true', default=False,
          dest='by_service',
          help=_('Discover hosts by service instead of compute node'))
    def discover_hosts(self, cell_uuid=None, verbose=False, strict=False,
                       by_service=False):
        """Searches cells, or a single cell, and maps found hosts.

        When a new host is added to a deployment it will add a service entry
        to the db it's configured to use. This command will check the db for
        each cell, or a single one if passed in, and map any hosts which are
        not currently mapped. If a host is already mapped nothing will be done.

        This command should be run once after all compute hosts have been
        deployed and should not be run in parallel. When run in parallel,
        the commands will collide with each other trying to map the same hosts
        in the database at the same time.
        """
        def status_fn(msg):
            if verbose:
                print(msg)

        ctxt = context.RequestContext()
        try:
            hosts = host_mapping_obj.discover_hosts(ctxt, cell_uuid, status_fn,
                                                    by_service)
        except exception.HostMappingExists as exp:
            print(_('ERROR: Duplicate host mapping was encountered. This '
                    'command should be run once after all compute hosts have '
                    'been deployed and should not be run in parallel. When '
                    'run in parallel, the commands will collide with each '
                    'other trying to map the same hosts in the database at '
                    'the same time. Error: %s') % exp)
            return 2
        # discover_hosts will return an empty list if no hosts are discovered
        if strict:
            return int(not hosts)

    @action_description(
        _("Add a new cell to nova API database. "
          "DB and MQ urls can be provided directly "
          "or can be taken from config. The result is cell uuid."))
    @args('--name', metavar='<cell_name>', help=_('The name of the cell'))
    @args('--database_connection', metavar='<database_connection>',
          dest='database_connection',
          help=_('The database url for the cell database'))
    @args('--transport-url', metavar='<transport_url>', dest='transport_url',
          help=_('The transport url for the cell message queue'))
    @args('--verbose', action='store_true',
          help=_('Output the uuid of the created cell'))
    @args('--disabled', action='store_true',
          help=_('To create a pre-disabled cell.'))
    def create_cell(self, name=None, database_connection=None,
                    transport_url=None, verbose=False, disabled=False):
        ctxt = context.get_context()
        transport_url = self._validate_transport_url(transport_url)
        if not transport_url:
            return 1

        database_connection = self._validate_database_connection(
            database_connection)
        if not database_connection:
            return 1
        if (self._non_unique_transport_url_database_connection_checker(ctxt,
            None, transport_url, database_connection)):
            return 2
        cell_mapping_uuid = uuidutils.generate_uuid()
        cell_mapping = objects.CellMapping(
            ctxt,
            uuid=cell_mapping_uuid, name=name,
            transport_url=transport_url,
            database_connection=database_connection,
            disabled=disabled)
        cell_mapping.create()
        if verbose:
            print(cell_mapping_uuid)
        return 0

    @args('--verbose', action='store_true',
          help=_('Show sensitive details, such as passwords'))
    def list_cells(self, verbose=False):
        """Lists the v2 cells in the deployment.

        By default the cell name, uuid, disabled state, masked transport
        URL and database connection details are shown. Use the --verbose
        option to see transport URL and database connection with their
        sensitive details.
        """
        cell_mappings = objects.CellMappingList.get_all(
            context.get_admin_context())

        field_names = [_('Name'), _('UUID'), _('Transport URL'),
                       _('Database Connection'), _('Disabled')]

        t = prettytable.PrettyTable(field_names)
        for cell in sorted(cell_mappings,
                           # CellMapping.name is optional
                           key=lambda _cell: _cell.name or ''):
            fields = [cell.name or '', cell.uuid]
            if verbose:
                fields.extend([cell.transport_url, cell.database_connection])
            else:
                fields.extend([
                    mask_passwd_in_url(cell.transport_url),
                    mask_passwd_in_url(cell.database_connection)])
            fields.extend([cell.disabled])
            t.add_row(fields)
        print(t)
        return 0

    @args('--force', action='store_true', default=False,
          help=_('Delete hosts and instance_mappings that belong '
                 'to the cell as well.'))
    @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
          required=True, help=_('The uuid of the cell to delete.'))
    def delete_cell(self, cell_uuid, force=False):
        """Delete an empty cell by the given uuid.

        This command will return a non-zero exit code in the following cases.

        * The cell is not found by uuid.
        * It has hosts and force is False.
        * It has instance mappings and force is False.

        If force is True and the cell has hosts and/or instance_mappings, they
        are deleted as well (as long as there are no living instances).

        Returns 0 in the following cases.

        * The empty cell is found and deleted successfully.
        * The cell has hosts and force is True then the cell, hosts and
          instance_mappings are deleted successfully; if there are no
          living instances.
        """
        ctxt = context.get_admin_context()
        # Find the CellMapping given the uuid.
        try:
            cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
        except exception.CellMappingNotFound:
            print(_('Cell with uuid %s was not found.') % cell_uuid)
            return 1

        # Check to see if there are any HostMappings for this cell.
        host_mappings = objects.HostMappingList.get_by_cell_id(
            ctxt, cell_mapping.id)
        nodes = []
        if host_mappings:
            if not force:
                print(_('There are existing hosts mapped to cell with uuid '
                        '%s.') % cell_uuid)
                return 2
            # We query for the compute nodes in the cell,
            # so that they can be unmapped.
            with context.target_cell(ctxt, cell_mapping) as cctxt:
                nodes = objects.ComputeNodeList.get_all(cctxt)

        # Check to see if there are any InstanceMappings for this cell.
        instance_mappings = objects.InstanceMappingList.get_by_cell_id(
            ctxt, cell_mapping.id)
        if instance_mappings:
            with context.target_cell(ctxt, cell_mapping) as cctxt:
                instances = objects.InstanceList.get_all(cctxt)
            if instances:
                # There are instances in the cell.
                print(_('There are existing instances mapped to cell with '
                        'uuid %s.') % cell_uuid)
                return 3
            else:
                if not force:
                    # There are no instances in the cell but the records remain
                    # in the 'instance_mappings' table.
                    print(_("There are instance mappings to cell with uuid "
                            "%s, but all instances have been deleted "
                            "in the cell.") % cell_uuid)
                    print(_("So execute 'nova-manage db archive_deleted_rows' "
                            "to delete the instance mappings."))
                    return 4

        # Delete instance_mappings of the deleted instances
        for instance_mapping in instance_mappings:
            instance_mapping.destroy()

        # Unmap the compute nodes so that they can be discovered
        # again in future, if needed.
        for node in nodes:
            node.mapped = 0
            node.save()

        # Delete hosts mapped to the cell.
        for host_mapping in host_mappings:
            host_mapping.destroy()

        # There are no hosts or instances mapped to the cell so delete it.
        cell_mapping.destroy()
        return 0

    @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
          required=True, help=_('The uuid of the cell to update.'))
    @args('--name', metavar='<cell_name>', dest='name',
          help=_('Set the cell name.'))
    @args('--transport-url', metavar='<transport_url>', dest='transport_url',
          help=_('Set the cell transport_url. NOTE that running nodes '
                 'will not see the change until restart!'))
    @args('--database_connection', metavar='<database_connection>',
          dest='db_connection',
          help=_('Set the cell database_connection. NOTE that running nodes '
                 'will not see the change until restart!'))
    @args('--disable', action='store_true', dest='disable',
          help=_('Disables the cell. Note that the scheduling will be blocked '
                 'to this cell until its enabled and followed by a SIGHUP of '
                 'nova-scheduler service.'))
    @args('--enable', action='store_true', dest='enable',
          help=_('Enables the cell. Note that this makes a disabled cell '
                 'available for scheduling after a SIGHUP of the '
                 'nova-scheduler service'))
    def update_cell(self, cell_uuid, name=None, transport_url=None,
                    db_connection=None, disable=False, enable=False):
        """Updates the properties of a cell by the given uuid.

        If the cell is not found by uuid, this command will return an exit
        code of 1. If the provided transport_url or/and database_connection
        is/are same as another cell, this command will return an exit code
        of 3. If the properties cannot be set, this will return 2. If an
        attempt is made to disable and enable a cell at the same time, this
        command will exit with a return code of 4. If an attempt is made to
        disable or enable cell0 this command will exit with a return code of 5.
        Otherwise, the exit code will be 0.

        NOTE: Updating the transport_url or database_connection fields on
        a running system will NOT result in all nodes immediately using the
        new values. Use caution when changing these values.
        NOTE (tssurya): The scheduler will not notice that a cell has been
        enabled/disabled until it is restarted or sent the SIGHUP signal.
        """
        ctxt = context.get_admin_context()
        try:
            cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
        except exception.CellMappingNotFound:
            print(_('Cell with uuid %s was not found.') % cell_uuid)
            return 1

        if name:
            cell_mapping.name = name

        # Having empty transport_url and db_connection means leaving the
        # existing values
        transport_url = self._validate_transport_url(
            transport_url, warn_about_none=False)
        db_connection = self._validate_database_connection(
            db_connection, warn_about_none=False)

        if (self._non_unique_transport_url_database_connection_checker(ctxt,
                cell_mapping, transport_url, db_connection)):
            # We use the return code 3 before 2 to avoid changing the
            # semantic meanings of return codes.
            return 3

        if transport_url:
            cell_mapping.transport_url = transport_url

        if db_connection:
            cell_mapping.database_connection = db_connection

        if disable and enable:
            print(_('Cell cannot be disabled and enabled at the same time.'))
            return 4
        if disable or enable:
            if cell_mapping.is_cell0():
                print(_('Cell0 cannot be disabled.'))
                return 5
            elif disable and not cell_mapping.disabled:
                cell_mapping.disabled = True
            elif enable and cell_mapping.disabled:
                cell_mapping.disabled = False
            elif disable and cell_mapping.disabled:
                print(_('Cell %s is already disabled') % cell_uuid)
            elif enable and not cell_mapping.disabled:
                print(_('Cell %s is already enabled') % cell_uuid)

        try:
            cell_mapping.save()
        except Exception as e:
            print(_('Unable to update CellMapping: %s') % e)
            return 2

        return 0

    @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
          help=_('The uuid of the cell.'))
    def list_hosts(self, cell_uuid=None):
        """Lists the hosts in one or all v2 cells."""
        ctxt = context.get_admin_context()
        if cell_uuid:
            # Find the CellMapping given the uuid.
            try:
                cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
            except exception.CellMappingNotFound:
                print(_('Cell with uuid %s was not found.') % cell_uuid)
                return 1

            host_mappings = objects.HostMappingList.get_by_cell_id(
                ctxt, cell_mapping.id)
        else:
            host_mappings = objects.HostMappingList.get_all(ctxt)

        field_names = [_('Cell Name'), _('Cell UUID'), _('Hostname')]

        t = prettytable.PrettyTable(field_names)
        for host in sorted(host_mappings, key=lambda _host: _host.host):
            fields = [host.cell_mapping.name, host.cell_mapping.uuid,
                      host.host]
            t.add_row(fields)
        print(t)
        return 0

    @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
          required=True, help=_('The uuid of the cell.'))
    @args('--host', metavar='<host>', dest='host',
          required=True, help=_('The host to delete.'))
    def delete_host(self, cell_uuid, host):
        """Delete a host in a cell (host mappings) by the given host name

        This command will return a non-zero exit code in the following cases.

        * The cell is not found by uuid.
        * The host is not found by host name.
        * The host is not in the cell.
        * The host has instances.

        Returns 0 if the host is deleted successfully.

        NOTE: The scheduler caches host-to-cell mapping information so when
        deleting a host the scheduler may need to be restarted or sent the
        SIGHUP signal.
        """
        ctxt = context.get_admin_context()
        # Find the CellMapping given the uuid.
        try:
            cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
        except exception.CellMappingNotFound:
            print(_('Cell with uuid %s was not found.') % cell_uuid)
            return 1

        try:
            host_mapping = objects.HostMapping.get_by_host(ctxt, host)
        except exception.HostMappingNotFound:
            print(_('The host %s was not found.') % host)
            return 2

        if host_mapping.cell_mapping.uuid != cell_mapping.uuid:
            print(_('The host %(host)s was not found '
                    'in the cell %(cell_uuid)s.') % {'host': host,
                                                     'cell_uuid': cell_uuid})
            return 3

        with context.target_cell(ctxt, cell_mapping) as cctxt:
            instances = objects.InstanceList.get_by_host(cctxt, host)
            try:
                nodes = objects.ComputeNodeList.get_all_by_host(cctxt, host)
            except exception.ComputeHostNotFound:
                nodes = []

        if instances:
            print(_('There are instances on the host %s.') % host)
            return 4

        for node in nodes:
            node.mapped = 0
            node.save()

        host_mapping.destroy()
        return 0


class PlacementCommands(object):
    """Commands for managing placement resources."""

    @staticmethod
    def _get_compute_node_uuid(ctxt, instance, node_cache):
        """Find the ComputeNode.uuid for the given Instance

        :param ctxt: cell-targeted nova.context.RequestContext
        :param instance: the instance to lookup a compute node
        :param node_cache: dict of Instance.node keys to ComputeNode.uuid
            values; this cache is updated if a new node is processed.
        :returns: ComputeNode.uuid for the given instance
        :raises: nova.exception.ComputeHostNotFound
        """
        if instance.node in node_cache:
            return node_cache[instance.node]

        compute_node = objects.ComputeNode.get_by_host_and_nodename(
            ctxt, instance.host, instance.node)
        node_uuid = compute_node.uuid
        node_cache[instance.node] = node_uuid
        return node_uuid

    @staticmethod
    def _get_ports(ctxt, instance, neutron):
        """Return the ports that are bound to the instance

        :param ctxt: nova.context.RequestContext
        :param instance: the instance to return the ports for
        :param neutron: nova.network.neutron.ClientWrapper to
            communicate with Neutron
        :return: a list of neutron port dict objects
        :raise UnableToQueryPorts: If the neutron list ports query fails.
        """
        try:
            return neutron.list_ports(
                ctxt, device_id=instance.uuid,
                fields=['id', constants.RESOURCE_REQUEST,
                        constants.BINDING_PROFILE]
            )['ports']
        except neutron_client_exc.NeutronClientException as e:
            raise exception.UnableToQueryPorts(
                instance_uuid=instance.uuid, error=str(e))

    @staticmethod
    def _has_request_but_no_allocation(port, neutron):
        has_res_req = neutron_api.API()._has_resource_request(
            context.get_admin_context(), port, neutron)

        binding_profile = neutron_api.get_binding_profile(port)
        allocation = binding_profile.get(constants.ALLOCATION)
        return has_res_req and not allocation

    @staticmethod
    def _merge_allocations(alloc1, alloc2):
        """Return a new allocation dict that contains the sum of alloc1 and
        alloc2.

        :param alloc1: a dict in the form of
            {
                <rp_uuid>: {'resources': {<resource class>: amount,
                                          <resource class>: amount},
                <rp_uuid>: {'resources': {<resource class>: amount},
            }
        :param alloc2: a dict in the same form as alloc1
        :return: the merged allocation of alloc1 and alloc2 in the same format
        """

        allocations = collections.defaultdict(
            lambda: {'resources': collections.defaultdict(int)})

        for alloc in [alloc1, alloc2]:
            for rp_uuid in alloc:
                for rc, amount in alloc[rp_uuid]['resources'].items():
                    allocations[rp_uuid]['resources'][rc] += amount
        return allocations

    @staticmethod
    def _get_resource_request_from_ports(
        ctxt: context.RequestContext,
        ports: ty.List[ty.Dict[str, ty.Any]]
    ) -> ty.Tuple[
            ty.Dict[str, ty.List['objects.RequestGroup']],
            'objects.RequestLevelParams']:
        """Collect RequestGroups and RequestLevelParams for all ports

        :param ctxt: the request context
        :param ports: a list of port dicts
        :returns: A two tuple where the first item is a dict mapping port
            uuids to a list of request groups coming from that port, the
            second item is a combined RequestLevelParams object from all ports.
        """
        groups = {}
        request_level_params = objects.RequestLevelParams()
        extended_res_req = (
            neutron_api.API().has_extended_resource_request_extension(
                ctxt)
        )

        for port in ports:
            resource_request = port.get(constants.RESOURCE_REQUEST)
            if extended_res_req:
                groups[port['id']] = (
                    objects.RequestGroup.from_extended_port_request(
                        ctxt, resource_request
                    )
                )
                request_level_params.extend_with(
                    objects.RequestLevelParams.from_port_request(
                        resource_request
                    )
                )
            else:
                # This is the legacy format, only one group per port and no
                # request level param support
                # TODO(gibi): remove this path once the extended resource
                # request extension is mandatory in neutron
                groups[port['id']] = [
                    objects.RequestGroup.from_port_request(
                        ctxt, port['id'], resource_request
                    )
                ]

        return groups, request_level_params

    @staticmethod
    def _get_port_binding_profile_allocation(
        ctxt: context.RequestContext,
        neutron: neutron_api.ClientWrapper,
        port: ty.Dict[str, ty.Any],
        request_groups: ty.List['objects.RequestGroup'],
        resource_provider_mapping: ty.Dict[str, ty.List[str]],
    ) -> ty.Dict[str, str]:
        """Generate the value of the allocation key of the port binding profile
        based on the provider mapping returned from placement

        :param ctxt: the request context
        :param neutron: the neutron client
        :param port: the port dict from neutron
        :param request_groups: the list of RequestGroups object generated from
            the port resource request
        :param resource_provider_mapping: The dict of request group to resource
            provider mapping returned by the Placement allocation candidate
            query
        :returns: a dict mapping request group ids to resource provider uuids
            in the form as Neutron expects in the port binding profile.
        """
        if neutron_api.API().has_extended_resource_request_extension(
            ctxt, neutron
        ):
            # The extended resource request format also means that a
            # port has more than a one request groups.
            # Each request group id from the port needs to be mapped to
            # a single provider id from the provider mappings. Each
            # group from the port is mapped to a numbered request group
            # in placement so we can assume that they are mapped to
            # a single provider and therefore the provider mapping list
            # has a single provider id.
            allocation = {
                group.requester_id: resource_provider_mapping[
                    group.requester_id][0]
                for group in request_groups
            }
        else:
            # This is the legacy resource request format where a port
            # is mapped to a single request group
            # NOTE(gibi): In the resource provider mapping there can be
            # more than one RP fulfilling a request group. But resource
            # requests of a Neutron port is always mapped to a
            # numbered request group that is always fulfilled by one
            # resource provider. So we only pass that single RP UUID
            # here.
            allocation = resource_provider_mapping[
                port['id']][0]

        return allocation

    def _get_port_allocations_to_heal(
            self, ctxt, instance, node_cache, placement, neutron, output):
        """Return the needed extra allocation for the ports of the instance.

        :param ctxt: nova.context.RequestContext
        :param instance: instance to get the port allocations for
        :param node_cache: dict of Instance.node keys to ComputeNode.uuid
            values; this cache is updated if a new node is processed.
        :param placement: nova.scheduler.client.report.SchedulerReportClient
            to communicate with the Placement service API.
        :param neutron: nova.network.neutron.ClientWrapper to
            communicate with Neutron
        :param output: function that takes a single message for verbose output
        :raise UnableToQueryPorts: If the neutron list ports query fails.
        :raise nova.exception.ComputeHostNotFound: if compute node of the
            instance not found in the db.
        :raise PlacementAPIConnectFailure: if placement API cannot be reached
        :raise AllocationUpdateFailed: if there is either no allocation
            candidate returned from placement for the missing port allocations
            or there are more than one candidates making the healing
            ambiguous.
        :return: A two tuple where the first item is a dict of resources keyed
            by RP uuid to be included in the instance allocation dict. The
            second item is a list of port dicts to be updated in Neutron.
        """
        # We need to heal port allocations for ports that have resource_request
        # but do not have an RP uuid in the binding:profile.allocation field.
        # We cannot use the instance info_cache to check the binding profile
        # as this code needs to be able to handle ports that were attached
        # before nova in stein started updating the allocation key in the
        # binding:profile.
        # In theory a port can be assigned to an instance without it being
        # bound to any host (e.g. in case of shelve offload) but
        # _heal_allocations_for_instance() already filters out instances that
        # are not on any host.
        ports_to_heal = [
            port for port in self._get_ports(ctxt, instance, neutron)
            if self._has_request_but_no_allocation(port, neutron)]

        if not ports_to_heal:
            # nothing to do, return early
            return {}, []

        node_uuid = self._get_compute_node_uuid(
            ctxt, instance, node_cache)

        # NOTE(gibi): We need to handle both legacy and extended resource
        # request. So we need to handle ports with multiple request groups
        # allocating from multiple providers.
        # The logic what we follow here is pretty similar to the logic
        # implemented in ComputeManager._allocate_port_resource_for_instance
        # for the interface attach case. We just apply it to more then one
        # ports here.
        request_groups_per_port, req_lvl_params = (
            self._get_resource_request_from_ports(ctxt, ports_to_heal)
        )
        # flatten the list of list of groups
        request_groups = [
            group
            for groups in request_groups_per_port.values()
            for group in groups
        ]

        # we can have multiple request groups, it would be enough to restrict
        # only one of them to the compute tree but for symmetry we restrict
        # all of them
        for request_group in request_groups:
            request_group.in_tree = node_uuid

        # If there are multiple groups then the group_policy is mandatory in
        # the allocation candidate query. We can assume that if this instance
        # booted successfully then we have the policy in the flavor. If there
        # is only one group and therefore no policy then the value of the
        # policy in the allocation candidate query is ignored, so we simply
        # default it here.
        group_policy = instance.flavor.extra_specs.get("group_policy", "none")

        rr = scheduler_utils.ResourceRequest.from_request_groups(
            request_groups, req_lvl_params, group_policy)
        res = placement.get_allocation_candidates(ctxt, rr)
        # NOTE(gibi): the get_allocation_candidates method has the
        # @safe_connect decorator applied. Such decorator will return None
        # if the connection to Placement is failed. So we raise an exception
        # here. The case when Placement successfully return a response, even
        # if it is a negative or empty response, the method will return a three
        # tuple. That case is handled couple of lines below.
        if not res:
            raise exception.PlacementAPIConnectFailure()
        alloc_reqs, __, __ = res

        if not alloc_reqs:
            port_ids = [port['id'] for port in ports_to_heal]
            raise exception.AllocationUpdateFailed(
                consumer_uuid=instance.uuid,
                error=f'Placement returned no allocation candidate to fulfill '
                      f'the resource request of the port(s) {port_ids}'
            )
        if len(alloc_reqs) > 1:
            # If there is more than one candidates then it is an ambiguous
            # situation that we cannot handle here because selecting the right
            # one might need extra information from the compute node. For
            # example which PCI PF the VF is allocated from and which RP
            # represents that PCI PF in placement.
            # TODO(gibi): One way to get that missing information to resolve
            # ambiguity would be to load up the InstancePciRequest objects and
            # try to use the parent_if_name in their spec to find the proper
            # candidate that allocates for the same port from the PF RP that
            # has the same name.
            port_ids = [port['id'] for port in ports_to_heal]
            raise exception.AllocationUpdateFailed(
                consumer_uuid=instance.uuid,
                error=f'Placement returned more than one possible allocation '
                      f'candidates to fulfill the resource request of the '
                      f'port(s) {port_ids}. This script does not have enough '
                      f'information to select the proper candidate to heal the'
                      f'missing allocations. A possible way to heal the'
                      f'allocation of this instance is to migrate it to '
                      f'another compute as the migration process re-creates '
                      f'the full allocation on the target host.'
            )

        # so we have one candidate, lets use that to get the needed allocations
        # and the provider mapping for the ports' binding profile
        alloc_req = alloc_reqs[0]
        allocations = alloc_req["allocations"]
        provider_mappings = alloc_req["mappings"]

        for port in ports_to_heal:
            # We also need to record the RPs we are allocated from in the
            # port. This will be sent back to Neutron before the allocation
            # is updated in placement
            profile_allocation = self._get_port_binding_profile_allocation(
                ctxt, neutron, port, request_groups_per_port[port['id']],
                provider_mappings
            )
            binding_profile = neutron_api.get_binding_profile(port)
            binding_profile[constants.ALLOCATION] = profile_allocation
            port[constants.BINDING_PROFILE] = binding_profile

            output(_(
                "Found a request group : resource provider mapping "
                "%(mapping)s for the port %(port_uuid)s with resource request "
                "%(request)s attached to the instance %(instance_uuid)s") %
                {"mapping": profile_allocation, "port_uuid": port['id'],
                 "request": port.get(constants.RESOURCE_REQUEST),
                 "instance_uuid": instance.uuid}
            )

        return allocations, ports_to_heal

    def _update_ports(self, neutron, ports_to_update, output):
        succeeded = []
        try:
            for port in ports_to_update:
                profile = neutron_api.get_binding_profile(port)
                body = {
                    'port': {
                        constants.BINDING_PROFILE: profile
                    }
                }
                output(
                    _('Updating port %(port_uuid)s with attributes '
                      '%(attributes)s') %
                      {'port_uuid': port['id'], 'attributes': body['port']})
                neutron.update_port(port['id'], body=body)
                succeeded.append(port)
        except neutron_client_exc.NeutronClientException as e:
            output(
                _('Updating port %(port_uuid)s failed: %(error)s') %
                {'port_uuid': port['id'], 'error': str(e)})
            # one of the port updates failed. We need to roll back the updates
            # that succeeded before
            self._rollback_port_updates(neutron, succeeded, output)
            # we failed to heal so we need to stop but we successfully rolled
            # back the partial updates so the admin can retry the healing.
            raise exception.UnableToUpdatePorts(error=str(e))

    @staticmethod
    def _rollback_port_updates(neutron, ports_to_rollback, output):
        # _update_ports() added the allocation key to these ports, so we need
        # to remove them during the rollback.
        manual_rollback_needed = []
        last_exc = None
        for port in ports_to_rollback:
            profile = neutron_api.get_binding_profile(port)
            profile.pop(constants.ALLOCATION)
            body = {
                'port': {
                    constants.BINDING_PROFILE: profile
                }
            }
            try:
                output(_('Rolling back port update for %(port_uuid)s') %
                       {'port_uuid': port['id']})
                neutron.update_port(port['id'], body=body)
            except neutron_client_exc.NeutronClientException as e:
                output(
                    _('Rolling back update for port %(port_uuid)s failed: '
                      '%(error)s') % {'port_uuid': port['id'],
                                      'error': str(e)})
                # TODO(gibi): We could implement a retry mechanism with
                # back off.
                manual_rollback_needed.append(port['id'])
                last_exc = e

        if manual_rollback_needed:
            # At least one of the port operation failed so we failed to roll
            # back. There are partial updates in neutron. Human intervention
            # needed.
            raise exception.UnableToRollbackPortUpdates(
                error=str(last_exc),
                port_uuids=manual_rollback_needed)

    def _heal_missing_alloc(self, ctxt, instance, node_cache):
        node_uuid = self._get_compute_node_uuid(
            ctxt, instance, node_cache)

        # Now get the resource allocations for the instance based
        # on its embedded flavor.
        resources = scheduler_utils.resources_from_flavor(
            instance, instance.flavor)

        payload = {
            'allocations': {
                node_uuid: {'resources': resources},
            },
            'project_id': instance.project_id,
            'user_id': instance.user_id,
            'consumer_generation': None
        }
        return payload

    def _heal_missing_project_and_user_id(self, allocations, instance):
        allocations['project_id'] = instance.project_id
        allocations['user_id'] = instance.user_id
        return allocations

    @staticmethod
    def ensure_instance_has_no_vgpu_request(instance):
        if instance.flavor.extra_specs.get("resources:VGPU"):
            raise exception.HealvGPUAllocationNotSupported(
                instance_uuid=instance.uuid)

    @staticmethod
    def ensure_instance_has_no_cyborg_device_profile_request(instance):
        if instance.flavor.extra_specs.get("accel:device_profile"):
            raise exception.HealDeviceProfileAllocationNotSupported(
                instance_uuid=instance.uuid)

    def _heal_allocations_for_instance(self, ctxt, instance, node_cache,
                                       output, placement, dry_run,
                                       heal_port_allocations, neutron,
                                       force):
        """Checks the given instance to see if it needs allocation healing

        :param ctxt: cell-targeted nova.context.RequestContext
        :param instance: the instance to check for allocation healing
        :param node_cache: dict of Instance.node keys to ComputeNode.uuid
            values; this cache is updated if a new node is processed.
        :param output: function that takes a single message for verbose output
        :param placement: nova.scheduler.client.report.SchedulerReportClient
            to communicate with the Placement service API.
        :param dry_run: Process instances and print output but do not commit
            any changes.
        :param heal_port_allocations: True if healing port allocation is
            requested, False otherwise.
        :param neutron: nova.network.neutron.ClientWrapper to
            communicate with Neutron
        :param force: True if force healing is requested for particular
            instance, False otherwise.
        :return: True if allocations were created or updated for the instance,
            None if nothing needed to be done
        :raises: nova.exception.ComputeHostNotFound if a compute node for a
            given instance cannot be found
        :raises: AllocationCreateFailed if unable to create allocations for
            a given instance against a given compute node resource provider
        :raises: AllocationUpdateFailed if unable to update allocations for
            a given instance with consumer project/user information
        :raise UnableToQueryPorts: If the neutron list ports query fails.
        :raise PlacementAPIConnectFailure: if placement API cannot be reached
        :raise UnableToUpdatePorts: if a port update failed in neutron but any
            partial update was rolled back successfully.
        :raise UnableToRollbackPortUpdates: if a port update failed in neutron
            and the rollback of the partial updates also failed.
        """
        if instance.task_state is not None:
            output(_('Instance %(instance)s is undergoing a task '
                     'state transition: %(task_state)s') %
                   {'instance': instance.uuid,
                    'task_state': instance.task_state})
            return

        if instance.node is None:
            output(_('Instance %s is not on a host.') % instance.uuid)
            return

        self.ensure_instance_has_no_vgpu_request(instance)
        self.ensure_instance_has_no_cyborg_device_profile_request(instance)

        try:
            allocations = placement.get_allocs_for_consumer(
                ctxt, instance.uuid)
        except (ks_exc.ClientException,
                exception.ConsumerAllocationRetrievalFailed) as e:
            raise exception.AllocationUpdateFailed(
                consumer_uuid=instance.uuid,
                error=_("Allocation retrieval failed: %s") % e)

        need_healing = False

        # Placement response can have an empty {'allocations': {}} in it if
        # there are no allocations for the instance
        if not allocations.get('allocations'):
            # This instance doesn't have allocations
            need_healing = _CREATE
            allocations = self._heal_missing_alloc(ctxt, instance, node_cache)

        if (allocations.get('project_id') != instance.project_id or
                allocations.get('user_id') != instance.user_id):
            # We have an instance with allocations but not the correct
            # project_id/user_id, so we want to update the allocations
            # and re-put them. We don't use put_allocations here
            # because we don't want to mess up shared or nested
            # provider allocations.
            need_healing = _UPDATE
            allocations = self._heal_missing_project_and_user_id(
                allocations, instance)

        if force:
            output(_('Force flag passed for instance %s') % instance.uuid)
            need_healing = _UPDATE
            # get default allocations
            alloc = self._heal_missing_alloc(ctxt, instance, node_cache)
            # set consumer generation of existing allocations
            alloc["consumer_generation"] = allocations["consumer_generation"]
            # set allocations
            allocations = alloc

        if heal_port_allocations:
            to_heal = self._get_port_allocations_to_heal(
                ctxt, instance, node_cache, placement, neutron, output)
            port_allocations, ports_to_update = to_heal
        else:
            port_allocations, ports_to_update = {}, []

        if port_allocations:
            need_healing = need_healing or _UPDATE
            # Merge in any missing port allocations
            allocations['allocations'] = self._merge_allocations(
                allocations['allocations'], port_allocations)

        if need_healing:
            if dry_run:
                # json dump the allocation dict as it contains nested default
                # dicts that is pretty hard to read in the verbose output
                alloc = jsonutils.dumps(allocations)
                if need_healing == _CREATE:
                    output(_('[dry-run] Create allocations for instance '
                             '%(instance)s: %(allocations)s') %
                           {'instance': instance.uuid,
                            'allocations': alloc})
                elif need_healing == _UPDATE:
                    output(_('[dry-run] Update allocations for instance '
                             '%(instance)s: %(allocations)s') %
                           {'instance': instance.uuid,
                            'allocations': alloc})
            else:
                # First update ports in neutron. If any of those operations
                # fail, then roll back the successful part of it and fail the
                # healing. We do this first because rolling back the port
                # updates is more straight-forward than rolling back allocation
                # changes.
                self._update_ports(neutron, ports_to_update, output)

                # Now that neutron update succeeded we can try to update
                # placement. If it fails we need to rollback every neutron port
                # update done before.
                resp = placement.put_allocations(ctxt, instance.uuid,
                                                 allocations)
                if resp:
                    if need_healing == _CREATE:
                        output(_('Successfully created allocations for '
                                 'instance %(instance)s.') %
                               {'instance': instance.uuid})
                    elif need_healing == _UPDATE:
                        output(_('Successfully updated allocations for '
                                 'instance %(instance)s.') %
                               {'instance': instance.uuid})
                    return True
                else:
                    # Rollback every neutron update. If we succeed to
                    # roll back then it is safe to stop here and let the admin
                    # retry. If the rollback fails then
                    # _rollback_port_updates() will raise another exception
                    # that instructs the operator how to clean up manually
                    # before the healing can be retried
                    self._rollback_port_updates(
                        neutron, ports_to_update, output)
                    raise exception.AllocationUpdateFailed(
                        consumer_uuid=instance.uuid, error='')
        else:
            output(_('The allocation of instance %s is up-to-date. '
                     'Nothing to be healed.') % instance.uuid)
            return

    def _heal_instances_in_cell(self, ctxt, max_count, unlimited, output,
                                placement, dry_run, instance_uuid,
                                heal_port_allocations, neutron,
                                force):
        """Checks for instances to heal in a given cell.

        :param ctxt: cell-targeted nova.context.RequestContext
        :param max_count: batch size (limit per instance query)
        :param unlimited: True if all instances in the cell should be
            processed, else False to just process $max_count instances
        :param output: function that takes a single message for verbose output
        :param placement: nova.scheduler.client.report.SchedulerReportClient
            to communicate with the Placement service API.
        :param dry_run: Process instances and print output but do not commit
            any changes.
        :param instance_uuid: UUID of a specific instance to process.
        :param heal_port_allocations: True if healing port allocation is
            requested, False otherwise.
        :param neutron: nova.network.neutron.ClientWrapper to
            communicate with Neutron
        :param force: True if force healing is requested for particular
            instance, False otherwise.
        :return: Number of instances that had allocations created.
        :raises: nova.exception.ComputeHostNotFound if a compute node for a
            given instance cannot be found
        :raises: AllocationCreateFailed if unable to create allocations for
            a given instance against a given compute node resource provider
        :raises: AllocationUpdateFailed if unable to update allocations for
            a given instance with consumer project/user information
        :raise UnableToQueryPorts: If the neutron list ports query fails.
        :raise PlacementAPIConnectFailure: if placement API cannot be reached
        :raise UnableToUpdatePorts: if a port update failed in neutron but any
            partial update was rolled back successfully.
        :raise UnableToRollbackPortUpdates: if a port update failed in neutron
            and the rollback of the partial updates also failed.
        """
        # Keep a cache of instance.node to compute node resource provider UUID.
        # This will save some queries for non-ironic instances to the
        # compute_nodes table.
        node_cache = {}
        # Track the total number of instances that have allocations created
        # for them in this cell. We return when num_processed equals max_count
        # and unlimited=True or we exhaust the number of instances to process
        # in this cell.
        num_processed = 0
        # Get all instances from this cell which have a host and are not
        # undergoing a task state transition. Go from oldest to newest.
        # NOTE(mriedem): Unfortunately we don't have a marker to use
        # between runs where the user is specifying --max-count.
        # TODO(mriedem): Store a marker in system_metadata so we can
        # automatically pick up where we left off without the user having
        # to pass it in (if unlimited is False).
        filters = {'deleted': False}
        if instance_uuid:
            filters['uuid'] = instance_uuid
        instances = objects.InstanceList.get_by_filters(
            ctxt, filters=filters, sort_key='created_at', sort_dir='asc',
            limit=max_count, expected_attrs=['flavor'])
        while instances:
            output(_('Found %s candidate instances.') % len(instances))
            # For each instance in this list, we need to see if it has
            # allocations in placement and if so, assume it's correct and
            # continue.
            for instance in instances:
                if self._heal_allocations_for_instance(
                        ctxt, instance, node_cache, output, placement,
                        dry_run, heal_port_allocations, neutron, force):
                    num_processed += 1

            # Make sure we don't go over the max count. Note that we
            # don't include instances that already have allocations in the
            # max_count number, only the number of instances that have
            # successfully created allocations.
            # If a specific instance was requested we return here as well.
            if (not unlimited and num_processed == max_count) or instance_uuid:
                return num_processed

            # Use a marker to get the next page of instances in this cell.
            # Note that InstanceList doesn't support slice notation.
            marker = instances[len(instances) - 1].uuid
            instances = objects.InstanceList.get_by_filters(
                ctxt, filters=filters, sort_key='created_at', sort_dir='asc',
                limit=max_count, marker=marker, expected_attrs=['flavor'])

        return num_processed

    @action_description(
        _("Iterates over non-cell0 cells looking for instances which do "
          "not have allocations in the Placement service, or have incomplete "
          "consumer project_id/user_id values in existing allocations or "
          "missing allocations for ports having resource request, and "
          "which are not undergoing a task state transition. For each "
          "instance found, allocations are created (or updated) against the "
          "compute node resource provider for that instance based on the "
          "flavor associated with the instance. This command requires that "
          "the [api_database]/connection and [placement] configuration "
          "options are set."))
    @args('--max-count', metavar='<max_count>', dest='max_count',
          help='Maximum number of instances to process. If not specified, all '
               'instances in each cell will be mapped in batches of 50. '
               'If you have a large number of instances, consider specifying '
               'a custom value and run the command until it exits with '
               '0 or 4.')
    @args('--verbose', action='store_true', dest='verbose', default=False,
          help='Provide verbose output during execution.')
    @args('--dry-run', action='store_true', dest='dry_run', default=False,
          help='Runs the command and prints output but does not commit any '
               'changes. The return code should be 4.')
    @args('--instance', metavar='<instance_uuid>', dest='instance_uuid',
          help='UUID of a specific instance to process. If specified '
               '--max-count has no effect. '
               'The --cell and --instance options are mutually exclusive.')
    @args('--skip-port-allocations', action='store_true',
          dest='skip_port_allocations', default=False,
          help='Skip the healing of the resource allocations of bound ports. '
               'E.g. healing bandwidth resource allocation for ports having '
               'minimum QoS policy rules attached. If your deployment does '
               'not use such a feature then the performance impact of '
               'querying neutron ports for each instance can be avoided with '
               'this flag.')
    @args('--cell', metavar='<cell_uuid>', dest='cell_uuid',
          help='Heal allocations within a specific cell. '
               'The --cell and --instance options are mutually exclusive.')
    @args('--force', action='store_true', dest='force', default=False,
          help='Force heal allocations. Requires the --instance argument.')
    def heal_allocations(self, max_count=None, verbose=False, dry_run=False,
                         instance_uuid=None, skip_port_allocations=False,
                         cell_uuid=None, force=False):
        """Heals instance allocations in the Placement service

        Return codes:

        * 0: Command completed successfully and allocations were created.
        * 1: --max-count was reached and there are more instances to process.
        * 2: Unable to find a compute node record for a given instance.
        * 3: Unable to create (or update) allocations for an instance against
             its compute node resource provider.
        * 4: Command completed successfully but no allocations were created.
        * 5: Unable to query ports from neutron
        * 6: Unable to update ports in neutron
        * 7: Cannot roll back neutron port updates. Manual steps needed.
        * 8: Cannot heal instance with vGPU or Cyborg resource request
        * 127: Invalid input.
        """
        # NOTE(mriedem): Thoughts on ways to expand this:
        # - allow filtering on enabled/disabled cells
        # - add a force option to force allocations for instances which have
        #   task_state is not None (would get complicated during a migration);
        #   for example, this could cleanup ironic instances that have
        #   allocations on VCPU/MEMORY_MB/DISK_GB but are now using a custom
        #   resource class
        # - deal with nested resource providers?

        heal_port_allocations = not skip_port_allocations

        output = lambda msg: None
        if verbose:
            output = lambda msg: print(msg)

        # If user has provided both cell and instance
        # Throw an error
        if instance_uuid and cell_uuid:
            print(_('The --cell and --instance options '
                    'are mutually exclusive.'))
            return 127

        if force and not instance_uuid:
            print(_('The --instance flag is required '
                    'when using --force flag.'))
            return 127

        # TODO(mriedem): Rather than --max-count being both a total and batch
        # count, should we have separate options to be specific, i.e. --total
        # and --batch-size? Then --batch-size defaults to 50 and --total
        # defaults to None to mean unlimited.
        if instance_uuid:
            max_count = 1
            unlimited = False
        elif max_count is not None:
            try:
                max_count = int(max_count)
            except ValueError:
                max_count = -1
            unlimited = False
            if max_count < 1:
                print(_('Must supply a positive integer for --max-count.'))
                return 127
        else:
            max_count = 50
            unlimited = True
            output(_('Running batches of %i until complete') % max_count)

        ctxt = context.get_admin_context()
        # If we are going to process a specific instance, just get the cell
        # it is in up front.
        if instance_uuid:
            try:
                im = objects.InstanceMapping.get_by_instance_uuid(
                    ctxt, instance_uuid)
                cells = objects.CellMappingList(objects=[im.cell_mapping])
            except exception.InstanceMappingNotFound:
                print('Unable to find cell for instance %s, is it mapped? Try '
                      'running "nova-manage cell_v2 verify_instance" or '
                      '"nova-manage cell_v2 map_instances".' %
                      instance_uuid)
                return 127
        elif cell_uuid:
            try:
                # validate cell_uuid
                cell = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
                # create CellMappingList
                cells = objects.CellMappingList(objects=[cell])
            except exception.CellMappingNotFound:
                print(_('Cell with uuid %s was not found.') % cell_uuid)
                return 127
        else:
            cells = objects.CellMappingList.get_all(ctxt)
            if not cells:
                output(_('No cells to process.'))
                return 4

        placement = report.report_client_singleton()

        neutron = None
        if heal_port_allocations:
            neutron = neutron_api.get_client(ctxt, admin=True)

        num_processed = 0
        # TODO(mriedem): Use context.scatter_gather_skip_cell0.
        for cell in cells:
            # Skip cell0 since that is where instances go that do not get
            # scheduled and hence would not have allocations against a host.
            if cell.uuid == objects.CellMapping.CELL0_UUID:
                continue
            output(_('Looking for instances in cell: %s') % cell.identity)

            limit_per_cell = max_count
            if not unlimited:
                # Adjust the limit for the next cell. For example, if the user
                # only wants to process a total of 100 instances and we did
                # 75 in cell1, then we only need 25 more from cell2 and so on.
                limit_per_cell = max_count - num_processed

            with context.target_cell(ctxt, cell) as cctxt:
                try:
                    num_processed += self._heal_instances_in_cell(
                        cctxt, limit_per_cell, unlimited, output, placement,
                        dry_run, instance_uuid, heal_port_allocations, neutron,
                        force)
                except exception.ComputeHostNotFound as e:
                    print(e.format_message())
                    return 2
                except (
                    exception.AllocationCreateFailed,
                    exception.AllocationUpdateFailed,
                    exception.PlacementAPIConnectFailure
                ) as e:
                    print(e.format_message())
                    return 3
                except exception.UnableToQueryPorts as e:
                    print(e.format_message())
                    return 5
                except exception.UnableToUpdatePorts as e:
                    print(e.format_message())
                    return 6
                except exception.UnableToRollbackPortUpdates as e:
                    print(e.format_message())
                    return 7
                except (
                    exception.HealvGPUAllocationNotSupported,
                    exception.HealDeviceProfileAllocationNotSupported,
                ) as e:
                    print(e.format_message())
                    return 8

                # Make sure we don't go over the max count. Note that we
                # don't include instances that already have allocations in the
                # max_count number, only the number of instances that have
                # successfully created allocations.
                # If a specific instance was provided then we'll just exit
                # the loop and process it below (either return 4 or 0).
                if num_processed == max_count and not instance_uuid:
                    output(_('Max count reached. Processed %s instances.')
                           % num_processed)
                    return 1

        output(_('Processed %s instances.') % num_processed)
        if not num_processed:
            return 4
        return 0

    @staticmethod
    def _get_rp_uuid_for_host(ctxt, host):
        """Finds the resource provider (compute node) UUID for the given host.

        :param ctxt: cell-targeted nova RequestContext
        :param host: name of the compute host
        :returns: The UUID of the resource provider (compute node) for the host
        :raises: nova.exception.HostMappingNotFound if no host_mappings record
            is found for the host; indicates
            "nova-manage cell_v2 discover_hosts" needs to be run on the cell.
        :raises: nova.exception.ComputeHostNotFound if no compute_nodes record
            is found in the cell database for the host; indicates the
            nova-compute service on that host might need to be restarted.
        :raises: nova.exception.TooManyComputesForHost if there are more than
            one compute_nodes records in the cell database for the host which
            is only possible (under normal circumstances) for ironic hosts but
            ironic hosts are not currently supported with host aggregates so
            if more than one compute node is found for the host, it is
            considered an error which the operator will need to resolve
            manually.
        """
        # Get the host mapping to determine which cell it's in.
        hm = objects.HostMapping.get_by_host(ctxt, host)
        # Now get the compute node record for the host from the cell.
        with context.target_cell(ctxt, hm.cell_mapping) as cctxt:
            # There should really only be one, since only ironic
            # hosts can have multiple nodes, and you can't have
            # ironic hosts in aggregates for that reason. If we
            # find more than one, it's an error.
            nodes = objects.ComputeNodeList.get_all_by_host(
                cctxt, host)

            if len(nodes) > 1:
                # This shouldn't happen, so we need to bail since we
                # won't know which node to use.
                raise exception.TooManyComputesForHost(
                    num_computes=len(nodes), host=host)
            return nodes[0].uuid

    @action_description(
        _("Mirrors compute host aggregates to resource provider aggregates "
          "in the Placement service. Requires the [api_database] and "
          "[placement] sections of the nova configuration file to be "
          "populated."))
    @args('--verbose', action='store_true', dest='verbose', default=False,
          help='Provide verbose output during execution.')
    # TODO(mriedem): Add an option for the 'remove aggregate' behavior.
    # We know that we want to mirror hosts aggregate membership to
    # placement, but regarding removal, what if the operator or some external
    # tool added the resource provider to an aggregate but there is no matching
    # host aggregate, e.g. ironic nodes or shared storage provider
    # relationships?
    # TODO(mriedem): Probably want an option to pass a specific host instead of
    # doing all of them.
    def sync_aggregates(self, verbose=False):
        """Synchronizes nova host aggregates with resource provider aggregates

        Adds nodes to missing provider aggregates in Placement.

        NOTE: Depending on the size of your deployment and the number of
        compute hosts in aggregates, this command could cause a non-negligible
        amount of traffic to the placement service and therefore is
        recommended to be run during maintenance windows.

        Return codes:

        * 0: Successful run
        * 1: A host was found with more than one matching compute node record
        * 2: An unexpected error occurred while working with the placement API
        * 3: Failed updating provider aggregates in placement
        * 4: Host mappings not found for one or more host aggregate members
        * 5: Compute node records not found for one or more hosts
        * 6: Resource provider not found by uuid for a given host
        """
        # Start by getting all host aggregates.
        ctxt = context.get_admin_context()
        aggregate_api = api.AggregateAPI()
        placement = aggregate_api.placement_client
        aggregates = aggregate_api.get_aggregate_list(ctxt)
        # Now we're going to loop over the existing compute hosts in aggregates
        # and check to see if their corresponding resource provider, found via
        # the host's compute node uuid, are in the same aggregate. If not, we
        # add the resource provider to the aggregate in Placement.
        output = lambda msg: None
        if verbose:
            output = lambda msg: print(msg)
        output(_('Filling in missing placement aggregates'))
        # Since hosts can be in more than one aggregate, keep track of the host
        # to its corresponding resource provider uuid to avoid redundant
        # lookups.
        host_to_rp_uuid = {}
        unmapped_hosts = set()  # keep track of any missing host mappings
        computes_not_found = set()  # keep track of missing nodes
        providers_not_found = {}  # map of hostname to missing provider uuid
        for aggregate in aggregates:
            output(_('Processing aggregate: %s') % aggregate.name)
            for host in aggregate.hosts:
                output(_('Processing host: %s') % host)
                rp_uuid = host_to_rp_uuid.get(host)
                if not rp_uuid:
                    try:
                        rp_uuid = self._get_rp_uuid_for_host(ctxt, host)
                        host_to_rp_uuid[host] = rp_uuid
                    except exception.HostMappingNotFound:
                        # Don't fail on this now, we can dump it at the end.
                        unmapped_hosts.add(host)
                        continue
                    except exception.ComputeHostNotFound:
                        # Don't fail on this now, we can dump it at the end.
                        computes_not_found.add(host)
                        continue
                    except exception.TooManyComputesForHost as e:
                        # TODO(mriedem): Should we treat this like the other
                        # errors and not fail immediately but dump at the end?
                        print(e.format_message())
                        return 1

                # We've got our compute node record, so now we can ensure that
                # the matching resource provider, found via compute node uuid,
                # is in the same aggregate in placement, found via aggregate
                # uuid.
                try:
                    placement.aggregate_add_host(ctxt, aggregate.uuid,
                                                 rp_uuid=rp_uuid)
                    output(_('Successfully added host (%(host)s) and '
                             'provider (%(provider)s) to aggregate '
                             '(%(aggregate)s).') %
                           {'host': host, 'provider': rp_uuid,
                            'aggregate': aggregate.uuid})
                except exception.ResourceProviderNotFound:
                    # The resource provider wasn't found. Store this for later.
                    providers_not_found[host] = rp_uuid
                except exception.ResourceProviderAggregateRetrievalFailed as e:
                    print(e.message)
                    return 2
                except exception.NovaException as e:
                    # The exception message is too generic in this case
                    print(_('Failed updating provider aggregates for '
                            'host (%(host)s), provider (%(provider)s) '
                            'and aggregate (%(aggregate)s). Error: '
                            '%(error)s') %
                          {'host': host, 'provider': rp_uuid,
                           'aggregate': aggregate.uuid,
                           'error': e.message})
                    return 3

        # Now do our error handling. Note that there is no real priority on
        # the error code we return. We want to dump all of the issues we hit
        # so the operator can fix them before re-running the command, but
        # whether we return 4 or 5 or 6 doesn't matter.
        return_code = 0
        if unmapped_hosts:
            print(_('The following hosts were found in nova host aggregates '
                    'but no host mappings were found in the nova API DB. Run '
                    '"nova-manage cell_v2 discover_hosts" and then retry. '
                    'Missing: %s') % ','.join(unmapped_hosts))
            return_code = 4

        if computes_not_found:
            print(_('Unable to find matching compute_nodes record entries in '
                    'the cell database for the following hosts; does the '
                    'nova-compute service on each host need to be restarted? '
                    'Missing: %s') % ','.join(computes_not_found))
            return_code = 5

        if providers_not_found:
            print(_('Unable to find matching resource provider record in '
                    'placement with uuid for the following hosts: %s. Try '
                    'restarting the nova-compute service on each host and '
                    'then retry.') %
                  ','.join('(%s=%s)' % (host, providers_not_found[host])
                           for host in sorted(providers_not_found.keys())))
            return_code = 6

        return return_code

    def _get_instances_and_current_migrations(self, ctxt, cn_uuid):
        if self.cn_uuid_mapping.get(cn_uuid):
            cell_uuid, cn_host, cn_node = self.cn_uuid_mapping[cn_uuid]
        else:
            # We need to find the compute node record from all cells.
            results = context.scatter_gather_skip_cell0(
                ctxt, objects.ComputeNode.get_by_uuid, cn_uuid)
            for result_cell_uuid, result in results.items():
                if not context.is_cell_failure_sentinel(result):
                    cn = result
                    cell_uuid = result_cell_uuid
                    break
            else:
                return False
            cn_host, cn_node = (cn.host, cn.hypervisor_hostname)
            self.cn_uuid_mapping[cn_uuid] = (cell_uuid, cn_host, cn_node)
        cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)

        # Get all the active instances from this compute node
        if self.instances_mapping.get(cn_uuid):
            inst_uuids = self.instances_mapping[cn_uuid]
        else:
            # Get the instance list record from the cell.
            with context.target_cell(ctxt, cell_mapping) as cctxt:
                instances = objects.InstanceList.get_by_host_and_node(
                    cctxt, cn_host, cn_node, expected_attrs=[])
            inst_uuids = [instance.uuid for instance in instances]
            self.instances_mapping[cn_uuid] = inst_uuids

        # Get all *active* migrations for this compute node
        # NOTE(sbauza): Since migrations are transient, it's better to not
        # cache the results as they could be stale
        with context.target_cell(ctxt, cell_mapping) as cctxt:
            migs = objects.MigrationList.get_in_progress_by_host_and_node(
                cctxt, cn_host, cn_node)
        mig_uuids = [migration.uuid for migration in migs]

        return (inst_uuids, mig_uuids)

    def _delete_allocations_from_consumer(self, ctxt, placement, provider,
                                          consumer_uuid, consumer_type):
        """Deletes allocations from a resource provider with consumer UUID.

        :param ctxt: nova.context.RequestContext
        :param placement: nova.scheduler.client.report.SchedulerReportClient
            to communicate with the Placement service API.
        :param provider: Resource Provider to look at.
        :param consumer_uuid: the consumer UUID having allocations.
        :param consumer_type: the type of consumer,
            either 'instance' or 'migration'
        :returns: bool whether the allocations were deleted.
        """
        # We need to be careful and only remove the allocations
        # against this specific RP or we would delete the
        # whole instance usage and then it would require some
        # healing.
        # TODO(sbauza): Remove this extra check once placement
        # supports querying allocation delete on both
        # consumer and resource provider parameters.
        allocations = placement.get_allocs_for_consumer(
            ctxt, consumer_uuid)
        if len(allocations['allocations']) > 1:
            # This consumer has resources spread among multiple RPs (think
            # nested or shared for example)
            # We then need to just update the usage to remove
            # the orphaned resources on the specific RP
            del allocations['allocations'][provider['uuid']]
            try:
                placement.put_allocations(
                    ctxt, consumer_uuid, allocations)
            except exception.AllocationUpdateFailed:
                return False

        else:
            try:
                placement.delete_allocation_for_instance(
                    ctxt, consumer_uuid, consumer_type, force=True)
            except exception.AllocationDeleteFailed:
                return False
        return True

    def _check_orphaned_allocations_for_provider(self, ctxt, placement,
                                                 output, provider,
                                                 delete):
        """Finds orphaned allocations for a specific resource provider.

        :param ctxt: nova.context.RequestContext
        :param placement: nova.scheduler.client.report.SchedulerReportClient
            to communicate with the Placement service API.
        :param output: function that takes a single message for verbose output
        :param provider: Resource Provider to look at.
        :param delete: deletes the found orphaned allocations.
        :return: a tuple (<number of orphaned allocs>, <number of faults>)
        """
        num_processed = 0
        faults = 0

        # TODO(sbauza): Are we sure we have all Nova RCs ?
        # FIXME(sbauza): Possibly use consumer types once Placement API
        # supports them.
        # NOTE(sbauza): We check allocations having *any* below RC, not having
        # *all* of them.
        NOVA_RCS = [orc.VCPU, orc.MEMORY_MB, orc.DISK_GB, orc.VGPU,
                    orc.NET_BW_EGR_KILOBIT_PER_SEC,
                    orc.NET_BW_IGR_KILOBIT_PER_SEC,
                    orc.PCPU, orc.MEM_ENCRYPTION_CONTEXT]

        # Since the RP can be a child RP, we need to get the root RP as it's
        # the compute node UUID
        # NOTE(sbauza): In case Placement doesn't support 1.14 microversion,
        # that means we don't have nested RPs.
        # Since we ask for microversion 1.14, all RPs have a root RP UUID.
        cn_uuid = provider.get("root_provider_uuid")
        # Now get all the existing instances and active migrations for this
        # compute node
        result = self._get_instances_and_current_migrations(ctxt, cn_uuid)
        if result is False:
            # We don't want to hard stop here because the compute service could
            # have disappear while we could still have orphaned allocations.
            output(_('The compute node for UUID %s can not be '
                     'found') % cn_uuid)
        inst_uuids, mig_uuids = result or ([], [])
        try:
            pallocs = placement.get_allocations_for_resource_provider(
                ctxt, provider['uuid'])
        except exception.ResourceProviderAllocationRetrievalFailed:
            print(_('Not able to find allocations for resource '
                    'provider %s.') % provider['uuid'])
            raise

        # Verify every allocations for each consumer UUID
        for consumer_uuid, consumer_resources in pallocs.allocations.items():
            consumer_allocs = consumer_resources['resources']
            if any(rc in NOVA_RCS
                   for rc in consumer_allocs):
                # We reset the consumer type for each allocation
                consumer_type = None
                # This is an allocation for Nova resources
                # We need to guess whether the instance was deleted
                # or if the instance is currently migrating
                if not (consumer_uuid in inst_uuids or
                        consumer_uuid in mig_uuids):
                    # By default we suspect the orphaned allocation was for a
                    # migration...
                    consumer_type = 'migration'
                    if not(consumer_uuid in inst_uuids):
                        # ... but if we can't find it either for an instance,
                        # that means it was for this.
                        consumer_type = 'instance'
                if consumer_type is not None:
                    output(_('Allocations were set against consumer UUID '
                             '%(consumer_uuid)s but no existing instances or '
                             'active migrations are related. ')
                           % {'consumer_uuid': consumer_uuid})
                    if delete:
                        deleted = self._delete_allocations_from_consumer(
                            ctxt, placement, provider, consumer_uuid,
                            consumer_type)
                        if not deleted:
                            print(_('Not able to delete allocations '
                                    'for consumer UUID %s')
                                  % consumer_uuid)
                            faults += 1
                            continue
                        output(_('Deleted allocations for consumer UUID '
                                 '%(consumer_uuid)s on Resource Provider '
                                 '%(rp)s: %(allocations)s')
                               % {'consumer_uuid': consumer_uuid,
                                  'rp': provider['uuid'],
                                  'allocations': consumer_allocs})
                    else:
                        output(_('Allocations for consumer UUID '
                                 '%(consumer_uuid)s on Resource Provider '
                                 '%(rp)s can be deleted: '
                                 '%(allocations)s')
                               % {'consumer_uuid': consumer_uuid,
                                  'rp': provider['uuid'],
                                  'allocations': consumer_allocs})
                    num_processed += 1
        return (num_processed, faults)

    # TODO(sbauza): Move this to the scheduler report client ?
    def _get_resource_provider(self, context, placement, uuid):
        """Returns a single Resource Provider by its UUID.

        :param context: The nova.context.RequestContext auth context
        :param placement: nova.scheduler.client.report.SchedulerReportClient
            to communicate with the Placement service API.
        :param uuid: A specific Resource Provider UUID
        :return: the existing resource provider.
        :raises: keystoneauth1.exceptions.base.ClientException on failure to
                 communicate with the placement API
        """

        resource_providers = self._get_resource_providers(context, placement,
                                                          uuid=uuid)
        if not resource_providers:
            # The endpoint never returns a 404, it rather returns an empty list
            raise exception.ResourceProviderNotFound(name_or_uuid=uuid)
        return resource_providers[0]

    def _get_resource_providers(self, context, placement, **kwargs):
        """Returns all resource providers regardless of their relationships.

        :param context: The nova.context.RequestContext auth context
        :param placement: nova.scheduler.client.report.SchedulerReportClient
            to communicate with the Placement service API.
        :param kwargs: extra attributes for the query string
        :return: list of resource providers.
        :raises: keystoneauth1.exceptions.base.ClientException on failure to
                 communicate with the placement API
        """
        url = '/resource_providers'
        if 'uuid' in kwargs:
            url += '?uuid=%s' % kwargs['uuid']

        resp = placement.get(url, global_request_id=context.global_id,
                             version='1.14')
        if resp is None:
            raise exception.PlacementAPIConnectFailure()

        data = resp.json()
        resource_providers = data.get('resource_providers')

        return resource_providers

    @action_description(
        _("Audits orphaned allocations that are no longer corresponding to "
          "existing instance resources. This command requires that "
          "the [api_database]/connection and [placement] configuration "
          "options are set."))
    @args('--verbose', action='store_true', dest='verbose', default=False,
          help='Provide verbose output during execution.')
    @args('--resource_provider', metavar='<provider_uuid>',
          dest='provider_uuid',
          help='UUID of a specific resource provider to verify.')
    @args('--delete', action='store_true', dest='delete', default=False,
          help='Deletes orphaned allocations that were found.')
    def audit(self, verbose=False, provider_uuid=None, delete=False):
        """Provides information about orphaned allocations that can be removed

        Return codes:

        * 0: Command completed successfully and no orphaned allocations exist.
        * 1: An unexpected error happened during run.
        * 3: Orphaned allocations were detected.
        * 4: Orphaned allocations were detected and deleted.
        * 127: Invalid input.
        """

        ctxt = context.get_admin_context()
        output = lambda msg: None
        if verbose:
            output = lambda msg: print(msg)

        placement = report.report_client_singleton()
        # Resets two in-memory dicts for knowing instances per compute node
        self.cn_uuid_mapping = collections.defaultdict(tuple)
        self.instances_mapping = collections.defaultdict(list)

        num_processed = 0
        faults = 0

        if provider_uuid:
            try:
                resource_provider = self._get_resource_provider(
                    ctxt, placement, provider_uuid)
            except exception.ResourceProviderNotFound:
                print(_('Resource provider with UUID %s does not exist.') %
                      provider_uuid)
                return 127
            resource_providers = [resource_provider]
        else:
            resource_providers = self._get_resource_providers(ctxt, placement)

        for provider in resource_providers:
            nb_p, faults = self._check_orphaned_allocations_for_provider(
                ctxt, placement, output, provider, delete)
            num_processed += nb_p
            if faults > 0:
                print(_('The Resource Provider %s had problems when '
                        'deleting allocations. Stopping now. Please fix the '
                        'problem by hand and run again.') %
                      provider['uuid'])
                return 1
        if num_processed > 0:
            suffix = 's.' if num_processed > 1 else '.'
            output(_('Processed %(num)s allocation%(suffix)s')
                   % {'num': num_processed,
                      'suffix': suffix})
            return 4 if delete else 3
        return 0


class LibvirtCommands(object):
    """Commands for managing libvirt instances"""

    @action_description(
        _("Fetch the stored machine type of the instance from the database."))
    @args('instance_uuid', metavar='<instance_uuid>',
          help='UUID of instance to fetch the machine type for')
    def get_machine_type(self, instance_uuid=None):
        """Fetch the stored machine type of the instance from the database.

        Return codes:

        * 0: Command completed successfully.
        * 1: An unexpected error happened.
        * 2: Unable to find instance or instance mapping.
        * 3: No machine type found for the instance.

        """
        try:
            ctxt = context.get_admin_context()
            mtype = machine_type_utils.get_machine_type(ctxt, instance_uuid)
            if mtype:
                print(mtype)
                return 0
            else:
                print(_('No machine type registered for instance %s' %
                        instance_uuid))
                return 3
        except (exception.InstanceNotFound,
                exception.InstanceMappingNotFound) as e:
            print(str(e))
            return 2
        except Exception as e:
            print('Unexpected error, see nova-manage.log for the full '
                  'trace: %s ' % str(e))
            LOG.exception('Unexpected error')
            return 1

    @action_description(
        _("Set or update the stored machine type of the instance in the "
          "database. This is only allowed for instances with a STOPPED, "
          "SHELVED or SHELVED_OFFLOADED vm_state."))
    @args('instance_uuid', metavar='<instance_uuid>',
          help='UUID of instance to update')
    @args('machine_type', metavar='<machine_type>',
          help='Machine type to set')
    @args('--force', action='store_true', default=False, dest='force',
          help='Force the update of the stored machine type')
    def update_machine_type(
        self,
        instance_uuid=None,
        machine_type=None,
        force=False
    ):
        """Set or update the machine type of a given instance.

        Return codes:

        * 0: Command completed successfully.
        * 1: An unexpected error happened.
        * 2: Unable to find the instance or instance cell mapping.
        * 3: Invalid instance vm_state.
        * 4: Unable to move between underlying machine types (pc to q35 etc)
             or to older versions.
        * 5: Unsupported machine type.
        """
        ctxt = context.get_admin_context()
        if force:
            print(_("Forcing update of machine type."))

        try:
            rtype, ptype = machine_type_utils.update_machine_type(
                ctxt, instance_uuid, machine_type, force=force)
        except exception.UnsupportedMachineType as e:
            print(str(e))
            return 5
        except exception.InvalidMachineTypeUpdate as e:
            print(str(e))
            return 4
        except exception.InstanceInvalidState as e:
            print(str(e))
            return 3
        except (
            exception.InstanceNotFound,
            exception.InstanceMappingNotFound,
        ) as e:
            print(str(e))
            return 2
        except Exception as e:
            print('Unexpected error, see nova-manage.log for the full '
                  'trace: %s ' % str(e))
            LOG.exception('Unexpected error')
            return 1

        print(_("Updated instance %(instance_uuid)s machine type to "
                "%(machine_type)s (previously %(previous_type)s)") %
                {'instance_uuid': instance_uuid,
                 'machine_type': rtype,
                 'previous_type': ptype})
        return 0

    @action_description(
        _("List the UUIDs of instances that do not have hw_machine_type set "
          "in their image metadata"))
    @args('--cell-uuid', metavar='<cell_uuid>', dest='cell_uuid',
          required=False, help='UUID of cell from which to list instances')
    def list_unset_machine_type(self, cell_uuid=None):
        """List the UUIDs of instances without image_hw_machine_type set

        Return codes:
        * 0: Command completed successfully, no instances found.
        * 1: An unexpected error happened.
        * 2: Unable to find cell mapping.
        * 3: Instances found without hw_machine_type set.
        """
        try:
            instance_list = machine_type_utils.get_instances_without_type(
                context.get_admin_context(), cell_uuid)
        except exception.CellMappingNotFound as e:
            print(str(e))
            return 2
        except Exception as e:
            print('Unexpected error, see nova-manage.log for the full '
                  'trace: %s ' % str(e))
            LOG.exception('Unexpected error')
            return 1

        if instance_list:
            print('\n'.join(i.uuid for i in instance_list))
            return 3
        else:
            print(_("No instances found without hw_machine_type set."))
            return 0


class VolumeAttachmentCommands(object):

    @action_description(_("Show the details of a given volume attachment."))
    @args(
        'instance_uuid', metavar='<instance_uuid>',
        help='UUID of the instance')
    @args(
        'volume_id', metavar='<volume_id>',
        help='UUID of the volume')
    @args(
        '--connection_info', action='store_true',
        default=False, dest='connection_info', required=False,
        help='Only display the connection_info of the volume attachment.')
    @args(
        '--json', action='store_true',
        default=False, dest='json', required=False,
        help='Display output as json without a table.')
    def show(
        self,
        instance_uuid=None,
        volume_id=None,
        connection_info=False,
        json=False
    ):
        """Show attributes of a given volume attachment.

        Return codes:
        * 0: Command completed successfully.
        * 1: An unexpected error happened.
        * 2: Instance not found.
        * 3: Volume is not attached to instance.
        """
        try:
            ctxt = context.get_admin_context()
            im = objects.InstanceMapping.get_by_instance_uuid(
                ctxt, instance_uuid)
            with context.target_cell(ctxt, im.cell_mapping) as cctxt:
                bdm = objects.BlockDeviceMapping.get_by_volume_and_instance(
                    cctxt, volume_id, instance_uuid)
                if connection_info and json:
                    print(bdm.connection_info)
                elif connection_info:
                    print(format_dict(jsonutils.loads(bdm.connection_info)))
                elif json:
                    print(jsonutils.dumps(bdm))
                else:
                    print(format_dict(bdm))
                return 0
        except exception.VolumeBDMNotFound as e:
            print(str(e))
            return 3
        except (
            exception.InstanceNotFound,
            exception.InstanceMappingNotFound,
        ) as e:
            print(str(e))
            return 2
        except Exception as e:
            print('Unexpected error, see nova-manage.log for the full '
                  'trace: %s ' % str(e))
            LOG.exception('Unexpected error')
            return 1

    @action_description(_('Show the host connector for this host'))
    @args(
        '--json', action='store_true',
        default=False, dest='json', required=False,
        help='Display output as json without a table.')
    def get_connector(self, json=False):
        """Show the host connector for this host.

        Return codes:
        * 0: Command completed successfully.
        * 1: An unexpected error happened.
        """
        try:
            root_helper = utils.get_root_helper()
            host_connector = connector.get_connector_properties(
                root_helper, CONF.my_block_storage_ip,
                CONF.libvirt.volume_use_multipath,
                enforce_multipath=True,
                host=CONF.host)
            if json:
                print(jsonutils.dumps(host_connector))
            else:
                print(format_dict(host_connector))
            return 0
        except Exception as e:
            print('Unexpected error, see nova-manage.log for the full '
                  'trace: %s ' % str(e))
            LOG.exception('Unexpected error')
            return 1

    def _refresh(self, instance_uuid, volume_id, connector):
        """Refresh the bdm.connection_info associated with a volume attachment

        Unlike the current driver BDM implementation under
        nova.virt.block_device.DriverVolumeBlockDevice.refresh_connection_info
        that simply GETs an existing volume attachment from cinder this method
        cleans up any existing volume connections from the host before creating
        a fresh attachment in cinder and populates the underlying BDM with
        connection_info from the new attachment.

        We can do that here as the command requires that the instance is
        stopped, something that isn't always the case with the current driver
        BDM approach and thus the two are kept seperate for the time being.

        :param instance_uuid: UUID of instance
        :param volume_id: ID of volume attached to the instance
        :param connector: Connector with which to create the new attachment
        """
        volume_api = cinder.API()
        compute_rpcapi = rpcapi.ComputeAPI()
        compute_api = api.API()

        ctxt = context.get_admin_context()
        im = objects.InstanceMapping.get_by_instance_uuid(ctxt, instance_uuid)
        with context.target_cell(ctxt, im.cell_mapping) as cctxt:

            instance = objects.Instance.get_by_uuid(cctxt, instance_uuid)
            bdm = objects.BlockDeviceMapping.get_by_volume_and_instance(
                    cctxt, volume_id, instance_uuid)

            if instance.vm_state != obj_fields.InstanceState.STOPPED:
                raise exception.InstanceInvalidState(
                    instance_uuid=instance_uuid, attr='vm_state',
                    state=instance.vm_state,
                    method='refresh connection_info (must be stopped)')

            if instance.locked:
                raise exception.InstanceInvalidState(
                    instance_uuid=instance_uuid, attr='locked', state='True',
                    method='refresh connection_info (must be unlocked)')

            compute_api.lock(
                cctxt, instance,
                reason=(
                    f'Refreshing connection_info for BDM {bdm.uuid} '
                    f'associated with instance {instance_uuid} and volume '
                    f'{volume_id}.'))

        # NOTE(lyarwood): Yes this is weird but we need to recreate the admin
        # context here to ensure the lock above uses a unique request-id
        # versus the following refresh and eventual unlock.
        ctxt = context.get_admin_context()
        with context.target_cell(ctxt, im.cell_mapping) as cctxt:
            instance_action = None
            new_attachment_id = None
            try:
                # Log this as an instance action so operators and users are
                # aware that this has happened.
                instance_action = objects.InstanceAction.action_start(
                    cctxt, instance_uuid,
                    instance_actions.NOVA_MANAGE_REFRESH_VOLUME_ATTACHMENT)

                # Create a blank attachment to keep the volume reserved
                new_attachment_id = volume_api.attachment_create(
                    cctxt, volume_id, instance_uuid)['id']

                # RPC call to the compute to cleanup the connections, which
                # will in turn unmap the volume from the compute host
                # TODO(lyarwood): Add delete_attachment as a kwarg to
                # remove_volume_connection as is available in the private
                # method within the manager.
                compute_rpcapi.remove_volume_connection(
                    cctxt, instance, volume_id, instance.host)

                # Delete the existing volume attachment if present in the bdm.
                # This isn't present when the original attachment was made
                # using the legacy cinderv2 APIs before the cinderv3 attachment
                # based APIs were present.
                if bdm.attachment_id:
                    volume_api.attachment_delete(cctxt, bdm.attachment_id)

                # Update the attachment with host connector, this regenerates
                # the connection_info that we can now stash in the bdm.
                new_connection_info = volume_api.attachment_update(
                    cctxt, new_attachment_id, connector,
                    bdm.device_name)['connection_info']

                # Before we save it to the BDM ensure the serial is stashed as
                # is done in various other codepaths when attaching volumes.
                if 'serial' not in new_connection_info:
                    new_connection_info['serial'] = bdm.volume_id

                # Save the new attachment id and connection_info to the DB
                bdm.attachment_id = new_attachment_id
                bdm.connection_info = jsonutils.dumps(new_connection_info)
                bdm.save()

                # Finally mark the attachment as complete, moving the volume
                # status from attaching to in-use ahead of the instance
                # restarting
                volume_api.attachment_complete(cctxt, new_attachment_id)
                return 0

            finally:
                # If the bdm.attachment_id wasn't updated make sure we clean
                # up any attachments created during the run.
                bdm = objects.BlockDeviceMapping.get_by_volume_and_instance(
                    cctxt, volume_id, instance_uuid)
                if (
                    new_attachment_id and
                    bdm.attachment_id != new_attachment_id
                ):
                    volume_api.attachment_delete(cctxt, new_attachment_id)

                # If we failed during attachment_update the bdm.attachment_id
                # has already been deleted so recreate it now to ensure the
                # volume is still associated with the instance and clear the
                # now stale connection_info.
                try:
                    volume_api.attachment_get(cctxt, bdm.attachment_id)
                except exception.VolumeAttachmentNotFound:
                    bdm.attachment_id = volume_api.attachment_create(
                        cctxt, volume_id, instance_uuid)['id']
                    bdm.connection_info = None
                    bdm.save()

                # Finish the instance action if it was created and started
                # TODO(lyarwood): While not really required we should store
                # the exec and traceback in here on failure.
                if instance_action:
                    instance_action.finish()

                # NOTE(lyarwood): As above we need to unlock the instance with
                # a fresh context and request-id to keep it unique. It's safe
                # to assume that the instance is locked as this point as the
                # earlier call to lock isn't part of this block.
                with context.target_cell(
                    context.get_admin_context(),
                    im.cell_mapping
                ) as u_cctxt:
                    compute_api.unlock(u_cctxt, instance)

    @action_description(
        _("Refresh the connection info for a given volume attachment"))
    @args(
        'instance_uuid', metavar='<instance_uuid>',
        help='UUID of the instance')
    @args(
        'volume_id', metavar='<volume_id>',
        help='UUID of the volume')
    @args(
        'connector_path', metavar='<connector_path>',
        help='Path to file containing the host connector in json format.')
    def refresh(self, instance_uuid=None, volume_id=None, connector_path=None):
        """Refresh the connection_info associated with a volume attachment

        Return codes:
        * 0: Command completed successfully.
        * 1: An unexpected error happened.
        * 2: Connector path does not exist.
        * 3: Failed to open connector path.
        * 4: Instance does not exist.
        * 5: Instance state invalid.
        * 6: Volume is not attached to instance.
        """
        try:
            # TODO(lyarwood): Make this optional and provide a rpcapi capable
            # of pulling this down from the target compute during this flow.
            if not os.path.exists(connector_path):
                raise exception.InvalidInput(
                    reason=f'Connector file not found at {connector_path}')

            # Read in the json connector file
            with open(connector_path, 'rb') as connector_file:
                connector = jsonutils.load(connector_file)

            # Refresh the volume attachment
            return self._refresh(instance_uuid, volume_id, connector)

        except exception.VolumeBDMNotFound as e:
            print(str(e))
            return 6
        except exception.InstanceInvalidState as e:
            print(str(e))
            return 5
        except (
            exception.InstanceNotFound,
            exception.InstanceMappingNotFound,
        ) as e:
            print(str(e))
            return 4
        except (ValueError, OSError):
            print(
                f'Failed to open {connector_path}. Does it contain valid '
                f'connector_info data?'
            )
            return 3
        except exception.InvalidInput as e:
            print(str(e))
            return 2
        except Exception as e:
            print('Unexpected error, see nova-manage.log for the full '
                  'trace: %s ' % str(e))
            LOG.exception('Unexpected error')
            return 1


class ImagePropertyCommands:

    @action_description(_("Show the value of an instance image property."))
    @args(
        'instance_uuid', metavar='<instance_uuid>',
        help='UUID of the instance')
    @args(
        'property', metavar='<image_property>',
        help='Image property to show')
    def show(self, instance_uuid=None, image_property=None):
        """Show value of a given instance image property.

        Return codes:
        * 0: Command completed successfully.
        * 1: An unexpected error happened.
        * 2: Instance not found.
        * 3: Image property not found.
        """
        try:
            ctxt = context.get_admin_context()
            im = objects.InstanceMapping.get_by_instance_uuid(
                ctxt, instance_uuid)
            with context.target_cell(ctxt, im.cell_mapping) as cctxt:
                instance = objects.Instance.get_by_uuid(
                    cctxt, instance_uuid, expected_attrs=['system_metadata'])
                image_property = instance.system_metadata.get(
                    f'image_{image_property}')
                if image_property:
                    print(image_property)
                    return 0
                else:
                    print(f'Image property {image_property} not found '
                          f'for instance {instance_uuid}.')
                    return 3
        except (
            exception.InstanceNotFound,
            exception.InstanceMappingNotFound,
        ) as e:
            print(str(e))
            return 2
        except Exception as e:
            print(f'Unexpected error, see nova-manage.log for the full '
                  f'trace: {str(e)}')
            LOG.exception('Unexpected error')
            return 1

    def _validate_image_properties(self, image_properties):
        """Validate the provided image property names and values

        :param image_properties: List of image property names and values
        """
        # Sanity check the format of the provided properties, this should be
        # in the format of name=value.
        if any(x for x in image_properties if '=' not in x):
            raise exception.InvalidInput(
                "--property should use the format key=value")

        # Transform the list of delimited properties to a dict
        image_properties = dict(prop.split('=') for prop in image_properties)

        # Validate the names of each property by checking against the o.vo
        # fields currently listed by ImageProps. We can't use from_dict to
        # do this as it silently ignores invalid property keys.
        for image_property_name in image_properties.keys():
            if image_property_name not in objects.ImageMetaProps.fields:
                raise exception.InvalidImagePropertyName(
                    image_property_name=image_property_name)

        # Validate the values by creating an object from the provided dict.
        objects.ImageMetaProps.from_dict(image_properties)

        # Return the dict so we can update the instance system_metadata
        return image_properties

    def _update_image_properties(self, instance, image_properties):
        """Update instance image properties

        :param instance: The instance to update
        :param image_properties: List of image properties and values to update
        """
        # Check the state of the instance
        allowed_states = [
            obj_fields.InstanceState.STOPPED,
            obj_fields.InstanceState.SHELVED,
            obj_fields.InstanceState.SHELVED_OFFLOADED,
        ]
        if instance.vm_state not in allowed_states:
            raise exception.InstanceInvalidState(
                instance_uuid=instance.uuid, attr='vm_state',
                state=instance.vm_state,
                method='image_property set (must be STOPPED, SHELVED, OR '
                       'SHELVED_OFFLOADED).')

        # Validate the property names and values
        image_properties = self._validate_image_properties(image_properties)

        # Update the image properties and save the instance record
        for image_property, value in image_properties.items():
            instance.system_metadata[f'image_{image_property}'] = value

        # Save and return 0
        instance.save()
        return 0

    @action_description(_(
        "Set the values of instance image properties stored in the database. "
        "This is only allowed for " "instances with a STOPPED, SHELVED or "
        "SHELVED_OFFLOADED vm_state."))
    @args(
        'instance_uuid', metavar='<instance_uuid>',
        help='UUID of the instance')
    @args(
        '--property', metavar='<image_property>', action='append',
        dest='image_properties',
        help='Image property to set using the format name=value. For example: '
             '--property hw_disk_bus=virtio --property hw_cdrom_bus=sata')
    def set(self, instance_uuid=None, image_properties=None):
        """Set instance image property values

        Return codes:
        * 0: Command completed successfully.
        * 1: An unexpected error happened.
        * 2: Unable to find instance.
        * 3: Instance is in an invalid state.
        * 4: Invalid input format.
        * 5: Invalid image property name.
        * 6: Invalid image property value.
        """
        try:
            ctxt = context.get_admin_context()
            im = objects.InstanceMapping.get_by_instance_uuid(
                ctxt, instance_uuid)
            with context.target_cell(ctxt, im.cell_mapping) as cctxt:
                instance = objects.Instance.get_by_uuid(
                    cctxt, instance_uuid, expected_attrs=['system_metadata'])
                return self._update_image_properties(
                    instance, image_properties)
        except ValueError as e:
            print(str(e))
            return 6
        except exception.InvalidImagePropertyName as e:
            print(str(e))
            return 5
        except exception.InvalidInput as e:
            print(str(e))
            return 4
        except exception.InstanceInvalidState as e:
            print(str(e))
            return 3
        except (
            exception.InstanceNotFound,
            exception.InstanceMappingNotFound,
        ) as e:
            print(str(e))
            return 2
        except Exception as e:
            print('Unexpected error, see nova-manage.log for the full '
                  'trace: %s ' % str(e))
            LOG.exception('Unexpected error')
            return 1


CATEGORIES = {
    'api_db': ApiDbCommands,
    'cell_v2': CellV2Commands,
    'db': DbCommands,
    'placement': PlacementCommands,
    'libvirt': LibvirtCommands,
    'volume_attachment': VolumeAttachmentCommands,
    'image_property': ImagePropertyCommands,
}


add_command_parsers = functools.partial(cmd_common.add_command_parsers,
                                        categories=CATEGORIES)


category_opt = cfg.SubCommandOpt('category',
                                 title='Command categories',
                                 help='Available categories',
                                 handler=add_command_parsers)

post_mortem_opt = cfg.BoolOpt('post-mortem',
                              default=False,
                              help='Allow post-mortem debugging')


def main():
    """Parse options and call the appropriate class/method."""
    CONF.register_cli_opts([category_opt, post_mortem_opt])
    config.parse_args(sys.argv)
    logging.set_defaults(
        default_log_levels=logging.get_default_log_levels() +
        _EXTRA_DEFAULT_LOG_LEVELS)
    logging.setup(CONF, "nova")
    objects.register_all()

    if CONF.category.name == "version":
        print(version.version_string_with_package())
        return 0

    if CONF.category.name == "bash-completion":
        cmd_common.print_bash_completion(CATEGORIES)
        return 0

    try:
        fn, fn_args, fn_kwargs = cmd_common.get_action_fn()
        ret = fn(*fn_args, **fn_kwargs)
        rpc.cleanup()
        return ret
    except Exception:
        if CONF.post_mortem:
            import pdb
            pdb.post_mortem()
        else:
            print(_("An error has occurred:\n%s") % traceback.format_exc())
        return 255