/usr/lib/oar/oarapi.pl is in oar-restful-api 2.5.7-3.
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 | #!/usr/bin/perl -w
# vim: set foldmethod=marker:syntax: #
#
# This is the main cgi script for the OAR REST API
# This script is part of the OAR project.
#
# Please, make a good usage of folding markers: {{{ and }}}
# into comments. Under vim, use "za" to fold and "zo" to
# unfold (or just go inside the title to automatically unfold)
# You have to ":set modeline" into vim for the above modeline
# to be interpreted by vim or just type the :set command given
# by this modeline.
#
# Copyright (C) 2009-2016 Laboratoire d'Informatique de Grenoble <http://www.liglab.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
use strict;
use DBI();
use OAR::API;
use OAR::Conf qw(init_conf dump_conf get_conf_list get_conf is_conf set_value);
use OAR::IO;
use OAR::Stat;
use OAR::Nodes;
use OAR::Tools;
use OAR::Version;
use POSIX;
use JSON;
use IO::Handle;
use File::Temp qw/ tempfile /;
use File::Basename;
use CGI::Fast qw/ standard /;
use File::Listing qw(parse_dir);
#use Data::Dumper;
my $VERSION="1.0.2";
##############################################################################
# CONFIGURATION AND INITIALIZATION STUFF
##############################################################################
# Load config
my $oardir;
my $config_file;
if (defined($ENV{OARDIR})){
$oardir = $ENV{OARDIR}."/";
}else{
die("ERROR: OARDIR env variable must be defined.\n");
}
if (defined($ENV{OARCONFFILE})){
init_conf($ENV{OARCONFFILE});
$config_file=$ENV{OARCONFFILE};
}else{
init_conf("/etc/oar/oar.conf");
$config_file="/etc/oar/oar.conf";
}
# Oar commands
my $OARSUB_CMD = "oarsub";
my $OARDEL_CMD = "oardel";
my $OARHOLD_CMD = "oarhold";
my $OARRESUME_CMD = "oarresume";
my $OARNODES_CMD = "oarnodes";
my $OARDODO_CMD = "$ENV{OARDIR}/oardodo/oardodo";
# Oar admin command (for new list of resources generation)
my $API_RESOURCES_LIST_GENERATION_CMD = "";
if (is_conf("API_RESOURCES_LIST_GENERATION_CMD")){ $API_RESOURCES_LIST_GENERATION_CMD = get_conf("API_RESOURCES_LIST_GENERATION_CMD"); }
# OAR server
my $remote_host = get_conf("SERVER_HOSTNAME");
my $remote_port = get_conf("SERVER_PORT");
my $stageout_dir = get_conf("STAGEOUT_DIR");
my $stagein_dir = get_conf("STAGEIN_DIR");
my $allow_create_node = get_conf("DESKTOP_COMPUTING_ALLOW_CREATE_NODE");
my $expiry = get_conf("DESKTOP_COMPUTING_EXPIRY");
# Enable this if you are ok with a simple pidentd "authentication"
# Not very secure, but useful for testing (no need for login/password)
# or in the case you fully trust the client hosts (with an apropriate
# ip-based access control into apache for example)
my $TRUST_IDENT = 1;
if (is_conf("API_TRUST_IDENT")){ $TRUST_IDENT = get_conf("API_TRUST_IDENT"); }
# Default data structure variant
my $STRUCTURE="simple";
if (is_conf("API_DEFAULT_DATA_STRUCTURE")){ $STRUCTURE = get_conf("API_DEFAULT_DATA_STRUCTURE"); }
my $DEFAULT_STRUCTURE=$STRUCTURE;
# Get the default maximum number of items
my $MAX_ITEMS=500;
if (is_conf("API_DEFAULT_MAX_ITEMS_NUMBER")){ $MAX_ITEMS = get_conf("API_DEFAULT_MAX_ITEMS_NUMBER"); }
# Relative/absolute uris config variable
$OAR::API::ABSOLUTE_URIS=1;
if (is_conf("API_ABSOLUTE_URIS")){ $OAR::API::ABSOLUTE_URIS=get_conf("API_ABSOLUTE_URIS"); }
# TMP directory
my $TMPDIR="/tmp";
if (defined($ENV{TMPDIR})) {
$TMPDIR=$ENV{TMPDIR};
}
# Load the html header file
my $file;
if (is_conf("API_HTML_HEADER")){ $file=get_conf("API_HTML_HEADER"); }
else { $file="/etc/oar/api_html_header.pl"; }
open(FILE,$file);
my(@html_header_lines) = <FILE>;
close(FILE);
##############################################################################
# FastCGI loop starting
##############################################################################
my $q;
my $fcgi_cycle_count=0;
FCGI: while ($q = new CGI::Fast) {
$fcgi_cycle_count++;
# Sets the cgi handler of the OAR::API (global variable)
$OAR::API::q=$q;
# Activate debug mode when the script name contains "debug" or when a
# debug parameter is found.
my $DEBUG_MODE=0;
if ( $q->url(-relative=>1) =~ /.*debug.*/ ) { $DEBUG_MODE = 1; };
if ( defined( $q->param('debug') ) && $q->param('debug') eq "1" ) {
$DEBUG_MODE = 1;
}
$OAR::API::DEBUG_MODE=$DEBUG_MODE;
# Check a possible extension
my $extension;
if ( $q->path_info =~ /^$/ ) { $extension = "html"; }
elsif ( $q->path_info =~ /.*\.(yaml|json|html|tgz|tar\.gz)$/ ) { $extension = $1; };
$extension=OAR::API::set_ext($q,$extension);
$OAR::API::extension=$extension;
# Base uri of the api
my $apiuri=OAR::API::get_api_uri_relative_base();
$apiuri =~ s/\/$//;
$OAR::API::apiuri=$apiuri;
# Header for html version
my $HTML_HEADER="";
eval join("\n",@html_header_lines);
$OAR::API::HTML_HEADER=$HTML_HEADER;
##############################################################################
# Authentication
##############################################################################
my $authenticated_user = "";
if ( defined( $ENV{AUTHENTICATE_UID} ) && $ENV{AUTHENTICATE_UID} ne "" ) {
$authenticated_user = $ENV{AUTHENTICATE_UID};
}
else {
# Get ident id from headers or from environement if not in the headers
my $remote_ident="";
if (defined($q->http('X_REMOTE_IDENT'))) {
$remote_ident=$q->http('X_REMOTE_IDENT');
}elsif (defined($ENV{X_REMOTE_IDENT})) {
$remote_ident=$ENV{X_REMOTE_IDENT};
}
if ( $TRUST_IDENT
&& $remote_ident ne ""
&& $remote_ident ne "unknown"
&& $remote_ident ne "(null)" )
{
$authenticated_user = $remote_ident;
}
}
##############################################################################
# URI prefix header variable
##############################################################################
if (defined( $q->http('HTTP_X_API_PATH_PREFIX') ) ) {
$OAR::API::HTTP_X_API_PATH_PREFIX=$q->http('HTTP_X_API_PATH_PREFIX');
}elsif (defined($ENV{X_API_PATH_PREFIX})) {
$OAR::API::HTTP_X_API_PATH_PREFIX=$ENV{X_API_PATH_PREFIX};
}else{
$OAR::API::HTTP_X_API_PATH_PREFIX="";
}
##############################################################################
# Data structure variants
##############################################################################
$STRUCTURE=$DEFAULT_STRUCTURE;
if (defined $q->param('structure')) {
$STRUCTURE=$q->param('structure');
}
if ($STRUCTURE ne "oar" && $STRUCTURE ne "simple") {
OAR::API::ERROR 406, "Unknown $STRUCTURE format",
"Unknown $STRUCTURE format for data structure";
last FCGI;
}
##############################################################################
# URI management
##############################################################################
SWITCH: for ($q) {
my $URI;
###########################################
# API informations
###########################################
#
#{{{ GET /: Root links
#
$URI = qr{^\/*\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$1);
(my $header, my $type)=OAR::API::set_output_format($ext);
my $root={ "oar_version" => OAR::Version::get_version(),
"api_version" => $VERSION,
"apilib_version" => OAR::API::get_version(),
"api_timestamp" => time(),
"api_timezone" => strftime("%Z", localtime()),
"links" => [
{ 'rel' => 'self' ,
'href' => OAR::API::htmlize_uri(OAR::API::make_uri("./",$ext,0),$ext)
},
{ 'rel' => 'collection',
'href' => OAR::API::htmlize_uri(OAR::API::make_uri("resources",$ext,0),$ext),
'title' => 'resources'
} ,
{ 'rel' => 'collection',
'href' => OAR::API::htmlize_uri(OAR::API::make_uri("resources/details",$ext,0),$ext),
'title' => 'full_resources'
} ,
{ 'rel' => 'collection',
'href' => OAR::API::htmlize_uri(OAR::API::make_uri("jobs",$ext,0),$ext),
'title' => 'jobs'
} ,
{ 'rel' => 'collection',
'href' => OAR::API::htmlize_uri(OAR::API::make_uri("jobs/details",$ext,0),$ext),
'title' => 'detailed_jobs'
} ,
{ 'rel' => 'collection',
'href' => OAR::API::htmlize_uri(OAR::API::make_uri("jobs/table",$ext,0),$ext),
'title' => 'jobs_table'
} ,
{ 'rel' => 'collection',
'href' => OAR::API::htmlize_uri(OAR::API::make_uri("config",$ext,0),$ext),
'title' => 'config'
} ,
{ 'rel' => 'collection',
'href' => OAR::API::htmlize_uri(OAR::API::make_uri("admission_rules",$ext,0),$ext),
'title' => 'admission_rules'
}
]
};
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($root,$ext);
last;
};
#}}}
#
#{{{ GET /version : Version informations
#
$URI = qr{^/version\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$1);
(my $header, my $type)=OAR::API::set_output_format($ext);
my $version={ "oar_version" => OAR::Version::get_version(),
"apilib_version" => OAR::API::get_version(),
"api_timestamp" => time(),
"api_timezone" => strftime("%Z", localtime()),
"api_version" => $VERSION };
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($version,$ext);
last;
};
#}}}
#
#{{{ GET /whoami : Authenticated User informations
#
$URI = qr{^/whoami\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$1);
(my $header, my $type)=OAR::API::set_output_format($ext);
my $version={ "api_timestamp" => time(),
"authenticated_user" => $authenticated_user };
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($version,$ext);
last;
};
#}}}
#
#{{{ GET /timezone: Timezone information
#
$URI = qr{^/timezone\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$1);
(my $header, my $type)=OAR::API::set_output_format($ext);
my $version={
"api_timestamp" => time(),
"timezone" => strftime("%Z", localtime())
};
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($version,$ext);
last;
};
#}}}
###########################################
# Jobs
###########################################
#
#{{{ GET /jobs[/details|table]?state=<state>,from=<from>,to=<to>,ids=<id1:id2:...> : List of jobs
#
$URI = qr{^/jobs(/details|/table)*\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext=OAR::API::set_ext($q,$2);
my $header ; my $type;
my $more_infos=$1;
if (!defined($more_infos)) {
($header,$type)=OAR::API::set_output_format($ext,"GET, POST");
}else{
($header,$type)=OAR::API::set_output_format($ext);
}
# Get the id of the user as more details may be obtained for her jobs
if ( $authenticated_user =~ /(\w+)/ ) {
$authenticated_user = $1;
$ENV{OARDO_USER} = $authenticated_user;
}
OAR::Stat::open_db_connection or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
# default parameters for the parameters
my $JOBS_URI_DEFAULT_PARAMS = "state=Finishing,Running,Resuming,Suspended,Launching,toLaunch,Waiting,toAckReservation,Hold";
if (is_conf("API_JOBS_URI_DEFAULT_PARAMS")){ $JOBS_URI_DEFAULT_PARAMS = get_conf("API_JOBS_URI_DEFAULT_PARAMS"); }
# query string parameters
my $from = $q->param('from');
my $to = $q->param('to');
my $state = $q->param('state');
my $user = $q->param('user');
my $array = $q->param('array');
my $max_items = $MAX_ITEMS;
my @ids;
if (!defined($q->param('from')) && !defined($q->param('to')) && !defined($q->param('state')) && !defined($q->param('array')) && !defined($q->param('ids'))) {
my $param = qr{.*from=(.*?)(&|$)};
if ($JOBS_URI_DEFAULT_PARAMS =~ m/$param/) {
$from = $1;
}
$param = qr{.*to=(.*?)(&|$)};
if ($JOBS_URI_DEFAULT_PARAMS =~ m/$param/) {
$to = $1;
}
$param = qr{.*state=(.*?)(&|$)};
if ($JOBS_URI_DEFAULT_PARAMS =~ m/$param/) {
$state = $1;
}
}
if (!defined($array)) { $array=""; };
# GET max items from configuration parameter
if (!defined($q->param('from')) && !defined($q->param('to')) && !defined($q->param('state')) && !defined($q->param('limit'))) {
# get limit from defaut url
my $param = qr{.*limit=(.*?)(&|$)};
if ($JOBS_URI_DEFAULT_PARAMS =~ m/$param/) {
$max_items = $1;
}
}
# GET max items from uri parameter
if (defined($q->param('limit'))) {
$max_items = $q->param('limit');
}
# set offset / GET offset from uri parameter
my $offset = 0;
if (defined($q->param('offset'))) {
$offset = $q->param('offset');
}
# Construct ids array if any
if (defined($q->param('ids'))) {
@ids=split(':',$q->param('ids'));
}
# Get the requested user jobs
my $jobs = OAR::Stat::get_jobs_for_user_query($user,$from,$to,$state,$max_items,$offset,$array,\@ids);
my $total_jobs = OAR::Stat::count_jobs_for_user_query($user,$from,$to,$state,$array,\@ids);
if ( !defined $jobs || keys %$jobs == 0 ) {
$jobs = OAR::API::struct_empty($STRUCTURE);
}
else {
$jobs = OAR::API::struct_job_list_hash_to_array($jobs);
OAR::API::add_joblist_uris($jobs,$ext);
if (defined($more_infos)) {
if ($more_infos eq "/details") {
# will be useful for cigri and behaves as a oarstat -D
# Warning: it's a kind of all in one query and may result in a lot of
# SQL queries. Maybe to optimize...
my $detailed_jobs;
foreach my $j (@$jobs) {
my $job_resources = OAR::Stat::get_job_resources($j);
$j = OAR::Stat::get_job_data($j,1);
my $resources = OAR::API::struct_job_resources($job_resources,$STRUCTURE);
my $nodes= OAR::API::struct_job_nodes($job_resources,$STRUCTURE);
OAR::API::add_resources_uris($resources,$ext,'');
$j->{'resources'}=$resources;
OAR::API::add_nodes_uris($nodes,$ext,'');
$j->{'nodes'}=$nodes;
$j = OAR::API::struct_job($j,$STRUCTURE);
OAR::API::add_job_uris($j,$ext);
push(@$detailed_jobs,$j);
}
$jobs = $detailed_jobs;
}
}
else {
$jobs = OAR::API::struct_job_list($jobs,$STRUCTURE);
}
}
OAR::Stat::close_db_connection();
# add pagination informations
$jobs = OAR::API::add_pagination($jobs,$total_jobs,$q->path_info,$q->query_string,$ext,$max_items,$offset,$STRUCTURE);
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($jobs,$ext);
last;
};
#}}}
#
#{{{ GET /jobs/<id>[/details] : Infos of a job. Adding /details results in a "oarstat -f" equivalent
#
$URI = qr{^/jobs/(\d+)(/details)*(\.yaml|\.json|\.html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $jobid = $1;
my $details = $2;
my $ext=OAR::API::set_ext($q,$3);
(my $header, my $type)=OAR::API::set_output_format($ext,"GET, POST, DELETE");
# Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before looking at jobs" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_USER} = $authenticated_user;
OAR::Stat::open_db_connection or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my $job = OAR::Stat::get_specific_jobs([$jobid]);
if (@$job == 0 ) {
OAR::Stat::close_db_connection;
OAR::API::ERROR( 404, "Job not found",
"Job not found" );
last;
}
my $j=OAR::Stat::get_job_data(@$job[0],1);
if (defined($details) and $details eq "/details") {
my $job_resources = OAR::Stat::get_job_resources(@$job[0]);
my $resources = OAR::API::struct_job_resources($job_resources,$STRUCTURE);
my $nodes= OAR::API::struct_job_nodes($job_resources,$STRUCTURE);
OAR::API::add_resources_uris($resources,$ext,'');
$j->{'resources'}=$resources;
OAR::API::add_nodes_uris($nodes,$ext,'');
$j->{'nodes'}=$nodes;
}
my $result = OAR::API::struct_job($j,$STRUCTURE);
OAR::API::add_job_uris($result,$ext);
OAR::Stat::close_db_connection;
print $header;
if ($ext eq "html") { OAR::API::job_html_header($j); };
print OAR::API::export($result,$ext);
last;
};
#}}}
#
#{{{ GET /jobs/<id>/[resources|nodes] : Resources or nodes assigned or scheduled to a job
#
$URI = qr{^/jobs/(\d+)/(resources|nodes)(\.yaml|\.json|\.html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $jobid = $1;
my $item = $2;
my $ext=OAR::API::set_ext($q,$3);
(my $header, my $type)=OAR::API::set_output_format($ext);
OAR::Stat::open_db_connection or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my $job = OAR::Stat::get_specific_jobs([$jobid]);
my $resources=OAR::Stat::get_job_resources(@$job[0]);
if ($item eq "resources") {
$resources = OAR::API::struct_job_resources($resources,$STRUCTURE);
OAR::API::add_resources_uris($resources,$ext,'');
}else{
$resources = OAR::API::struct_job_nodes($resources,$STRUCTURE);
OAR::API::add_nodes_uris($resources,$ext,'');
}
$resources = OAR::API::add_pagination($resources,@$resources,$q->path_info,undef,$ext,0,0,$STRUCTURE);
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($resources,$ext);
OAR::Stat::close_db_connection;
last;
};
#}}}
#
#{{{ POST /jobs/[array/]<id>/checkpoints|deletions|holds|rholds|resumptions|resubmissions/new : Actions on a job (checkpoint, hold, resume,...)
#
$URI = qr{^/jobs/(array/|)(\d+)/(checkpoints|deletions|holds|rholds|resumptions|resubmissions)+/new(\.yaml|\.json|\.html)*$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $array = $1;
my $jobid = $2;
my $action = $3;
my $ext=OAR::API::set_ext($q,$4);
(my $header, my $type)=OAR::API::set_output_format($ext,"GET, POST");
# Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before modifying jobs" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_BECOME_USER} = $authenticated_user;
if ($array eq "array/") { $array="--array"; }
# Delete (alternative way to DELETE request, for html forms)
my $cmd; my $status;
if ($action eq "deletions" ) {
$cmd = "$OARDODO_CMD $OARDEL_CMD $array $jobid";
$status = "Delete request registered";
}
# Checkpoint
elsif ( $action eq "checkpoints" ) {
$cmd = "$OARDODO_CMD $OARDEL_CMD $array -c $jobid";
$status = "Checkpoint request registered";
}
# Hold
elsif ( $action eq "holds" ) {
$cmd = "$OARDODO_CMD $OARHOLD_CMD $array $jobid";
$status = "Hold request registered";
}
# Hold a running job
elsif ( $action eq "rholds" ) {
$cmd = "$OARDODO_CMD $OARHOLD_CMD $array -r $jobid";
$status = "Hold request registered";
}
# Resume
elsif ( $action eq "resumptions" ) {
$cmd = "$OARDODO_CMD $OARRESUME_CMD $array $jobid";
$status = "Resume request registered";
}
# Resubmit
elsif ( $action eq "resubmissions" ) {
$cmd = "$OARDODO_CMD $OARSUB_CMD $array --resubmit $jobid";
$status = "Resubmit request registered";
}
# Impossible to get here!
else {
OAR::API::ERROR(400,"Bad query","Could not understand ". $action ." method");
last;
}
my $cmdRes = OAR::API::send_cmd($cmd,"Oar");
# Resubmit case (it is a oarsub and we have to catch the new job_id)
if ($action eq "resubmissions" ) {
if ( $? != 0 ) {
my $err = $? >> 8;
OAR::API::ERROR(
500,
"Oar server error",
"Oarsub command exited with status $err: $cmdRes\nCmd:\n$cmd"
);
}
elsif ( $cmdRes =~ m/.*JOB_ID\s*=\s*(\d+).*/m ) {
print $q->header( -status => 201, -type => "$type" );
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( {
'id' => int($1),
'links' => [ { 'rel' => 'self' , 'href' =>
OAR::API::htmlize_uri(OAR::API::make_uri("jobs/$1",$ext,0),$ext) },
{ 'rel' => 'parent', 'href' =>
OAR::API::htmlize_uri(OAR::API::make_uri("jobs/$jobid",$ext,0),$ext) } ],
'status' => "submitted",
'cmd_output' => "$cmdRes",
'api_timestamp' => time()
} , $ext );
}else {
OAR::API::ERROR( 500, "Parse error",
"Job submitted but the id could not be parsed.\nCmd:\n$cmd" );
}
# Other cases
}else{
print $q->header( -status => 202, -type => "$type" );
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( { 'id' => "$jobid",
'status' => "$status",
'cmd_output' => "$cmdRes",
'api_timestamp' => time(),
'links' => [ { 'rel' => 'self' , 'href' =>
OAR::API::htmlize_uri(OAR::API::make_uri("jobs/$jobid",$ext,0),$ext) } ]
} , $ext );
}
last;
};
#}}}
#
#{{{ POST /jobs/<id> : Update of a job (delete, checkpoint, ...)
# Should not be used unless for delete from an http browser
# (better to use the URI above)
#
$URI = qr{^/jobs/(\d+)(\.yaml|\.json|\.html)*$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $jobid = $1;
my $ext=OAR::API::set_ext($q,$2);
(my $header, my $type)=OAR::API::set_output_format($ext,"GET, POST");
# Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before modifying jobs" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_BECOME_USER} = $authenticated_user;
# Check and get the submitted data
# From encoded data
my $job;
if ($q->param('POSTDATA')) {
$job = OAR::API::check_job_update( $q->param('POSTDATA'), $q->content_type );
}
# From html form
else {
$job = OAR::API::check_job_update( $q->Vars, $q->content_type );
}
# Delete (alternative way to DELETE request, for html forms)
my $cmd; my $status;
if ( $job->{method} eq "delete" ) {
$cmd = "$OARDODO_CMD $OARDEL_CMD $jobid";
$status = "Delete request registered";
}
# Checkpoint
elsif ( $job->{method} eq "checkpoint" ) {
$cmd = "$OARDODO_CMD $OARDEL_CMD -c $jobid";
$status = "Checkpoint request registered";
}
# Hold
elsif ( $job->{method} eq "hold" ) {
$cmd = "$OARDODO_CMD $OARHOLD_CMD $jobid";
$status = "Hold request registered";
}
# Resume
elsif ( $job->{method} eq "resume" ) {
$cmd = "$OARDODO_CMD $OARRESUME_CMD $jobid";
$status = "Resume request registered";
}
else {
OAR::API::ERROR(400,"Bad query","Could not understand ". $job->{method} ." method");
last;
}
my $cmdRes = OAR::API::send_cmd($cmd,"Oar");
print $q->header( -status => 202, -type => "$type" );
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( { 'id' => "$jobid",
'status' => "$status",
'cmd_output' => "$cmdRes",
'api_timestamp' => time(),
'links' => [ { 'rel' => 'self' , 'href' =>
OAR::API::htmlize_uri(OAR::API::make_uri("jobs/$jobid",$ext,0),$ext) } ]
} , $ext );
last;
};
#}}}
#
#{{{ POST /jobs/<id>/signals/<signal> : Signal sending
#
$URI = qr{^/jobs/(\d+)/signals/(\d+)(\.yaml|\.json|\.html)*$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $jobid = $1;
my $signal = $2;
my $ext=OAR::API::set_ext($q,$3);
(my $header, my $type)=OAR::API::set_output_format($ext,"GET, POST");
# Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before modifying jobs" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_BECOME_USER} = $authenticated_user;
my $cmd = "$OARDODO_CMD $OARDEL_CMD -s $signal $jobid";
my $status = "Signal sending request registered";
my $cmdRes = OAR::API::send_cmd($cmd,"Oar");
print $q->header( -status => 202, -type => "$type" );
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( { 'id' => "$jobid",
'status' => "$status",
'cmd_output' => "$cmdRes",
'api_timestamp' => time()
} , $ext );
last;
};
#}}}
#
#{{{ POST /jobs : A new job (oarsub wrapper)
#
$URI = qr{^/jobs\.*(yaml|json|html)*$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext=OAR::API::set_ext($q,$1);
(my $header, my $type)=OAR::API::set_output_format($ext,"GET, POST");
# Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before posting jobs" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_BECOME_USER} = $authenticated_user;
# Check and get the submitted job
# From encoded data
my $job;
if ($q->param('POSTDATA')) {
$job = OAR::API::check_job( $q->param('POSTDATA'), $q->content_type );
}
# From html form
else {
$job = OAR::API::check_job( $q->Vars, $q->content_type );
}
# Make the query (the hash is converted into a list of long options)
my $oarcmd = "$OARSUB_CMD ";
my $workdir = "~$authenticated_user";
my $command = "";
my $script = "";
my $param_file = "";
my $tmpfilename = "";
my $tmpparamfilename = "";
my @user_infos;
# Alias resources=resource
if (defined($job->{resources})) {
$job->{resource} = $job->{resources} ;
delete($job->{resources});
}
# Options parsing
foreach my $option ( keys( %{$job} ) ) {
if ($option eq "script_path") {
$job->{script_path} =~ s/(\\*)"/$1$1\\"/g;
$command = " \"$job->{script_path}\"";
# Expand ~ to home directory
@user_infos=getpwnam($authenticated_user);
$command =~ s|/~/|$user_infos[7]/|;
}
elsif ($option eq "command") {
# Escapes double quotes
$job->{command} =~ s/(\\*)"/$1$1\\"/g;
$command = " \"$job->{command}\"";
# Expand ~ to home directory
@user_infos=getpwnam($authenticated_user);
$command =~ s|/~/|$user_infos[7]/|;
}
elsif ($option eq "script") {
$script = $job->{script};
# Expand ~ to home directory
@user_infos=getpwnam($authenticated_user);
$script =~ s|/~/|$user_infos[7]/|;
}
elsif ($option eq "param_file") {
$param_file = $job->{param_file};
}
elsif ($option eq "workdir") {
$workdir = $job->{workdir};
# Expand ~ to home directory
@user_infos=getpwnam($authenticated_user);
$workdir =~ s|/~/|$user_infos[7]/|;
}
elsif (ref($job->{$option}) eq "ARRAY") {
foreach my $elem (@{$job->{$option}}) {
$oarcmd .= " --$option";
# Escapes double quotes
$elem =~ s/(\\*)"/$1$1\\"/g;
$oarcmd .= "=\"$elem\"" if $elem ne "";
}
}
else {
$oarcmd .= " --$option";
# Escapes double quotes
$job->{$option}=~ s/(\\*)"/$1$1\\"/g;
$oarcmd .= "=\"$job->{$option}\"" if $job->{$option} ne "";
}
}
$oarcmd .= $command;
# Escapes double quotes (one more time, for oardodo)
$oarcmd =~ s/(\\*)"/$1$1\\"/g;
my $cmd;
# If a parameters file is provided, we create a temporary file
# and write the parameters inside.
if ($param_file ne "") {
my $TMP;
($TMP, $tmpparamfilename) = tempfile( "oarapi.paramfile.XXXXX", DIR => $TMPDIR, UNLINK => 1 );
print $TMP $param_file;
$oarcmd .= " --array-param-file=$tmpparamfilename";
}
# If a script is provided, we create a file into the workdir and write
# the script inside.
if ($script ne "") {
my $TMP;
($TMP, $tmpfilename) = tempfile( "oarapi.subscript.XXXXX", DIR => $TMPDIR, UNLINK => 1 );
print $TMP $script;
# This is probably the most annoying thing about this trick:
# the tmp file (owned by oar at this stage) has to be readable by the user.
# So, the user should be warned that his script may be public.
# We could make it owned by the user, with a OARDODO call, but it has a
# performance cost.
chmod 0755, $tmpfilename;
$oarcmd .= " ./". basename($tmpfilename);
$cmd = "$OARDODO_CMD bash --noprofile --norc -c \"cp $tmpfilename $workdir/ && cd $workdir && $oarcmd\"";
}else{
$cmd = "$OARDODO_CMD bash --noprofile --norc -c \"cd $workdir && $oarcmd\"";
}
# Escapes some special characters (especially security fix with backquote)
$cmd =~ s/(\\*)(`|\$)/$1$1\\$2/g;
my $cmdRes = `$cmd 2>&1`;
unlink $tmpfilename;
unlink $tmpparamfilename;
if ( $? != 0 ) {
my $err = $? >> 8;
# Error codes corresponding to an error into the user's request
if ( $err == 6 || $err == 4 || $err == 5 || $err == 7
|| $err == 17 || $err == 16 || $err == 6
|| $err == 8 || $err == 10 ) {
OAR::API::ERROR(
400,
"Bad query",
"Oarsub command exited with status $err: $cmdRes\nCmd:\n$oarcmd"
);
}elsif( $err == 52) {
OAR::API::ERROR(
401,
"Permission denied",
"Oardodo error: $err: $cmdRes\n"
);
}else{
OAR::API::ERROR(
500,
"Oar server error",
"Oarsub command exited with status $err: $cmdRes\nCmd:\n$oarcmd"
);
}
}
elsif ( $cmdRes =~ m/.*JOB_ID\s*=\s*(\d+).*/m ) {
my $uri=OAR::API::htmlize_uri(OAR::API::make_uri("jobs/$1",$ext,0),$ext);
my $abs_uri=OAR::API::make_uri("jobs/$1",$ext,1);
print $q->header( -status => 201, -type => "$type" , -location => $abs_uri );
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( { 'id' => int($1),
'links' => [ { 'href' => $uri,
'rel' => "self"} ],
'api_timestamp' => time(),
'cmd_output' => "$cmdRes",
} , $ext );
}
else {
OAR::API::ERROR( 500, "Parse error",
"Job submitted but the id could not be parsed.\nCmd:\n$oarcmd" );
}
last;
};
#}}}
#
#{{{ DELETE /jobs/<id> : Delete a job (oardel wrapper)
#
$URI = qr{^/jobs/(\d+)(\.yaml|\.json|\.html)*$};
OAR::API::DELETE( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $jobid = $1;
my $ext=OAR::API::set_ext($q,$2);
(my $header, my $type)=OAR::API::set_output_format($ext);
# Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before deleting jobs" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_BECOME_USER} = $authenticated_user;
my $cmd = "$OARDODO_CMD $OARDEL_CMD $jobid";
my $cmdRes = OAR::API::send_cmd($cmd,"Oardel");
print $q->header( -status => 202, -type => "$type" );
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( { 'id' => "$jobid",
'status' => "Delete request registered",
'oardel_output' => "$cmdRes",
'api_timestamp' => time()
} , $ext );
last;
};
#}}}
#
#{{{ /jobs/stagein and stageout (desktop computing)
#
$URI = qr{^/jobs/(\d+)/stagein(.tar\.gz|.tgz)$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$2);
(my $header, my $type)=OAR::API::set_output_format($ext);
OAR::API::jobStageIn($1);
last;
};
$URI = qr{^/jobs/(\d+)/stagein(.tar\.gz|.tgz)$};
OAR::API::HEAD( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$2);
(my $header, my $type)=OAR::API::set_output_format($ext);
OAR::API::jobStageInHead($1);
last;
};
$URI = qr{^/jobs/(\d+)/stageout(.tar\.gz|.tgz)$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$2);
(my $header, my $type)=OAR::API::set_output_format($ext,"GET, POST");
my $fh = $q->upload('myfile');
if (defined $fh) {
my $io_handle = $fh->handle;
my $buffer;
open (OUTFILE, '>>', "$stageout_dir/$1.tgz");
while (my $bytesread = $io_handle->read($buffer, 1024)) {
print OUTFILE $buffer;
}
}
print $q->header( -status => 200, -type => "application/x-tar" );
last;
};
#}}}
#
#{{{ POST /jobs/<id>/state : changes the state of a job
#
$URI = qr{^/jobs/(\d+)/state(.*)$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$2);
(my $header, my $type)=OAR::API::set_output_format($ext,"GET, POST");
print $q->header( -status => 200, -type => $type );
my $json = decode_json $q->param('POSTDATA');
my $state = $json->{'state'};
if ($state eq 'running'){
OAR::API::runJob($1);
} elsif($state eq 'terminated'){
OAR::API::terminateJob($1);
} elsif($state eq 'error'){
OAR::API::errorJob($1);
} else {
die "unknown state"
}
last;
};
#}}}
###########################################
# Resources
###########################################
#
#{{{ GET /resources/(details|<id>) : List of resources or details of a resource
#
$URI = qr{^/resources(/full|/details|/[0-9]+)*\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext=OAR::API::set_ext($q,$2);
my $header, my $type;
if(defined($1)) {
# "/full" is kept for backward compatibility
if ($1 ne "/full" && $1 ne "/details") {
($header, $type)=OAR::API::set_output_format($ext,"GET, DELETE");
}else{
($header, $type)=OAR::API::set_output_format($ext,"GET");
}
}else{
($header, $type)=OAR::API::set_output_format($ext,"GET, POST");
}
# will the resources need be paged or not
# by default resources results are paged
my $paged = 1;
my $compact = 0;
my $max_items = $MAX_ITEMS;
# GET limit from uri parameter
if (defined($q->param('limit'))) {
$max_items = $q->param('limit');
}
# set offset / GET offset from uri parameter
my $offset = 0;
if (defined($q->param('offset'))) {
$offset = $q->param('offset');
}
my $resources;
OAR::Nodes::open_db_connection or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
if (defined($1)) {
if ($1 eq "/full" || $1 eq "/details") {
# get specified intervals of resources
$resources = OAR::Nodes::get_requested_resources($max_items,$offset);
}
elsif ($1 =~ /\/([0-9]+)/) {
# get the resources infos
my $resource = OAR::Nodes::get_resource_infos($1);
if (defined($resource)) {
$resources = [OAR::Nodes::get_resource_infos($1)];
}
else {
# resource does not exist
#$resources = OAR::API::struct_empty($STRUCTURE);
OAR::Nodes::close_db_connection;
OAR::API::ERROR( 404, "Resource not found",
"Resource not found" );
}
# do not need to paging resource detail
$paged = 0;
$compact = 1;
}
else {
OAR::Nodes::close_db_connection;
OAR::API::ERROR(500,"Error 666!","Error 666");
}
}
else
{
# get specified intervals of resources
$resources = OAR::Nodes::get_requested_resources($max_items,$offset);
$resources = OAR::API::filter_resource_list($resources);
}
OAR::API::fix_resource_ids($resources);
OAR::API::add_resources_uris($resources,$ext,'');
$resources = OAR::API::struct_resource_list($resources,$STRUCTURE,$compact);
# test if resources need to be paged
if ($paged == 1) {
# get the total number of resources
my $total_resources = OAR::Nodes::count_all_resources();
# add pagination informations
$resources = OAR::API::add_pagination($resources,$total_resources,$q->path_info,$q->query_string,$ext,$max_items,$offset,$STRUCTURE);
}
OAR::Nodes::close_db_connection;
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($resources,$ext);
last;
};
#}}}
#
#{{{ GET /resources/nodes/<node> : List the resources of a node
#
$URI = qr{^/resources/nodes/([\w\.-]+?)(\.yaml|\.json|\.html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext=OAR::API::set_ext($q,$2);
(my $header, my $type)=OAR::API::set_output_format($ext);
OAR::Nodes::open_db_connection or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my $resources = OAR::Nodes::get_resources_for_host($1);
$resources = OAR::API::filter_resource_list($resources);
OAR::Nodes::close_db_connection;
OAR::API::add_resources_uris($resources,$ext,'');
$resources = OAR::API::struct_resource_list($resources,$STRUCTURE,0);
$resources = OAR::API::add_pagination($resources,@$resources,$q->path_info,undef,$ext,0,0,$STRUCTURE);
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($resources,$ext);
last;
};
#}}}
#
#{{{ GET /resources/(<id>)/jobs : Jobs running on a resource
#
$URI = qr{^/resources(/[0-9]+)+/jobs(\.yaml|\.json|\.html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext=OAR::API::set_ext($q,$2);
(my $header, my $type)=OAR::API::set_output_format($ext);
OAR::Nodes::open_db_connection or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my $jobs;
if ($1 =~ /\/([0-9]+)/) {
my $job_array=OAR::Nodes::get_jobs_running_on_resource($1);
foreach my $job_id (@$job_array) {
push(@$jobs,{id =>int($job_id)});
}
OAR::API::add_jobs_on_resource_uris($jobs,$ext);
}
OAR::Nodes::close_db_connection;
$jobs = OAR::API::add_pagination($jobs,@$jobs,$q->path_info,undef,$ext,0,0,$STRUCTURE);
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($jobs,$ext);
last;
};
#}}}
#
#{{{ GET /resources/nodes/<node>/jobs : Jobs running on a node
#
$URI = qr{^/resources/nodes/([-\.\w]+)/jobs(.*)$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$2);
(my $header, my $type)=OAR::API::set_output_format($ext);
OAR::Nodes::open_db_connection or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my $jobs;
my $job_array;
#OAR::Nodes::heartbeat($1); <-- what the hell is that????
# a "?state=" filter is possible, but prevent Terminated and Error state
# because this result is not paginated and output may be too big
if (defined($q->param('state')) && $q->param('state') eq "toKill"){
# This "toKill" state is virtual and implemented for the desktop
# computing agent purpose.
$job_array=OAR::Nodes::get_jobs_running_on_node($1);
foreach my $job_id (@$job_array) {
push(@$jobs,{id =>int($job_id)}) if OAR::Nodes::is_job_tokill($job_id);
}
}
elsif (defined($q->param('state')) && $q->param('state') ne "Terminated"
&& $q->param('state') ne "Error" ) {
$job_array=OAR::Nodes::get_jobs_on_node($1,$q->param('state'));
foreach my $job_id (@$job_array) {
push(@$jobs,{id =>int($job_id)});
}
}else{
$job_array=OAR::Nodes::get_jobs_running_on_node($1);
foreach my $job_id (@$job_array) {
push(@$jobs,{id =>int($job_id)});
}
}
OAR::API::add_jobs_on_resource_uris($jobs,$ext);
OAR::Nodes::close_db_connection;
$jobs = OAR::API::add_pagination($jobs,@$jobs,$q->path_info,undef,$ext,0,0,$STRUCTURE);
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($jobs,$ext);
last;
};
#}}}
#
#{{{ GET /resources/<property>/jobs : List jobs running on resources, by property
#
$URI = qr{^/resources/([A-za-z0-9]+)*/jobs\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext=OAR::API::set_ext($q,$2);
my $header, my $type;
my $property=$1;
($header, $type)=OAR::API::set_output_format($ext,"GET");
my $dbh = OAR::IO::connect() or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my $jobs=OAR::IO::get_resources_jobs($dbh) or OAR::IO::disconnect($dbh), OAR::API::ERROR(500,
"Could not get resources jobs",
"Could not get resources jobs"
);
my $resources=OAR::IO::get_resources_by_property($dbh,$property) or OAR::IO::disconnect($dbh), OAR::API::ERROR(500,
"Could not get resources by property $property",
"Could not get resources by property $property"
);
my %resources_by_property_jobs;
foreach my $property (keys %$resources) {
if (!defined($resources_by_property_jobs{$property})) { @{$resources_by_property_jobs{$property}}=();}
foreach my $resource_id (@{$resources->{$property}}) {
foreach my $job_id (@{$jobs->{$resource_id}}) {
push(@{$resources_by_property_jobs{$property}}, $job_id);
}
}
}
# Formatting pass
my @items;
foreach my $r (keys %resources_by_property_jobs) {
push(@items,{ $property => $r, jobs => $resources_by_property_jobs{$r} });
}
my $output={ 'items' => \@items,
'links' => [ { 'rel' => "self", 'href' => OAR::API::htmlize_uri(OAR::API::make_uri("/resources/$property/jobs",$ext,0),$ext) } ],
'api_timestamp' => time() };
OAR::IO::disconnect($dbh);
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($output,$ext);
last;
};
#}}}
#
#{{{ POST /resources : Create new resources
#
$URI = qr{^/resources(\.yaml|\.json|\.html)*$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext=OAR::API::set_ext($q,$1);
(my $header)=OAR::API::set_output_format($ext,"GET, POST");
# Must be administrator (oar user)
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before creating new resources" );
last;
}
if ( not $authenticated_user eq "oar" ) {
OAR::API::ERROR( 401, "Permission denied",
"Only the oar user can create new resources" );
last;
}
$ENV{OARDO_BECOME_USER} = "oar";
# Check and get the submited resource
# From encoded data
my $resources;
if ($q->param('POSTDATA')) {
$resources = OAR::API::check_resources( $q->param('POSTDATA'), $q->content_type );
}
# From html form
else {
$resources = OAR::API::check_resources( $q->Vars, $q->content_type );
}
my $dbh = OAR::IO::connect() or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my @ids=OAR::IO::add_resources($dbh,$resources) or OAR::IO::disconnect($dbh), OAR::API::ERROR(500,
"Could not create asked resources",
"Could not create asked resources"
);
my $result=[];
foreach my $id (@ids) {
if (not $id =~ /^\d+$/) {
OAR::IO::disconnect($dbh);
OAR::API::ERROR(500,"SQL query failed into resources creation",$id);
}
push(@$result,{ id => $id, links => [ { rel => "self", href => "resources/$id" } ] });
}
$result = OAR::API::add_pagination($result,@ids,$q->path_info,$q->query_string,$ext,0,0,$STRUCTURE);
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( $result , $ext );
OAR::Tools::notify_tcp_socket($remote_host,$remote_port,"ChState");
OAR::Tools::notify_tcp_socket($remote_host,$remote_port,"Term");
OAR::IO::disconnect($dbh);
last;
};
#}}}
#
#{{{ POST /resources/<id>/state : Change the state of a resource
#
$URI = qr{^/resources/(\d+)/state(\.yaml|\.json|\.html)*$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $id=$1;
my $ext=OAR::API::set_ext($q,$2);
(my $header)=OAR::API::set_output_format($ext,"GET, POST");
# Must be administrator (oar user)
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before creating new resources" );
last;
}
if ( not $authenticated_user eq "oar" ) {
OAR::API::ERROR( 401, "Permission denied",
"Only the oar user can create new resources" );
last;
}
$ENV{OARDO_BECOME_USER} = "oar";
# Check and get the submited resource
# From encoded data
my $resource;
if ($q->param('POSTDATA')) {
$resource = OAR::API::check_resource_state( $q->param('POSTDATA'), $q->content_type );
}
# From html form
else {
$resource = OAR::API::check_resource_state( $q->Vars, $q->content_type );
}
my $dbh = OAR::IO::connect() or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
OAR::IO::set_resource_state($dbh,$id,$resource->{state},"NO");
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( {
'status' => "Change state request registered",
'id' => "$id",
'api_timestamp' => time(),
'uri' => OAR::API::htmlize_uri(OAR::API::make_uri("resources/$id",$ext,0),$ext)
} , $ext );
OAR::Tools::notify_tcp_socket($remote_host,$remote_port,"ChState");
OAR::Tools::notify_tcp_socket($remote_host,$remote_port,"Term");
OAR::IO::disconnect($dbh);
last;
};
#}}}
#
#{{{ DELETE /resources/(<id>|<node>/<cpuset) : Delete a resource (by id or node+cpuset)
#
$URI = qr{^/resources/([\w\.-]+?)(/\d)*(\.yaml|\.json|\.html)*$};
OAR::API::DELETE( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $id;
my $node;
my $cpuset;
if ($2) { $node=$1; $id=0; $cpuset=$2; $cpuset =~ s,^/,, ;}
else { $node=""; $id=$1; $cpuset=""; } ;
my $ext=OAR::API::set_ext($q,$3);
(my $header)=OAR::API::set_output_format($ext);
# Must be administrator (oar user)
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before deleting new resources" );
last;
}
if ( not $authenticated_user eq "oar" ) {
OAR::API::ERROR( 401, "Permission denied",
"Only the oar user can delete resources" );
last;
}
$ENV{OARDO_BECOME_USER} = "oar";
my $base = OAR::IO::connect() or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
# Check if the resource exists
my $query;
my $Resource;
if ($id == 0) {
$query="WHERE network_address = \"$node\" AND cpuset = $cpuset";
}
else {
$query="WHERE resource_id=$id";
}
my $sth = $base->prepare("SELECT resource_id FROM resources $query");
$sth->execute();
my @res = $sth->fetchrow_array();
if ($res[0]) { $Resource=$res[0];}
else {
OAR::IO::disconnect($base);
OAR::API::ERROR(404,"Not found","Corresponding resource could not be found ($id,$node,$cpuset)");
last;
}
# Resource deletion
# !!! This is a dirty cut/paste of oarremoveresource code !!!
my $resource_ref = OAR::IO::get_resource_info($base,$Resource);
if (defined($resource_ref->{state}) && ($resource_ref->{state} eq "Dead")){
my $sth = $base->prepare(" SELECT jobs.job_id, jobs.assigned_moldable_job
FROM assigned_resources, jobs
WHERE
assigned_resources.resource_id = $Resource
AND assigned_resources.moldable_job_id = jobs.assigned_moldable_job
");
$sth->execute();
my @jobList;
while (my @ref = $sth->fetchrow_array()) {
push(@jobList, [$ref[0], $ref[1]]);
}
$sth->finish();
foreach my $i (@jobList){
$base->do("DELETE from event_logs WHERE job_id = $i->[0]");
$base->do("DELETE from frag_jobs WHERE frag_id_job = $i->[0]");
$base->do("DELETE from jobs WHERE job_id = $i->[0]");
$base->do("DELETE from assigned_resources WHERE moldable_job_id = $i->[1]");
}
$base->do("DELETE from assigned_resources WHERE resource_id = $Resource");
$base->do("DELETE from resource_logs WHERE resource_id = $Resource");
$base->do("DELETE from resources WHERE resource_id = $Resource");
#print("Resource $Resource removed.\n");
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( { 'status' => "deleted",'api_timestamp' => time() } , $ext );
}else{
OAR::IO::disconnect($base);
OAR::API::ERROR(403,"Forbidden","The resource $Resource must be in the Dead status");
last;
}
OAR::IO::disconnect($base);
last;
};
#}}}
#
#{{{ POST /resources/generate : Generate resources (needs an external command like oar_resources_add)
#
$URI = qr{^/resources/generate(\.yaml|\.json|\.html)*$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$1);
(my $header) = OAR::API::set_output_format($ext,"GET, POST");
# Must be administrator (oar user)
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before generating resources" );
last;
}
if ( not $authenticated_user eq "oar" ) {
OAR::API::ERROR( 401, "Permission denied",
"Only the oar user can generate resources" );
last;
}
$ENV{OARDO_BECOME_USER} = "oar";
# Check feature activation
if ($API_RESOURCES_LIST_GENERATION_CMD eq "" ) {
OAR::API::ERROR( 500, "Disabled feature",
"To enable this feature, please provide a command into the API_RESOURCES_LIST_GENERATION_CMD configuration variable" );
last;
}
# Check and get the submited resource description
# From encoded data
my $description;
# command generation
my $cmd;
# ressources properties
my $cmd_properties="";
if ($q->param('POSTDATA')) {
$description = OAR::API::check_resource_description( $q->param('POSTDATA'), $q->content_type );
# getting properties
if (defined($description->{properties})) {
foreach my $property ( keys %{$description->{properties}} ) {
$cmd_properties .= " -p ".$property."=".$description->{properties}->{$property}
}
}
}
# From html form
else {
$description = OAR::API::check_resource_description( $q->Vars, $q->content_type );
# getting properties
if (defined($description->{properties})) {
my @properties = split(/,/,$description->{properties});
foreach my $property (@properties) {
$cmd_properties .= " -p $property";
}
}
}
my $auto_offset="";
if (defined($description->{auto_offset}) && "$description->{auto_offset}" eq "1") {
$auto_offset="--auto-offset ";
}
# command with arguments
$cmd = "$API_RESOURCES_LIST_GENERATION_CMD $auto_offset".$description->{resources}.$cmd_properties;
# execute the command
my $cmdRes = OAR::API::send_cmd($cmd,"Oar");
my $data = OAR::API::import_data($cmdRes,"yaml");
OAR::API::struct_resource_list_fix_ints($data);
$data = OAR::API::add_pagination($data,@$data,$q->path_info,undef,$ext,0,0,$STRUCTURE);
print $header;
if ($ext eq "html") {
print $HTML_HEADER;
api_uri::resources_commit_button($data->{"items"});
}
print OAR::API::export($data,$ext);
last;
};
#}}}
###########################################
# Accounting
###########################################
#
#{{{ GET /accounting[<user>]?from=<from>,to=<to> : Show accounting informations between 2 unix timestamps
#
$URI = qr{^/accounting(/[^\..]+)*\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext=OAR::API::set_ext($q,$2);
my $header ; my $type;
my $user=$1;
($header,$type)=OAR::API::set_output_format($ext,"GET");
if (defined($user)) {
$user=~s/^\///;
}
my $JOBS_URI_DEFAULT_PARAMS = "from=0&to=2147483647";
if (is_conf("API_JOBS_URI_DEFAULT_PARAMS")){ $JOBS_URI_DEFAULT_PARAMS = get_conf("API_JOBS_URI_DEFAULT_PARAMS"); }
my $from;
if (!defined($q->param('from'))) {
my $param = qr{.*from=(.*?)(&|$)};
if ($JOBS_URI_DEFAULT_PARAMS =~ m/$param/) {
$from = $1;
}else {
$from = 0;
}
}else{
$from = $q->param('from');
}
my $to;
if (!defined($q->param('to'))) {
my $param = qr{.*to=(.*?)(&|$)};
if ($JOBS_URI_DEFAULT_PARAMS =~ m/$param/) {
$to = $1;
}else{
$to = 2147483647;
}
}else{
$to = $q->param('to');
}
# get accounting infos
my $dbh = OAR::IO::connect() or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my $accounting = OAR::API::struct_accounting(OAR::IO::get_accounting_summary_byproject($dbh,$from,$to,$user,undef,undef));
my $total;
if (defined($accounting)) {
$total = @$accounting;
}else{
$accounting=OAR::API::struct_empty($STRUCTURE);
$total=0;
}
OAR::IO::disconnect($dbh);
# add pagination informations and export
$accounting = OAR::API::add_pagination($accounting,$total,$q->path_info,$q->query_string,$ext,0,0,$STRUCTURE);
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($accounting,$ext);
last;
};
#}}}
###########################################
# Admission rules
###########################################
#
#{{{ GET /admission_rules : List of all admissions rules
#
$URI = qr{^/admission_rules\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$1);
(my $header, my $type) = OAR::API::set_output_format($ext);
# Must be administrator (oar user)
OAR::API::authenticate_user($authenticated_user,"get admission rules","oar");
# GET limit from uri parameter
my $max_items=$MAX_ITEMS;
if (defined($q->param('limit'))) {
$max_items = $q->param('limit');
}
# set offset / GET offset from uri parameter
my $offset = 0;
if (defined($q->param('offset'))) {
$offset = $q->param('offset');
}
OAR::Stat::open_db_connection or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
# get specified intervals of admission rules
my $admissions_rules = OAR::Stat::get_requested_admission_rules($max_items,$offset);
OAR::API::add_admission_rules_uris($admissions_rules,$ext);
$admissions_rules = OAR::API::struct_admission_rule_list($admissions_rules,$STRUCTURE);
# get the total number of admissions rules
my $total_rules = OAR::Stat::count_all_admission_rules();
OAR::Stat::close_db_connection();
# add pagination informations
$admissions_rules = OAR::API::add_pagination($admissions_rules,$total_rules,$q->path_info,$q->query_string,$ext,$max_items,$offset,$STRUCTURE);
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($admissions_rules,$ext);
last;
};
#}}}
#
#{{{ GET /admission_rules/<id> : Details of an admission rule
#
$URI = qr{^/admission_rules/(\d+)(\.yaml|\.json|\.html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $rule_id = $1;
my $ext = OAR::API::set_ext($q,$2);
(my $header, my $type)=OAR::API::set_output_format($ext);
# Must be administrator (oar user)
OAR::API::authenticate_user($authenticated_user,"get admission rules","oar");
OAR::Stat::open_db_connection or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my $admission_rule = OAR::Stat::get_specific_admission_rule($rule_id);
if (defined($admission_rule->{id})) {
OAR::API::add_admission_rule_uris($admission_rule,$ext);
$admission_rule = OAR::API::struct_admission_rule($admission_rule,$STRUCTURE);
OAR::Stat::close_db_connection;
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($admission_rule,$ext);
}else{
OAR::Stat::close_db_connection;
OAR::API::ERROR(404,"Admission rule not found","No admission rule corresponding to id=$rule_id");
}
last;
};
#}}}
#
#{{{ POST /admission_rules : Create a new admission rule
#
$URI = qr{^/admission_rules(\.yaml|\.json|\.html)*$};
(OAR::API::POST( $_, $URI ) || OAR::API::PUT( $_, $URI )) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$1);
(my $header) = OAR::API::set_output_format($ext,"GET, POST");
# Must be administrator (oar user)
OAR::API::authenticate_user($authenticated_user,"create admission rules","oar");
# Check and get the submited admission rule
# From encoded data
my $admission_rule;
if ($q->param('POSTDATA')) {
$admission_rule = OAR::API::check_admission_rule( $q->param('POSTDATA'), $q->content_type );
}
elsif ($q->param('PUTDATA')) {
$admission_rule = OAR::API::check_admission_rule( $q->param('PUTDATA'), $q->content_type );
}
# From html form
else {
$admission_rule = OAR::API::check_admission_rule( $q->Vars, $q->content_type );
}
my $dbh = OAR::IO::connect() or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my $id = OAR::IO::add_admission_rule($dbh,$admission_rule->{priority},$admission_rule->{enabled},$admission_rule->{rule});
if ( $id && $id > 0) {
print $q->header( -status => 201, -type => OAR::API::get_content_type($ext) , -location => OAR::API::htmlize_uri(OAR::API::make_uri("admission_rules/$id",$ext,0),$ext) );
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( {
'id' => "$id",
'priority' => "$admission_rule->{priority}",
'enalbed' => "$admission_rule->{enabled}",
'rule' => OAR::API::nl2br($admission_rule->{rule}),
'api_timestamp' => time(),
'uri' => OAR::API::htmlize_uri(OAR::API::make_uri("admission_rules/$id",$ext,0),$ext)
} , $ext );
OAR::IO::disconnect($dbh);
}
else {
OAR::IO::disconnect($dbh);
OAR::API::ERROR(
500,
"Admission rule not created",
"Could not create the new admission rule"
);
}
last;
};
#}}}
#
#{{{ DELETE /admission_rules/<id> : Delete an admission rule
#
$URI = qr{^/admission_rules/(\d+)(\.yaml|\.json|\.html)*$};
OAR::API::DELETE( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $rule_id = $1;
my $ext = OAR::API::set_ext($q,$2);
(my $header, my $type)=OAR::API::set_output_format($ext);
# Must be administrator (oar user)
OAR::API::authenticate_user($authenticated_user,"delete admission rules","oar");
OAR::Stat::open_db_connection or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my $admission_rule = OAR::Stat::get_specific_admission_rule($rule_id);
print $header;
if (defined($admission_rule)) {
OAR::Stat::delete_specific_admission_rule($rule_id);
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( {
'id' => "$admission_rule->{id}",
'priority' => "$admission_rule->{priority}",
'enalbed' => "$admission_rule->{enabled}",
'rule' => "$admission_rule->{rule}",
'status' => "deleted",
'api_timestamp' => time()
} , $ext );
OAR::Stat::close_db_connection;
}
else {
OAR::Stat::close_db_connection;
OAR::API::ERROR(404,"Not found","Corresponding admission rule could not be found");
}
last;
};
#}}}
#
#{{{ POST /admission_rules/<id>[?method=delete]: Erase or Delete an admission rule
#
$URI = qr{^/admission_rules/(\d+)(\.yaml|\.json|\.html)*$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $rule_id = $1;
my $ext = OAR::API::set_ext($q,$2);
(my $header, my $type) = OAR::API::set_output_format($ext,"GET, POST");
# Must be administrator (oar user)
OAR::API::authenticate_user($authenticated_user,"edit admission rules","oar");
my $dbh = OAR::IO::connect() or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
# Check and get the submitted data
# From encoded data
my $admission_rule;
if ($q->param('POSTDATA')) {
$admission_rule = OAR::API::check_admission_rule_update( $q->param('POSTDATA'), $q->content_type );
}
# From html form
else {
$admission_rule = OAR::API::check_admission_rule_update( $q->Vars, $q->content_type );
}
# Delete (alternative way to DELETE request, for html forms)
if (defined($admission_rule->{method}) && $admission_rule->{method} eq "delete" ) {
print $q->header( -status => 202, -type => OAR::API::get_content_type($ext) );
my $id=OAR::IO::delete_admission_rule($dbh,$rule_id);
OAR::IO::disconnect($dbh);
if ( $id && $id > 0) {
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( { 'id' => "$rule_id",
'status' => "deleted",
'api_timestamp' => time()
} , $ext );
}else{
OAR::API::ERROR(
404,
"Admission rule not found",
"Could not find admission rule to delete: id=$rule_id"
);
}
}elsif (defined($admission_rule->{priority}) and defined($admission_rule->{enabled}) and defined($admission_rule->{rule})) {
my $id = OAR::IO::update_admission_rule($dbh,$rule_id,$admission_rule->{priority},$admission_rule->{enabled},$admission_rule->{rule});
OAR::IO::disconnect($dbh);
if ( $id && $id > 0) {
print $q->header( -status => 201, -type => OAR::API::get_content_type($ext) , -location => OAR::API::htmlize_uri(OAR::API::make_uri("admission_rules/$id",$ext,0),$ext) );
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export( {
'id' => "$id",
'priority' => "$admission_rule->{priority}",
'enalbed' => "$admission_rule->{enabled}",
'rule' => OAR::API::nl2br($admission_rule->{rule}),
'api_timestamp' => time(),
'uri' => OAR::API::htmlize_uri(OAR::API::make_uri("admission_rules/$id",$ext,0),$ext)
} , $ext );
}
else {
OAR::API::ERROR(
404,
"Admission rule not found",
"Could not find admission rule to update: id=$rule_id"
);
}
}else {
OAR::API::ERROR(400,"Bad query","Could not understand ". $admission_rule->{method} ." method");
}
last;
};
#}}}
###########################################
# Config file edition
###########################################
#
#{{{ GET /config : List of all the configured variables
#
$URI = qr{^/config\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$1);
(my $header, my $type) = OAR::API::set_output_format($ext);
# Must be administrator (oar user)
OAR::API::authenticate_user($authenticated_user,"get configuration variables","oar");
# get all configured parameters
my $list_params = get_conf_list();
# parameters hash result
my $parameters;
if ( !defined $list_params || keys %$list_params == 0 ) {
$parameters = OAR::API::struct_empty($STRUCTURE);
}
else {
foreach my $param (keys %$list_params) {
$parameters->{$param}->{value} = $list_params->{$param};
}
OAR::API::add_config_parameters_uris($parameters,$ext);
$parameters = OAR::API::struct_config_parameters_list($parameters,$STRUCTURE);
}
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($parameters,$ext);
last;
};
#}}}
#
#{{{ GET /config/file : Get the raw configuration file
#
$URI = qr{^/config/file\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$1);
(my $header, my $type) = OAR::API::set_output_format($ext);
# Must be administrator (oar user)
OAR::API::authenticate_user($authenticated_user,"get configuration variables","oar");
my $cmd="$OARDODO_CMD cat $config_file";
my $cmdRes = OAR::API::send_cmd($cmd,"cat $config_file");
my $file= { 'path' => $config_file, 'file' => $cmdRes };
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($file,$ext);
last;
};
#}}}
#
#{{{ GET /config/<variable_name> : Get a configuration variable value
#
$URI = qr{^/config/(\w+)\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $variable = $1;
my $ext = OAR::API::set_ext($q,$2);
(my $header, my $type) = OAR::API::set_output_format($ext);
# Must be administrator (oar user)
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before getting configuration parameters" );
last;
}
if ( not $authenticated_user eq "oar" ) {
OAR::API::ERROR( 401, "Permission denied",
"Only the oar user can get configuration parameters" );
last;
}
$ENV{OARDO_BECOME_USER} = "oar";
# result parameter
my $parameter;
if (is_conf($variable)) {
$parameter->{id} = $variable;
$parameter->{value} = get_conf($variable);
OAR::API::add_config_parameter_uris($parameter,$ext);
$parameter = OAR::API::struct_config_parameter($parameter,$STRUCTURE);
}
else {
$parameter->{id} = OAR::API::struct_empty($STRUCTURE);
}
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export($parameter,$ext);
last;
};
#}}}
#
#{{{ POST /config/<variable_name> : Change the value of a configuration parameter
#
$URI = qr{^/config/(\w+)\.*(yaml|json|html)*$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $variable = $1;
my $ext = OAR::API::set_ext($q,$2);
(my $header, my $type) = OAR::API::set_output_format($ext,"GET, POST");
print $header;
print $HTML_HEADER if ($ext eq "html");
# Must be administrator (oar user)
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before changing configuration parameters" );
last;
}
if ( not $authenticated_user eq "oar" ) {
OAR::API::ERROR( 401, "Permission denied",
"Only the oar user can make changes on configuration parameters" );
last;
}
$ENV{OARDO_BECOME_USER} = "oar";
# configuration parameter
my $parameter;
if ($q->param('POSTDATA')) {
$parameter = OAR::API::check_configuration_variable( $q->param('POSTDATA'), $q->content_type );
}
# From html form
else {
$parameter = OAR::API::check_configuration_variable( $q->Vars, $q->content_type );
}
my $result;
set_value($variable, $parameter->{value});
if (is_conf($variable)) {
print $q->header( -status => 202, -type => $type , -location => "/config/$variable" );
$result={"status" => "accepted"};
}
else {
print $q->header( -status => 201, -type => $type , -location => "/config/$variable" );
$result={"status" => "created"};
}
print OAR::API::export($result,$ext);
last;
};
#}}}
###########################################
# Desktop computing specific
###########################################
#
#{{{ GET /desktop/agents : Desktop computing agent sign in
#
$URI = qr{^/desktop/agents(.*)$};
OAR::API::GET( $_, $URI ) && do {
my $db = OAR::IO::connect() or die "cannot connect to the data base\n";
OAR::IO::lock_table($db,["event_logs"]);
my $result = OAR::IO::get_last_event_from_type($db, "NEW_VIRTUAL_HOSTNAME");
if ($result) {
$result = $result->{'description'};
$result++;
OAR::API::sign_in($result,$remote_host,$remote_port,$expiry,$allow_create_node);
OAR::IO::add_new_event($db,"NEW_VIRTUAL_HOSTNAME",0,$result);
$result = {'hostname' => $result};
} else {
OAR::API::sign_in('vnode1',$remote_host,$remote_port,$expiry,$allow_create_node);
OAR::IO::add_new_event($db,"NEW_VIRTUAL_HOSTNAME",0,'vnode1');
$result = {'hostname' => 'vnode1'};
}
OAR::IO::unlock_table($db);
# TODO: reject if DESKTOP_COMPUTING_ALLOW_CREATE_NODE="0"
print $q->header( -status => 200, -type => "application/json" );
print OAR::API::export($result,'json');
last;
};
#}}}
###########################################
# Media (files) download/upload
###########################################
#
#{{{ GET /media/ls/<path> : List files
#
$URI = qr{^/media/ls/(.*)$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $path=$1;
my $ext = OAR::API::set_ext($q,undef);
(my $header, my $type) = OAR::API::set_output_format($ext);
# Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before getting files" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_BECOME_USER} = $authenticated_user;
# Security escaping
$path =~ s/(\\*)(`|\$)/$1$1\\$2/g;
# Get the path and replace "~" by the home directory
$path="/".$path;
my @user_infos=getpwnam($authenticated_user);
$path =~ s|/~/|$user_infos[7]/|;
# Check file existency
if (system("$OARDODO_CMD","test","-d","$path") != 0) {
OAR::API::ERROR(404, "Not found", "Path not found: $path");
last;
}
# Check file readability
if (system("$OARDODO_CMD","test","-r","$path") != 0) {
OAR::API::ERROR(403, "Forbidden","File could not be read: $path" );
last;
}
# Get the listing
my $cmd="$OARDODO_CMD ls -l $path";
my $cmdRes = OAR::API::send_cmd($cmd,"ls");
my $listing=[];
my ($l_name, $l_type, $l_size, $l_mtime, $l_mode);
for (parse_dir($cmdRes)) {
($l_name, $l_type, $l_size, $l_mtime, $l_mode) = @$_;
push(@$listing,{'name' => $l_name, 'type' => $l_type, 'size' => $l_size, 'mtime' => $l_mtime, 'mode' => $l_mode });
}
print $header;
print $HTML_HEADER if ($ext eq "html");
$listing = OAR::API::add_pagination($listing,@$listing,$q->path_info,undef,$ext,0,0,$STRUCTURE);
print OAR::API::export($listing,$ext);
last;
};
#}}}
#
#{{{ GET /media/<file>?tail=<n> : Get a file (tail it to <n> lines if specified)
#
$URI = qr{^/media/(.*)$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $filename=$1;
# Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before getting files" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_BECOME_USER} = $authenticated_user;
# Security escaping
$filename =~ s/(\\*)(`|\$)/$1$1\\$2/g;
# Tail parameter
my $tail=0;
if (defined($q->param('tail')) && $q->param('tail') =~ /^\d+$/) {
$tail=$q->param('tail');
}
# Get the filename and replace "~" by the home directory
my $file="/".$filename;
my @user_infos=getpwnam($authenticated_user);
$file =~ s|/~/|$user_infos[7]/|;
# Check file existency
if (system("$OARDODO_CMD","test","-f","$file") != 0) {
OAR::API::ERROR(404, "Not found", "File not found: $file");
last;
}
# Check file readability
if (system("$OARDODO_CMD","test","-r","$file") != 0) {
OAR::API::ERROR(403, "Forbidden","File could not be read: $file" );
last;
}
# Output the file
print $q->header( -status => 200, -type => "application/octet-stream" );
if ($tail != 0) {
print `$OARDODO_CMD tail -n $tail $file`;
}else{
print `$OARDODO_CMD cat $file`;
}
last;
};
#}}}
#
#{{{ POST /media/chmod/<file>?mode=<mode> : Change the unix mode of a file
#
$URI = qr{^/media/chmod/(.*)$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $filename = $1;
# Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before deleting an admission rule" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_BECOME_USER} = $authenticated_user;
# Security escaping
$filename =~ s/(\\*)(`|\$)/$1$1\\$2/g;
# Get the filename and replace "~" by the home directory
my $file="/".$filename;
my @user_infos=getpwnam($authenticated_user);
$file =~ s|/~/|$user_infos[7]/|;
# Check and get the submitted data
# From encoded data
my $chmod;
if ($q->param('POSTDATA')) {
$chmod = OAR::API::check_chmod( $q->param('POSTDATA'), $q->content_type );
}
# From html form
else {
$chmod = OAR::API::check_chmod( $q->Vars, $q->content_type );
}
# Security checking
if ( not $chmod->{mode} =~ /^[a-z0-9+]+$/ ) {
OAR::API::ERROR( 401, "Permission denied",
"Bad mode value: ". $chmod->{mode} );
last;
}
# Do the chmod
my $cmd="$OARDODO_CMD chmod ". $chmod->{mode} ." $file";
my $cmdRes = OAR::API::send_cmd($cmd,"chmod");
if ($? != 0) {
OAR::API::ERROR(500, "Chmod error", "Could not set mode $chmod->{mode} on file $file: $cmdRes");
}
print $q->header( -status => 202, -type => "application/octet-stream" , -location => "/media/$file" );
last;
};
#}}}
#
#{{{ POST /media/<file> : Upload a file and create underlying directories
#
$URI = qr{^/media/(.*)$};
OAR::API::POST( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $filename=$1;
my $ext = OAR::API::set_ext($q,undef);
(my $header, my $type) = OAR::API::set_output_format($ext);
#Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before getting files" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_BECOME_USER} = $authenticated_user;
# For frameworks that can't set the Accept header field:
if (defined($q->param('Accept'))) {
$type=$q->param('Accept');
$ext=OAR::API::get_ext($type);
$OAR::API::extension=$ext;
}
# Security escaping
$filename =~ s/(\\*)(`|\$)/$1$1\\$2/g;
# Get the filename and replace "~" by the home directory
my $file="/".$filename;
my @user_infos=getpwnam($authenticated_user);
$file =~ s|/~/|$user_infos[7]/|;
# Check if the file already exists
if (system("$OARDODO_CMD","test","-f","$file") == 0) {
OAR::API::ERROR(403, "File already exists", "The file already exists");
last;
}
# Create the directories if necessary
my $path=dirname($file);
if (system("$OARDODO_CMD","mkdir","-p",$path) != 0) {
OAR::API::ERROR(500, "mkpath error", "Problem while creating path: $path");
last;
}
# Touch the file
if (system("$OARDODO_CMD","touch",$file) != 0) {
OAR::API::ERROR(500, "write error", "Error creating file: $file");
last;
}
# Upload file from an html form
if ($q->content_type =~ /^multipart\/form-data.*/) {
my $fh = $q->upload('file');
if (defined $fh) {
my $io_handle = $fh->handle;
my $buffer;
open (OUTFILE, "|$OARDODO_CMD bash --noprofile --norc -c \"cat > $file\"");
while (my $bytesread = $io_handle->read($buffer, 1024)) {
print OUTFILE $buffer;
}
close(OUTFILE);
}
# Upload file from a direct octet-streaam
}elsif ($q->content_type eq "application/octet-stream") {
if ($q->param('POSTDATA')) {
if (system("$OARDODO_CMD","touch",$file) != 0) {
OAR::API::ERROR(500, "write error", "Error creating file: $file");
last;
}
open (OUTFILE, "|$OARDODO_CMD bash --noprofile --norc -c \"cat > $file\"");
print OUTFILE $q->param('POSTDATA');
close(OUTFILE);
}
}else{
OAR::API::ERROR(406, "Bad content type", $q->content_type ." not allowed for file upload");
last;
}
print $q->header( -status => 201, -location => "media/$file" );
# Warning: we should do this:
#print $q->header( -status => 201, -type => $type, -location => "media/$file" );
# But ajax queries form popular browsers add <pre></pre> tags
# (http://dotclear.placeoweb.com/post/json-error-pre-tag-added-undesirable)
print $HTML_HEADER if ($ext eq "html");
# Warning: for extjs, we need to send a "success => 'true'"
print OAR::API::export( {
'status' => "created",
'success' => "true",
'links' => [ { "rel" => "self",
"href" => OAR::API::htmlize_uri(OAR::API::make_uri("media/$file",undef,0),$ext)
} ]
} , $ext );
last;
};
#}}}
#
#{{{ DELETE /media/<file> : Delete a file or a directory recursively
#
$URI = qr{^/media/(.*)$};
OAR::API::DELETE( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $filename=$1;
# Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before getting files" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_BECOME_USER} = $authenticated_user;
# Security escaping
$filename =~ s/(\\*)(`|\$)/$1$1\\$2/g;
# Get the filename and replace "~" by the home directory
my $file="/".$filename;
my @user_infos=getpwnam($authenticated_user);
$file =~ s|/~/|$user_infos[7]/|;
# Check file existency
if (system("$OARDODO_CMD","test","-e","$file") != 0) {
OAR::API::ERROR(404, "Not found", "File not found: $file");
last;
}
# Check file readability
if (system("$OARDODO_CMD","test","-w","$file") != 0) {
OAR::API::ERROR(403, "Forbidden","File or directory is not writeable: $file" );
last;
}
# Delete the file
print $q->header( -status => 204, -type => "application/octet-stream" );
print `$OARDODO_CMD rm -rf $file`;
last;
};
#}}}
###########################################
# SQL queries
###########################################
#
#{{{ GET /select_all?query=<query>) : Allows select SQL queries into the OAR database (ro)
#
$URI = qr{^/select_all\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext=OAR::API::set_ext($q,$1);
my $header, my $type;
($header, $type)=OAR::API::set_output_format($ext,"GET");
# Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before looking at jobs" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_USER} = $authenticated_user;
my $query;
# GET the query parameter from the uri
if (defined($q->param('query'))) {
$query = $q->param('query');
}else{
OAR::API::ERROR(400,"Bad query","The 'query' parameter is mandatory");
}
# GET limit from uri parameter
my $limit;
if (defined($q->param('limit'))) {
$limit = $q->param('limit');
}else{
$limit=$MAX_ITEMS;
}
# set offset / GET offset from uri parameter
my $offset = 0;
if (defined($q->param('offset'))) {
$offset = $q->param('offset');
}
# Do the query
# The query should not contain the "SELECT <something> part". Example:
# query="FROM events,jobs WHERE ..."
my $dbh = OAR::IO::connect_ro() or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my $count = OAR::IO::sql_count($dbh,$query) or OAR::IO::disconnect($dbh), OAR::API::ERROR(500,
"SQL error",
"SQL error" # <- add here the sql error output
);
my $result = OAR::IO::sql_select($dbh,$query,$limit,$offset) or OAR::IO::disconnect($dbh), OAR::API::ERROR(500,
"SQL error",
"SQL error" # <- add here the sql error output
);
#$result = OAR::API::format_select_result($result);
$result = OAR::API::add_pagination($result,$count,$q->path_info,$q->query_string,$ext,$limit,$offset,$STRUCTURE);
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export(\$result,$ext);
OAR::IO::disconnect($dbh);
last;
};
#}}}
###########################################
# Colmet
############################################
#
#{{{ GET /colmet/job/<id>?[from=timestamp]&[to=timestamp]&[metrics=m1,m2...] : Extract colmet data for the given job
#
$URI = qr{^/colmet/job/(\d+)$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $jobid = $1;
my $ext=OAR::API::set_ext($q,undef);
if ( not $ext eq "tgz" ) {
OAR::API::ERROR( 400, "Bad format",
"Colmet data is only available in compressed JSON , as application/x-gzip" );
}
(my $header, my $type)=OAR::API::set_output_format($ext,"GET");
# Must be authenticated
if ( not $authenticated_user =~ /(\w+)/ ) {
OAR::API::ERROR( 401, "Permission denied",
"A suitable authentication must be done before looking at jobs" );
last;
}
$authenticated_user = $1;
$ENV{OARDO_USER} = $authenticated_user;
OAR::Stat::open_db_connection or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
my $job = OAR::Stat::get_specific_jobs([$jobid]);
if (@$job == 0 ) {
OAR::Stat::close_db_connection;
OAR::API::ERROR( 404, "Job not found",
"Job not found" );
last;
}
OAR::Stat::close_db_connection;
my $COLMET_EXTRACT_PATH="/usr/lib/oar/colmet_extract.py";
if (is_conf("API_COLMET_EXTRACT_PATH")){ $COLMET_EXTRACT_PATH = get_conf("API_COLMET_EXTRACT_PATH"); }
my $COLMET_HDF5_PATH_PREFIX="/var/lib/colmet/hdf5/data";
if (is_conf("API_COLMET_HDF5_PATH_PREFIX")){ $COLMET_HDF5_PATH_PREFIX = get_conf("API_COLMET_HDF5_PATH_PREFIX"); }
if (not -X $COLMET_EXTRACT_PATH) {
OAR::API::ERROR( 400, "Missing extractor script",
"You have to install the colmet extraction script ($COLMET_EXTRACT_PATH), python and the h5py module.\nPlease, check https://github.com/oar-team/colmet/oar/api." );
}
my $stop_time=@$job[0]->{'stop_time'};
if ($stop_time==0) {
my $dbh = OAR::IO::connect() or OAR::API::ERROR(500,
"Cannot connect to the database",
"Cannot connect to the database"
);
$stop_time=OAR::IO::get_date($dbh);
OAR::IO::disconnect($dbh);
}
if (defined($q->param('to'))) {
my $to = $q->param('to');
if ($to < $stop_time) {
$stop_time=$to;
}
}
my $start_time=@$job[0]->{'start_time'};
if (defined($q->param('from'))) {
my $from = $q->param('from');
if ($from > $start_time) {
$start_time=$from;
}
}
my $metrics="ac_etime,cpu_run_real_total,coremem,read_bytes,write_bytes";
if (defined($q->param('metrics'))) {
$metrics=$q->param('metrics');
}
my $cmd="$COLMET_EXTRACT_PATH $COLMET_HDF5_PATH_PREFIX $jobid $start_time $stop_time $metrics";
my $cmdRes = OAR::API::send_cmd($cmd,"Oar");
print $header;
print $cmdRes;
last;
};
#}}}
###########################################
# Html stuff
###########################################
#
#{{{ GET /index : Welcome page (html only)
#
$URI = qr{^/index\.html$};
OAR::API::GET( $_, $URI ) && do {
print $q->header( -status => 200, -type => "text/html" );
print $HTML_HEADER;
print "Welcome on the oar API\n";
last;
};
#}}}
#
#{{{ GET /jobs/form : Html form for job posting
#
$URI = qr{^/jobs/form.html$};
OAR::API::GET( $_, $URI ) && do {
(my $header, my $type)=OAR::API::set_output_format("html");
print $header;
print $HTML_HEADER;
my $POSTFORM="";
my $file;
if (is_conf("API_HTML_POSTFORM")){ $file=get_conf("API_HTML_POSTFORM"); }
else { $file="/etc/oar/api_html_postform.pl"; }
open(FILE,$file);
my(@lines) = <FILE>;
eval join("\n",@lines);
close(FILE);
print $POSTFORM;
last;
};
#}}}
#
#{{{ GET /admission_rules/form : Html form for admission rules submission
#
$URI = qr{^/admission_rules/form.html$};
OAR::API::GET( $_, $URI ) && do {
(my $header, my $type)=OAR::API::set_output_format("html");
print $header;
print $HTML_HEADER;
my $POSTFORM="";
my $file = "/etc/oar/api_html_postform_rule.pl";
open(FILE,$file);
my(@lines) = <FILE>;
eval join("\n",@lines);
close(FILE);
print $POSTFORM;
last;
};
#}}}
#
#{{{ GET /resources/form : Html form for resources generation
#
$URI = qr{^/resources/form.html$};
OAR::API::GET( $_, $URI ) && do {
(my $header, my $type)=OAR::API::set_output_format("html");
print $header;
print $HTML_HEADER;
my $POSTFORM="";
my $file = "/etc/oar/api_html_postform_resources.pl";
open(FILE,$file);
my(@lines) = <FILE>;
eval join("\n",@lines);
close(FILE);
print $POSTFORM;
last;
};
#}}}
#
###########################################
# Misc
###########################################
#
#{{{ GET /stress_factor/<cluster_name> : return the stress factor of the given cluster
#
$URI = qr{^/stress_factor\.*(yaml|json|html)*$};
OAR::API::GET( $_, $URI ) && do {
$_->path_info =~ m/$URI/;
my $ext = OAR::API::set_ext($q,$1);
(my $header, my $type) = OAR::API::set_output_format($ext);
my $stress_factor_script="/etc/oar/stress_factor.sh";
if (is_conf("API_STRESS_FACTOR_SCRIPT")){ $stress_factor_script=get_conf("API_STRESS_FACTOR_SCRIPT"); }
my $cmd = "$OARDODO_CMD bash --noprofile --norc -c \"$stress_factor_script\"";
my $cmdRes = `$cmd 2>&1`;
my %output=( "api_timestamp" => time(),
"links" => [
{ 'rel' => 'self' ,
'href' => OAR::API::htmlize_uri(OAR::API::make_uri("stress_factor",$ext,0),$ext)
} ]
);
my @lines=split("\n",$cmdRes);
foreach my $line (@lines) {
if ($line =~ qr{^\s*(.+)\s*=\s*(.+)\s*}) {
my ($key,$val) = ($1,$2);
$output{$key}=$val;
}
}
print $header;
print $HTML_HEADER if ($ext eq "html");
print OAR::API::export(\%output,$ext);
last;
};
#}}}
###########################################
# Anything else -> 404
###########################################
#
OAR::API::ERROR( 404, "Not found", "No way to handle your request " . $q->path_info );
}
if ($fcgi_cycle_count > 50) {
exit 0;
}
} # End of fastcgi loop
|