/usr/lib/obs/server/bs_worker is in obs-server 2.7.1-10.
This file is owned by root:root, with mode 0o755.
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 | #!/usr/bin/perl -w
#
# Copyright (c) 2006-2009 Michael Schroeder, Novell Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################
#
# Worker build process. Builds jobs received from a Repository Server,
# sends build binary packages back.
#
BEGIN {
my ($wd) = $0 =~ m-(.*)/- ;
$wd ||= '.';
unshift @INC, "$wd/build";
unshift @INC, "$wd";
}
use Digest::MD5 ();
use XML::Structured ':bytes';
use Data::Dumper;
use POSIX;
use Fcntl qw(:DEFAULT :flock);
BEGIN { Fcntl->import(':seek') unless defined &SEEK_SET; }
use Storable;
use BSRPC;
use BSServer;
use BSDispatch; # for parse_cgi
use BSConfiguration;
use BSUtil;
use BSXML;
use BSKiwiXML;
use BSHTTP;
use BSBuild;
use BSCando;
use strict;
my @binsufs = qw{rpm deb pkg.tar.gz pkg.tar.xz};
my $binsufsre = join('|', map {"\Q$_\E"} @binsufs);
my $buildroot;
my $port;
my $statedir;
my $hostarch;
my $vm = '';
my $vm_tmpfs_mode;
my $vm_root = '';
my $vm_swap = '';
my $vm_kernel;
my $vm_initrd;
my $vm_memory;
my $vm_enable_console;
my $vm_worker_name;
my $vm_worker_instance;
my $vmdisk_rootsize;
my $vmdisk_swapsize;
my $vmdisk_filesystem;
my $vmdisk_mount_options;
my $vmdisk_clean;
my $emulator_script;
my $hugetlbfs;
my $workerid;
my $srcserver;
my @reposervers;
my $testmode;
my $noworkercheck;
my $nobuildcodecheck;
my $oneshot;
my $silent;
my $hostcheck;
my $localkiwi;
my $owner;
my @hostlabel;
my $getbinariesproxy;
my $hardstatus;
my $jobs;
my $threads;
my $cachedir;
my $cachesize;
# current XEN has no xenstore anymore
my $xenstore_maxsize = 20 * 1000000;
# 1 hour timeout to avoid forever hanging workers
my $gettimeout = 1 * 3600;
my $buildlog_maxsize = $BSConfig::buildlog_maxsize ? $BSConfig::buildlog_maxsize : 500 * 1000000;
my $buildlog_maxidle = $BSConfig::buildlog_maxidle ? $BSConfig::buildlog_maxidle : 8 * 3600;
# for collecting statistics
my $binariesdownload;
my $binariescachehits;
my $binariesdownloadsize;
$hostcheck = $BSConfig::workerhostcheck if defined($BSConfig::workerhostcheck);
sub lockstate {
while (1) {
open(STATELOCK, '>>', "$statedir/state") || die("$statedir/state: $!\n");
flock(STATELOCK, LOCK_EX) || die("flock $statedir/state: $!\n");
my @s = stat(STATELOCK);
last if $s[3]; # check nlink
close(STATELOCK); # race, try again
}
my $oldstate = readxml("$statedir/state", $BSXML::workerstate, 1);
$oldstate = {} unless $oldstate;
return $oldstate;
}
sub unlockstate {
close(STATELOCK);
}
sub commitstate {
my ($newstate) = @_;
writexml("$statedir/state.new", "$statedir/state", $newstate, $BSXML::workerstate) if $newstate;
close(STATELOCK);
}
sub hardstatus {
return unless $hardstatus;
my $l = $_[0] || '';
$l =~ s/[\000-\037\177-\377]//g; # ascii only, please
$l = substr($l, 0, 40) if length($l) > 40;
print "\033_$l\033\\";
}
sub trunc_logfile {
my ($lf) = @_;
open(LF, '<', $lf) || return;
my $buf;
sysread(LF, $buf, 1000000);
$buf .= "\n\n[truncated]\n\n";
sysseek(LF, -1000000, 2);
sysread(LF, $buf, 1000000, length($buf));
close LF;
$buf .= "\nLogfile got too big, killed job.\n";
open(LF, ">$lf.new") || return;
syswrite(LF, $buf);
close LF;
rename("$lf.new", $lf);
}
sub tail_logfile {
my ($lf, $size) = @_;
local *LF;
open(LF, '<', $lf) || return '';
if (-s LF > $size) {
defined(sysseek(LF, -$size, 2)) || return '';
}
my $buf = '';
sysread(LF, $buf, $size);
close LF;
return $buf;
}
sub cleanup_job {
if ($vm_tmpfs_mode) {
# no need to umount if $buildroot is already umounted
if (not qsystem('mountpoint', '-q', $buildroot)) {
qsystem("umount", "-l", $buildroot) && die("umount tmpfs failed: $!\n");
}
}
}
sub kill_job {
my @args;
# same args as in the build call
if ($vm =~ /(xen|kvm|zvm|emulator)/) {
push @args, '--root', "$buildroot/.mount";
if ($vm =~ /emulator/) {
push @args, '--vm-type', 'emulator';
push @args, '--vm-disk', $vm_root;
push @args, '--emulator-script', $emulator_script if $emulator_script;
} else {
push @args, '--vm-worker', $vm_worker_name if $vm_worker_name;
push @args, '--vm-worker-nr', $vm_worker_instance if $vm_worker_instance;
push @args, $vm, $vm_root;
}
} else {
push @args, '--root', $buildroot;
push @args, '--vm-type', 'lxc' if $vm =~ /lxc/;
push @args, '--vm-type', 'docker' if $vm =~ /docker/;
}
if (system("$statedir/build/build", @args, "--kill")) {
return 0;
}
cleanup_job();
return 1;
}
sub usage {
my ($ret) = @_;
print <<EOF;
Usage: $0 [OPTION] --root <directory> --statedir <directory>
--root : buildroot directory
--port : fixed port number
--statedir : state directory
--id : worker id
--reposerver: define reposerver, can be used multiple times
--arch : define hostarch (overrides 'uname -m')
currently supported architectures:
@{[sort keys %BSCando::cando]}
--kvm : enable kvm
--xen : enable xen
--lxc : enable lxc
--docker : enable docker
--emulator : enable emulator (requires prepared emulator.sh script)
--zvm : enable z/VM
--tmpfs : uses tmpfs (memory) for the build root
--device : set kvm or xen root device (default is <root>/root file)
--swap : set kvm or xen swap device (default is <root>/swap file)
--hostlabel : define a host label for build constraints, can be used multiple times
--vm-kernel : set kernel to use (xen/kvm)
--vm-initrd : set initrd to use (xen/kvm)
--vm-memory : set amount of memory to use (xen/kvm)
--vm-worker NAME
: (z/VM) set name of the actual worker
--vm-worker-nr NUMBER
: (z/VM) set instance number of the actual worker
--vmdisk-rootsize <size>
: size of the root disk image (default 4096M)
--vmdisk-swapsize <size>
: size of the swap disk image (default 1024M)
--vmdisk-filesystem <none|ext3|ext4>
: filesystem to use for autosetup root disk image
--emulator-script <script>
: Define the emulator script
--hugetlbfs HUGETLBFSPATH
: Use hugetlb for memory management, path to mounted hugetlbfs.
--test : enable test mode
--build : just build the package, don't send anything back
(needs a buildinfo file as argument)
--noworkerupdate
: do not check if the worker is up-to-date
--nobuildupdate
: do not check if the build code is up-to-date
--nocodeupdate
: do not update both worker and build code
--jobs <nr> : hand over the number of parallel jobs to build
--threads <nr> : sets number of threads for KVM
--oneshot <seconds>
: just build one package, do not wait more then
<seconds> seconds if nothing is available
--hostcheck <hostcheck>
: call to check if the host can build the package
--owner <name>
: report the build host owner name to server
--cachedir <cachedir>
--cachesize <size_in_mb>
: use cachedir to cache fetched binaries
--getbinariesproxy <proxyserver>
: define a proxy server for getbinaries requests
--help : this message
EOF
exit($ret || 0);
}
my @saveargv = @ARGV; # so we can restart ourself
my $justbuild;
my $exitrestart;
my $exitrestart_timeout = 300; # wait max 5 minuntes
exit(0) if @ARGV == 1 && $ARGV[0] eq '--selftest';
while (@ARGV) {
usage(0) if $ARGV[0] eq '--help';
if ($ARGV[0] eq '--exit' || $ARGV[0] eq '--stop') {
shift @ARGV;
$exitrestart = 'exit';
next;
}
if ($ARGV[0] eq '--restart') {
shift @ARGV;
$exitrestart = 'restart';
next;
}
if ($ARGV[0] eq '--root') {
shift @ARGV;
$buildroot = shift @ARGV;
next;
}
if ($ARGV[0] eq '--port') {
shift @ARGV;
$port = shift @ARGV;
next;
}
if ($ARGV[0] eq '--arch') {
shift @ARGV;
$hostarch = shift @ARGV;
next;
}
if ($ARGV[0] eq '--statedir') {
shift @ARGV;
$statedir = shift @ARGV;
next;
}
if ($ARGV[0] eq '--srcserver') {
# default value used if buildinfo does not contain srcserver element
shift @ARGV;
$srcserver = shift @ARGV;
next;
}
if ($ARGV[0] eq '--reposerver') {
shift @ARGV;
my $server = shift @ARGV;
push @reposervers, $server unless grep {$_ eq $server} @reposervers;
next;
}
if ($ARGV[0] eq '--getbinariesproxy') {
shift @ARGV;
$getbinariesproxy = shift @ARGV;
next;
}
if ($ARGV[0] eq '--hostlabel') {
shift @ARGV;
my $label = shift @ARGV;
push @hostlabel, $label unless grep {$_ eq $label} @hostlabel;
next;
}
if ($ARGV[0] eq '--id') {
shift @ARGV;
$workerid = shift @ARGV;
next;
}
if ($ARGV[0] eq '--test') {
shift @ARGV;
$testmode = 1;
next;
}
if ($ARGV[0] =~ /^--(kvm|xen|lxc|emulator|zvm|docker)$/) {
$vm = "--$1";
shift @ARGV;
next;
}
if ($ARGV[0] eq '--tmpfs') {
shift @ARGV;
$vm_tmpfs_mode = 1;
next;
}
if ($ARGV[0] eq '--xendevice' || $ARGV[0] eq '--device') {
shift @ARGV;
$vm_root = shift @ARGV;
next;
}
if ($ARGV[0] eq '--xenswap' || $ARGV[0] eq '--swap') {
shift @ARGV;
$vm_swap = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vm-kernel') {
shift @ARGV;
$vm_kernel = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vm-initrd') {
shift @ARGV;
$vm_initrd = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vm-memory') {
shift @ARGV;
$vm_memory = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vm-worker') {
shift @ARGV;
$vm_worker_name = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vm-worker-nr') {
shift @ARGV;
$vm_worker_instance = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vm-enable-console') {
shift @ARGV;
$vm_enable_console = 1;
next;
}
if ($ARGV[0] eq '--vmdisk-rootsize') {
shift @ARGV;
$vmdisk_rootsize = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vmdisk-swapsize') {
shift @ARGV;
$vmdisk_swapsize = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vmdisk-filesystem') {
shift @ARGV;
$vmdisk_filesystem = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vmdisk-mount-options') {
shift @ARGV;
$vmdisk_mount_options = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vmdisk-clean') {
shift @ARGV;
$vmdisk_clean = 1;
next;
}
if ($ARGV[0] eq '--emulator-script') {
shift @ARGV;
$emulator_script = shift @ARGV;
next;
}
if ($ARGV[0] eq '--hugetlbfs') {
shift @ARGV;
$hugetlbfs = shift @ARGV;
next;
}
if ($ARGV[0] eq '--build') {
shift @ARGV;
$justbuild = 1;
next;
}
if ($ARGV[0] eq '--oneshot') {
shift @ARGV;
$oneshot = shift @ARGV;
next;
}
if ($ARGV[0] eq '--hostcheck') {
shift @ARGV;
$hostcheck = shift @ARGV;
next;
}
if ($ARGV[0] eq '--nocodeupdate') {
shift @ARGV;
$noworkercheck = 1;
$nobuildcodecheck = 1;
next;
}
if ($ARGV[0] eq '--noworkerupdate') {
shift @ARGV;
$noworkercheck = 1;
next;
}
if ($ARGV[0] eq '--nobuildupdate') {
shift @ARGV;
$nobuildcodecheck = 1;
next;
}
if ($ARGV[0] eq '--silent') {
shift @ARGV;
$silent= 1;
next;
}
if ($ARGV[0] eq '--localkiwi') {
shift @ARGV;
$localkiwi = shift @ARGV;
next;
}
if ($ARGV[0] eq '--owner') {
shift @ARGV;
$owner = shift @ARGV;
next;
}
if ($ARGV[0] eq '--jobs') {
shift @ARGV;
$jobs = shift @ARGV;
next;
}
if ($ARGV[0] eq '--threads') {
shift @ARGV;
$threads = shift @ARGV;
next;
}
if ($ARGV[0] eq '--cachedir') {
shift @ARGV;
$cachedir = shift @ARGV;
next;
}
if ($ARGV[0] eq '--cachesize') {
shift @ARGV;
$cachesize = shift @ARGV;
$cachesize *= 1024*1024;
next;
}
if ($ARGV[0] eq '--hardstatus') {
shift @ARGV;
$hardstatus = 1;
next;
}
last;
}
if ($exitrestart) {
usage(1) unless $statedir;
if ((! -e "$statedir/lock") || BSUtil::lockcheck('>>', "$statedir/lock")) {
die("worker not running.\n") if $exitrestart eq 'restart';
print "worker not running.\n";
exit(0);
}
my $state = lockstate();
$state->{'state'} = 'idle' unless $state->{'state'};
if ($state->{'state'} eq 'building' || $state->{'state'} eq 'killed' || $state->{'state'} eq 'discarded') {
$state->{'state'} = 'discarded';
$state->{'nextstate'} = $exitrestart eq 'exit' ? 'exit' : 'rebooting';
} else {
$state->{'state'} = $exitrestart eq 'exit' ? 'exit' : 'rebooting';
}
commitstate($state);
# now wait till the state changes...
while(1) {
select(undef, undef, undef, .1);
if (defined($exitrestart_timeout)) {
$exitrestart_timeout -= .1;
die("timeout reached, worker not responding\n") if $exitrestart_timeout < 0;
}
my $curstate = readxml("$statedir/state", $BSXML::workerstate, 1);
last unless $curstate && $curstate->{'state'};
next if $curstate->{'nextstate'};
last if $curstate->{'state'} ne 'exit' && $curstate->{'state'} ne 'rebooting';
}
exit(0);
}
usage(1) unless $buildroot && $statedir;
usage(1) if ($cachedir && !$cachesize) || ($cachesize && !$cachedir);
# default needs to be in sync with build(1)
# otherwise job killing does not work in a default setup
$vm_root = "${buildroot}.img" unless $vm_root;
$vm_swap = "${buildroot}.swap" unless $vm_swap;
# here's the build code we want to use
$::ENV{'BUILD_DIR'} = "$statedir/build";
if (!$hostarch) {
$hostarch = `uname -m`;
chomp $hostarch;
die("could not determine hostarch\n") unless $hostarch;
}
die("arch $hostarch cannot build anything!\n") unless $BSCando::cando{$hostarch} || ($hostarch eq 'local' && $localkiwi);
$srcserver = $BSConfig::srcserver unless defined $srcserver;
@reposervers = @BSConfig::reposervers unless @reposervers;
if ($justbuild) {
my $buildinfo = readxml($ARGV[0], $BSXML::buildinfo);
if ($localkiwi) {
# make sure this is the right job for us
die("not a kiwi job\n") unless $buildinfo->{'file'} =~ /\.kiwi$/;
die("not a kiwi product job\n") unless $buildinfo->{'imagetype'} && $buildinfo->{'imagetype'}->[0] eq 'product';
}
$| = 1;
dobuild($buildinfo);
exit(0);
}
# collect environment information for the dispatcher. To be checked with job constraints
sub device_size($) {
my ($device) = @_;
if ($device) {
if (-b $device) {
if (open(SIZE, '-|', '/sbin/blockdev', '--getsize64', $device)) {
my $line = <SIZE>;
chomp $line;
my $size = $line / ( 1024 * 1024 );
close SIZE;
return $size;
}
} else {
# implement me for loop device files
}
}
return undef;
}
# create worker info data
my $workerinfo = {};
# host labels
$workerinfo->{'hostlabel'} = \@hostlabel if @hostlabel;
# sandbox
$workerinfo->{'sandbox'} = 'chroot';
$workerinfo->{'sandbox'} = $1 if $vm =~ /(xen|kvm|emulator|lxc|zvm|docker)/;
# build host owner
$workerinfo->{'owner'} = $owner if $owner;
# linux kernel
my $str = readstr('/proc/version');
if ($str =~ /^Linux version ([^-]*)-([^-]*)-([^ ]*) /s) {
$workerinfo->{'linux'} = { 'version' => "$1-$2", 'flavor' => $3 };
}
my $hw = {'cpu' => {}, 'processors' => 0};
# memory, undefined in non vm mode since no guarantees are exist
if (open(FILE, "<", "/proc/cpuinfo")) {
while(<FILE>) {
chomp;
if (/^processor/s) {
$hw->{'processors'} = $hw->{'processors'} + 1;
} elsif (/^flags\s*:\s(.*)$/) {
my @cpuflags = split(' ', $1);
$hw->{'cpu'}->{'flag'} = \@cpuflags;
} elsif (/^cpu\s*:\sPOWER8/) {
# PowerPC kernel does not provide any flags, but we need to be able
# to distinguish between power7 and 8 at least
$hw->{'cpu'}->{'flag'} = [ "power7", "power8" ];
} elsif (/^cpu\s*:\sPOWER7/) {
$hw->{'cpu'}->{'flag'} = [ "power7" ];
}
}
close FILE;
}
$hw->{'memory'} = $vm_memory if $vm_memory;
$hw->{'swap'} = $vmdisk_swapsize if $vmdisk_swapsize;
$hw->{'disk'} = $vmdisk_rootsize if $vmdisk_rootsize;
if ($vm_root) {
my $size = device_size($vm_root);
$hw->{'disk'} = $size if $size;
}
if ($vm_swap) {
my $size = device_size($vm_swap);
$hw->{'swap'} = $size if $size;
}
$workerinfo->{'hardware'} = $hw;
sub stream_logfile {
my ($nostream, $start, $end) = @_;
open(F, "<$buildroot/.build.log") || die("$buildroot/.build.log: $!\n");
my @s = stat(F);
$start ||= 0;
if (defined($end)) {
$end -= $start;
die("end is smaller than start\n") if $end < 0;
}
die("Logfile is not that big\n") if $s[7] < abs($start);
defined(sysseek(F, $start, $start < 0 ? Fcntl::SEEK_END : Fcntl::SEEK_SET)) || die("sysseek: $!\n");
BSServer::reply(undef, 'Content-Type: text/plain', 'Transfer-Encoding: chunked');
my $pos = sysseek(F, 0, Fcntl::SEEK_CUR) || die("sysseek: $!\n");
while(!defined($end) || $end) {
@s = stat(F);
if ($s[7] <= $pos) {
last if !$s[3];
select(undef, undef, undef, .5);
next;
}
my $data = '';
my $l = $s[7] - $pos;
$l = 4096 if $l > 4096;
sysread(F, $data, $l);
next unless length($data);
$data = substr($data, 0, $end) if defined($end) && length($data) > $end;
$pos += length($data);
$end -= length($data) if defined $end;
$data = sprintf("%X\r\n", length($data)).$data."\r\n";
BSServer::swrite($data);
last if $nostream && $pos >= $s[7];
}
close F;
BSServer::swrite("0\r\n\r\n");
}
sub send_state {
my ($state, $p, $ba, $exclude) = @_;
my @args = ("state=$state", "arch=$ba", "port=$p");
push @args, "workerid=$workerid" if defined $workerid;
for my $server (@reposervers) {
next if $exclude && $server eq $exclude;
if ($state eq 'idle' && @reposervers > 1) {
my $curstate = readxml("$statedir/state", $BSXML::workerstate, 1);
last if $curstate && $curstate->{'state'} ne $state;
}
my $param = {
'uri' => "$server/worker",
'timeout' => 3,
};
if ($state eq 'idle') {
$param->{'request'} = 'POST';
$param->{'data'} = BSUtil::toxml($workerinfo, $BSXML::worker);
}
eval {
BSRPC::rpc($param, undef, @args);
};
print "send_state $server: $@" if $@;
}
}
sub send_dispatched {
my ($server, $p, $ba, $buildinfo) = @_;
my @args = ("hostarch=$ba", "port=$p");
push @args, "workerid=$workerid" if defined $workerid;
push @args, "job=$buildinfo->{'job'}";
push @args, "arch=$buildinfo->{'arch'}";
push @args, "jobid=$buildinfo->{'jobid'}";
my $param = {
'uri' => "$server/workerdispatched",
'request' => 'POST',
'timeout' => 300,
};
BSRPC::rpc($param, undef, @args);
}
sub codemd5 {
my ($dir) = @_;
my @files = grep {!/^\./} ls($dir);
my @bfiles = map {"Build/$_"} grep {!/^\./} ls("$dir/Build");
my @efiles = map {"emulator/$_"} grep {!/^\./} ls("$dir/emulator");
@files = sort(@files, @bfiles, @efiles);
my $md5 = '';
for my $file (@files) {
next if -l "$dir/$file" || -d _;
$md5 .= Digest::MD5::md5_hex(readstr("$dir/$file"))." $file\n";
}
$md5 = Digest::MD5::md5_hex($md5);
return $md5;
}
sub getcode {
my ($dir, $uri, $ineval) = @_;
# evalize ourself
if (!$ineval) {
my $md5;
eval {
$md5 = getcode($dir, $uri, 1);
};
if ($@) {
warn($@);
return '';
}
return $md5;
}
my $ndir = "$dir.new";
my $odir = "$dir.old";
# clean up stale runs
rm_rf($ndir) if -e $ndir;
rm_rf($odir) if -e $odir;
mkdir($ndir) || die("mkdir $ndir: $!\n");
my $res = BSRPC::rpc({
'uri' => $uri,
'directory' => $ndir,
'timeout' => $gettimeout,
'withmd5' => 1,
'createsubdirs' => 1,
'receiver' => \&BSHTTP::cpio_receiver,
});
die("getcode error\n") unless $res;
# got everything, clean things up, check if it really works
if ($dir eq 'worker') {
symlink('.', "$ndir/XML") || die("symlink: $!\n");
chmod(0755, "$ndir/bs_worker");
die("bs_worker selftest failed\n") if system("cd $ndir && ./bs_worker --selftest");
} elsif ($dir eq 'build') {
symlink('.', "$ndir/Date") || die("symlink: $!\n");
symlink('.', "$ndir/Time") || die("symlink: $!\n");
# we just change every file to be on the safe side
chmod(0755, "$ndir/$_->{'name'}") for @$res;
}
# ok, commit
if (-e $dir) {
rename($dir, $odir) || die("rename $dir $odir: $!\n");
}
rename($ndir, $dir) || die("rename $ndir $dir: $!\n");
rm_rf($odir) if -e $odir;
my $md5 = '';
for my $file (sort {$a->{'name'} cmp $b->{'name'}} @$res) {
$md5 .= "$file->{'md5'} $file->{'name'}\n" if $file->{'md5'};
}
$md5 = Digest::MD5::md5_hex($md5);
return $md5;
}
sub rm_rf {
my ($dir) = @_;
BSUtil::cleandir($dir);
rmdir($dir);
die("rmdir $dir failed\n") if -d $dir;
}
sub importbuild {
return if defined &Build::queryhdrmd5;
unshift @INC, "$statedir/build";
require Build;
Build->import();
}
sub getsources {
my ($buildinfo, $dir) = @_;
my @meta;
push @meta, ($buildinfo->{'verifymd5'} || $buildinfo->{'srcmd5'})." $buildinfo->{'package'}";
my $server = $buildinfo->{'srcserver'} || $srcserver;
my $res = BSRPC::rpc({
'uri' => "$server/getsources",
'directory' => $dir,
'timeout' => $gettimeout,
'withmd5' => 1,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, "project=$buildinfo->{'project'}", "package=$buildinfo->{'package'}", "srcmd5=$buildinfo->{'srcmd5'}");
die("Error\n") unless ref($res) eq 'ARRAY';
if (-e "$dir/.errors") {
my $errors = readstr("$dir/.errors", 1);
die("getsources: $errors");
}
# verify sources
my %res = map {$_->{'name'} => $_} @$res;
my $md5 = '';
my @f = ls($dir);
for my $f (sort @f) {
die("unexpected file: $f") unless $res{$f};
$md5 .= "$res{$f}->{'md5'} $f\n";
}
$md5 = Digest::MD5::md5_hex($md5);
die("source verification fails: $md5 != $buildinfo->{'verifymd5'}\n") if $md5 ne $buildinfo->{'verifymd5'};
return @meta unless $buildinfo->{'file'} =~ /\.kiwi$/;
# get additional kiwi sources
my @sdep = grep {($_->{'repoarch'} || '') eq 'src'} @{$buildinfo->{'bdep'} || []};
for my $src (@sdep) {
print "$src->{'name'}, ";
my $idir = "$src->{'project'}/$src->{'package'}";
$idir = "$dir/images/$idir";
mkdir_p($idir);
my $res = BSRPC::rpc({
'uri' => "$server/getsources",
'directory' => $idir,
'timeout' => $gettimeout,
'withmd5' => 1,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, "project=$src->{'project'}", "package=$src->{'package'}", "srcmd5=$src->{'srcmd5'}");
die("Error\n") unless ref($res) eq 'ARRAY';
if (-e "$idir/.errors") {
my $errors = readstr("$idir/.errors", 1);
die("getsources: $errors");
}
push @meta, "$src->{'srcmd5'} $src->{'project'}/$src->{'package'}";
}
return @meta;
}
sub getdeltasources {
my ($buildinfo, $dir) = @_;
my @meta;
push @meta, ($buildinfo->{'verifymd5'} || $buildinfo->{'srcmd5'})." $buildinfo->{'package'}";
my $server = $buildinfo->{'reposerver'};
my $res = BSRPC::rpc({
'uri' => "$server/getjobdata",
'directory' => $dir,
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, "job=$buildinfo->{'job'}", "arch=$buildinfo->{'arch'}", "jobid=$buildinfo->{'jobid'}");
die("Error\n") unless ref($res) eq 'ARRAY';
return @meta;
}
sub getfollowupsources {
my ($buildinfo, $dir) = @_;
my $server = $buildinfo->{'reposerver'};
my $res = BSRPC::rpc({
'uri' => "$server/getjobdata",
'directory' => $dir,
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, "job=$buildinfo->{'job'}", "arch=$buildinfo->{'arch'}", "jobid=$buildinfo->{'jobid'}");
die("Error\n") unless ref($res) eq 'ARRAY';
die("no old sources in followup job\n") unless @$res;
# need to get buildenv from old sources ;(
$server = $buildinfo->{'srcserver'} || $srcserver;
$res = BSRPC::rpc({
'uri' => "$server/source/$buildinfo->{'project'}/$buildinfo->{'package'}",
'timeout' => $gettimeout,
}, $BSXML::dir, "rev=$buildinfo->{'srcmd5'}");
my %entries = map {$_->{'name'} => $_} @{$res->{'entry'} || []};
my $bifile = "_buildenv.$buildinfo->{'repository'}.$buildinfo->{'arch'}";
$bifile = '_buildenv' unless $entries{$bifile};
return unless $entries{$bifile};
BSRPC::rpc({
'uri' => "$server/source/$buildinfo->{'project'}/$buildinfo->{'package'}/$bifile",
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::file_receiver,
'filename' => "$dir/_buildenv.$buildinfo->{'repository'}.$buildinfo->{'arch'}",
}, undef, "rev=$buildinfo->{'srcmd5'}");
}
sub getsslcert {
my ($buildinfo, $dir) = @_;
my $server = $buildinfo->{'srcserver'} || $srcserver;
my $cert;
eval {
$cert = BSRPC::rpc({
'uri' => "$server/getsslcert",
'timeout' => $gettimeout,
}, undef, "project=$buildinfo->{'project'}", "autoextend=1");
};
die("could not retrieve ssl certificate: $@\n") if $@;
writestr("$dir/_projectcert.crt", undef, $cert) if $cert;
}
sub qsystem {
my (@args) = @_;
my $pid;
if (!($pid = xfork())) {
$SIG{'PIPE'} = 'DEFAULT';
open(STDOUT, ">/dev/null") if $silent;
exec(@args);
die("$args[0]: $!\n");
}
waitpid($pid, 0) == $pid || die("waitpid $pid: $!\n");
return $?;
}
sub link_or_copy {
my ($from, $to) = @_;
unlink($to);
return 1 if link($from, $to);
local *F;
local *G;
return undef unless open(F, '<', $from);
if (!open(G, '>', $to)) {
close F;
return undef;
}
my $buf;
while (sysread(F, $buf, 8192)) {
(syswrite(G, $buf) || 0) == length($buf) || die("$to write: $!\n");
}
close(F);
if (!close(G)) {
unlink($to);
return undef;
}
return 1;
}
sub manage_cache {
my ($prunesize, $cacheold, $cachenew) = @_;
# get the lock
local *F;
BSUtil::lockopen(\*F, '+>>', "$cachedir/content", 1) || return;
my $content;
if (-s F) {
seek(F, 0, 0);
$content = Storable::fd_retrieve(\*F);
}
$content ||= [];
my %content = map {$_->[0] => $_->[1]} @$content;
# put cacheold, cachenew at the top
if ($cacheold && @$cacheold) {
splice(@$content, 0, 0, @$cacheold);
$content{$_->[0]} = $_->[1] for @$cacheold;
}
if ($cachenew) {
for my $c (reverse @$cachenew) {
my $path = pop(@$c);
my $cacheid = $c->[0];
my $cachefile = "$cachedir/".substr($cacheid, 0, 2)."/$cacheid";
mkdir_p("$cachedir/".substr($cacheid, 0, 2));
unlink("$cachefile.$$");
next unless link_or_copy($path, "$cachefile.$$");
rename("$cachefile.$$", $cachefile) || die("500 rename $cachefile.$$ $cachefile: $!\n");
unlink("$cachefile.$$");
my $mpath = "$path.meta";
$mpath = "$1.meta" if $path =~ /^(.*)\.(?:$binsufsre)$/;
if (-s $mpath) {
unlink("$cachefile.meta.$$");
if (link_or_copy($mpath, "$cachefile.meta.$$")) {
rename("$cachefile.meta.$$", "$cachefile.meta") || die("500 rename $cachefile.meta.$$ $cachefile.meta: $!\n");
unlink("$cachefile.meta.$$");
} else {
unlink("$cachefile.meta");
}
} else {
unlink("$cachefile.meta");
}
unshift @$content, $c;
$content{$c->[0]} = $c->[1];
}
}
# prune cache
my @pruneids;
for my $c (@$content) {
if (!defined delete $content{$c->[0]}) {
$c = undef; # already pruned
next;
}
$prunesize -= $c->[1];
if ($prunesize < 0) {
push @pruneids, $c->[0];
$c = undef;
}
}
for my $cacheid (sort @pruneids) {
my $cachefile = "$cachedir/".substr($cacheid, 0, 2)."/$cacheid";
unlink($cachefile);
unlink("$cachefile.meta");
}
@$content = grep {defined $_} @$content;
Storable::nstore($content, "$cachedir/content.new");
rename("$cachedir/content.new", "$cachedir/content") || die("500 rename $cachedir/content.new $cachedir/content: $!\n");
close F;
}
sub trygetbinariesproxy {
my ($server, $dir, $bins, $bvs, @args) = @_;
return undef unless $getbinariesproxy;
my @proxybins;
for my $bin (@$bins) {
my $bv = $bvs->{$bin};
if (!$bv || !$bv->{'name'} || $bv->{'name'} !~ /\.($binsufsre)$/) {
push @proxybins, '@'.$bin;
} else {
push @proxybins, $bv->{'hdrmd5'}.($bv->{'metamd5'} || '').':'.($bv->{'sizek'} || 0).":$1\@".$bin;
}
}
my $res;
eval {
$res = BSRPC::rpc({
'uri' => "$getbinariesproxy/getbinaries",
'directory' => $dir,
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, "server=$server", @args, 'binaries='.join(',', @proxybins));
};
if ($@) {
warn($@);
undef $res;
}
return $res;
}
sub getbinaries_cache {
my ($dir, $server, $projid, $repoid, $arch, $nometa, $bins, $bvl) = @_;
importbuild() unless defined &Build::queryhdrmd5;
if (! -d $dir) {
mkdir_p($dir) || die("mkdir_p $dir: $!\n");
}
my %ret;
undef $bvl unless $cachedir; # no use here if ther's no cachedir
if ($bvl) {
# check if it contains everything we want to know
my %bv;
for (@{$bvl->{'binary'} || []}) {
if ($_->{'error'}) {
$bv{$_->{'name'}} = 1;
} else {
$bv{$1} = 1 if $_->{'name'} =~ /(.*)\.(?:$binsufsre)$/;
}
}
undef $bvl if grep {!$bv{$_}} @$bins;
}
if ($cachedir && !$bvl) {
my @args;
push @args, "project=$projid";
push @args, "repository=$repoid";
push @args, "arch=$arch";
push @args, "nometa" if $nometa;
push @args, "binaries=".join(',', @$bins);
eval {
$bvl = BSRPC::rpc({
'uri' => "$server/getbinaryversions",
'timeout' => $gettimeout,
}, $BSXML::binaryversionlist, @args);
};
warn($@) if $@;
}
$bvl ||= {};
my %bv;
for (@{$bvl->{'binary'} || []}) {
if ($_->{'error'}) {
$bv{$_->{'name'}} = $_;
} else {
$bv{$1} = $_ if $_->{'name'} =~ /(.*)\.(?:$binsufsre)$/;
}
}
my @downloadbins;
my $downloadsizek = 0;
my @cacheold;
my @cachenew;
for my $bin (@$bins) {
my $bv = $bv{$bin};
if (!$bv) {
push @downloadbins, $bin;
next;
}
next if $bv->{'error'};
my $cacheid = Digest::MD5::md5_hex("$projid/$repoid/$arch/$bv->{'hdrmd5'}");
my $cachefile = "$cachedir/".substr($cacheid, 0, 2)."/$cacheid";
my $usecache = 0;
my $havemeta;
if (link_or_copy($cachefile, "$dir/$bv->{'name'}")) {
my @s = stat("$dir/$bv->{'name'}");
die unless @s;
if (!$nometa && $bv->{'hdrmd5'}) {
my $mn = $bv->{'name'};
$mn =~ s/\.(?:$binsufsre)/.meta/;
if (link_or_copy("$cachefile.meta", "$dir/$mn")) {
local *F;
open(F, '<', "$dir/$mn") || die;
my $ctx = Digest::MD5->new;
$ctx->addfile(*F);
close F;
if ($ctx->hexdigest() eq $bv->{'metamd5'}) {
$usecache = 1;
$havemeta = 1;
} else {
unlink("$dir/$mn");
}
}
} else {
$usecache = 1;
}
if ($usecache) {
# check hdrmd5 to be sure we got the right bin
my $id = Build::queryhdrmd5("$dir/$bv->{'name'}");
$usecache = 0 if ($id || '') ne $bv->{'hdrmd5'};
}
if (!$usecache) {
unlink("$dir/$bv->{'name'}");
} else {
push @cacheold, [$cacheid, $s[7]];
}
}
if (!$usecache) {
push @downloadbins, $bin;
$downloadsizek += $bv->{'sizek'};
} else {
$ret{$bin} = {'name' => $bv->{'name'}, 'hdrmd5' => $bv->{'hdrmd5'}};
if (!$nometa && $havemeta) {
$ret{$bin}->{'meta'} = 1;
}
}
}
$binariescachehits += @cacheold;
if (@downloadbins) {
$binariesdownload += @downloadbins;
$binariesdownloadsize += $downloadsizek;
if ($cachedir && $downloadsizek * 1024 * 100 > $cachesize) {
# reserve space
manage_cache($cachesize - $downloadsizek * 1024);
}
my @args;
push @args, "project=$projid";
push @args, "repository=$repoid";
push @args, "arch=$arch";
my $res;
$res = trygetbinariesproxy($server, $dir, \@downloadbins, \%bv, @args) if $getbinariesproxy;
eval {
$res ||= BSRPC::rpc({
'uri' => "$server/getbinaries",
'directory' => $dir,
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, @args, 'binaries='.join(',', @downloadbins));
};
# do not die if a (remote) project does not exist
return {} if !@cacheold && $@ && $@ =~ /^404 /;
die($@) if $@;
die("Error\n") unless ref($res) eq 'ARRAY';
my %havemeta;
for my $r (@$res) {
if ($r->{'name'} =~ /^(.*)\.(?:$binsufsre)$/) {
my $n = $1;
my @s = stat("$dir/$r->{'name'}");
die unless @s;
my $id = Build::queryhdrmd5("$dir/$r->{'name'}");
$r->{'hdrmd5'} = $id;
my $cacheid = Digest::MD5::md5_hex("$projid/$repoid/$arch/$id");
push @cachenew, [$cacheid, $s[7], "$dir/$r->{'name'}"];
$ret{$n} = $r;
} elsif ($r->{'name'} =~ /^(.*)\.meta$/) {
$havemeta{$1} = 1;
}
}
for (keys %havemeta) {
next unless $ret{$_};
$ret{$_}->{'meta'} = 1;
}
}
manage_cache($cachesize, \@cacheold, \@cachenew) if $cachedir;
if ($nometa) {
for (keys %ret) {
next unless $ret{$_}->{'meta'};
unlink("$dir/$_.meta");
delete $ret{$_}->{'meta'};
}
}
return \%ret;
}
# bring xarch to the back... (last one wins)
sub prpasort {
my ($xarch, @p) = @_;
my %h;
for my $prpap (@p) {
my ($projid, $repoid, $arch, $packid) = split('/', $prpap, 4);
$arch = "~$arch" if $arch eq $xarch;
$h{$prpap} = "$projid/$repoid/$arch/$packid";
}
return sort { $h{$a} cmp $h{$b} } @p;
}
# this specialized version of getbinaries only works for
# kiwi product builds
sub getbinaries_kiwiproduct {
my ($buildinfo, $dir, $srcdir, $kiwiorigins) = @_;
mkdir_p($dir);
# we need the Build package for queryhdrmd5
importbuild() unless defined &Build::queryhdrmd5;
# create list of prpaps
my @kdeps;
my %prpaps;
my %prpapackages;
my %packagebinaryversionlist;
my $nodbgpkgs = $buildinfo->{'nodbgpkgs'};
my $nosrcpkgs = $buildinfo->{'nosrcpkgs'};
for my $dep (@{$buildinfo->{'bdep'} || []}) {
if (!defined($dep->{'package'})) {
push @kdeps, $dep->{'name'};
next;
}
my $repoarch = $dep->{'repoarch'} || $buildinfo->{'arch'};
next if $repoarch eq 'src';
if (!$prpaps{"$dep->{'project'}/$dep->{'repository'}/$repoarch/$dep->{'package'}"}) {
push @{$prpapackages{"$dep->{'project'}/$dep->{'repository'}/$repoarch"}}, $dep->{'package'};
$prpaps{"$dep->{'project'}/$dep->{'repository'}/$repoarch/$dep->{'package'}"} = 1;
}
}
my %prp2server;
for (@{$buildinfo->{'path'} || []}) {
$prp2server{"$_->{'project'}/$_->{'repository'}"} = $_->{'server'};
}
mkdir_p($dir);
# fetch packages needed for product building
for my $repo (@{$buildinfo->{'syspath'} || $buildinfo->{'path'} || []}) {
last if !@kdeps;
my $repoarch = $buildinfo->{'arch'};
$repoarch = $BSConfig::localarch if $repoarch eq 'local' && $BSConfig::localarch;
my $server = $repo->{'server'} || $buildinfo->{'reposerver'};
my $got = getbinaries_cache($dir, $server, $repo->{'project'}, $repo->{'repository'}, $repoarch, 1, \@kdeps);
@kdeps = grep {!$got->{$_}} @kdeps;
}
die("getbinaries_kiwiproduct: missing packages: @kdeps\n") if @kdeps;
# all done for followup builds
return () if $buildinfo->{'followupfile'};
# now fetch all packages that go into the repos subdir
my %meta;
my $linklocal = $localkiwi && -d "$localkiwi/build" ? 1 : 0;
my %cachenew;
my @cacheold;
my $downloadsizek = 0;
my @prpaps = sort keys %prpaps;
if ($buildinfo->{'package'} =~ /-([^-]+)$/) {
@prpaps = prpasort($1, @prpaps);
}
my %binfilter;
my $binfilterprpa = '';
for my $prpap (@prpaps) {
my ($projid, $repoid, $arch, $packid) = split('/', $prpap, 4);
if ($binfilterprpa ne "$projid/$repoid/$arch") {
%binfilter = ();
for my $dep (@{$buildinfo->{'bdep'} || []}) {
next unless defined($dep->{'package'});
$binfilter{"$dep->{'package'}/$dep->{'name'}.$dep->{'arch'}"} = 1 if $dep->{'project'} eq $projid && $dep->{'repository'} eq $repoid && ($dep->{'repoarch'} || $dep->{'arch'}) eq $arch;
}
$binfilterprpa = "$projid/$repoid/$arch";
}
my $prpdir = "$projid/$repoid";
my $ddir = "$srcdir/repos/$prpdir";
mkdir_p($ddir);
my $res;
my $server = $prp2server{"$projid/$repoid"} || $buildinfo->{'reposerver'};
my %knownmd5;
if ($linklocal) {
$res = [];
for my $name (ls("$localkiwi/build/$projid/$repoid/$arch/$packid")) {
my $rarch;
if ($name =~ /^(?:::import::.*::)?(.*)-[^-]+-[^-]+\.([a-zA-Z][^\.\-]*)\.d?rpm$/) {
$rarch = $2 if $binfilter{"$packid/$1.$2"};
} elsif ($name =~ /appdata\.xml$/) {
$rarch = 'appdata';
}
next unless $rarch;
next if $nodbgpkgs && $name =~ /-(?:debuginfo|debugsource)-/;
next if $nosrcpkgs && ($rarch eq 'src' || $rarch eq 'nosrc');
mkdir_p("$ddir/$rarch") unless -d "$ddir/$rarch";
if (!link("$localkiwi/build/$projid/$repoid/$arch/$packid/$name", "$ddir/$rarch/$name")) {
# link fails if file already exists, rpc just overwrites
unlink("$ddir/$rarch/$name");
link_or_copy("$localkiwi/build/$projid/$repoid/$arch/$packid/$name", "$ddir/$rarch/$name") || die("link $localkiwi/build/$projid/$repoid/$arch/$packid/$name $ddir/$rarch/$name: $!\n");
}
$kiwiorigins->{"obs://$prpdir/$rarch/$name"} = $prpap;
push @$res, {'name' => "$rarch/$name"};
}
} else {
my $bvl;
if ($cachedir) {
my $pbvl = $packagebinaryversionlist{"$projid/$repoid/$arch"};
if (!$pbvl) {
eval {
if ($server ne $buildinfo->{'srcserver'}) {
$pbvl = BSRPC::rpc({
'uri' => "$server/getpackagebinaryversionlist",
'timeout' => $gettimeout,
}, $BSXML::packagebinaryversionlist, "project=$projid", "repository=$repoid", "arch=$arch", map {"package=$_"} @{$prpapackages{"$projid/$repoid/$arch"}});
} else {
$pbvl = BSRPC::rpc({
'uri' => "$server/build/$projid/$repoid/$arch",
'timeout' => $gettimeout,
}, $BSXML::packagebinaryversionlist, "view=binaryversions", map {"package=$_"} @{$prpapackages{"$projid/$repoid/$arch"}});
}
};
undef $pbvl if $@;
warn($@) if $@;
$pbvl = { map {$_->{'package'} => $_} @{$pbvl->{'binaryversionlist'} || []} } if $pbvl;
$pbvl ||= {};
$packagebinaryversionlist{"$projid/$repoid/$arch"} = $pbvl;
}
$bvl = $pbvl->{$packid};
if (!defined($bvl)) {
eval {
$bvl = BSRPC::rpc({
'uri' => "$server/build/$projid/$repoid/$arch/$packid",
'timeout' => $gettimeout,
}, $BSXML::binaryversionlist, 'view=binaryversions');
};
undef $bvl if $@;
warn($@) if $@;
}
}
my @good;
my @bad;
if ($bvl) {
for my $bv (@{$bvl->{'binary'} || []}) {
my $bin = $bv->{'name'};
if ($bin =~ /appdata\.xml$/) {
push @bad, $bv; # currently not in cache
$downloadsizek += $bv->{'sizek'};
next;
}
next unless $bin =~ /^(?:::import::.*::)?(.*)-[^-]+-[^-]+\.([a-zA-Z][^\.\-]*)\.d?rpm$/;
my $rarch = $2;
next unless $binfilter{"$packid/$1.$2"};
next if $nodbgpkgs && $bin =~ /-(?:debuginfo|debugsource)-/;
next if $nosrcpkgs && ($rarch eq 'src' || $rarch eq 'nosrc');
my $cacheid = Digest::MD5::md5_hex("$projid/$repoid/$arch/$bv->{'hdrmd5'}");
my $cachefile = "$cachedir/".substr($cacheid, 0, 2)."/$cacheid";
mkdir_p("$ddir/$rarch");
if (link_or_copy($cachefile, "$ddir/$rarch/$bin.new.rpm")) {
my @s = stat("$ddir/$rarch/$bin.new.rpm");
die unless @s;
my $leadsigmd5 = '';
my $id = Build::queryhdrmd5("$ddir/$rarch/$bin.new.rpm", \$leadsigmd5);
if ($id eq $bv->{'hdrmd5'} && (!$bv->{'leadsigmd5'} || $bv->{'leadsigmd5'} eq $leadsigmd5)) {
push @good, "$rarch/$bin";
push @cacheold, [$cacheid, $s[7]];
$knownmd5{"$rarch/$bin"} = $id;
} else {
unlink "$ddir/$rarch/$bin.new.rpm";
push @bad, $bv;
$downloadsizek += $bv->{'sizek'};
}
} else {
push @bad, $bv;
$downloadsizek += $bv->{'sizek'};
}
}
}
#print "(cache: ".@good." hits, ".@bad." misses)";
$binariesdownload += @bad;
$binariescachehits += @good;
if ($bvl && @bad) {
if ($cachedir && $downloadsizek * 1024 * 100 > $cachesize) {
# reserve space
manage_cache($cachesize - $downloadsizek * 1024, \@cacheold, [ values %cachenew ]);
@cacheold = ();
%cachenew = ();
$binariesdownloadsize += $downloadsizek;
$downloadsizek = 0;
}
eval {
my @args;
push @args, "view=cpio";
push @args, "noajax=1" if $server ne $buildinfo->{'srcserver'};
push @args, map {"binary=$_->{'name'}"} @bad;
BSRPC::rpc({
'uri' => "$server/build/$projid/$repoid/$arch/$packid",
'directory' => $ddir,
'timeout' => $gettimeout,
'map' => sub {
my ($param, $name) = @_;
my $rarch;
if ($name =~ /^(?:::import::.*::)?(.*)-[^-]+-[^-]+\.([a-zA-Z][^\.\-]*)\.d?rpm$/) {
$rarch = $2 if $binfilter{"$packid/$1.$2"};
} elsif ($name =~ /appdata\.xml$/) {
$rarch = 'appdata';
}
return undef unless $rarch;
mkdir_p("$param->{'directory'}/$rarch");
return "$rarch/$name.new.rpm";
},
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, @args);
};
if ($@) {
# delete everything as a file might be incomplete
for my $bv (@bad) {
my $bin = $bv->{'name'};
if ($bin =~ /-[^-]+-[^-]+\.([a-zA-Z][^\.\-]*)\.d?rpm$/) {
unlink("$ddir/$1/$bin.new.rpm");
} elsif ($bin =~ /appdata\.xml$/) {
unlink("$ddir/$arch/$bin.new.rpm");
}
}
}
for my $bv (splice @bad) {
my $bin = $bv->{'name'};
my $rarch;
if ($bin =~ /^(?:::import::.*::)?(.*)-[^-]+-[^-]+\.([a-zA-Z][^\.\-]*)\.d?rpm$/) {
$rarch = $2 if $binfilter{"$packid/$1.$2"};
} elsif ($bin =~ /appdata\.xml$/) {
$rarch = 'appdata';
}
next unless $rarch;
my @s = stat("$ddir/$rarch/$bin.new.rpm");
if (!@s) {
push @bad, $bv;
next;
}
if (!$bv->{'hdrmd5'}) {
push @good, "$rarch/$bin";
next;
}
my $leadsigmd5 = '';
my $id = Build::queryhdrmd5("$ddir/$rarch/$bin.new.rpm", \$leadsigmd5);
if ($id eq $bv->{'hdrmd5'} && (!$bv->{'leadsigmd5'} || $bv->{'leadsigmd5'} eq $leadsigmd5)) {
push @good, "$rarch/$bin";
my $cacheid = Digest::MD5::md5_hex("$projid/$repoid/$arch/$bv->{'hdrmd5'}");
$cachenew{"$ddir/$rarch/$bin"} = [$cacheid, $s[7], "$ddir/$rarch/$bin"];
$knownmd5{"$rarch/$bin"} = $id;
} else {
unlink "$ddir/$rarch/$bin.new.rpm";
push @bad, $bv;
}
}
#print "(still ".@bad." misses)";
}
if ($bvl && !@bad) {
for (@good) {
rename("$ddir/$_.new.rpm", "$ddir/$_") || die("rename $ddir/$_.new.rpm $ddir/$_: $!\n");
$kiwiorigins->{"obs://$prpdir/$_"} = $prpap;
}
$res = [ map {{'name' => $_}} @good ];
} else {
%knownmd5 = ();
for (@good) {
unlink("$ddir/$_.new.rpm");
delete $cachenew{"$ddir/$_"};
}
if ($bvl && $server ne $buildinfo->{'srcserver'}) {
# the bvl entry seems to be wrong. tell our server about that.
eval {
my @bargs = ("project=$projid", "repository=$repoid", "arch=$arch", "package=$packid");
push @bargs, "workerid=$workerid" if defined $workerid;
BSRPC::rpc({
'uri' => "$server/badpackagebinaryversionlist",
'timeout' => $gettimeout,
}, undef, @bargs);
};
warn($@) if $@;
}
my @args;
push @args, "view=cpio";
push @args, "noajax=1" if $server ne $buildinfo->{'srcserver'};
$res = BSRPC::rpc({
'uri' => "$server/build/$projid/$repoid/$arch/$packid",
'directory' => $ddir,
'timeout' => $gettimeout,
'map' => sub {
my ($param, $name) = @_;
my $rarch;
if ($name =~ /^(?:::import::.*::)?(.*)-[^-]+-[^-]+\.([a-zA-Z][^\.\-]*)\.d?rpm$/) {
$rarch = $2 if $binfilter{"$packid/$1.$2"};
} elsif ($name =~ /appdata\.xml$/) {
$rarch = 'appdata';
}
return undef unless $rarch;
return undef if $nodbgpkgs && $name =~ /-(?:debuginfo|debugsource)-/;
return undef if $nosrcpkgs && ($rarch eq 'src' || $rarch eq 'nosrc');
mkdir_p("$param->{'directory'}/$rarch");
$kiwiorigins->{"obs://$prpdir/$rarch/$name"} = $prpap;
return "$rarch/$name";
},
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, @args);
if ($cachedir) {
for my $f (@{$res || []}) {
my @s = stat("$ddir/$f->{'name'}");
next unless @s;
my $id = Build::queryhdrmd5("$ddir/$f->{'name'}");
next unless $id;
my $cacheid = Digest::MD5::md5_hex("$projid/$repoid/$arch/$id");
$cachenew{"$ddir/$f->{'name'}"} = [$cacheid, $s[7], "$ddir/$f->{'name'}"];
$knownmd5{$f->{'name'}} = $id;
}
#print "(put ".@cachenew." entries)";
}
}
}
die unless $res;
my @todometa;
my @todometa_f;
for my $f (@$res) {
my (undef, $name) = split('/', $f->{'name'}, 2);
next unless defined($name) && $name =~ /^(?:::import::.*::)?(.*)-[^-]+-[^-]+\.([a-zA-Z][^\.\-]*)\.d?rpm$/;
my ($n, $rarch) = ($1, $2);
my $id = $knownmd5{$f->{'name'}};
$id ||= Build::queryhdrmd5("$ddir/$f->{'name'}");
$id ||= 'deaddeaddeaddeaddeaddeaddeaddead';
$meta{"$prpap/$n.$rarch"} = $id;
}
}
$binariesdownloadsize += $downloadsizek;
manage_cache($cachesize, \@cacheold, [ values %cachenew ]) if $cachedir;
# create meta
my @meta;
for (sort keys %meta) {
push @meta, "$meta{$_} $_";
}
return @meta;
}
sub getpreinstallimage_metas {
my ($buildinfo, $dir, $metas, $img, $hdrmd5s) = @_;
my $prpa = "$buildinfo->{'project'}/$buildinfo->{'repository'}/$buildinfo->{'arch'}";
# find out which metas are needed for this image
my @bins;
my %havehdrmd5 = map {$_ => 1} @{$img->{'hdrmd5s'} || []};
for my $bin (sort(keys %$metas)) {
push @bins, $bin if $hdrmd5s->{$bin} && $havehdrmd5{$hdrmd5s->{$bin}};
}
my @todo;
if ($cachedir) {
# check the cache
for my $bin (@bins) {
my $bv = $metas->{$bin};
my $cacheid = Digest::MD5::md5_hex("$prpa/$bv->{'hdrmd5'}");
my $cachefile = "$cachedir/".substr($cacheid, 0, 2)."/$cacheid";
if (link_or_copy("$cachefile.meta", "$dir/$bin.meta")) {
local *F;
open(F, '<', "$dir/$bin.meta") || die;
my $ctx = Digest::MD5->new;
$ctx->addfile(*F);
close F;
next if $ctx->hexdigest() eq $bv->{'metamd5'};
unlink("$dir/$bin.meta");
}
push @todo, $bin;
}
} else {
@todo = @bins;
}
return 1 unless @todo;
# get missing ones for the repo server for this build
my @args;
push @args, "project=$buildinfo->{'project'}";
push @args, "repository=$buildinfo->{'repository'}";
push @args, "arch=$buildinfo->{'arch'}";
push @args, "metaonly=1";
my $repo = (grep {$_->{'project'} eq $buildinfo->{'project'} && $_->{'repository'} eq $buildinfo->{'repository'}} @{$buildinfo->{'path'} || []})[0] || {};
my $server = $repo->{'server'} || $buildinfo->{'reposerver'};
my $res;
$res = trygetbinariesproxy($server, $dir, \@todo, $metas, @args) if $getbinariesproxy;
eval {
$res ||= BSRPC::rpc({
'uri' => "$server/getbinaries",
'directory' => $dir,
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, @args, "binaries=".join(',', @todo));
die("Error\n") unless ref($res);
};
warn($@) if $@;
return undef unless $res;
my %havemeta;
for my $r (@$res) {
next unless $r->{'name'} =~ /^(.*)\.meta$/;
my $bin = $1;
my $bv = $metas->{$bin};
die("downloaded the wrong meta\n") unless $bv;
local *F;
next unless open(F, '<', "$dir/$bin.meta");
my $ctx = Digest::MD5->new;
$ctx->addfile(*F);
close F;
if ($ctx->hexdigest() eq $bv->{'metamd5'}) {
# matching meta!
$havemeta{$bin} = 1;
if ($cachedir) {
# put in cache so that we don't need to download it the next time
my $cacheid = Digest::MD5::md5_hex("$prpa/$bv->{'hdrmd5'}");
my $cachefile = "$cachedir/".substr($cacheid, 0, 2)."/$cacheid";
unlink("$cachefile.meta.$$");
if (link_or_copy("$dir/$bin.meta", "$cachefile.meta.$$")) {
rename("$cachefile.meta.$$", "$cachefile.meta") || die("rename $cachefile.meta.$$ $cachefile.meta: $!\n");
}
}
} else {
unlink("$dir/$bin.meta");
}
}
return (grep {!$havemeta{$_}} @todo) ? undef : 1;
}
sub getpreinstallimage {
my ($buildinfo, $dir, $todo, $bvls) = @_;
# get all bvls to calculate image data
my @bins = @$todo;
my %prpas; # server -> prpas on this server
my %hdrmd5s;
my %imageorigins;
my $projid = $buildinfo->{'project'};
my $repoid = $buildinfo->{'repository'};
my %metas;
for my $repo (@{$buildinfo->{'path'} || []}) {
my $server = $repo->{'server'} || $buildinfo->{'reposerver'};
my $nometa = $repo->{'project'} ne $projid || $repo->{'repository'} ne $repoid ? 1 : 0;
$nometa = 1 if $buildinfo->{'file'} eq '_preinstallimage';
my @args;
push @args, "project=$repo->{'project'}";
push @args, "repository=$repo->{'repository'}";
push @args, "arch=$buildinfo->{'arch'}";
push @args, "nometa" if $nometa;
push @args, "binaries=".join(',', @bins);
my $bvl;
eval {
$bvl = BSRPC::rpc({
'uri' => "$server/getbinaryversions",
'timeout' => $gettimeout,
}, $BSXML::binaryversionlist, @args);
};
warn($@) if $@;
last unless $bvl; # stop here
push @{$prpas{$server}}, "$repo->{'project'}/$repo->{'repository'}/$buildinfo->{'arch'}" if $server ne $buildinfo->{'srcserver'};
$bvls->{"$repo->{'project'}/$repo->{'repository'}/$buildinfo->{'arch'}"} = $bvl if $bvls;
my %bv;
for (@{$bvl->{'binary'} || []}) {
if ($_->{'error'}) {
$bv{$_->{'name'}} = $_;
} else {
next unless $_->{'name'} =~ /(.*)\.(?:$binsufsre)$/;
$bv{$1} = $_;
$metas{$1} = $_ if !$nometa && $_->{'metamd5'};
}
}
my @origin = ($repo->{'project'}, $repo->{'repository'}, $buildinfo->{'arch'});
for my $bin (@bins) {
my $bv = $bv{$bin} || {};
next if $bv->{'error'};
$hdrmd5s{$bin} = $bv->{'hdrmd5'};
$imageorigins{$bin} = \@origin if exists $hdrmd5s{$bin};
}
@bins = grep {!exists $hdrmd5s{$_}} @bins;
last unless @bins;
}
# wipe error packages from hdrmd5s
for (keys %hdrmd5s) {
delete $hdrmd5s{$_} unless defined $hdrmd5s{$_};
}
# wipe noinstall packages from hdrmd5s (buildengine case)
for (grep {$_->{'noinstall'}} @{$buildinfo->{'bdep'} || []}) {
delete $hdrmd5s{$_->{'name'}};
}
return undef unless %hdrmd5s && %prpas;
# ok, now check if there is an image on one of the repo servers
my $match = "\0" x 512;
for (values %hdrmd5s) {
vec($match, hex(substr($_, 0, 3)), 1) = 1;
}
#$match = unpack("H*", $match); # hexify
my @images;
for my $server (sort(keys %prpas)) {
my $serverimages;
eval {
$serverimages = BSRPC::rpc({
'uri' => "$server/getpreinstallimageinfos",
'request' => 'POST',
'data' => $match,
'headers' => [ 'Content-Type: application/octet-stream' ],
'timeout' => $gettimeout,
}, undef, "match=body", map {"prpa=$_"} @{$prpas{$server}});
};
warn($@) if $@;
if ($serverimages && substr($serverimages, 0, 4) eq 'pst0') {
$serverimages = BSUtil::fromstorable($serverimages, 2);
}
$serverimages = [] unless $serverimages && ref($serverimages);
$_->{'server'} = $server for @$serverimages;
push @images, @$serverimages;
}
# got all available images, now find a good one to use
my $imagefile;
my $imageinfo;
my %neededhdrmd5s;
$neededhdrmd5s{$_} = 1 for values %hdrmd5s;
while (@images) {
my $bestimgn = 2;
my $bestimg;
for my $img (@images) {
# check if it really fits!
next if @{$img->{'hdrmd5s'} || []} < $bestimgn;
next unless $img->{'sizek'} && $img->{'hdrmd5'};
next if grep {!$neededhdrmd5s{$_}} @{$img->{'hdrmd5s'} || []};
# ignore our own image
next if $img->{'prpa'} eq "$projid/$repoid/$buildinfo->{'arch'}" && $img->{'package'} eq $buildinfo->{'package'};
if ($buildinfo->{'file'} eq '_preinstallimage') {
# for building preinstall images we want at least one new package to avoid cycles
my %havehdrmd5 = map {$_ => 1} @{$img->{'hdrmd5s'} || []};
next unless grep {!$havehdrmd5{$_}} values %hdrmd5s;
}
$bestimg = $img;
$bestimgn = @{$img->{'hdrmd5s'} || []};
}
last unless $bestimg; # nothing fits
# try this one
my $ifile = "preinstallimage.$bestimg->{'file'}";
unlink("$dir/$ifile");
# check if the image is in the cache
my $cacheid;
my $cachefile;
if ($cachedir) {
$cacheid = Digest::MD5::md5_hex("$bestimg->{'prpa'}/$bestimg->{'hdrmd5'}");
$cachefile = "$cachedir/".substr($cacheid, 0, 2)."/$cacheid";
my $cachefilemeta = readstr("$cachefile.meta", 1) || '';
if ($cachefilemeta eq "$bestimg->{'hdrmd5'} :preinstallimage\n") {
if (link_or_copy($cachefile, "$dir/$ifile")) {
# re-check to make races unlikely
$cachefilemeta = readstr("$cachefile.meta", 1) || '';
if ($cachefilemeta eq "$bestimg->{'hdrmd5'} :preinstallimage\n") {
# looks good, use it!
my @s = stat("$dir/$ifile");
if (@s) {
# put entry on top
manage_cache($cachesize, [ [$cacheid, $s[7]] ], undef);
}
if (getpreinstallimage_metas($buildinfo, $dir, \%metas, $bestimg, \%hdrmd5s)) {
$imagefile = $ifile;
$imageinfo = $bestimg;
last;
}
}
}
unlink("$dir/$ifile");
}
}
# nope, download it
manage_cache($cachesize - $bestimg->{'sizek'} * 1024) if $cachedir; # make room
my $res;
eval {
$res = BSRPC::rpc({
'uri' => "$bestimg->{'server'}/build/$bestimg->{'prpa'}/$bestimg->{'path'}",
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::file_receiver,
'filename' => "$dir/$ifile",
});
};
if ($@) {
warn($@);
} elsif (-s "$dir/$ifile") {
my @s = stat(_);
if ($cachedir && @s) {
# put in cache (and fake meta)
writestr("$dir/$ifile.meta", undef, "$bestimg->{'hdrmd5'} :preinstallimage\n");
manage_cache($cachesize, undef, [ [$cacheid, $s[7], "$dir/$ifile"] ]);
unlink("$dir/$ifile.meta");
}
if (getpreinstallimage_metas($buildinfo, $dir, \%metas, $bestimg, \%hdrmd5s)) {
$imagefile = $ifile;
$imageinfo = $bestimg;
last;
}
}
unlink("$dir/$ifile");
# that did not turn out well, try another image
$bestimg->{'hdrmd5s'} = []; # disables this image
}
return undef unless $imagefile;
my %imagebins;
my %havehdrmd5 = map {$_ => 1} @{$imageinfo->{'hdrmd5s'} || []};
for my $bin (keys %hdrmd5s) {
$imagebins{$bin} = $hdrmd5s{$bin} if $havehdrmd5{$hdrmd5s{$bin}};
}
for my $bin (keys %metas) {
delete $metas{$bin} unless $imagebins{$bin}; # only return metas on the image
}
for my $bin (keys %imageorigins) {
delete $imageorigins{$bin} unless $imagebins{$bin}; # only return origins on the image
}
my $imagesource = $imageinfo->{'prpa'};
$imagesource =~ s/\/[^\/]*$//; # strip arch
$imagesource .= "/$imageinfo->{'package'}" if $imageinfo->{'package'};
$imagesource .= " [$imageinfo->{'hdrmd5'}]";
return $imagefile, \%imagebins, $imagesource, \%metas, \%imageorigins;
}
sub getbinaries_buildenv {
my ($buildinfo, $dir, $srcdir) = @_;
mkdir_p($dir);
importbuild() unless defined &Build::queryhdrmd5;
my @bdeps = grep {$_->{'name'} && $_->{'hdrmd5'}} @{$buildinfo->{'bdep'} || []};
die("binaries without hdrmd5 in buildenv\n") unless @bdeps == @{$buildinfo->{'bdep'} || []};
my %needed = map {+"$_->{'name'}.$_->{'hdrmd5'}" => []} @bdeps;;
my %needed_names = map {$_->{'name'} => 1} @bdeps;
my @needed_names = sort(keys %needed_names);
my $arch = $buildinfo->{'arch'};
for my $repo (@{$buildinfo->{'path'} || []}) {
my $server = $repo->{'server'} || $buildinfo->{'reposerver'};
my $projid = $repo->{'project'};
my $repoid = $repo->{'repository'};
my $pbvl;
eval {
if ($server ne $buildinfo->{'srcserver'}) {
$pbvl = BSRPC::rpc({
'uri' => "$server/getpackagebinaryversionlist",
'timeout' => $gettimeout,
}, $BSXML::packagebinaryversionlist, "project=$projid", "repository=$repoid", "arch=$arch", map {"package=$_"} @needed_names);
} else {
$pbvl = BSRPC::rpc({
'uri' => "$server/build/$projid/$repoid/$arch",
'timeout' => $gettimeout,
}, $BSXML::packagebinaryversionlist, "view=binaryversions", map {"package=$_"} @needed_names);
}
};
undef $pbvl if $@;
warn($@) if $@;
next unless $pbvl;
$pbvl = { map {$_->{'package'} => $_} @{$pbvl->{'binaryversionlist'} || []} };
for my $packid (sort keys %$pbvl) {
my $bvl = $pbvl->{$packid};
for (@{$bvl->{'binary'} || []}) {
next unless $_->{'name'} && $_->{'hdrmd5'};
next unless $_->{'name'} =~ /^(?:::import::.*::)?(.*)-[^-]+-[^-]+\.([a-zA-Z][^\.\-]*)\.rpm$/;
$_->{'repo'} = $repo;
$_->{'package'} = $packid;
$_->{'binary'} = $1;
$_->{'filename'} = $_->{'name'};
$_->{'name'} = "$1.rpm";
push @{$needed{"$1.$_->{'hdrmd5'}"}}, $_ if $needed{"$1.$_->{'hdrmd5'}"};
}
}
}
%needed_names = ();
$needed_names{$_->{'name'}} = 1 for grep {!@{$needed{"$_->{'name'}.$_->{'hdrmd5'}"}}} @bdeps;
@needed_names = sort(keys %needed_names);
if (@needed_names) {
# maybe we need to look at the full tree as well...
for my $repo (@{$buildinfo->{'path'} || []}) {
my $server = $repo->{'server'} || $buildinfo->{'reposerver'};
my @args;
push @args, "project=$repo->{'project'}";
push @args, "repository=$repo->{'repository'}";
push @args, "arch=$arch";
push @args, 'nometa';
push @args, "binaries=".join(',', @needed_names);
my $bvl;
eval {
$bvl = BSRPC::rpc({
'uri' => "$server/getbinaryversions",
'timeout' => $gettimeout,
}, $BSXML::binaryversionlist, @args);
};
undef $bvl if $@;
warn($@) if $@;
next unless $bvl;
for (@{$bvl->{'binary'} || []}) {
next unless $_->{'hdrmd5'};
next unless $_->{'name'} =~ /(.*)\.(?:$binsufsre)$/;
$_->{'repo'} = $repo;
$_->{'binary'} = $1;
push @{$needed{"$1.$_->{'hdrmd5'}"}}, $_ if $needed{"$1.$_->{'hdrmd5'}"};
}
}
}
# ok, all info is collected
my @missing = grep {!@{$needed{"$_->{'name'}.$_->{'hdrmd5'}"}}} @bdeps;
die("missing packages: ".join(', ', map {+"$_->{'name'}\@$_->{'hdrmd5'}"} @missing)."\n") if @missing;
my @cacheold;
my @cachenew;
# check the cache
if ($cachedir) {
for my $needed (sort keys %needed) {
for my $bv (@{$needed{$needed}}) {
my $repo = $bv->{'repo'};
my $projid = $repo->{'project'};
my $repoid = $repo->{'repository'};
my $cacheid = Digest::MD5::md5_hex("$projid/$repoid/$arch/$bv->{'hdrmd5'}");
my $cachefile = "$cachedir/".substr($cacheid, 0, 2)."/$cacheid";
if (link_or_copy($cachefile, "$dir/$bv->{'name'}")) {
my @s = stat("$dir/$bv->{'name'}");
die unless @s;
my $id = Build::queryhdrmd5("$dir/$bv->{'name'}");
if ($id eq $bv->{'hdrmd5'}) {
push @cacheold, [$cacheid, $s[7]];
delete $needed{$needed};
last;
}
} else {
unlink("$dir/$bv->{'name'}");
}
}
}
}
my $downloadsizek = 0;
for my $needed (keys %needed) {
$downloadsizek += $needed{$needed}->[0]->{'sizek'} || 0;
}
$binariescachehits += @cacheold;
$binariesdownload += keys %needed;
$binariesdownloadsize += $downloadsizek;
if ($cachedir && $downloadsizek * 1024 * 100 > $cachesize) {
# reserve space
manage_cache($cachesize - $downloadsizek * 1024);
}
# download... slowly...
for my $needed (sort keys %needed) {
for my $bv (@{$needed{$needed}}) {
my $repo = $bv->{'repo'};
my $server = $repo->{'server'} || $buildinfo->{'reposerver'};
my $projid = $repo->{'project'};
my $repoid = $repo->{'repository'};
my $param;
unlink("$dir/$bv->{'name'}");
if ($bv->{'package'}) {
$param = {
'uri' => "$server/build/$projid/$repoid/$arch/$bv->{'package'}/$bv->{'filename'}",
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::file_receiver,
'filename' => "$dir/$bv->{'name'}",
};
} else {
$param = {
'uri' => "$server/build/$projid/$repoid/$arch/_repository/$bv->{'name'}",
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::file_receiver,
'filename' => "$dir/$bv->{'name'}",
};
}
eval {
BSRPC::rpc($param);
};
if ($@) {
warn($@);
unlink("$dir/$bv->{'name'}");
next;
}
my @s = stat("$dir/$bv->{'name'}");
next unless @s;
my $id = Build::queryhdrmd5("$dir/$bv->{'name'}");
if ($id ne $bv->{'hdrmd5'}) {
unlink("$dir/$bv->{'name'}");
next;
}
my $cacheid = Digest::MD5::md5_hex("$projid/$repoid/$arch/$bv->{'hdrmd5'}");
push @cachenew, [$cacheid, $s[7], "$dir/$bv->{'name'}"];
delete $needed{$needed};
last;
}
die("download of $needed failed\n") if $needed{$needed};
}
manage_cache($cachesize, \@cacheold, \@cachenew) if $cachedir;
return (); # no meta
}
sub getbinaries {
my ($buildinfo, $dir, $srcdir, $preinstallimagedata) = @_;
return getbinaries_buildenv($buildinfo, $dir, $srcdir) if $buildinfo->{'hasbuildenv'};
mkdir_p($dir);
my $kiwimode;
$kiwimode = 1 if $buildinfo->{'file'} =~ /\.kiwi$/;
# we need the Build package for queryhdrmd5
importbuild() unless defined &Build::queryhdrmd5;
my @bdep = @{$buildinfo->{'bdep'} || []};
my %bdep_notmeta = map {$_->{'name'} => 1} grep {$_->{'notmeta'} && ($_->{'repoarch'} || '') ne 'src'} @bdep;
my %bdep_noinstall; # kiwi mode noinstall
if ($kiwimode) {
%bdep_noinstall = map {$_->{'name'} => 1} grep {$_->{'noinstall'} && ($_->{'repoarch'} || '') ne 'src'} @bdep;
}
@bdep = map {$_->{'name'}} grep {($_->{'repoarch'} || '') ne 'src'} @bdep;
my $outbdep = ($buildinfo->{'outbuildinfo'} || {})->{'bdep'};
my %done;
my @todo = @bdep;
die("no binaries needed for this package?\n") unless @todo;
my @meta;
my %meta;
my $packid = $buildinfo->{'package'};
my $projid = $buildinfo->{'project'};
my $repoid = $buildinfo->{'repository'};
if ($kiwimode && $buildinfo->{'syspath'}) {
# two path mode: fetch environment
@todo = grep {!$bdep_noinstall{$_}} @bdep;
for my $repo (@{$buildinfo->{'syspath'} || []}) {
last if !@todo;
my $server = $repo->{'server'} || $buildinfo->{'reposerver'};
my $got = getbinaries_cache($dir, $server, $repo->{'project'}, $repo->{'repository'}, $buildinfo->{'arch'}, 1, \@todo);
@todo = grep {!$got->{$_}} @todo;
}
die("getbinaries: missing packages: @todo\n") if @todo;
@todo = grep {$bdep_noinstall{$_} || !$bdep_notmeta{$_}} @bdep;
}
my %bvls; # bvl cache, the query is not free
my ($imagefile, $imagebins);
if (@todo && !$kiwimode && $preinstallimagedata && $Build::Features::preinstallimage) {
my ($imagesource, $imagemetas, $imageorigins);
($imagefile, $imagebins, $imagesource, $imagemetas, $imageorigins) = getpreinstallimage($buildinfo, $dir, \@todo, \%bvls);
if ($imagefile && $imagebins && $imagemetas) {
$preinstallimagedata->{'imagename'} = $imagefile;
$preinstallimagedata->{'imagebins'} = $imagebins;
$preinstallimagedata->{'imagesource'} = $imagesource;
$meta{$_} = 1 for keys %$imagemetas;
if ($outbdep) {
# record preinstallimage data
for (grep {$imagebins->{$_}} @todo) {
my $obdep = { 'name' => $_, 'hdrmd5' => $imagebins->{$_}, 'project' => $imageorigins->{$_}->[0], 'repository' => $imageorigins->{$_}->[1] };
push @$outbdep, $obdep;
}
}
# remove image packages from todo
@todo = grep {!$imagebins->{$_}} @todo;
}
}
$imagebins ||= {};
for my $repo (@{$buildinfo->{'path'} || []}) {
my $ddir = $dir;
if ($kiwimode) {
my $prpdir = "$repo->{'project'}/$repo->{'repository'}";
$ddir = "$srcdir/repos/$prpdir";
mkdir_p($ddir);
}
next if !@todo;
my $nometa = 0;
$nometa = 1 if $kiwimode || $repo->{'project'} ne $projid || $repo->{'repository'} ne $repoid;
$nometa = 1 if $buildinfo->{'file'} eq '_preinstallimage';
my $server = $repo->{'server'} || $buildinfo->{'reposerver'};
my $got = getbinaries_cache($ddir, $server, $repo->{'project'}, $repo->{'repository'}, $buildinfo->{'arch'}, $nometa, \@todo, $bvls{"$repo->{'project'}/$repo->{'repository'}/$buildinfo->{'arch'}"});
for (sort keys %$got) {
my $gotpkg = $got->{$_};
$done{$_} = $gotpkg->{'name'};
$meta{$_} = 1 if !$nometa && $gotpkg->{'meta'};
if ($outbdep) {
my $obdep = { 'name' => $_, 'project' => $repo->{'project'}, 'repository' => $repo->{'repository'} };
$obdep->{'hdrmd5'} = $gotpkg->{'hdrmd5'} if $gotpkg->{'hdrmd5'};
# fill epoch/version/release/arch?
push @$outbdep, $obdep;
}
}
@todo = grep {!$done{$_}} @todo;
if ($kiwimode) {
for my $n (keys %$got) {
my $f = $got->{$n};
if (!$bdep_notmeta{$n}) {
my $id = Build::queryhdrmd5("$ddir/$f->{'name'}") || "deaddeaddeaddeaddeaddeaddeaddead";
push @meta, "$id $repo->{'project'}/$repo->{'repository'}/$n";
}
if (!$buildinfo->{'syspath'} && !$bdep_noinstall{$n}) {
if (!-e "$dir/$f->{'name'}") {
link_or_copy("$ddir/$f->{'name'}", "$dir/$f->{'name'}") || die("link_or_copy $ddir/$f->{'name'} $dir/$f->{'name'}: $!\n");
}
}
}
}
}
die("getbinaries: missing packages: @todo\n") if @todo;
if (!$kiwimode) {
# generate meta data
# we carefully prune entries here to keep the memory usage down
# so that coolo's image builds work
# - do not prune entries that have one of our subpacks in the
# path as they are handled in a special way
# - use the same order as the code in BSBuild
my %mseen;
my @subp = @{$buildinfo->{'subpack'} || []};
my $subpackre = '';
for (@subp) {
$subpackre .= "|/\Q$_\E/";
}
if ($subpackre) {
$subpackre = substr($subpackre, 1);
$subpackre = qr/$subpackre/;
}
for my $dep (sort {"$a/" cmp "$b/"} map {$_->{'name'}} grep {!$_->{'notmeta'}} @{$buildinfo->{'bdep'} || []}) {
my $m;
$m = readstr("$dir/$dep.meta", 1) if $meta{$dep};
if (!$m) {
my $id = $imagebins->{$dep} || Build::queryhdrmd5("$dir/$done{$dep}") || "deaddeaddeaddeaddeaddeaddeaddead";
push @meta, "$id $dep";
} else {
chomp $m;
my @m = split("\n", $m);
# do not include our own build results
next if $m[0] =~ / \Q$packid\E$/s;
$m[0] =~ s/ .*/ $dep/;
push @meta, shift @m;
if ($subpackre && "/$dep/" =~ /$subpackre/) {
s/ / $dep\// for @m;
push @meta, @m;
} else {
for (@m) {
next if $mseen{$_};
my $x = $_;
s/ / $dep\//;
$mseen{$x} = 1 unless $subpackre && "$_/" =~ /$subpackre/;
push @meta, $_;
}
}
}
}
@meta = BSBuild::gen_meta(($buildinfo->{'verifymd5'} || $buildinfo->{'srcmd5'})." $packid", $buildinfo->{'subpack'} || [], @meta);
shift @meta; # strip srcinfo again
} else {
# simply sort the meta
@meta = sort {substr($a, 34) cmp substr($b, 34)} @meta;
}
return @meta;
}
sub getoldpackages {
my ($buildinfo, $odir) = @_;
# get old package build for compare and diffing tools
mkdir_p($odir) || die("mkdir_p $odir: $!\n");
my $res = BSRPC::rpc({
'uri' => "$buildinfo->{'reposerver'}/build/$buildinfo->{'project'}/$buildinfo->{'repository'}/$buildinfo->{'arch'}/$buildinfo->{'package'}",
'directory' => $odir,
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, 'view=cpio', 'noajax=1', 'noimport=1');
rmdir($odir); # if not needed
for my $file (@$res) {
$binariesdownload += 1;
$binariesdownloadsize += $file->{'size'} / 1024;
}
}
sub readbuildenv {
my ($buildinfo, $src) = @_;
my $bi = readxml("$src/_buildenv.$buildinfo->{'repository'}.$buildinfo->{'arch'}", $BSXML::buildinfo, 1);
$bi ||= readxml("$src/_buildenv", $BSXML::buildinfo, 1) unless $buildinfo->{'followupfile'};
return unless $bi;
die("buildenv does not work for kiwi builds\n") if $buildinfo->{'file'} =~ /\.kiwi$/;
# have a buildenv! patch stuff!
$buildinfo->{'hasbuildenv'} = 1;
# work around buildenv generation bug
$_->{'name'} =~ s/\.rpm$// for @{$bi->{'bdep'} || []};
for (qw{error versrel bcnt release bdep}) {
$buildinfo->{$_} = $bi->{$_} if defined $bi->{$_};
}
for ('versrel', 'bcnt', 'release') {
$buildinfo->{'outbuildinfo'}->{$_} = $bi->{$_} if $buildinfo->{'outbuildinfo'} && defined $bi->{$_};
}
if (!grep {$_->{'preinstall'}} @{$buildinfo->{'bdep'} || []}) {
# fixup preinstall/vminstall...
importbuild() unless defined &Build::queryhdrmd5;
my $server = $buildinfo->{'srcserver'} || $srcserver;
my $bconf = BSRPC::rpc("$server/getconfig", undef, "project=$buildinfo->{'project'}", "repository=$buildinfo->{'repository'}");
$bconf = Build::read_config($buildinfo->{'arch'}, [ split("\n", $bconf) ]);
my %pdeps = map {$_ => 1} Build::get_preinstalls($bconf);
my %vmdeps = map {$_ => 1} Build::get_vminstalls($bconf);
my %runscripts = map {$_ => 1} Build::get_runscripts($bconf);
for (@{$buildinfo->{'bdep'} || []}) {
$_->{'preinstall'} = 1 if $pdeps{$_->{'name'}};
$_->{'vminstall'} = 1 if $vmdeps{$_->{'name'}};
$_->{'runscripts'} = 1 if $runscripts{$_->{'name'}};
}
}
}
sub has_obsrepositories {
my ($repos) = @_;
for my $repo (@{$repos || []}) {
return 1 if ($repo->{'source'} || {})->{'path'} eq 'obsrepositories:/';
}
return 0;
}
sub patchproductkiwi {
my ($buildinfo, $kiwifile, $meta) = @_;
my $kiwi = readxml($kiwifile, $BSKiwiXML::kiwidesc);
die("no instsource section in kiwi file\n") unless $kiwi->{'instsource'};
my $repo_only;
my @vars;
my $tag_media;
for my $productvar (@{$kiwi->{'instsource'}->{'productoptions'}->{'productvar'} || []}) {
push @vars, $productvar;
$repo_only = 1 if $productvar->{'name'} eq 'REPO_ONLY' and $productvar->{'_content'} eq 'true';
if ($productvar->{'name'} eq 'MEDIUM_NAME') {
my $mediumbase = $productvar->{'_content'};
my $cicnt = $buildinfo->{'versrel'};
$cicnt =~ s/.*-//;
if ($cicnt eq '1') {
# simple case for standard productconverter case
$mediumbase .= sprintf("-Build%04d", $buildinfo->{'bcnt'} || 0);
} else {
# standard CI_CNT-BCNT writing
$mediumbase .= sprintf("-Build%s.%d", $cicnt, $buildinfo->{'bcnt'} || 0);
}
pop @vars;
push @vars, { 'name' => 'BUILD_ID', '_content' => $mediumbase };
push @vars, { 'name' => 'MEDIUM_NAME', '_content' => "$mediumbase-Media" };
} elsif ($productvar->{'name'} eq 'RUN_MEDIA_CHECK' && $productvar->{'_content'} eq 'true') {
$tag_media = 1;
}
}
$kiwi->{'instsource'}->{'productoptions'}->{'productvar'} = \@vars;
# expand obsrepositories:/ directive
if (has_obsrepositories($kiwi->{'instsource'}->{'instrepo'})) {
# fill in pathes from buildinfo instead
my @rp;
my $prio = 0;
for my $path (@{$buildinfo->{'path'} || []}) {
$prio = $prio + 1;
my $h = { 'source' => { 'path' => "obs://$path->{'project'}/$path->{'repository'}" } };
$h->{'priority'} = $prio;
$h->{'name'} = "obsrepositories_$prio";
push @rp, $h;
}
$kiwi->{'instsource'}->{'instrepo'} = \@rp;
}
# expand a "take all packages" for products
for my $repopackages (@{$kiwi->{'instsource'}->{'repopackages'} || []}) {
next unless grep {$_->{'name'} eq '*'} @{$repopackages->{'repopackage'} || []};
# hey, a substitute all modifier!
my @rp;
my %allpkgs;
for my $m (@$meta) {
# md5 proj/rep/arch/pack/bin.arch
my @s = split('/', $m);
next unless $s[-1] =~ /^(.*)\.([^\.]*)$/;
next if $2 eq 'src' || $2 eq 'nosrc';
$allpkgs{$1} ||= {};
$allpkgs{$1}->{$2} = 1;
}
for my $rp (@{$repopackages->{'repopackage'} || []}) {
if ($rp->{'name'} ne '*') {
push @rp, $rp;
next;
}
for my $pkg (sort keys %allpkgs) {
# exclude blind take of all debug packages. They will be taken
# automatically if a configured debug medium exists.
next if $pkg =~ /-debug(?:info|source)(?:-32bit|-64bit|-x86)?$/;
my @a;
for my $availableArch (sort keys %{$allpkgs{$pkg}}){
if ($availableArch eq 'noarch') {
push @a, sort(map { $_->{'ref'} } @{$kiwi->{'instsource'}->{'architectures'}->{'requiredarch'}});
} else {
push @a, $availableArch;
}
}
my %a = map {$_ => 1} @a;
push @rp, {'name' => $pkg, 'arch' => join(',', sort keys %a)};
}
}
$repopackages->{'repopackage'} = \@rp;
}
writexml($kiwifile, undef, $kiwi, $BSKiwiXML::kiwidesc);
}
sub xmlescape {
my ($d) = @_;
$d =~ s/&/&/sg;
$d =~ s/</</sg;
$d =~ s/>/>/sg;
$d =~ s/"/"/sg;
return $d;
}
# parse all .report file and create a channel file from them
sub createchannel {
my ($dir, $kiwiorigins) = @_;
my @reports = grep {/\.report$/} ls($dir);
my %channel;
my $hasunknown;
my $hasconflict = '';
my %channelnames;
for my $reportfile (@reports) {
my $report = readxml("$dir/$reportfile", $BSXML::report, 1);
next unless $report;
for my $bin (@{$report->{'binary'} || []}) {
# update missing entries
my $prpap = $kiwiorigins->{$bin->{'_content'}};
if (!$prpap) {
$prpap = 'UNKNOWN/UNKNOWN/UNKNOWN/UNKNOWN';
$hasunknown = 1;
}
my ($projid, $repoid, $arch, $packid) = split('/', $prpap, 4);
$bin->{'project'} = $projid;
$bin->{'repository'} = $repoid;
$bin->{'arch'} = $arch;
$bin->{'package'} = $packid;
# do not put src rpms into channel
next if $bin->{'binaryarch'} eq 'src' || $bin->{'binaryarch'} eq 'nosrc';
if ($channelnames{$bin->{'name'}} && $channelnames{$bin->{'name'}} ne "$projid/$repoid/$arch/$packid") {
$hasconflict .= "$bin->{'name'}: $channelnames{$bin->{'name'}} - $projid/$repoid/$arch/$packid\n";
}
$channelnames{$bin->{'name'}} = "$projid/$repoid/$arch/$packid";
$channel{"$projid/$repoid/$arch"}->{"$packid/$bin->{'name'}"} = $bin;
}
writexml("$dir/.$reportfile", "$dir/$reportfile", $report, $BSXML::report);
}
# sort helper
my @chansplits;
for my $prpa (keys %channel) {
my ($projid, $repoid, $arch) = split('/', $prpa, 3);
push @chansplits, {
'project' => $projid,
'repository' => $repoid,
'arch' => $arch,
'prpa' => $prpa,
};
}
@chansplits = sort {
$a->{'project'} cmp $b->{'project'} ||
$a->{'repository'} cmp $b->{'repository'} ||
$a->{'arch'} cmp $b->{'arch'}
} @chansplits;
# write the channel
# we directly write it so we can format a bit
my $c = '';
$c .= "\n<!-- WARNING: this file contains UNKNOWN entries -->\n\n" if $hasunknown;
$c .= "\n<!-- WARNING: this file contains the following conflicts:\n\n$hasconflict\n-->\n\n" if $hasconflict;
$c .= "<channel>\n";
for my $chansplit (@chansplits) {
$c .= " <binaries";
for (qw{project repository package arch name supportstatus}) {
$c .= " $_=\"".xmlescape($chansplit->{$_}).'"' if defined $chansplit->{$_};
}
$c .= ">\n";
my @bins = values(%{$channel{$chansplit->{'prpa'}}});
for my $bin (sort {
$a->{'project'} cmp $b->{'project'} ||
$a->{'repository'} cmp $b->{'repository'} ||
$a->{'arch'} cmp $b->{'arch'} ||
$a->{'package'} cmp $b->{'package'} ||
$a->{'name'} cmp $b->{'name'} ||
$a->{'_content'} cmp $b->{'_content'}
} @bins) {
$c .= " <binary";
my $x = length(" <binary") + 1;
my $first = 0;
for (qw{project repository package arch name supportstatus}) {
next unless defined($bin->{$_});
next if defined($chansplit->{$_}) && $chansplit->{$_} eq $bin->{$_};
if ($first == 1 && $x < 48) {
my $pad = ' ' x (48 - $x);
$c .= $pad;
$x += length($pad);
}
if ($first == 2 && $x < 88) {
my $pad = ' ' x (88 - $x);
$c .= $pad;
$x += length($pad);
}
my $str = " $_=\"".xmlescape($bin->{$_}).'"';
$c .= $str;
$x += length($str);
$first++;
}
$c .= "/>\n";
}
$c .= " </binaries>\n\n";
}
$c .= "</channel>\n";
writestr("$dir/._channel", "$dir/_channel", $c);
}
sub kiwiurlmapper {
my ($url) = @_;
return '_obsrepositories';
}
$Build::Kiwi::urlmapper = \&kiwiurlmapper;
sub dobuild {
my ($buildinfo) = @_;
my $projid = $buildinfo->{'project'};
my $packid = $buildinfo->{'package'};
my $repoid = $buildinfo->{'repository'};
my $arch = $buildinfo->{'arch'};
my $helperarch = $buildinfo->{'hostarch'} || $arch;
my $kiwimode;
$kiwimode = 'image' if $buildinfo->{'file'} =~ /\.kiwi$/;
$kiwimode = 'product' if $kiwimode && $buildinfo->{'imagetype'} && $buildinfo->{'imagetype'}->[0] eq 'product';
my $kiwiorigins = {};
my $stats = {};
my $deltamode;
$deltamode = 1 if $buildinfo->{'file'} eq '_delta';
my $followupmode = $buildinfo->{'followupfile'};
my $helper = '';
/^\Q$helperarch\E:(.*)$/ && ($helper = $1) for @{$BSCando::cando{$hostarch}};
my $starttime = time();
my @lt = localtime($starttime);
my $timestring = sprintf "%04d-%02d-%02d %02d:%02d:%02d", $lt[5] + 1900, $lt[4] + 1, @lt[3,2,1,0];
print "$timestring: building '$packid' for project '$projid' repository '$repoid' arch '$arch'";
print " using helper $helper" if $helper;
print "\n";
my $srcdir = "$buildroot/.build-srcdir";
my $pkgdir = "$buildroot/.pkgs";
my $oldpkgdir = "$buildroot/.build.oldpackages";
if ($vm_tmpfs_mode) {
my @targs;
# Note that the xen/kvm vmdisk_rootsize is in MB
push @targs, "mount", "-t", "tmpfs", "-osize=${vmdisk_rootsize}M", "none", $buildroot;
qsystem(@targs) && die("mount tmpfs failed: $!\n");
}
unlink("$buildroot/.build.meta");
rm_rf("$buildroot/.build.packages") if -d "$buildroot/.build.packages";
rm_rf($srcdir) if -d $srcdir;
# changed to cleandir so that pkgdir can be a symlink
BSUtil::cleandir($pkgdir) if -d $pkgdir;
rm_rf($oldpkgdir) if -d $oldpkgdir;
if (!$kiwimode && !$followupmode && !$deltamode) {
$buildinfo->{'outbuildinfo'} = {
'project' => $projid,
'package' => $packid,
'repository' => $repoid,
'arch' => $arch,
'srcmd5' => $buildinfo->{'srcmd5'},
'verifymd5' => $buildinfo->{'verifymd5'} || $buildinfo->{'srcmd5'},
'bdep' => [],
};
for ('versrel', 'bcnt', 'release') {
$buildinfo->{'outbuildinfo'}->{$_} = $buildinfo->{$_} if defined $buildinfo->{$_};
}
}
####### download phase
$binariesdownload = 0;
$binariescachehits = 0;
$binariesdownloadsize = 0;
my $downloadstarttime = time();
my @meta;
my $preinstallimagedata = {};
print "fetching sources, ";
mkdir($srcdir) || die("mkdir $srcdir: $!\n");
if ($deltamode) {
push @meta, getdeltasources($buildinfo, $srcdir);
print "packages, ";
getbinaries($buildinfo, $pkgdir, $srcdir, $preinstallimagedata);
undef $oldpkgdir;
my $spec = readstr("$statedir/worker/worker-deltagen.spec", 1) || readstr("worker-deltagen.spec");
writestr("$srcdir/worker-deltagen.spec", undef, $spec);
$buildinfo->{'file'} = 'worker-deltagen.spec';
} elsif ($followupmode) {
getfollowupsources($buildinfo, $srcdir);
getsslcert($buildinfo, $srcdir);
readbuildenv($buildinfo, $srcdir);
unlink("$srcdir/_buildenv.$buildinfo->{'repository'}.$buildinfo->{'arch'}");
print "packages, ";
if (($kiwimode || '') eq 'product') {
getbinaries_kiwiproduct($buildinfo, $pkgdir, $srcdir, {});
$kiwiorigins->{$_} = readstr("$srcdir/$_") for grep {$_ eq '_channel' || /\.report$/} ls($srcdir);
} else {
getbinaries($buildinfo, $pkgdir, $srcdir, $preinstallimagedata);
}
undef $oldpkgdir;
$buildinfo->{'rootforbuild'} = 1 if $buildinfo->{'file'} =~ /\.kiwi$/;
$buildinfo->{'file'} = $followupmode;
# recalc kiwimode as we changed the file
undef $kiwimode;
$kiwimode = 'image' if $buildinfo->{'file'} =~ /\.kiwi$/;
$kiwimode = 'product' if $kiwimode && $buildinfo->{'imagetype'} && $buildinfo->{'imagetype'}->[0] eq 'product';
@meta = split("\n", readstr("$srcdir/meta"));
} else {
my $needsslcert;
push @meta, getsources($buildinfo, $srcdir);
importbuild() unless defined &Build::queryhdrmd5;
if (!$kiwimode && -s "$srcdir/$buildinfo->{'file'}") {
# check for build markers
local *F;
if (open(F, '<', "$srcdir/$buildinfo->{'file'}")) {
while(<F>) {
chomp;
if (/^#\s*needsbinariesforbuild\s*$/s && $Build::Features::preinstallimage) {
undef $preinstallimagedata; # can't use an image, sorry
}
if (/^#\s*needssslcertforbuild\s*$/s) {
$needsslcert = 1;
}
}
close F;
}
}
getsslcert($buildinfo, $srcdir) if $needsslcert;
readbuildenv($buildinfo, $srcdir);
print "packages, ";
if (($kiwimode || '') eq 'product') {
push @meta, getbinaries_kiwiproduct($buildinfo, $pkgdir, $srcdir, $kiwiorigins);
} else {
push @meta, getbinaries($buildinfo, $pkgdir, $srcdir, $preinstallimagedata);
}
}
$preinstallimagedata ||= {};
undef $oldpkgdir if $buildinfo->{'nounchanged'};
writestr("$buildroot/.build.meta", undef, join("\n", @meta)."\n");
getoldpackages($buildinfo, $oldpkgdir) if $oldpkgdir && !$kiwimode && $buildinfo->{'file'} ne 'preinstallimage';
$stats->{'times'}->{'download'}->{'time'}->{'unit'} = "s";
$stats->{'times'}->{'download'}->{'time'}->{'_content'} = (time() - $downloadstarttime);
$stats->{'download'}->{'cachehits'} = $binariescachehits;
if ($binariesdownload) {
$stats->{'download'}->{'size'}->{'unit'} = 'k';
$stats->{'download'}->{'size'}->{'_content'} = $binariesdownloadsize;
$stats->{'download'}->{'binaries'} = $binariesdownload;
}
$stats->{'download'}->{'preinstallimage'} = $preinstallimagedata->{'imagename'} if $preinstallimagedata->{'imagename'};
####### end of download phase
my @configpath;
if ($kiwimode) {
@configpath = map {"path=$_->{'project'}/$_->{'repository'}"} @{$buildinfo->{'syspath'} || $buildinfo->{'path'} || []};
unshift @configpath, "path=$projid/$repoid" unless @configpath;
}
my $server = $buildinfo->{'srcserver'} || $srcserver;
my $config = BSRPC::rpc("$server/getconfig", undef, "project=$projid", "repository=$repoid", @configpath);
writestr("$buildroot/.build.config", undef, $config);
my $release = $buildinfo->{'release'};
#FIXME2.6: drop the following line
my $disturl = "obs://$BSConfig::obsname/$projid/$repoid/$buildinfo->{'srcmd5'}-$packid";
$disturl = $buildinfo->{'disturl'} if $buildinfo->{'disturl'};
my @args;
push @args, $helper if $helper;
# build rpmlist for build script
my @rpmlist;
my @bdep = @{$buildinfo->{'bdep'} || []};
my $imagebins = $preinstallimagedata->{'imagebins'} || {};
for my $bdep (@bdep) {
next if $bdep->{'package'} || (($bdep->{'repoarch'} || '') eq 'src');
next if $kiwimode && $bdep->{'noinstall'};
my $bin = $bdep->{'name'};
if ($imagebins->{$bin}) {
push @rpmlist, "$bin preinstallimage";
next;
}
for my $osuf (@binsufs, '__notfound') {
die("missing package: $bin\n") if $osuf eq '__notfound';
next unless -e "$pkgdir/$bin.$osuf";
push @rpmlist, "$bin $pkgdir/$bin.$osuf";
last;
}
}
push @rpmlist, "preinstallimage: $pkgdir/$preinstallimagedata->{'imagename'}" if $preinstallimagedata->{'imagename'};
push @rpmlist, "preinstallimagesource: $preinstallimagedata->{'imagesource'}" if $preinstallimagedata->{'imagesource'};
push @rpmlist, "localkiwi $localkiwi/localkiwi.rpm" if $localkiwi && -e "$localkiwi/localkiwi.rpm";
push @rpmlist, "preinstall: ".join(' ', map {$_->{'name'}} grep {$_->{'preinstall'}} @bdep);
push @rpmlist, "vminstall: ".join(' ', map {$_->{'name'}} grep {$_->{'vminstall'}} @bdep);
push @rpmlist, "runscripts: ".join(' ', map {$_->{'name'}} grep {$_->{'runscripts'}} @bdep);
if (!$kiwimode) {
push @rpmlist, "noinstall: ".join(' ', map {$_->{'name'}} grep {$_->{'noinstall'}} @bdep);
push @rpmlist, "installonly: ".join(' ', map {$_->{'name'}} grep {$_->{'installonly'}} @bdep);
}
writestr("$buildroot/.build.rpmlist", undef, join("\n", @rpmlist)."\n");
print "building...\n";
die("$buildinfo->{'error'}\n") if $buildinfo->{'error'};
if (($kiwimode || '') eq 'product') {
patchproductkiwi($buildinfo, "$srcdir/$buildinfo->{'file'}", \@meta);
}
push @args, "$statedir/build/build";
if ($vm =~ /(xen|kvm|zvm|emulator)/) {
mkdir("$buildroot/.mount") unless -d "$buildroot/.mount";
push @args, '--root', "$buildroot/.mount";
if ($vm =~ /emulator/) {
push @args, '--vm-type', 'emulator';
push @args, '--vm-disk', $vm_root;
push @args, '--emulator-script', $emulator_script if $emulator_script;
} else {
push @args, $vm, "$vm_root";
}
push @args, '--swap', "$vm_swap";
push @args, '--statistics';
push @args, '--vm-watchdog';
my $vmmemory = readstr("$buildroot/memory", 1);
$vmmemory = $vm_memory if $vm_memory;
push @args, '--memory', $vmmemory if $vmmemory;
push @args, '--vm-kernel', $vm_kernel if $vm_kernel;
push @args, '--vm-initrd', $vm_initrd if $vm_initrd;
push @args, '--vmdisk-rootsize', $vmdisk_rootsize if $vmdisk_rootsize;
push @args, '--vmdisk-swapsize', $vmdisk_swapsize if $vmdisk_swapsize;
push @args, '--vmdisk-filesystem', $vmdisk_filesystem if $vmdisk_filesystem;
# mount options require quoting due to arguments which might start with "-o ..."
push @args, '--vmdisk-mount-options', "\"$vmdisk_mount_options\"" if $vmdisk_mount_options;
push @args, '--vmdisk-clean'if $vmdisk_clean;
push @args, '--hugetlbfs', $hugetlbfs if $hugetlbfs;
push @args, '--vm-worker', $vm_worker_name if $vm_worker_name;
push @args, '--vm-worker-nr', $vm_worker_instance if $vm_worker_instance;
push @args, '--vm-enable-console' if $vm_enable_console;
} else {
push @args, '--root', $buildroot;
push @args, '--vm-type', $1 if $vm =~ /(lxc|docker)/;
}
push @args, '--clean';
push @args, '--changelog';
push @args, '--oldpackages', $oldpkgdir if $oldpkgdir && -d $oldpkgdir;
push @args, '--norootforbuild' unless $buildinfo->{'rootforbuild'} || ($BSConfig::norootexceptions && grep {"$projid/$packid" =~ /^$_$/} keys %$BSConfig::norootexceptions);
push @args, '--baselibs-internal';
push @args, '--lint';
push @args, '--dist', "$buildroot/.build.config";
push @args, '--rpmlist', "$buildroot/.build.rpmlist";
push @args, '--logfile', "$buildroot/.build.log";
push @args, '--release', "$release" if defined $release;
push @args, '--debug' if $buildinfo->{'debuginfo'};
push @args, '--arch', $arch;
push @args, '--jobs', $jobs if $jobs;
push @args, '--threads', $threads if $threads;
push @args, '--reason', "Building $packid for project '$projid' repository '$repoid' arch '$arch' srcmd5 '$buildinfo->{'srcmd5'}'";
push @args, '--disturl', $disturl;
push @args, '--linksources' if $localkiwi;
push @args, '--signdummy' if ($kiwimode || '') eq 'product' && -e "$statedir/build/signdummy";
push @args, "$srcdir/$buildinfo->{'file'}";
if ($kiwimode && $kiwimode ne 'product') {
# expand obsrepositories:/ directive for appliances
my $kiwi = Build::parse(undef, "$srcdir/$buildinfo->{'file'}") || {};
if (grep {$_->{'project'} eq '_obsrepositories'} @{$kiwi->{'path'} || []}) {
push @args, '--kiwi-parameter', '--ignore-repos';
my $prio = 0;
for my $prjpath (@{$buildinfo->{'path'} || []}) {
$prio = $prio + 1;
my $repo ="repos/$prjpath->{'project'}/$prjpath->{'repository'}";
$repo =~ s/:/:\//g; # XXX: no longer needed?
push @args, '--kiwi-parameter', '--add-repo', '--kiwi-parameter', $repo;
push @args, '--kiwi-parameter', '--add-repotype', '--kiwi-parameter', 'rpm-md';
push @args, '--kiwi-parameter', '--add-repoprio', '--kiwi-parameter', $prio; # different prio for smart?
}
}
}
qsystem(@args);
print "\n";
print "$timestring: build finished '$packid' for project '$projid' repository '$repoid' arch '$arch'";
print " using helper $helper" if $helper;
print "\n";
my $ret = $?;
if ($ret == 512) { # system exit code 2 maps to 2 * 256
if (($buildinfo->{'reason'} || '') eq "rebuild counter sync") {
$ret = 0;
} else {
return 2;
}
} elsif ($ret == 768) {
return 3;
}
if ($ret) {
return 1;
}
if (! -s "$buildroot/.build.log") {
print "build succeeded, but no logfile?\n";
return 1;
}
if ($vm =~ /(xen|kvm|zvm|emulator)/) {
rm_rf("$buildroot/.build.packages");
# move directory with extracted build results
if(!rename("$buildroot/.mount/.build.packages", "$buildroot/.build.packages")) {
print "final rename failed: $!";
return 1;
}
# XXX: extracted cpio is flat but code below expects those directories...
symlink('.', "$buildroot/.build.packages/SRPMS");
symlink('.', "$buildroot/.build.packages/DEBS");
symlink('.', "$buildroot/.build.packages/KIWI");
# convert build statistics into xml
if( -e "$buildroot/.build.packages/OTHER/_statistics") {
my $iosectors = 0;
my $iorequests = 0;
open(FILE, "<", "$buildroot/.build.packages/OTHER/_statistics") || die;
while(<FILE>) {
chomp;
my ($key, $value) = split( ": ", $_ );
if ($key eq "MAX_mb_used_on_disk" ){
$stats->{'disk'}->{'usage'}->{'size'}->{'unit'} = "M";
$stats->{'disk'}->{'usage'}->{'size'}->{'_content'} = $value;
} elsif ($key eq "MAX_mb_used_memory" ){
$stats->{'memory'}->{'usage'}->{'size'}->{'unit'} = "M";
$stats->{'memory'}->{'usage'}->{'size'}->{'_content'} = $value;
} elsif ($key eq "IO_requests_read" || $key eq "IO_requests_write" ){
$iorequests += $value;
$stats->{'disk'}->{'usage'}->{'io_requests'} = $iorequests;
} elsif ($key eq "IO_sectors_read" || $key eq "IO_sectors_write" ){
$iosectors += $value;
$stats->{'disk'}->{'usage'}->{'io_sectors'} = $iosectors;
} elsif ($key eq "TIME_preinstall" ){
$stats->{'times'}->{'preinstall'}->{'time'}->{'unit'} = "s";
$stats->{'times'}->{'preinstall'}->{'time'}->{'_content'} = $value;
} elsif ($key eq "TIME_install" ){
$stats->{'times'}->{'install'}->{'time'}->{'unit'} = "s";
$stats->{'times'}->{'install'}->{'time'}->{'_content'} = $value;
} elsif ($key eq "TIME_main_build" ){
$stats->{'times'}->{'main'}->{'time'}->{'unit'} = "s";
$stats->{'times'}->{'main'}->{'time'}->{'_content'} = $value;
}
}
close FILE;
}
}
$stats->{'times'}->{'total'}->{'time'}->{'unit'} = "s";
$stats->{'times'}->{'total'}->{'time'}->{'_content'} = (time() - $starttime);
writexml("$buildroot/.build.packages/OTHER/_statistics.new", "$buildroot/.build.packages/OTHER/_statistics", $stats, $BSXML::buildstatistics);
if ($buildinfo->{'outbuildinfo'}) {
writexml("$buildroot/.build.packages/OTHER/_buildenv", undef, $buildinfo->{'outbuildinfo'}, $BSXML::buildinfo);
}
# as a special service we also create a channel file from
# the report files
if (($kiwimode || '') eq 'product' && !$followupmode) {
createchannel("$buildroot/.build.packages/OTHER", $kiwiorigins);
}
if ($followupmode && %$kiwiorigins) {
writestr("$buildroot/.build.packages/OTHER/$_", undef, $kiwiorigins->{$_}) for keys %$kiwiorigins;
}
return 0;
}
sub buildkiwitree {
my ($destfile, @dirs) = @_;
my %tree;
my @todo;
while (@dirs) {
my $dir = shift @dirs;
my $sdir = shift @dirs;
$sdir = '' unless defined $sdir;
next unless -d $dir;
$tree{$1} = "d ". BSRPC::urlencode($1) if $sdir =~ /^(.+)\/$/;
push @todo, $dir, $sdir;
}
while (@todo) {
my $dir = shift @todo;
my $sdir = shift @todo;
for my $f (sort(ls($dir))) {
my $sf = "$sdir$f";
$f = "$dir/$f";
next if $tree{$sf};
if (-l $f) {
my $sl = readlink($f);
die("readlink $f: $!\n") unless defined $sl;
$tree{$sf} = "l ". BSRPC::urlencode($sf) . " " . BSRPC::urlencode($sl);
} elsif (-d $f) {
$tree{$sf} = "d ". BSRPC::urlencode($sf);
push @todo, $f, "$sf/";
} elsif (-f $f) {
my $leadsigmd5 = '';
Build::queryhdrmd5($f, \$leadsigmd5) if $f =~ /\.rpm$/;
if ($leadsigmd5) {
$tree{$sf} = "f ". BSRPC::urlencode($sf) . " " . $leadsigmd5;
} else {
$tree{$sf} = "f ". BSRPC::urlencode($sf);
}
}
}
}
local *F;
open(F, '>', $destfile) || die("$destfile: $!\n");
for (sort keys %tree) {
print F "$tree{$_}\n";
}
close(F) || die("close $destfile: $!\n");
}
if (defined($oneshot)) {
$oneshot = time() + $oneshot;
}
# better safe than sorry...
chdir($statedir) || die("$statedir: $!\n");
BSServer::deamonize(@ARGV) unless $oneshot;
$SIG{'PIPE'} = 'IGNORE';
# calculate code meta md5
my $workercode = codemd5('worker');
my $buildcode = codemd5('build');
$| = 1;
print "starting worker $workercode build $buildcode\n";
open(RUNLOCK, '>>', "$statedir/lock") || die("$statedir/lock: $!");
flock(RUNLOCK, LOCK_EX | LOCK_NB) || die("worker is already running on $statedir!\n");
utime undef, undef, "$statedir/lock";
# we always start idle
lockstate();
unlink("$statedir/job");
unlink("$buildroot/.build.log");
commitstate({'state' => 'idle'});
# start server process...
if ($port) {
BSServer::serveropen($port);
} else {
BSServer::serveropen(\$port);
}
mkdir($buildroot) unless -d $buildroot;
send_state('idle', $port, $hostarch);
hardstatus('idle');
my $idlecnt = 0;
my $rekillcnt = 0;
my $conf = {
'timeout' => 10,
};
my $req;
while (!($req = BSServer::server($conf))) {
# timeout handler, called every 10 seconds
my $state = readxml("$statedir/state", $BSXML::workerstate, 1);
next unless $state;
if ($state->{'state'} eq 'idle') {
if ($oneshot && time() > $oneshot) {
send_state('exit', $port, $hostarch);
print "exiting.\n";
exit(0);
}
$idlecnt++;
if ($idlecnt % 30 == 0) {
# send idle message every 5 minutes in case the server was down
$idlecnt = 0;
send_state('idle', $port, $hostarch) if $state->{'state'} eq 'idle';
}
} else {
$idlecnt = 0;
}
if ($state->{'state'} eq 'exit') {
$state = lockstate();
if ($state->{'state'} eq 'exit') {
close RUNLOCK;
$state = {'state' => 'idle'};
commitstate($state);
send_state('exit', $port, $hostarch);
exit(0);
}
unlockstate();
}
if ($state->{'state'} eq 'rebooting') {
chdir("$statedir/worker") || die("$statedir/worker: $!");
close RUNLOCK;
exec("./bs_worker", @saveargv);
die("$statedir/worker/bs_worker: $!\n"); # oops
}
if ($state->{'state'} eq 'killed' || $state->{'state'} eq 'discarded') {
$rekillcnt++;
if ($state->{'state'} eq 'discarded' && $state->{'nextstate'}) {
# signal early that we're going down
send_state('exit', $port, $hostarch);
$rekillcnt = 12;
}
if ($rekillcnt % 12 == 0) {
# re-kill after 2 minutes, maybe build is stuck somewhere
$rekillcnt = 0;
$state = lockstate();
if ($state->{'state'} eq 'killed' || $state->{'state'} eq 'discarded') {
kill_job();
}
unlockstate();
}
} else {
$rekillcnt = 0;
}
if ($state->{'state'} eq 'killed' || $state->{'state'} eq 'discarded' || $state->{'state'} eq 'building') {
if ($state->{'pid'} && !kill(0, $state->{'pid'})) {
$state = lockstate();
if ($state->{'state'} eq 'killed' || $state->{'state'} eq 'discarded' || $state->{'state'} eq 'building') {
if ($state->{'pid'} && !kill(0, $state->{'pid'})) {
# our worker process is gone, should never happen...
kill_job(); # just in case...
$state = {'state' => $state->{'nextstate'} || 'idle'};
commitstate($state);
next;
}
}
unlockstate();
}
}
next unless $state->{'state'} eq 'building';
my $locked = -1;
while ($locked++ < 1) {
$state = lockstate() if $locked == 1;
last if $state->{'state'} ne 'building';
my $ct = time();
my @s = stat("$buildroot/.build.log");
next unless @s;
if ($s[7] > $buildlog_maxsize) {
next unless $locked;
if (!kill_job()) {
warn("could not kill job\n");
last;
}
trunc_logfile("$buildroot/.build.log");
$state->{'state'} = 'killed';
commitstate($state);
$locked = 0;
} elsif ($ct - $s[9] > $buildlog_maxidle) {
next unless $locked;
if (!kill_job()) {
warn("could not kill job\n");
last;
}
local *F;
if (open(F, '>>', "$buildroot/.build.log")) {
print F "\n\nJob seems to be stuck here, killed. (after $buildlog_maxidle seconds of inactivity)\n";
close F;
}
$state->{'state'} = 'killed';
commitstate($state);
$locked = 0;
} elsif ($s[7] > 250) {
my $tail = tail_logfile("$buildroot/.build.log", 250) || '';
my $kill_job;
$kill_job = "kvm memory page bug" if $tail =~ /BUG: unable to handle kernel NULL pointer dereference at/;
$kill_job = "kvm spinnlock bug" if $tail =~ /INFO: rcu_sched self-detected stall on CPU/;
$kill_job = "xen soft lockup" if $tail =~ /BUG: soft lockup - CPU#\d+ stuck for/;
$kill_job = "stuck in SLOF" if $tail =~ /\( \d+ \) Data Storage Exception/;
if ($kill_job) {
next unless $locked;
print "detected $kill_job, discarding job\n";
if (!kill_job()) {
warn("could not kill job\n");
last;
}
$state->{'state'} = 'discarded';
commitstate($state);
$locked = 0;
}
}
last;
}
unlockstate() if $locked;
}
close RUNLOCK;
BSServer::readrequest($req);
my $path = $req->{'path'};
my $multies;
$multies = {'file' => undef} if $path eq '/kiwitree';
my $cgi = BSDispatch::parse_cgi($req, $multies);
if ($path eq '/info') {
# check state?
my $state = readxml("$statedir/state", $BSXML::workerstate, 1);
if ($cgi->{'jobid'}) {
die("not building a job\n") if $state->{'state'} ne 'building';
die("building a different job\n") if $cgi->{'jobid'} ne $state->{'jobid'};
}
my $info;
if ($state->{'state'} eq 'building') {
$info = readstr("$statedir/job");
} else {
$info = "<buildinfo>\n <error>".$state->{'state'}."</error>\n</buildinfo>\n";
}
BSServer::reply($info, 'Content-Type: text/xml');
exit(0);
} elsif ($path eq '/worker') {
my $state = readxml("$statedir/state", $BSXML::workerstate, 1);
if ($cgi->{'jobid'}) {
die("not building a job\n") if $state->{'state'} ne 'building';
die("building a different job\n") if $cgi->{'jobid'} ne $state->{'jobid'};
}
my $info = {};
$info->{'hostarch'} = $hostarch;
$info->{'port'} = $port;
$info->{'workerid'} = $workerid if defined $workerid;
# $info->{'job'} = $state->{'job'};
# $info->{'arch'} = $state->{'arch'};
$info = XMLout($BSXML::worker, $info);
BSServer::reply($info, 'Content-Type: text/xml');
exit(0);
} elsif ($path eq '/kiwitree') {
my $state = readxml("$statedir/state", $BSXML::workerstate, 1);
if ($cgi->{'jobid'}) {
die("not building a job\n") if $state->{'state'} ne 'building';
die("building a different job\n") if $cgi->{'jobid'} ne $state->{'jobid'};
}
my $kiwitreefile = "$buildroot/.build.packages/.kiwitree";
die("kiwitreefile does not exist\n") unless -e $kiwitreefile;
my @send;
my %ok;
for my $f (@{$cgi->{'file'} || []}) {
die("bad file $f\n") if "/$f/" =~ m!//!s; # no dup slashes
die("bad file $f\n") if "/$f/" =~ m!/\.\.?/!s; # no . or ..
my @f = split('/', $f);
die("bad file $f\n") unless @f;
my $c = pop(@f);
$f = '';
for (@f) {
$f .= $_;
die("bad file $f\n") if !$ok{$f} && (-l "$buildroot/.build.packages/KIWI/$f" || ! -d _);
$ok{$f} = 1;
$f .= '/';
}
$f .= $c;
die("bad file $f\n") if -l "$buildroot/.build.packages/KIWI/$f" || ! -f _;
push @send, { 'name' => $f, 'filename' => "$buildroot/.build.packages/KIWI/$f" };
}
BSServer::reply_cpio(\@send);
exit(0);
} elsif ($path eq '/logfile') {
my $state = readxml("$statedir/state", $BSXML::workerstate, 1);
die("not building\n") if $state->{'state'} ne 'building';
die("building a different job\n") if $cgi->{'jobid'} && $cgi->{'jobid'} ne $state->{'jobid'};
if ($cgi->{'view'} && $cgi->{'view'} eq 'entry') {
my @s = stat("$buildroot/.build.log");
die("$buildroot/.build.log: $!\n") unless @s;
my $xml = "<directory>\n <entry name=\"_log\" size=\"$s[7]\" mtime=\"$s[9]\" />\n</directory>\n";
BSServer::reply($xml, 'Content-Type: text/xml');
exit(0);
}
stream_logfile($cgi->{'nostream'}, $cgi->{'start'}, $cgi->{'end'});
exit(0);
} elsif ($path eq '/kill' || $path eq '/discard') {
my $state = lockstate();
die("not building\n") if $state->{'state'} ne 'building';
die("building a different job\n") if $cgi->{'jobid'} && $cgi->{'jobid'} ne $state->{'jobid'};
if (!kill_job()) {
die("could not kill job\n");
}
local *F;
if (open(F, '>>', "$buildroot/.build.log")) {
if ($path eq '/kill') {
print F "\n\nKilled Job\n";
} else {
print F "\n\nDiscarded Job\n";
}
close F;
}
if ($path eq '/kill') {
$state->{'state'} = 'killed';
commitstate($state);
BSServer::reply("<status=\"ok\" />\n", 'Content-Type: text/xml');
} else {
$state->{'state'} = 'discarded';
commitstate($state);
BSServer::reply("<status=\"ok\" />\n", 'Content-Type: text/xml');
}
exit(0);
} elsif ($path ne '/build' || $req->{'action'} ne 'PUT') {
die("unknown request: $path\n");
}
# Check for XEN daemon database leak
if ($vm =~ /xen/ && $xenstore_maxsize && 0 + (-s '/var/lib/xenstored/tdb') > $xenstore_maxsize) {
die("xenstore too big:".(-s '/var/lib/xenstored/tdb')."\n");
}
unlink("job.new.$$");
BSServer::read_file("job.new.$$");
my $state = lockstate();
die("I am not idle!\n") unless $state->{'state'} eq 'idle';
if ($cgi->{'workercode'} && $cgi->{'port'} && $cgi->{'workercode'} ne $workercode && !$noworkercheck) {
my $peer = "$req->{'peer'}:$cgi->{'port'}";
$workercode = getcode('worker', "http://$peer/getworkercode");
die("could not update worker code\n") unless $workercode;
$state->{'state'} = 'rebooting';
print "activating new worker code $workercode\n";
commitstate($state);
hardstatus('rebooting');
die("rebooting...\n");
}
my $infoxml = readstr("job.new.$$");
die("bad job xml data\n") unless $infoxml =~ /<.*?>/s;
my $buildinfo = XMLin($BSXML::buildinfo, $infoxml);
my $jobid = $cgi->{'jobid'};
$jobid ||= Digest::MD5::md5_hex($infoxml);
$buildinfo->{'jobid'} = $jobid;
# old buildinfos missed some entries
if (@{$buildinfo->{'path'} || []}) {
$buildinfo->{'project'} ||= $buildinfo->{'path'}->[0]->{'project'};
$buildinfo->{'repository'} ||= $buildinfo->{'path'}->[0]->{'repository'};
$buildinfo->{'reposerver'} ||= $buildinfo->{'path'}->[0]->{'server'};
}
if ($localkiwi) {
# make sure this is the right job for us
my $jobname = ($buildinfo->{'arch'} || '<unknown>') . '/' . ($buildinfo->{'job'} || '<unknown>');
die("bad job: $jobname: not a kiwi job\n") unless $buildinfo->{'file'} =~ /\.kiwi$/;
die("bad job: $jobname: not a kiwi product job\n") unless $buildinfo->{'imagetype'} && $buildinfo->{'imagetype'}->[0] eq 'product';
}
$buildcode = codemd5('build');
if (!$nobuildcodecheck && $cgi->{'buildcode'} && $cgi->{'port'} && $cgi->{'buildcode'} ne $buildcode) {
print "fetching new buildcode $cgi->{'buildcode'}, mine was $buildcode\n";
my $peer = "$req->{'peer'}:$cgi->{'port'}";
$buildcode = getcode('build', "http://$peer/getbuildcode");
die("could not update build code\n") unless $buildcode;
}
rename("job.new.$$", 'job') || die("rename job.new.$$ job: $!\n");
if ($hostcheck) {
my $server = $buildinfo->{'srcserver'} || $srcserver;
if (system($hostcheck, '--srcserver', $server, "$statedir/job", 'precheck', $buildroot)) {
my $res = ($? >> 8) || 1;
unlink('job');
die("400 cannot build this repository\n") if $res == 2;
die("400 cannot build anything\n") if $res == 3;
die("400 cannot build this package\n");
}
}
if (!$testmode && $buildinfo->{'masterdispatched'}) {
$buildinfo->{'jobid'} = $jobid = $buildinfo->{'masterdispatched'};
eval {
send_dispatched($buildinfo->{'reposerver'}, $port, $hostarch, $buildinfo);
};
if ($@) {
unlink('job');
if ($@ =~ /no such job/ || $@ =~ /wrong job/) {
# not an error for the master dispatcher
BSServer::reply("<status code=\"ok\">\n <details>better luck next time...</details>\n</status>\n", 'Content-Type: text/xml');
delete $SIG{'__DIE__'};
}
die($@);
}
}
if ($testmode) {
BSServer::reply("<status code=\"failed\">\n <details>testmode activated</details>\n</status>\n", 'Status: 400 Testmode', 'Content-Type: text/xml');
} else {
BSServer::reply("<status code=\"ok\">\n <details>so much work, so little time...</details>\n</status>\n", 'Content-Type: text/xml');
}
print "got job, run build...\n";
delete $SIG{'__DIE__'};
unlink("$buildroot/.build.meta");
unlink("$buildroot/.build.packages");
unlink("$buildroot/.build.log");
writestr("$buildroot/.build.log", undef, '');
$state->{'state'} = 'building';
$state->{'jobid'} = $jobid;
$state->{'pid'} = $$;
commitstate($state);
hardstatus("$buildinfo->{'arch'} $buildinfo->{'package'}");
send_state('building', $port, $hostarch, $buildinfo->{'reposerver'});
my $ex;
eval {
$ex = dobuild($buildinfo);
};
if ($@) {
local *F;
if (open(F, '>>', "$buildroot/.build.log")) {
print F $@;
close(F);
}
print "$@";
$ex = 1;
# mark as bad build host if Build.pm could not be imported
$ex = 3 unless defined &Build::queryhdrmd5;
$ex = 3 if ( $@ =~ /^500 / );
}
if ($buildinfo->{'followupfile'}) {
my $buildsrcdir = "$buildroot/.build.packages/SOURCES";
$buildsrcdir = "$buildroot/.build-srcdir" if $vm =~ /(xen|kvm|emulator)/;
# if it was a follow up build, prepend old logfile
if (-s "$buildsrcdir/logfile") {
local *F;
local *T;
if (open(F, '<', "$buildroot/.build.log")) {
if (open(T, '>>', "$buildsrcdir/logfile")) {
my $buf;
syswrite(T, $buf) while sysread(F, $buf, 8192);
close T;
rename("$buildsrcdir/logfile", "$buildroot/.build.log");
}
close F;
}
}
if (-d "$buildroot/.build.packages/OTHER") {
for my $f ('rpmlint.log', '_statistics', '_buildenv') {
next unless -s "$buildsrcdir/$f";
BSUtil::cp("$buildsrcdir/$f", "$buildroot/.build.packages/OTHER/$f");
}
}
# delete follow-up srcrpm
for my $srcrpm (ls("$buildroot/.build.packages/SRPMS")) {
unlink("$buildroot/.build.packages/SRPMS/$srcrpm") unless -e "$buildsrcdir/$srcrpm";
}
}
# build is done, send back result
$state = lockstate();
if ($state->{'state'} eq 'discarded') {
# our poor job is no longer needed
print "build discarded...\n";
unlink("$buildroot/.build.log");
unlink("$buildroot/job");
cleanup_job();
if ($state->{'nextstate'}) {
$state = {'state' => $state->{'nextstate'}};
commitstate($state);
exit(0);
}
$state = {'state' => 'idle'};
commitstate($state);
hardstatus('idle');
exit(0) if $oneshot && time() > $oneshot;
send_state('idle', $port, $hostarch);
exit(0);
}
if ($state->{'state'} ne 'building') {
# something is wrong, consider job bad
$ex = 1;
}
if (! -s "$buildroot/.build.log") {
eval {
if (defined($workerid)) {
writestr("$buildroot/.build.log", undef, "build on $workerid did not create a logfile\n");
} else {
writestr("$buildroot/.build.log", undef, "build did not create a logfile\n");
}
};
$ex = 3;
}
if ($hostcheck) {
print "running post-build host check\n";
my $server = $buildinfo->{'srcserver'} || $srcserver;
if (system($hostcheck, '--srcserver', $server, "$statedir/job", $ex ? 'failed' : 'succeeded', "$buildroot/.build.log")) {
print "post-build host check failed\n";
$ex = 3;
}
}
my @send;
my $kiwitree;
if ($ex == 0 && $buildinfo->{'reason'} ne "rebuild counter sync" && -f "$buildroot/.build.packages/same_result_marker") {
$ex = 2;
}
if ($ex == 0) {
my @d;
push @d, map {"RPMS/$_"} sort(ls("$buildroot/.build.packages/RPMS"));
push @d, 'SRPMS';
@d = ('DEBS') if $buildinfo->{'file'} =~ /(?:\.dsc|build\.collax)$/;
if (-d "$buildroot/.build.packages/SDEBS") {
@d = map {"DEBS/$_"} sort(ls("$buildroot/.build.packages/DEBS")); # assume debbuild
push @d, 'SDEBS';
}
@d = ('ARCHPKGS') if $buildinfo->{'file'} =~ /PKGBUILD$/;
@d = ('KIWI') if $buildinfo->{'file'} =~ /\.kiwi$/;
push @d, 'OTHER';
for my $d ('.', @d) {
my @files = sort(ls("$buildroot/.build.packages/$d"));
@files = grep {$_ ne 'same_result_marker' && $_ ne '.kiwitree'} @files;
if ($localkiwi && ! -d "$localkiwi/jobs/$buildinfo->{'arch'}" && $d eq 'KIWI') {
$kiwitree = [ grep {-d "$buildroot/.build.packages/$d/$_"} @files ];
undef $kiwitree unless @$kiwitree;
undef $kiwitree if defined($BSConfig::nokiwitree) && $BSConfig::nokiwitree;
}
@files = grep {-f "$buildroot/.build.packages/$d/$_"} @files;
push @send, map {"$buildroot/.build.packages/$d/$_"} @files;
}
@send = map {{name => (split('/', $_))[-1], filename => $_}} @send;
if ($kiwitree) {
my $kiwitreefile = "$buildroot/.build.packages/.kiwitree";
eval {
die(".kiwitree already exists\n") if -e $kiwitreefile;
buildkiwitree($kiwitreefile, map { ("$buildroot/.build.packages/KIWI/$_", "$_/") } @$kiwitree);
push @send, { name => '.kiwitree', filename => $kiwitreefile };
};
if ($@) {
print "could not create kiwitree: $@\n";
BSUtil::appendstr("$buildroot/.build.log", "\ncould not create kiwitree: $@\n");
$ex = 1;
}
}
if (!@send && !$localkiwi) {
print "build did not create anything to send back!\n";
$ex = 1;
}
}
my $code;
if (!$ex) {
print "build succeeded, send everything back...\n";
$code = 'succeeded';
} elsif ($ex == 2) {
print "build succeeded, but does not differ from old build result...\n";
$code = 'unchanged';
} elsif ($ex == 3) {
print "build failed, marked as bad build host...\n";
$code = 'badhost';
# Check if buildconfig is just broken and won't ever succeed
my $data = tail_logfile("$buildroot/.build.log", 10240);
if ($data) {
# Remove timing prefix, e.g.
# [ 41s]
$data =~ s/^(?:\[[\d\s]+s\]\s*)?//gm;
# Match any of those:
# run-init: /.build/build: Exec format error
# /bin/rpm: Exec format error
# /bin/sh: error while loading shared libraries: libgcc_s.so.1: cannot open shared object file
if ($data =~ /^(\S{1,30}): error while loading shared libraries: lib/m
|| $data =~ /^(?:run-init: )?\S{1,3330}: Exec format error/m) {
print "Wait ... spotted failed build. Host is good.\n";
$code = 'failed';
}
}
} else {
print "build failed, send back logfile...\n";
$code = 'failed';
}
push @send, {name => 'meta', filename => "$buildroot/.build.meta"} if -e "$buildroot/.build.meta";
push @send, {name => 'logfile', filename => "$buildroot/.build.log"};
if (!$testmode) {
if ($localkiwi && -d "$localkiwi/jobs/$buildinfo->{'arch'}") {
# local delivery hack...
my @s = stat(_);
my ($localkiwi_uid, $localkiwi_gid) = ($s[4], $s[5]);
my $jobdir = "$localkiwi/jobs/$buildinfo->{'arch'}/$buildinfo->{'job'}:dir";
mkdir_p($jobdir);
chown($localkiwi_uid, $localkiwi_gid, $jobdir);
BSUtil::cleandir($jobdir);
for my $f (ls("$buildroot/.build.packages/KIWI")) {
system('chown', '-h', '-R', '--reference', "$jobdir", "$buildroot/.build.packages/KIWI/$f");
rename("$buildroot/.build.packages/KIWI/$f", "$jobdir/$f") || die("rename $buildroot/.build.packages/KIWI/$f $jobdir/$f: $!\n");
}
chown($localkiwi_uid, $localkiwi_gid, "$buildroot/.build.log");
chown($localkiwi_uid, $localkiwi_gid, "$buildroot/.build.meta");
rename("$buildroot/.build.log", "$jobdir/logfile");
rename("$buildroot/.build.meta", "$jobdir/meta");
@send = ();
}
my $param = {
uri => "$buildinfo->{'reposerver'}/putjob",
request => 'POST',
headers => [ 'Content-Type: application/x-cpio' ],
chunked => 1,
data => \&BSHTTP::cpio_sender,
cpiofiles => \@send,
};
my $now = time();
my @args = ("job=$buildinfo->{'job'}", "arch=$buildinfo->{'arch'}", "jobid=$jobid", "code=$code", "now=$now");
push @args, "workerid=$workerid" if defined $workerid;
push @args, "kiwitree=1" if $kiwitree && $code eq 'succeeded';
if ($code eq 'badhost') {
# don't transmit anything in the badhost case
$param = {
uri => "$buildinfo->{'reposerver'}/putjob",
request => 'POST',
};
}
eval {
my $res = BSRPC::rpc($param, undef, @args);
};
if ($@) {
print "rpc failed: $@\nsleeping one minute just in case...\n";
sleep(60);
} else {
print "sent, all done...\n";
}
} else {
print "testmode, not sending anything\n";
print Dumper(\@send);
}
unlink("$buildroot/.build.log");
unlink("$buildroot/job");
print "\n";
cleanup_job();
if ($state->{'nextstate'}) {
$state = {'state' => $state->{'nextstate'}};
commitstate($state);
exit(0);
}
$state = {'state' => 'idle'};
commitstate($state);
hardstatus('idle');
exit(0) if $oneshot && time() > $oneshot;
send_state('idle', $port, $hostarch);
exit(0);
|