py-serverless-kit 2.2.0__py3-none-any.whl → 2.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. py_serverless_kit/_version.py +1 -1
  2. py_serverless_kit/cli/main.py +317 -8
  3. py_serverless_kit/cli/templates.py +507 -509
  4. py_serverless_kit/core/deployer.py +353 -255
  5. py_serverless_kit/core/onprem_balancers.py +699 -0
  6. py_serverless_kit/core/onprem_cluster.py +1607 -1528
  7. py_serverless_kit/core/onprem_cluster_scripts.py +711 -847
  8. py_serverless_kit/core/onprem_lb.py +616 -0
  9. py_serverless_kit/core/onprem_network.py +458 -0
  10. py_serverless_kit/core/onprem_placement.py +304 -0
  11. py_serverless_kit/core/onprem_services.py +3 -0
  12. py_serverless_kit/core/project.py +8 -2
  13. py_serverless_kit/core/settings_bundle.py +2 -2
  14. py_serverless_kit/packaging/builder.py +38 -4
  15. py_serverless_kit/packaging/node.py +300 -299
  16. py_serverless_kit/packaging/subprojects.py +113 -0
  17. py_serverless_kit/providers/base.py +7 -0
  18. py_serverless_kit/providers/onprem.py +214 -12
  19. py_serverless_kit/providers/onprem_remote.py +2 -2
  20. py_serverless_kit/runtime/invoke.py +179 -157
  21. py_serverless_kit/runtime/onprem_host.py +163 -21
  22. py_serverless_kit/runtime/peers.py +235 -0
  23. py_serverless_kit/runtime/workflow.py +434 -427
  24. py_serverless_kit/runtime_js/package.json +1 -1
  25. py_serverless_kit/runtime_js/workflow.js +5 -4
  26. py_serverless_kit/studio/project.py +36 -2
  27. py_serverless_kit/studio/server.py +110 -5
  28. py_serverless_kit/studio/studio.html +47 -38
  29. {py_serverless_kit-2.2.0.dist-info → py_serverless_kit-2.3.0.dist-info}/METADATA +3 -2
  30. {py_serverless_kit-2.2.0.dist-info → py_serverless_kit-2.3.0.dist-info}/RECORD +33 -27
  31. {py_serverless_kit-2.2.0.dist-info → py_serverless_kit-2.3.0.dist-info}/WHEEL +0 -0
  32. {py_serverless_kit-2.2.0.dist-info → py_serverless_kit-2.3.0.dist-info}/entry_points.txt +0 -0
  33. {py_serverless_kit-2.2.0.dist-info → py_serverless_kit-2.3.0.dist-info}/licenses/LICENSE +0 -0
@@ -1,2 +1,2 @@
1
- __version__ = "2.2.0"
1
+ __version__ = "2.3.0"
2
2
  __author__ = "Asif Khan @ Kavion.ai"
@@ -9,7 +9,7 @@ import sys
9
9
  import traceback
10
10
  import webbrowser
11
11
  from pathlib import Path
12
- from typing import Any, Callable, Optional
12
+ from typing import Any, Callable, Dict, Optional, Tuple
13
13
 
14
14
  import click
15
15
 
@@ -149,6 +149,20 @@ Operate
149
149
  pskit cluster requests prod --follow
150
150
  pskit cluster list
151
151
  pskit cluster remove prod --yes
152
+ firewall Ports each on-prem server must open pskit firewall --server edge2
153
+ lb algorithms | create | update | list | status | attach | detach | routes | sync | logs | remove | forget
154
+ pskit lb algorithms
155
+ pskit lb create edge --server lb1 --algorithm least_time
156
+ pskit lb update edge --algorithm weighted_round_robin --weight app1=3
157
+ pskit lb list
158
+ pskit lb status edge
159
+ pskit lb attach edge --cluster prod
160
+ pskit lb detach edge --cluster prod
161
+ pskit lb routes remove edge xyz
162
+ pskit lb sync edge
163
+ pskit lb logs edge -n 200
164
+ pskit lb remove edge --yes
165
+ pskit lb forget edge
152
166
  settings export | import (.pskit settings and secrets to another machine, encrypted)
153
167
  pskit settings export -o prod.pskit-settings
154
168
  pskit settings import prod.pskit-settings
@@ -1392,7 +1406,8 @@ def cluster() -> None:
1392
1406
  pskit cluster scale prod --replicas 6
1393
1407
  pskit cluster requests prod --follow # which server handled each request, live
1394
1408
  pskit cluster leave prod edge2
1395
- pskit cluster balancer set prod lb1 # one URL (nginx on lb1) for every server
1409
+ pskit cluster balancer set prod lb1 # one URL (OpenResty on lb1) for every server
1410
+ pskit lb attach edge --cluster prod # or a shared load balancer: http://<edge>/<service>/
1396
1411
 
1397
1412
  Functions use it with provider.options.cluster in pskit.yml (cluster: prod, or {name: prod, replicas: 3}),
1398
1413
  or per deploy: pskit deploy --cluster prod (the stage remembers it; pskit deploy --server NAME goes back to one
@@ -1417,6 +1432,25 @@ def _cluster_root(config_path: Optional[str]) -> Path:
1417
1432
 
1418
1433
 
1419
1434
  _CONFIG_OPTION = click.option("-c", "--config", "config_path", default=None, help="Config file (default: pskit.yml).")
1435
+ _ALGORITHM_OPTION = click.option(
1436
+ "--algorithm", default=None,
1437
+ help="round_robin, sticky_round_robin, weighted_round_robin, ip_hash, least_connections or least_time"
1438
+ " (pskit lb algorithms explains each).")
1439
+ _WEIGHT_OPTION = click.option(
1440
+ "--weight", "weights", multiple=True, metavar="SERVER=N",
1441
+ help="Weight 1..100 of a saved server (weighted round robin, least connections, least time). Repeat it.")
1442
+
1443
+
1444
+ def _weights(values: Tuple[str, ...]) -> Optional[Dict[str, int]]:
1445
+ if not values:
1446
+ return None
1447
+ out: Dict[str, int] = {}
1448
+ for value in values:
1449
+ server, sep, number = value.partition("=")
1450
+ if not sep or not server.strip() or not number.strip().isdigit():
1451
+ raise click.BadParameter(f"{value!r}: use SERVER=N, for example edge1=3", param_hint="--weight")
1452
+ out[server.strip()] = int(number)
1453
+ return out
1420
1454
 
1421
1455
 
1422
1456
  @cluster.command("list")
@@ -1710,19 +1744,21 @@ def cluster_remove(name: str, yes: bool, config_path: Optional[str]) -> None:
1710
1744
 
1711
1745
  @cluster.group("balancer")
1712
1746
  def cluster_balancer() -> None:
1713
- """A load balancer in front of the cluster: one more saved server (outside the cluster) running nginx.
1747
+ """A load balancer in front of the cluster: one more saved server (outside the cluster) running OpenResty.
1714
1748
 
1715
1749
  \b
1716
1750
  Examples:
1717
1751
  pskit cluster balancer set prod lb1 # http://<lb1>/ spreads over the cluster's servers
1718
- pskit cluster balancer set prod lb1 --port 8000
1752
+ pskit cluster balancer set prod lb1 --port 8000 --algorithm least_time
1753
+ pskit cluster balancer set prod lb1 --algorithm weighted_round_robin --weight edge1=3 --weight edge2=1
1719
1754
  pskit cluster balancer logs prod -n 200 # one line per request, with the server that answered
1720
1755
  pskit cluster balancer sync prod # rewrite its config now (deploy / join / leave do it)
1721
1756
  pskit cluster balancer remove prod
1722
1757
 
1723
1758
  pskit deploy, pskit remove, cluster join and cluster leave update it. Logs, tests and the admin API keep
1724
1759
  using the master. A second deployment on the same cluster needs its own port:
1725
- provider.options.cluster.balancer_port.
1760
+ provider.options.cluster.balancer_port. To share one load balancer by path between clusters and projects,
1761
+ use pskit lb instead. Algorithms and their trade-offs: pskit lb algorithms.
1726
1762
  """
1727
1763
 
1728
1764
 
@@ -1732,12 +1768,16 @@ def cluster_balancer() -> None:
1732
1768
  @click.option("--port", default=80, show_default=True, type=click.IntRange(1, 65535),
1733
1769
  help="Port users call on the load balancer.")
1734
1770
  @click.option("--address", default="", help="Its address in the URL users call (default: the server's host).")
1771
+ @_ALGORITHM_OPTION
1772
+ @_WEIGHT_OPTION
1735
1773
  @_CONFIG_OPTION
1736
- def cluster_balancer_set(name: str, server: str, port: int, address: str, config_path: Optional[str]) -> None:
1737
- """Make saved SERVER the load balancer of cluster NAME (installs Docker and nginx there)."""
1774
+ def cluster_balancer_set(name: str, server: str, port: int, address: str, algorithm: Optional[str],
1775
+ weights: Tuple[str, ...], config_path: Optional[str]) -> None:
1776
+ """Make saved SERVER the load balancer of cluster NAME (installs Docker and OpenResty there)."""
1738
1777
  from ..core.onprem_cluster import set_balancer
1739
1778
 
1740
- item = _cluster_call(set_balancer, _cluster_root(config_path), name, server, echo, port=port, address=address)
1779
+ item = _cluster_call(set_balancer, _cluster_root(config_path), name, server, echo, port=port, address=address,
1780
+ algorithm=algorithm, weights=_weights(weights))
1741
1781
  ok(f"{server} is the load balancer of {name}")
1742
1782
  for stack, url in sorted((item.get("balancer") or {}).get("urls", {}).items()):
1743
1783
  echo(f" {stack}: {url}")
@@ -1778,6 +1818,275 @@ def cluster_balancer_logs(name: str, lines: int, config_path: Optional[str]) ->
1778
1818
  click.echo(_cluster_call(balancer_logs, _cluster_root(config_path), name, lines).rstrip("\n"))
1779
1819
 
1780
1820
 
1821
+ # ----------------------------------------------------------------------------- firewall
1822
+ @cli.command(short_help="Ports each on-prem server must open (inbound / outbound), with server names.")
1823
+ @click.option("--server", "only", default="", help="Only this server (name or address).")
1824
+ @click.option("--json", "as_json", is_flag=True)
1825
+ @_CONFIG_OPTION
1826
+ def firewall(only: str, as_json: bool, config_path: Optional[str]) -> None:
1827
+ """The firewall rules this project's on-prem servers need now: deployments, function groups, clusters, load
1828
+ balancers and resources, per server, with who connects and why, and ufw commands.
1829
+
1830
+ \b
1831
+ Examples:
1832
+ pskit firewall
1833
+ pskit firewall --server edge2
1834
+ pskit firewall --json
1835
+
1836
+ Each step also prints the rules it adds (cluster create / join, deploy, lb create / attach, resources onprem
1837
+ create). REQUIRED: it does not work without it; NEEDED: for that feature; optional: pskit falls back to SSH.
1838
+ Every role and port: docs/onprem-network.md.
1839
+ """
1840
+ from ..core import onprem_network as network
1841
+
1842
+ servers = network.by_server(network.project(_cluster_root(config_path)))
1843
+ if only:
1844
+ servers = [s for s in servers if only in (s["server"], s["address"])]
1845
+ if as_json:
1846
+ click.echo(json.dumps(servers, indent=2))
1847
+ return
1848
+ if not servers:
1849
+ echo("Nothing on premises yet (deploy, create a cluster or a load balancer first).")
1850
+ return
1851
+ rules = [r for s in servers for r in s["rules"]]
1852
+ for line in network.lines("this project", rules):
1853
+ echo(line)
1854
+
1855
+
1856
+ # ----------------------------------------------------------------------------- load balancers
1857
+ @cli.group("lb", short_help="Shared on-prem load balancers: one URL, a path per project or cluster.")
1858
+ def lb_group() -> None:
1859
+ """Load balancers shared by projects and clusters: http://<lb>/xyz/, http://<lb>/abc/, ...
1860
+
1861
+ \b
1862
+ Examples:
1863
+ pskit lb algorithms # the 6 algorithms, static / dynamic, pros and cons
1864
+ pskit lb create edge --server lb1 --algorithm least_time
1865
+ pskit lb attach edge --cluster prod # every deployment on prod: http://<lb1>/<service>/
1866
+ pskit lb status edge # its routes (all projects), health, server use
1867
+ pskit lb update edge --algorithm weighted_round_robin --weight app1=3 --weight app2=1
1868
+ pskit lb routes remove edge xyz
1869
+ pskit lb logs edge -n 200
1870
+ pskit lb remove edge
1871
+
1872
+ A project puts itself on one in pskit.yml: provider.options.balancer: edge (path: the service name), or
1873
+ {name: edge, path: xyz, algorithm: sticky_round_robin}. pskit deploy updates its route, pskit remove drops it.
1874
+ Routes are kept on the load balancer server, so projects in other folders or on other machines share it:
1875
+ there, pskit lb create edge --server lb1 finds the running one and adds it.
1876
+ """
1877
+
1878
+
1879
+ def _lb_call(fn: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
1880
+ from ..core.onprem_balancers import BalancerError
1881
+ from ..core.onprem_cluster import ClusterError
1882
+ from ..core.onprem_services import ServiceError
1883
+
1884
+ try:
1885
+ return fn(*args, **kwargs)
1886
+ except (BalancerError, ClusterError, ServiceError, ValueError) as exc:
1887
+ raise click.ClickException(str(exc)) from None
1888
+
1889
+
1890
+ @lb_group.command("algorithms")
1891
+ @click.option("--json", "as_json", is_flag=True)
1892
+ def lb_algorithms(as_json: bool) -> None:
1893
+ """The load balancing algorithms: how each picks a server, static or dynamic, pros, cons, when to use it."""
1894
+ from ..core.onprem_lb import DEFAULT_ALGORITHM, catalog
1895
+
1896
+ items = catalog()
1897
+ if as_json:
1898
+ click.echo(json.dumps(items, indent=2))
1899
+ return
1900
+ for kind, title in (("static", "Static: the rule is fixed, live load is not measured"),
1901
+ ("dynamic", "Dynamic: decided per request from live measurements")):
1902
+ echo(click.style(title, bold=True))
1903
+ for item in (i for i in items if i["kind"] == kind):
1904
+ default = " (default)" if item["name"] == DEFAULT_ALGORITHM else ""
1905
+ echo(f" {click.style(item['name'], fg='cyan')}{default}: {item['summary']}")
1906
+ for pro in item["pros"]:
1907
+ echo(f" + {pro}")
1908
+ for con in item["cons"]:
1909
+ echo(f" - {con}")
1910
+ echo(f" best for: {item['best_for']}")
1911
+ echo("")
1912
+
1913
+
1914
+ @lb_group.command("create")
1915
+ @click.argument("name")
1916
+ @click.option("--server", required=True, help="Saved server that runs it (outside the clusters it serves).")
1917
+ @click.option("--port", default=None, type=click.IntRange(1, 65535), help="Port users call (default 80).")
1918
+ @click.option("--address", default="", help="Its address in the URLs (default: the server's host).")
1919
+ @_ALGORITHM_OPTION
1920
+ @_WEIGHT_OPTION
1921
+ @_CONFIG_OPTION
1922
+ def lb_create(name: str, server: str, port: Optional[int], address: str, algorithm: Optional[str],
1923
+ weights: Tuple[str, ...], config_path: Optional[str]) -> None:
1924
+ """Make saved SERVER load balancer NAME (Docker and OpenResty there), or add the one already running there."""
1925
+ from ..core.onprem_balancers import create_balancer
1926
+
1927
+ item = _lb_call(create_balancer, _cluster_root(config_path), name, server, echo, port=port, address=address,
1928
+ algorithm=algorithm, weights=_weights(weights))
1929
+ ok(f"load balancer {name}: {item['url']} ({item['algorithm_label']}, {item['kind']})")
1930
+
1931
+
1932
+ @lb_group.command("update")
1933
+ @click.argument("name")
1934
+ @click.option("--port", default=None, type=click.IntRange(1, 65535))
1935
+ @click.option("--address", default=None)
1936
+ @_ALGORITHM_OPTION
1937
+ @_WEIGHT_OPTION
1938
+ @_CONFIG_OPTION
1939
+ def lb_update(name: str, port: Optional[int], address: Optional[str], algorithm: Optional[str],
1940
+ weights: Tuple[str, ...], config_path: Optional[str]) -> None:
1941
+ """Change the port, default algorithm, weights or address (routes on the default algorithm switch too)."""
1942
+ from ..core.onprem_balancers import update_balancer
1943
+
1944
+ item = _lb_call(update_balancer, _cluster_root(config_path), name, echo, port=port, address=address,
1945
+ algorithm=algorithm, weights=_weights(weights))
1946
+ ok(f"load balancer {name}: {item['url']} ({item['algorithm_label']})")
1947
+
1948
+
1949
+ @lb_group.command("list")
1950
+ @click.option("--json", "as_json", is_flag=True)
1951
+ @_CONFIG_OPTION
1952
+ def lb_list(as_json: bool, config_path: Optional[str]) -> None:
1953
+ """Load balancers this project uses."""
1954
+ from ..core.onprem_balancers import list_balancers
1955
+
1956
+ items = list_balancers(_cluster_root(config_path))
1957
+ if as_json:
1958
+ click.echo(json.dumps(items, indent=2))
1959
+ return
1960
+ if not items:
1961
+ echo("No load balancers yet: pskit lb create NAME --server SERVER")
1962
+ return
1963
+ for item in items:
1964
+ clusters = f", clusters: {', '.join(item['clusters'])}" if item["clusters"] else ""
1965
+ echo(f" {item['name']}: {item['url']} on {item['server']} ({item['algorithm_label']}{clusters})")
1966
+
1967
+
1968
+ @lb_group.command("status")
1969
+ @click.argument("name")
1970
+ @click.option("--json", "as_json", is_flag=True)
1971
+ @_CONFIG_OPTION
1972
+ def lb_status(name: str, as_json: bool, config_path: Optional[str]) -> None:
1973
+ """Whether it runs, its server's use, and every route on it (from every project)."""
1974
+ from ..core.onprem_balancers import status
1975
+
1976
+ current = _lb_call(status, _cluster_root(config_path), name)
1977
+ if as_json:
1978
+ click.echo(json.dumps(current, indent=2))
1979
+ return
1980
+ state = current.get("state")
1981
+ echo(f"{name}: {current['url']} on {current['server']} - {state}, default {current['algorithm_label']}")
1982
+ if current.get("reachable") is False:
1983
+ fail(f"{current['server']} does not answer: {current.get('error')}")
1984
+ if current.get("cpu") is not None:
1985
+ echo(f" server: cpu {current.get('cpu')}%, memory {current.get('memory')}%, disk {current.get('disk')}%")
1986
+ if current.get("drift"):
1987
+ echo(" note: another project changed its settings; pskit lb sync takes them from this project again")
1988
+ for route in current.get("routes") or []:
1989
+ mark = "" if route.get("mine") else " (another project)"
1990
+ inherit = " (default)" if route.get("inherits") else ""
1991
+ servers = ", ".join(b.get("server") or b["address"] for b in route.get("backends") or [])
1992
+ echo(f" {route['url']} -> {route['owner']}{mark}: {route['effective_algorithm']}{inherit} over {servers}")
1993
+ if not current.get("routes"):
1994
+ echo(" no routes yet: pskit lb attach NAME --cluster CLUSTER, or provider.options.balancer in pskit.yml")
1995
+ if state != "running":
1996
+ fail(f"load balancer {name} is not running: pskit lb sync {name}")
1997
+
1998
+
1999
+ @lb_group.command("attach")
2000
+ @click.argument("name")
2001
+ @click.option("--cluster", "cluster_name", required=True, help="Cluster whose deployments it serves.")
2002
+ @_CONFIG_OPTION
2003
+ def lb_attach(name: str, cluster_name: str, config_path: Optional[str]) -> None:
2004
+ """Serve every deployment of a cluster on load balancer NAME (http://<lb>/<service>/), now and on deploys."""
2005
+ from ..core.onprem_balancers import attach_cluster
2006
+
2007
+ _lb_call(attach_cluster, _cluster_root(config_path), name, cluster_name, echo)
2008
+ ok(f"{name} serves cluster {cluster_name}")
2009
+
2010
+
2011
+ @lb_group.command("detach")
2012
+ @click.argument("name")
2013
+ @click.option("--cluster", "cluster_name", required=True)
2014
+ @_CONFIG_OPTION
2015
+ def lb_detach(name: str, cluster_name: str, config_path: Optional[str]) -> None:
2016
+ """Stop serving a cluster's deployments on load balancer NAME."""
2017
+ from ..core.onprem_balancers import detach_cluster
2018
+
2019
+ _lb_call(detach_cluster, _cluster_root(config_path), name, cluster_name, echo)
2020
+ ok(f"{name} no longer serves cluster {cluster_name}")
2021
+
2022
+
2023
+ @lb_group.group("routes")
2024
+ def lb_routes() -> None:
2025
+ """Routes on a load balancer (pskit lb status lists them)."""
2026
+
2027
+
2028
+ @lb_routes.command("remove")
2029
+ @click.argument("name")
2030
+ @click.argument("path")
2031
+ @click.option("--yes", is_flag=True, help="Do not ask.")
2032
+ @_CONFIG_OPTION
2033
+ def lb_routes_remove(name: str, path: str, yes: bool, config_path: Optional[str]) -> None:
2034
+ """Remove the route at PATH (xyz, or port-8081) from load balancer NAME, whichever project owns it."""
2035
+ from ..core.onprem_balancers import remove_route
2036
+
2037
+ if not yes:
2038
+ click.confirm(f"Remove /{path.strip('/')} from load balancer {name}?", abort=True)
2039
+ _lb_call(remove_route, _cluster_root(config_path), name, path, echo)
2040
+
2041
+
2042
+ @lb_group.command("sync")
2043
+ @click.argument("name")
2044
+ @_CONFIG_OPTION
2045
+ def lb_sync(name: str, config_path: Optional[str]) -> None:
2046
+ """Rewrite the load balancer from this project's settings: every route, and attached clusters."""
2047
+ from ..core.onprem_balancers import sync_balancer
2048
+
2049
+ _lb_call(sync_balancer, _cluster_root(config_path), name, echo)
2050
+ ok(f"load balancer {name} updated")
2051
+
2052
+
2053
+ @lb_group.command("logs")
2054
+ @click.argument("name")
2055
+ @click.option("-n", "--lines", default=100, show_default=True, type=click.IntRange(1, 10000))
2056
+ @_CONFIG_OPTION
2057
+ def lb_logs(name: str, lines: int, config_path: Optional[str]) -> None:
2058
+ """Recent requests (upstream= is the server that answered) and errors."""
2059
+ from ..core.onprem_balancers import logs
2060
+
2061
+ click.echo(_lb_call(logs, _cluster_root(config_path), name, lines).rstrip("\n"))
2062
+
2063
+
2064
+ @lb_group.command("remove")
2065
+ @click.argument("name")
2066
+ @click.option("--force", is_flag=True, help="Also when other projects still use it (their URLs stop too).")
2067
+ @click.option("--yes", is_flag=True, help="Do not ask.")
2068
+ @_CONFIG_OPTION
2069
+ def lb_remove(name: str, force: bool, yes: bool, config_path: Optional[str]) -> None:
2070
+ """Stop load balancer NAME on its server and forget it."""
2071
+ from ..core.onprem_balancers import remove_balancer
2072
+
2073
+ if not yes:
2074
+ click.confirm(f"Stop load balancer {name}? Every URL on it stops answering.", abort=True)
2075
+ _lb_call(remove_balancer, _cluster_root(config_path), name, echo, force=force)
2076
+ ok(f"load balancer {name} removed")
2077
+
2078
+
2079
+ @lb_group.command("forget")
2080
+ @click.argument("name")
2081
+ @_CONFIG_OPTION
2082
+ def lb_forget(name: str, config_path: Optional[str]) -> None:
2083
+ """Forget load balancer NAME in this project only (it keeps running for other projects)."""
2084
+ from ..core.onprem_balancers import forget_balancer
2085
+
2086
+ _lb_call(forget_balancer, _cluster_root(config_path), name)
2087
+ ok(f"forgot {name}")
2088
+
2089
+
1781
2090
  # ----------------------------------------------------------------------------- settings
1782
2091
  @cli.group(short_help="Export / import this project's .pskit settings and secrets (encrypted).")
1783
2092
  def settings() -> None: