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
|
# Copyright (c) 2015 SUSE Linux GmbH. All rights reserved.
#
# This file is part of kiwi.
#
# kiwi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# kiwi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with kiwi. If not, see <http://www.gnu.org/licenses/>
#
import os
from typing import (
List, Optional, Any, Dict, NamedTuple, Callable, Union
)
import re
import logging
import copy
from textwrap import dedent
# project
import kiwi.defaults as defaults
from kiwi import xml_parse
from kiwi.storage.disk import ptable_entry_type
from kiwi.system.uri import Uri
from kiwi.defaults import Defaults
from kiwi.utils.size import StringToSize
from kiwi.command import Command
from kiwi.exceptions import (
KiwiProfileNotFound,
KiwiTypeNotFound,
KiwiDistributionNameError,
KiwiFileAccessError
)
log = logging.getLogger('kiwi')
description_type = NamedTuple(
'description_type', [
('author', str),
('contact', str),
('specification', str)
]
)
package_type = NamedTuple(
'package_type', [
('packages_section', xml_parse.packages),
('package_section', xml_parse.package)
]
)
size_type = NamedTuple(
'size_type', [
('mbytes', int),
('additive', str)
]
)
volume_type = NamedTuple(
'volume_type', [
('name', str),
('parent', str),
('size', str),
('realpath', str),
('mountpoint', Optional[str]),
('fullsize', bool),
('label', Optional[str]),
('attributes', list),
('is_root_volume', bool)
]
)
class DracutT(NamedTuple):
uefi: bool
modules: List[str]
drivers: List[str]
class FileT(NamedTuple):
target: str
owner: str
permissions: str
class ContainerT(NamedTuple):
name: str
backend: str
container_file: str
fetch_only: bool
fetch_command: Callable
load_command: List[str]
class XMLState:
"""
**Implements methods to get stateful information from the XML data**
:param object xml_data: parse result from XMLDescription.load()
:param list profiles: list of used profiles
:param object build_type: build <type> section reference
"""
def __init__(
self, xml_data: Any, profiles: List = None,
build_type: Any = None
):
self.root_partition_uuid: Optional[str] = None
self.root_filesystem_uuid: Optional[str] = None
self.host_architecture = defaults.PLATFORM_MACHINE
self.xml_data = xml_data
self.profiles = self._used_profiles(profiles)
self.build_type = self._build_type_section(
build_type
)
self.resolve_this_path()
def get_preferences_sections(self) -> List:
"""
All preferences sections for the selected profiles that match the
host architecture
:return: list of <preferences> section reference(s)
:rtype: list
"""
preferences_list = []
for preferences in self._profiled(self.xml_data.get_preferences()):
if self.preferences_matches_host_architecture(preferences):
preferences_list.append(preferences)
return preferences_list
def get_description_section(self) -> description_type:
"""
The description section
:return:
description_type tuple providing the elements
author contact and specification
:rtype: tuple
"""
description = self.xml_data.get_description()[0]
return description_type(
author=description.get_author()[0],
contact=description.get_contact()[0],
specification=description.get_specification()[0].strip()
)
def get_users_sections(self) -> List:
"""
All users sections for the selected profiles
:return: list of <users> section reference(s)
:rtype: list
"""
users = []
for users_section in self._profiled(self.xml_data.get_users()):
if self.users_matches_host_architecture(users_section):
users.append(users_section)
return users
def get_build_type_bundle_format(self) -> str:
"""
Return bundle_format for build type
The bundle_format string is validated against the available
name tags from kiwi.system.result::result_name_tags.
:return: bundle format string
:rtype: str
"""
return self.build_type.get_bundle_format()
def get_build_type_name(self) -> str:
"""
Default build type name
:return: Content of image attribute from build type
:rtype: str
"""
return self.build_type.get_image()
def btrfs_default_volume_requested(self) -> bool:
"""
Check if setting a default volume for btrfs is requested
"""
if self.build_type.get_btrfs_set_default_volume() is False:
# Setting a default volume is explicitly switched off
return False
else:
# In any other case (True | None) a default volume
# is wanted and will be set
return True
def get_image_version(self) -> str:
"""
Image version from preferences section.
Multiple occurences of version in preferences sections are not
forbidden, however only the first version found defines the
final image version
:return: Content of <version> section
:rtype: str
"""
for preferences in self.get_preferences_sections():
version = preferences.get_version()
if version:
return version[0]
return ''
def get_initrd_system(self) -> str:
"""
Name of initrd system to use
Depending on the image type a specific initrd system is
either pre selected or free of choice according to the
XML type setup.
:return: 'dracut', 'kiwi' or 'none'
:rtype: str
"""
pre_selection_map = {
'vmx': 'dracut',
'oem': 'dracut',
'iso': 'dracut',
'kis': 'dracut',
'pxe': 'kiwi',
}
build_type = self.get_build_type_name()
default_initrd_system = pre_selection_map.get(build_type) or 'none'
if build_type == 'iso':
# iso type always use dracut as initrd system
return default_initrd_system
# Allow to choose for any other build type
return self.build_type.get_initrd_system() or default_initrd_system
def get_locale(self) -> Optional[List]:
"""
Gets list of locale names if configured. Takes
the first locale setup from the existing preferences
sections into account.
:return: List of names or None
:rtype: list|None
"""
for preferences in self.get_preferences_sections():
locale_section = preferences.get_locale()
if locale_section:
return locale_section[0].split(',')
return None
def get_rpm_locale(self) -> Optional[List]:
"""
Gets list of locale names to filter out by rpm
if rpm-locale-filtering is switched on the
the list always contains: [POSIX, C, C.UTF-8]
and is extended by the optionaly configured
locale
:return: List of names or None
:rtype: list|None
"""
if self.get_rpm_locale_filtering():
rpm_locale = ['POSIX', 'C', 'C.UTF-8']
configured_locale = self.get_locale()
if configured_locale:
for locale in configured_locale:
rpm_locale.append(locale)
return rpm_locale
return None
def get_rpm_locale_filtering(self) -> bool:
"""
Gets the rpm-locale-filtering configuration flag. Returns
False if not present.
:return: True or False
:rtype: bool
"""
for preferences in self.get_preferences_sections():
locale_filtering = preferences.get_rpm_locale_filtering()
if locale_filtering:
return locale_filtering[0]
return False
def get_rpm_excludedocs(self) -> bool:
"""
Gets the rpm-excludedocs configuration flag. Returns
False if not present.
:return: True or False
:rtype: bool
"""
for preferences in self.get_preferences_sections():
exclude_docs = preferences.get_rpm_excludedocs()
if exclude_docs:
return exclude_docs[0]
return False
def get_rpm_check_signatures(self) -> bool:
"""
Gets the rpm-check-signatures configuration flag. Returns
False if not present.
:return: True or False
:rtype: bool
"""
for preferences in self.get_preferences_sections():
check_signatures = preferences.get_rpm_check_signatures()
if check_signatures:
return check_signatures[0]
return False
def get_package_manager(self) -> str:
"""
Get configured package manager from selected preferences section
:return: Content of the <packagemanager> section
:rtype: str
"""
for preferences in self.get_preferences_sections():
package_manager = preferences.get_packagemanager()
if package_manager:
return package_manager[0]
return Defaults.get_default_package_manager()
def get_release_version(self) -> str:
"""
Get configured release version from selected preferences section
:return: Content of the <release-version> section or ''
:rtype: str
"""
release_version = ''
for preferences in self.get_preferences_sections():
release_version = preferences.get_release_version()
if release_version:
release_version = release_version[0]
break
return release_version
def get_packages_sections(self, section_types: List) -> List:
"""
List of packages sections matching given section type(s)
:param list section_types: type name(s) from packages sections
:return: list of <packages> section reference(s)
:rtype: list
"""
result = []
packages_sections = self._profiled(
self.xml_data.get_packages()
)
for packages in packages_sections:
packages_type = packages.get_type()
if packages_type in section_types:
result.append(packages)
return result
def volume_matches_host_architecture(self, volume: Any) -> bool:
"""
Tests if the given volume section is applicable for the current host
architecture. If no architecture is specified within the section
it is considered as a match returning True.
Note: The XML section pointer must provide an arch attribute
:param section: XML section object
:return: True or False
:rtype: bool
"""
return self._section_matches_host_architecture(volume)
def package_matches_host_architecture(self, package: Any) -> bool:
"""
Tests if the given package section is applicable for the current host
architecture. If no architecture is specified within the section
it is considered as a match returning True.
Note: The XML section pointer must provide an arch attribute
:param section: XML section object
:return: True or False
:rtype: bool
"""
return self._section_matches_host_architecture(package)
def users_matches_host_architecture(self, users: Any) -> bool:
"""
Tests if the given users section is applicable for the current host
architecture. If no architecture is specified within the section
it is considered as a match returning True.
Note: The XML section pointer must provide an arch attribute
:param section: XML section object
:return: True or False
:rtype: bool
"""
return self._section_matches_host_architecture(users)
def collection_matches_host_architecture(self, collection: Any) -> bool:
"""
Tests if the given namedcollection section is applicable for
the current host architecture. If no architecture is specified
within the section it is considered as a match returning True.
Note: The XML section pointer must provide an arch attribute
:param section: XML section object
:return: True or False
:rtype: bool
"""
return self._section_matches_host_architecture(collection)
def profile_matches_host_architecture(self, profile: Any) -> bool:
"""
Tests if the given profile section is applicable for the current host
architecture. If no architecture is specified within the section
it is considered as a match returning True.
Note: The XML section pointer must provide an arch attribute
:param section: XML section object
:return: True or False
:rtype: bool
"""
return self._section_matches_host_architecture(profile)
def preferences_matches_host_architecture(self, preferences: Any) -> bool:
"""
Tests if the given preferences section is applicable for the
current host architecture. If no architecture is specified within
the section it is considered as a match returning True.
Note: The XML section pointer must provide an arch attribute
:param section: XML section object
:return: True or False
:rtype: bool
"""
return self._section_matches_host_architecture(preferences)
def repository_matches_host_architecture(self, repository: Any) -> bool:
"""
Tests if the given repository section is applicable for the
current host architecture. If no architecture is specified within
the section it is considered as a match returning True.
Note: The XML section pointer must provide an arch attribute
:param section: XML section object
:return: True or False
:rtype: bool
"""
return self._section_matches_host_architecture(repository)
def containers_matches_host_architecture(self, containers: Any) -> bool:
"""
Tests if the given containers section is applicable for the
current host architecture. If no arch attribute is provided in
the section it is considered as a match and returns: True.
:param section: XML section object
:return: True or False
:rtype: bool
"""
return self._section_matches_host_architecture(containers)
def container_matches_host_architecture(self, container: Any) -> bool:
"""
Tests if the given container section is applicable for the
current host architecture. If no arch attribute is provided in
the section it is considered as a match and returns: True.
:param section: XML section object
:return: True or False
:rtype: bool
"""
return self._section_matches_host_architecture(container)
def get_package_sections(
self, packages_sections: List
) -> List[package_type]:
"""
List of package sections from the given packages sections.
Each list element contains a tuple with the <package> section
reference and the <packages> section this package belongs to
If a package entry specfies an architecture, it is only taken if
the host architecture matches the configured architecture
:param list packages_sections: <packages>
:return:
Contains list of package_type tuples
.. code:: python
[package_type(packages_section=object, package_section=object)]
:rtype: list
"""
result = []
if packages_sections:
for packages_section in packages_sections:
package_list = packages_section.get_package()
if package_list:
for package in package_list:
if self.package_matches_host_architecture(package):
result.append(
package_type(
packages_section=packages_section,
package_section=package
)
)
return result
def get_to_become_deleted_packages(self, force: bool = True) -> List:
"""
List of package names from the type="delete" or type="uninstall"
packages section(s)
:param bool force: return "delete" type if True, "uninstall" type
otherwise
:return: package names
:rtype: list
"""
result = []
to_become_deleted_packages_sections = self.get_packages_sections(
['delete' if force else 'uninstall']
)
package_list = self.get_package_sections(
to_become_deleted_packages_sections
)
if package_list:
for package in package_list:
result.append(package.package_section.get_name())
return sorted(list(set(result)))
def get_bootstrap_packages_sections(self) -> List:
"""
List of packages sections matching type="bootstrap"
:return: list of <packages> section reference(s)
:rtype: list
"""
return self.get_packages_sections(['bootstrap'])
def get_image_packages_sections(self) -> List:
"""
List of packages sections matching type="image"
:return: list of <packages> section reference(s)
:rtype: list
"""
return self.get_packages_sections(['image'])
def get_bootstrap_packages(self, plus_packages: List = None) -> List:
"""
List of package names from the type="bootstrap" packages section(s)
The list gets the selected package manager appended
if there is a request to install packages inside of
the image via a chroot operation
:param list plus_packages: list of additional packages
:return: package names
:rtype: list
"""
result = []
bootstrap_packages_sections = self.get_bootstrap_packages_sections()
package_list = self.get_package_sections(
bootstrap_packages_sections
)
if package_list:
for package in package_list:
result.append(package.package_section.get_name().strip())
if self.get_system_packages():
package_manager_name = self.get_package_manager()
if package_manager_name == 'dnf4':
# The package name for dnf4 is just dnf. Thus
# the name must be adapted in this case
package_manager_name = 'dnf'
elif package_manager_name == 'apk':
package_manager_name = 'apk-tools'
result.append(package_manager_name)
if plus_packages:
result += plus_packages
return sorted(list(set(result)))
def get_system_packages(self) -> List:
"""
List of package names from the packages sections matching
type="image" and type=build_type
:return: package names
:rtype: list
"""
result = []
image_packages_sections = self.get_packages_sections(
['image', self.get_build_type_name()]
)
package_list = self.get_package_sections(
image_packages_sections
)
if package_list:
for package in package_list:
result.append(package.package_section.get_name().strip())
return sorted(list(set(result)))
def get_bootstrap_files(self) -> Dict[str, FileT]:
"""
List of file names from the type="bootstrap" packages section(s)
:return: file names
:rtype: dict
"""
result = {}
bootstrap_packages_sections = self.get_bootstrap_packages_sections()
if bootstrap_packages_sections:
for bootstrap_packages_section in bootstrap_packages_sections:
file_list = bootstrap_packages_section.get_file() or []
for file in file_list:
result[file.get_name()] = FileT(
target=file.get_target() or '',
owner=file.get_owner() or '',
permissions=file.get_permissions() or ''
)
return result
def get_system_files(self) -> Dict[str, FileT]:
"""
List of file names from the packages sections matching
type="image" and type=build_type
:return: file names
:rtype: dict
"""
result = {}
image_packages_sections = self.get_packages_sections(
['image', self.get_build_type_name()]
)
for packages in image_packages_sections:
for file in packages.get_file():
result[file.get_name()] = FileT(
target=file.get_target() or '',
owner=file.get_owner() or '',
permissions=file.get_permissions() or ''
)
return result
def get_bootstrap_archives(self) -> List:
"""
List of archive names from the type="bootstrap" packages section(s)
:return: archive names
:rtype: list
"""
result = []
bootstrap_packages_sections = self.get_bootstrap_packages_sections()
if bootstrap_packages_sections:
for bootstrap_packages_section in bootstrap_packages_sections:
archive_list = bootstrap_packages_section.get_archive()
if archive_list:
for archive in archive_list:
result.append(archive.get_name().strip())
return sorted(result)
def get_system_archives(self) -> List:
"""
List of archive names from the packages sections matching
type="image" and type=build_type
:return: archive names
:rtype: list
"""
result = []
image_packages_sections = self.get_packages_sections(
['image', self.get_build_type_name()]
)
for packages in image_packages_sections:
for archive in packages.get_archive():
result.append(archive.get_name().strip())
return sorted(result)
def get_ignore_packages(self, section_type: str) -> List:
"""
List of ignore package names from the packages sections matching
section_type and type=build_type
:return: package names
:rtype: list
"""
result = []
image_packages_sections = self.get_packages_sections(
[section_type, self.get_build_type_name()]
)
for packages in image_packages_sections:
for package in packages.get_ignore():
if self.package_matches_host_architecture(package):
result.append(package.get_name().strip())
return sorted(result)
def get_system_files_ignore_packages(self) -> List[str]:
"""
List of ignore package names from the type="systemfiles"
packages section(s)
:return: package names
:rtype: list
"""
return self.get_ignore_packages('systemfiles')
def get_system_ignore_packages(self) -> List:
"""
List of ignore package names from the packages sections matching
type="image" and type=build_type
:return: package names
:rtype: list
"""
return self.get_ignore_packages('image')
def get_bootstrap_ignore_packages(self) -> List:
"""
List of ignore package names from the packages sections matching
type="image" and type=build_type
:return: package names
:rtype: list
"""
return self.get_ignore_packages('bootstrap')
def get_bootstrap_package_name(self) -> str:
"""
bootstrap_package name from type="bootstrap" packages section
:return: bootstrap_package name
:rtype: str
"""
typed_packages_sections = self.get_packages_sections(
['bootstrap', self.get_build_type_name()]
)
bootstrap_package = ''
for packages in typed_packages_sections:
bootstrap_package = packages.get_bootstrap_package()
if bootstrap_package:
break
return bootstrap_package
def get_collection_type(self, section_type: str = 'image') -> str:
"""
Collection type from packages sections matching given section
type.
If no collection type is specified the default collection
type is set to: onlyRequired
:param str section_type: type name from packages section
:return: collection type name
:rtype: str
"""
typed_packages_sections = self.get_packages_sections(
[section_type, self.get_build_type_name()]
)
collection_type = 'onlyRequired'
for packages in typed_packages_sections:
packages_collection_type = packages.get_patternType()
if packages_collection_type:
collection_type = packages_collection_type
break
return collection_type
def get_bootstrap_collection_type(self) -> str:
"""
Collection type for packages sections matching type="bootstrap"
:return: collection type name
:rtype: str
"""
return self.get_collection_type('bootstrap')
def get_system_collection_type(self) -> str:
"""
Collection type for packages sections matching type="image"
:return: collection type name
:rtype: str
"""
return self.get_collection_type('image')
def get_collection_modules(self) -> Dict[str, List[str]]:
"""
Dict of collection modules to enable and/or disable
:return:
Dict of the form:
.. code:: python
{
'enable': [
"module:stream", "module"
],
'disable': [
"module"
]
}
:rtype: dict
"""
modules: Dict[str, List[str]] = {
'disable': [],
'enable': []
}
for packages in self.get_bootstrap_packages_sections():
for collection_module in packages.get_collectionModule():
module_name = collection_module.get_name()
if collection_module.get_enable() is False:
modules['disable'].append(module_name)
else:
stream = collection_module.get_stream()
if stream:
modules['enable'].append(f'{module_name}:{stream}')
else:
modules['enable'].append(module_name)
return modules
def get_collections(self, section_type: str = 'image') -> List:
"""
List of collection names from the packages sections matching
type=section_type and type=build_type
:return: collection names
:rtype: list
"""
result = []
typed_packages_sections = self.get_packages_sections(
[section_type, self.get_build_type_name()]
)
for packages in typed_packages_sections:
for collection in packages.get_namedCollection():
if self.collection_matches_host_architecture(collection):
result.append(collection.get_name())
return sorted(list(set(result)))
def get_bootstrap_collections(self) -> List:
"""
List of collection names from the packages sections
matching type="bootstrap"
:return: collection names
:rtype: list
"""
return self.get_collections('bootstrap')
def get_system_collections(self) -> List:
"""
List of collection names from the packages sections
matching type="image"
:return: collection names
:rtype: list
"""
return self.get_collections('image')
def get_products(self, section_type: str = 'image') -> List:
"""
List of product names from the packages sections matching
type=section_type and type=build_type
:param str section_type: type name from packages section
:return: product names
:rtype: list
"""
result = []
typed_packages_sections = self.get_packages_sections(
[section_type, self.get_build_type_name()]
)
for packages in typed_packages_sections:
for product in packages.get_product():
result.append(product.get_name())
return list(set(result))
def get_bootstrap_products(self) -> List:
"""
List of product names from the packages sections
matching type="bootstrap"
:return: product names
:rtype: list
"""
return self.get_products('bootstrap')
def get_system_products(self) -> List:
"""
List of product names from the packages sections
matching type="image"
:return: product names
:rtype: list
"""
return self.get_products('image')
def is_xen_server(self) -> bool:
"""
Check if build type domain setup specifies a Xen Server (dom0)
:return: True or False
:rtype: bool
"""
return self.build_type.get_xen_server()
def is_xen_guest(self) -> bool:
"""
Check if build type setup specifies a Xen Guest (domX)
The check is based on the architecture, the firmware and
xen_loader configuration values:
* We only support Xen setup on the x86_64 architecture
* Firmware pointing to ec2 means the image is targeted to run
in Amazon EC2 which is a Xen guest
* Machine setup with a xen_loader attribute also indicates a
Xen guest target
:return: True or False
:rtype: bool
"""
if self.host_architecture != 'x86_64':
# We only support Xen stuff on x86_64
return False
firmware = self.build_type.get_firmware()
machine_section = self.get_build_type_machine_section()
if firmware and firmware in Defaults.get_ec2_capable_firmware_names():
# the image is targeted to run in Amazon EC2 which is a Xen system
return True
elif machine_section and machine_section.get_xen_loader():
# the image provides a machine section with a guest loader setup
return True
return False
def get_build_type_partitions_section(self) -> Any:
"""
First partitions section from the build type section
:return: <partitions> section reference
:rtype: xml_parse::partitions
"""
partitions_sections = self.build_type.get_partitions()
if partitions_sections:
return partitions_sections[0]
return None
def get_build_type_system_disk_section(self) -> Any:
"""
First system disk section from the build type section
:return: <systemdisk> section reference
:rtype: xml_parse::systemdisk
"""
systemdisk_sections = self.build_type.get_systemdisk()
if systemdisk_sections:
return systemdisk_sections[0]
return None
def get_build_type_machine_section(self) -> Any:
"""
First machine section from the build type section
:return: <machine> section reference
:rtype: xml_parse::machine
"""
machine_sections = self.build_type.get_machine()
if machine_sections:
return machine_sections[0]
return None
def get_build_type_vagrant_config_section(self) -> Any:
"""
First vagrantconfig section from the build type section
:return: <vagrantconfig> section reference
:rtype: xml_parse::vagrantconfig
"""
vagrant_config_sections = self.build_type.get_vagrantconfig()
if vagrant_config_sections:
return vagrant_config_sections[0]
return None
def get_vagrant_config_virtualbox_guest_additions(self) -> bool:
"""
Attribute virtualbox_guest_additions_present from the first
vagrantconfig section.
:return: True|False
:rtype: bool
"""
vagrant_config_sections = self.get_build_type_vagrant_config_section()
if not vagrant_config_sections.virtualbox_guest_additions_present:
return Defaults.get_vagrant_config_virtualbox_guest_additions()
else:
return vagrant_config_sections.virtualbox_guest_additions_present
def get_build_type_vmdisk_section(self) -> Any:
"""
First vmdisk section from the first machine section in the
build type section
:return: <vmdisk> section reference
:rtype: xml_parse::vmdisk
"""
machine_section = self.get_build_type_machine_section()
if machine_section:
vmdisk_sections = machine_section.get_vmdisk()
if vmdisk_sections:
return vmdisk_sections[0]
return None
def get_build_type_vmnic_entries(self) -> List:
"""
vmnic section(s) from the first machine section in the
build type section
:return: list of <vmnic> section reference(s)
:rtype: list
"""
machine_section = self.get_build_type_machine_section()
if machine_section:
return machine_section.get_vmnic()
else:
return []
def get_build_type_vmdvd_section(self) -> Any:
"""
First vmdvd section from the first machine section in the
build type section
:return: <vmdvd> section reference
:rtype: xml_parse::vmdvd
"""
machine_section = self.get_build_type_machine_section()
if machine_section:
vmdvd_sections = machine_section.get_vmdvd()
if vmdvd_sections:
return vmdvd_sections[0]
return None
def get_build_type_vmconfig_entries(self) -> List:
"""
List of vmconfig-entry section values from the first
machine section in the build type section
:return: <vmconfig_entry> section reference(s)
:rtype: list
"""
machine_section = self.get_build_type_machine_section()
if machine_section:
vmconfig_entries = machine_section.get_vmconfig_entry()
if vmconfig_entries:
return vmconfig_entries
return []
def get_build_type_bootloader_section(self) -> Any:
"""
First bootloader section from the build type section
:return: <bootloader> section reference
:rtype: xml_parse::bootloader
"""
bootloader_sections = self.build_type.get_bootloader()
if bootloader_sections:
return bootloader_sections[0]
return None
def get_build_type_bootloader_name(self) -> str:
"""
Return bootloader name for selected build type
:return: bootloader name
:rtype: str
"""
bootloader = self.get_build_type_bootloader_section()
return bootloader.get_name() if bootloader else \
Defaults.get_default_bootloader()
def get_build_type_bootloader_bls(self) -> bool:
"""
Return bootloader bls setting for selected build type
:return: True or False
:rtype: bool
"""
bootloader = self.get_build_type_bootloader_section()
if bootloader and bootloader.get_bls() is not None:
return bootloader.get_bls()
return True
def get_build_type_bootloader_console(self) -> List[str]:
"""
Return bootloader console setting for selected build type
:return:
list of console settings for output (first element)
and input (second element)
:rtype: list
"""
result = ['', '']
bootloader = self.get_build_type_bootloader_section()
if bootloader:
combined_console = bootloader.get_console()
if combined_console:
console_out, *console_in = combined_console.split(' ')[:2]
console_in = console_out if not console_in else console_in[0]
result = [
console_out if console_out != 'none' else '',
console_in if console_in != 'none' else ''
]
return result
def get_build_type_bootloader_serial_line_setup(self) -> Optional[str]:
"""
Return bootloader serial line setup parameters for the
selected build type
:return: serial line setup
:rtype: str
"""
bootloader = self.get_build_type_bootloader_section()
if bootloader:
return bootloader.get_serial_line()
return None
def get_build_type_bootloader_timeout(self) -> Optional[str]:
"""
Return bootloader timeout setting for selected build type
:return: timeout string
:rtype: str
"""
bootloader = self.get_build_type_bootloader_section()
if bootloader:
return bootloader.get_timeout()
return None
def get_build_type_bootloader_timeout_style(self) -> Optional[str]:
"""
Return bootloader timeout style setting for selected build type
:return: timeout_style string
:rtype: str
"""
bootloader = self.get_build_type_bootloader_section()
if bootloader:
return bootloader.get_timeout_style()
return None
def get_build_type_bootloader_targettype(self) -> Optional[str]:
"""
Return bootloader target type setting. Only relevant for
the zipl bootloader because zipl is installed differently
depending on the storage target it runs later
:return: target type string
:rtype: str
"""
bootloader = self.get_build_type_bootloader_section()
if bootloader:
return bootloader.get_targettype()
return None
def get_build_type_bootloader_settings_section(self) -> Any:
"""
First bootloadersettings section from the build
type bootloader section
:return: <bootloadersettings> section reference
:rtype: xml_parse::bootloadersettings
"""
bootloader_section = self.get_build_type_bootloader_section()
bootloader_settings_section = None
if bootloader_section and bootloader_section.get_bootloadersettings():
bootloader_settings_section = \
bootloader_section.get_bootloadersettings()[0]
return bootloader_settings_section
def get_build_type_bootloader_securelinux_section(self) -> List[Any]:
"""
First securelinux section from the build
type bootloader section
:return: <securelinux> section reference
:rtype: xml_parse::securelinux
"""
bootloader_section = self.get_build_type_bootloader_section()
bootloader_securelinux_section = []
if bootloader_section and bootloader_section.get_securelinux():
bootloader_securelinux_section = \
bootloader_section.get_securelinux()
return bootloader_securelinux_section
def get_bootloader_options(self, option_type: str) -> List[str]:
"""
List of custom options used in the process to
run bootloader setup workloads
"""
result: List[str] = []
bootloader_settings = self.get_build_type_bootloader_settings_section()
if bootloader_settings:
options = []
if option_type == 'shim':
options = bootloader_settings.get_shimoption()
elif option_type == 'install':
options = bootloader_settings.get_installoption()
elif option_type == 'config':
options = bootloader_settings.get_configoption()
for option in options:
result.append(option.get_name())
if option.get_value():
result.append(option.get_value())
return result
def get_bootloader_shim_options(self) -> List[str]:
"""
List of custom options used in the process to setup secure boot
"""
return self.get_bootloader_options('shim')
def get_bootloader_install_options(self) -> List[str]:
"""
List of custom options used in the bootloader installation
"""
return self.get_bootloader_options('install')
def get_bootloader_config_options(self) -> List[str]:
"""
List of custom options used in the bootloader configuration
"""
return self.get_bootloader_options('config')
def get_build_type_bootloader_use_disk_password(self) -> bool:
"""
Indicate whether the bootloader configuration should use the
password protecting the encrypted root volume.
:return: True|False
:rtype: bool
"""
bootloader = self.get_build_type_bootloader_section()
if bootloader:
return bootloader.get_use_disk_password()
return False
def get_build_type_oemconfig_section(self) -> Any:
"""
First oemconfig section from the build type section
:return: <oemconfig> section reference
:rtype: xml_parse::oemconfig
"""
oemconfig_sections = self.build_type.get_oemconfig()
if oemconfig_sections:
return oemconfig_sections[0]
return None
def get_oemconfig_oem_resize(self) -> bool:
"""
State value to activate/deactivate disk resize. Returns a
boolean value if specified or True to set resize on by default
:return: Content of <oem-resize> section value
:rtype: bool
"""
oemconfig = self.get_build_type_oemconfig_section()
if oemconfig and oemconfig.get_oem_resize():
return oemconfig.get_oem_resize()[0]
else:
return True
def get_oemconfig_oem_systemsize(self) -> int:
"""
State value to retrieve root partition size
:return: Content of <oem-systemsize> section value
:rtype: int
"""
oemconfig = self.get_build_type_oemconfig_section()
if oemconfig and oemconfig.get_oem_systemsize():
return int(oemconfig.get_oem_systemsize()[0])
else:
return 0
def get_oemconfig_oem_multipath_scan(self) -> bool:
"""
State value to activate multipath maps. Returns a boolean
value if specified or False
:return: Content of <oem-multipath-scan> section value
:rtype: bool
"""
oemconfig = self.get_build_type_oemconfig_section()
if oemconfig and oemconfig.get_oem_multipath_scan():
return oemconfig.get_oem_multipath_scan()[0]
return False
def get_oemconfig_swap_mbytes(self) -> Optional[int]:
"""
Return swapsize in MB if requested or None
Operates on the value of oem-swap and if set to true
returns the given size or the default value.
:return: Content of <oem-swapsize> section value or default
:rtype: int
"""
oemconfig = self.get_build_type_oemconfig_section()
if oemconfig and oemconfig.get_oem_swap():
swap_requested = oemconfig.get_oem_swap()[0]
if swap_requested:
swapsize = oemconfig.get_oem_swapsize()
if swapsize:
return swapsize[0]
else:
return Defaults.get_swapsize_mbytes()
return None
def get_oemconfig_swap_name(self) -> str:
"""
Return the swap space name
Operates on the value of oem-swapname and if set
returns the configured name or the default name: LVSwap
The name of the swap space is used only if the
image is configured to use the LVM volume manager.
In this case swap is a volume and the volume takes
a name. In any other case the given name will have
no effect.
:return: Content of <oem-swapname> section value or default
:rtype: str
"""
oemconfig = self.get_build_type_oemconfig_section()
if oemconfig and oemconfig.get_oem_swapname():
return oemconfig.get_oem_swapname()[0]
return 'LVSwap'
def get_build_type_containerconfig_section(self) -> Any:
"""
First containerconfig section from the build type section
:return: <containerconfig> section reference
:rtype: xml_parse::containerconfig
"""
container_config_sections = self.build_type.get_containerconfig()
if container_config_sections:
return container_config_sections[0]
return None
def get_dracut_config(self, action: str) -> DracutT:
"""
Get dracut initrd config for the specified action
"""
uefi = False
modules = []
drivers = []
initrd_sections = self.build_type.get_initrd()
for initrd_section in initrd_sections:
if initrd_section.get_action() == action:
for dracut in initrd_section.get_dracut():
uefi = bool(dracut.get_uefi())
if dracut.get_module():
modules.append(dracut.get_module())
if dracut.get_driver():
drivers.append(dracut.get_driver())
return DracutT(
uefi=uefi, modules=modules, drivers=drivers
)
def get_installmedia_initrd_modules(self, action: str) -> List[str]:
"""
Gets the list of modules to append in installation initrds
:return: a list of dracut module names
:rtype: list
"""
modules: List[str] = []
installmedia = self.build_type.get_installmedia()
if not installmedia:
return modules
initrd_sections = installmedia[0].get_initrd()
for initrd_section in initrd_sections:
if initrd_section.get_action() == action:
for module in initrd_section.get_dracut():
if module.get_module():
modules.append(module.get_module())
return modules
def get_installmedia_initrd_drivers(self, action: str) -> List[str]:
"""
Gets the list of drivers to append in installation initrds
:return: a list of dracut driver names
:rtype: list
"""
drivers: List[str] = []
installmedia = self.build_type.get_installmedia()
if not installmedia:
return drivers
initrd_sections = installmedia[0].get_initrd()
for initrd_section in initrd_sections:
if initrd_section.get_action() == action:
for driver in initrd_section.get_dracut():
if driver.get_driver():
drivers.append(driver.get_driver())
return drivers
def get_build_type_size(
self, include_unpartitioned: bool = False
) -> Optional[size_type]:
"""
Size information from the build type section.
If no unit is set the value is treated as mbytes
:param bool include_unpartitioned: sets if the unpartitioned area
should be included in the computed size or not
:return: mbytes
:rtype: int
"""
size_section = self.build_type.get_size()
if size_section:
unit = size_section[0].get_unit()
additive = size_section[0].get_additive()
unpartitioned = size_section[0].get_unpartitioned()
value = int(size_section[0].get_valueOf_())
if not include_unpartitioned and unpartitioned is not None:
value -= unpartitioned
if unit == 'G':
value *= 1024
return size_type(
mbytes=value, additive=additive
)
return None
def get_build_type_unpartitioned_bytes(self) -> int:
"""
Size of the unpartitioned area for image in megabytes
:return: mbytes
:rtype: int
"""
size_section = self.build_type.get_size()
if size_section:
unit = size_section[0].get_unit() or 'M'
unpartitioned = size_section[0].get_unpartitioned() or 0
return StringToSize.to_bytes('{0}{1}'.format(unpartitioned, unit))
return 0
def get_disk_start_sector(self) -> int:
"""
First disk sector number to be used by the first disk partition.
:return: number
:rtype: int
"""
disk_start_sector = self.build_type.get_disk_start_sector()
if disk_start_sector is None:
disk_start_sector = Defaults.get_default_disk_start_sector()
return disk_start_sector
def get_build_type_spare_part_size(self) -> Optional[int]:
"""
Size information for the spare_part size from the build
type. If no unit is set the value is treated as mbytes
:return: mbytes
:rtype: int
"""
spare_part_size = self.build_type.get_spare_part()
if spare_part_size:
return self._to_mega_byte(spare_part_size)
return None
def get_build_type_spare_part_fs_attributes(self) -> Optional[List]:
"""
Build type specific list of filesystem attributes applied to
the spare partition.
:return: list of strings or empty list
:rtype: list
"""
spare_part_attributes = self.build_type.get_spare_part_fs_attributes()
if spare_part_attributes:
return spare_part_attributes.strip().split(',')
return None
def get_build_type_format_options(self) -> Dict:
"""
Disk format options returned as a dictionary
:return: format options
:rtype: dict
"""
result = {}
format_options = self.build_type.get_formatoptions()
if format_options:
for option in format_options.split(','):
key_value_list = option.split('=')
if len(key_value_list) == 2:
result[key_value_list[0]] = key_value_list[1]
else:
result[key_value_list[0]] = None
return result
def get_volume_group_name(self) -> str:
"""
Volume group name from selected <systemdisk> section
:return: volume group name
:rtype: str
"""
systemdisk_section = self.get_build_type_system_disk_section()
volume_group_name = None
if systemdisk_section:
volume_group_name = systemdisk_section.get_name()
if not volume_group_name:
volume_group_name = Defaults.get_default_volume_group_name()
return volume_group_name
def get_users(self) -> List:
"""
List of configured users.
Each entry in the list is a single xml_parse::user instance.
:return: list of <user> section reference(s)
:rtype: list
"""
users_list = []
users_names_added = []
for users_section in self.get_users_sections():
for user in users_section.get_user():
if user.get_name() not in users_names_added:
users_list.append(user)
users_names_added.append(user.get_name())
return users_list
def get_user_groups(self, user_name) -> List[str]:
"""
List of group names matching specified user
Each entry in the list is the name of a group and optionally its
group ID separated by a colon, that the specified user belongs to.
The first item in the list is the login or primary group. The
list will be empty if no groups are specified in the
description file.
:return: groups data for the given user
:rtype: list
"""
groups_list = []
for users_section in self.get_users_sections():
for user in users_section.get_user():
if user.get_name() == user_name:
user_groups = user.get_groups()
if user_groups:
groups_list += user.get_groups().split(',')
# order of list items matter, thus we don't use set() here
# better faster, nicer solutions welcome :)
result_group_list = []
for item in groups_list:
if item not in result_group_list:
result_group_list.append(item)
return result_group_list
def get_container_config(self) -> Dict:
"""
Dictionary of containerconfig information
Takes attributes and subsection data from the selected
<containerconfig> section and stores it in a dictionary
"""
container_config = self._match_docker_base_data()
container_config.update(
self._match_docker_entrypoint()
)
container_config.update(
self._match_docker_subcommand()
)
container_config.update(
self._match_docker_expose_ports()
)
container_config.update(
self._match_docker_volumes()
)
container_config.update(
self._match_docker_stopsignal()
)
container_config.update(
self._match_docker_environment()
)
container_config.update(
self._match_docker_labels()
)
container_config.update(
self._match_docker_history()
)
desc = self.get_description_section()
author_contact = "{0} <{1}>".format(desc.author, desc.contact)
if 'history' not in container_config:
container_config['history'] = {}
if 'author' not in container_config['history']:
container_config['history']['author'] = author_contact
if 'maintainer' not in container_config:
container_config['maintainer'] = author_contact
return container_config
def set_container_config_tag(self, tag: str) -> None:
"""
Set new tag name in containerconfig section
In order to set a new tag value an existing containerconfig and
tag setup is required
:param str tag: tag name
"""
container_config_section = self.get_build_type_containerconfig_section()
if container_config_section and container_config_section.get_tag():
container_config_section.set_tag(tag)
else:
message = dedent('''\n
No <containerconfig> section and/or tag attribute configured
In order to set the tag {0} as new container tag,
an initial containerconfig section including a tag
setup is required
''')
log.warning(message.format(tag))
def add_container_config_label(self, label_name: str, value: str) -> None:
"""
Adds a new label in the containerconfig section, if a label with the
same name is already defined in containerconfig it gets overwritten by
this method.
:param str label_name: the string representing the label name
:param str value: the value of the label
"""
if self.get_build_type_name() not in ['docker', 'oci']:
message = dedent('''\n
Labels can only be configured for container image types
docker and oci.
''')
log.warning(message)
return
container_config_section = self.get_build_type_containerconfig_section()
if not container_config_section:
container_config_section = xml_parse.containerconfig(
name=Defaults.get_default_container_name(),
tag=Defaults.get_default_container_tag()
)
self.build_type.set_containerconfig([container_config_section])
labels = container_config_section.get_labels()
if not labels:
labels = [xml_parse.labels()]
label_names = []
for label in labels[0].get_label():
label_names.append(label.get_name())
if label_name in label_names:
labels[0].replace_label_at(
label_names.index(label_name),
xml_parse.label(label_name, value)
)
else:
labels[0].add_label(xml_parse.label(label_name, value))
container_config_section.set_labels(labels)
def get_partitions(self) -> Dict[str, ptable_entry_type]:
"""
Dictionary of configured partitions.
Each entry in the dict references a ptable_entry_type
Each key in the dict references the name of the
partition entry as handled by KIWI
:return:
Contains dict of ptable_entry_type tuples
.. code:: python
{
'NAME': ptable_entry_type(
mbsize=int,
clone=int,
partition_name=str,
partition_type=str,
mountpoint=str,
filesystem=str,
label=str
)
}
:rtype: dict
"""
partitions: Dict[str, ptable_entry_type] = {}
partitions_section = self.get_build_type_partitions_section()
if not partitions_section:
return partitions
for partition in partitions_section.get_partition():
name = partition.get_name()
partition_name = partition.get_partition_name() or f'p.lx{name}'
partitions[name] = ptable_entry_type(
mbsize=self._to_mega_byte(partition.get_size()),
clone=int(partition.get_clone()) if partition.get_clone() else 0,
partition_name=partition_name,
partition_type=partition.get_partition_type() or 't.linux',
mountpoint=partition.get_mountpoint(),
filesystem=partition.get_filesystem(),
label=partition.get_label() or ''
)
return partitions
def get_host_key_certificates(
self
) -> Union[List[Dict[str, List[str]]], List[Dict[str, str]]]:
cc_result = []
cc_certificates: Dict[str, List[str]] = {}
securelinux_list = \
self.get_build_type_bootloader_securelinux_section()
for securelinux in securelinux_list:
cc_certificates = {
'hkd_cert': [],
'hkd_revocation_list': [],
'hkd_ca_cert': securelinux.get_hkd_ca_cert(),
'hkd_sign_cert': securelinux.get_hkd_sign_cert()
}
for hkd_cert in securelinux.get_hkd_cert():
cc_certificates['hkd_cert'].append(hkd_cert.get_name())
for hkd_revocation_list in securelinux.get_hkd_revocation_list():
cc_certificates['hkd_revocation_list'].append(
hkd_revocation_list.get_name()
)
cc_result.append(cc_certificates)
return cc_result
def get_containers(self) -> List[ContainerT]:
containers = []
def build_fetch_command(
root_dir: str,
container_uri: str = '',
container_file_name: str = '',
container_endpoint: str = ''
):
pass # pragma: nocover
for containers_section in self.get_containers_sections():
for container in containers_section.get_container():
if self.container_matches_host_architecture(container):
fetch_command = build_fetch_command
load_command = []
container_tag = container.get_tag() or 'latest'
container_path = container.get_path() or ''
container_endpoint = os.path.normpath(
'{0}/{1}/{2}:{3}'.format(
containers_section.get_source(), container_path,
container.name, container_tag
)
)
container_file_name = '{0}/{1}_{2}'.format(
defaults.LOCAL_CONTAINERS, container.name, container_tag
)
container_backend = containers_section.get_backend() or ''
if container_backend in ['podman', 'docker', 'container-snap']:
if Defaults.is_buildservice_worker():
container_uri = Uri(
'obsrepositories:/{0}'.format(
container_endpoint
), 'container'
).translate()
def build_fetch_command(
root_dir: str,
container_uri: str = container_uri,
container_file_name: str = container_file_name,
container_endpoint: str = container_endpoint
):
def perform():
Command.run(
[
'cp', '{0}.ociarchive'.format(
container_uri
), os.path.normpath(
'{0}/{1}'.format(
root_dir,
container_file_name
)
)
]
)
perform()
fetch_command = build_fetch_command
else:
def build_fetch_command(
root_dir: str,
container_uri: str = '',
container_file_name: str = container_file_name,
container_endpoint: str = container_endpoint
):
def perform():
Command.run(
[
'chroot', root_dir,
'/usr/bin/skopeo', 'copy',
'docker://{0}'.format(
container_endpoint
),
'oci-archive:{0}:{1}'.format(
container_file_name,
container_endpoint
)
]
)
perform()
fetch_command = build_fetch_command
if not container.get_fetch_only():
load_command = [
f'/usr/bin/{container_backend}',
'load', '-i', container_file_name
]
containers.append(
ContainerT(
name=f'{container.name}_{container_tag}',
backend=container_backend,
container_file=container_file_name,
fetch_only=bool(container.get_fetch_only()),
fetch_command=fetch_command,
load_command=load_command
)
)
return containers
def get_volumes(self) -> List[volume_type]:
"""
List of configured systemdisk volumes.
Each entry in the list is a tuple with the following information
* name: name of the volume
* size: size of the volume
* realpath: system path to lookup volume data. If no mountpoint
is set the volume name is used as data path.
* mountpoint: volume mount point and volume data path
* fullsize: takes all space True|False
* attributes: list of volume attributes handled via chattr
:return:
Contains list of volume_type tuples
.. code:: python
[
volume_type(
name=volume_name,
parent=volume_parent,
size=volume_size,
realpath=path,
mountpoint=path,
fullsize=True,
label=volume_label,
attributes=['no-copy-on-write'],
is_root_volume=True|False
)
]
:rtype: list
"""
volume_type_list: List[volume_type] = []
systemdisk_section = self.get_build_type_system_disk_section()
selected_filesystem = self.build_type.get_filesystem()
swap_mbytes = self.get_oemconfig_swap_mbytes()
swap_name = self.get_oemconfig_swap_name()
if not systemdisk_section:
return volume_type_list
volumes = systemdisk_section.get_volume()
have_root_volume_setup = False
have_full_size_volume = False
if volumes:
for volume in volumes:
if not self.volume_matches_host_architecture(volume):
continue
# volume setup for a full qualified volume with name and
# mountpoint information. See below for exceptions
name = volume.get_name()
parent = volume.get_parent() or ''
mountpoint = volume.get_mountpoint()
realpath = mountpoint
size = volume.get_size()
freespace = volume.get_freespace()
fullsize = False
label = volume.get_label()
attributes = []
is_root_volume = False
if volume.get_quota():
attributes.append(f'quota={volume.get_quota()}')
if volume.get_copy_on_write() is False:
# by default copy-on-write is switched on for any
# filesystem. Thus only if no copy on write is requested
# the attribute is handled
attributes.append('no-copy-on-write')
if volume.get_filesystem_check() is True:
# by default filesystem check is switched off for any
# filesystem except the rootfs. Thus only if filesystem
# check is requested the attribute is handled
attributes.append('enable-for-filesystem-check')
if '@root' in name:
# setup root volume, it takes an optional volume
# name if specified as @root=name and has no specific
# mountpoint. The default name is set to
# defaults.ROOT_VOLUME_NAME if no other root volume
# name is provided
mountpoint = None
realpath = '/'
is_root_volume = True
root_volume_expression = re.match(
r'@root=(.+)', name
)
if root_volume_expression:
name = root_volume_expression.group(1)
else:
name = defaults.ROOT_VOLUME_NAME
have_root_volume_setup = True
elif not mountpoint:
# setup volume without mountpoint. In this case the name
# attribute is used as mountpoint path and a name for the
# volume is created from that path information
mountpoint = name
realpath = mountpoint
name = self._to_volume_name(name)
if size:
size = 'size:' + format(
self._to_mega_byte(size)
)
elif freespace:
size = 'freespace:' + format(
self._to_mega_byte(freespace)
)
else:
size = 'freespace:' + format(
Defaults.get_min_volume_mbytes(selected_filesystem)
)
if ':all' in size:
size = None
fullsize = True
have_full_size_volume = True
volume_type_list.append(
volume_type(
name=name,
parent=parent,
size=size,
fullsize=fullsize,
mountpoint=mountpoint,
realpath=realpath,
label=label,
attributes=attributes,
is_root_volume=is_root_volume
)
)
if not have_root_volume_setup:
# There must always be a root volume setup. It will be the
# full size volume if no other volume has this setup
volume_management = self.get_volume_management()
root_volume_name = \
defaults.ROOT_VOLUME_NAME if volume_management == 'lvm' else ''
if have_full_size_volume:
size = 'freespace:' + format(
Defaults.get_min_volume_mbytes(selected_filesystem)
)
fullsize = False
else:
size = None
fullsize = True
volume_type_list.append(
volume_type(
name=root_volume_name,
parent='',
size=size,
fullsize=fullsize,
mountpoint=None,
realpath='/',
label=None,
attributes=[],
is_root_volume=True
)
)
if swap_mbytes and self.get_volume_management() == 'lvm':
volume_type_list.append(
volume_type(
name=swap_name,
parent='',
size='size:{0}'.format(swap_mbytes),
fullsize=False,
mountpoint=None,
realpath='swap',
label='SWAP',
attributes=[],
is_root_volume=False
)
)
return volume_type_list
def get_volume_management(self) -> Optional[str]:
"""
Provides information which volume management system is used
:return: name of volume manager
:rtype: str
"""
volume_filesystems = ['btrfs']
selected_filesystem = self.build_type.get_filesystem()
selected_system_disk = self.get_build_type_system_disk_section()
volume_management = None
if selected_system_disk and selected_system_disk.get_preferlvm():
# LVM volume management is preferred, use it
volume_management = 'lvm'
elif selected_filesystem in volume_filesystems and selected_system_disk:
# specified filesystem has its own volume management system
volume_management = selected_filesystem
elif selected_system_disk:
# systemdisk section is specified with non volume capable
# filesystem and no volume management preference. So let's
# use LVM by default
volume_management = 'lvm'
return volume_management
def get_drivers_list(self) -> List:
"""
List of driver names from all drivers sections matching
configured profiles
:return: driver names
:rtype: list
"""
drivers_sections = self._profiled(
self.xml_data.get_drivers()
)
result = []
if drivers_sections:
for driver in drivers_sections:
for file_section in driver.get_file():
result.append(file_section.get_name())
return result
def get_strip_list(self, section_type: str) -> List:
"""
List of strip names matching the given section type
and profiles
:param str section_type: type name from packages section
:return: strip names
:rtype: list
"""
strip_sections = self._profiled(
self.xml_data.get_strip()
)
result = []
if strip_sections:
for strip in strip_sections:
if strip.get_type() == section_type:
for file_section in strip.get_file():
result.append(file_section.get_name())
return result
def get_strip_files_to_delete(self) -> List:
"""
Items to delete from strip section
:return: item names
:rtype: list
"""
return self.get_strip_list('delete')
def get_strip_tools_to_keep(self) -> List:
"""
Tools to keep from strip section
:return: tool names
:rtype: list
"""
return self.get_strip_list('tools')
def get_strip_libraries_to_keep(self) -> List:
"""
Libraries to keep from strip section
:return: librarie names
:rtype: list
"""
return self.get_strip_list('libs')
def get_include_section_reference_file_names(self) -> List[str]:
"""
List of all <include> section file name references
:return: List[str]
:rtype: list
"""
include_files = []
for include in self.xml_data.get_include():
include_files.append(include.get_from())
return include_files
def get_repository_sections(self) -> List:
"""
List of all repository sections for the selected profiles that
matches the host architecture
:return: <repository> section reference(s)
:rtype: list
"""
repository_list = []
for repository in self._profiled(self.xml_data.get_repository()):
if self.repository_matches_host_architecture(repository):
repository_list.append(repository)
return repository_list
def get_containers_sections(self) -> List:
"""
List of all containers sections for the selected profiles that
matches the host architecture
:return: <containers> section reference(s)
:rtype: list
"""
containers_list = []
for containers in self._profiled(self.xml_data.get_containers()):
if self.containers_matches_host_architecture(containers):
containers_list.append(containers)
return containers_list
def get_repository_sections_used_for_build(self) -> List:
"""
List of all repositorys sections used to build the image and
matching configured profiles.
:return: <repository> section reference(s)
:rtype: list
"""
repos = self.get_repository_sections()
return list(
repo for repo in repos if not repo.get_imageonly()
)
def get_repository_sections_used_in_image(self) -> List:
"""
List of all repositorys sections to be configured in the resulting
image matching configured profiles.
:return: <repository> section reference(s)
:rtype: list
"""
repos = self.get_repository_sections()
return list(
repo for repo in repos
if repo.get_imageinclude() or repo.get_imageonly()
)
def delete_repository_sections(self) -> None:
"""
Delete all repository sections matching configured profiles
"""
self.xml_data.set_repository([])
def delete_repository_sections_used_for_build(self) -> None:
"""
Delete all repository sections used to build the image matching
configured profiles
"""
used_for_build = self.get_repository_sections_used_for_build()
all_repos = self.get_repository_sections()
self.xml_data.set_repository(
[
repo for repo in all_repos if repo not in used_for_build
]
)
def get_repositories_signing_keys(self) -> List[str]:
"""
Get list of signing keys specified on the repositories
"""
key_file_list: List[str] = []
release_version = self.get_release_version()
release_vars = [
'$releasever',
'${releasever}'
]
for repository in self.get_repository_sections() or []:
for signing in repository.get_source().get_signing() or []:
normalized_key_url = Uri(signing.get_key()).translate()
if release_version:
for release_var in release_vars:
if release_var in normalized_key_url:
normalized_key_url = normalized_key_url.replace(
release_var, release_version
)
if normalized_key_url not in key_file_list:
key_file_list.append(normalized_key_url)
return key_file_list
def set_repository(
self, repo_source: str, repo_type: str, repo_alias: str,
repo_prio: str, repo_imageinclude: bool = False,
repo_package_gpgcheck: Optional[bool] = None,
repo_signing_keys: List[str] = [], components: str = None,
distribution: str = None, repo_gpgcheck: Optional[bool] = None,
repo_sourcetype: str = None
) -> None:
"""
Overwrite repository data of the first repository
:param str repo_source: repository URI
:param str repo_type: type name defined by schema
:param str repo_alias: alias name
:param str repo_prio: priority number, package manager specific
:param bool repo_imageinclude: setup repository inside of the image
:param bool repo_package_gpgcheck: enable/disable package gpg checks
:param list repo_signing_keys: list of signing key file names
:param str components: component names for debian repos
:param str distribution: base distribution name for debian repos
:param bool repo_gpgcheck: enable/disable repo gpg checks
"""
repository_sections = self.get_repository_sections()
if repository_sections:
repository = repository_sections[0]
if repo_alias:
repository.set_alias(repo_alias)
if repo_type:
repository.set_type(repo_type)
if repo_source:
repository.get_source().set_path(repo_source)
if repo_prio:
repository.set_priority(int(repo_prio))
if repo_imageinclude:
repository.set_imageinclude(repo_imageinclude)
if repo_package_gpgcheck is not None:
repository.set_package_gpgcheck(repo_package_gpgcheck)
if repo_signing_keys:
repository.get_source().set_signing(
[xml_parse.signing(key=k) for k in repo_signing_keys]
)
if components:
repository.set_components(components)
if distribution:
repository.set_distribution(distribution)
if repo_gpgcheck is not None:
repository.set_repository_gpgcheck(repo_gpgcheck)
if repo_sourcetype:
repository.set_sourcetype(repo_sourcetype)
def add_repository(
self, repo_source: str, repo_type: str, repo_alias: str = None,
repo_prio: str = '', repo_imageinclude: bool = False,
repo_package_gpgcheck: Optional[bool] = None,
repo_signing_keys: List[str] = [], components: str = None,
distribution: str = None, repo_gpgcheck: Optional[bool] = None,
repo_sourcetype: str = None
) -> None:
"""
Add a new repository section at the end of the list
:param str repo_source: repository URI
:param str repo_type: type name defined by schema
:param str repo_alias: alias name
:param str repo_prio: priority number, package manager specific
:param bool repo_imageinclude: setup repository inside of the image
:param bool repo_package_gpgcheck: enable/disable package gpg checks
:param list repo_signing_keys: list of signing key file names
:param str components: component names for debian repos
:param str distribution: base distribution name for debian repos
:param bool repo_gpgcheck: enable/disable repo gpg checks
"""
priority_number: Optional[int] = None
try:
priority_number = int(repo_prio)
except Exception:
pass
self.xml_data.add_repository(
xml_parse.repository(
type_=repo_type,
alias=repo_alias,
priority=priority_number,
source=xml_parse.source(
path=repo_source,
signing=[
xml_parse.signing(key=k) for k in repo_signing_keys
]
),
imageinclude=repo_imageinclude,
package_gpgcheck=repo_package_gpgcheck,
repository_gpgcheck=repo_gpgcheck,
components=components,
distribution=distribution,
sourcetype=repo_sourcetype
)
)
def resolve_this_path(self) -> None:
"""
Resolve any this:// repo source path into the path
representing the target inside of the image description
directory
"""
for repository in self.get_repository_sections() or []:
repo_source = repository.get_source()
repo_path = repo_source.get_path()
if repo_path.startswith('this://'):
repo_path = repo_path.replace('this://', '')
repo_source.set_path(
'dir://{0}'.format(
os.path.realpath(
os.path.join(
self.xml_data.description_dir, repo_path
)
)
)
)
def copy_displayname(self, target_state: Any) -> None:
"""
Copy image displayname from this xml state to the target xml state
:param object target_state: XMLState instance
"""
displayname = self.xml_data.get_displayname()
if displayname:
target_state.xml_data.set_displayname(displayname)
def copy_name(self, target_state: Any) -> None:
"""
Copy image name from this xml state to the target xml state
:param object target_state: XMLState instance
"""
target_state.xml_data.set_name(
self.xml_data.get_name()
)
def copy_drivers_sections(self, target_state: Any) -> None:
"""
Copy drivers sections from this xml state to the target xml state
:param object target_state: XMLState instance
"""
drivers_sections = self._profiled(
self.xml_data.get_drivers()
)
if drivers_sections:
for drivers_section in drivers_sections:
target_state.xml_data.add_drivers(drivers_section)
def copy_systemdisk_section(self, target_state: Any) -> None:
"""
Copy systemdisk sections from this xml state to the target xml state
:param object target_state: XMLState instance
"""
systemdisk_section = self.get_build_type_system_disk_section()
if systemdisk_section:
target_state.build_type.set_systemdisk(
[systemdisk_section]
)
def copy_strip_sections(self, target_state: Any) -> None:
"""
Copy strip sections from this xml state to the target xml state
:param object target_state: XMLState instance
"""
strip_sections = self._profiled(
self.xml_data.get_strip()
)
if strip_sections:
for strip_section in strip_sections:
target_state.xml_data.add_strip(strip_section)
def copy_machine_section(self, target_state: Any) -> None:
"""
Copy machine sections from this xml state to the target xml state
:param object target_state: XMLState instance
"""
machine_section = self.get_build_type_machine_section()
if machine_section:
target_state.build_type.set_machine(
[machine_section]
)
def copy_bootloader_section(self, target_state: Any) -> None:
"""
Copy bootloader section from this xml state to the target xml state
:param object target_state: XMLState instance
"""
bootloader_section = self.get_build_type_bootloader_section()
if bootloader_section:
target_state.build_type.set_bootloader(
[bootloader_section]
)
def copy_oemconfig_section(self, target_state: Any) -> None:
"""
Copy oemconfig sections from this xml state to the target xml state
:param object target_state: XMLState instance
"""
oemconfig_section = self.get_build_type_oemconfig_section()
if oemconfig_section:
target_state.build_type.set_oemconfig(
[oemconfig_section]
)
def copy_repository_sections(
self, target_state: Any, wipe: bool = False
) -> None:
"""
Copy repository sections from this xml state to the target xml state
:param object target_state: XMLState instance
:param bool wipe: delete all repos in target prior to copy
"""
repository_sections = self._profiled(
self.xml_data.get_repository()
)
if repository_sections:
if wipe:
target_state.xml_data.set_repository([])
for repository_section in repository_sections:
repository_copy = copy.deepcopy(repository_section)
# profiles are not copied because they might not exist
# in the target description
repository_copy.set_profiles(None)
target_state.xml_data.add_repository(repository_copy)
def copy_preferences_subsections(
self, section_names: List, target_state: Any
) -> None:
"""
Copy subsections of the preferences sections, matching given
section names, from this xml state to the target xml state
:param list section_names: preferences subsection names
:param object target_state: XMLState instance
"""
target_preferences_sections = target_state.get_preferences_sections()
if target_preferences_sections:
target_preferences_section = target_preferences_sections[0]
for preferences_section in self.get_preferences_sections():
for section_name in section_names:
get_section_method = getattr(
preferences_section, 'get_' + section_name
)
section = get_section_method()
if section:
set_section_method = getattr(
target_preferences_section, 'set_' + section_name
)
set_section_method(section)
def copy_build_type_attributes(
self, attribute_names: List, target_state: Any
) -> None:
"""
Copy specified attributes from this build type section to the
target xml state build type section
:param list attribute_names: type section attributes
:param object target_state: XMLState instance
"""
for attribute in attribute_names:
get_type_method = getattr(
self.build_type, 'get_' + attribute
)
attribute_value = get_type_method()
if attribute_value:
set_type_method = getattr(
target_state.build_type, 'set_' + attribute
)
set_type_method(attribute_value)
def copy_bootincluded_packages(self, target_state: Any) -> None:
"""
Copy packages marked as bootinclude to the packages
type=bootstrap section in the target xml state. The package
will also be removed from the packages type=delete section
in the target xml state if present there
:param object target_state: XMLState instance
"""
target_packages_sections = \
target_state.get_bootstrap_packages_sections()
if target_packages_sections:
target_packages_section = \
target_packages_sections[0]
package_names_added = []
packages_sections = self.get_packages_sections(
['image', 'bootstrap', self.get_build_type_name()]
)
package_list = self.get_package_sections(
packages_sections
)
if package_list:
for package in package_list:
if package.package_section.get_bootinclude():
target_packages_section.add_package(
xml_parse.package(
name=package.package_section.get_name()
)
)
package_names_added.append(
package.package_section.get_name()
)
delete_packages_sections = target_state.get_packages_sections(
['delete']
)
package_list = self.get_package_sections(
delete_packages_sections
)
if package_list:
for package in package_list:
package_name = package.package_section.get_name()
if package_name in package_names_added:
package.packages_section.package.remove(
package.package_section
)
def copy_bootincluded_archives(self, target_state: Any) -> None:
"""
Copy archives marked as bootinclude to the packages type=bootstrap
section in the target xml state
:param object target_state: XMLState instance
"""
target_bootstrap_packages_sections = \
target_state.get_bootstrap_packages_sections()
if target_bootstrap_packages_sections:
target_bootstrap_packages_section = \
target_bootstrap_packages_sections[0]
packages_sections = self.get_packages_sections(
['image', 'bootstrap', self.get_build_type_name()]
)
for packages_section in packages_sections:
archive_list = packages_section.get_archive()
if archive_list:
for archive in archive_list:
if archive.get_bootinclude():
target_bootstrap_packages_section.add_archive(
xml_parse.archive(
name=archive.get_name()
)
)
def copy_bootdelete_packages(self, target_state: Any) -> None:
"""
Copy packages marked as bootdelete to the packages type=delete
section in the target xml state
:param object target_state: XMLState instance
"""
target_delete_packages_sections = target_state.get_packages_sections(
['delete']
)
if not target_delete_packages_sections:
target_delete_packages_sections = [
xml_parse.packages(type_='delete')
]
target_state.xml_data.add_packages(
target_delete_packages_sections[0]
)
target_delete_packages_section = \
target_delete_packages_sections[0]
packages_sections = self.get_packages_sections(
['image', 'bootstrap', self.get_build_type_name()]
)
package_list = self.get_package_sections(
packages_sections
)
if package_list:
for package in package_list:
if package.package_section.get_bootdelete():
target_delete_packages_section.add_package(
xml_parse.package(
name=package.package_section.get_name()
)
)
def get_distribution_name_from_boot_attribute(self) -> str:
"""
Extract the distribution name from the boot attribute of the
build type section.
If no boot attribute is configured or the contents does not
match the kiwi defined naming schema for boot image descriptions,
an exception is thrown
:return: lowercase distribution name
:rtype: str
"""
boot_attribute = self.build_type.get_boot()
if not boot_attribute:
raise KiwiDistributionNameError(
'No boot attribute to extract distribution name from found'
)
boot_attribute_format = '^.*-(.*)$'
boot_attribute_expression = re.match(
boot_attribute_format, boot_attribute
)
if not boot_attribute_expression:
raise KiwiDistributionNameError(
'Boot attribute "%s" does not match expected format %s' %
(boot_attribute, boot_attribute_format)
)
return boot_attribute_expression.group(1).lower()
def get_fs_mount_option_list(self) -> List:
"""
List of root filesystem mount options
The list contains one element with the information from the
fsmountoptions attribute. The value there is passed along to
the -o mount option
:return: max one element list with mount option string
:rtype: list
"""
option_list = []
mount_options = self.build_type.get_fsmountoptions()
if mount_options:
option_list = [mount_options]
return option_list
def get_fs_create_option_list(self) -> List:
"""
List of root filesystem creation options
The list contains elements with the information from the
fscreateoptions attribute string that got split into its
substring components
:return: list with create options
:rtype: list
"""
option_list = []
create_options = self.build_type.get_fscreateoptions()
if create_options:
option_list = create_options.split()
return option_list
def get_luks_credentials(self) -> Optional[str]:
"""
Return key or passphrase credentials to open the luks pool
:return: data
:rtype: str
"""
data = self.build_type.get_luks()
if data:
keyfile_name = None
try:
# try to interpret data as an URI
uri = Uri(data)
if not uri.is_remote():
keyfile_name = uri.translate()
except Exception:
# this doesn't look like a valid URI, continue as just data
pass
if keyfile_name:
try:
with open(keyfile_name) as keyfile:
return keyfile.read()
except Exception as issue:
raise KiwiFileAccessError(
f'Failed to read from {keyfile_name!r}: {issue}'
)
return data
def get_luks_format_options(self) -> List[str]:
"""
Return list of luks format options
:return: list of options
:rtype: list
"""
result = []
luksversion = self.build_type.get_luks_version()
luksformat = self.build_type.get_luksformat()
luks_pbkdf = self.build_type.get_luks_pbkdf()
if luksversion:
result.append('--type')
result.append(luksversion)
if luksformat:
for option in luksformat[0].get_option():
result.append(option.get_name())
if option.get_value():
result.append(option.get_value())
if luks_pbkdf:
# Allow to override the pbkdf algorithm that cryptsetup
# uses by default. Cryptsetup may use argon2i by default,
# which is not supported by all bootloaders.
result.append('--pbkdf')
result.append(luks_pbkdf)
return result
def get_derived_from_image_uri(self) -> List[Uri]:
"""
Uri object(s) of derived image if configured
Specific image types can be based on one ore more derived
images. This method returns the location of this image(s)
when configured in the XML description
:return: List of Uri instances
:rtype: list
"""
image_uris = []
derived_images = self.build_type.get_derived_from()
if derived_images:
for derived_image in derived_images.split(','):
image_uris.append(
Uri(derived_image, repo_type='container')
)
return image_uris
def set_derived_from_image_uri(self, uri: str) -> None:
"""
Set derived_from attribute to a new value
In order to set a new value the derived_from attribute
must be already present in the image configuration
:param str uri: URI
"""
if self.build_type.get_derived_from():
self.build_type.set_derived_from(uri)
else:
message = dedent('''\n
No derived_from attribute configured in image <type>
In order to set the uri {0} as base container reference
an initial derived_from attribute must be set in the
type section
''')
log.warning(message.format(uri))
def set_root_partition_uuid(self, uuid: str) -> None:
"""
Store PARTUUID provided in uuid as state information
:param str uuid: PARTUUID
"""
self.root_partition_uuid = uuid
def get_root_partition_uuid(self) -> Optional[str]:
"""
Return preserved PARTUUID
"""
return self.root_partition_uuid
def set_root_filesystem_uuid(self, uuid: str) -> None:
"""
Store UUID provided in uuid as state information
:param str uuid: UUID
"""
self.root_filesystem_uuid = uuid
def get_root_filesystem_uuid(self) -> Optional[str]:
"""
Return preserved UUID
"""
return self.root_filesystem_uuid
@staticmethod
def get_archives_target_dirs(
packages_sections_names: Optional[List[xml_parse.packages]]
) -> Dict:
"""
Dict of archive names and target dirs for packages section(s), if any
:return: archive names and its target dir
:rtype: dict
"""
result = {}
if packages_sections_names:
for package_section_name in packages_sections_names:
for archive in package_section_name.get_archive():
result[archive.get_name().strip()] = archive.get_target_dir()
return result
def get_bootstrap_archives_target_dirs(self) -> Dict:
"""
Dict of archive names and target dirs from the type="bootstrap"
packages section(s)
:return: archive names and its target dir
:rtype: dict
"""
return self.get_archives_target_dirs(
self.get_packages_sections(['bootstrap'])
)
def get_system_archives_target_dirs(self) -> Dict:
"""
Dict of archive names and its target dir from the packages sections matching
type="image" and type=build_type
:return: archive names and its target dir
:rtype: dict
"""
return self.get_archives_target_dirs(
self.get_packages_sections(['image', self.get_build_type_name()])
)
def _used_profiles(self, profiles=None):
"""
return list of profiles to use. The method looks up the
profiles section in the XML description and searches for
profiles matching the architecture. If no arch specifier
is set the profile is considered to be valid for any arch
If the profiles argument is not set only profiles
marked with the attribute import=true will be selected.
Profiles specified in the argument will take the highest
priority and causes to skip the lookup of import profiles
in the XML description
:param list profiles: selected profile names
"""
available_profiles = dict()
import_profiles = []
for profiles_section in self.xml_data.get_profiles():
for profile in profiles_section.get_profile():
if self.profile_matches_host_architecture(profile):
name = profile.get_name()
available_profiles[name] = profile
if profile.get_import():
import_profiles.append(name)
if not profiles:
return import_profiles
else:
resolved_profiles = []
for profile in profiles:
resolved_profiles += self._solve_profile_dependencies(
profile, available_profiles, resolved_profiles
)
return resolved_profiles
def _section_matches_host_architecture(self, section):
architectures = section.get_arch()
if architectures:
if self.host_architecture not in architectures.split(','):
return False
return True
def _match_docker_base_data(self):
container_config_section = self.get_build_type_containerconfig_section()
container_base = {}
if container_config_section:
name = container_config_section.get_name()
tag = container_config_section.get_tag()
maintainer = container_config_section.get_maintainer()
user = container_config_section.get_user()
workingdir = container_config_section.get_workingdir()
additional_names = container_config_section.get_additionalnames()
if name:
container_base['container_name'] = name
if tag:
container_base['container_tag'] = tag
if additional_names:
container_base['additional_names'] = additional_names.split(',')
if maintainer:
container_base['maintainer'] = maintainer
if user:
container_base['user'] = user
if workingdir:
container_base['workingdir'] = workingdir
return container_base
def _match_docker_entrypoint(self):
container_config_section = self.get_build_type_containerconfig_section()
container_entry = {}
if container_config_section:
entrypoint = container_config_section.get_entrypoint()
if entrypoint and entrypoint[0].get_execute():
container_entry['entry_command'] = [
entrypoint[0].get_execute()
]
argument_list = entrypoint[0].get_argument()
if argument_list:
for argument in argument_list:
container_entry['entry_command'].append(
argument.get_name()
)
elif entrypoint and entrypoint[0].get_clear():
container_entry['entry_command'] = []
return container_entry
def _match_docker_subcommand(self):
container_config_section = self.get_build_type_containerconfig_section()
container_subcommand = {}
if container_config_section:
subcommand = container_config_section.get_subcommand()
if subcommand and subcommand[0].get_execute():
container_subcommand['entry_subcommand'] = [
subcommand[0].get_execute()
]
argument_list = subcommand[0].get_argument()
if argument_list:
for argument in argument_list:
container_subcommand['entry_subcommand'].append(
argument.get_name()
)
elif subcommand and subcommand[0].get_clear():
container_subcommand['entry_subcommand'] = []
return container_subcommand
def _match_docker_expose_ports(self):
container_config_section = self.get_build_type_containerconfig_section()
container_expose = {}
if container_config_section:
expose = container_config_section.get_expose()
if expose and expose[0].get_port():
container_expose['expose_ports'] = []
for port in expose[0].get_port():
container_expose['expose_ports'].append(
format(port.get_number())
)
return container_expose
def _match_docker_volumes(self):
container_config_section = self.get_build_type_containerconfig_section()
container_volumes = {}
if container_config_section:
volumes = container_config_section.get_volumes()
if volumes and volumes[0].get_volume():
container_volumes['volumes'] = []
for volume in volumes[0].get_volume():
container_volumes['volumes'].append(volume.get_name())
return container_volumes
def _match_docker_stopsignal(self) -> dict:
container_config_section = self.get_build_type_containerconfig_section()
container_stopsignal = {}
if container_config_section:
stopsignal_section = container_config_section.get_stopsignal()
if stopsignal_section:
container_stopsignal['stopsignal'] = stopsignal_section[0]
return container_stopsignal
def _match_docker_environment(self):
container_config_section = self.get_build_type_containerconfig_section()
container_env = {}
if container_config_section:
environment = container_config_section.get_environment()
if environment and environment[0].get_env():
container_env['environment'] = {}
for env in environment[0].get_env():
container_env['environment'][env.get_name()] = \
env.get_value()
return container_env
def _match_docker_labels(self):
container_config_section = self.get_build_type_containerconfig_section()
container_labels = {}
if container_config_section:
labels = container_config_section.get_labels()
if labels and labels[0].get_label():
container_labels['labels'] = {}
for label in labels[0].get_label():
container_labels['labels'][label.get_name()] = \
label.get_value()
return container_labels
def _match_docker_history(self):
container_config_section = self.get_build_type_containerconfig_section()
container_history = {}
if container_config_section:
history = container_config_section.get_history()
if history:
container_history['history'] = {}
if history[0].get_created_by():
container_history['history']['created_by'] = \
history[0].get_created_by()
if history[0].get_author():
container_history['history']['author'] = \
history[0].get_author()
if history[0].get_launcher():
container_history['history']['launcher'] = \
history[0].get_launcher()
if history[0].get_application_id():
container_history['history']['application_id'] = \
history[0].get_application_id()
if history[0].get_package_version():
container_history['history']['package_version'] = \
history[0].get_package_version()
container_history['history']['comment'] = \
history[0].get_valueOf_()
return container_history
def _solve_profile_dependencies(
self, profile, available_profiles, current_profiles
):
if profile not in available_profiles:
raise KiwiProfileNotFound(
'profile {0} not found for host arch {1}'.format(
profile, self.host_architecture
)
)
profiles_to_add = []
if profile not in current_profiles:
profiles_to_add.append(profile)
for required in available_profiles[profile].get_requires():
if required.get_profile() not in current_profiles:
profiles_to_add += self._solve_profile_dependencies(
required.get_profile(), available_profiles,
current_profiles + profiles_to_add
)
return profiles_to_add
def _build_type_section(self, build_type=None):
"""
find type section matching build type and profiles or default
"""
# lookup all preferences sections for selected profiles
image_type_sections = []
for preferences in self.get_preferences_sections():
image_type_sections += preferences.get_type()
# lookup if build type matches provided type
if build_type:
for image_type in image_type_sections:
if build_type == image_type.get_image():
return image_type
raise KiwiTypeNotFound(
'Build type {0!r} not found for applied profiles: {1!r}'.format(
build_type, self.profiles
)
)
# lookup if build type matches primary type
for image_type in image_type_sections:
if image_type.get_primary():
return image_type
# build type is first type section in XML sequence
if image_type_sections:
return image_type_sections[0]
raise KiwiTypeNotFound(
'No build type defined with applied profiles: {0!r}'.format(
self.profiles
)
)
def _profiled(self, xml_abstract):
"""
return only those sections matching the instance stored
profile list from the given XML abstract. Sections without
a profile are wildcard sections and will be used in any
case
"""
result = []
for section in xml_abstract:
profiles = section.get_profiles()
if profiles:
for profile in profiles.split(','):
if self.profiles and profile in self.profiles:
result.append(section)
break
else:
result.append(section)
return result
def _to_volume_name(self, name):
name = name.strip()
name = re.sub(r'^\/+', r'', name)
name = name.replace('/', '_')
return name
def _to_mega_byte(self, size):
value = re.search(r'(\d+)([MG]*)', format(size))
if value:
number = value.group(1)
unit = value.group(2)
if unit == 'G':
return int(number) * 1024
else:
return int(number)
else:
return size
|