kdev-cli 0.1.3__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.
kdev/wizard.py ADDED
@@ -0,0 +1,251 @@
1
+ """`kdev setup`: get this machine ready, doing only what is missing.
2
+
3
+ Safe to re-run at any point: every step checks first and leaves a working
4
+ piece alone. On a second machine joining an existing workspace, the tunnel
5
+ comes from the notebook itself, so Cloudflare setup is skipped entirely.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import subprocess
11
+ from pathlib import Path
12
+
13
+ from . import auth, cloudflared, config, tunnel, ui
14
+ from . import notebook as nb
15
+ from .errors import KdevError
16
+
17
+
18
+ def setup_named_tunnel(cfg: config.Config, hostname: str = "", force: bool = False) -> bool:
19
+ """Provision the tunnel with the cloudflared CLI. Returns True on success.
20
+
21
+ Locally-managed, so the ingress rule ships with the Kaggle container and
22
+ nothing needs provisioning in the Zero Trust dashboard.
23
+ """
24
+ if not force and not hostname:
25
+ with ui.spinner("checking your existing tunnel…"):
26
+ already = tunnel.is_configured(cfg)
27
+ if already:
28
+ ui.ok(f"{cfg.tunnel_hostname} is already set up")
29
+ ui.hint("Nothing to change. Use --force to rebuild it.")
30
+ return True
31
+
32
+ if not tunnel.logged_in():
33
+ ui.hint("A browser will open so you can authorise your Cloudflare account.")
34
+ if not ui.confirm("Continue?", default=True):
35
+ return False
36
+ try:
37
+ tunnel.login()
38
+ except tunnel.TunnelError as e:
39
+ ui.err(str(e))
40
+ return False
41
+
42
+ with ui.spinner("reading your Cloudflare zone…"):
43
+ zone = tunnel.zone_name()
44
+
45
+ if hostname:
46
+ # --hostname overrides the derived name, for a zone layout that needs it.
47
+ cfg.tunnel_hostname = tunnel.validate_hostname(hostname, zone)
48
+ cfg.tunnel_name = cfg.tunnel_name or cfg.tunnel_hostname.split(".")[0]
49
+ elif zone:
50
+ ui.ok(f"zone {zone}")
51
+ # Look for a tunnel that already has a matching DNS record before
52
+ # asking for a name -- otherwise re-running setup invents a second one.
53
+ with ui.spinner("checking for an existing tunnel…"):
54
+ found = tunnel.existing_for_zone(zone)
55
+ if found and not force:
56
+ pick = found[0] if len(found) == 1 else None
57
+ if pick is None:
58
+ choice = ui.choose(
59
+ "Reuse an existing tunnel?",
60
+ [(f["hostname"], f["hostname"]) for f in found] + [("", "Create a new one")],
61
+ )
62
+ pick = next((f for f in found if f["hostname"] == choice), None)
63
+ elif not ui.confirm(f"Reuse {pick['hostname']}?", default=True):
64
+ pick = None
65
+ if pick:
66
+ cfg.tunnel_name, cfg.tunnel_id = pick["name"], pick["id"]
67
+ cfg.tunnel_hostname = pick["hostname"]
68
+ try:
69
+ cfg.tunnel_credentials = tunnel.credentials(pick["name"])
70
+ except tunnel.TunnelError as e:
71
+ ui.err(str(e))
72
+ return False
73
+ cfg.tunnel_token = ""
74
+ ui.ok(f"{cfg.tunnel_hostname} (reused existing tunnel and DNS record)")
75
+ return True
76
+ while True:
77
+ try:
78
+ cfg.tunnel_name = tunnel.validate_label(
79
+ ui.ask(
80
+ f"Subdomain for the box (becomes <name>.{zone})",
81
+ default=cfg.tunnel_name or "kdev",
82
+ )
83
+ )
84
+ break
85
+ except tunnel.TunnelError as e:
86
+ ui.err(str(e))
87
+ cfg.tunnel_hostname = f"{cfg.tunnel_name}.{zone}"
88
+ else:
89
+ ui.warn("Could not read your zone name.")
90
+ while True:
91
+ try:
92
+ cfg.tunnel_hostname = tunnel.validate_hostname(
93
+ ui.ask("Hostname you will ssh to", default=cfg.tunnel_hostname)
94
+ )
95
+ break
96
+ except tunnel.TunnelError as e:
97
+ ui.err(str(e))
98
+ if not ui.confirm("Try again?", default=True):
99
+ return False
100
+ cfg.tunnel_name = cfg.tunnel_name or cfg.tunnel_hostname.split(".")[0]
101
+
102
+ def on_conflict(record: dict) -> bool:
103
+ ui.warn(f"{cfg.tunnel_hostname} already points at {record.get('content')}")
104
+ return ui.confirm("Repoint it at this tunnel?", default=False)
105
+
106
+ try:
107
+ with ui.spinner("setting up tunnel…"):
108
+ cfg.tunnel_id, cfg.tunnel_credentials, dns = tunnel.provision(
109
+ cfg.tunnel_name, cfg.tunnel_hostname, on_conflict
110
+ )
111
+ except tunnel.TunnelError as e:
112
+ ui.err(str(e))
113
+ return False
114
+
115
+ cfg.tunnel_token = ""
116
+ explain = {
117
+ "reused": "DNS record already correct, left alone",
118
+ "created": "DNS record created",
119
+ "repointed": "DNS record repointed at this tunnel",
120
+ }
121
+ ui.ok(f"{cfg.tunnel_hostname} ({explain.get(dns, dns)})")
122
+ return True
123
+
124
+
125
+ def choose_tunnel(cfg: config.Config) -> None:
126
+ """Named or quick tunnel.
127
+
128
+ They ride the same connector and edge, so throughput and latency are the
129
+ same. The difference is a drop: a named tunnel comes back on the same
130
+ hostname, a quick one on a new random hostname, stranding a session that
131
+ is still using quota until `kdev up` finds it again.
132
+ """
133
+ mode = ui.choose(
134
+ "How should the box be reachable?",
135
+ [
136
+ ("named", "Your Cloudflare domain same hostname every time (recommended)"),
137
+ ("quick", "Free quick tunnel new hostname every session"),
138
+ ],
139
+ )
140
+ if mode == "named" and setup_named_tunnel(cfg):
141
+ return
142
+ if mode == "named":
143
+ ui.warn("Using a quick tunnel for now; `kdev tunnel setup` retries the named one.")
144
+ cfg.tunnel_token = cfg.tunnel_credentials = cfg.tunnel_id = ""
145
+ cfg.tunnel_hostname = ""
146
+
147
+
148
+ def ensure_ssh_key(cfg: config.Config) -> None:
149
+ if not cfg.ssh_public_key:
150
+ cfg.ssh_public_key = config.default_ssh_public_key()
151
+ if cfg.ssh_public_key:
152
+ comment = cfg.ssh_public_key.split()[-1] if " " in cfg.ssh_public_key else "found"
153
+ ui.ok(f"ssh key {comment}")
154
+ return
155
+ ui.warn("No SSH key in ~/.ssh")
156
+ if not ui.confirm("Generate one now?", default=True):
157
+ raise KdevError(
158
+ "kdev needs an SSH key to let you into the box.", "Make one with: ssh-keygen -t ed25519"
159
+ )
160
+ path = Path.home() / ".ssh" / "id_ed25519"
161
+ path.parent.mkdir(mode=0o700, exist_ok=True)
162
+ subprocess.run(["ssh-keygen", "-q", "-t", "ed25519", "-f", str(path), "-N", ""], check=True)
163
+ cfg.ssh_public_key = path.with_suffix(".pub").read_text().strip()
164
+ ui.ok("ssh key generated ~/.ssh/id_ed25519")
165
+
166
+
167
+ def ensure_cloudflared() -> None:
168
+ found = cloudflared.find()
169
+ if found:
170
+ ui.ok(f"cloudflared {cloudflared.version(found)}")
171
+ return
172
+ ui.warn("cloudflared is missing; ssh uses it to reach the box")
173
+ if not ui.confirm(
174
+ f"Download Cloudflare's official build ({cloudflared.asset_name()})?", default=True
175
+ ):
176
+ raise KdevError("kdev needs cloudflared.", "Install it: brew install cloudflared")
177
+ path = ui.download("cloudflared", cloudflared.install)
178
+ ui.ok(f"cloudflared {cloudflared.version(path)}")
179
+
180
+
181
+ def sign_in(cfg: config.Config, name: str = "") -> str:
182
+ """Sign a Kaggle account in through the browser. Returns its account name."""
183
+ while not name:
184
+ try:
185
+ name = config.valid_profile_name(ui.ask("Name for this account", default="me"))
186
+ except ValueError as e:
187
+ ui.err(str(e))
188
+ name = config.valid_profile_name(name)
189
+ ui.hint("Your browser will open to sign in to Kaggle.")
190
+ try:
191
+ username = auth.login(name)
192
+ except auth.AuthError as e:
193
+ raise KdevError(str(e), f"Try again with: kdev account add {name}") from e
194
+ if not username:
195
+ raise KdevError(
196
+ "Kaggle did not say which account signed in.",
197
+ f"Try again with: kdev account add {name}",
198
+ )
199
+ clash = next((n for n, p in cfg.profiles.items() if p.username == username and n != name), "")
200
+ if clash:
201
+ auth.forget(name)
202
+ raise KdevError(
203
+ f"{username} is already signed in here as {clash!r}.",
204
+ f"Use it with: kdev account use {clash}",
205
+ )
206
+ cfg.add(name, config.Profile(username=username))
207
+ return name
208
+
209
+
210
+ def run() -> config.Config:
211
+ ui.header("setup")
212
+ cfg = config.load()
213
+
214
+ ensure_ssh_key(cfg)
215
+ ensure_cloudflared()
216
+
217
+ if cfg.profiles:
218
+ ui.ok(f"accounts {', '.join(cfg.profiles)}")
219
+ else:
220
+ ui.blank()
221
+ name = sign_in(cfg)
222
+ ui.ok(f"account {name} ({cfg.profiles[name].username})")
223
+ config.save(cfg)
224
+
225
+ # Workspace before tunnel: a second machine joining an existing workspace
226
+ # takes the tunnel the notebook already runs, and skips Cloudflare setup.
227
+ if cfg.notebook:
228
+ ui.ok(f"workspace {cfg.notebook}")
229
+ else:
230
+ ui.blank()
231
+ prof = cfg.profile()
232
+ if not nb.join(cfg, prof.creds, prof.username):
233
+ raise KdevError("No workspace chosen.", "Pick one later with: kdev workspace join")
234
+ taken = nb.adopt_config(cfg, cfg.profile().creds)
235
+ config.save(cfg)
236
+
237
+ if cfg.tunnel_credentials or cfg.tunnel_token:
238
+ source = "from the notebook" if any(k.startswith("tunnel_") for k in taken) else "set up"
239
+ ui.ok(f"tunnel {cfg.tunnel_hostname or 'token'} ({source})")
240
+ else:
241
+ ui.blank()
242
+ choose_tunnel(cfg)
243
+ config.save(cfg)
244
+
245
+ ui.blank()
246
+ ui.ok("ready", f"settings saved to {config.CONFIG_PATH}")
247
+ ui.blank()
248
+ ui.next_steps(
249
+ [("kdev up", "start the box"), ("kdev account add", "add another account to share quota")]
250
+ )
251
+ return cfg