/usr/share/ganeti/2.15/ganeti/backend.py is in ganeti-2.15 2.15.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 | #
#
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Functions used by the node daemon
@var _ALLOWED_UPLOAD_FILES: denotes which files are accepted in
the L{UploadFile} function
@var _ALLOWED_CLEAN_DIRS: denotes which directories are accepted
in the L{_CleanDirectory} function
"""
# pylint: disable=E1103,C0302
# E1103: %s %r has no %r member (but some types could not be
# inferred), because the _TryOSFromDisk returns either (True, os_obj)
# or (False, "string") which confuses pylint
# C0302: This module has become too big and should be split up
import base64
import errno
import logging
import os
import os.path
import pycurl
import random
import re
import shutil
import signal
import stat
import tempfile
import time
import zlib
import contextlib
import collections
from ganeti import errors
from ganeti import http
from ganeti import utils
from ganeti import ssh
from ganeti import hypervisor
from ganeti.hypervisor import hv_base
from ganeti import constants
from ganeti.storage import bdev
from ganeti.storage import drbd
from ganeti.storage import extstorage
from ganeti.storage import filestorage
from ganeti import objects
from ganeti import ssconf
from ganeti import serializer
from ganeti import netutils
from ganeti import runtime
from ganeti import compat
from ganeti import pathutils
from ganeti import vcluster
from ganeti import ht
from ganeti.storage.base import BlockDev
from ganeti.storage.drbd import DRBD8
from ganeti import hooksmaster
import ganeti.metad as metad
_BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
_ALLOWED_CLEAN_DIRS = compat.UniqueFrozenset([
pathutils.DATA_DIR,
pathutils.JOB_QUEUE_ARCHIVE_DIR,
pathutils.QUEUE_DIR,
pathutils.CRYPTO_KEYS_DIR,
])
_MAX_SSL_CERT_VALIDITY = 7 * 24 * 60 * 60
_X509_KEY_FILE = "key"
_X509_CERT_FILE = "cert"
_IES_STATUS_FILE = "status"
_IES_PID_FILE = "pid"
_IES_CA_FILE = "ca"
#: Valid LVS output line regex
_LVSLINE_REGEX = re.compile(r"^ *([^|]+)\|([^|]+)\|([0-9.]+)\|([^|]{6,})\|?$")
# Actions for the master setup script
_MASTER_START = "start"
_MASTER_STOP = "stop"
#: Maximum file permissions for restricted command directory and executables
_RCMD_MAX_MODE = (stat.S_IRWXU |
stat.S_IRGRP | stat.S_IXGRP |
stat.S_IROTH | stat.S_IXOTH)
#: Delay before returning an error for restricted commands
_RCMD_INVALID_DELAY = 10
#: How long to wait to acquire lock for restricted commands (shorter than
#: L{_RCMD_INVALID_DELAY}) to reduce blockage of noded forks when many
#: command requests arrive
_RCMD_LOCK_TIMEOUT = _RCMD_INVALID_DELAY * 0.8
class RPCFail(Exception):
"""Class denoting RPC failure.
Its argument is the error message.
"""
def _GetInstReasonFilename(instance_name):
"""Path of the file containing the reason of the instance status change.
@type instance_name: string
@param instance_name: The name of the instance
@rtype: string
@return: The path of the file
"""
return utils.PathJoin(pathutils.INSTANCE_REASON_DIR, instance_name)
def _StoreInstReasonTrail(instance_name, trail):
"""Serialize a reason trail related to an instance change of state to file.
The exact location of the file depends on the name of the instance and on
the configuration of the Ganeti cluster defined at deploy time.
@type instance_name: string
@param instance_name: The name of the instance
@type trail: list of reasons
@param trail: reason trail
@rtype: None
"""
json = serializer.DumpJson(trail)
filename = _GetInstReasonFilename(instance_name)
utils.WriteFile(filename, data=json)
def _Fail(msg, *args, **kwargs):
"""Log an error and the raise an RPCFail exception.
This exception is then handled specially in the ganeti daemon and
turned into a 'failed' return type. As such, this function is a
useful shortcut for logging the error and returning it to the master
daemon.
@type msg: string
@param msg: the text of the exception
@raise RPCFail
"""
if args:
msg = msg % args
if "log" not in kwargs or kwargs["log"]: # if we should log this error
if "exc" in kwargs and kwargs["exc"]:
logging.exception(msg)
else:
logging.error(msg)
raise RPCFail(msg)
def _GetConfig():
"""Simple wrapper to return a SimpleStore.
@rtype: L{ssconf.SimpleStore}
@return: a SimpleStore instance
"""
return ssconf.SimpleStore()
def _GetSshRunner(cluster_name):
"""Simple wrapper to return an SshRunner.
@type cluster_name: str
@param cluster_name: the cluster name, which is needed
by the SshRunner constructor
@rtype: L{ssh.SshRunner}
@return: an SshRunner instance
"""
return ssh.SshRunner(cluster_name)
def _Decompress(data):
"""Unpacks data compressed by the RPC client.
@type data: list or tuple
@param data: Data sent by RPC client
@rtype: str
@return: Decompressed data
"""
assert isinstance(data, (list, tuple))
assert len(data) == 2
(encoding, content) = data
if encoding == constants.RPC_ENCODING_NONE:
return content
elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
return zlib.decompress(base64.b64decode(content))
else:
raise AssertionError("Unknown data encoding")
def _CleanDirectory(path, exclude=None):
"""Removes all regular files in a directory.
@type path: str
@param path: the directory to clean
@type exclude: list
@param exclude: list of files to be excluded, defaults
to the empty list
"""
if path not in _ALLOWED_CLEAN_DIRS:
_Fail("Path passed to _CleanDirectory not in allowed clean targets: '%s'",
path)
if not os.path.isdir(path):
return
if exclude is None:
exclude = []
else:
# Normalize excluded paths
exclude = [os.path.normpath(i) for i in exclude]
for rel_name in utils.ListVisibleFiles(path):
full_name = utils.PathJoin(path, rel_name)
if full_name in exclude:
continue
if os.path.isfile(full_name) and not os.path.islink(full_name):
utils.RemoveFile(full_name)
def _BuildUploadFileList():
"""Build the list of allowed upload files.
This is abstracted so that it's built only once at module import time.
"""
allowed_files = set([
pathutils.CLUSTER_CONF_FILE,
pathutils.ETC_HOSTS,
pathutils.SSH_KNOWN_HOSTS_FILE,
pathutils.VNC_PASSWORD_FILE,
pathutils.RAPI_CERT_FILE,
pathutils.SPICE_CERT_FILE,
pathutils.SPICE_CACERT_FILE,
pathutils.RAPI_USERS_FILE,
pathutils.CONFD_HMAC_KEY,
pathutils.CLUSTER_DOMAIN_SECRET_FILE,
])
for hv_name in constants.HYPER_TYPES:
hv_class = hypervisor.GetHypervisorClass(hv_name)
allowed_files.update(hv_class.GetAncillaryFiles()[0])
assert pathutils.FILE_STORAGE_PATHS_FILE not in allowed_files, \
"Allowed file storage paths should never be uploaded via RPC"
return frozenset(allowed_files)
_ALLOWED_UPLOAD_FILES = _BuildUploadFileList()
def JobQueuePurge():
"""Removes job queue files and archived jobs.
@rtype: tuple
@return: True, None
"""
_CleanDirectory(pathutils.QUEUE_DIR, exclude=[pathutils.JOB_QUEUE_LOCK_FILE])
_CleanDirectory(pathutils.JOB_QUEUE_ARCHIVE_DIR)
def GetMasterNodeName():
"""Returns the master node name.
@rtype: string
@return: name of the master node
@raise RPCFail: in case of errors
"""
try:
return _GetConfig().GetMasterNode()
except errors.ConfigurationError, err:
_Fail("Cluster configuration incomplete: %s", err, exc=True)
def RunLocalHooks(hook_opcode, hooks_path, env_builder_fn):
"""Decorator that runs hooks before and after the decorated function.
@type hook_opcode: string
@param hook_opcode: opcode of the hook
@type hooks_path: string
@param hooks_path: path of the hooks
@type env_builder_fn: function
@param env_builder_fn: function that returns a dictionary containing the
environment variables for the hooks. Will get all the parameters of the
decorated function.
@raise RPCFail: in case of pre-hook failure
"""
def decorator(fn):
def wrapper(*args, **kwargs):
_, myself = ssconf.GetMasterAndMyself()
nodes = ([myself], [myself]) # these hooks run locally
env_fn = compat.partial(env_builder_fn, *args, **kwargs)
cfg = _GetConfig()
hr = HooksRunner()
hm = hooksmaster.HooksMaster(hook_opcode, hooks_path, nodes,
hr.RunLocalHooks, None, env_fn, None,
logging.warning, cfg.GetClusterName(),
cfg.GetMasterNode())
hm.RunPhase(constants.HOOKS_PHASE_PRE)
result = fn(*args, **kwargs)
hm.RunPhase(constants.HOOKS_PHASE_POST)
return result
return wrapper
return decorator
def _BuildMasterIpEnv(master_params, use_external_mip_script=None):
"""Builds environment variables for master IP hooks.
@type master_params: L{objects.MasterNetworkParameters}
@param master_params: network parameters of the master
@type use_external_mip_script: boolean
@param use_external_mip_script: whether to use an external master IP
address setup script (unused, but necessary per the implementation of the
_RunLocalHooks decorator)
"""
# pylint: disable=W0613
ver = netutils.IPAddress.GetVersionFromAddressFamily(master_params.ip_family)
env = {
"MASTER_NETDEV": master_params.netdev,
"MASTER_IP": master_params.ip,
"MASTER_NETMASK": str(master_params.netmask),
"CLUSTER_IP_VERSION": str(ver),
}
return env
def _RunMasterSetupScript(master_params, action, use_external_mip_script):
"""Execute the master IP address setup script.
@type master_params: L{objects.MasterNetworkParameters}
@param master_params: network parameters of the master
@type action: string
@param action: action to pass to the script. Must be one of
L{backend._MASTER_START} or L{backend._MASTER_STOP}
@type use_external_mip_script: boolean
@param use_external_mip_script: whether to use an external master IP
address setup script
@raise backend.RPCFail: if there are errors during the execution of the
script
"""
env = _BuildMasterIpEnv(master_params)
if use_external_mip_script:
setup_script = pathutils.EXTERNAL_MASTER_SETUP_SCRIPT
else:
setup_script = pathutils.DEFAULT_MASTER_SETUP_SCRIPT
result = utils.RunCmd([setup_script, action], env=env, reset_env=True)
if result.failed:
_Fail("Failed to %s the master IP. Script return value: %s, output: '%s'" %
(action, result.exit_code, result.output), log=True)
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNUP, "master-ip-turnup",
_BuildMasterIpEnv)
def ActivateMasterIp(master_params, use_external_mip_script):
"""Activate the IP address of the master daemon.
@type master_params: L{objects.MasterNetworkParameters}
@param master_params: network parameters of the master
@type use_external_mip_script: boolean
@param use_external_mip_script: whether to use an external master IP
address setup script
@raise RPCFail: in case of errors during the IP startup
"""
_RunMasterSetupScript(master_params, _MASTER_START,
use_external_mip_script)
def StartMasterDaemons(no_voting):
"""Activate local node as master node.
The function will start the master daemons (ganeti-masterd and ganeti-rapi).
@type no_voting: boolean
@param no_voting: whether to start ganeti-masterd without a node vote
but still non-interactively
@rtype: None
"""
if no_voting:
masterd_args = "--no-voting --yes-do-it"
else:
masterd_args = ""
env = {
"EXTRA_MASTERD_ARGS": masterd_args,
}
result = utils.RunCmd([pathutils.DAEMON_UTIL, "start-master"], env=env)
if result.failed:
msg = "Can't start Ganeti master: %s" % result.output
logging.error(msg)
_Fail(msg)
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNDOWN, "master-ip-turndown",
_BuildMasterIpEnv)
def DeactivateMasterIp(master_params, use_external_mip_script):
"""Deactivate the master IP on this node.
@type master_params: L{objects.MasterNetworkParameters}
@param master_params: network parameters of the master
@type use_external_mip_script: boolean
@param use_external_mip_script: whether to use an external master IP
address setup script
@raise RPCFail: in case of errors during the IP turndown
"""
_RunMasterSetupScript(master_params, _MASTER_STOP,
use_external_mip_script)
def StopMasterDaemons():
"""Stop the master daemons on this node.
Stop the master daemons (ganeti-masterd and ganeti-rapi) on this node.
@rtype: None
"""
# TODO: log and report back to the caller the error failures; we
# need to decide in which case we fail the RPC for this
result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-master"])
if result.failed:
logging.error("Could not stop Ganeti master, command %s had exitcode %s"
" and error %s",
result.cmd, result.exit_code, result.output)
def ChangeMasterNetmask(old_netmask, netmask, master_ip, master_netdev):
"""Change the netmask of the master IP.
@param old_netmask: the old value of the netmask
@param netmask: the new value of the netmask
@param master_ip: the master IP
@param master_netdev: the master network device
"""
if old_netmask == netmask:
return
if not netutils.IPAddress.Own(master_ip):
_Fail("The master IP address is not up, not attempting to change its"
" netmask")
result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "add",
"%s/%s" % (master_ip, netmask),
"dev", master_netdev, "label",
"%s:0" % master_netdev])
if result.failed:
_Fail("Could not set the new netmask on the master IP address")
result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "del",
"%s/%s" % (master_ip, old_netmask),
"dev", master_netdev, "label",
"%s:0" % master_netdev])
if result.failed:
_Fail("Could not bring down the master IP address with the old netmask")
def EtcHostsModify(mode, host, ip):
"""Modify a host entry in /etc/hosts.
@param mode: The mode to operate. Either add or remove entry
@param host: The host to operate on
@param ip: The ip associated with the entry
"""
if mode == constants.ETC_HOSTS_ADD:
if not ip:
RPCFail("Mode 'add' needs 'ip' parameter, but parameter not"
" present")
utils.AddHostToEtcHosts(host, ip)
elif mode == constants.ETC_HOSTS_REMOVE:
if ip:
RPCFail("Mode 'remove' does not allow 'ip' parameter, but"
" parameter is present")
utils.RemoveHostFromEtcHosts(host)
else:
RPCFail("Mode not supported")
def LeaveCluster(modify_ssh_setup):
"""Cleans up and remove the current node.
This function cleans up and prepares the current node to be removed
from the cluster.
If processing is successful, then it raises an
L{errors.QuitGanetiException} which is used as a special case to
shutdown the node daemon.
@param modify_ssh_setup: boolean
"""
_CleanDirectory(pathutils.DATA_DIR)
_CleanDirectory(pathutils.CRYPTO_KEYS_DIR)
JobQueuePurge()
if modify_ssh_setup:
try:
priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.SSH_LOGIN_USER)
ssh.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key))
utils.RemoveFile(priv_key)
utils.RemoveFile(pub_key)
except errors.OpExecError:
logging.exception("Error while processing ssh files")
try:
utils.RemoveFile(pathutils.CONFD_HMAC_KEY)
utils.RemoveFile(pathutils.RAPI_CERT_FILE)
utils.RemoveFile(pathutils.SPICE_CERT_FILE)
utils.RemoveFile(pathutils.SPICE_CACERT_FILE)
utils.RemoveFile(pathutils.NODED_CERT_FILE)
except: # pylint: disable=W0702
logging.exception("Error while removing cluster secrets")
utils.StopDaemon(constants.CONFD)
utils.StopDaemon(constants.MOND)
utils.StopDaemon(constants.KVMD)
# Raise a custom exception (handled in ganeti-noded)
raise errors.QuitGanetiException(True, "Shutdown scheduled")
def _CheckStorageParams(params, num_params):
"""Performs sanity checks for storage parameters.
@type params: list
@param params: list of storage parameters
@type num_params: int
@param num_params: expected number of parameters
"""
if params is None:
raise errors.ProgrammerError("No storage parameters for storage"
" reporting is provided.")
if not isinstance(params, list):
raise errors.ProgrammerError("The storage parameters are not of type"
" list: '%s'" % params)
if not len(params) == num_params:
raise errors.ProgrammerError("Did not receive the expected number of"
"storage parameters: expected %s,"
" received '%s'" % (num_params, len(params)))
def _CheckLvmStorageParams(params):
"""Performs sanity check for the 'exclusive storage' flag.
@see: C{_CheckStorageParams}
"""
_CheckStorageParams(params, 1)
excl_stor = params[0]
if not isinstance(params[0], bool):
raise errors.ProgrammerError("Exclusive storage parameter is not"
" boolean: '%s'." % excl_stor)
return excl_stor
def _GetLvmVgSpaceInfo(name, params):
"""Wrapper around C{_GetVgInfo} which checks the storage parameters.
@type name: string
@param name: name of the volume group
@type params: list
@param params: list of storage parameters, which in this case should be
containing only one for exclusive storage
"""
excl_stor = _CheckLvmStorageParams(params)
return _GetVgInfo(name, excl_stor)
def _GetVgInfo(
name, excl_stor, info_fn=bdev.LogicalVolume.GetVGInfo):
"""Retrieves information about a LVM volume group.
"""
# TODO: GetVGInfo supports returning information for multiple VGs at once
vginfo = info_fn([name], excl_stor)
if vginfo:
vg_free = int(round(vginfo[0][0], 0))
vg_size = int(round(vginfo[0][1], 0))
else:
vg_free = None
vg_size = None
return {
"type": constants.ST_LVM_VG,
"name": name,
"storage_free": vg_free,
"storage_size": vg_size,
}
def _GetLvmPvSpaceInfo(name, params):
"""Wrapper around C{_GetVgSpindlesInfo} with sanity checks.
@see: C{_GetLvmVgSpaceInfo}
"""
excl_stor = _CheckLvmStorageParams(params)
return _GetVgSpindlesInfo(name, excl_stor)
def _GetVgSpindlesInfo(
name, excl_stor, info_fn=bdev.LogicalVolume.GetVgSpindlesInfo):
"""Retrieves information about spindles in an LVM volume group.
@type name: string
@param name: VG name
@type excl_stor: bool
@param excl_stor: exclusive storage
@rtype: dict
@return: dictionary whose keys are "name", "vg_free", "vg_size" for VG name,
free spindles, total spindles respectively
"""
if excl_stor:
(vg_free, vg_size) = info_fn(name)
else:
vg_free = 0
vg_size = 0
return {
"type": constants.ST_LVM_PV,
"name": name,
"storage_free": vg_free,
"storage_size": vg_size,
}
def _GetHvInfo(name, hvparams, get_hv_fn=hypervisor.GetHypervisor):
"""Retrieves node information from a hypervisor.
The information returned depends on the hypervisor. Common items:
- vg_size is the size of the configured volume group in MiB
- vg_free is the free size of the volume group in MiB
- memory_dom0 is the memory allocated for domain0 in MiB
- memory_free is the currently available (free) ram in MiB
- memory_total is the total number of ram in MiB
- hv_version: the hypervisor version, if available
@type hvparams: dict of string
@param hvparams: the hypervisor's hvparams
"""
return get_hv_fn(name).GetNodeInfo(hvparams=hvparams)
def _GetHvInfoAll(hv_specs, get_hv_fn=hypervisor.GetHypervisor):
"""Retrieves node information for all hypervisors.
See C{_GetHvInfo} for information on the output.
@type hv_specs: list of pairs (string, dict of strings)
@param hv_specs: list of pairs of a hypervisor's name and its hvparams
"""
if hv_specs is None:
return None
result = []
for hvname, hvparams in hv_specs:
result.append(_GetHvInfo(hvname, hvparams, get_hv_fn))
return result
def _GetNamedNodeInfo(names, fn):
"""Calls C{fn} for all names in C{names} and returns a dictionary.
@rtype: None or dict
"""
if names is None:
return None
else:
return map(fn, names)
def GetNodeInfo(storage_units, hv_specs):
"""Gives back a hash with different information about the node.
@type storage_units: list of tuples (string, string, list)
@param storage_units: List of tuples (storage unit, identifier, parameters) to
ask for disk space information. In case of lvm-vg, the identifier is
the VG name. The parameters can contain additional, storage-type-specific
parameters, for example exclusive storage for lvm storage.
@type hv_specs: list of pairs (string, dict of strings)
@param hv_specs: list of pairs of a hypervisor's name and its hvparams
@rtype: tuple; (string, None/dict, None/dict)
@return: Tuple containing boot ID, volume group information and hypervisor
information
"""
bootid = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n")
storage_info = _GetNamedNodeInfo(
storage_units,
(lambda (storage_type, storage_key, storage_params):
_ApplyStorageInfoFunction(storage_type, storage_key, storage_params)))
hv_info = _GetHvInfoAll(hv_specs)
return (bootid, storage_info, hv_info)
def _GetFileStorageSpaceInfo(path, params):
"""Wrapper around filestorage.GetSpaceInfo.
The purpose of this wrapper is to call filestorage.GetFileStorageSpaceInfo
and ignore the *args parameter to not leak it into the filestorage
module's code.
@see: C{filestorage.GetFileStorageSpaceInfo} for description of the
parameters.
"""
_CheckStorageParams(params, 0)
return filestorage.GetFileStorageSpaceInfo(path)
# FIXME: implement storage reporting for all missing storage types.
_STORAGE_TYPE_INFO_FN = {
constants.ST_BLOCK: None,
constants.ST_DISKLESS: None,
constants.ST_EXT: None,
constants.ST_FILE: _GetFileStorageSpaceInfo,
constants.ST_LVM_PV: _GetLvmPvSpaceInfo,
constants.ST_LVM_VG: _GetLvmVgSpaceInfo,
constants.ST_SHARED_FILE: None,
constants.ST_GLUSTER: None,
constants.ST_RADOS: None,
}
def _ApplyStorageInfoFunction(storage_type, storage_key, *args):
"""Looks up and applies the correct function to calculate free and total
storage for the given storage type.
@type storage_type: string
@param storage_type: the storage type for which the storage shall be reported.
@type storage_key: string
@param storage_key: identifier of a storage unit, e.g. the volume group name
of an LVM storage unit
@type args: any
@param args: various parameters that can be used for storage reporting. These
parameters and their semantics vary from storage type to storage type and
are just propagated in this function.
@return: the results of the application of the storage space function (see
_STORAGE_TYPE_INFO_FN) if storage space reporting is implemented for that
storage type
@raises NotImplementedError: for storage types who don't support space
reporting yet
"""
fn = _STORAGE_TYPE_INFO_FN[storage_type]
if fn is not None:
return fn(storage_key, *args)
else:
raise NotImplementedError
def _CheckExclusivePvs(pvi_list):
"""Check that PVs are not shared among LVs
@type pvi_list: list of L{objects.LvmPvInfo} objects
@param pvi_list: information about the PVs
@rtype: list of tuples (string, list of strings)
@return: offending volumes, as tuples: (pv_name, [lv1_name, lv2_name...])
"""
res = []
for pvi in pvi_list:
if len(pvi.lv_list) > 1:
res.append((pvi.name, pvi.lv_list))
return res
def _VerifyHypervisors(what, vm_capable, result, all_hvparams,
get_hv_fn=hypervisor.GetHypervisor):
"""Verifies the hypervisor. Appends the results to the 'results' list.
@type what: C{dict}
@param what: a dictionary of things to check
@type vm_capable: boolean
@param vm_capable: whether or not this node is vm capable
@type result: dict
@param result: dictionary of verification results; results of the
verifications in this function will be added here
@type all_hvparams: dict of dict of string
@param all_hvparams: dictionary mapping hypervisor names to hvparams
@type get_hv_fn: function
@param get_hv_fn: function to retrieve the hypervisor, to improve testability
"""
if not vm_capable:
return
if constants.NV_HYPERVISOR in what:
result[constants.NV_HYPERVISOR] = {}
for hv_name in what[constants.NV_HYPERVISOR]:
hvparams = all_hvparams[hv_name]
try:
val = get_hv_fn(hv_name).Verify(hvparams=hvparams)
except errors.HypervisorError, err:
val = "Error while checking hypervisor: %s" % str(err)
result[constants.NV_HYPERVISOR][hv_name] = val
def _VerifyHvparams(what, vm_capable, result,
get_hv_fn=hypervisor.GetHypervisor):
"""Verifies the hvparams. Appends the results to the 'results' list.
@type what: C{dict}
@param what: a dictionary of things to check
@type vm_capable: boolean
@param vm_capable: whether or not this node is vm capable
@type result: dict
@param result: dictionary of verification results; results of the
verifications in this function will be added here
@type get_hv_fn: function
@param get_hv_fn: function to retrieve the hypervisor, to improve testability
"""
if not vm_capable:
return
if constants.NV_HVPARAMS in what:
result[constants.NV_HVPARAMS] = []
for source, hv_name, hvparms in what[constants.NV_HVPARAMS]:
try:
logging.info("Validating hv %s, %s", hv_name, hvparms)
get_hv_fn(hv_name).ValidateParameters(hvparms)
except errors.HypervisorError, err:
result[constants.NV_HVPARAMS].append((source, hv_name, str(err)))
def _VerifyInstanceList(what, vm_capable, result, all_hvparams):
"""Verifies the instance list.
@type what: C{dict}
@param what: a dictionary of things to check
@type vm_capable: boolean
@param vm_capable: whether or not this node is vm capable
@type result: dict
@param result: dictionary of verification results; results of the
verifications in this function will be added here
@type all_hvparams: dict of dict of string
@param all_hvparams: dictionary mapping hypervisor names to hvparams
"""
if constants.NV_INSTANCELIST in what and vm_capable:
# GetInstanceList can fail
try:
val = GetInstanceList(what[constants.NV_INSTANCELIST],
all_hvparams=all_hvparams)
except RPCFail, err:
val = str(err)
result[constants.NV_INSTANCELIST] = val
def _VerifyNodeInfo(what, vm_capable, result, all_hvparams):
"""Verifies the node info.
@type what: C{dict}
@param what: a dictionary of things to check
@type vm_capable: boolean
@param vm_capable: whether or not this node is vm capable
@type result: dict
@param result: dictionary of verification results; results of the
verifications in this function will be added here
@type all_hvparams: dict of dict of string
@param all_hvparams: dictionary mapping hypervisor names to hvparams
"""
if constants.NV_HVINFO in what and vm_capable:
hvname = what[constants.NV_HVINFO]
hyper = hypervisor.GetHypervisor(hvname)
hvparams = all_hvparams[hvname]
result[constants.NV_HVINFO] = hyper.GetNodeInfo(hvparams=hvparams)
def _VerifyClientCertificate(cert_file=pathutils.NODED_CLIENT_CERT_FILE):
"""Verify the existance and validity of the client SSL certificate.
Also, verify that the client certificate is not self-signed. Self-
signed client certificates stem from Ganeti versions 2.12.0 - 2.12.4
and should be replaced by client certificates signed by the server
certificate. Hence we output a warning when we encounter a self-signed
one.
"""
create_cert_cmd = "gnt-cluster renew-crypto --new-node-certificates"
if not os.path.exists(cert_file):
return (constants.CV_ERROR,
"The client certificate does not exist. Run '%s' to create"
" client certificates for all nodes." % create_cert_cmd)
(errcode, msg) = utils.VerifyCertificate(cert_file)
if errcode is not None:
return (errcode, msg)
(errcode, msg) = utils.IsCertificateSelfSigned(cert_file)
if errcode is not None:
return (errcode, msg)
# if everything is fine, we return the digest to be compared to the config
return (None, utils.GetCertificateDigest(cert_filename=cert_file))
def _VerifySshSetup(node_status_list, my_name,
pub_key_file=pathutils.SSH_PUB_KEYS):
"""Verifies the state of the SSH key files.
@type node_status_list: list of tuples
@param node_status_list: list of nodes of the cluster associated with a
couple of flags: (uuid, name, is_master_candidate,
is_potential_master_candidate, online)
@type my_name: str
@param my_name: name of this node
@type pub_key_file: str
@param pub_key_file: filename of the public key file
"""
if node_status_list is None:
return ["No node list to check against the pub_key_file received."]
my_status_list = [(my_uuid, name, mc, pot_mc, online) for
(my_uuid, name, mc, pot_mc, online)
in node_status_list if name == my_name]
if len(my_status_list) == 0:
return ["Cannot find node information for node '%s'." % my_name]
(my_uuid, _, _, potential_master_candidate, online) = \
my_status_list[0]
result = []
if not os.path.exists(pub_key_file):
result.append("The public key file '%s' does not exist. Consider running"
" 'gnt-cluster renew-crypto --new-ssh-keys"
" [--no-ssh-key-check]' to fix this." % pub_key_file)
return result
pot_mc_uuids = [uuid for (uuid, _, _, _, _) in node_status_list]
offline_nodes = [uuid for (uuid, _, _, _, online) in node_status_list
if not online]
pub_keys = ssh.QueryPubKeyFile(None)
if potential_master_candidate:
# Check that the set of potential master candidates matches the
# public key file
pub_uuids_set = set(pub_keys.keys()) - set(offline_nodes)
pot_mc_uuids_set = set(pot_mc_uuids) - set(offline_nodes)
missing_uuids = set([])
if pub_uuids_set != pot_mc_uuids_set:
unknown_uuids = pub_uuids_set - pot_mc_uuids_set
if unknown_uuids:
result.append("The following node UUIDs are listed in the public key"
" file on node '%s', but are not potential master"
" candidates: %s."
% (my_name, ", ".join(list(unknown_uuids))))
missing_uuids = pot_mc_uuids_set - pub_uuids_set
if missing_uuids:
result.append("The following node UUIDs of potential master candidates"
" are missing in the public key file on node %s: %s."
% (my_name, ", ".join(list(missing_uuids))))
(_, key_files) = \
ssh.GetAllUserFiles(constants.SSH_LOGIN_USER, mkdir=False, dircheck=False)
(_, dsa_pub_key_filename) = key_files[constants.SSHK_DSA]
my_keys = pub_keys[my_uuid]
dsa_pub_key = utils.ReadFile(dsa_pub_key_filename)
if dsa_pub_key.strip() not in my_keys:
result.append("The dsa key of node %s does not match this node's key"
" in the pub key file." % (my_name))
if len(my_keys) != 1:
result.append("There is more than one key for node %s in the public key"
" file." % my_name)
else:
if len(pub_keys.keys()) > 0:
result.append("The public key file of node '%s' is not empty, although"
" the node is not a potential master candidate."
% my_name)
# Check that all master candidate keys are in the authorized_keys file
(auth_key_file, _) = \
ssh.GetAllUserFiles(constants.SSH_LOGIN_USER, mkdir=False, dircheck=False)
for (uuid, name, mc, _, online) in node_status_list:
if not online:
continue
if uuid in missing_uuids:
continue
if mc:
for key in pub_keys[uuid]:
if not ssh.HasAuthorizedKey(auth_key_file, key):
result.append("A SSH key of master candidate '%s' (UUID: '%s') is"
" not in the 'authorized_keys' file of node '%s'."
% (name, uuid, my_name))
else:
for key in pub_keys[uuid]:
if name != my_name and ssh.HasAuthorizedKey(auth_key_file, key):
result.append("A SSH key of normal node '%s' (UUID: '%s') is in the"
" 'authorized_keys' file of node '%s'."
% (name, uuid, my_name))
if name == my_name and not ssh.HasAuthorizedKey(auth_key_file, key):
result.append("A SSH key of normal node '%s' (UUID: '%s') is not"
" in the 'authorized_keys' file of itself."
% (my_name, uuid))
return result
def _VerifySshClutter(node_status_list, my_name):
"""Verifies that the 'authorized_keys' files are not cluttered up.
@type node_status_list: list of tuples
@param node_status_list: list of nodes of the cluster associated with a
couple of flags: (uuid, name, is_master_candidate,
is_potential_master_candidate, online)
@type my_name: str
@param my_name: name of this node
"""
result = []
(auth_key_file, _) = \
ssh.GetAllUserFiles(constants.SSH_LOGIN_USER, mkdir=False, dircheck=False)
node_names = [name for (_, name, _, _) in node_status_list]
multiple_occurrences = ssh.CheckForMultipleKeys(auth_key_file, node_names)
if multiple_occurrences:
msg = "There are hosts which have more than one SSH key stored for the" \
" same user in the 'authorized_keys' file of node %s. This can be" \
" due to an unsuccessful operation which cluttered up the" \
" 'authorized_keys' file. We recommend to clean this up manually. " \
% my_name
for host, occ in multiple_occurrences.items():
msg += "Entry for '%s' in lines %s. " % (host, utils.CommaJoin(occ))
result.append(msg)
return result
def VerifyNode(what, cluster_name, all_hvparams, node_groups, groups_cfg):
"""Verify the status of the local node.
Based on the input L{what} parameter, various checks are done on the
local node.
If the I{filelist} key is present, this list of
files is checksummed and the file/checksum pairs are returned.
If the I{nodelist} key is present, we check that we have
connectivity via ssh with the target nodes (and check the hostname
report).
If the I{node-net-test} key is present, we check that we have
connectivity to the given nodes via both primary IP and, if
applicable, secondary IPs.
@type what: C{dict}
@param what: a dictionary of things to check:
- filelist: list of files for which to compute checksums
- nodelist: list of nodes we should check ssh communication with
- node-net-test: list of nodes we should check node daemon port
connectivity with
- hypervisor: list with hypervisors to run the verify for
@type cluster_name: string
@param cluster_name: the cluster's name
@type all_hvparams: dict of dict of strings
@param all_hvparams: a dictionary mapping hypervisor names to hvparams
@type node_groups: a dict of strings
@param node_groups: node _names_ mapped to their group uuids (it's enough to
have only those nodes that are in `what["nodelist"]`)
@type groups_cfg: a dict of dict of strings
@param groups_cfg: a dictionary mapping group uuids to their configuration
@rtype: dict
@return: a dictionary with the same keys as the input dict, and
values representing the result of the checks
"""
result = {}
my_name = netutils.Hostname.GetSysName()
port = netutils.GetDaemonPort(constants.NODED)
vm_capable = my_name not in what.get(constants.NV_NONVMNODES, [])
_VerifyHypervisors(what, vm_capable, result, all_hvparams)
_VerifyHvparams(what, vm_capable, result)
if constants.NV_FILELIST in what:
fingerprints = utils.FingerprintFiles(map(vcluster.LocalizeVirtualPath,
what[constants.NV_FILELIST]))
result[constants.NV_FILELIST] = \
dict((vcluster.MakeVirtualPath(key), value)
for (key, value) in fingerprints.items())
if constants.NV_CLIENT_CERT in what:
result[constants.NV_CLIENT_CERT] = _VerifyClientCertificate()
if constants.NV_SSH_SETUP in what:
result[constants.NV_SSH_SETUP] = \
_VerifySshSetup(what[constants.NV_SSH_SETUP], my_name)
if constants.NV_SSH_CLUTTER in what:
result[constants.NV_SSH_CLUTTER] = \
_VerifySshClutter(what[constants.NV_SSH_SETUP], my_name)
if constants.NV_NODELIST in what:
(nodes, bynode, mcs) = what[constants.NV_NODELIST]
# Add nodes from other groups (different for each node)
try:
nodes.extend(bynode[my_name])
except KeyError:
pass
# Use a random order
random.shuffle(nodes)
# Try to contact all nodes
val = {}
for node in nodes:
params = groups_cfg.get(node_groups.get(node))
ssh_port = params["ndparams"].get(constants.ND_SSH_PORT)
logging.debug("Ssh port %s (None = default) for node %s",
str(ssh_port), node)
# We only test if master candidates can communicate to other nodes.
# We cannot test if normal nodes cannot communicate with other nodes,
# because the administrator might have installed additional SSH keys,
# over which Ganeti has no power.
if my_name in mcs:
success, message = _GetSshRunner(cluster_name). \
VerifyNodeHostname(node, ssh_port)
if not success:
val[node] = message
result[constants.NV_NODELIST] = val
if constants.NV_NODENETTEST in what:
result[constants.NV_NODENETTEST] = tmp = {}
my_pip = my_sip = None
for name, pip, sip in what[constants.NV_NODENETTEST]:
if name == my_name:
my_pip = pip
my_sip = sip
break
if not my_pip:
tmp[my_name] = ("Can't find my own primary/secondary IP"
" in the node list")
else:
for name, pip, sip in what[constants.NV_NODENETTEST]:
fail = []
if not netutils.TcpPing(pip, port, source=my_pip):
fail.append("primary")
if sip != pip:
if not netutils.TcpPing(sip, port, source=my_sip):
fail.append("secondary")
if fail:
tmp[name] = ("failure using the %s interface(s)" %
" and ".join(fail))
if constants.NV_MASTERIP in what:
# FIXME: add checks on incoming data structures (here and in the
# rest of the function)
master_name, master_ip = what[constants.NV_MASTERIP]
if master_name == my_name:
source = constants.IP4_ADDRESS_LOCALHOST
else:
source = None
result[constants.NV_MASTERIP] = netutils.TcpPing(master_ip, port,
source=source)
if constants.NV_USERSCRIPTS in what:
result[constants.NV_USERSCRIPTS] = \
[script for script in what[constants.NV_USERSCRIPTS]
if not utils.IsExecutable(script)]
if constants.NV_OOB_PATHS in what:
result[constants.NV_OOB_PATHS] = tmp = []
for path in what[constants.NV_OOB_PATHS]:
try:
st = os.stat(path)
except OSError, err:
tmp.append("error stating out of band helper: %s" % err)
else:
if stat.S_ISREG(st.st_mode):
if stat.S_IMODE(st.st_mode) & stat.S_IXUSR:
tmp.append(None)
else:
tmp.append("out of band helper %s is not executable" % path)
else:
tmp.append("out of band helper %s is not a file" % path)
if constants.NV_LVLIST in what and vm_capable:
try:
val = GetVolumeList(utils.ListVolumeGroups().keys())
except RPCFail, err:
val = str(err)
result[constants.NV_LVLIST] = val
_VerifyInstanceList(what, vm_capable, result, all_hvparams)
if constants.NV_VGLIST in what and vm_capable:
result[constants.NV_VGLIST] = utils.ListVolumeGroups()
if constants.NV_PVLIST in what and vm_capable:
check_exclusive_pvs = constants.NV_EXCLUSIVEPVS in what
val = bdev.LogicalVolume.GetPVInfo(what[constants.NV_PVLIST],
filter_allocatable=False,
include_lvs=check_exclusive_pvs)
if check_exclusive_pvs:
result[constants.NV_EXCLUSIVEPVS] = _CheckExclusivePvs(val)
for pvi in val:
# Avoid sending useless data on the wire
pvi.lv_list = []
result[constants.NV_PVLIST] = map(objects.LvmPvInfo.ToDict, val)
if constants.NV_VERSION in what:
result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION,
constants.RELEASE_VERSION)
_VerifyNodeInfo(what, vm_capable, result, all_hvparams)
if constants.NV_DRBDVERSION in what and vm_capable:
try:
drbd_version = DRBD8.GetProcInfo().GetVersionString()
except errors.BlockDeviceError, err:
logging.warning("Can't get DRBD version", exc_info=True)
drbd_version = str(err)
result[constants.NV_DRBDVERSION] = drbd_version
if constants.NV_DRBDLIST in what and vm_capable:
try:
used_minors = drbd.DRBD8.GetUsedDevs()
except errors.BlockDeviceError, err:
logging.warning("Can't get used minors list", exc_info=True)
used_minors = str(err)
result[constants.NV_DRBDLIST] = used_minors
if constants.NV_DRBDHELPER in what and vm_capable:
status = True
try:
payload = drbd.DRBD8.GetUsermodeHelper()
except errors.BlockDeviceError, err:
logging.error("Can't get DRBD usermode helper: %s", str(err))
status = False
payload = str(err)
result[constants.NV_DRBDHELPER] = (status, payload)
if constants.NV_NODESETUP in what:
result[constants.NV_NODESETUP] = tmpr = []
if not os.path.isdir("/sys/block") or not os.path.isdir("/sys/class/net"):
tmpr.append("The sysfs filesytem doesn't seem to be mounted"
" under /sys, missing required directories /sys/block"
" and /sys/class/net")
if (not os.path.isdir("/proc/sys") or
not os.path.isfile("/proc/sysrq-trigger")):
tmpr.append("The procfs filesystem doesn't seem to be mounted"
" under /proc, missing required directory /proc/sys and"
" the file /proc/sysrq-trigger")
if constants.NV_TIME in what:
result[constants.NV_TIME] = utils.SplitTime(time.time())
if constants.NV_OSLIST in what and vm_capable:
result[constants.NV_OSLIST] = DiagnoseOS()
if constants.NV_BRIDGES in what and vm_capable:
result[constants.NV_BRIDGES] = [bridge
for bridge in what[constants.NV_BRIDGES]
if not utils.BridgeExists(bridge)]
if what.get(constants.NV_ACCEPTED_STORAGE_PATHS) == my_name:
result[constants.NV_ACCEPTED_STORAGE_PATHS] = \
filestorage.ComputeWrongFileStoragePaths()
if what.get(constants.NV_FILE_STORAGE_PATH):
pathresult = filestorage.CheckFileStoragePath(
what[constants.NV_FILE_STORAGE_PATH])
if pathresult:
result[constants.NV_FILE_STORAGE_PATH] = pathresult
if what.get(constants.NV_SHARED_FILE_STORAGE_PATH):
pathresult = filestorage.CheckFileStoragePath(
what[constants.NV_SHARED_FILE_STORAGE_PATH])
if pathresult:
result[constants.NV_SHARED_FILE_STORAGE_PATH] = pathresult
return result
def GetCryptoTokens(token_requests):
"""Perform actions on the node's cryptographic tokens.
Token types can be 'ssl' or 'ssh'. So far only some actions are implemented
for 'ssl'. Action 'get' returns the digest of the public client ssl
certificate. Action 'create' creates a new client certificate and private key
and also returns the digest of the certificate. The third parameter of a
token request are optional parameters for the actions, so far only the
filename is supported.
@type token_requests: list of tuples of (string, string, dict), where the
first string is in constants.CRYPTO_TYPES, the second in
constants.CRYPTO_ACTIONS. The third parameter is a dictionary of string
to string.
@param token_requests: list of requests of cryptographic tokens and actions
to perform on them. The actions come with a dictionary of options.
@rtype: list of tuples (string, string)
@return: list of tuples of the token type and the public crypto token
"""
tokens = []
for (token_type, action, _) in token_requests:
if token_type not in constants.CRYPTO_TYPES:
raise errors.ProgrammerError("Token type '%s' not supported." %
token_type)
if action not in constants.CRYPTO_ACTIONS:
raise errors.ProgrammerError("Action '%s' is not supported." %
action)
if token_type == constants.CRYPTO_TYPE_SSL_DIGEST:
tokens.append((token_type,
utils.GetCertificateDigest()))
return tokens
def EnsureDaemon(daemon_name, run):
"""Ensures the given daemon is running or stopped.
@type daemon_name: string
@param daemon_name: name of the daemon (e.g., constants.KVMD)
@type run: bool
@param run: whether to start or stop the daemon
@rtype: bool
@return: 'True' if daemon successfully started/stopped,
'False' otherwise
"""
allowed_daemons = [constants.KVMD]
if daemon_name not in allowed_daemons:
fn = lambda _: False
elif run:
fn = utils.EnsureDaemon
else:
fn = utils.StopDaemon
return fn(daemon_name)
def _InitSshUpdateData(data, noded_cert_file, ssconf_store):
(_, noded_cert) = \
utils.ExtractX509Certificate(utils.ReadFile(noded_cert_file))
data[constants.SSHS_NODE_DAEMON_CERTIFICATE] = noded_cert
cluster_name = ssconf_store.GetClusterName()
data[constants.SSHS_CLUSTER_NAME] = cluster_name
def AddNodeSshKey(node_uuid, node_name,
potential_master_candidates,
to_authorized_keys=False,
to_public_keys=False,
get_public_keys=False,
pub_key_file=pathutils.SSH_PUB_KEYS,
ssconf_store=None,
noded_cert_file=pathutils.NODED_CERT_FILE,
run_cmd_fn=ssh.RunSshCmdWithStdin):
"""Distributes a node's public SSH key across the cluster.
Note that this function should only be executed on the master node, which
then will copy the new node's key to all nodes in the cluster via SSH.
Also note: at least one of the flags C{to_authorized_keys},
C{to_public_keys}, and C{get_public_keys} has to be set to C{True} for
the function to actually perform any actions.
@type node_uuid: str
@param node_uuid: the UUID of the node whose key is added
@type node_name: str
@param node_name: the name of the node whose key is added
@type potential_master_candidates: list of str
@param potential_master_candidates: list of node names of potential master
candidates; this should match the list of uuids in the public key file
@type to_authorized_keys: boolean
@param to_authorized_keys: whether the key should be added to the
C{authorized_keys} file of all nodes
@type to_public_keys: boolean
@param to_public_keys: whether the keys should be added to the public key file
@type get_public_keys: boolean
@param get_public_keys: whether the node should add the clusters' public keys
to its {ganeti_pub_keys} file
"""
node_list = [SshAddNodeInfo(name=node_name, uuid=node_uuid,
to_authorized_keys=to_authorized_keys,
to_public_keys=to_public_keys,
get_public_keys=get_public_keys)]
return AddNodeSshKeyBulk(node_list,
potential_master_candidates,
pub_key_file=pub_key_file,
ssconf_store=ssconf_store,
noded_cert_file=noded_cert_file,
run_cmd_fn=run_cmd_fn)
# Node info named tuple specifically for the use with AddNodeSshKeyBulk
SshAddNodeInfo = collections.namedtuple(
"SshAddNodeInfo",
["uuid",
"name",
"to_authorized_keys",
"to_public_keys",
"get_public_keys"])
def AddNodeSshKeyBulk(node_list,
potential_master_candidates,
pub_key_file=pathutils.SSH_PUB_KEYS,
ssconf_store=None,
noded_cert_file=pathutils.NODED_CERT_FILE,
run_cmd_fn=ssh.RunSshCmdWithStdin):
"""Distributes a node's public SSH key across the cluster.
Note that this function should only be executed on the master node, which
then will copy the new node's key to all nodes in the cluster via SSH.
Also note: at least one of the flags C{to_authorized_keys},
C{to_public_keys}, and C{get_public_keys} has to be set to C{True} for
the function to actually perform any actions.
@type node_list: list of SshAddNodeInfo tuples
@param node_list: list of tuples containing the necessary node information for
adding their keys
@type potential_master_candidates: list of str
@param potential_master_candidates: list of node names of potential master
candidates; this should match the list of uuids in the public key file
"""
# whether there are any keys to be added or retrieved at all
to_authorized_keys = any([node_info.to_authorized_keys for node_info in
node_list])
to_public_keys = any([node_info.to_public_keys for node_info in
node_list])
get_public_keys = any([node_info.get_public_keys for node_info in
node_list])
# assure that at least one of those flags is true, as the function would
# not do anything otherwise
assert (to_authorized_keys or to_public_keys or get_public_keys)
if not ssconf_store:
ssconf_store = ssconf.SimpleStore()
for node_info in node_list:
# replacement not necessary for keys that are not supposed to be in the
# list of public keys
if not node_info.to_public_keys:
continue
# Check and fix sanity of key file
keys_by_name = ssh.QueryPubKeyFile([node_info.name], key_file=pub_key_file)
keys_by_uuid = ssh.QueryPubKeyFile([node_info.uuid], key_file=pub_key_file)
if (not keys_by_name or node_info.name not in keys_by_name) \
and (not keys_by_uuid or node_info.uuid not in keys_by_uuid):
raise errors.SshUpdateError(
"No keys found for the new node '%s' (UUID %s) in the list of public"
" SSH keys, neither for the name or the UUID" %
(node_info.name, node_info.uuid))
else:
if node_info.name in keys_by_name:
# Replace the name by UUID in the file as the name should only be used
# temporarily
ssh.ReplaceNameByUuid(node_info.uuid, node_info.name,
error_fn=errors.SshUpdateError,
key_file=pub_key_file)
# Retrieve updated map of UUIDs to keys
keys_by_uuid = ssh.QueryPubKeyFile(
[node_info.uuid for node_info in node_list], key_file=pub_key_file)
# Update the master node's key files
(auth_key_file, _) = \
ssh.GetAllUserFiles(constants.SSH_LOGIN_USER, mkdir=False, dircheck=False)
for node_info in node_list:
if node_info.to_authorized_keys:
ssh.AddAuthorizedKeys(auth_key_file, keys_by_uuid[node_info.uuid])
base_data = {}
_InitSshUpdateData(base_data, noded_cert_file, ssconf_store)
cluster_name = base_data[constants.SSHS_CLUSTER_NAME]
ssh_port_map = ssconf_store.GetSshPortMap()
# Update the target nodes themselves
for node_info in node_list:
logging.debug("Updating SSH key files of target node '%s'.", node_info.name)
if node_info.get_public_keys:
node_data = {}
_InitSshUpdateData(node_data, noded_cert_file, ssconf_store)
all_keys = ssh.QueryPubKeyFile(None, key_file=pub_key_file)
node_data[constants.SSHS_SSH_PUBLIC_KEYS] = \
(constants.SSHS_OVERRIDE, all_keys)
try:
utils.RetryByNumberOfTimes(
constants.SSHS_MAX_RETRIES,
errors.SshUpdateError,
run_cmd_fn, cluster_name, node_info.name, pathutils.SSH_UPDATE,
ssh_port_map.get(node_info.name), node_data,
debug=False, verbose=False, use_cluster_key=False,
ask_key=False, strict_host_check=False)
except errors.SshUpdateError as e:
# Clean up the master's public key file if adding key fails
if node_info.to_public_keys:
ssh.RemovePublicKey(node_info.uuid)
raise e
# Update all nodes except master and the target nodes
keys_by_uuid_auth = ssh.QueryPubKeyFile(
[node_info.uuid for node_info in node_list
if node_info.to_authorized_keys],
key_file=pub_key_file)
if to_authorized_keys:
base_data[constants.SSHS_SSH_AUTHORIZED_KEYS] = \
(constants.SSHS_ADD, keys_by_uuid_auth)
pot_mc_data = base_data.copy()
keys_by_uuid_pub = ssh.QueryPubKeyFile(
[node_info.uuid for node_info in node_list
if node_info.to_public_keys],
key_file=pub_key_file)
if to_public_keys:
pot_mc_data[constants.SSHS_SSH_PUBLIC_KEYS] = \
(constants.SSHS_REPLACE_OR_ADD, keys_by_uuid_pub)
all_nodes = ssconf_store.GetNodeList()
master_node = ssconf_store.GetMasterNode()
online_nodes = ssconf_store.GetOnlineNodeList()
node_errors = []
for node in all_nodes:
if node == master_node:
logging.debug("Skipping master node '%s'.", master_node)
continue
if node not in online_nodes:
logging.debug("Skipping offline node '%s'.", node)
continue
if node in potential_master_candidates:
logging.debug("Updating SSH key files of node '%s'.", node)
try:
utils.RetryByNumberOfTimes(
constants.SSHS_MAX_RETRIES,
errors.SshUpdateError,
run_cmd_fn, cluster_name, node, pathutils.SSH_UPDATE,
ssh_port_map.get(node), pot_mc_data,
debug=False, verbose=False, use_cluster_key=False,
ask_key=False, strict_host_check=False)
except errors.SshUpdateError as last_exception:
error_msg = ("When adding the key of node '%s', updating SSH key"
" files of node '%s' failed after %s retries."
" Not trying again. Last error was: %s." %
(node, node_info.name, constants.SSHS_MAX_RETRIES,
last_exception))
node_errors.append((node, error_msg))
# We only log the error and don't throw an exception, because
# one unreachable node shall not abort the entire procedure.
logging.error(error_msg)
else:
if to_authorized_keys:
run_cmd_fn(cluster_name, node, pathutils.SSH_UPDATE,
ssh_port_map.get(node), base_data,
debug=False, verbose=False, use_cluster_key=False,
ask_key=False, strict_host_check=False)
return node_errors
def RemoveNodeSshKey(node_uuid, node_name,
master_candidate_uuids,
potential_master_candidates,
master_uuid=None,
keys_to_remove=None,
from_authorized_keys=False,
from_public_keys=False,
clear_authorized_keys=False,
clear_public_keys=False,
pub_key_file=pathutils.SSH_PUB_KEYS,
ssconf_store=None,
noded_cert_file=pathutils.NODED_CERT_FILE,
readd=False,
run_cmd_fn=ssh.RunSshCmdWithStdin):
"""Removes the node's SSH keys from the key files and distributes those.
Note that at least one of the flags C{from_authorized_keys},
C{from_public_keys}, C{clear_authorized_keys}, and C{clear_public_keys}
has to be set to C{True} for the function to perform any action at all.
Not doing so will trigger an assertion in the function.
@type node_uuid: str
@param node_uuid: UUID of the node whose key is removed
@type node_name: str
@param node_name: name of the node whose key is remove
@type master_candidate_uuids: list of str
@param master_candidate_uuids: list of UUIDs of the current master candidates
@type potential_master_candidates: list of str
@param potential_master_candidates: list of names of potential master
candidates
@type keys_to_remove: dict of str to list of str
@param keys_to_remove: a dictionary mapping node UUIDS to lists of SSH keys
to be removed. This list is supposed to be used only if the keys are not
in the public keys file. This is for example the case when removing a
master node's key.
@type from_authorized_keys: boolean
@param from_authorized_keys: whether or not the key should be removed
from the C{authorized_keys} file
@type from_public_keys: boolean
@param from_public_keys: whether or not the key should be remove from
the C{ganeti_pub_keys} file
@type clear_authorized_keys: boolean
@param clear_authorized_keys: whether or not the C{authorized_keys} file
should be cleared on the node whose keys are removed
@type clear_public_keys: boolean
@param clear_public_keys: whether to clear the node's C{ganeti_pub_key} file
@type readd: boolean
@param readd: whether this is called during a readd operation.
@rtype: list of string
@returns: list of feedback messages
"""
# Non-disruptive error messages, list of (node, msg) pairs
result_msgs = []
# Make sure at least one of these flags is true.
if not (from_authorized_keys or from_public_keys or clear_authorized_keys
or clear_public_keys):
raise errors.SshUpdateError("No removal from any key file was requested.")
if not ssconf_store:
ssconf_store = ssconf.SimpleStore()
master_node = ssconf_store.GetMasterNode()
ssh_port_map = ssconf_store.GetSshPortMap()
if from_authorized_keys or from_public_keys:
if keys_to_remove:
keys = keys_to_remove
else:
keys = ssh.QueryPubKeyFile([node_uuid], key_file=pub_key_file)
if (not keys or node_uuid not in keys) and not readd:
raise errors.SshUpdateError("Node '%s' not found in the list of public"
" SSH keys. It seems someone tries to"
" remove a key from outside the cluster!"
% node_uuid)
# During an upgrade all nodes have the master key. In this case we
# should not remove it to avoid accidentally shutting down cluster
# SSH communication
master_keys = None
if master_uuid:
master_keys = ssh.QueryPubKeyFile([master_uuid], key_file=pub_key_file)
for master_key in master_keys:
if master_key in keys[node_uuid]:
keys[node_uuid].remove(master_key)
if node_name == master_node and not keys_to_remove:
raise errors.SshUpdateError("Cannot remove the master node's keys.")
if node_uuid in keys:
base_data = {}
_InitSshUpdateData(base_data, noded_cert_file, ssconf_store)
cluster_name = base_data[constants.SSHS_CLUSTER_NAME]
if from_authorized_keys:
base_data[constants.SSHS_SSH_AUTHORIZED_KEYS] = \
(constants.SSHS_REMOVE, keys)
(auth_key_file, _) = \
ssh.GetAllUserFiles(constants.SSH_LOGIN_USER, mkdir=False,
dircheck=False)
ssh.RemoveAuthorizedKeys(auth_key_file, keys[node_uuid])
pot_mc_data = base_data.copy()
if from_public_keys:
pot_mc_data[constants.SSHS_SSH_PUBLIC_KEYS] = \
(constants.SSHS_REMOVE, keys)
ssh.RemovePublicKey(node_uuid, key_file=pub_key_file)
all_nodes = ssconf_store.GetNodeList()
online_nodes = ssconf_store.GetOnlineNodeList()
logging.debug("Removing key of node '%s' from all nodes but itself and"
" master.", node_name)
for node in all_nodes:
if node == master_node:
logging.debug("Skipping master node '%s'.", master_node)
continue
if node not in online_nodes:
logging.debug("Skipping offline node '%s'.", node)
continue
if node == node_name:
logging.debug("Skipping node itself '%s'.", node_name)
continue
ssh_port = ssh_port_map.get(node)
if not ssh_port:
raise errors.OpExecError("No SSH port information available for"
" node '%s', map: %s." %
(node, ssh_port_map))
error_msg_final = ("When removing the key of node '%s', updating the"
" SSH key files of node '%s' failed. Last error"
" was: %s.")
if node in potential_master_candidates:
logging.debug("Updating key setup of potential master candidate node"
" %s.", node)
try:
utils.RetryByNumberOfTimes(
constants.SSHS_MAX_RETRIES,
errors.SshUpdateError,
run_cmd_fn, cluster_name, node, pathutils.SSH_UPDATE,
ssh_port, pot_mc_data,
debug=False, verbose=False, use_cluster_key=False,
ask_key=False, strict_host_check=False)
except errors.SshUpdateError as last_exception:
error_msg = error_msg_final % (
node_name, node, last_exception)
result_msgs.append((node, error_msg))
logging.error(error_msg)
else:
if from_authorized_keys:
logging.debug("Updating key setup of normal node %s.", node)
try:
utils.RetryByNumberOfTimes(
constants.SSHS_MAX_RETRIES,
errors.SshUpdateError,
run_cmd_fn, cluster_name, node, pathutils.SSH_UPDATE,
ssh_port, base_data,
debug=False, verbose=False, use_cluster_key=False,
ask_key=False, strict_host_check=False)
except errors.SshUpdateError as last_exception:
error_msg = error_msg_final % (
node_name, node, last_exception)
result_msgs.append((node, error_msg))
logging.error(error_msg)
if clear_authorized_keys or from_public_keys or clear_public_keys:
data = {}
_InitSshUpdateData(data, noded_cert_file, ssconf_store)
cluster_name = data[constants.SSHS_CLUSTER_NAME]
ssh_port = ssh_port_map.get(node_name)
if not ssh_port:
raise errors.OpExecError("No SSH port information available for"
" node '%s', which is leaving the cluster.")
if clear_authorized_keys:
# The 'authorized_keys' file is not solely managed by Ganeti. Therefore,
# we have to specify exactly which keys to clear to leave keys untouched
# that were not added by Ganeti.
other_master_candidate_uuids = [uuid for uuid in master_candidate_uuids
if uuid != node_uuid]
candidate_keys = ssh.QueryPubKeyFile(other_master_candidate_uuids,
key_file=pub_key_file)
data[constants.SSHS_SSH_AUTHORIZED_KEYS] = \
(constants.SSHS_REMOVE, candidate_keys)
if clear_public_keys:
data[constants.SSHS_SSH_PUBLIC_KEYS] = \
(constants.SSHS_CLEAR, {})
elif from_public_keys:
# Since clearing the public keys subsumes removing just a single key,
# we only do it of clear_public_keys is 'False'.
if keys[node_uuid]:
data[constants.SSHS_SSH_PUBLIC_KEYS] = \
(constants.SSHS_REMOVE, keys)
# If we have no changes to any keyfile, just return
if not (constants.SSHS_SSH_PUBLIC_KEYS in data or
constants.SSHS_SSH_AUTHORIZED_KEYS in data):
return
logging.debug("Updating SSH key setup of target node '%s'.", node_name)
try:
utils.RetryByNumberOfTimes(
constants.SSHS_MAX_RETRIES,
errors.SshUpdateError,
run_cmd_fn, cluster_name, node_name, pathutils.SSH_UPDATE,
ssh_port, data,
debug=False, verbose=False, use_cluster_key=False,
ask_key=False, strict_host_check=False)
except errors.SshUpdateError as last_exception:
result_msgs.append(
(node_name,
("Removing SSH keys from node '%s' failed."
" This can happen when the node is already unreachable."
" Error: %s" % (node_name, last_exception))))
return result_msgs
def _GenerateNodeSshKey(node_uuid, node_name, ssh_port_map,
pub_key_file=pathutils.SSH_PUB_KEYS,
ssconf_store=None,
noded_cert_file=pathutils.NODED_CERT_FILE,
run_cmd_fn=ssh.RunSshCmdWithStdin,
suffix=""):
"""Generates the root SSH key pair on the node.
@type node_uuid: str
@param node_uuid: UUID of the node whose key is removed
@type node_name: str
@param node_name: name of the node whose key is remove
@type ssh_port_map: dict of str to int
@param ssh_port_map: mapping of node names to their SSH port
"""
if not ssconf_store:
ssconf_store = ssconf.SimpleStore()
keys_by_uuid = ssh.QueryPubKeyFile([node_uuid], key_file=pub_key_file)
if not keys_by_uuid or node_uuid not in keys_by_uuid:
raise errors.SshUpdateError("Node %s (UUID: %s) whose key is requested to"
" be regenerated is not registered in the"
" public keys file." % (node_name, node_uuid))
data = {}
_InitSshUpdateData(data, noded_cert_file, ssconf_store)
cluster_name = data[constants.SSHS_CLUSTER_NAME]
data[constants.SSHS_GENERATE] = {constants.SSHS_SUFFIX: suffix}
run_cmd_fn(cluster_name, node_name, pathutils.SSH_UPDATE,
ssh_port_map.get(node_name), data,
debug=False, verbose=False, use_cluster_key=False,
ask_key=False, strict_host_check=False)
def _GetMasterNodeUUID(node_uuid_name_map, master_node_name):
master_node_uuids = [node_uuid for (node_uuid, node_name)
in node_uuid_name_map
if node_name == master_node_name]
if len(master_node_uuids) != 1:
raise errors.SshUpdateError("No (unique) master UUID found. Master node"
" name: '%s', Master UUID: '%s'" %
(master_node_name, master_node_uuids))
return master_node_uuids[0]
def _GetOldMasterKeys(master_node_uuid, pub_key_file):
old_master_keys_by_uuid = ssh.QueryPubKeyFile([master_node_uuid],
key_file=pub_key_file)
if not old_master_keys_by_uuid:
raise errors.SshUpdateError("No public key of the master node (UUID '%s')"
" found, not generating a new key."
% master_node_uuid)
return old_master_keys_by_uuid
def _GetNewMasterKey(root_keyfiles, master_node_uuid):
new_master_keys = []
for (_, (_, public_key_file)) in root_keyfiles.items():
public_key_dir = os.path.dirname(public_key_file)
public_key_file_tmp_filename = \
os.path.splitext(os.path.basename(public_key_file))[0] \
+ constants.SSHS_MASTER_SUFFIX + ".pub"
public_key_path_tmp = os.path.join(public_key_dir,
public_key_file_tmp_filename)
if os.path.exists(public_key_path_tmp):
# for some key types, there might not be any keys
key = utils.ReadFile(public_key_path_tmp)
new_master_keys.append(key)
if not new_master_keys:
raise errors.SshUpdateError("Cannot find any type of temporary SSH key.")
return {master_node_uuid: new_master_keys}
def _ReplaceMasterKeyOnMaster(root_keyfiles):
number_of_moves = 0
for (_, (private_key_file, public_key_file)) in root_keyfiles.items():
key_dir = os.path.dirname(public_key_file)
private_key_file_tmp = \
os.path.basename(private_key_file) + constants.SSHS_MASTER_SUFFIX
public_key_file_tmp = private_key_file_tmp + ".pub"
private_key_path_tmp = os.path.join(key_dir,
private_key_file_tmp)
public_key_path_tmp = os.path.join(key_dir,
public_key_file_tmp)
if os.path.exists(public_key_file):
utils.CreateBackup(public_key_file)
utils.RemoveFile(public_key_file)
if os.path.exists(private_key_file):
utils.CreateBackup(private_key_file)
utils.RemoveFile(private_key_file)
if os.path.exists(public_key_path_tmp) and \
os.path.exists(private_key_path_tmp):
# for some key types, there might not be any keys
shutil.move(public_key_path_tmp, public_key_file)
shutil.move(private_key_path_tmp, private_key_file)
number_of_moves += 1
if not number_of_moves:
raise errors.SshUpdateError("Could not move at least one master SSH key.")
def RenewSshKeys(node_uuids, node_names, master_candidate_uuids,
potential_master_candidates,
pub_key_file=pathutils.SSH_PUB_KEYS,
ssconf_store=None,
noded_cert_file=pathutils.NODED_CERT_FILE,
run_cmd_fn=ssh.RunSshCmdWithStdin):
"""Renews all SSH keys and updates authorized_keys and ganeti_pub_keys.
@type node_uuids: list of str
@param node_uuids: list of node UUIDs whose keys should be renewed
@type node_names: list of str
@param node_names: list of node names whose keys should be removed. This list
should match the C{node_uuids} parameter
@type master_candidate_uuids: list of str
@param master_candidate_uuids: list of UUIDs of master candidates or
master node
@type pub_key_file: str
@param pub_key_file: file path of the the public key file
@type noded_cert_file: str
@param noded_cert_file: path of the noded SSL certificate file
@type run_cmd_fn: function
@param run_cmd_fn: function to run commands on remote nodes via SSH
@raises ProgrammerError: if node_uuids and node_names don't match;
SshUpdateError if a node's key is missing from the public key file,
if a node's new SSH key could not be fetched from it, if there is
none or more than one entry in the public key list for the master
node.
"""
if not ssconf_store:
ssconf_store = ssconf.SimpleStore()
cluster_name = ssconf_store.GetClusterName()
if not len(node_uuids) == len(node_names):
raise errors.ProgrammerError("List of nodes UUIDs and node names"
" does not match in length.")
(_, root_keyfiles) = \
ssh.GetAllUserFiles(constants.SSH_LOGIN_USER, mkdir=False, dircheck=False)
(_, dsa_pub_keyfile) = root_keyfiles[constants.SSHK_DSA]
old_master_key = utils.ReadFile(dsa_pub_keyfile)
node_uuid_name_map = zip(node_uuids, node_names)
master_node_name = ssconf_store.GetMasterNode()
master_node_uuid = _GetMasterNodeUUID(node_uuid_name_map, master_node_name)
ssh_port_map = ssconf_store.GetSshPortMap()
# List of all node errors that happened, but which did not abort the
# procedure as a whole. It is important that this is a list to have a
# somewhat chronological history of events.
all_node_errors = []
# process non-master nodes
# keys to add in bulk at the end
node_keys_to_add = []
for node_uuid, node_name in node_uuid_name_map:
if node_name == master_node_name:
continue
master_candidate = node_uuid in master_candidate_uuids
potential_master_candidate = node_name in potential_master_candidates
keys_by_uuid = ssh.QueryPubKeyFile([node_uuid], key_file=pub_key_file)
if not keys_by_uuid:
raise errors.SshUpdateError("No public key of node %s (UUID %s) found,"
" not generating a new key."
% (node_name, node_uuid))
if master_candidate:
logging.debug("Fetching old SSH key from node '%s'.", node_name)
old_pub_key = ssh.ReadRemoteSshPubKeys(dsa_pub_keyfile,
node_name, cluster_name,
ssh_port_map[node_name],
False, # ask_key
False) # key_check
if old_pub_key != old_master_key:
# If we are already in a multi-key setup (that is past Ganeti 2.12),
# we can safely remove the old key of the node. Otherwise, we cannot
# remove that node's key, because it is also the master node's key
# and that would terminate all communication from the master to the
# node.
logging.debug("Removing SSH key of node '%s'.", node_name)
node_errors = RemoveNodeSshKey(
node_uuid, node_name, master_candidate_uuids,
potential_master_candidates,
master_uuid=master_node_uuid, from_authorized_keys=master_candidate,
from_public_keys=False, clear_authorized_keys=False,
clear_public_keys=False)
if node_errors:
all_node_errors = all_node_errors + node_errors
else:
logging.debug("Old key of node '%s' is the same as the current master"
" key. Not deleting that key on the node.", node_name)
logging.debug("Generating new SSH key for node '%s'.", node_name)
_GenerateNodeSshKey(node_uuid, node_name, ssh_port_map,
pub_key_file=pub_key_file,
ssconf_store=ssconf_store,
noded_cert_file=noded_cert_file,
run_cmd_fn=run_cmd_fn)
try:
logging.debug("Fetching newly created SSH key from node '%s'.", node_name)
pub_key = ssh.ReadRemoteSshPubKeys(dsa_pub_keyfile,
node_name, cluster_name,
ssh_port_map[node_name],
False, # ask_key
False) # key_check
except:
raise errors.SshUpdateError("Could not fetch key of node %s"
" (UUID %s)" % (node_name, node_uuid))
if potential_master_candidate:
ssh.RemovePublicKey(node_uuid, key_file=pub_key_file)
ssh.AddPublicKey(node_uuid, pub_key, key_file=pub_key_file)
logging.debug("Add ssh key of node '%s'.", node_name)
node_info = SshAddNodeInfo(name=node_name,
uuid=node_uuid,
to_authorized_keys=master_candidate,
to_public_keys=potential_master_candidate,
get_public_keys=True)
node_keys_to_add.append(node_info)
node_errors = AddNodeSshKeyBulk(
node_keys_to_add, potential_master_candidates,
pub_key_file=pub_key_file, ssconf_store=ssconf_store,
noded_cert_file=noded_cert_file,
run_cmd_fn=run_cmd_fn)
if node_errors:
all_node_errors = all_node_errors + node_errors
# Renewing the master node's key
# Preserve the old keys for now
old_master_keys_by_uuid = _GetOldMasterKeys(master_node_uuid, pub_key_file)
# Generate a new master key with a suffix, don't touch the old one for now
logging.debug("Generate new ssh key of master.")
_GenerateNodeSshKey(master_node_uuid, master_node_name, ssh_port_map,
pub_key_file=pub_key_file,
ssconf_store=ssconf_store,
noded_cert_file=noded_cert_file,
run_cmd_fn=run_cmd_fn,
suffix=constants.SSHS_MASTER_SUFFIX)
# Read newly created master key
new_master_key_dict = _GetNewMasterKey(root_keyfiles, master_node_uuid)
# Replace master key in the master nodes' public key file
ssh.RemovePublicKey(master_node_uuid, key_file=pub_key_file)
for pub_key in new_master_key_dict[master_node_uuid]:
ssh.AddPublicKey(master_node_uuid, pub_key, key_file=pub_key_file)
# Add new master key to all node's public and authorized keys
logging.debug("Add new master key to all nodes.")
node_errors = AddNodeSshKey(
master_node_uuid, master_node_name, potential_master_candidates,
to_authorized_keys=True, to_public_keys=True,
get_public_keys=False, pub_key_file=pub_key_file,
ssconf_store=ssconf_store, noded_cert_file=noded_cert_file,
run_cmd_fn=run_cmd_fn)
if node_errors:
all_node_errors = all_node_errors + node_errors
# Remove the old key file and rename the new key to the non-temporary filename
_ReplaceMasterKeyOnMaster(root_keyfiles)
# Remove old key from authorized keys
(auth_key_file, _) = \
ssh.GetAllUserFiles(constants.SSH_LOGIN_USER, mkdir=False, dircheck=False)
ssh.RemoveAuthorizedKeys(auth_key_file,
old_master_keys_by_uuid[master_node_uuid])
# Remove the old key from all node's authorized keys file
logging.debug("Remove the old master key from all nodes.")
node_errors = RemoveNodeSshKey(
master_node_uuid, master_node_name, master_candidate_uuids,
potential_master_candidates,
keys_to_remove=old_master_keys_by_uuid, from_authorized_keys=True,
from_public_keys=False, clear_authorized_keys=False,
clear_public_keys=False)
if node_errors:
all_node_errors = all_node_errors + node_errors
return all_node_errors
def GetBlockDevSizes(devices):
"""Return the size of the given block devices
@type devices: list
@param devices: list of block device nodes to query
@rtype: dict
@return:
dictionary of all block devices under /dev (key). The value is their
size in MiB.
{'/dev/disk/by-uuid/123456-12321231-312312-312': 124}
"""
DEV_PREFIX = "/dev/"
blockdevs = {}
for devpath in devices:
if not utils.IsBelowDir(DEV_PREFIX, devpath):
continue
try:
st = os.stat(devpath)
except EnvironmentError, err:
logging.warning("Error stat()'ing device %s: %s", devpath, str(err))
continue
if stat.S_ISBLK(st.st_mode):
result = utils.RunCmd(["blockdev", "--getsize64", devpath])
if result.failed:
# We don't want to fail, just do not list this device as available
logging.warning("Cannot get size for block device %s", devpath)
continue
size = int(result.stdout) / (1024 * 1024)
blockdevs[devpath] = size
return blockdevs
def GetVolumeList(vg_names):
"""Compute list of logical volumes and their size.
@type vg_names: list
@param vg_names: the volume groups whose LVs we should list, or
empty for all volume groups
@rtype: dict
@return:
dictionary of all partions (key) with value being a tuple of
their size (in MiB), inactive and online status::
{'xenvg/test1': ('20.06', True, True)}
in case of errors, a string is returned with the error
details.
"""
lvs = {}
sep = "|"
if not vg_names:
vg_names = []
result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
"--separator=%s" % sep,
"-ovg_name,lv_name,lv_size,lv_attr"] + vg_names)
if result.failed:
_Fail("Failed to list logical volumes, lvs output: %s", result.output)
for line in result.stdout.splitlines():
line = line.strip()
match = _LVSLINE_REGEX.match(line)
if not match:
logging.error("Invalid line returned from lvs output: '%s'", line)
continue
vg_name, name, size, attr = match.groups()
inactive = attr[4] == "-"
online = attr[5] == "o"
virtual = attr[0] == "v"
if virtual:
# we don't want to report such volumes as existing, since they
# don't really hold data
continue
lvs[vg_name + "/" + name] = (size, inactive, online)
return lvs
def ListVolumeGroups():
"""List the volume groups and their size.
@rtype: dict
@return: dictionary with keys volume name and values the
size of the volume
"""
return utils.ListVolumeGroups()
def NodeVolumes():
"""List all volumes on this node.
@rtype: list
@return:
A list of dictionaries, each having four keys:
- name: the logical volume name,
- size: the size of the logical volume
- dev: the physical device on which the LV lives
- vg: the volume group to which it belongs
In case of errors, we return an empty list and log the
error.
Note that since a logical volume can live on multiple physical
volumes, the resulting list might include a logical volume
multiple times.
"""
result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
"--separator=|",
"--options=lv_name,lv_size,devices,vg_name"])
if result.failed:
_Fail("Failed to list logical volumes, lvs output: %s",
result.output)
def parse_dev(dev):
return dev.split("(")[0]
def handle_dev(dev):
return [parse_dev(x) for x in dev.split(",")]
def map_line(line):
line = [v.strip() for v in line]
return [{"name": line[0], "size": line[1],
"dev": dev, "vg": line[3]} for dev in handle_dev(line[2])]
all_devs = []
for line in result.stdout.splitlines():
if line.count("|") >= 3:
all_devs.extend(map_line(line.split("|")))
else:
logging.warning("Strange line in the output from lvs: '%s'", line)
return all_devs
def BridgesExist(bridges_list):
"""Check if a list of bridges exist on the current node.
@rtype: boolean
@return: C{True} if all of them exist, C{False} otherwise
"""
missing = []
for bridge in bridges_list:
if not utils.BridgeExists(bridge):
missing.append(bridge)
if missing:
_Fail("Missing bridges %s", utils.CommaJoin(missing))
def GetInstanceListForHypervisor(hname, hvparams=None,
get_hv_fn=hypervisor.GetHypervisor):
"""Provides a list of instances of the given hypervisor.
@type hname: string
@param hname: name of the hypervisor
@type hvparams: dict of strings
@param hvparams: hypervisor parameters for the given hypervisor
@type get_hv_fn: function
@param get_hv_fn: function that returns a hypervisor for the given hypervisor
name; optional parameter to increase testability
@rtype: list
@return: a list of all running instances on the current node
- instance1.example.com
- instance2.example.com
"""
try:
return get_hv_fn(hname).ListInstances(hvparams=hvparams)
except errors.HypervisorError, err:
_Fail("Error enumerating instances (hypervisor %s): %s",
hname, err, exc=True)
def GetInstanceList(hypervisor_list, all_hvparams=None,
get_hv_fn=hypervisor.GetHypervisor):
"""Provides a list of instances.
@type hypervisor_list: list
@param hypervisor_list: the list of hypervisors to query information
@type all_hvparams: dict of dict of strings
@param all_hvparams: a dictionary mapping hypervisor types to respective
cluster-wide hypervisor parameters
@type get_hv_fn: function
@param get_hv_fn: function that returns a hypervisor for the given hypervisor
name; optional parameter to increase testability
@rtype: list
@return: a list of all running instances on the current node
- instance1.example.com
- instance2.example.com
"""
results = []
for hname in hypervisor_list:
hvparams = all_hvparams[hname]
results.extend(GetInstanceListForHypervisor(hname, hvparams=hvparams,
get_hv_fn=get_hv_fn))
return results
def GetInstanceInfo(instance, hname, hvparams=None):
"""Gives back the information about an instance as a dictionary.
@type instance: string
@param instance: the instance name
@type hname: string
@param hname: the hypervisor type of the instance
@type hvparams: dict of strings
@param hvparams: the instance's hvparams
@rtype: dict
@return: dictionary with the following keys:
- memory: memory size of instance (int)
- state: state of instance (HvInstanceState)
- time: cpu time of instance (float)
- vcpus: the number of vcpus (int)
"""
output = {}
iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance,
hvparams=hvparams)
if iinfo is not None:
output["memory"] = iinfo[2]
output["vcpus"] = iinfo[3]
output["state"] = iinfo[4]
output["time"] = iinfo[5]
return output
def GetInstanceMigratable(instance):
"""Computes whether an instance can be migrated.
@type instance: L{objects.Instance}
@param instance: object representing the instance to be checked.
@rtype: tuple
@return: tuple of (result, description) where:
- result: whether the instance can be migrated or not
- description: a description of the issue, if relevant
"""
hyper = hypervisor.GetHypervisor(instance.hypervisor)
iname = instance.name
if iname not in hyper.ListInstances(hvparams=instance.hvparams):
_Fail("Instance %s is not running", iname)
for idx in range(len(instance.disks_info)):
link_name = _GetBlockDevSymlinkPath(iname, idx)
if not os.path.islink(link_name):
logging.warning("Instance %s is missing symlink %s for disk %d",
iname, link_name, idx)
def GetAllInstancesInfo(hypervisor_list, all_hvparams):
"""Gather data about all instances.
This is the equivalent of L{GetInstanceInfo}, except that it
computes data for all instances at once, thus being faster if one
needs data about more than one instance.
@type hypervisor_list: list
@param hypervisor_list: list of hypervisors to query for instance data
@type all_hvparams: dict of dict of strings
@param all_hvparams: mapping of hypervisor names to hvparams
@rtype: dict
@return: dictionary of instance: data, with data having the following keys:
- memory: memory size of instance (int)
- state: xen state of instance (string)
- time: cpu time of instance (float)
- vcpus: the number of vcpus
"""
output = {}
for hname in hypervisor_list:
hvparams = all_hvparams[hname]
iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo(hvparams)
if iinfo:
for name, _, memory, vcpus, state, times in iinfo:
value = {
"memory": memory,
"vcpus": vcpus,
"state": state,
"time": times,
}
if name in output:
# we only check static parameters, like memory and vcpus,
# and not state and time which can change between the
# invocations of the different hypervisors
for key in "memory", "vcpus":
if value[key] != output[name][key]:
_Fail("Instance %s is running twice"
" with different parameters", name)
output[name] = value
return output
def GetInstanceConsoleInfo(instance_param_dict,
get_hv_fn=hypervisor.GetHypervisor):
"""Gather data about the console access of a set of instances of this node.
This function assumes that the caller already knows which instances are on
this node, by calling a function such as L{GetAllInstancesInfo} or
L{GetInstanceList}.
For every instance, a large amount of configuration data needs to be
provided to the hypervisor interface in order to receive the console
information. Whether this could or should be cut down can be discussed.
The information is provided in a dictionary indexed by instance name,
allowing any number of instance queries to be done.
@type instance_param_dict: dict of string to tuple of dictionaries, where the
dictionaries represent: L{objects.Instance}, L{objects.Node},
L{objects.NodeGroup}, HvParams, BeParams
@param instance_param_dict: mapping of instance name to parameters necessary
for console information retrieval
@rtype: dict
@return: dictionary of instance: data, with data having the following keys:
- instance: instance name
- kind: console kind
- message: used with kind == CONS_MESSAGE, indicates console to be
unavailable, supplies error message
- host: host to connect to
- port: port to use
- user: user for login
- command: the command, broken into parts as an array
- display: unknown, potentially unused?
"""
output = {}
for inst_name in instance_param_dict:
instance = instance_param_dict[inst_name]["instance"]
pnode = instance_param_dict[inst_name]["node"]
group = instance_param_dict[inst_name]["group"]
hvparams = instance_param_dict[inst_name]["hvParams"]
beparams = instance_param_dict[inst_name]["beParams"]
instance = objects.Instance.FromDict(instance)
pnode = objects.Node.FromDict(pnode)
group = objects.NodeGroup.FromDict(group)
h = get_hv_fn(instance.hypervisor)
output[inst_name] = h.GetInstanceConsole(instance, pnode, group,
hvparams, beparams).ToDict()
return output
def _InstanceLogName(kind, os_name, instance, component):
"""Compute the OS log filename for a given instance and operation.
The instance name and os name are passed in as strings since not all
operations have these as part of an instance object.
@type kind: string
@param kind: the operation type (e.g. add, import, etc.)
@type os_name: string
@param os_name: the os name
@type instance: string
@param instance: the name of the instance being imported/added/etc.
@type component: string or None
@param component: the name of the component of the instance being
transferred
"""
# TODO: Use tempfile.mkstemp to create unique filename
if component:
assert "/" not in component
c_msg = "-%s" % component
else:
c_msg = ""
base = ("%s-%s-%s%s-%s.log" %
(kind, os_name, instance, c_msg, utils.TimestampForFilename()))
return utils.PathJoin(pathutils.LOG_OS_DIR, base)
def InstanceOsAdd(instance, reinstall, debug):
"""Add an OS to an instance.
@type instance: L{objects.Instance}
@param instance: Instance whose OS is to be installed
@type reinstall: boolean
@param reinstall: whether this is an instance reinstall
@type debug: integer
@param debug: debug level, passed to the OS scripts
@rtype: None
"""
inst_os = OSFromDisk(instance.os)
create_env = OSEnvironment(instance, inst_os, debug)
if reinstall:
create_env["INSTANCE_REINSTALL"] = "1"
logfile = _InstanceLogName("add", instance.os, instance.name, None)
result = utils.RunCmd([inst_os.create_script], env=create_env,
cwd=inst_os.path, output=logfile, reset_env=True)
if result.failed:
logging.error("os create command '%s' returned error: %s, logfile: %s,"
" output: %s", result.cmd, result.fail_reason, logfile,
result.output)
lines = [utils.SafeEncode(val)
for val in utils.TailFile(logfile, lines=20)]
_Fail("OS create script failed (%s), last lines in the"
" log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
def RunRenameInstance(instance, old_name, debug):
"""Run the OS rename script for an instance.
@type instance: L{objects.Instance}
@param instance: Instance whose OS is to be installed
@type old_name: string
@param old_name: previous instance name
@type debug: integer
@param debug: debug level, passed to the OS scripts
@rtype: boolean
@return: the success of the operation
"""
inst_os = OSFromDisk(instance.os)
rename_env = OSEnvironment(instance, inst_os, debug)
rename_env["OLD_INSTANCE_NAME"] = old_name
logfile = _InstanceLogName("rename", instance.os,
"%s-%s" % (old_name, instance.name), None)
result = utils.RunCmd([inst_os.rename_script], env=rename_env,
cwd=inst_os.path, output=logfile, reset_env=True)
if result.failed:
logging.error("os create command '%s' returned error: %s output: %s",
result.cmd, result.fail_reason, result.output)
lines = [utils.SafeEncode(val)
for val in utils.TailFile(logfile, lines=20)]
_Fail("OS rename script failed (%s), last lines in the"
" log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
def _GetBlockDevSymlinkPath(instance_name, idx, _dir=None):
"""Returns symlink path for block device.
"""
if _dir is None:
_dir = pathutils.DISK_LINKS_DIR
return utils.PathJoin(_dir,
("%s%s%s" %
(instance_name, constants.DISK_SEPARATOR, idx)))
def _SymlinkBlockDev(instance_name, device_path, idx):
"""Set up symlinks to a instance's block device.
This is an auxiliary function run when an instance is start (on the primary
node) or when an instance is migrated (on the target node).
@param instance_name: the name of the target instance
@param device_path: path of the physical block device, on the node
@param idx: the disk index
@return: absolute path to the disk's symlink
"""
# In case we have only a userspace access URI, device_path is None
if not device_path:
return None
link_name = _GetBlockDevSymlinkPath(instance_name, idx)
try:
os.symlink(device_path, link_name)
except OSError, err:
if err.errno == errno.EEXIST:
if (not os.path.islink(link_name) or
os.readlink(link_name) != device_path):
os.remove(link_name)
os.symlink(device_path, link_name)
else:
raise
return link_name
def _RemoveBlockDevLinks(instance_name, disks):
"""Remove the block device symlinks belonging to the given instance.
"""
for idx, _ in enumerate(disks):
link_name = _GetBlockDevSymlinkPath(instance_name, idx)
if os.path.islink(link_name):
try:
os.remove(link_name)
except OSError:
logging.exception("Can't remove symlink '%s'", link_name)
def _CalculateDeviceURI(instance, disk, device):
"""Get the URI for the device.
@type instance: L{objects.Instance}
@param instance: the instance which disk belongs to
@type disk: L{objects.Disk}
@param disk: the target disk object
@type device: L{bdev.BlockDev}
@param device: the corresponding BlockDevice
@rtype: string
@return: the device uri if any else None
"""
access_mode = disk.params.get(constants.LDP_ACCESS,
constants.DISK_KERNELSPACE)
if access_mode == constants.DISK_USERSPACE:
# This can raise errors.BlockDeviceError
return device.GetUserspaceAccessUri(instance.hypervisor)
else:
return None
def _GatherAndLinkBlockDevs(instance):
"""Set up an instance's block device(s).
This is run on the primary node at instance startup. The block
devices must be already assembled.
@type instance: L{objects.Instance}
@param instance: the instance whose disks we should assemble
@rtype: list
@return: list of (disk_object, link_name, drive_uri)
"""
block_devices = []
for idx, disk in enumerate(instance.disks_info):
device = _RecursiveFindBD(disk)
if device is None:
raise errors.BlockDeviceError("Block device '%s' is not set up." %
str(disk))
device.Open()
try:
link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx)
except OSError, e:
raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
e.strerror)
uri = _CalculateDeviceURI(instance, disk, device)
block_devices.append((disk, link_name, uri))
return block_devices
def _IsInstanceUserDown(instance_info):
return instance_info and \
"state" in instance_info and \
hv_base.HvInstanceState.IsShutdown(instance_info["state"])
def _GetInstanceInfo(instance):
"""Helper function L{GetInstanceInfo}"""
return GetInstanceInfo(instance.name, instance.hypervisor,
hvparams=instance.hvparams)
def StartInstance(instance, startup_paused, reason, store_reason=True):
"""Start an instance.
@type instance: L{objects.Instance}
@param instance: the instance object
@type startup_paused: bool
@param instance: pause instance at startup?
@type reason: list of reasons
@param reason: the reason trail for this startup
@type store_reason: boolean
@param store_reason: whether to store the shutdown reason trail on file
@rtype: None
"""
instance_info = _GetInstanceInfo(instance)
if instance_info and not _IsInstanceUserDown(instance_info):
logging.info("Instance '%s' already running, not starting", instance.name)
return
try:
block_devices = _GatherAndLinkBlockDevs(instance)
hyper = hypervisor.GetHypervisor(instance.hypervisor)
hyper.StartInstance(instance, block_devices, startup_paused)
if store_reason:
_StoreInstReasonTrail(instance.name, reason)
except errors.BlockDeviceError, err:
_Fail("Block device error: %s", err, exc=True)
except errors.HypervisorError, err:
_RemoveBlockDevLinks(instance.name, instance.disks_info)
_Fail("Hypervisor error: %s", err, exc=True)
def InstanceShutdown(instance, timeout, reason, store_reason=True):
"""Shut an instance down.
@note: this functions uses polling with a hardcoded timeout.
@type instance: L{objects.Instance}
@param instance: the instance object
@type timeout: integer
@param timeout: maximum timeout for soft shutdown
@type reason: list of reasons
@param reason: the reason trail for this shutdown
@type store_reason: boolean
@param store_reason: whether to store the shutdown reason trail on file
@rtype: None
"""
hyper = hypervisor.GetHypervisor(instance.hypervisor)
if not _GetInstanceInfo(instance):
logging.info("Instance '%s' not running, doing nothing", instance.name)
return
class _TryShutdown(object):
def __init__(self):
self.tried_once = False
def __call__(self):
if not _GetInstanceInfo(instance):
return
try:
hyper.StopInstance(instance, retry=self.tried_once, timeout=timeout)
if store_reason:
_StoreInstReasonTrail(instance.name, reason)
except errors.HypervisorError, err:
# if the instance is no longer existing, consider this a
# success and go to cleanup
if not _GetInstanceInfo(instance):
return
_Fail("Failed to stop instance '%s': %s", instance.name, err)
self.tried_once = True
raise utils.RetryAgain()
try:
utils.Retry(_TryShutdown(), 5, timeout)
except utils.RetryTimeout:
# the shutdown did not succeed
logging.error("Shutdown of '%s' unsuccessful, forcing", instance.name)
try:
hyper.StopInstance(instance, force=True)
except errors.HypervisorError, err:
# only raise an error if the instance still exists, otherwise
# the error could simply be "instance ... unknown"!
if _GetInstanceInfo(instance):
_Fail("Failed to force stop instance '%s': %s", instance.name, err)
time.sleep(1)
if _GetInstanceInfo(instance):
_Fail("Could not shutdown instance '%s' even by destroy", instance.name)
try:
hyper.CleanupInstance(instance.name)
except errors.HypervisorError, err:
logging.warning("Failed to execute post-shutdown cleanup step: %s", err)
_RemoveBlockDevLinks(instance.name, instance.disks_info)
def InstanceReboot(instance, reboot_type, shutdown_timeout, reason):
"""Reboot an instance.
@type instance: L{objects.Instance}
@param instance: the instance object to reboot
@type reboot_type: str
@param reboot_type: the type of reboot, one the following
constants:
- L{constants.INSTANCE_REBOOT_SOFT}: only reboot the
instance OS, do not recreate the VM
- L{constants.INSTANCE_REBOOT_HARD}: tear down and
restart the VM (at the hypervisor level)
- the other reboot type (L{constants.INSTANCE_REBOOT_FULL}) is
not accepted here, since that mode is handled differently, in
cmdlib, and translates into full stop and start of the
instance (instead of a call_instance_reboot RPC)
@type shutdown_timeout: integer
@param shutdown_timeout: maximum timeout for soft shutdown
@type reason: list of reasons
@param reason: the reason trail for this reboot
@rtype: None
"""
# TODO: this is inconsistent with 'StartInstance' and 'InstanceShutdown'
# because those functions simply 'return' on error whereas this one
# raises an exception with '_Fail'
if not _GetInstanceInfo(instance):
_Fail("Cannot reboot instance '%s' that is not running", instance.name)
hyper = hypervisor.GetHypervisor(instance.hypervisor)
if reboot_type == constants.INSTANCE_REBOOT_SOFT:
try:
hyper.RebootInstance(instance)
except errors.HypervisorError, err:
_Fail("Failed to soft reboot instance '%s': %s", instance.name, err)
elif reboot_type == constants.INSTANCE_REBOOT_HARD:
try:
InstanceShutdown(instance, shutdown_timeout, reason, store_reason=False)
result = StartInstance(instance, False, reason, store_reason=False)
_StoreInstReasonTrail(instance.name, reason)
return result
except errors.HypervisorError, err:
_Fail("Failed to hard reboot instance '%s': %s", instance.name, err)
else:
_Fail("Invalid reboot_type received: '%s'", reboot_type)
def InstanceBalloonMemory(instance, memory):
"""Resize an instance's memory.
@type instance: L{objects.Instance}
@param instance: the instance object
@type memory: int
@param memory: new memory amount in MB
@rtype: None
"""
hyper = hypervisor.GetHypervisor(instance.hypervisor)
running = hyper.ListInstances(hvparams=instance.hvparams)
if instance.name not in running:
logging.info("Instance %s is not running, cannot balloon", instance.name)
return
try:
hyper.BalloonInstanceMemory(instance, memory)
except errors.HypervisorError, err:
_Fail("Failed to balloon instance memory: %s", err, exc=True)
def MigrationInfo(instance):
"""Gather information about an instance to be migrated.
@type instance: L{objects.Instance}
@param instance: the instance definition
"""
hyper = hypervisor.GetHypervisor(instance.hypervisor)
try:
info = hyper.MigrationInfo(instance)
except errors.HypervisorError, err:
_Fail("Failed to fetch migration information: %s", err, exc=True)
return info
def AcceptInstance(instance, info, target):
"""Prepare the node to accept an instance.
@type instance: L{objects.Instance}
@param instance: the instance definition
@type info: string/data (opaque)
@param info: migration information, from the source node
@type target: string
@param target: target host (usually ip), on this node
"""
hyper = hypervisor.GetHypervisor(instance.hypervisor)
try:
hyper.AcceptInstance(instance, info, target)
except errors.HypervisorError, err:
_Fail("Failed to accept instance: %s", err, exc=True)
def FinalizeMigrationDst(instance, info, success):
"""Finalize any preparation to accept an instance.
@type instance: L{objects.Instance}
@param instance: the instance definition
@type info: string/data (opaque)
@param info: migration information, from the source node
@type success: boolean
@param success: whether the migration was a success or a failure
"""
hyper = hypervisor.GetHypervisor(instance.hypervisor)
try:
hyper.FinalizeMigrationDst(instance, info, success)
except errors.HypervisorError, err:
_Fail("Failed to finalize migration on the target node: %s", err, exc=True)
def MigrateInstance(cluster_name, instance, target, live):
"""Migrates an instance to another node.
@type cluster_name: string
@param cluster_name: name of the cluster
@type instance: L{objects.Instance}
@param instance: the instance definition
@type target: string
@param target: the target node name
@type live: boolean
@param live: whether the migration should be done live or not (the
interpretation of this parameter is left to the hypervisor)
@raise RPCFail: if migration fails for some reason
"""
hyper = hypervisor.GetHypervisor(instance.hypervisor)
try:
hyper.MigrateInstance(cluster_name, instance, target, live)
except errors.HypervisorError, err:
_Fail("Failed to migrate instance: %s", err, exc=True)
def FinalizeMigrationSource(instance, success, live):
"""Finalize the instance migration on the source node.
@type instance: L{objects.Instance}
@param instance: the instance definition of the migrated instance
@type success: bool
@param success: whether the migration succeeded or not
@type live: bool
@param live: whether the user requested a live migration or not
@raise RPCFail: If the execution fails for some reason
"""
hyper = hypervisor.GetHypervisor(instance.hypervisor)
try:
hyper.FinalizeMigrationSource(instance, success, live)
except Exception, err: # pylint: disable=W0703
_Fail("Failed to finalize the migration on the source node: %s", err,
exc=True)
def GetMigrationStatus(instance):
"""Get the migration status
@type instance: L{objects.Instance}
@param instance: the instance that is being migrated
@rtype: L{objects.MigrationStatus}
@return: the status of the current migration (one of
L{constants.HV_MIGRATION_VALID_STATUSES}), plus any additional
progress info that can be retrieved from the hypervisor
@raise RPCFail: If the migration status cannot be retrieved
"""
hyper = hypervisor.GetHypervisor(instance.hypervisor)
try:
return hyper.GetMigrationStatus(instance)
except Exception, err: # pylint: disable=W0703
_Fail("Failed to get migration status: %s", err, exc=True)
def HotplugDevice(instance, action, dev_type, device, extra, seq):
"""Hotplug a device
Hotplug is currently supported only for KVM Hypervisor.
@type instance: L{objects.Instance}
@param instance: the instance to which we hotplug a device
@type action: string
@param action: the hotplug action to perform
@type dev_type: string
@param dev_type: the device type to hotplug
@type device: either L{objects.NIC} or L{objects.Disk}
@param device: the device object to hotplug
@type extra: tuple
@param extra: extra info used for disk hotplug (disk link, drive uri)
@type seq: int
@param seq: the index of the device from master perspective
@raise RPCFail: in case instance does not have KVM hypervisor
"""
hyper = hypervisor.GetHypervisor(instance.hypervisor)
try:
hyper.VerifyHotplugSupport(instance, action, dev_type)
except errors.HotplugError, err:
_Fail("Hotplug is not supported: %s", err)
if action == constants.HOTPLUG_ACTION_ADD:
fn = hyper.HotAddDevice
elif action == constants.HOTPLUG_ACTION_REMOVE:
fn = hyper.HotDelDevice
elif action == constants.HOTPLUG_ACTION_MODIFY:
fn = hyper.HotModDevice
else:
assert action in constants.HOTPLUG_ALL_ACTIONS
return fn(instance, dev_type, device, extra, seq)
def HotplugSupported(instance):
"""Checks if hotplug is generally supported.
"""
hyper = hypervisor.GetHypervisor(instance.hypervisor)
try:
hyper.HotplugSupported(instance)
except errors.HotplugError, err:
_Fail("Hotplug is not supported: %s", err)
def ModifyInstanceMetadata(metadata):
"""Sends instance data to the metadata daemon.
Uses the Luxi transport layer to communicate with the metadata
daemon configuration server. It starts the metadata daemon if it is
not running.
The daemon must be enabled during at configuration time.
@type metadata: dict
@param metadata: instance metadata obtained by calling
L{objects.Instance.ToDict} on an instance object
"""
if not constants.ENABLE_METAD:
raise errors.ProgrammerError("The metadata deamon is disabled, yet"
" ModifyInstanceMetadata has been called")
if not utils.IsDaemonAlive(constants.METAD):
result = utils.RunCmd([pathutils.DAEMON_UTIL, "start", constants.METAD])
if result.failed:
raise errors.HypervisorError("Failed to start metadata daemon")
with contextlib.closing(metad.Client()) as client:
client.UpdateConfig(metadata)
def BlockdevCreate(disk, size, owner, on_primary, info, excl_stor):
"""Creates a block device for an instance.
@type disk: L{objects.Disk}
@param disk: the object describing the disk we should create
@type size: int
@param size: the size of the physical underlying device, in MiB
@type owner: str
@param owner: the name of the instance for which disk is created,
used for device cache data
@type on_primary: boolean
@param on_primary: indicates if it is the primary node or not
@type info: string
@param info: string that will be sent to the physical device
creation, used for example to set (LVM) tags on LVs
@type excl_stor: boolean
@param excl_stor: Whether exclusive_storage is active
@return: the new unique_id of the device (this can sometime be
computed only after creation), or None. On secondary nodes,
it's not required to return anything.
"""
# TODO: remove the obsolete "size" argument
# pylint: disable=W0613
clist = []
if disk.children:
for child in disk.children:
try:
crdev = _RecursiveAssembleBD(child, owner, on_primary)
except errors.BlockDeviceError, err:
_Fail("Can't assemble device %s: %s", child, err)
if on_primary or disk.AssembleOnSecondary():
# we need the children open in case the device itself has to
# be assembled
try:
# pylint: disable=E1103
crdev.Open()
except errors.BlockDeviceError, err:
_Fail("Can't make child '%s' read-write: %s", child, err)
clist.append(crdev)
try:
device = bdev.Create(disk, clist, excl_stor)
except errors.BlockDeviceError, err:
_Fail("Can't create block device: %s", err)
if on_primary or disk.AssembleOnSecondary():
try:
device.Assemble()
except errors.BlockDeviceError, err:
_Fail("Can't assemble device after creation, unusual event: %s", err)
if on_primary or disk.OpenOnSecondary():
try:
device.Open(force=True)
except errors.BlockDeviceError, err:
_Fail("Can't make device r/w after creation, unusual event: %s", err)
DevCacheManager.UpdateCache(device.dev_path, owner,
on_primary, disk.iv_name)
device.SetInfo(info)
return device.unique_id
def _DumpDevice(source_path, target_path, offset, size, truncate):
"""This function images/wipes the device using a local file.
@type source_path: string
@param source_path: path of the image or data source (e.g., "/dev/zero")
@type target_path: string
@param target_path: path of the device to image/wipe
@type offset: int
@param offset: offset in MiB in the output file
@type size: int
@param size: maximum size in MiB to write (data source might be smaller)
@type truncate: bool
@param truncate: whether the file should be truncated
@return: None
@raise RPCFail: in case of failure
"""
# Internal sizes are always in Mebibytes; if the following "dd" command
# should use a different block size the offset and size given to this
# function must be adjusted accordingly before being passed to "dd".
block_size = constants.DD_BLOCK_SIZE
cmd = [constants.DD_CMD, "if=%s" % source_path, "seek=%d" % offset,
"bs=%s" % block_size, "oflag=direct", "of=%s" % target_path,
"count=%d" % size]
if not truncate:
cmd.append("conv=notrunc")
result = utils.RunCmd(cmd)
if result.failed:
_Fail("Dump command '%s' exited with error: %s; output: %s", result.cmd,
result.fail_reason, result.output)
def _DownloadAndDumpDevice(source_url, target_path, size):
"""This function images a device using a downloaded image file.
@type source_url: string
@param source_url: URL of image to dump to disk
@type target_path: string
@param target_path: path of the device to image
@type size: int
@param size: maximum size in MiB to write (data source might be smaller)
@rtype: NoneType
@return: None
@raise RPCFail: in case of download or write failures
"""
class DDParams(object):
def __init__(self, current_size, total_size):
self.current_size = current_size
self.total_size = total_size
self.image_size_error = False
def dd_write(ddparams, out):
if ddparams.current_size < ddparams.total_size:
ddparams.current_size += len(out)
target_file.write(out)
else:
ddparams.image_size_error = True
return -1
target_file = open(target_path, "r+")
ddparams = DDParams(0, 1024 * 1024 * size)
curl = pycurl.Curl()
curl.setopt(pycurl.VERBOSE, True)
curl.setopt(pycurl.NOSIGNAL, True)
curl.setopt(pycurl.USERAGENT, http.HTTP_GANETI_VERSION)
curl.setopt(pycurl.URL, source_url)
curl.setopt(pycurl.WRITEFUNCTION, lambda out: dd_write(ddparams, out))
try:
curl.perform()
except pycurl.error:
if ddparams.image_size_error:
_Fail("Disk image larger than the disk")
else:
raise
target_file.close()
def BlockdevConvert(src_disk, target_disk):
"""Copies data from source block device to target.
This function gets the export and import commands from the source and
target devices respectively, and then concatenates them to a single
command using a pipe ("|"). Finally, executes the unified command that
will transfer the data between the devices during the disk template
conversion operation.
@type src_disk: L{objects.Disk}
@param src_disk: the disk object we want to copy from
@type target_disk: L{objects.Disk}
@param target_disk: the disk object we want to copy to
@rtype: NoneType
@return: None
@raise RPCFail: in case of failure
"""
src_dev = _RecursiveFindBD(src_disk)
if src_dev is None:
_Fail("Cannot copy from device '%s': device not found", src_disk.uuid)
dest_dev = _RecursiveFindBD(target_disk)
if dest_dev is None:
_Fail("Cannot copy to device '%s': device not found", target_disk.uuid)
src_cmd = src_dev.Export()
dest_cmd = dest_dev.Import()
command = "%s | %s" % (utils.ShellQuoteArgs(src_cmd),
utils.ShellQuoteArgs(dest_cmd))
result = utils.RunCmd(command)
if result.failed:
_Fail("Disk conversion command '%s' exited with error: %s; output: %s",
result.cmd, result.fail_reason, result.output)
def BlockdevWipe(disk, offset, size):
"""Wipes a block device.
@type disk: L{objects.Disk}
@param disk: the disk object we want to wipe
@type offset: int
@param offset: The offset in MiB in the file
@type size: int
@param size: The size in MiB to write
"""
try:
rdev = _RecursiveFindBD(disk)
except errors.BlockDeviceError:
rdev = None
if not rdev:
_Fail("Cannot wipe device %s: device not found", disk.iv_name)
if offset < 0:
_Fail("Negative offset")
if size < 0:
_Fail("Negative size")
if offset > rdev.size:
_Fail("Wipe offset is bigger than device size")
if (offset + size) > rdev.size:
_Fail("Wipe offset and size are bigger than device size")
_DumpDevice("/dev/zero", rdev.dev_path, offset, size, True)
def BlockdevImage(disk, image, size):
"""Images a block device either by dumping a local file or
downloading a URL.
@type disk: L{objects.Disk}
@param disk: the disk object we want to image
@type image: string
@param image: file path to the disk image be dumped
@type size: int
@param size: The size in MiB to write
@rtype: NoneType
@return: None
@raise RPCFail: in case of failure
"""
if not (utils.IsUrl(image) or os.path.exists(image)):
_Fail("Image '%s' not found", image)
try:
rdev = _RecursiveFindBD(disk)
except errors.BlockDeviceError:
rdev = None
if not rdev:
_Fail("Cannot image device %s: device not found", disk.iv_name)
if size < 0:
_Fail("Negative size")
if size > rdev.size:
_Fail("Image size is bigger than device size")
if utils.IsUrl(image):
_DownloadAndDumpDevice(image, rdev.dev_path, size)
else:
_DumpDevice(image, rdev.dev_path, 0, size, False)
def BlockdevPauseResumeSync(disks, pause):
"""Pause or resume the sync of the block device.
@type disks: list of L{objects.Disk}
@param disks: the disks object we want to pause/resume
@type pause: bool
@param pause: Wheater to pause or resume
"""
success = []
for disk in disks:
try:
rdev = _RecursiveFindBD(disk)
except errors.BlockDeviceError:
rdev = None
if not rdev:
success.append((False, ("Cannot change sync for device %s:"
" device not found" % disk.iv_name)))
continue
result = rdev.PauseResumeSync(pause)
if result:
success.append((result, None))
else:
if pause:
msg = "Pause"
else:
msg = "Resume"
success.append((result, "%s for device %s failed" % (msg, disk.iv_name)))
return success
def BlockdevRemove(disk):
"""Remove a block device.
@note: This is intended to be called recursively.
@type disk: L{objects.Disk}
@param disk: the disk object we should remove
@rtype: boolean
@return: the success of the operation
"""
msgs = []
try:
rdev = _RecursiveFindBD(disk)
except errors.BlockDeviceError, err:
# probably can't attach
logging.info("Can't attach to device %s in remove", disk)
rdev = None
if rdev is not None:
r_path = rdev.dev_path
def _TryRemove():
try:
rdev.Remove()
return []
except errors.BlockDeviceError, err:
return [str(err)]
msgs.extend(utils.SimpleRetry([], _TryRemove,
constants.DISK_REMOVE_RETRY_INTERVAL,
constants.DISK_REMOVE_RETRY_TIMEOUT))
if not msgs:
DevCacheManager.RemoveCache(r_path)
if disk.children:
for child in disk.children:
try:
BlockdevRemove(child)
except RPCFail, err:
msgs.append(str(err))
if msgs:
_Fail("; ".join(msgs))
def _RecursiveAssembleBD(disk, owner, as_primary):
"""Activate a block device for an instance.
This is run on the primary and secondary nodes for an instance.
@note: this function is called recursively.
@type disk: L{objects.Disk}
@param disk: the disk we try to assemble
@type owner: str
@param owner: the name of the instance which owns the disk
@type as_primary: boolean
@param as_primary: if we should make the block device
read/write
@return: the assembled device or None (in case no device
was assembled)
@raise errors.BlockDeviceError: in case there is an error
during the activation of the children or the device
itself
"""
children = []
if disk.children:
mcn = disk.ChildrenNeeded()
if mcn == -1:
mcn = 0 # max number of Nones allowed
else:
mcn = len(disk.children) - mcn # max number of Nones
for chld_disk in disk.children:
try:
cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary)
except errors.BlockDeviceError, err:
if children.count(None) >= mcn:
raise
cdev = None
logging.error("Error in child activation (but continuing): %s",
str(err))
children.append(cdev)
if as_primary or disk.AssembleOnSecondary():
r_dev = bdev.Assemble(disk, children)
result = r_dev
if as_primary or disk.OpenOnSecondary():
r_dev.Open()
DevCacheManager.UpdateCache(r_dev.dev_path, owner,
as_primary, disk.iv_name)
else:
result = True
return result
def BlockdevAssemble(disk, instance, as_primary, idx):
"""Activate a block device for an instance.
This is a wrapper over _RecursiveAssembleBD.
@rtype: str or boolean
@return: a tuple with the C{/dev/...} path and the created symlink
for primary nodes, and (C{True}, C{True}) for secondary nodes
"""
try:
result = _RecursiveAssembleBD(disk, instance.name, as_primary)
if isinstance(result, BlockDev):
# pylint: disable=E1103
dev_path = result.dev_path
link_name = None
uri = None
if as_primary:
link_name = _SymlinkBlockDev(instance.name, dev_path, idx)
uri = _CalculateDeviceURI(instance, disk, result)
elif result:
return result, result
else:
_Fail("Unexpected result from _RecursiveAssembleBD")
except errors.BlockDeviceError, err:
_Fail("Error while assembling disk: %s", err, exc=True)
except OSError, err:
_Fail("Error while symlinking disk: %s", err, exc=True)
return dev_path, link_name, uri
def BlockdevShutdown(disk):
"""Shut down a block device.
First, if the device is assembled (Attach() is successful), then
the device is shutdown. Then the children of the device are
shutdown.
This function is called recursively. Note that we don't cache the
children or such, as oppossed to assemble, shutdown of different
devices doesn't require that the upper device was active.
@type disk: L{objects.Disk}
@param disk: the description of the disk we should
shutdown
@rtype: None
"""
msgs = []
r_dev = _RecursiveFindBD(disk)
if r_dev is not None:
r_path = r_dev.dev_path
try:
r_dev.Shutdown()
DevCacheManager.RemoveCache(r_path)
except errors.BlockDeviceError, err:
msgs.append(str(err))
if disk.children:
for child in disk.children:
try:
BlockdevShutdown(child)
except RPCFail, err:
msgs.append(str(err))
if msgs:
_Fail("; ".join(msgs))
def BlockdevAddchildren(parent_cdev, new_cdevs):
"""Extend a mirrored block device.
@type parent_cdev: L{objects.Disk}
@param parent_cdev: the disk to which we should add children
@type new_cdevs: list of L{objects.Disk}
@param new_cdevs: the list of children which we should add
@rtype: None
"""
parent_bdev = _RecursiveFindBD(parent_cdev)
if parent_bdev is None:
_Fail("Can't find parent device '%s' in add children", parent_cdev)
new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs]
if new_bdevs.count(None) > 0:
_Fail("Can't find new device(s) to add: %s:%s", new_bdevs, new_cdevs)
parent_bdev.AddChildren(new_bdevs)
def BlockdevRemovechildren(parent_cdev, new_cdevs):
"""Shrink a mirrored block device.
@type parent_cdev: L{objects.Disk}
@param parent_cdev: the disk from which we should remove children
@type new_cdevs: list of L{objects.Disk}
@param new_cdevs: the list of children which we should remove
@rtype: None
"""
parent_bdev = _RecursiveFindBD(parent_cdev)
if parent_bdev is None:
_Fail("Can't find parent device '%s' in remove children", parent_cdev)
devs = []
for disk in new_cdevs:
rpath = disk.StaticDevPath()
if rpath is None:
bd = _RecursiveFindBD(disk)
if bd is None:
_Fail("Can't find device %s while removing children", disk)
else:
devs.append(bd.dev_path)
else:
if not utils.IsNormAbsPath(rpath):
_Fail("Strange path returned from StaticDevPath: '%s'", rpath)
devs.append(rpath)
parent_bdev.RemoveChildren(devs)
def BlockdevGetmirrorstatus(disks):
"""Get the mirroring status of a list of devices.
@type disks: list of L{objects.Disk}
@param disks: the list of disks which we should query
@rtype: disk
@return: List of L{objects.BlockDevStatus}, one for each disk
@raise errors.BlockDeviceError: if any of the disks cannot be
found
"""
stats = []
for dsk in disks:
rbd = _RecursiveFindBD(dsk)
if rbd is None:
_Fail("Can't find device %s", dsk)
stats.append(rbd.CombinedSyncStatus())
return stats
def BlockdevGetmirrorstatusMulti(disks):
"""Get the mirroring status of a list of devices.
@type disks: list of L{objects.Disk}
@param disks: the list of disks which we should query
@rtype: disk
@return: List of tuples, (bool, status), one for each disk; bool denotes
success/failure, status is L{objects.BlockDevStatus} on success, string
otherwise
"""
result = []
for disk in disks:
try:
rbd = _RecursiveFindBD(disk)
if rbd is None:
result.append((False, "Can't find device %s" % disk))
continue
status = rbd.CombinedSyncStatus()
except errors.BlockDeviceError, err:
logging.exception("Error while getting disk status")
result.append((False, str(err)))
else:
result.append((True, status))
assert len(disks) == len(result)
return result
def _RecursiveFindBD(disk):
"""Check if a device is activated.
If so, return information about the real device.
@type disk: L{objects.Disk}
@param disk: the disk object we need to find
@return: None if the device can't be found,
otherwise the device instance
"""
children = []
if disk.children:
for chdisk in disk.children:
children.append(_RecursiveFindBD(chdisk))
return bdev.FindDevice(disk, children)
def _OpenRealBD(disk):
"""Opens the underlying block device of a disk.
@type disk: L{objects.Disk}
@param disk: the disk object we want to open
"""
real_disk = _RecursiveFindBD(disk)
if real_disk is None:
_Fail("Block device '%s' is not set up", disk)
real_disk.Open()
return real_disk
def BlockdevFind(disk):
"""Check if a device is activated.
If it is, return information about the real device.
@type disk: L{objects.Disk}
@param disk: the disk to find
@rtype: None or objects.BlockDevStatus
@return: None if the disk cannot be found, otherwise a the current
information
"""
try:
rbd = _RecursiveFindBD(disk)
except errors.BlockDeviceError, err:
_Fail("Failed to find device: %s", err, exc=True)
if rbd is None:
return None
return rbd.GetSyncStatus()
def BlockdevGetdimensions(disks):
"""Computes the size of the given disks.
If a disk is not found, returns None instead.
@type disks: list of L{objects.Disk}
@param disks: the list of disk to compute the size for
@rtype: list
@return: list with elements None if the disk cannot be found,
otherwise the pair (size, spindles), where spindles is None if the
device doesn't support that
"""
result = []
for cf in disks:
try:
rbd = _RecursiveFindBD(cf)
except errors.BlockDeviceError:
result.append(None)
continue
if rbd is None:
result.append(None)
else:
result.append(rbd.GetActualDimensions())
return result
def UploadFile(file_name, data, mode, uid, gid, atime, mtime):
"""Write a file to the filesystem.
This allows the master to overwrite(!) a file. It will only perform
the operation if the file belongs to a list of configuration files.
@type file_name: str
@param file_name: the target file name
@type data: str
@param data: the new contents of the file
@type mode: int
@param mode: the mode to give the file (can be None)
@type uid: string
@param uid: the owner of the file
@type gid: string
@param gid: the group of the file
@type atime: float
@param atime: the atime to set on the file (can be None)
@type mtime: float
@param mtime: the mtime to set on the file (can be None)
@rtype: None
"""
file_name = vcluster.LocalizeVirtualPath(file_name)
if not os.path.isabs(file_name):
_Fail("Filename passed to UploadFile is not absolute: '%s'", file_name)
if file_name not in _ALLOWED_UPLOAD_FILES:
_Fail("Filename passed to UploadFile not in allowed upload targets: '%s'",
file_name)
raw_data = _Decompress(data)
if not (isinstance(uid, basestring) and isinstance(gid, basestring)):
_Fail("Invalid username/groupname type")
getents = runtime.GetEnts()
uid = getents.LookupUser(uid)
gid = getents.LookupGroup(gid)
utils.SafeWriteFile(file_name, None,
data=raw_data, mode=mode, uid=uid, gid=gid,
atime=atime, mtime=mtime)
def RunOob(oob_program, command, node, timeout):
"""Executes oob_program with given command on given node.
@param oob_program: The path to the executable oob_program
@param command: The command to invoke on oob_program
@param node: The node given as an argument to the program
@param timeout: Timeout after which we kill the oob program
@return: stdout
@raise RPCFail: If execution fails for some reason
"""
result = utils.RunCmd([oob_program, command, node], timeout=timeout)
if result.failed:
_Fail("'%s' failed with reason '%s'; output: %s", result.cmd,
result.fail_reason, result.output)
return result.stdout
def _OSOndiskAPIVersion(os_dir):
"""Compute and return the API version of a given OS.
This function will try to read the API version of the OS residing in
the 'os_dir' directory.
@type os_dir: str
@param os_dir: the directory in which we should look for the OS
@rtype: tuple
@return: tuple (status, data) with status denoting the validity and
data holding either the valid versions or an error message
"""
api_file = utils.PathJoin(os_dir, constants.OS_API_FILE)
try:
st = os.stat(api_file)
except EnvironmentError, err:
return False, ("Required file '%s' not found under path %s: %s" %
(constants.OS_API_FILE, os_dir, utils.ErrnoOrStr(err)))
if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
return False, ("File '%s' in %s is not a regular file" %
(constants.OS_API_FILE, os_dir))
try:
api_versions = utils.ReadFile(api_file).splitlines()
except EnvironmentError, err:
return False, ("Error while reading the API version file at %s: %s" %
(api_file, utils.ErrnoOrStr(err)))
try:
api_versions = [int(version.strip()) for version in api_versions]
except (TypeError, ValueError), err:
return False, ("API version(s) can't be converted to integer: %s" %
str(err))
return True, api_versions
def DiagnoseOS(top_dirs=None):
"""Compute the validity for all OSes.
@type top_dirs: list
@param top_dirs: the list of directories in which to
search (if not given defaults to
L{pathutils.OS_SEARCH_PATH})
@rtype: list of L{objects.OS}
@return: a list of tuples (name, path, status, diagnose, variants,
parameters, api_version) for all (potential) OSes under all
search paths, where:
- name is the (potential) OS name
- path is the full path to the OS
- status True/False is the validity of the OS
- diagnose is the error message for an invalid OS, otherwise empty
- variants is a list of supported OS variants, if any
- parameters is a list of (name, help) parameters, if any
- api_version is a list of support OS API versions
"""
if top_dirs is None:
top_dirs = pathutils.OS_SEARCH_PATH
result = []
for dir_name in top_dirs:
if os.path.isdir(dir_name):
try:
f_names = utils.ListVisibleFiles(dir_name)
except EnvironmentError, err:
logging.exception("Can't list the OS directory %s: %s", dir_name, err)
break
for name in f_names:
os_path = utils.PathJoin(dir_name, name)
status, os_inst = _TryOSFromDisk(name, base_dir=dir_name)
if status:
diagnose = ""
variants = os_inst.supported_variants
parameters = os_inst.supported_parameters
api_versions = os_inst.api_versions
trusted = False if os_inst.create_script_untrusted else True
else:
diagnose = os_inst
variants = parameters = api_versions = []
trusted = True
result.append((name, os_path, status, diagnose, variants,
parameters, api_versions, trusted))
return result
def _TryOSFromDisk(name, base_dir=None):
"""Create an OS instance from disk.
This function will return an OS instance if the given name is a
valid OS name.
@type base_dir: string
@keyword base_dir: Base directory containing OS installations.
Defaults to a search in all the OS_SEARCH_PATH dirs.
@rtype: tuple
@return: success and either the OS instance if we find a valid one,
or error message
"""
if base_dir is None:
os_dir = utils.FindFile(name, pathutils.OS_SEARCH_PATH, os.path.isdir)
else:
os_dir = utils.FindFile(name, [base_dir], os.path.isdir)
if os_dir is None:
return False, "Directory for OS %s not found in search path" % name
status, api_versions = _OSOndiskAPIVersion(os_dir)
if not status:
# push the error up
return status, api_versions
if not constants.OS_API_VERSIONS.intersection(api_versions):
return False, ("API version mismatch for path '%s': found %s, want %s." %
(os_dir, api_versions, constants.OS_API_VERSIONS))
# OS Files dictionary, we will populate it with the absolute path
# names; if the value is True, then it is a required file, otherwise
# an optional one
os_files = dict.fromkeys(constants.OS_SCRIPTS, True)
os_files[constants.OS_SCRIPT_CREATE] = False
os_files[constants.OS_SCRIPT_CREATE_UNTRUSTED] = False
if max(api_versions) >= constants.OS_API_V15:
os_files[constants.OS_VARIANTS_FILE] = False
if max(api_versions) >= constants.OS_API_V20:
os_files[constants.OS_PARAMETERS_FILE] = True
else:
del os_files[constants.OS_SCRIPT_VERIFY]
for (filename, required) in os_files.items():
os_files[filename] = utils.PathJoin(os_dir, filename)
try:
st = os.stat(os_files[filename])
except EnvironmentError, err:
if err.errno == errno.ENOENT and not required:
del os_files[filename]
continue
return False, ("File '%s' under path '%s' is missing (%s)" %
(filename, os_dir, utils.ErrnoOrStr(err)))
if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
return False, ("File '%s' under path '%s' is not a regular file" %
(filename, os_dir))
if filename in constants.OS_SCRIPTS:
if stat.S_IMODE(st.st_mode) & stat.S_IXUSR != stat.S_IXUSR:
return False, ("File '%s' under path '%s' is not executable" %
(filename, os_dir))
if not constants.OS_SCRIPT_CREATE in os_files and \
not constants.OS_SCRIPT_CREATE_UNTRUSTED in os_files:
return False, ("A create script (trusted or untrusted) under path '%s'"
" must exist" % os_dir)
create_script = os_files.get(constants.OS_SCRIPT_CREATE, None)
create_script_untrusted = os_files.get(constants.OS_SCRIPT_CREATE_UNTRUSTED,
None)
variants = []
if constants.OS_VARIANTS_FILE in os_files:
variants_file = os_files[constants.OS_VARIANTS_FILE]
try:
variants = \
utils.FilterEmptyLinesAndComments(utils.ReadFile(variants_file))
except EnvironmentError, err:
# we accept missing files, but not other errors
if err.errno != errno.ENOENT:
return False, ("Error while reading the OS variants file at %s: %s" %
(variants_file, utils.ErrnoOrStr(err)))
parameters = []
if constants.OS_PARAMETERS_FILE in os_files:
parameters_file = os_files[constants.OS_PARAMETERS_FILE]
try:
parameters = utils.ReadFile(parameters_file).splitlines()
except EnvironmentError, err:
return False, ("Error while reading the OS parameters file at %s: %s" %
(parameters_file, utils.ErrnoOrStr(err)))
parameters = [v.split(None, 1) for v in parameters]
os_obj = objects.OS(name=name, path=os_dir,
create_script=create_script,
create_script_untrusted=create_script_untrusted,
export_script=os_files[constants.OS_SCRIPT_EXPORT],
import_script=os_files[constants.OS_SCRIPT_IMPORT],
rename_script=os_files[constants.OS_SCRIPT_RENAME],
verify_script=os_files.get(constants.OS_SCRIPT_VERIFY,
None),
supported_variants=variants,
supported_parameters=parameters,
api_versions=api_versions)
return True, os_obj
def OSFromDisk(name, base_dir=None):
"""Create an OS instance from disk.
This function will return an OS instance if the given name is a
valid OS name. Otherwise, it will raise an appropriate
L{RPCFail} exception, detailing why this is not a valid OS.
This is just a wrapper over L{_TryOSFromDisk}, which doesn't raise
an exception but returns true/false status data.
@type base_dir: string
@keyword base_dir: Base directory containing OS installations.
Defaults to a search in all the OS_SEARCH_PATH dirs.
@rtype: L{objects.OS}
@return: the OS instance if we find a valid one
@raise RPCFail: if we don't find a valid OS
"""
name_only = objects.OS.GetName(name)
status, payload = _TryOSFromDisk(name_only, base_dir)
if not status:
_Fail(payload)
return payload
def OSCoreEnv(os_name, inst_os, os_params, debug=0):
"""Calculate the basic environment for an os script.
@type os_name: str
@param os_name: full operating system name (including variant)
@type inst_os: L{objects.OS}
@param inst_os: operating system for which the environment is being built
@type os_params: dict
@param os_params: the OS parameters
@type debug: integer
@param debug: debug level (0 or 1, for OS Api 10)
@rtype: dict
@return: dict of environment variables
@raise errors.BlockDeviceError: if the block device
cannot be found
"""
result = {}
api_version = \
max(constants.OS_API_VERSIONS.intersection(inst_os.api_versions))
result["OS_API_VERSION"] = "%d" % api_version
result["OS_NAME"] = inst_os.name
result["DEBUG_LEVEL"] = "%d" % debug
# OS variants
if api_version >= constants.OS_API_V15 and inst_os.supported_variants:
variant = objects.OS.GetVariant(os_name)
if not variant:
variant = inst_os.supported_variants[0]
else:
variant = ""
result["OS_VARIANT"] = variant
# OS params
for pname, pvalue in os_params.items():
result["OSP_%s" % pname.upper().replace("-", "_")] = pvalue
# Set a default path otherwise programs called by OS scripts (or
# even hooks called from OS scripts) might break, and we don't want
# to have each script require setting a PATH variable
result["PATH"] = constants.HOOKS_PATH
return result
def OSEnvironment(instance, inst_os, debug=0):
"""Calculate the environment for an os script.
@type instance: L{objects.Instance}
@param instance: target instance for the os script run
@type inst_os: L{objects.OS}
@param inst_os: operating system for which the environment is being built
@type debug: integer
@param debug: debug level (0 or 1, for OS Api 10)
@rtype: dict
@return: dict of environment variables
@raise errors.BlockDeviceError: if the block device
cannot be found
"""
result = OSCoreEnv(instance.os, inst_os, objects.FillDict(instance.osparams,
instance.osparams_private.Unprivate()), debug=debug)
for attr in ["name", "os", "uuid", "ctime", "mtime", "primary_node"]:
result["INSTANCE_%s" % attr.upper()] = str(getattr(instance, attr))
result["HYPERVISOR"] = instance.hypervisor
result["DISK_COUNT"] = "%d" % len(instance.disks_info)
result["NIC_COUNT"] = "%d" % len(instance.nics)
result["INSTANCE_SECONDARY_NODES"] = \
("%s" % " ".join(instance.secondary_nodes))
# Disks
for idx, disk in enumerate(instance.disks_info):
real_disk = _OpenRealBD(disk)
uri = _CalculateDeviceURI(instance, disk, real_disk)
result["DISK_%d_ACCESS" % idx] = disk.mode
result["DISK_%d_UUID" % idx] = disk.uuid
if real_disk.dev_path:
result["DISK_%d_PATH" % idx] = real_disk.dev_path
if uri:
result["DISK_%d_URI" % idx] = uri
if disk.name:
result["DISK_%d_NAME" % idx] = disk.name
if constants.HV_DISK_TYPE in instance.hvparams:
result["DISK_%d_FRONTEND_TYPE" % idx] = \
instance.hvparams[constants.HV_DISK_TYPE]
if disk.dev_type in constants.DTS_BLOCK:
result["DISK_%d_BACKEND_TYPE" % idx] = "block"
elif disk.dev_type in constants.DTS_FILEBASED:
result["DISK_%d_BACKEND_TYPE" % idx] = \
"file:%s" % disk.logical_id[0]
# NICs
for idx, nic in enumerate(instance.nics):
result["NIC_%d_MAC" % idx] = nic.mac
result["NIC_%d_UUID" % idx] = nic.uuid
if nic.name:
result["NIC_%d_NAME" % idx] = nic.name
if nic.ip:
result["NIC_%d_IP" % idx] = nic.ip
result["NIC_%d_MODE" % idx] = nic.nicparams[constants.NIC_MODE]
if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
result["NIC_%d_BRIDGE" % idx] = nic.nicparams[constants.NIC_LINK]
if nic.nicparams[constants.NIC_LINK]:
result["NIC_%d_LINK" % idx] = nic.nicparams[constants.NIC_LINK]
if nic.netinfo:
nobj = objects.Network.FromDict(nic.netinfo)
result.update(nobj.HooksDict("NIC_%d_" % idx))
if constants.HV_NIC_TYPE in instance.hvparams:
result["NIC_%d_FRONTEND_TYPE" % idx] = \
instance.hvparams[constants.HV_NIC_TYPE]
# HV/BE params
for source, kind in [(instance.beparams, "BE"), (instance.hvparams, "HV")]:
for key, value in source.items():
result["INSTANCE_%s_%s" % (kind, key)] = str(value)
return result
def DiagnoseExtStorage(top_dirs=None):
"""Compute the validity for all ExtStorage Providers.
@type top_dirs: list
@param top_dirs: the list of directories in which to
search (if not given defaults to
L{pathutils.ES_SEARCH_PATH})
@rtype: list of L{objects.ExtStorage}
@return: a list of tuples (name, path, status, diagnose, parameters)
for all (potential) ExtStorage Providers under all
search paths, where:
- name is the (potential) ExtStorage Provider
- path is the full path to the ExtStorage Provider
- status True/False is the validity of the ExtStorage Provider
- diagnose is the error message for an invalid ExtStorage Provider,
otherwise empty
- parameters is a list of (name, help) parameters, if any
"""
if top_dirs is None:
top_dirs = pathutils.ES_SEARCH_PATH
result = []
for dir_name in top_dirs:
if os.path.isdir(dir_name):
try:
f_names = utils.ListVisibleFiles(dir_name)
except EnvironmentError, err:
logging.exception("Can't list the ExtStorage directory %s: %s",
dir_name, err)
break
for name in f_names:
es_path = utils.PathJoin(dir_name, name)
status, es_inst = extstorage.ExtStorageFromDisk(name, base_dir=dir_name)
if status:
diagnose = ""
parameters = es_inst.supported_parameters
else:
diagnose = es_inst
parameters = []
result.append((name, es_path, status, diagnose, parameters))
return result
def BlockdevGrow(disk, amount, dryrun, backingstore, excl_stor):
"""Grow a stack of block devices.
This function is called recursively, with the childrens being the
first ones to resize.
@type disk: L{objects.Disk}
@param disk: the disk to be grown
@type amount: integer
@param amount: the amount (in mebibytes) to grow with
@type dryrun: boolean
@param dryrun: whether to execute the operation in simulation mode
only, without actually increasing the size
@param backingstore: whether to execute the operation on backing storage
only, or on "logical" storage only; e.g. DRBD is logical storage,
whereas LVM, file, RBD are backing storage
@rtype: (status, result)
@type excl_stor: boolean
@param excl_stor: Whether exclusive_storage is active
@return: a tuple with the status of the operation (True/False), and
the errors message if status is False
"""
r_dev = _RecursiveFindBD(disk)
if r_dev is None:
_Fail("Cannot find block device %s", disk)
try:
r_dev.Grow(amount, dryrun, backingstore, excl_stor)
except errors.BlockDeviceError, err:
_Fail("Failed to grow block device: %s", err, exc=True)
def BlockdevSnapshot(disk, snap_name, snap_size):
"""Create a snapshot copy of a block device.
This function is called recursively, and the snapshot is actually created
just for the leaf lvm backend device.
@type disk: L{objects.Disk}
@param disk: the disk to be snapshotted
@type snap_name: string
@param snap_name: the name of the snapshot
@type snap_size: int
@param snap_size: the size of the snapshot
@rtype: string
@return: snapshot disk ID as (vg, lv)
"""
def _DiskSnapshot(disk, snap_name=None, snap_size=None):
r_dev = _RecursiveFindBD(disk)
if r_dev is not None:
return r_dev.Snapshot(snap_name=snap_name, snap_size=snap_size)
else:
_Fail("Cannot find block device %s", disk)
if disk.SupportsSnapshots():
if disk.dev_type == constants.DT_DRBD8:
if not disk.children:
_Fail("DRBD device '%s' without backing storage cannot be snapshotted",
disk.unique_id)
return BlockdevSnapshot(disk.children[0], snap_name, snap_size)
else:
return _DiskSnapshot(disk, snap_name, snap_size)
else:
_Fail("Cannot snapshot block device '%s' of type '%s'",
disk.logical_id, disk.dev_type)
def BlockdevSetInfo(disk, info):
"""Sets 'metadata' information on block devices.
This function sets 'info' metadata on block devices. Initial
information is set at device creation; this function should be used
for example after renames.
@type disk: L{objects.Disk}
@param disk: the disk to be grown
@type info: string
@param info: new 'info' metadata
@rtype: (status, result)
@return: a tuple with the status of the operation (True/False), and
the errors message if status is False
"""
r_dev = _RecursiveFindBD(disk)
if r_dev is None:
_Fail("Cannot find block device %s", disk)
try:
r_dev.SetInfo(info)
except errors.BlockDeviceError, err:
_Fail("Failed to set information on block device: %s", err, exc=True)
def FinalizeExport(instance, snap_disks):
"""Write out the export configuration information.
@type instance: L{objects.Instance}
@param instance: the instance which we export, used for
saving configuration
@type snap_disks: list of L{objects.Disk}
@param snap_disks: list of snapshot block devices, which
will be used to get the actual name of the dump file
@rtype: None
"""
destdir = utils.PathJoin(pathutils.EXPORT_DIR, instance.name + ".new")
finaldestdir = utils.PathJoin(pathutils.EXPORT_DIR, instance.name)
disk_template = utils.GetDiskTemplate(snap_disks)
config = objects.SerializableConfigParser()
config.add_section(constants.INISECT_EXP)
config.set(constants.INISECT_EXP, "version", str(constants.EXPORT_VERSION))
config.set(constants.INISECT_EXP, "timestamp", "%d" % int(time.time()))
config.set(constants.INISECT_EXP, "source", instance.primary_node)
config.set(constants.INISECT_EXP, "os", instance.os)
config.set(constants.INISECT_EXP, "compression", "none")
config.add_section(constants.INISECT_INS)
config.set(constants.INISECT_INS, "name", instance.name)
config.set(constants.INISECT_INS, "maxmem", "%d" %
instance.beparams[constants.BE_MAXMEM])
config.set(constants.INISECT_INS, "minmem", "%d" %
instance.beparams[constants.BE_MINMEM])
# "memory" is deprecated, but useful for exporting to old ganeti versions
config.set(constants.INISECT_INS, "memory", "%d" %
instance.beparams[constants.BE_MAXMEM])
config.set(constants.INISECT_INS, "vcpus", "%d" %
instance.beparams[constants.BE_VCPUS])
config.set(constants.INISECT_INS, "disk_template", disk_template)
config.set(constants.INISECT_INS, "hypervisor", instance.hypervisor)
config.set(constants.INISECT_INS, "tags", " ".join(instance.GetTags()))
nic_total = 0
for nic_count, nic in enumerate(instance.nics):
nic_total += 1
config.set(constants.INISECT_INS, "nic%d_mac" %
nic_count, "%s" % nic.mac)
config.set(constants.INISECT_INS, "nic%d_ip" % nic_count, "%s" % nic.ip)
config.set(constants.INISECT_INS, "nic%d_network" % nic_count,
"%s" % nic.network)
config.set(constants.INISECT_INS, "nic%d_name" % nic_count,
"%s" % nic.name)
for param in constants.NICS_PARAMETER_TYPES:
config.set(constants.INISECT_INS, "nic%d_%s" % (nic_count, param),
"%s" % nic.nicparams.get(param, None))
# TODO: redundant: on load can read nics until it doesn't exist
config.set(constants.INISECT_INS, "nic_count", "%d" % nic_total)
disk_total = 0
for disk_count, disk in enumerate(snap_disks):
if disk:
disk_total += 1
config.set(constants.INISECT_INS, "disk%d_ivname" % disk_count,
("%s" % disk.iv_name))
config.set(constants.INISECT_INS, "disk%d_dump" % disk_count,
("%s" % disk.uuid))
config.set(constants.INISECT_INS, "disk%d_size" % disk_count,
("%d" % disk.size))
config.set(constants.INISECT_INS, "disk%d_name" % disk_count,
"%s" % disk.name)
config.set(constants.INISECT_INS, "disk_count", "%d" % disk_total)
# New-style hypervisor/backend parameters
config.add_section(constants.INISECT_HYP)
for name, value in instance.hvparams.items():
if name not in constants.HVC_GLOBALS:
config.set(constants.INISECT_HYP, name, str(value))
config.add_section(constants.INISECT_BEP)
for name, value in instance.beparams.items():
config.set(constants.INISECT_BEP, name, str(value))
config.add_section(constants.INISECT_OSP)
for name, value in instance.osparams.items():
config.set(constants.INISECT_OSP, name, str(value))
config.add_section(constants.INISECT_OSP_PRIVATE)
for name, value in instance.osparams_private.items():
config.set(constants.INISECT_OSP_PRIVATE, name, str(value.Get()))
utils.WriteFile(utils.PathJoin(destdir, constants.EXPORT_CONF_FILE),
data=config.Dumps())
shutil.rmtree(finaldestdir, ignore_errors=True)
shutil.move(destdir, finaldestdir)
def ExportInfo(dest):
"""Get export configuration information.
@type dest: str
@param dest: directory containing the export
@rtype: L{objects.SerializableConfigParser}
@return: a serializable config file containing the
export info
"""
cff = utils.PathJoin(dest, constants.EXPORT_CONF_FILE)
config = objects.SerializableConfigParser()
config.read(cff)
if (not config.has_section(constants.INISECT_EXP) or
not config.has_section(constants.INISECT_INS)):
_Fail("Export info file doesn't have the required fields")
return config.Dumps()
def ListExports():
"""Return a list of exports currently available on this machine.
@rtype: list
@return: list of the exports
"""
if os.path.isdir(pathutils.EXPORT_DIR):
return sorted(utils.ListVisibleFiles(pathutils.EXPORT_DIR))
else:
_Fail("No exports directory")
def RemoveExport(export):
"""Remove an existing export from the node.
@type export: str
@param export: the name of the export to remove
@rtype: None
"""
target = utils.PathJoin(pathutils.EXPORT_DIR, export)
try:
shutil.rmtree(target)
except EnvironmentError, err:
_Fail("Error while removing the export: %s", err, exc=True)
def BlockdevRename(devlist):
"""Rename a list of block devices.
@type devlist: list of tuples
@param devlist: list of tuples of the form (disk, new_unique_id); disk is
an L{objects.Disk} object describing the current disk, and new
unique_id is the name we rename it to
@rtype: boolean
@return: True if all renames succeeded, False otherwise
"""
msgs = []
result = True
for disk, unique_id in devlist:
dev = _RecursiveFindBD(disk)
if dev is None:
msgs.append("Can't find device %s in rename" % str(disk))
result = False
continue
try:
old_rpath = dev.dev_path
dev.Rename(unique_id)
new_rpath = dev.dev_path
if old_rpath != new_rpath:
DevCacheManager.RemoveCache(old_rpath)
# FIXME: we should add the new cache information here, like:
# DevCacheManager.UpdateCache(new_rpath, owner, ...)
# but we don't have the owner here - maybe parse from existing
# cache? for now, we only lose lvm data when we rename, which
# is less critical than DRBD or MD
except errors.BlockDeviceError, err:
msgs.append("Can't rename device '%s' to '%s': %s" %
(dev, unique_id, err))
logging.exception("Can't rename device '%s' to '%s'", dev, unique_id)
result = False
if not result:
_Fail("; ".join(msgs))
def _TransformFileStorageDir(fs_dir):
"""Checks whether given file_storage_dir is valid.
Checks wheter the given fs_dir is within the cluster-wide default
file_storage_dir or the shared_file_storage_dir, which are stored in
SimpleStore. Only paths under those directories are allowed.
@type fs_dir: str
@param fs_dir: the path to check
@return: the normalized path if valid, None otherwise
"""
filestorage.CheckFileStoragePath(fs_dir)
return os.path.normpath(fs_dir)
def CreateFileStorageDir(file_storage_dir):
"""Create file storage directory.
@type file_storage_dir: str
@param file_storage_dir: directory to create
@rtype: tuple
@return: tuple with first element a boolean indicating wheter dir
creation was successful or not
"""
file_storage_dir = _TransformFileStorageDir(file_storage_dir)
if os.path.exists(file_storage_dir):
if not os.path.isdir(file_storage_dir):
_Fail("Specified storage dir '%s' is not a directory",
file_storage_dir)
else:
try:
os.makedirs(file_storage_dir, 0750)
except OSError, err:
_Fail("Cannot create file storage directory '%s': %s",
file_storage_dir, err, exc=True)
def RemoveFileStorageDir(file_storage_dir):
"""Remove file storage directory.
Remove it only if it's empty. If not log an error and return.
@type file_storage_dir: str
@param file_storage_dir: the directory we should cleanup
@rtype: tuple (success,)
@return: tuple of one element, C{success}, denoting
whether the operation was successful
"""
file_storage_dir = _TransformFileStorageDir(file_storage_dir)
if os.path.exists(file_storage_dir):
if not os.path.isdir(file_storage_dir):
_Fail("Specified Storage directory '%s' is not a directory",
file_storage_dir)
# deletes dir only if empty, otherwise we want to fail the rpc call
try:
os.rmdir(file_storage_dir)
except OSError, err:
_Fail("Cannot remove file storage directory '%s': %s",
file_storage_dir, err)
def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir):
"""Rename the file storage directory.
@type old_file_storage_dir: str
@param old_file_storage_dir: the current path
@type new_file_storage_dir: str
@param new_file_storage_dir: the name we should rename to
@rtype: tuple (success,)
@return: tuple of one element, C{success}, denoting
whether the operation was successful
"""
old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir)
new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir)
if not os.path.exists(new_file_storage_dir):
if os.path.isdir(old_file_storage_dir):
try:
os.rename(old_file_storage_dir, new_file_storage_dir)
except OSError, err:
_Fail("Cannot rename '%s' to '%s': %s",
old_file_storage_dir, new_file_storage_dir, err)
else:
_Fail("Specified storage dir '%s' is not a directory",
old_file_storage_dir)
else:
if os.path.exists(old_file_storage_dir):
_Fail("Cannot rename '%s' to '%s': both locations exist",
old_file_storage_dir, new_file_storage_dir)
def _EnsureJobQueueFile(file_name):
"""Checks whether the given filename is in the queue directory.
@type file_name: str
@param file_name: the file name we should check
@rtype: None
@raises RPCFail: if the file is not valid
"""
if not utils.IsBelowDir(pathutils.QUEUE_DIR, file_name):
_Fail("Passed job queue file '%s' does not belong to"
" the queue directory '%s'", file_name, pathutils.QUEUE_DIR)
def JobQueueUpdate(file_name, content):
"""Updates a file in the queue directory.
This is just a wrapper over L{utils.io.WriteFile}, with proper
checking.
@type file_name: str
@param file_name: the job file name
@type content: str
@param content: the new job contents
@rtype: boolean
@return: the success of the operation
"""
file_name = vcluster.LocalizeVirtualPath(file_name)
_EnsureJobQueueFile(file_name)
getents = runtime.GetEnts()
# Write and replace the file atomically
utils.WriteFile(file_name, data=_Decompress(content), uid=getents.masterd_uid,
gid=getents.daemons_gid, mode=constants.JOB_QUEUE_FILES_PERMS)
def JobQueueRename(old, new):
"""Renames a job queue file.
This is just a wrapper over os.rename with proper checking.
@type old: str
@param old: the old (actual) file name
@type new: str
@param new: the desired file name
@rtype: tuple
@return: the success of the operation and payload
"""
old = vcluster.LocalizeVirtualPath(old)
new = vcluster.LocalizeVirtualPath(new)
_EnsureJobQueueFile(old)
_EnsureJobQueueFile(new)
getents = runtime.GetEnts()
utils.RenameFile(old, new, mkdir=True, mkdir_mode=0750,
dir_uid=getents.masterd_uid, dir_gid=getents.daemons_gid)
def BlockdevClose(instance_name, disks):
"""Closes the given block devices.
This means they will be switched to secondary mode (in case of
DRBD).
@param instance_name: if the argument is not empty, the symlinks
of this instance will be removed
@type disks: list of L{objects.Disk}
@param disks: the list of disks to be closed
@rtype: tuple (success, message)
@return: a tuple of success and message, where success
indicates the succes of the operation, and message
which will contain the error details in case we
failed
"""
bdevs = []
for cf in disks:
rd = _RecursiveFindBD(cf)
if rd is None:
_Fail("Can't find device %s", cf)
bdevs.append(rd)
msg = []
for rd in bdevs:
try:
rd.Close()
except errors.BlockDeviceError, err:
msg.append(str(err))
if msg:
_Fail("Can't close devices: %s", ",".join(msg))
else:
if instance_name:
_RemoveBlockDevLinks(instance_name, disks)
def BlockdevOpen(instance_name, disks, exclusive):
"""Opens the given block devices.
"""
bdevs = []
for cf in disks:
rd = _RecursiveFindBD(cf)
if rd is None:
_Fail("Can't find device %s", cf)
bdevs.append(rd)
msg = []
for idx, rd in enumerate(bdevs):
try:
rd.Open(exclusive=exclusive)
_SymlinkBlockDev(instance_name, rd.dev_path, idx)
except errors.BlockDeviceError, err:
msg.append(str(err))
if msg:
_Fail("Can't open devices: %s", ",".join(msg))
def ValidateHVParams(hvname, hvparams):
"""Validates the given hypervisor parameters.
@type hvname: string
@param hvname: the hypervisor name
@type hvparams: dict
@param hvparams: the hypervisor parameters to be validated
@rtype: None
"""
try:
hv_type = hypervisor.GetHypervisor(hvname)
hv_type.ValidateParameters(hvparams)
except errors.HypervisorError, err:
_Fail(str(err), log=False)
def _CheckOSPList(os_obj, parameters):
"""Check whether a list of parameters is supported by the OS.
@type os_obj: L{objects.OS}
@param os_obj: OS object to check
@type parameters: list
@param parameters: the list of parameters to check
"""
supported = [v[0] for v in os_obj.supported_parameters]
delta = frozenset(parameters).difference(supported)
if delta:
_Fail("The following parameters are not supported"
" by the OS %s: %s" % (os_obj.name, utils.CommaJoin(delta)))
def _CheckOSVariant(os_obj, name):
"""Check whether an OS name conforms to the os variants specification.
@type os_obj: L{objects.OS}
@param os_obj: OS object to check
@type name: string
@param name: OS name passed by the user, to check for validity
@rtype: NoneType
@return: None
@raise RPCFail: if OS variant is not valid
"""
variant = objects.OS.GetVariant(name)
if not os_obj.supported_variants:
if variant:
_Fail("OS '%s' does not support variants ('%s' passed)" %
(os_obj.name, variant))
else:
return
if not variant:
_Fail("OS name '%s' must include a variant" % name)
if variant not in os_obj.supported_variants:
_Fail("OS '%s' does not support variant '%s'" % (os_obj.name, variant))
def ValidateOS(required, osname, checks, osparams, force_variant):
"""Validate the given OS parameters.
@type required: boolean
@param required: whether absence of the OS should translate into
failure or not
@type osname: string
@param osname: the OS to be validated
@type checks: list
@param checks: list of the checks to run (currently only 'parameters')
@type osparams: dict
@param osparams: dictionary with OS parameters, some of which may be
private.
@rtype: boolean
@return: True if the validation passed, or False if the OS was not
found and L{required} was false
"""
if not constants.OS_VALIDATE_CALLS.issuperset(checks):
_Fail("Unknown checks required for OS %s: %s", osname,
set(checks).difference(constants.OS_VALIDATE_CALLS))
name_only = objects.OS.GetName(osname)
status, tbv = _TryOSFromDisk(name_only, None)
if not status:
if required:
_Fail(tbv)
else:
return False
if not force_variant:
_CheckOSVariant(tbv, osname)
if max(tbv.api_versions) < constants.OS_API_V20:
return True
if constants.OS_VALIDATE_PARAMETERS in checks:
_CheckOSPList(tbv, osparams.keys())
validate_env = OSCoreEnv(osname, tbv, osparams)
result = utils.RunCmd([tbv.verify_script] + checks, env=validate_env,
cwd=tbv.path, reset_env=True)
if result.failed:
logging.error("os validate command '%s' returned error: %s output: %s",
result.cmd, result.fail_reason, result.output)
_Fail("OS validation script failed (%s), output: %s",
result.fail_reason, result.output, log=False)
return True
def ExportOS(instance, override_env):
"""Creates a GZIPed tarball with an OS definition and environment.
The archive contains a file with the environment variables needed by
the OS scripts.
@type instance: L{objects.Instance}
@param instance: instance for which the OS definition is exported
@type override_env: dict of string to string
@param override_env: if supplied, it overrides the environment on a
key-by-key basis that is part of the archive
@rtype: string
@return: filepath of the archive
"""
assert instance
assert instance.os
temp_dir = tempfile.mkdtemp()
inst_os = OSFromDisk(instance.os)
result = utils.RunCmd(["ln", "-s", inst_os.path,
utils.PathJoin(temp_dir, "os")])
if result.failed:
_Fail("Failed to copy OS package '%s' to '%s': %s, output '%s'",
inst_os, temp_dir, result.fail_reason, result.output)
env = OSEnvironment(instance, inst_os)
env.update(override_env)
with open(utils.PathJoin(temp_dir, "environment"), "w") as f:
for var in env:
f.write(var + "=" + env[var] + "\n")
(fd, os_package) = tempfile.mkstemp(suffix=".tgz")
os.close(fd)
result = utils.RunCmd(["tar", "--dereference", "-czv",
"-f", os_package,
"-C", temp_dir,
"."])
if result.failed:
_Fail("Failed to create OS archive '%s': %s, output '%s'",
os_package, result.fail_reason, result.output)
result = utils.RunCmd(["rm", "-rf", temp_dir])
if result.failed:
_Fail("Failed to remove copy of OS package '%s' in '%s': %s, output '%s'",
inst_os, temp_dir, result.fail_reason, result.output)
return os_package
def DemoteFromMC():
"""Demotes the current node from master candidate role.
"""
# try to ensure we're not the master by mistake
master, myself = ssconf.GetMasterAndMyself()
if master == myself:
_Fail("ssconf status shows I'm the master node, will not demote")
result = utils.RunCmd([pathutils.DAEMON_UTIL, "check", constants.MASTERD])
if not result.failed:
_Fail("The master daemon is running, will not demote")
try:
if os.path.isfile(pathutils.CLUSTER_CONF_FILE):
utils.CreateBackup(pathutils.CLUSTER_CONF_FILE)
except EnvironmentError, err:
if err.errno != errno.ENOENT:
_Fail("Error while backing up cluster file: %s", err, exc=True)
utils.RemoveFile(pathutils.CLUSTER_CONF_FILE)
def _GetX509Filenames(cryptodir, name):
"""Returns the full paths for the private key and certificate.
"""
return (utils.PathJoin(cryptodir, name),
utils.PathJoin(cryptodir, name, _X509_KEY_FILE),
utils.PathJoin(cryptodir, name, _X509_CERT_FILE))
def CreateX509Certificate(validity, cryptodir=pathutils.CRYPTO_KEYS_DIR):
"""Creates a new X509 certificate for SSL/TLS.
@type validity: int
@param validity: Validity in seconds
@rtype: tuple; (string, string)
@return: Certificate name and public part
"""
serial_no = int(time.time())
(key_pem, cert_pem) = \
utils.GenerateSelfSignedX509Cert(netutils.Hostname.GetSysName(),
min(validity, _MAX_SSL_CERT_VALIDITY),
serial_no)
cert_dir = tempfile.mkdtemp(dir=cryptodir,
prefix="x509-%s-" % utils.TimestampForFilename())
try:
name = os.path.basename(cert_dir)
assert len(name) > 5
(_, key_file, cert_file) = _GetX509Filenames(cryptodir, name)
utils.WriteFile(key_file, mode=0400, data=key_pem)
utils.WriteFile(cert_file, mode=0400, data=cert_pem)
# Never return private key as it shouldn't leave the node
return (name, cert_pem)
except Exception:
shutil.rmtree(cert_dir, ignore_errors=True)
raise
def RemoveX509Certificate(name, cryptodir=pathutils.CRYPTO_KEYS_DIR):
"""Removes a X509 certificate.
@type name: string
@param name: Certificate name
"""
(cert_dir, key_file, cert_file) = _GetX509Filenames(cryptodir, name)
utils.RemoveFile(key_file)
utils.RemoveFile(cert_file)
try:
os.rmdir(cert_dir)
except EnvironmentError, err:
_Fail("Cannot remove certificate directory '%s': %s",
cert_dir, err)
def _GetImportExportIoCommand(instance, mode, ieio, ieargs):
"""Returns the command for the requested input/output.
@type instance: L{objects.Instance}
@param instance: The instance object
@param mode: Import/export mode
@param ieio: Input/output type
@param ieargs: Input/output arguments
"""
assert mode in (constants.IEM_IMPORT, constants.IEM_EXPORT)
env = None
prefix = None
suffix = None
exp_size = None
if ieio == constants.IEIO_FILE:
(filename, ) = ieargs
if not utils.IsNormAbsPath(filename):
_Fail("Path '%s' is not normalized or absolute", filename)
real_filename = os.path.realpath(filename)
directory = os.path.dirname(real_filename)
if not utils.IsBelowDir(pathutils.EXPORT_DIR, real_filename):
_Fail("File '%s' is not under exports directory '%s': %s",
filename, pathutils.EXPORT_DIR, real_filename)
# Create directory
utils.Makedirs(directory, mode=0750)
quoted_filename = utils.ShellQuote(filename)
if mode == constants.IEM_IMPORT:
suffix = "> %s" % quoted_filename
elif mode == constants.IEM_EXPORT:
suffix = "< %s" % quoted_filename
# Retrieve file size
try:
st = os.stat(filename)
except EnvironmentError, err:
logging.error("Can't stat(2) %s: %s", filename, err)
else:
exp_size = utils.BytesToMebibyte(st.st_size)
elif ieio == constants.IEIO_RAW_DISK:
(disk, ) = ieargs
real_disk = _OpenRealBD(disk)
if mode == constants.IEM_IMPORT:
suffix = "| %s" % utils.ShellQuoteArgs(real_disk.Import())
elif mode == constants.IEM_EXPORT:
prefix = "%s |" % utils.ShellQuoteArgs(real_disk.Export())
exp_size = disk.size
elif ieio == constants.IEIO_SCRIPT:
(disk, disk_index, ) = ieargs
assert isinstance(disk_index, (int, long))
inst_os = OSFromDisk(instance.os)
env = OSEnvironment(instance, inst_os)
if mode == constants.IEM_IMPORT:
env["IMPORT_DEVICE"] = env["DISK_%d_PATH" % disk_index]
env["IMPORT_INDEX"] = str(disk_index)
script = inst_os.import_script
elif mode == constants.IEM_EXPORT:
real_disk = _OpenRealBD(disk)
env["EXPORT_DEVICE"] = real_disk.dev_path
env["EXPORT_INDEX"] = str(disk_index)
script = inst_os.export_script
# TODO: Pass special environment only to script
script_cmd = utils.BuildShellCmd("( cd %s && %s; )", inst_os.path, script)
if mode == constants.IEM_IMPORT:
suffix = "| %s" % script_cmd
elif mode == constants.IEM_EXPORT:
prefix = "%s |" % script_cmd
# Let script predict size
exp_size = constants.IE_CUSTOM_SIZE
else:
_Fail("Invalid %s I/O mode %r", mode, ieio)
return (env, prefix, suffix, exp_size)
def _CreateImportExportStatusDir(prefix):
"""Creates status directory for import/export.
"""
return tempfile.mkdtemp(dir=pathutils.IMPORT_EXPORT_DIR,
prefix=("%s-%s-" %
(prefix, utils.TimestampForFilename())))
def StartImportExportDaemon(mode, opts, host, port, instance, component,
ieio, ieioargs):
"""Starts an import or export daemon.
@param mode: Import/output mode
@type opts: L{objects.ImportExportOptions}
@param opts: Daemon options
@type host: string
@param host: Remote host for export (None for import)
@type port: int
@param port: Remote port for export (None for import)
@type instance: L{objects.Instance}
@param instance: Instance object
@type component: string
@param component: which part of the instance is transferred now,
e.g. 'disk/0'
@param ieio: Input/output type
@param ieioargs: Input/output arguments
"""
# Use Import/Export over socat.
#
# Export() gives a command that produces a flat stream.
# Import() gives a command that reads a flat stream to a disk template.
if mode == constants.IEM_IMPORT:
prefix = "import"
if not (host is None and port is None):
_Fail("Can not specify host or port on import")
elif mode == constants.IEM_EXPORT:
prefix = "export"
if host is None or port is None:
_Fail("Host and port must be specified for an export")
else:
_Fail("Invalid mode %r", mode)
if (opts.key_name is None) ^ (opts.ca_pem is None):
_Fail("Cluster certificate can only be used for both key and CA")
(cmd_env, cmd_prefix, cmd_suffix, exp_size) = \
_GetImportExportIoCommand(instance, mode, ieio, ieioargs)
if opts.key_name is None:
# Use server.pem
key_path = pathutils.NODED_CERT_FILE
cert_path = pathutils.NODED_CERT_FILE
assert opts.ca_pem is None
else:
(_, key_path, cert_path) = _GetX509Filenames(pathutils.CRYPTO_KEYS_DIR,
opts.key_name)
assert opts.ca_pem is not None
for i in [key_path, cert_path]:
if not os.path.exists(i):
_Fail("File '%s' does not exist" % i)
status_dir = _CreateImportExportStatusDir("%s-%s" % (prefix, component))
try:
status_file = utils.PathJoin(status_dir, _IES_STATUS_FILE)
pid_file = utils.PathJoin(status_dir, _IES_PID_FILE)
ca_file = utils.PathJoin(status_dir, _IES_CA_FILE)
if opts.ca_pem is None:
# Use server.pem
ca = utils.ReadFile(pathutils.NODED_CERT_FILE)
else:
ca = opts.ca_pem
# Write CA file
utils.WriteFile(ca_file, data=ca, mode=0400)
cmd = [
pathutils.IMPORT_EXPORT_DAEMON,
status_file, mode,
"--key=%s" % key_path,
"--cert=%s" % cert_path,
"--ca=%s" % ca_file,
]
if host:
cmd.append("--host=%s" % host)
if port:
cmd.append("--port=%s" % port)
if opts.ipv6:
cmd.append("--ipv6")
else:
cmd.append("--ipv4")
if opts.compress:
cmd.append("--compress=%s" % opts.compress)
if opts.magic:
cmd.append("--magic=%s" % opts.magic)
if exp_size is not None:
cmd.append("--expected-size=%s" % exp_size)
if cmd_prefix:
cmd.append("--cmd-prefix=%s" % cmd_prefix)
if cmd_suffix:
cmd.append("--cmd-suffix=%s" % cmd_suffix)
if mode == constants.IEM_EXPORT:
# Retry connection a few times when connecting to remote peer
cmd.append("--connect-retries=%s" % constants.RIE_CONNECT_RETRIES)
cmd.append("--connect-timeout=%s" % constants.RIE_CONNECT_ATTEMPT_TIMEOUT)
elif opts.connect_timeout is not None:
assert mode == constants.IEM_IMPORT
# Overall timeout for establishing connection while listening
cmd.append("--connect-timeout=%s" % opts.connect_timeout)
logfile = _InstanceLogName(prefix, instance.os, instance.name, component)
# TODO: Once _InstanceLogName uses tempfile.mkstemp, StartDaemon has
# support for receiving a file descriptor for output
utils.StartDaemon(cmd, env=cmd_env, pidfile=pid_file,
output=logfile)
# The import/export name is simply the status directory name
return os.path.basename(status_dir)
except Exception:
shutil.rmtree(status_dir, ignore_errors=True)
raise
def GetImportExportStatus(names):
"""Returns import/export daemon status.
@type names: sequence
@param names: List of names
@rtype: List of dicts
@return: Returns a list of the state of each named import/export or None if a
status couldn't be read
"""
result = []
for name in names:
status_file = utils.PathJoin(pathutils.IMPORT_EXPORT_DIR, name,
_IES_STATUS_FILE)
try:
data = utils.ReadFile(status_file)
except EnvironmentError, err:
if err.errno != errno.ENOENT:
raise
data = None
if not data:
result.append(None)
continue
result.append(serializer.LoadJson(data))
return result
def AbortImportExport(name):
"""Sends SIGTERM to a running import/export daemon.
"""
logging.info("Abort import/export %s", name)
status_dir = utils.PathJoin(pathutils.IMPORT_EXPORT_DIR, name)
pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE))
if pid:
logging.info("Import/export %s is running with PID %s, sending SIGTERM",
name, pid)
utils.IgnoreProcessNotFound(os.kill, pid, signal.SIGTERM)
def CleanupImportExport(name):
"""Cleanup after an import or export.
If the import/export daemon is still running it's killed. Afterwards the
whole status directory is removed.
"""
logging.info("Finalizing import/export %s", name)
status_dir = utils.PathJoin(pathutils.IMPORT_EXPORT_DIR, name)
pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE))
if pid:
logging.info("Import/export %s is still running with PID %s",
name, pid)
utils.KillProcess(pid, waitpid=False)
shutil.rmtree(status_dir, ignore_errors=True)
def _FindDisks(disks):
"""Finds attached L{BlockDev}s for the given disks.
@type disks: list of L{objects.Disk}
@param disks: the disk objects we need to find
@return: list of L{BlockDev} objects or C{None} if a given disk
was not found or was no attached.
"""
bdevs = []
for disk in disks:
rd = _RecursiveFindBD(disk)
if rd is None:
_Fail("Can't find device %s", disk)
bdevs.append(rd)
return bdevs
def DrbdDisconnectNet(disks):
"""Disconnects the network on a list of drbd devices.
"""
bdevs = _FindDisks(disks)
# disconnect disks
for rd in bdevs:
try:
rd.DisconnectNet()
except errors.BlockDeviceError, err:
_Fail("Can't change network configuration to standalone mode: %s",
err, exc=True)
def DrbdAttachNet(disks, multimaster):
"""Attaches the network on a list of drbd devices.
"""
bdevs = _FindDisks(disks)
# reconnect disks, switch to new master configuration and if
# needed primary mode
for rd in bdevs:
try:
rd.AttachNet(multimaster)
except errors.BlockDeviceError, err:
_Fail("Can't change network configuration: %s", err)
# wait until the disks are connected; we need to retry the re-attach
# if the device becomes standalone, as this might happen if the one
# node disconnects and reconnects in a different mode before the
# other node reconnects; in this case, one or both of the nodes will
# decide it has wrong configuration and switch to standalone
def _Attach():
all_connected = True
for rd in bdevs:
stats = rd.GetProcStatus()
if multimaster:
# In the multimaster case we have to wait explicitly until
# the resource is Connected and UpToDate/UpToDate, because
# we promote *both nodes* to primary directly afterwards.
# Being in resync is not enough, since there is a race during which we
# may promote a node with an Outdated disk to primary, effectively
# tearing down the connection.
all_connected = (all_connected and
stats.is_connected and
stats.is_disk_uptodate and
stats.peer_disk_uptodate)
else:
all_connected = (all_connected and
(stats.is_connected or stats.is_in_resync))
if stats.is_standalone:
# peer had different config info and this node became
# standalone, even though this should not happen with the
# new staged way of changing disk configs
try:
rd.AttachNet(multimaster)
except errors.BlockDeviceError, err:
_Fail("Can't change network configuration: %s", err)
if not all_connected:
raise utils.RetryAgain()
try:
# Start with a delay of 100 miliseconds and go up to 5 seconds
utils.Retry(_Attach, (0.1, 1.5, 5.0), 2 * 60)
except utils.RetryTimeout:
_Fail("Timeout in disk reconnecting")
def DrbdWaitSync(disks):
"""Wait until DRBDs have synchronized.
"""
def _helper(rd):
stats = rd.GetProcStatus()
if not (stats.is_connected or stats.is_in_resync):
raise utils.RetryAgain()
return stats
bdevs = _FindDisks(disks)
min_resync = 100
alldone = True
for rd in bdevs:
try:
# poll each second for 15 seconds
stats = utils.Retry(_helper, 1, 15, args=[rd])
except utils.RetryTimeout:
stats = rd.GetProcStatus()
# last check
if not (stats.is_connected or stats.is_in_resync):
_Fail("DRBD device %s is not in sync: stats=%s", rd, stats)
alldone = alldone and (not stats.is_in_resync)
if stats.sync_percent is not None:
min_resync = min(min_resync, stats.sync_percent)
return (alldone, min_resync)
def DrbdNeedsActivation(disks):
"""Checks which of the passed disks needs activation and returns their UUIDs.
"""
faulty_disks = []
for disk in disks:
rd = _RecursiveFindBD(disk)
if rd is None:
faulty_disks.append(disk)
continue
stats = rd.GetProcStatus()
if stats.is_standalone or stats.is_diskless:
faulty_disks.append(disk)
return [disk.uuid for disk in faulty_disks]
def GetDrbdUsermodeHelper():
"""Returns DRBD usermode helper currently configured.
"""
try:
return drbd.DRBD8.GetUsermodeHelper()
except errors.BlockDeviceError, err:
_Fail(str(err))
def PowercycleNode(hypervisor_type, hvparams=None):
"""Hard-powercycle the node.
Because we need to return first, and schedule the powercycle in the
background, we won't be able to report failures nicely.
"""
hyper = hypervisor.GetHypervisor(hypervisor_type)
try:
pid = os.fork()
except OSError:
# if we can't fork, we'll pretend that we're in the child process
pid = 0
if pid > 0:
return "Reboot scheduled in 5 seconds"
# ensure the child is running on ram
try:
utils.Mlockall()
except Exception: # pylint: disable=W0703
pass
time.sleep(5)
hyper.PowercycleNode(hvparams=hvparams)
def _VerifyRestrictedCmdName(cmd):
"""Verifies a restricted command name.
@type cmd: string
@param cmd: Command name
@rtype: tuple; (boolean, string or None)
@return: The tuple's first element is the status; if C{False}, the second
element is an error message string, otherwise it's C{None}
"""
if not cmd.strip():
return (False, "Missing command name")
if os.path.basename(cmd) != cmd:
return (False, "Invalid command name")
if not constants.EXT_PLUGIN_MASK.match(cmd):
return (False, "Command name contains forbidden characters")
return (True, None)
def _CommonRestrictedCmdCheck(path, owner):
"""Common checks for restricted command file system directories and files.
@type path: string
@param path: Path to check
@param owner: C{None} or tuple containing UID and GID
@rtype: tuple; (boolean, string or C{os.stat} result)
@return: The tuple's first element is the status; if C{False}, the second
element is an error message string, otherwise it's the result of C{os.stat}
"""
if owner is None:
# Default to root as owner
owner = (0, 0)
try:
st = os.stat(path)
except EnvironmentError, err:
return (False, "Can't stat(2) '%s': %s" % (path, err))
if stat.S_IMODE(st.st_mode) & (~_RCMD_MAX_MODE):
return (False, "Permissions on '%s' are too permissive" % path)
if (st.st_uid, st.st_gid) != owner:
(owner_uid, owner_gid) = owner
return (False, "'%s' is not owned by %s:%s" % (path, owner_uid, owner_gid))
return (True, st)
def _VerifyRestrictedCmdDirectory(path, _owner=None):
"""Verifies restricted command directory.
@type path: string
@param path: Path to check
@rtype: tuple; (boolean, string or None)
@return: The tuple's first element is the status; if C{False}, the second
element is an error message string, otherwise it's C{None}
"""
(status, value) = _CommonRestrictedCmdCheck(path, _owner)
if not status:
return (False, value)
if not stat.S_ISDIR(value.st_mode):
return (False, "Path '%s' is not a directory" % path)
return (True, None)
def _VerifyRestrictedCmd(path, cmd, _owner=None):
"""Verifies a whole restricted command and returns its executable filename.
@type path: string
@param path: Directory containing restricted commands
@type cmd: string
@param cmd: Command name
@rtype: tuple; (boolean, string)
@return: The tuple's first element is the status; if C{False}, the second
element is an error message string, otherwise the second element is the
absolute path to the executable
"""
executable = utils.PathJoin(path, cmd)
(status, msg) = _CommonRestrictedCmdCheck(executable, _owner)
if not status:
return (False, msg)
if not utils.IsExecutable(executable):
return (False, "access(2) thinks '%s' can't be executed" % executable)
return (True, executable)
def _PrepareRestrictedCmd(path, cmd,
_verify_dir=_VerifyRestrictedCmdDirectory,
_verify_name=_VerifyRestrictedCmdName,
_verify_cmd=_VerifyRestrictedCmd):
"""Performs a number of tests on a restricted command.
@type path: string
@param path: Directory containing restricted commands
@type cmd: string
@param cmd: Command name
@return: Same as L{_VerifyRestrictedCmd}
"""
# Verify the directory first
(status, msg) = _verify_dir(path)
if status:
# Check command if everything was alright
(status, msg) = _verify_name(cmd)
if not status:
return (False, msg)
# Check actual executable
return _verify_cmd(path, cmd)
def RunRestrictedCmd(cmd,
_lock_timeout=_RCMD_LOCK_TIMEOUT,
_lock_file=pathutils.RESTRICTED_COMMANDS_LOCK_FILE,
_path=pathutils.RESTRICTED_COMMANDS_DIR,
_sleep_fn=time.sleep,
_prepare_fn=_PrepareRestrictedCmd,
_runcmd_fn=utils.RunCmd,
_enabled=constants.ENABLE_RESTRICTED_COMMANDS):
"""Executes a restricted command after performing strict tests.
@type cmd: string
@param cmd: Command name
@rtype: string
@return: Command output
@raise RPCFail: In case of an error
"""
logging.info("Preparing to run restricted command '%s'", cmd)
if not _enabled:
_Fail("Restricted commands disabled at configure time")
lock = None
try:
cmdresult = None
try:
lock = utils.FileLock.Open(_lock_file)
lock.Exclusive(blocking=True, timeout=_lock_timeout)
(status, value) = _prepare_fn(_path, cmd)
if status:
cmdresult = _runcmd_fn([value], env={}, reset_env=True,
postfork_fn=lambda _: lock.Unlock())
else:
logging.error(value)
except Exception: # pylint: disable=W0703
# Keep original error in log
logging.exception("Caught exception")
if cmdresult is None:
logging.info("Sleeping for %0.1f seconds before returning",
_RCMD_INVALID_DELAY)
_sleep_fn(_RCMD_INVALID_DELAY)
# Do not include original error message in returned error
_Fail("Executing command '%s' failed" % cmd)
elif cmdresult.failed or cmdresult.fail_reason:
_Fail("Restricted command '%s' failed: %s; output: %s",
cmd, cmdresult.fail_reason, cmdresult.output)
else:
return cmdresult.output
finally:
if lock is not None:
# Release lock at last
lock.Close()
lock = None
def SetWatcherPause(until, _filename=pathutils.WATCHER_PAUSEFILE):
"""Creates or removes the watcher pause file.
@type until: None or number
@param until: Unix timestamp saying until when the watcher shouldn't run
"""
if until is None:
logging.info("Received request to no longer pause watcher")
utils.RemoveFile(_filename)
else:
logging.info("Received request to pause watcher until %s", until)
if not ht.TNumber(until):
_Fail("Duration must be numeric")
utils.WriteFile(_filename, data="%d\n" % (until, ), mode=0644)
def ConfigureOVS(ovs_name, ovs_link):
"""Creates a OpenvSwitch on the node.
This function sets up a OpenvSwitch on the node with given name nad
connects it via a given eth device.
@type ovs_name: string
@param ovs_name: Name of the OpenvSwitch to create.
@type ovs_link: None or string
@param ovs_link: Ethernet device for outside connection (can be missing)
"""
# Initialize the OpenvSwitch
result = utils.RunCmd(["ovs-vsctl", "add-br", ovs_name])
if result.failed:
_Fail("Failed to create openvswitch. Script return value: %s, output: '%s'"
% (result.exit_code, result.output), log=True)
# And connect it to a physical interface, if given
if ovs_link:
result = utils.RunCmd(["ovs-vsctl", "add-port", ovs_name, ovs_link])
if result.failed:
_Fail("Failed to connect openvswitch to interface %s. Script return"
" value: %s, output: '%s'" % (ovs_link, result.exit_code,
result.output), log=True)
def GetFileInfo(file_path):
""" Checks if a file exists and returns information related to it.
Currently returned information:
- file size: int, size in bytes
@type file_path: string
@param file_path: Name of file to examine.
@rtype: tuple of bool, dict
@return: Whether the file exists, and a dictionary of information about the
file gathered by os.stat.
"""
try:
stat_info = os.stat(file_path)
values_dict = {
constants.STAT_SIZE: stat_info.st_size,
}
return True, values_dict
except IOError:
return False, {}
class HooksRunner(object):
"""Hook runner.
This class is instantiated on the node side (ganeti-noded) and not
on the master side.
"""
def __init__(self, hooks_base_dir=None):
"""Constructor for hooks runner.
@type hooks_base_dir: str or None
@param hooks_base_dir: if not None, this overrides the
L{pathutils.HOOKS_BASE_DIR} (useful for unittests)
"""
if hooks_base_dir is None:
hooks_base_dir = pathutils.HOOKS_BASE_DIR
# yeah, _BASE_DIR is not valid for attributes, we use it like a
# constant
self._BASE_DIR = hooks_base_dir # pylint: disable=C0103
def RunLocalHooks(self, node_list, hpath, phase, env):
"""Check that the hooks will be run only locally and then run them.
"""
assert len(node_list) == 1
node = node_list[0]
_, myself = ssconf.GetMasterAndMyself()
assert node == myself
results = self.RunHooks(hpath, phase, env)
# Return values in the form expected by HooksMaster
return {node: (None, False, results)}
def RunHooks(self, hpath, phase, env):
"""Run the scripts in the hooks directory.
@type hpath: str
@param hpath: the path to the hooks directory which
holds the scripts
@type phase: str
@param phase: either L{constants.HOOKS_PHASE_PRE} or
L{constants.HOOKS_PHASE_POST}
@type env: dict
@param env: dictionary with the environment for the hook
@rtype: list
@return: list of 3-element tuples:
- script path
- script result, either L{constants.HKR_SUCCESS} or
L{constants.HKR_FAIL}
- output of the script
@raise errors.ProgrammerError: for invalid input
parameters
"""
if phase == constants.HOOKS_PHASE_PRE:
suffix = "pre"
elif phase == constants.HOOKS_PHASE_POST:
suffix = "post"
else:
_Fail("Unknown hooks phase '%s'", phase)
subdir = "%s-%s.d" % (hpath, suffix)
dir_name = utils.PathJoin(self._BASE_DIR, subdir)
results = []
if not os.path.isdir(dir_name):
# for non-existing/non-dirs, we simply exit instead of logging a
# warning at every operation
return results
runparts_results = utils.RunParts(dir_name, env=env, reset_env=True)
for (relname, relstatus, runresult) in runparts_results:
if relstatus == constants.RUNPARTS_SKIP:
rrval = constants.HKR_SKIP
output = ""
elif relstatus == constants.RUNPARTS_ERR:
rrval = constants.HKR_FAIL
output = "Hook script execution error: %s" % runresult
elif relstatus == constants.RUNPARTS_RUN:
if runresult.failed:
rrval = constants.HKR_FAIL
else:
rrval = constants.HKR_SUCCESS
output = utils.SafeEncode(runresult.output.strip())
results.append(("%s/%s" % (subdir, relname), rrval, output))
return results
class IAllocatorRunner(object):
"""IAllocator runner.
This class is instantiated on the node side (ganeti-noded) and not on
the master side.
"""
@staticmethod
def Run(name, idata, ial_params):
"""Run an iallocator script.
@type name: str
@param name: the iallocator script name
@type idata: str
@param idata: the allocator input data
@type ial_params: list
@param ial_params: the iallocator parameters
@rtype: tuple
@return: two element tuple of:
- status
- either error message or stdout of allocator (for success)
"""
alloc_script = utils.FindFile(name, constants.IALLOCATOR_SEARCH_PATH,
os.path.isfile)
if alloc_script is None:
_Fail("iallocator module '%s' not found in the search path", name)
fd, fin_name = tempfile.mkstemp(prefix="ganeti-iallocator.")
try:
os.write(fd, idata)
os.close(fd)
result = utils.RunCmd([alloc_script, fin_name] + ial_params)
if result.failed:
_Fail("iallocator module '%s' failed: %s, output '%s'",
name, result.fail_reason, result.output)
finally:
os.unlink(fin_name)
return result.stdout
class DevCacheManager(object):
"""Simple class for managing a cache of block device information.
"""
_DEV_PREFIX = "/dev/"
_ROOT_DIR = pathutils.BDEV_CACHE_DIR
@classmethod
def _ConvertPath(cls, dev_path):
"""Converts a /dev/name path to the cache file name.
This replaces slashes with underscores and strips the /dev
prefix. It then returns the full path to the cache file.
@type dev_path: str
@param dev_path: the C{/dev/} path name
@rtype: str
@return: the converted path name
"""
if dev_path.startswith(cls._DEV_PREFIX):
dev_path = dev_path[len(cls._DEV_PREFIX):]
dev_path = dev_path.replace("/", "_")
fpath = utils.PathJoin(cls._ROOT_DIR, "bdev_%s" % dev_path)
return fpath
@classmethod
def UpdateCache(cls, dev_path, owner, on_primary, iv_name):
"""Updates the cache information for a given device.
@type dev_path: str
@param dev_path: the pathname of the device
@type owner: str
@param owner: the owner (instance name) of the device
@type on_primary: bool
@param on_primary: whether this is the primary
node nor not
@type iv_name: str
@param iv_name: the instance-visible name of the
device, as in objects.Disk.iv_name
@rtype: None
"""
if dev_path is None:
logging.error("DevCacheManager.UpdateCache got a None dev_path")
return
fpath = cls._ConvertPath(dev_path)
if on_primary:
state = "primary"
else:
state = "secondary"
if iv_name is None:
iv_name = "not_visible"
fdata = "%s %s %s\n" % (str(owner), state, iv_name)
try:
utils.WriteFile(fpath, data=fdata)
except EnvironmentError, err:
logging.exception("Can't update bdev cache for %s: %s", dev_path, err)
@classmethod
def RemoveCache(cls, dev_path):
"""Remove data for a dev_path.
This is just a wrapper over L{utils.io.RemoveFile} with a converted
path name and logging.
@type dev_path: str
@param dev_path: the pathname of the device
@rtype: None
"""
if dev_path is None:
logging.error("DevCacheManager.RemoveCache got a None dev_path")
return
fpath = cls._ConvertPath(dev_path)
try:
utils.RemoveFile(fpath)
except EnvironmentError, err:
logging.exception("Can't update bdev cache for %s: %s", dev_path, err)
|