canyonos 0.2.3.dev5__tar.gz → 0.2.4.dev1__tar.gz

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 (119) hide show
  1. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/PKG-INFO +2 -1
  2. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/auth.py +2 -2
  3. canyonos-0.2.4.dev1/canyonos/aws_account.py +349 -0
  4. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/aws_provision.py +186 -158
  5. canyonos-0.2.4.dev1/canyonos/cluster_provision.py +692 -0
  6. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/context.py +60 -12
  7. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/dashboard.compose.yml +3 -0
  8. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/dashboard.py +25 -18
  9. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/deploy.py +3 -0
  10. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/deployment.py +24 -0
  11. canyonos-0.2.4.dev1/canyonos/keychain.py +197 -0
  12. canyonos-0.2.4.dev1/canyonos/kubernetes_local.py +688 -0
  13. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/kubernetes_target.py +78 -4
  14. canyonos-0.2.4.dev1/canyonos/login.py +472 -0
  15. canyonos-0.2.4.dev1/canyonos/logout.py +54 -0
  16. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/os_support/_windows.py +6 -2
  17. canyonos-0.2.4.dev1/canyonos/platform.py +165 -0
  18. canyonos-0.2.4.dev1/canyonos/provision.py +221 -0
  19. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/records.py +25 -0
  20. canyonos-0.2.4.dev1/canyonos/rosa_provision.py +932 -0
  21. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/sdk/models.py +10 -0
  22. canyonos-0.2.4.dev1/canyonos/session.py +396 -0
  23. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/status.py +28 -6
  24. canyonos-0.2.4.dev1/canyonos/terraform/rosa/.terraform.lock.hcl +124 -0
  25. canyonos-0.2.4.dev1/canyonos/terraform/rosa/main.tf +65 -0
  26. canyonos-0.2.4.dev1/canyonos/terraform/rosa/outputs.tf +29 -0
  27. canyonos-0.2.4.dev1/canyonos/terraform/rosa/variables.tf +80 -0
  28. canyonos-0.2.4.dev1/canyonos/terraform/rosa/versions.tf +17 -0
  29. canyonos-0.2.4.dev1/canyonos/tools.py +131 -0
  30. canyonos-0.2.4.dev1/canyonos/whoami.py +92 -0
  31. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos.egg-info/PKG-INFO +2 -1
  32. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos.egg-info/SOURCES.txt +15 -0
  33. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos.egg-info/requires.txt +1 -0
  34. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/cli.py +123 -5
  35. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/pyproject.toml +6 -1
  36. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/utils/help_screen.py +17 -2
  37. canyonos-0.2.3.dev5/canyonos/kubernetes_local.py +0 -419
  38. canyonos-0.2.3.dev5/canyonos/provision.py +0 -44
  39. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/README.md +0 -0
  40. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/__init__.py +0 -0
  41. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/api.py +0 -0
  42. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/chat.py +0 -0
  43. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/clean.py +0 -0
  44. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/config.py +0 -0
  45. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/constants.py +0 -0
  46. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/contact.py +0 -0
  47. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/convert.py +0 -0
  48. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/curl.py +0 -0
  49. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/dashboard_registration.py +0 -0
  50. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/dashboard_stack.py +0 -0
  51. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/dev_machine.py +0 -0
  52. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/docker_cmd.py +0 -0
  53. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/doctor.py +0 -0
  54. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/env.py +0 -0
  55. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/feedback.py +0 -0
  56. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/forwards.py +0 -0
  57. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/init.py +0 -0
  58. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/internal_ca.py +0 -0
  59. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/logs.py +0 -0
  60. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/new_app.py +0 -0
  61. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/new_app_template/.env.example +0 -0
  62. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/new_app_template/.gitignore +0 -0
  63. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/new_app_template/agents/my_agent.py.tmpl +0 -0
  64. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/new_app_template/agents/workflow.py.tmpl +0 -0
  65. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/new_app_template/config/llms.yaml +0 -0
  66. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/new_app_template/config/policy.yaml +0 -0
  67. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/new_app_template/config/prompts.yaml +0 -0
  68. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/new_app_template/config/scaling.yaml +0 -0
  69. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/new_app_template/config/workflow_config.yaml +0 -0
  70. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/os_support/__init__.py +0 -0
  71. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/os_support/_posix.py +0 -0
  72. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/port_utils.py +0 -0
  73. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/ports.py +0 -0
  74. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/quickstart.py +0 -0
  75. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/resources.py +0 -0
  76. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/sdk/__init__.py +0 -0
  77. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/selection.py +0 -0
  78. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/serve.py +0 -0
  79. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/README.md +0 -0
  80. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/helloworld/agents/hello_agent.py.tmpl +0 -0
  81. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/helloworld/agents/workflow.py.tmpl +0 -0
  82. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/helloworld/config/policy.yaml +0 -0
  83. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/helloworld/config/test_query.txt +0 -0
  84. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/helloworld/config/workflow_config.yaml +0 -0
  85. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/workflow_group.yaml +0 -0
  86. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/.env +0 -0
  87. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/.env.example +0 -0
  88. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/agents/advisor_agent.py.tmpl +0 -0
  89. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/agents/intent_agent.py.tmpl +0 -0
  90. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/agents/metrics_agent.py.tmpl +0 -0
  91. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/agents/price_agent.py.tmpl +0 -0
  92. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/agents/risk_agent.py.tmpl +0 -0
  93. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/agents/workflow.py.tmpl +0 -0
  94. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/config/llms.yaml +0 -0
  95. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/config/policy.yaml +0 -0
  96. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/config/prompts.yaml +0 -0
  97. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/config/scaling.yaml +0 -0
  98. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/config/test_query.txt +0 -0
  99. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/config/workflow_config.yaml +0 -0
  100. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/workflow_group.yaml +0 -0
  101. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/stop.py +0 -0
  102. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/sync.py +0 -0
  103. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/test.py +0 -0
  104. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/theme.py +0 -0
  105. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/ui.py +0 -0
  106. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/upgrade.py +0 -0
  107. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/usage/__init__.py +0 -0
  108. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/usage/consent.py +0 -0
  109. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/usage/delivery.py +0 -0
  110. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/usage/outbox.py +0 -0
  111. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/usage/settings.py +0 -0
  112. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/validate.py +0 -0
  113. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos/workflow_group.yaml +0 -0
  114. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos.egg-info/dependency_links.txt +0 -0
  115. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos.egg-info/entry_points.txt +0 -0
  116. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/canyonos.egg-info/top_level.txt +0 -0
  117. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/setup.cfg +0 -0
  118. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/utils/__init__.py +0 -0
  119. {canyonos-0.2.3.dev5 → canyonos-0.2.4.dev1}/utils/tui.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: canyonos
3
- Version: 0.2.3.dev5
3
+ Version: 0.2.4.dev1
4
4
  Summary: Open-source platform for multi-agent apps: per-agent scaling, tracing and cost, in plain Python
5
5
  License-Expression: AGPL-3.0-only
6
6
  Project-URL: Homepage, https://canyonos.ai
@@ -17,6 +17,7 @@ Description-Content-Type: text/markdown
17
17
  Requires-Dist: boto3[crt]>=1.43.109
18
18
  Requires-Dist: cryptography>=50.0.2
19
19
  Requires-Dist: pyfiglet
20
+ Requires-Dist: pyjwt[crypto]>=2.10.1
20
21
  Requires-Dist: python-dotenv
21
22
  Requires-Dist: pyyaml
22
23
  Requires-Dist: rich
@@ -107,8 +107,8 @@ def _adopt_machine_key(deploying: str) -> None:
107
107
  ) from error
108
108
  if key is not None:
109
109
  ui.note(
110
- f"Each workflow group now keeps its own API key: {legacy} moved into the "
111
- "record of each workflow group here, in its api_key file."
110
+ f"Each workflow group now has its own API key. The key from {legacy} "
111
+ "was copied into each group's api_key file."
112
112
  )
113
113
 
114
114
 
@@ -0,0 +1,349 @@
1
+ # SPDX-FileCopyrightText: 2026 Canyon Code, Inc.
2
+ # SPDX-License-Identifier: AGPL-3.0-only
3
+
4
+ """
5
+ The AWS account `canyonos provision` works in: signing in, and the region, asked the
6
+ same way by every provisioner that makes something in AWS.
7
+ """
8
+
9
+ import re
10
+ import shutil
11
+ import subprocess
12
+
13
+ from canyonos import ui
14
+ from canyonos.provision import Cancelled, ask, pick
15
+
16
+ STS_REGION = "us-east-1"
17
+ REGIONS = (
18
+ "us-east-1",
19
+ "us-east-2",
20
+ "us-west-2",
21
+ "eu-west-1",
22
+ "eu-central-1",
23
+ "ap-southeast-1",
24
+ "ap-northeast-1",
25
+ )
26
+
27
+ DEFAULT_CREDENTIALS = "__default__"
28
+ AWS_LOGIN = "__aws_login__"
29
+ ACCESS_KEYS = "__access_keys__"
30
+ OTHER_REGION = "__other__"
31
+ NO_BOUNDARY = "__no_boundary__"
32
+ NO_TAGS = "__no_tags__"
33
+
34
+
35
+ def sign_in():
36
+ """Ask which AWS credentials to use and return a boto3 session with them, once AWS
37
+ accepts them. Raises Cancelled when you back out."""
38
+ import boto3
39
+ from botocore.exceptions import BotoCoreError, ClientError
40
+
41
+ while True:
42
+ entries = [
43
+ (
44
+ DEFAULT_CREDENTIALS,
45
+ "Use your default AWS credentials",
46
+ "Environment variables, AWS_PROFILE, ~/.aws, SSO or an earlier "
47
+ "`aws login`.",
48
+ )
49
+ ]
50
+ if shutil.which("aws"):
51
+ entries.append(
52
+ (
53
+ AWS_LOGIN,
54
+ "Sign in with your browser",
55
+ "Runs `aws login` with your AWS console sign-in.",
56
+ )
57
+ )
58
+ entries.append(
59
+ (
60
+ ACCESS_KEYS,
61
+ "Enter an access key and secret",
62
+ "Used for this run only and not saved.",
63
+ )
64
+ )
65
+ choice = pick(entries, "Which AWS credentials should provisioning use?")
66
+ if choice == AWS_LOGIN:
67
+ if subprocess.run(["aws", "login"]).returncode != 0:
68
+ ui.fail("`aws login` did not finish.")
69
+ continue
70
+ session = boto3.Session()
71
+ elif choice == ACCESS_KEYS:
72
+ try:
73
+ session = boto3.Session(
74
+ aws_access_key_id=ask("Access key ID: "),
75
+ aws_secret_access_key=ui.secret("Secret access key"),
76
+ )
77
+ except Cancelled:
78
+ continue
79
+ else:
80
+ session = boto3.Session()
81
+ try:
82
+ identity = session.client(
83
+ "sts", region_name=session.region_name or STS_REGION
84
+ ).get_caller_identity()
85
+ except (BotoCoreError, ClientError) as e:
86
+ ui.fail(f"AWS did not accept these credentials: {e}")
87
+ continue
88
+ ui.ok(f"Signed in to AWS account {identity['Account']} as {identity['Arn']}")
89
+ return session
90
+
91
+
92
+ def choose_region(session, title):
93
+ """Ask for a region under `title`, the session's default first. Raises Cancelled
94
+ when you back out."""
95
+ current = session.region_name
96
+ regions = [current, *(r for r in REGIONS if r != current)] if current else REGIONS
97
+ entries = [
98
+ (region, region, "Your default region" if region == current else None)
99
+ for region in regions
100
+ ]
101
+ entries.append((OTHER_REGION, "Another region", None))
102
+ region = pick(entries, title)
103
+ if region == OTHER_REGION:
104
+ region = ask("Region, e.g. ca-central-1: ")
105
+ return region
106
+
107
+
108
+ def terraform_environment(session, region):
109
+ """The environment Terraform signs in to AWS with as `session` does. Access keys
110
+ entered for this run are passed as they are; any other credentials Terraform reads
111
+ from where boto3 found them (the environment, AWS_PROFILE, SSO), so it refreshes
112
+ short-lived ones itself during a long run instead of using a copy that expires."""
113
+ aws = {"AWS_REGION": region}
114
+ credentials = session.get_credentials()
115
+ if credentials.method == "explicit":
116
+ frozen = credentials.get_frozen_credentials()
117
+ aws["AWS_ACCESS_KEY_ID"] = frozen.access_key
118
+ aws["AWS_SECRET_ACCESS_KEY"] = frozen.secret_key
119
+ return aws
120
+
121
+
122
+ def choose_boundary():
123
+ """Ask for the permissions boundary the account requires on new IAM roles: its
124
+ ARN, or "" for none."""
125
+ choice = pick(
126
+ [
127
+ (NO_BOUNDARY, "No", "The account has no such rule. Most don't."),
128
+ (
129
+ "",
130
+ "Yes, enter the boundary policy",
131
+ "Every IAM role made for the cluster gets it as its upper limit.",
132
+ ),
133
+ ],
134
+ "Does your AWS account require a permissions boundary on the IAM roles made "
135
+ "for the cluster? Some company accounts do: ask your AWS admin if unsure.",
136
+ )
137
+ if choice == NO_BOUNDARY:
138
+ return ""
139
+ while True:
140
+ arn = ask("Boundary policy ARN, e.g. arn:aws:iam::<account>:policy/<name>: ")
141
+ if arn.startswith("arn:aws") and ":policy/" in arn:
142
+ return arn
143
+ ui.fail("That isn't an IAM policy ARN.")
144
+
145
+
146
+ def choose_tags():
147
+ """Ask for tags of your own to put on everything made in AWS, as a dict."""
148
+ choice = pick(
149
+ [
150
+ (
151
+ NO_TAGS,
152
+ "No",
153
+ "CanyonOS still tags everything it makes, to find it again.",
154
+ ),
155
+ (
156
+ "",
157
+ "Yes, add tags",
158
+ "Put on everything made in AWS. Keys and values are case-sensitive.",
159
+ ),
160
+ ],
161
+ "Does your AWS account require tags of its own on new resources?",
162
+ )
163
+ if choice == NO_TAGS:
164
+ return {}
165
+ while True:
166
+ text = ask("Tags as key=value, separated by commas, e.g. team=platform: ")
167
+ pairs = [part.strip().split("=", 1) for part in text.split(",") if part.strip()]
168
+ if pairs and all(len(pair) == 2 and pair[0] for pair in pairs):
169
+ return {key.strip(): value.strip() for key, value in pairs}
170
+ ui.fail("Write each tag as key=value.")
171
+
172
+
173
+ # Service Quotas codes: VPCs per Region, and EC2-VPC Elastic IPs.
174
+ VPC_QUOTA = ("vpc", "L-F678F1CE")
175
+ ELASTIC_IP_QUOTA = ("ec2", "L-0263D0A3")
176
+ # Subnet tags that tell Kubernetes where to put a load balancer; only the key counts.
177
+ PUBLIC_ELB_TAG = "kubernetes.io/role/elb"
178
+ PRIVATE_ELB_TAG = "kubernetes.io/role/internal-elb"
179
+
180
+
181
+ def _limit(session, region, service, code):
182
+ """An account's limit in `region`, or None when it can't be read."""
183
+ from botocore.exceptions import BotoCoreError, ClientError
184
+
185
+ quotas = session.client("service-quotas", region_name=region)
186
+ try:
187
+ try:
188
+ found = quotas.get_service_quota(ServiceCode=service, QuotaCode=code)
189
+ except quotas.exceptions.NoSuchResourceException:
190
+ # Never changed in this account: AWS's default.
191
+ found = quotas.get_aws_default_service_quota(
192
+ ServiceCode=service, QuotaCode=code
193
+ )
194
+ return int(found["Quota"]["Value"])
195
+ except (BotoCoreError, ClientError, KeyError, ValueError):
196
+ return None
197
+
198
+
199
+ def network_room(session, region, vpcs, elastic_ips):
200
+ """What would stop `vpcs` new VPCs and `elastic_ips` new Elastic IPs in `region`
201
+ for the account's limits, as lines; [] when they fit, or the limits can't be read."""
202
+ ec2 = session.client("ec2", region_name=region)
203
+ problems = []
204
+ for needed, (service, code), used, what in (
205
+ (vpcs, VPC_QUOTA, lambda: len(ec2.describe_vpcs()["Vpcs"]), "VPCs"),
206
+ (
207
+ elastic_ips,
208
+ ELASTIC_IP_QUOTA,
209
+ lambda: len(ec2.describe_addresses()["Addresses"]),
210
+ "Elastic IPs",
211
+ ),
212
+ ):
213
+ limit = _limit(session, region, service, code)
214
+ if limit is None:
215
+ continue
216
+ in_use = used()
217
+ if in_use + needed > limit:
218
+ problems.append(
219
+ f"{region} has {in_use} of its {limit} {what}, and this needs {needed} "
220
+ f"more: free {in_use + needed - limit}, use an existing network, or ask "
221
+ "AWS for a higher limit (Service Quotas)."
222
+ )
223
+ return problems
224
+
225
+
226
+ def _name(tags):
227
+ return next((t["Value"] for t in tags or [] if t["Key"] == "Name"), "")
228
+
229
+
230
+ def networks(session, region):
231
+ """The VPCs in `region` a cluster could be made in, each with its subnets by zone: a
232
+ subnet is public when its route table sends 0.0.0.0/0 to an internet gateway, and
233
+ private otherwise, `egress` saying how it reaches out (NAT gateway, transit
234
+ gateway, ...) or None when it can't. Returns [{vpc_id, name, cidr, zones: {zone:
235
+ {"public": [subnet], "private": [subnet]}}}], a subnet being {id, name, free_ips,
236
+ egress, elb_tag}."""
237
+ ec2 = session.client("ec2", region_name=region)
238
+ tables = ec2.describe_route_tables()["RouteTables"]
239
+ found = []
240
+ for vpc in ec2.describe_vpcs()["Vpcs"]:
241
+ vpc_id = vpc["VpcId"]
242
+ mine = [t for t in tables if t["VpcId"] == vpc_id]
243
+ main = next(
244
+ (t for t in mine for a in t.get("Associations", []) if a.get("Main")), None
245
+ )
246
+ zones = {}
247
+ subnets = ec2.describe_subnets(
248
+ Filters=[{"Name": "vpc-id", "Values": [vpc_id]}]
249
+ )["Subnets"]
250
+ for subnet in subnets:
251
+ table = next(
252
+ (
253
+ t
254
+ for t in mine
255
+ for a in t.get("Associations", [])
256
+ if a.get("SubnetId") == subnet["SubnetId"]
257
+ ),
258
+ main,
259
+ )
260
+ default = next(
261
+ (
262
+ r
263
+ for r in (table or {}).get("Routes", [])
264
+ if r.get("DestinationCidrBlock") == "0.0.0.0/0"
265
+ and r.get("State", "active") == "active"
266
+ ),
267
+ {},
268
+ )
269
+ public = default.get("GatewayId", "").startswith("igw-")
270
+ egress = next(
271
+ (
272
+ label
273
+ for key, label in (
274
+ ("GatewayId", "internet gateway"),
275
+ ("NatGatewayId", "NAT gateway"),
276
+ ("TransitGatewayId", "transit gateway"),
277
+ ("NetworkInterfaceId", "network appliance"),
278
+ ("VpcPeeringConnectionId", "VPC peering"),
279
+ )
280
+ if default.get(key)
281
+ ),
282
+ None,
283
+ )
284
+ tags = {t["Key"] for t in subnet.get("Tags", [])}
285
+ entry = {
286
+ "id": subnet["SubnetId"],
287
+ "name": _name(subnet.get("Tags")),
288
+ "free_ips": subnet["AvailableIpAddressCount"],
289
+ "egress": egress,
290
+ "elb_tag": (PUBLIC_ELB_TAG if public else PRIVATE_ELB_TAG) in tags,
291
+ }
292
+ zone = zones.setdefault(
293
+ subnet["AvailabilityZone"], {"public": [], "private": []}
294
+ )
295
+ zone["public" if public else "private"].append(entry)
296
+ found.append(
297
+ {
298
+ "vpc_id": vpc_id,
299
+ "name": _name(vpc.get("Tags")),
300
+ "cidr": vpc["CidrBlock"],
301
+ "zones": dict(sorted(zones.items())),
302
+ }
303
+ )
304
+ return found
305
+
306
+
307
+ def refused(session, requests):
308
+ """Which of `requests`, each (action, resource ARN, {condition key: value}), IAM's
309
+ policy simulator says the signed-in identity may not do, as "action on resource"
310
+ lines, without doing any of them. [] when it allows them all, or when the
311
+ simulator can't be asked (no permission to use it): then only trying tells."""
312
+ from botocore.exceptions import BotoCoreError, ClientError
313
+
314
+ iam = session.client("iam")
315
+ try:
316
+ arn = session.client(
317
+ "sts", region_name=session.region_name or STS_REGION
318
+ ).get_caller_identity()["Arn"]
319
+ # An assumed role's session is simulated as its role.
320
+ assumed = re.match(r"arn:[\w-]+:sts::\d+:assumed-role/([^/]+)/", arn)
321
+ if assumed:
322
+ arn = iam.get_role(RoleName=assumed.group(1))["Role"]["Arn"]
323
+ found = []
324
+ for action, resource, context in requests:
325
+ result = iam.simulate_principal_policy(
326
+ PolicySourceArn=arn,
327
+ ActionNames=[action],
328
+ ResourceArns=[resource],
329
+ ContextEntries=[
330
+ {
331
+ "ContextKeyName": key,
332
+ "ContextKeyValues": [value],
333
+ "ContextKeyType": "string",
334
+ }
335
+ for key, value in context.items()
336
+ ],
337
+ )["EvaluationResults"][0]
338
+ if result["EvalDecision"] != "allowed":
339
+ found.append(f"{action} on {resource}")
340
+ except (BotoCoreError, ClientError):
341
+ return []
342
+ return found
343
+
344
+
345
+ def errors():
346
+ """The errors boto3 raises, for a provisioner to report as a failed step."""
347
+ from botocore.exceptions import BotoCoreError, ClientError
348
+
349
+ return (BotoCoreError, ClientError)