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
//! `router deploy` command surface.
//!
//! Split from `main.rs` to keep that file within the repository's 1000-line
//! limit. The converge engine is in [`link_assistant_router::deploy`]; this file
//! resolves defaults, prints the report, and maps outcomes onto exit codes.
//!
//! The decisions here — which image and root a run uses when none is named, and
//! which exit code an outcome deserves — are separated from the printing so they
//! can be tested without a container runtime. They are worth testing: defaulting
//! to a moving image tag would defeat the immutable-reference check the converge
//! engine performs, and a refusal that exits `1` is indistinguishable from a
//! deployment failure to a script.
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use link_assistant_router::cli::DeployArgs;
use link_assistant_router::config::Config;
use link_assistant_router::deploy::{self, Plan, runtime::Docker};
/// Default image for a local deployment: this binary's own version.
///
/// A deployment of a *different* version than the CLI driving it is the exact
/// disagreement the immutable-reference rule exists to prevent, so the default is
/// pinned to the version that is running rather than to a moving tag.
fn default_image() -> String {
format!(
"ghcr.io/link-assistant/router:{}",
link_assistant_router::VERSION
)
}
/// Where a local deployment keeps its credential and data directories.
fn default_root(data_dir: &Path) -> PathBuf {
data_dir.join("deploy")
}
/// The plan a set of flags describes, with defaults filled in.
fn plan_for(args: &DeployArgs, data_dir: &Path, token_secret: &str) -> Plan {
let root = args
.root
.as_deref()
.map_or_else(|| default_root(data_dir), PathBuf::from);
let mut plan = Plan::local(
&root,
&args
.image
.as_deref()
.map_or_else(default_image, str::to_string),
token_secret,
);
plan.port = args.port;
plan.status_only = args.status;
plan.build_context = args.build.as_deref().map(PathBuf::from);
plan
}
/// Exit code for a removal outcome.
///
/// A refusal for want of consent exits `2`: it is a usage answer, and a script
/// that cannot tell it from `1` cannot tell "you forgot --yes" from "the
/// deployment is broken".
fn down_code(removed: bool) -> ExitCode {
if removed {
ExitCode::SUCCESS
} else {
ExitCode::from(2)
}
}
/// The closing line a converged run prints.
fn ready_line(plan: &Plan, skipped: bool) -> String {
if plan.status_only {
format!(
"{} on 127.0.0.1:{}",
if skipped {
"deployment is up with steps skipped"
} else {
"deployment is converged"
},
plan.port
)
} else {
format!(
"deployment is ready: `router with claude --server http://127.0.0.1:{}`",
plan.port
)
}
}
/// Why a run cannot proceed with the secret it was given, if it cannot.
///
/// A deployment signs its own tokens, so it needs a real secret. Without this
/// check the stand-in installed for non-serving commands reaches the container's
/// environment, where its NUL prefix surfaces as an opaque `nul byte found in
/// provided data` from the process API — and if it ever stopped doing so, the
/// deployment would sign tokens nothing can validate. Removal is exempt: it
/// names a container and deletes it, signing nothing.
fn secret_refusal(token_secret: &str, down: bool) -> Option<String> {
if down {
return None;
}
link_assistant_router::token_secret::ensure_real(token_secret)
.err()
.map(|error| {
format!(
"error: {error}\nnote: the deployment signs its own tokens, so pass \
TOKEN_SECRET in the environment."
)
})
}
pub fn run(config: &Config, args: &DeployArgs) -> ExitCode {
let runtime = Docker;
if let Some(refusal) = secret_refusal(&config.token_secret, args.down) {
eprintln!("{refusal}");
return ExitCode::from(2);
}
if args.down {
return match deploy::down(&runtime, args.yes) {
Ok(message) => {
println!("{message}");
down_code(true)
}
Err(error) => {
eprintln!("error: {error}");
down_code(false)
}
};
}
let plan = plan_for(args, &config.data_dir, &config.token_secret);
let report = deploy::converge(&runtime, &plan);
report.print();
if report.converged() {
println!("\n{}", ready_line(&plan, !report.skips().is_empty()));
ExitCode::SUCCESS
} else {
ExitCode::from(1)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn args() -> DeployArgs {
DeployArgs {
status: false,
down: false,
yes: false,
port: link_assistant_router::deploy::DEFAULT_PORT,
image: None,
build: None,
root: None,
}
}
#[test]
fn the_default_image_is_this_binarys_own_version_not_a_moving_tag() {
let plan = plan_for(&args(), Path::new("/tmp/state"), "secret");
// Defaulting to `latest` would make every unqualified run fail the
// immutable-reference check — or worse, deploy a container that disagrees
// with the CLI about the API contract.
assert!(
plan.image.ends_with(link_assistant_router::VERSION),
"{}",
plan.image
);
deploy::immutable_ref(&plan.image).expect("the default is deployable");
}
#[test]
fn a_named_image_and_root_are_used_verbatim() {
let mut args = args();
args.image = Some("ghcr.io/link-assistant/router@sha256:abc".to_string());
args.root = Some("/srv/router".to_string());
args.port = 19000;
let plan = plan_for(&args, Path::new("/tmp/state"), "secret");
assert_eq!(plan.image, "ghcr.io/link-assistant/router@sha256:abc");
assert_eq!(plan.credential_home, Path::new("/srv/router/credentials"));
assert_eq!(plan.data_home, Path::new("/srv/router/data"));
assert_eq!(plan.port, 19000);
}
#[test]
fn the_default_root_lives_under_the_data_directory() {
let plan = plan_for(&args(), Path::new("/var/lib/router"), "secret");
// Under the data directory rather than beside it, so a deployment's own
// state is not scattered across the filesystem.
assert_eq!(
plan.credential_home,
Path::new("/var/lib/router/deploy/credentials")
);
assert_eq!(plan.data_home, Path::new("/var/lib/router/deploy/data"));
// Separate paths: the credential mount is read-only and the request log
// cannot live on it.
assert_ne!(plan.credential_home, plan.data_home);
}
#[test]
fn status_and_build_flags_reach_the_plan() {
let mut args = args();
args.status = true;
args.build = Some("/src/router".to_string());
let plan = plan_for(&args, Path::new("/tmp/state"), "secret");
assert!(plan.status_only);
assert_eq!(
plan.build_context.as_deref(),
Some(Path::new("/src/router"))
);
}
#[test]
fn the_signing_secret_is_passed_through_rather_than_invented() {
let plan = plan_for(&args(), Path::new("/tmp/state"), "the-deployments-secret");
// The deployment must sign with the same secret the CLI would, or tokens
// minted here are rejected there.
assert_eq!(plan.token_secret, "the-deployments-secret");
}
#[test]
fn a_stand_in_secret_is_refused_before_a_container_is_created() {
let stand_in = link_assistant_router::token_secret::placeholder("cli-command");
let refusal = secret_refusal(&stand_in, false).expect("a stand-in is refused");
// The stand-in carries a NUL so it can never be supplied deliberately,
// which means it reaches the process API and fails there with `nul byte
// found in provided data` — a message that describes the mechanism and
// not the mistake. It is caught here instead, and a deployment is never
// created that would sign tokens nothing can validate.
assert!(refusal.contains("TOKEN_SECRET"), "{refusal}");
assert!(
!refusal.contains("nul byte"),
"the operator is told what to do, not what the process API said: {refusal}"
);
assert!(
!refusal.contains(&stand_in),
"the refusal does not echo the secret it rejected"
);
}
#[test]
fn a_real_secret_passes_and_removal_needs_none() {
assert!(secret_refusal("a-real-operator-secret", false).is_none());
// `--down` names a container and deletes it. Demanding a signing secret
// to tear down a deployment would make a broken one unremovable by the
// operator who most needs to remove it.
assert!(
secret_refusal(
&link_assistant_router::token_secret::placeholder("cli-command"),
true
)
.is_none()
);
}
#[test]
fn a_refused_removal_exits_two_rather_than_one() {
// `1` means the deployment failed; `2` means the command was not asked
// correctly. A script that cannot tell them apart cannot retry safely.
assert_eq!(
format!("{:?}", down_code(false)),
format!("{:?}", ExitCode::from(2))
);
assert_eq!(
format!("{:?}", down_code(true)),
format!("{:?}", ExitCode::SUCCESS)
);
}
#[test]
fn the_closing_line_tells_the_operator_what_to_do_next() {
let plan = plan_for(&args(), Path::new("/tmp/state"), "secret");
let ready = ready_line(&plan, false);
assert!(
ready.contains("router with claude"),
"a converged deploy names the next command: {ready}"
);
assert!(ready.contains(&plan.port.to_string()), "{ready}");
let mut reporting = plan;
reporting.status_only = true;
// `--status` reports rather than instructs, and it distinguishes a fully
// converged deployment from one that came up with steps skipped.
assert!(ready_line(&reporting, false).contains("converged"));
assert!(ready_line(&reporting, true).contains("skipped"));
}
}