/usr/lib/python3/dist-packages/consul/base.py is in python3-consul 0.7.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 | import abc
import collections
import logging
import base64
import json
import os
import six
from six.moves import urllib
log = logging.getLogger(__name__)
class ConsulException(Exception):
pass
class ACLDisabled(ConsulException):
pass
class ACLPermissionDenied(ConsulException):
pass
class NotFound(ConsulException):
pass
class Timeout(ConsulException):
pass
class BadRequest(ConsulException):
pass
#
# Convenience to define checks
class Check(object):
"""
There are three different kinds of checks: script, http and ttl
"""
@classmethod
def script(klass, script, interval):
"""
Run *script* every *interval* (e.g. "10s") to peform health check
"""
return {'script': script, 'interval': interval}
@classmethod
def http(klass, url, interval, timeout=None, deregister=None):
"""
Peform a HTTP GET against *url* every *interval* (e.g. "10s") to peform
health check with an optional *timeout* and optional *deregister* after
which a failing service will be automatically deregistered.
"""
ret = {'http': url, 'interval': interval}
if timeout:
ret['timeout'] = timeout
if deregister:
ret['DeregisterCriticalServiceAfter'] = deregister
return ret
@classmethod
def tcp(klass, host, port, interval, timeout=None, deregister=None):
"""
Attempt to establish a tcp connection to the specified *host* and
*port* at a specified *interval* with optional *timeout* and optional
*deregister* after which a failing service will be automatically
deregistered.
"""
ret = {
'tcp': '{host:s}:{port:d}'.format(host=host, port=port),
'interval': interval
}
if timeout:
ret['timeout'] = timeout
if deregister:
ret['DeregisterCriticalServiceAfter'] = deregister
return ret
@classmethod
def ttl(klass, ttl):
"""
Set check to be marked as critical after *ttl* (e.g. "10s") unless the
check is periodically marked as passing.
"""
return {'ttl': ttl}
@classmethod
def docker(klass, container_id, shell, script, interval, deregister=None):
"""
Invoke *script* packaged within a running docker container with
*container_id* at a specified *interval* on the configured
*shell* using the Docker Exec API. Optional *register* after which a
failing service will be automatically deregistered.
"""
ret = {
'docker_container_id': container_id,
'shell': shell,
'script': script,
'interval': interval
}
if deregister:
ret['DeregisterCriticalServiceAfter'] = deregister
return ret
@classmethod
def _compat(
self,
script=None,
interval=None,
ttl=None,
http=None,
timeout=None,
deregister=None):
if not script and not http and not ttl:
return {}
log.warn(
'DEPRECATED: use consul.Check.script/http/ttl to specify check')
ret = {'check': {}}
if script:
assert interval and not (ttl or http)
ret['check'] = {'script': script, 'interval': interval}
if ttl:
assert not (interval or script or http)
ret['check'] = {'ttl': ttl}
if http:
assert interval and not (script or ttl)
ret['check'] = {'http': http, 'interval': interval}
if timeout:
assert http
ret['check']['timeout'] = timeout
if deregister:
ret['check']['DeregisterCriticalServiceAfter'] = deregister
return ret
Response = collections.namedtuple('Response', ['code', 'headers', 'body'])
#
# Conveniences to create consistent callback handlers for endpoints
class CB(object):
@classmethod
def __status(klass, response, allow_404=True):
# status checking
if response.code >= 500 and response.code < 600:
raise ConsulException("%d %s" % (response.code, response.body))
if response.code == 400:
raise BadRequest('%d %s' % (response.code, response.body))
if response.code == 401:
raise ACLDisabled(response.body)
if response.code == 403:
raise ACLPermissionDenied(response.body)
if response.code == 404 and not allow_404:
raise NotFound(response.body)
@classmethod
def bool(klass):
# returns True on successful response
def cb(response):
CB.__status(response)
return response.code == 200
return cb
@classmethod
def json(
klass,
map=None,
allow_404=True,
one=False,
decode=False,
is_id=False,
index=False):
"""
*map* is a function to apply to the final result.
*allow_404* if set, None will be returned on 404, instead of raising
NotFound.
*index* if set, a tuple of index, data will be returned.
*one* returns only the first item of the list of items. empty lists are
coerced to None.
*decode* if specified this key will be base64 decoded.
*is_id* only the 'ID' field of the json object will be returned.
"""
def cb(response):
CB.__status(response, allow_404=allow_404)
if response.code == 404:
return response.headers['X-Consul-Index'], None
data = json.loads(response.body)
if decode:
for item in data:
if item.get(decode) is not None:
item[decode] = base64.b64decode(item[decode])
if is_id:
data = data['ID']
if one:
if data == []:
data = None
if data is not None:
data = data[0]
if map:
data = map(data)
if index:
return response.headers['X-Consul-Index'], data
return data
return cb
class HTTPClient(six.with_metaclass(abc.ABCMeta, object)):
def __init__(self, host='127.0.0.1', port=8500, scheme='http',
verify=True, cert=None):
self.host = host
self.port = port
self.scheme = scheme
self.verify = verify
self.base_uri = '%s://%s:%s' % (self.scheme, self.host, self.port)
self.cert = cert
def uri(self, path, params=None):
uri = urllib.parse.quote(self.base_uri + path, safe='/:')
if params:
uri = '%s?%s' % (uri, urllib.parse.urlencode(params))
return uri
@abc.abstractmethod
def get(self, callback, path, params=None):
raise NotImplementedError
@abc.abstractmethod
def put(self, callback, path, params=None, data=''):
raise NotImplementedError
@abc.abstractmethod
def delete(self, callback, path, params=None):
raise NotImplementedError
@abc.abstractmethod
def post(self, callback, path, params=None, data=''):
raise NotImplementedError
class Consul(object):
def __init__(
self,
host='127.0.0.1',
port=8500,
token=None,
scheme='http',
consistency='default',
dc=None,
verify=True,
cert=None):
"""
*token* is an optional `ACL token`_. If supplied it will be used by
default for all requests made with this client session. It's still
possible to override this token by passing a token explicitly for a
request.
*consistency* sets the consistency mode to use by default for all reads
that support the consistency option. It's still possible to override
this by passing explicitly for a given request. *consistency* can be
either 'default', 'consistent' or 'stale'.
*dc* is the datacenter that this agent will communicate with.
By default the datacenter of the host is used.
*verify* is whether to verify the SSL certificate for HTTPS requests
*cert* client side certificates for HTTPS requests
"""
# TODO: Status
if os.getenv('CONSUL_HTTP_ADDR'):
try:
host, port = os.getenv('CONSUL_HTTP_ADDR').split(':')
except ValueError:
raise ConsulException('CONSUL_HTTP_ADDR (%s) invalid, '
'does not match <host>:<port>'
% os.getenv('CONSUL_HTTP_ADDR'))
use_ssl = os.getenv('CONSUL_HTTP_SSL')
if use_ssl is not None:
scheme = 'https' if use_ssl == 'true' else 'http'
if os.getenv('CONSUL_HTTP_SSL_VERIFY') is not None:
verify = os.getenv('CONSUL_HTTP_SSL_VERIFY') == 'true'
self.http = self.connect(host, port, scheme, verify, cert)
self.token = os.getenv('CONSUL_HTTP_TOKEN', token)
self.scheme = scheme
self.dc = dc
assert consistency in ('default', 'consistent', 'stale'), \
'consistency must be either default, consistent or state'
self.consistency = consistency
self.event = Consul.Event(self)
self.kv = Consul.KV(self)
self.agent = Consul.Agent(self)
self.catalog = Consul.Catalog(self)
self.health = Consul.Health(self)
self.session = Consul.Session(self)
self.acl = Consul.ACL(self)
self.status = Consul.Status(self)
self.query = Consul.Query(self)
self.coordinate = Consul.Coordinate(self)
self.operator = Consul.Operator(self)
class Event(object):
"""
The event command provides a mechanism to fire a custom user event to
an entire datacenter. These events are opaque to Consul, but they can
be used to build scripting infrastructure to do automated deploys,
restart services, or perform any other orchestration action.
Unlike most Consul data, which is replicated using consensus, event
data is purely peer-to-peer over gossip.
This means it is not persisted and does not have a total ordering. In
practice, this means you cannot rely on the order of message delivery.
An advantage however is that events can still be used even in the
absence of server nodes or during an outage."""
def __init__(self, agent):
self.agent = agent
def fire(
self,
name,
body="",
node=None,
service=None,
tag=None,
token=None):
"""
Sends an event to Consul's gossip protocol.
*name* is the Consul-opaque name of the event. This can be filtered
on in calls to list, below
*body* is the Consul-opaque body to be delivered with the event.
From the Consul documentation:
The underlying gossip also sets limits on the size of a user
event message. It is hard to give an exact number, as it
depends on various parameters of the event, but the payload
should be kept very small (< 100 bytes). Specifying too large
of an event will return an error.
*node*, *service*, and *tag* are regular expressions which remote
agents will filter against to determine if they should store the
event
*token* is an optional `ACL token`_ to apply to this request. If
the token's policy is not allowed to fire an event of this *name*
an *ACLPermissionDenied* exception will be raised.
"""
assert not name.startswith('/'), \
'keys should not start with a forward slash'
params = {}
if node is not None:
params['node'] = node
if service is not None:
params['service'] = service
if tag is not None:
params['tag'] = tag
token = token or self.agent.token
if token:
params['token'] = token
return self.agent.http.put(
CB.json(),
'/v1/event/fire/%s' % name, params=params, data=body)
def list(
self,
name=None,
index=None,
wait=None):
"""
Returns a tuple of (*index*, *events*)
Note: Since Consul's event protocol uses gossip, there is no
ordering, and instead index maps to the newest event that
matches the query.
*name* is the type of events to list, if None, lists all available.
*index* is the current event Consul index, suitable for making
subsequent calls to wait for changes since this query was last run.
Check https://consul.io/docs/agent/http/event.html#event_list for
more infos about indexes on events.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. This parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
Consul agents only buffer the most recent entries. The current
buffer size is 256, but this value could change in the future.
Each *event* looks like this::
{
{
"ID": "b54fe110-7af5-cafc-d1fb-afc8ba432b1c",
"Name": "deploy",
"Payload": "1609030",
"NodeFilter": "",
"ServiceFilter": "",
"TagFilter": "",
"Version": 1,
"LTime": 19
},
}
"""
params = {}
if name is not None:
params['name'] = name
if index:
params['index'] = index
if wait:
params['wait'] = wait
return self.agent.http.get(
CB.json(index=True, decode='Payload'),
'/v1/event/list', params=params)
class KV(object):
"""
The KV endpoint is used to expose a simple key/value store. This can be
used to store service configurations or other meta data in a simple
way.
"""
def __init__(self, agent):
self.agent = agent
def get(
self,
key,
index=None,
recurse=False,
wait=None,
token=None,
consistency=None,
keys=False,
separator=None,
dc=None):
"""
Returns a tuple of (*index*, *value[s]*)
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
*token* is an optional `ACL token`_ to apply to this request.
*keys* is a boolean which, if True, says to return a flat list of
keys without values or other metadata. *separator* can be used
with *keys* to list keys only up to a given separator character.
*dc* is the optional datacenter that you wish to communicate with.
If None is provided, defaults to the agent's datacenter.
The *value* returned is for the specified key, or if *recurse* is
True a list of *values* for all keys with the given prefix is
returned.
Each *value* looks like this::
{
"CreateIndex": 100,
"ModifyIndex": 200,
"LockIndex": 200,
"Key": "foo",
"Flags": 0,
"Value": "bar",
"Session": "adf4238a-882b-9ddc-4a9d-5b6758e4159e"
}
Note, if the requested key does not exists *(index, None)* is
returned. It's then possible to long poll on the index for when the
key is created.
"""
assert not key.startswith('/'), \
'keys should not start with a forward slash'
params = {}
if index:
params['index'] = index
if wait:
params['wait'] = wait
if recurse:
params['recurse'] = '1'
token = token or self.agent.token
if token:
params['token'] = token
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
if keys:
params['keys'] = True
if separator:
params['separator'] = separator
consistency = consistency or self.agent.consistency
if consistency in ('consistent', 'stale'):
params[consistency] = '1'
one = False
decode = False
if not keys:
decode = 'Value'
if not recurse and not keys:
one = True
return self.agent.http.get(
CB.json(index=True, decode=decode, one=one),
'/v1/kv/%s' % key,
params=params)
def put(
self,
key,
value,
cas=None,
flags=None,
acquire=None,
release=None,
token=None,
dc=None):
"""
Sets *key* to the given *value*.
*value* can either be None (useful for marking a key as a
directory) or any string type, including binary data (e.g. a
msgpack'd data structure)
The optional *cas* parameter is used to turn the PUT into a
Check-And-Set operation. This is very useful as it allows clients
to build more complex syncronization primitives on top. If the
index is 0, then Consul will only put the key if it does not
already exist. If the index is non-zero, then the key is only set
if the index matches the ModifyIndex of that key.
An optional *flags* can be set. This can be used to specify an
unsigned value between 0 and 2^64-1.
*acquire* is an optional session_id. if supplied a lock acquisition
will be attempted.
*release* is an optional session_id. if supplied a lock release
will be attempted.
*token* is an optional `ACL token`_ to apply to this request. If
the token's policy is not allowed to write to this key an
*ACLPermissionDenied* exception will be raised.
*dc* is the optional datacenter that you wish to communicate with.
If None is provided, defaults to the agent's datacenter.
The return value is simply either True or False. If False is
returned, then the update has not taken place.
"""
assert not key.startswith('/'), \
'keys should not start with a forward slash'
assert value is None or \
isinstance(value, (six.string_types, six.binary_type)), \
"value should be None or a string / binary data"
params = {}
if cas is not None:
params['cas'] = cas
if flags is not None:
params['flags'] = flags
if acquire:
params['acquire'] = acquire
if release:
params['release'] = release
token = token or self.agent.token
if token:
params['token'] = token
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
return self.agent.http.put(
CB.json(), '/v1/kv/%s' % key, params=params, data=value)
def delete(self, key, recurse=None, cas=None, token=None, dc=None):
"""
Deletes a single key or if *recurse* is True, all keys sharing a
prefix.
*cas* is an optional flag is used to turn the DELETE into a
Check-And-Set operation. This is very useful as a building block
for more complex synchronization primitives. Unlike PUT, the index
must be greater than 0 for Consul to take any action: a 0 index
will not delete the key. If the index is non-zero, the key is only
deleted if the index matches the ModifyIndex of that key.
*token* is an optional `ACL token`_ to apply to this request. If
the token's policy is not allowed to delete to this key an
*ACLPermissionDenied* exception will be raised.
*dc* is the optional datacenter that you wish to communicate with.
If None is provided, defaults to the agent's datacenter.
"""
assert not key.startswith('/'), \
'keys should not start with a forward slash'
params = {}
if recurse:
params['recurse'] = '1'
if cas is not None:
params['cas'] = cas
token = token or self.agent.token
if token:
params['token'] = token
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
return self.agent.http.delete(
CB.json(), '/v1/kv/%s' % key, params=params)
class Agent(object):
"""
The Agent endpoints are used to interact with a local Consul agent.
Usually, services and checks are registered with an agent, which then
takes on the burden of registering with the Catalog and performing
anti-entropy to recover from outages.
"""
def __init__(self, agent):
self.agent = agent
self.service = Consul.Agent.Service(agent)
self.check = Consul.Agent.Check(agent)
def self(self):
"""
Returns configuration of the local agent and member information.
"""
return self.agent.http.get(CB.json(), '/v1/agent/self')
def services(self):
"""
Returns all the services that are registered with the local agent.
These services were either provided through configuration files, or
added dynamically using the HTTP API. It is important to note that
the services known by the agent may be different than those
reported by the Catalog. This is usually due to changes being made
while there is no leader elected. The agent performs active
anti-entropy, so in most situations everything will be in sync
within a few seconds.
"""
return self.agent.http.get(CB.json(), '/v1/agent/services')
def checks(self):
"""
Returns all the checks that are registered with the local agent.
These checks were either provided through configuration files, or
added dynamically using the HTTP API. Similar to services,
the checks known by the agent may be different than those
reported by the Catalog. This is usually due to changes being made
while there is no leader elected. The agent performs active
anti-entropy, so in most situations everything will be in sync
within a few seconds.
"""
return self.agent.http.get(CB.json(), '/v1/agent/checks')
def members(self, wan=False):
"""
Returns all the members that this agent currently sees. This may
vary by agent, use the nodes api of Catalog to retrieve a cluster
wide consistent view of members.
For agents running in server mode, setting *wan* to *True* returns
the list of WAN members instead of the LAN members which is
default.
"""
params = {}
if wan:
params['wan'] = 1
return self.agent.http.get(
CB.json(), '/v1/agent/members', params=params)
def maintenance(self, enable, reason=None):
"""
The node maintenance endpoint can place the agent into
"maintenance mode".
*enable* is either 'true' or 'false'. 'true' enables maintenance
mode, 'false' disables maintenance mode.
*reason* is an optional string. This is simply to aid human
operators.
"""
params = {}
params['enable'] = enable
if reason:
params['reason'] = reason
return self.agent.http.put(
CB.bool(), '/v1/agent/maintenance', params=params)
def join(self, address, wan=False):
"""
This endpoint instructs the agent to attempt to connect to a
given address.
*address* is the ip to connect to.
*wan* is either 'true' or 'false'. For agents running in server
mode, 'true' causes the agent to attempt to join using the WAN
pool. Default is 'false'.
"""
params = {}
if wan:
params['wan'] = 1
return self.agent.http.get(
CB.bool(), '/v1/agent/join/%s' % address, params=params)
def force_leave(self, node):
"""
This endpoint instructs the agent to force a node into the left
state. If a node fails unexpectedly, then it will be in a failed
state. Once in the failed state, Consul will attempt to reconnect,
and the services and checks belonging to that node will not be
cleaned up. Forcing a node into the left state allows its old
entries to be removed.
*node* is the node to change state for.
"""
return self.agent.http.get(
CB.bool(), '/v1/agent/force-leave/%s' % node)
class Service(object):
def __init__(self, agent):
self.agent = agent
def register(
self,
name,
service_id=None,
address=None,
port=None,
tags=None,
check=None,
token=None,
# *deprecated* use check parameter
script=None,
interval=None,
ttl=None,
http=None,
timeout=None,
enable_tag_override=False):
"""
Add a new service to the local agent. There is more
documentation on services
`here <http://www.consul.io/docs/agent/services.html>`_.
*name* is the name of the service.
If the optional *service_id* is not provided it is set to
*name*. You cannot have duplicate *service_id* entries per
agent, so it may be necessary to provide one.
*address* will default to the address of the agent if not
provided.
An optional health *check* can be created for this service is
one of `Check.script`_, `Check.http`_, `Check.tcp`_,
`Check.ttl`_ or `Check.docker`_.
*token* is an optional `ACL token`_ to apply to this request.
Note this call will return successful even if the token doesn't
have permissions to register this service.
*script*, *interval*, *ttl*, *http*, and *timeout* arguments
are deprecated. use *check* instead.
*enable_tag_override* is an optional bool that enable you
to modify a service tags from servers(consul agent role server)
Default is set to False.
This option is only for >=v0.6.0 version on both agent and
servers.
for more information
https://www.consul.io/docs/agent/services.html
"""
payload = {}
payload['name'] = name
if enable_tag_override:
payload['enabletagoverride'] = enable_tag_override
if service_id:
payload['id'] = service_id
if address:
payload['address'] = address
if port:
payload['port'] = port
if tags:
payload['tags'] = tags
if check:
payload['check'] = check
else:
payload.update(Check._compat(
script=script,
interval=interval,
ttl=ttl,
http=http,
timeout=timeout))
params = {}
token = token or self.agent.token
if token:
params['token'] = token
return self.agent.http.put(
CB.bool(),
'/v1/agent/service/register',
params=params,
data=json.dumps(payload))
def deregister(self, service_id):
"""
Used to remove a service from the local agent. The agent will
take care of deregistering the service with the Catalog. If
there is an associated check, that is also deregistered.
"""
return self.agent.http.get(
CB.bool(), '/v1/agent/service/deregister/%s' % service_id)
def maintenance(self, service_id, enable, reason=None):
"""
The service maintenance endpoint allows placing a given service
into "maintenance mode".
*service_id* is the id of the service that is to be targeted
for maintenance.
*enable* is either 'true' or 'false'. 'true' enables
maintenance mode, 'false' disables maintenance mode.
*reason* is an optional string. This is simply to aid human
operators.
"""
params = {}
params['enable'] = enable
if reason:
params['reason'] = reason
return self.agent.http.put(
CB.bool(),
'/v1/agent/service/maintenance/{0}'.format(service_id),
params=params)
class Check(object):
def __init__(self, agent):
self.agent = agent
def register(
self,
name,
check=None,
check_id=None,
notes=None,
service_id=None,
token=None,
# *deprecated* use check parameter
script=None,
interval=None,
ttl=None,
http=None,
timeout=None):
"""
Register a new check with the local agent. More documentation
on checks can be found `here
<http://www.consul.io/docs/agent/checks.html>`_.
*name* is the name of the check.
*check* is one of `Check.script`_, `Check.http`_, `Check.tcp`_
`Check.ttl`_ or `Check.docker`_ and is required.
If the optional *check_id* is not provided it is set to *name*.
*check_id* must be unique for this agent.
*notes* is not used by Consul, and is meant to be human
readable.
Optionally, a *service_id* can be specified to associate a
registered check with an existing service.
*token* is an optional `ACL token`_ to apply to this request.
Note this call will return successful even if the token doesn't
have permissions to register this check.
*script*, *interval*, *ttl*, *http*, and *timeout* arguments
are deprecated. use *check* instead.
Returns *True* on success.
"""
payload = {'name': name}
assert check or script or ttl or http, \
'check is required'
if check:
payload.update(check)
else:
payload.update(Check._compat(
script=script,
interval=interval,
ttl=ttl,
http=http,
timeout=timeout)['check'])
if check_id:
payload['id'] = check_id
if notes:
payload['notes'] = notes
if service_id:
payload['serviceid'] = service_id
params = {}
token = token or self.agent.token
if token:
params['token'] = token
return self.agent.http.put(
CB.bool(),
'/v1/agent/check/register',
params=params,
data=json.dumps(payload))
def deregister(self, check_id):
"""
Remove a check from the local agent.
"""
return self.agent.http.get(
CB.bool(),
'/v1/agent/check/deregister/%s' % check_id)
def ttl_pass(self, check_id, notes=None):
"""
Mark a ttl based check as passing. Optional notes can be
attached to describe the status of the check.
"""
params = {}
if notes:
params['note'] = notes
return self.agent.http.get(
CB.bool(),
'/v1/agent/check/pass/%s' % check_id,
params=params)
def ttl_fail(self, check_id, notes=None):
"""
Mark a ttl based check as failing. Optional notes can be
attached to describe why check is failing. The status of the
check will be set to critical and the ttl clock will be reset.
"""
params = {}
if notes:
params['note'] = notes
return self.agent.http.get(
CB.bool(),
'/v1/agent/check/fail/%s' % check_id,
params=params)
def ttl_warn(self, check_id, notes=None):
"""
Mark a ttl based check with warning. Optional notes can be
attached to describe the warning. The status of the
check will be set to warn and the ttl clock will be reset.
"""
params = {}
if notes:
params['note'] = notes
return self.agent.http.get(
CB.bool(),
'/v1/agent/check/warn/%s' % check_id,
params=params)
class Catalog(object):
def __init__(self, agent):
self.agent = agent
def register(self,
node,
address,
service=None,
check=None,
dc=None,
token=None):
"""
A low level mechanism for directly registering or updating entries
in the catalog. It is usually recommended to use
agent.service.register and agent.check.register, as they are
simpler and perform anti-entropy.
*node* is the name of the node to register.
*address* is the ip of the node.
*service* is an optional service to register. if supplied this is a
dict::
{
"Service": "redis",
"ID": "redis1",
"Tags": [
"master",
"v1"
],
"Port": 8000
}
where
*Service* is required and is the name of the service
*ID* is optional, and will be set to *Service* if not provided.
Note *ID* must be unique for the given *node*.
*Tags* and *Port* are optional.
*check* is an optional check to register. if supplied this is a
dict::
{
"Node": "foobar",
"CheckID": "service:redis1",
"Name": "Redis health check",
"Notes": "Script based health check",
"Status": "passing",
"ServiceID": "redis1"
}
*dc* is the datacenter of the node and defaults to this agents
datacenter.
*token* is an optional `ACL token`_ to apply to this request.
This manipulates the health check entry, but does not setup a
script or TTL to actually update the status. The full documentation
is `here <https://consul.io/docs/agent/http.html#catalog>`_.
Returns *True* on success.
"""
data = {'node': node, 'address': address}
params = {}
dc = dc or self.agent.dc
if dc:
data['datacenter'] = dc
if service:
data['service'] = service
if check:
data['check'] = check
token = token or self.agent.token
if token:
data['WriteRequest'] = {'Token': token}
params['token'] = token
return self.agent.http.put(
CB.bool(),
'/v1/catalog/register',
data=json.dumps(data),
params=params)
def deregister(self,
node,
service_id=None,
check_id=None,
dc=None,
token=None):
"""
A low level mechanism for directly removing entries in the catalog.
It is usually recommended to use the agent APIs, as they are
simpler and perform anti-entropy.
*node* and *dc* specify which node on which datacenter to remove.
If *service_id* and *check_id* are not provided, all associated
services and checks are deleted. Otherwise only one of *service_id*
and *check_id* should be provided and only that service or check
will be removed.
*token* is an optional `ACL token`_ to apply to this request.
Returns *True* on success.
"""
assert not (service_id and check_id)
data = {'node': node}
dc = dc or self.agent.dc
if dc:
data['datacenter'] = dc
if service_id:
data['serviceid'] = service_id
if check_id:
data['checkid'] = check_id
token = token or self.agent.token
if token:
data['WriteRequest'] = {'Token': token}
return self.agent.http.put(
CB.bool(), '/v1/catalog/deregister', data=json.dumps(data))
def datacenters(self):
"""
Returns all the datacenters that are known by the Consul server.
"""
return self.agent.http.get(
CB.json(), '/v1/catalog/datacenters')
def nodes(
self,
index=None,
wait=None,
consistency=None,
dc=None,
near=None,
token=None):
"""
Returns a tuple of (*index*, *nodes*) of all nodes known
about in the *dc* datacenter. *dc* defaults to the current
datacenter of this agent.
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
*near* is a node name to sort the resulting list in ascending
order based on the estimated round trip time from that node
*consistency* can be either 'default', 'consistent' or 'stale'. if
not specified *consistency* will the consistency level this client
was configured with.
*token* is an optional `ACL token`_ to apply to this request.
The response looks like this::
(index, [
{
"Node": "baz",
"Address": "10.1.10.11"
},
{
"Node": "foobar",
"Address": "10.1.10.12"
}
])
"""
params = {}
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
if index:
params['index'] = index
if wait:
params['wait'] = wait
if near:
params['near'] = near
token = token or self.agent.token
if token:
params['token'] = token
consistency = consistency or self.agent.consistency
if consistency in ('consistent', 'stale'):
params[consistency] = '1'
return self.agent.http.get(
CB.json(index=True), '/v1/catalog/nodes', params=params)
def services(self,
index=None,
wait=None,
consistency=None,
dc=None,
token=None):
"""
Returns a tuple of (*index*, *services*) of all services known
about in the *dc* datacenter. *dc* defaults to the current
datacenter of this agent.
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
*consistency* can be either 'default', 'consistent' or 'stale'. if
not specified *consistency* will the consistency level this client
was configured with.
*token* is an optional `ACL token`_ to apply to this request.
The response looks like this::
(index, {
"consul": [],
"redis": [],
"postgresql": [
"master",
"slave"
]
})
The main keys are the service names and the list provides all the
known tags for a given service.
"""
params = {}
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
if index:
params['index'] = index
if wait:
params['wait'] = wait
token = token or self.agent.token
if token:
params['token'] = token
consistency = consistency or self.agent.consistency
if consistency in ('consistent', 'stale'):
params[consistency] = '1'
return self.agent.http.get(
CB.json(index=True), '/v1/catalog/services', params=params)
def node(self,
node,
index=None,
wait=None,
consistency=None,
dc=None,
token=None):
"""
Returns a tuple of (*index*, *services*) of all services provided
by *node*.
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
*consistency* can be either 'default', 'consistent' or 'stale'. if
not specified *consistency* will the consistency level this client
was configured with.
*dc* is the datacenter of the node and defaults to this agents
datacenter.
*token* is an optional `ACL token`_ to apply to this request.
The response looks like this::
(index, {
"Node": {
"Node": "foobar",
"Address": "10.1.10.12"
},
"Services": {
"consul": {
"ID": "consul",
"Service": "consul",
"Tags": null,
"Port": 8300
},
"redis": {
"ID": "redis",
"Service": "redis",
"Tags": [
"v1"
],
"Port": 8000
}
}
})
"""
params = {}
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
if index:
params['index'] = index
if wait:
params['wait'] = wait
token = token or self.agent.token
if token:
params['token'] = token
consistency = consistency or self.agent.consistency
if consistency in ('consistent', 'stale'):
params[consistency] = '1'
return self.agent.http.get(
CB.json(index=True),
'/v1/catalog/node/%s' % node,
params=params)
def service(
self,
service,
index=None,
wait=None,
tag=None,
consistency=None,
dc=None,
near=None,
token=None):
"""
Returns a tuple of (*index*, *nodes*) of the nodes providing
*service* in the *dc* datacenter. *dc* defaults to the current
datacenter of this agent.
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
If *tag* is provided, the list of nodes returned will be filtered
by that tag.
*near* is a node name to sort the resulting list in ascending
order based on the estimated round trip time from that node
*consistency* can be either 'default', 'consistent' or 'stale'. if
not specified *consistency* will the consistency level this client
was configured with.
*token* is an optional `ACL token`_ to apply to this request.
The response looks like this::
(index, [
{
"Node": "foobar",
"Address": "10.1.10.12",
"ServiceID": "redis",
"ServiceName": "redis",
"ServiceTags": null,
"ServicePort": 8000
}
])
"""
params = {}
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
if tag:
params['tag'] = tag
if index:
params['index'] = index
if wait:
params['wait'] = wait
if near:
params['near'] = near
token = token or self.agent.token
if token:
params['token'] = token
consistency = consistency or self.agent.consistency
if consistency in ('consistent', 'stale'):
params[consistency] = '1'
return self.agent.http.get(
CB.json(index=True),
'/v1/catalog/service/%s' % service,
params=params)
class Health(object):
# TODO: All of the health endpoints support all consistency modes
def __init__(self, agent):
self.agent = agent
def service(self,
service,
index=None,
wait=None,
passing=None,
tag=None,
dc=None,
near=None,
token=None):
"""
Returns a tuple of (*index*, *nodes*)
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
*nodes* are the nodes providing the given service.
Calling with *passing* set to True will filter results to only
those nodes whose checks are currently passing.
Calling with *tag* will filter the results by tag.
*dc* is the datacenter of the node and defaults to this agents
datacenter.
*near* is a node name to sort the resulting list in ascending
order based on the estimated round trip time from that node
*token* is an optional `ACL token`_ to apply to this request.
"""
params = {}
if index:
params['index'] = index
if wait:
params['wait'] = wait
if passing:
params['passing'] = '1'
if tag is not None:
params['tag'] = tag
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
if near:
params['near'] = near
token = token or self.agent.token
if token:
params['token'] = token
return self.agent.http.get(
CB.json(index=True),
'/v1/health/service/%s' % service,
params=params)
def checks(
self,
service,
index=None,
wait=None,
dc=None,
near=None,
token=None):
"""
Returns a tuple of (*index*, *checks*) with *checks* being the
checks associated with the service.
*service* is the name of the service being checked.
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
*dc* is the datacenter of the node and defaults to this agents
datacenter.
*near* is a node name to sort the resulting list in ascending
order based on the estimated round trip time from that node
*token* is an optional `ACL token`_ to apply to this request.
"""
params = {}
if index:
params['index'] = index
if wait:
params['wait'] = wait
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
if near:
params['near'] = near
token = token or self.agent.token
if token:
params['token'] = token
return self.agent.http.get(
CB.json(index=True),
'/v1/health/checks/%s' % service,
params=params)
def state(self,
name,
index=None,
wait=None,
dc=None,
near=None,
token=None):
"""
Returns a tuple of (*index*, *nodes*)
*name* is a supported state. From the Consul docs:
The supported states are any, unknown, passing, warning, or
critical. The any state is a wildcard that can be used to
return all checks.
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
*dc* is the datacenter of the node and defaults to this agents
datacenter.
*near* is a node name to sort the resulting list in ascending
order based on the estimated round trip time from that node
*token* is an optional `ACL token`_ to apply to this request.
*nodes* are the nodes providing the given service.
"""
assert name in ['any', 'unknown', 'passing', 'warning', 'critical']
params = {}
if index:
params['index'] = index
if wait:
params['wait'] = wait
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
if near:
params['near'] = near
token = token or self.agent.token
if token:
params['token'] = token
return self.agent.http.get(
CB.json(index=True),
'/v1/health/state/%s' % name,
params=params)
def node(self, node, index=None, wait=None, dc=None, token=None):
"""
Returns a tuple of (*index*, *checks*)
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
*dc* is the datacenter of the node and defaults to this agents
datacenter.
*token* is an optional `ACL token`_ to apply to this request.
*nodes* are the nodes providing the given service.
"""
params = {}
if index:
params['index'] = index
if wait:
params['wait'] = wait
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
token = token or self.agent.token
if token:
params['token'] = token
return self.agent.http.get(
CB.json(index=True),
'/v1/health/node/%s' % node,
params=params)
class Session(object):
def __init__(self, agent):
self.agent = agent
def create(
self,
name=None,
node=None,
checks=None,
lock_delay=15,
behavior='release',
ttl=None,
dc=None):
"""
Creates a new session. There is more documentation for sessions
`here <https://consul.io/docs/internals/sessions.html>`_.
*name* is an optional human readable name for the session.
*node* is the node to create the session on. if not provided the
current agent's node will be used.
*checks* is a list of checks to associate with the session. if not
provided it defaults to the *serfHealth* check. It is highly
recommended that, if you override this list, you include the
default *serfHealth*.
*lock_delay* is an integer of seconds.
*behavior* can be set to either 'release' or 'delete'. This
controls the behavior when a session is invalidated. By default,
this is 'release', causing any locks that are held to be released.
Changing this to 'delete' causes any locks that are held to be
deleted. 'delete' is useful for creating ephemeral key/value
entries.
when *ttl* is provided, the session is invalidated if it is not
renewed before the TTL expires. If specified, it is an integer of
seconds. Currently it must be between 10 and 86400 seconds.
By default the session will be created in the current datacenter
but an optional *dc* can be provided.
Returns the string *session_id* for the session.
"""
params = {}
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
data = {}
if name:
data['name'] = name
if node:
data['node'] = node
if checks is not None:
data['checks'] = checks
if lock_delay != 15:
data['lockdelay'] = '%ss' % lock_delay
assert behavior in ('release', 'delete'), \
'behavior must be release or delete'
if behavior != 'release':
data['behavior'] = behavior
if ttl:
assert 10 <= ttl <= 86400
data['ttl'] = '%ss' % ttl
if data:
data = json.dumps(data)
else:
data = ''
return self.agent.http.put(
CB.json(is_id=True),
'/v1/session/create',
params=params,
data=data)
def destroy(self, session_id, dc=None):
"""
Destroys the session *session_id*
Returns *True* on success.
"""
params = {}
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
return self.agent.http.put(
CB.bool(),
'/v1/session/destroy/%s' % session_id,
params=params)
def list(self, index=None, wait=None, consistency=None, dc=None):
"""
Returns a tuple of (*index*, *sessions*) of all active sessions in
the *dc* datacenter. *dc* defaults to the current datacenter of
this agent.
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
*consistency* can be either 'default', 'consistent' or 'stale'. if
not specified *consistency* will the consistency level this client
was configured with.
The response looks like this::
(index, [
{
"LockDelay": 1.5e+10,
"Checks": [
"serfHealth"
],
"Node": "foobar",
"ID": "adf4238a-882b-9ddc-4a9d-5b6758e4159e",
"CreateIndex": 1086449
},
...
])
"""
params = {}
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
if index:
params['index'] = index
if wait:
params['wait'] = wait
consistency = consistency or self.agent.consistency
if consistency in ('consistent', 'stale'):
params[consistency] = '1'
return self.agent.http.get(
CB.json(index=True), '/v1/session/list', params=params)
def node(self, node, index=None, wait=None, consistency=None, dc=None):
"""
Returns a tuple of (*index*, *sessions*) as per *session.list*, but
filters the sessions returned to only those active for *node*.
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
*consistency* can be either 'default', 'consistent' or 'stale'. if
not specified *consistency* will the consistency level this client
was configured with.
"""
params = {}
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
if index:
params['index'] = index
if wait:
params['wait'] = wait
consistency = consistency or self.agent.consistency
if consistency in ('consistent', 'stale'):
params[consistency] = '1'
return self.agent.http.get(
CB.json(index=True),
'/v1/session/node/%s' % node, params=params)
def info(self,
session_id,
index=None,
wait=None,
consistency=None,
dc=None):
"""
Returns a tuple of (*index*, *session*) for the session
*session_id* in the *dc* datacenter. *dc* defaults to the current
datacenter of this agent.
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
*consistency* can be either 'default', 'consistent' or 'stale'. if
not specified *consistency* will the consistency level this client
was configured with.
"""
params = {}
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
if index:
params['index'] = index
if wait:
params['wait'] = wait
consistency = consistency or self.agent.consistency
if consistency in ('consistent', 'stale'):
params[consistency] = '1'
return self.agent.http.get(
CB.json(index=True, one=True),
'/v1/session/info/%s' % session_id,
params=params)
def renew(self, session_id, dc=None):
"""
This is used with sessions that have a TTL, and it extends the
expiration by the TTL.
*dc* is the optional datacenter that you wish to communicate with.
If None is provided, defaults to the agent's datacenter.
Returns the session.
"""
params = {}
dc = dc or self.agent.dc
if dc:
params['dc'] = dc
return self.agent.http.put(
CB.json(one=True, allow_404=False),
'/v1/session/renew/%s' % session_id,
params=params)
class ACL(object):
def __init__(self, agent):
self.agent = agent
def list(self, token=None):
"""
Lists all the active ACL tokens. This is a privileged endpoint, and
requires a management token. *token* will override this client's
default token. An *ACLPermissionDenied* exception will be raised
if a management token is not used.
"""
params = {}
token = token or self.agent.token
if token:
params['token'] = token
return self.agent.http.get(
CB.json(), '/v1/acl/list', params=params)
def info(self, acl_id, token=None):
"""
Returns the token information for *acl_id*.
"""
params = {}
token = token or self.agent.token
if token:
params['token'] = token
return self.agent.http.get(
CB.json(one=True), '/v1/acl/info/%s' % acl_id, params=params)
def create(self,
name=None,
type='client',
rules=None,
acl_id=None,
token=None):
"""
Creates a new ACL token. This is a privileged endpoint, and
requires a management token. *token* will override this client's
default token. An *ACLPermissionDenied* exception will be raised
if a management token is not used.
*name* is an optional name for this token.
*type* is either 'management' or 'client'. A management token is
effectively like a root user, and has the ability to perform any
action including creating, modifying, and deleting ACLs. A client
token can only perform actions as permitted by *rules*.
*rules* is an optional `HCL`_ string for this `ACL Token`_ Rule
Specification.
Rules look like this::
# Default all keys to read-only
key "" {
policy = "read"
}
key "foo/" {
policy = "write"
}
key "foo/private/" {
# Deny access to the private dir
policy = "deny"
}
Returns the string *acl_id* for the new token.
"""
params = {}
token = token or self.agent.token
if token:
params['token'] = token
payload = {}
if name:
payload['Name'] = name
if type:
assert type in ('client', 'management'), \
'type must be client or management'
payload['Type'] = type
if rules:
assert isinstance(rules, str), \
'Only HCL or JSON encoded strings supported for the moment'
payload['Rules'] = rules
if acl_id:
payload['ID'] = acl_id
if payload:
data = json.dumps(payload)
else:
data = ''
return self.agent.http.put(
CB.json(is_id=True),
'/v1/acl/create',
params=params,
data=data)
def update(self, acl_id, name=None, type=None, rules=None, token=None):
"""
Updates the ACL token *acl_id*. This is a privileged endpoint, and
requires a management token. *token* will override this client's
default token. An *ACLPermissionDenied* exception will be raised if
a management token is not used.
*name* is an optional name for this token.
*type* is either 'management' or 'client'. A management token is
effectively like a root user, and has the ability to perform any
action including creating, modifying, and deleting ACLs. A client
token can only perform actions as permitted by *rules*.
*rules* is an optional `HCL`_ string for this `ACL Token`_ Rule
Specification.
Returns the string *acl_id* of this token on success.
"""
params = {}
token = token or self.agent.token
if token:
params['token'] = token
payload = {'ID': acl_id}
if name:
payload['Name'] = name
if type:
assert type in ('client', 'management'), \
'type must be client or management'
payload['Type'] = type
if rules:
assert isinstance(rules, str), \
'Only HCL or JSON encoded strings supported for the moment'
payload['Rules'] = rules
data = json.dumps(payload)
return self.agent.http.put(
CB.json(is_id=True),
'/v1/acl/update',
params=params,
data=data)
def clone(self, acl_id, token=None):
"""
Clones the ACL token *acl_id*. This is a privileged endpoint, and
requires a management token. *token* will override this client's
default token. An *ACLPermissionDenied* exception will be raised if
a management token is not used.
Returns the string of the newly created *acl_id*.
"""
params = {}
token = token or self.agent.token
if token:
params['token'] = token
return self.agent.http.put(
CB.json(is_id=True),
'/v1/acl/clone/%s' % acl_id,
params=params)
def destroy(self, acl_id, token=None):
"""
Destroys the ACL token *acl_id*. This is a privileged endpoint, and
requires a management token. *token* will override this client's
default token. An *ACLPermissionDenied* exception will be raised if
a management token is not used.
Returns *True* on success.
"""
params = {}
token = token or self.agent.token
if token:
params['token'] = token
return self.agent.http.put(
CB.json(),
'/v1/acl/destroy/%s' % acl_id,
params=params)
class Status(object):
"""
The Status endpoints are used to get information about the status
of the Consul cluster.
"""
def __init__(self, agent):
self.agent = agent
def leader(self):
"""
This endpoint is used to get the Raft leader for the datacenter
in which the agent is running.
"""
return self.agent.http.get(CB.json(), '/v1/status/leader')
def peers(self):
"""
This endpoint retrieves the Raft peers for the datacenter in which
the the agent is running.
"""
return self.agent.http.get(CB.json(), '/v1/status/peers')
class Query(object):
def __init__(self, agent):
self.agent = agent
def list(self, dc=None, token=None):
"""
Lists all the active queries. This is a privileged endpoint,
therefore you will only be able to get the prepared queries
which the token supplied has read privileges to.
*dc* is the datacenter that this agent will communicate with. By
default the datacenter of the host is used.
*token* is an optional `ACL token`_ to apply to this request.
"""
params = {}
token = token or self.agent.token
if token:
params['token'] = token
if dc:
params['dc'] = dc
return self.agent.http.get(CB.json(), '/v1/query', params=params)
def _query_data(self, service=None,
name=None,
session=None,
token=None,
nearestn=None,
datacenters=None,
onlypassing=None,
tags=None,
ttl=None,
regexp=None):
service_body = dict([
(k, v) for k, v in {
'service': service,
'onlypassing': onlypassing,
'tags': tags,
'failover': dict([
(k, v) for k, v in {
'nearestn': nearestn,
'datacenters': datacenters
}.items() if v is not None
])
}.items() if v is not None
])
data = dict([
(k, v) for k, v in {
'name': name,
'session': session,
'token': token or self.agent.token,
'dns': {
'ttl': ttl
} if ttl is not None else None,
'template': dict([
(k, v) for k, v in {
'type': 'name_prefix_match',
'regexp': regexp
}.items() if v is not None
]),
'service': service_body
}.items() if v is not None
])
return json.dumps(data)
def create(self, service,
name=None,
dc=None,
session=None,
token=None,
nearestn=None,
datacenters=None,
onlypassing=None,
tags=None,
ttl=None,
regexp=None):
"""
Creates a new query. This is a privileged endpoint, and
requires a management token for a certain query name.*token* will
override this client's default token.
*service* is mandatory for new query. represent service name to
query.
*name* is an optional name for this query.
*dc* is the datacenter that this agent will communicate with. By
default the datacenter of the host is used.
*token* is an optional `ACL token`_ to apply to this request.
*nearestn* if set to a value greater than zero, then the query will
be forwarded to up to NearestN other datacenters based on their
estimated network round trip time using Network Coordinates from
the WAN gossip pool.
*datacenters* is a fixed list of remote datacenters to forward the
query to if there are no healthy nodes in the local datacenter.
*onlypassing* controls the behavior of the query's health check
filtering.
*tags* is a list of service tags to filter the query results.
*ttl* is a duration string that can use "s" as a suffix for
seconds. It controls how the TTL is set when query results are
served over DNS.
*regexp* is optional for template this option is only supported
in Consul 0.6.4 or later. The only option for type is
name_prefix_match so if you want a query template with no regexp
enter an empty string.
For more information about query
https://www.consul.io/docs/agent/http/query.html
"""
path = '/v1/query'
params = None if dc is None else {'dc': dc}
data = self._query_data(
service, name, session, token, nearestn, datacenters,
onlypassing, tags, ttl, regexp
)
return self.agent.http.post(
CB.json(), path, params=params, data=data)
def update(self, query_id,
service=None,
name=None,
dc=None,
session=None,
token=None,
nearestn=None,
datacenters=None,
onlypassing=None,
tags=None,
ttl=None,
regexp=None):
"""
This endpoint will update a certain query
*query_id* is the query id for update
all the other setting remains the same as the query create method
"""
path = '/v1/query/%s' % query_id
params = None if dc is None else {'dc': dc}
data = self._query_data(
service, name, session, token, nearestn, datacenters,
onlypassing, tags, ttl, regexp
)
return self.agent.http.put(
CB.bool(), path, params=params, data=data)
def get(self,
query_id,
token=None,
dc=None):
"""
This endpoint will return information about a certain query
*query_id* the query id to retrieve information about
*token* is an optional `ACL token`_ to apply to this request.
*dc* is the datacenter that this agent will communicate with. By
default the datacenter of the host is used.
"""
params = {}
token = token or self.agent.token
if token:
params['token'] = token
if dc:
params['dc'] = dc
return self.agent.http.get(
CB.json(), '/v1/query/%s' % query_id, params=params)
def delete(self, query_id, token=None, dc=None):
"""
This endpoint will delete certain query
*query_id* the query id delete
*token* is an optional `ACL token`_ to apply to this request.
*dc* is the datacenter that this agent will communicate with. By
default the datacenter of the host is used.
"""
params = {}
token = token or self.agent.token
if token:
params['token'] = token
if dc:
params['dc'] = dc
return self.agent.http.delete(
CB.bool(), '/v1/query/%s' % query_id, params=params)
def execute(self,
query,
token=None,
dc=None,
near=None,
limit=None):
"""
This endpoint will execute certain query
*query* name or query id to execute
*token* is an optional `ACL token`_ to apply to this request.
*dc* is the datacenter that this agent will communicate with. By
default the datacenter of the host is used.
*near* is a node name to sort the resulting list in ascending
order based on the estimated round trip time from that node
*limit* is used to limit the size of the list to the given number
of nodes. This is applied after any sorting or shuffling.
"""
params = {}
token = token or self.agent.token
if token:
params['token'] = token
if dc:
params['dc'] = dc
if near:
params['near'] = near
if limit:
params['limit'] = limit
return self.agent.http.get(
CB.json(), '/v1/query/%s/execute' % query, params=params)
def explain(self,
query,
token=None,
dc=None):
"""
This endpoint shows a fully-rendered query for a given name
*query* name to explain. This cannot be query id.
*token* is an optional `ACL token`_ to apply to this request.
*dc* is the datacenter that this agent will communicate with. By
default the datacenter of the host is used.
"""
params = {}
token = token or self.agent.token
if token:
params['token'] = token
if dc:
params['dc'] = dc
return self.agent.http.get(
CB.json(), '/v1/query/%s/explain' % query, params=params)
class Coordinate(object):
def __init__(self, agent):
self.agent = agent
def datacenters(self):
"""
Returns the WAN network coordinates for all Consul servers,
organized by DCs.
"""
return self.agent.http.get(CB.json(), '/v1/coordinate/datacenters')
def nodes(self, dc=None, index=None, wait=None, consistency=None):
"""
*dc* is the datacenter that this agent will communicate with. By
default the datacenter of the host is used.
*index* is the current Consul index, suitable for making subsequent
calls to wait for changes since this query was last run.
*wait* the maximum duration to wait (e.g. '10s') to retrieve
a given index. this parameter is only applied if *index* is also
specified. the wait time by default is 5 minutes.
*consistency* can be either 'default', 'consistent' or 'stale'. if
not specified *consistency* will the consistency level this client
was configured with.
"""
params = {}
if dc:
params['dc'] = dc
if index:
params['index'] = index
if wait:
params['wait'] = wait
consistency = consistency or self.agent.consistency
if consistency in ('consistent', 'stale'):
params[consistency] = '1'
return self.agent.http.get(
CB.json(index=True), '/v1/coordinate/nodes', params=params)
class Operator(object):
def __init__(self, agent):
self.agent = agent
def raft_config(self):
"""
Returns raft configuration.
"""
return self.agent.http.get(
CB.json(), '/v1/operator/raft/configuration')
|