add a default archetype, preselected by the clients
This commit is contained in:
parent
a74d23d313
commit
8f7c45d40f
4 changed files with 253 additions and 47 deletions
|
|
@ -42,6 +42,7 @@ pub(crate) fn create(
|
|||
target: &Target,
|
||||
name: &str,
|
||||
hive: &str,
|
||||
archetype: Option<&str>,
|
||||
agents_md: Option<&Path>,
|
||||
) -> Result<()> {
|
||||
// Client-side before the round trip, so a typo is an immediate local
|
||||
|
|
@ -57,7 +58,7 @@ pub(crate) fn create(
|
|||
})
|
||||
.transpose()?;
|
||||
|
||||
let resp = request_create(target, &name, &hive, agents_md.as_deref())?;
|
||||
let resp = request_create(target, &name, &hive, archetype, agents_md.as_deref())?;
|
||||
|
||||
println!("queued: job node {}", resp.node_id);
|
||||
println!(
|
||||
|
|
@ -73,18 +74,20 @@ pub(crate) fn create(
|
|||
}
|
||||
|
||||
/// The round trip of [`create`] for an already validated `name` and `hive`,
|
||||
/// printing nothing.
|
||||
/// printing nothing. `archetype` names an archetype of the `hyperhive` source;
|
||||
/// `None` creates a custom agent with a config repo.
|
||||
pub(crate) fn request_create(
|
||||
target: &Target,
|
||||
name: &str,
|
||||
hive: &str,
|
||||
archetype: Option<&str>,
|
||||
agents_md: Option<&str>,
|
||||
) -> Result<CreateAgentResponse> {
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.context("starting a tokio runtime for the controller request")?;
|
||||
rt.block_on(post_create(target, name, hive, agents_md))
|
||||
rt.block_on(post_create(target, name, hive, archetype, agents_md))
|
||||
}
|
||||
|
||||
/// Parse a CLI-supplied name into a validated identifier, naming which
|
||||
|
|
@ -184,6 +187,7 @@ async fn post_create(
|
|||
target: &Target,
|
||||
name: &str,
|
||||
hive: &str,
|
||||
archetype: Option<&str>,
|
||||
agents_md: Option<&str>,
|
||||
) -> Result<CreateAgentResponse> {
|
||||
post(
|
||||
|
|
@ -196,7 +200,10 @@ async fn post_create(
|
|||
inference: None,
|
||||
permissions: None,
|
||||
unrestricted_internet: None,
|
||||
archetype: None,
|
||||
archetype: archetype.map(|name| swarm_wire::agent_archetype::ArchetypeRef {
|
||||
source: "hyperhive".to_owned(),
|
||||
name: name.to_owned(),
|
||||
}),
|
||||
agents_md: agents_md.map(str::to_owned),
|
||||
},
|
||||
"create-agent",
|
||||
|
|
|
|||
|
|
@ -281,7 +281,9 @@ pub(crate) trait Actions {
|
|||
fn make_admin(&mut self, name: &str) -> Result<String>;
|
||||
/// `ruth`, with the permissions of a manager, declared `up`.
|
||||
fn declare_ruth(&mut self, hive: &str, exists: bool) -> Result<String>;
|
||||
fn create_agent(&mut self, name: &str, hive: &str) -> Result<String>;
|
||||
/// An agent of the `default` archetype, or a custom one with its own config
|
||||
/// repo when `custom`.
|
||||
fn create_agent(&mut self, name: &str, hive: &str, custom: bool) -> Result<String>;
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
|
|
@ -748,7 +750,12 @@ fn other_agent(ui: &mut impl Ui, actions: &mut impl Actions, session: &mut Sessi
|
|||
let hive = ask_valid(ui, "Hive", session.hive.as_deref(), |h| {
|
||||
crate::agent::parse_ident(h, "hive").map(drop)
|
||||
})?;
|
||||
let said = ui.working("creating the agent", || actions.create_agent(&name, &hive))?;
|
||||
let custom =
|
||||
ui.ask("Custom agent with its own config repo, instead of the default archetype? [y/N]: ")?;
|
||||
let custom = matches!(custom.trim().to_lowercase().as_str(), "y" | "yes");
|
||||
let said = ui.working("creating the agent", || {
|
||||
actions.create_agent(&name, &hive, custom)
|
||||
})?;
|
||||
session.hive = Some(hive);
|
||||
ui.say(Tone::Good, &format!("✔ {said}"));
|
||||
Ok(())
|
||||
|
|
@ -944,8 +951,8 @@ mod tests {
|
|||
Ok("ruth is declared up".to_owned())
|
||||
}
|
||||
|
||||
fn create_agent(&mut self, name: &str, hive: &str) -> Result<String> {
|
||||
self.note(&format!("create_agent:{name}:{hive}"));
|
||||
fn create_agent(&mut self, name: &str, hive: &str, custom: bool) -> Result<String> {
|
||||
self.note(&format!("create_agent:{name}:{hive}:custom={custom}"));
|
||||
self.world.others += 1;
|
||||
Ok("queued".to_owned())
|
||||
}
|
||||
|
|
@ -982,8 +989,8 @@ mod tests {
|
|||
fn declare_ruth(&mut self, _: &str, _: bool) -> Result<String> {
|
||||
Ok("declared".to_owned())
|
||||
}
|
||||
fn create_agent(&mut self, name: &str, hive: &str) -> Result<String> {
|
||||
self.0.create_agent(name, hive)
|
||||
fn create_agent(&mut self, name: &str, hive: &str, custom: bool) -> Result<String> {
|
||||
self.0.create_agent(name, hive, custom)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1445,6 +1452,22 @@ mod tests {
|
|||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn another_agent_is_a_default_archetype_agent_unless_asked_custom() {
|
||||
let created = |custom_answer: &'static str| {
|
||||
let log = log();
|
||||
let mut actions = Fake::new(World::fresh(), &log);
|
||||
let mut script: Vec<&str> = FULL_RUN[..FULL_RUN.len() - 1].to_vec();
|
||||
script.extend(["y", "scribe", "", custom_answer]);
|
||||
let mut ui = Script::new(&script, &log);
|
||||
run(&mut ui, &mut actions).expect("the run finishes");
|
||||
calls(&log, "create_agent")
|
||||
};
|
||||
assert_eq!(created(""), ["create_agent:scribe:firsthive:custom=false"]);
|
||||
assert_eq!(created("n"), ["create_agent:scribe:firsthive:custom=false"]);
|
||||
assert_eq!(created("y"), ["create_agent:scribe:firsthive:custom=true"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_step_that_the_check_still_finds_open_is_not_taken_as_done() {
|
||||
let log = log();
|
||||
|
|
|
|||
|
|
@ -590,6 +590,22 @@ struct AgentCreateArgs {
|
|||
/// hive roster and names the known hives if it misses.
|
||||
#[arg(long, value_name = "HIVE")]
|
||||
hive: String,
|
||||
/// Archetype of the `hyperhive` source to build the agent from.
|
||||
///
|
||||
/// `default` is the default config with no config repo; turn the agent
|
||||
/// into a custom one later when it needs a repo. The hive's `hyperhive`
|
||||
/// input must know the archetype, or the agent's build fails.
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "NAME",
|
||||
default_value = "default",
|
||||
conflicts_with = "custom"
|
||||
)]
|
||||
archetype: String,
|
||||
/// Create a custom agent: it gets an `agent-configs/<name>` repo and no
|
||||
/// archetype.
|
||||
#[arg(long)]
|
||||
custom: bool,
|
||||
/// A text file the agent's `AGENTS.md` starts with.
|
||||
///
|
||||
/// The agent writes it on its first start, and only when it has no
|
||||
|
|
@ -606,6 +622,13 @@ struct AgentCreateArgs {
|
|||
controller_socket: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl AgentCreateArgs {
|
||||
/// The archetype to send: `--archetype`, or none for `--custom`.
|
||||
fn requested_archetype(&self) -> Option<&str> {
|
||||
(!self.custom).then_some(self.archetype.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
struct AgentMintForgeTokenArgs {
|
||||
/// Name of an agent that already exists.
|
||||
|
|
@ -838,8 +861,15 @@ fn main() -> Result<()> {
|
|||
Verb::Agent {
|
||||
command: AgentVerb::Create(args),
|
||||
} => {
|
||||
let archetype = args.requested_archetype().map(str::to_owned);
|
||||
let target = remote.target(args.controller_socket)?;
|
||||
agent::create(&target, &args.name, &args.hive, args.agents_md.as_deref())
|
||||
agent::create(
|
||||
&target,
|
||||
&args.name,
|
||||
&args.hive,
|
||||
archetype.as_deref(),
|
||||
args.agents_md.as_deref(),
|
||||
)
|
||||
}
|
||||
// Same socket-resolution reasoning as `Create` above.
|
||||
Verb::Agent {
|
||||
|
|
@ -1729,6 +1759,30 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn agent_create_defaults_to_the_default_archetype_and_custom_sends_none() {
|
||||
let args = |extra: &[&str]| {
|
||||
let base = ["swarmctl", "agent", "create", "scribe", "--hive", "alpha"];
|
||||
Cli::try_parse_from(base.iter().chain(extra))
|
||||
};
|
||||
let archetype_of = |cli: Cli| {
|
||||
let Verb::Agent {
|
||||
command: AgentVerb::Create(args),
|
||||
} = cli.command
|
||||
else {
|
||||
panic!("expected `agent create`");
|
||||
};
|
||||
args.requested_archetype().map(str::to_owned)
|
||||
};
|
||||
assert_eq!(archetype_of(args(&[]).unwrap()).as_deref(), Some("default"));
|
||||
assert_eq!(
|
||||
archetype_of(args(&["--archetype", "reviewer"]).unwrap()).as_deref(),
|
||||
Some("reviewer")
|
||||
);
|
||||
assert_eq!(archetype_of(args(&["--custom"]).unwrap()), None);
|
||||
assert!(args(&["--custom", "--archetype", "reviewer"]).is_err());
|
||||
}
|
||||
|
||||
/// The socket is optional on the command line because the nix wrapper
|
||||
/// sets `SWARM_CONTROLLER_SOCKET`; `path_from` is what turns an absent
|
||||
/// pair into an error rather than a guess.
|
||||
|
|
|
|||
|
|
@ -322,7 +322,7 @@ impl Actions for Live {
|
|||
let target = self.target()?;
|
||||
let hive = agent::parse_ident(hive, "hive")?;
|
||||
if !exists {
|
||||
agent::request_create(target, RUTH, &hive, None)?;
|
||||
agent::request_create(target, RUTH, &hive, None, None)?;
|
||||
}
|
||||
let every = Duration::from_secs(2);
|
||||
ensure_manager_permission(
|
||||
|
|
@ -339,10 +339,11 @@ impl Actions for Live {
|
|||
))
|
||||
}
|
||||
|
||||
fn create_agent(&mut self, name: &str, hive: &str) -> Result<String> {
|
||||
fn create_agent(&mut self, name: &str, hive: &str, custom: bool) -> Result<String> {
|
||||
let name = agent::parse_ident(name, "agent name")?;
|
||||
let hive = agent::parse_ident(hive, "hive")?;
|
||||
let resp = agent::request_create(self.target()?, &name, &hive, None)?;
|
||||
let archetype = (!custom).then_some("default");
|
||||
let resp = agent::request_create(self.target()?, &name, &hive, archetype, None)?;
|
||||
let mut said = format!(
|
||||
"queued job node {} to deploy {name:?} to hive {hive:?}",
|
||||
resp.node_id
|
||||
|
|
|
|||
Loading…
Reference in a new issue