Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions crates/berth-core/src/provisioner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub const SECRETS: &str = "secrets";
pub const REMOVE: &str = "remove";
/// berth's own action that makes a provisioner registered by hand a node's.
pub const ADOPT: &str = "adopt";
/// berth's own action that gives a registered provisioner another name.
pub const RENAME: &str = "rename";
/// The start of berth's own action that makes a resource of a kind, such as
/// `new-deployment`.
pub const NEW: &str = "new-";
Expand Down Expand Up @@ -250,6 +252,113 @@ pub fn remove_entry(paths: &Paths, store: &Store, name: &str) -> Result<()> {
Ok(())
}

/// Why the registered provisioner `old` cannot be called `new`, if it cannot.
pub fn rename_problem(paths: &Paths, old: &str, new: &str) -> Result<Option<String>> {
if entry(paths, old)?.is_none() {
return Err(
format!("No provisioner {old} is registered. See: berth provisioner list").into(),
);
}
Ok(if !plugins::valid_name(new) {
Some(format!(
"{new} is not a name for a provisioner: lowercase letters, digits, - and _, starting with a letter"
))
} else if new == old {
Some(format!("{old} has that name already"))
} else if entry(paths, new)?.is_some() {
Some(format!("There is a provisioner {new} already"))
} else {
None
})
}

/// Give the registered provisioner `old` the name `new`, in one change: its
/// entry with its settings and the node it is of, the secrets it keeps of its
/// own, the records of what it made and of the machines planned with it, and
/// its jobs. Each resource keeps its id, and the machines and what runs on
/// them are not touched. The caller holds the lock of `old`, as its job does;
/// the lock of `new` and those of what it made are taken here, so that no
/// work on one of them saves its record with the old name meanwhile.
pub fn rename(paths: &Paths, store: &Store, old: &str, new: &str) -> Result<()> {
if let Some(problem) = rename_problem(paths, old, new)? {
return Err(problem.into());
}
let _new = crate::runner::lock(
paths,
&crate::runner::Subject::Provisioner(new.to_owned()),
"rename the provisioner",
)?;
let _held = crate::resource::list(paths)?
.iter()
.filter(|kept| kept.record.provisioner == old)
.map(|kept| kept.lock(paths))
.collect::<Result<Vec<_>>>()?;
let shared = store::shared(paths)?;
let mut writes: Vec<(&str, String, String, Vec<u8>)> = vec![];
let mut removals: Vec<(&str, String, String)> = vec![];
let mut moved = |kind: &'static str, scope: &str, from: String, to: String, value: Vec<u8>| {
writes.push((kind, scope.to_owned(), to, value));
removals.push((kind, scope.to_owned(), from));
};
for kind in [kind::PROVISIONER_ENTRY, kind::PROVISIONER] {
if let Some(value) = shared.read(kind, OWN, old)? {
moved(kind, OWN, old.to_owned(), new.to_owned(), value);
}
}
let (from, to) = (secret_name(old, ""), secret_name(new, ""));
let mut stale = vec![];
for secret in store.all_secret_names()? {
if let Some(own) = secret.strip_prefix(&from) {
if let Some(value) = shared.read(kind::SECRET, OWN, &secret)? {
moved(
kind::SECRET,
OWN,
secret.clone(),
format!("{to}{own}"),
value,
);
}
} else if secret.starts_with(&to) {
// Left by a provisioner of that name before; the renamed one has its own.
stale.push(secret);
}
}
for scope in [OWN, crate::profiles::FILE_SCOPE] {
for (id, value) in shared.entries(kind::RESOURCE, scope)? {
let mut record: crate::resource::Record =
serde_json::from_slice(&value).map_err(|error| {
format!("The record of the resource {id} in berth's store: {error}")
})?;
if record.provisioner == old {
record.provisioner = new.to_owned();
writes.push((
kind::RESOURCE,
scope.to_owned(),
id,
serde_json::to_vec(&record)?,
));
}
}
}
let written: Vec<(&str, &str, &str, Vec<u8>)> = writes
.iter()
.map(|(kind, scope, name, value)| (*kind, scope.as_str(), name.as_str(), value.clone()))
.collect();
let mut removed: Vec<(&str, &str, &str)> = removals
.iter()
.map(|(kind, scope, name)| (*kind, scope.as_str(), name.as_str()))
.collect();
removed.extend(
stale
.iter()
.filter(|secret| !written.iter().any(|(_, _, name, _)| name == secret))
.map(|secret| (kind::SECRET, OWN, secret.as_str())),
);
let subject = |name: &str| crate::runner::Subject::Provisioner(name.to_owned()).key();
shared.rename_subject(&subject(old), &subject(new), &written, &removed)?;
Ok(())
}

/// The values of a provisioner's own arguments that berth keeps, by name.
pub fn settings(paths: &Paths, name: &str) -> Result<BTreeMap<String, String>> {
if let Some(entry) = entry(paths, name)? {
Expand Down Expand Up @@ -608,6 +717,7 @@ pub fn actions(paths: &Paths, store: &Store, name: &str) -> Result<Vec<Action>>
if entry(paths, name)?.is_some_and(|kept| kept.node.is_empty()) {
actions.push(adopt_action(name));
}
actions.push(rename_action(name));
actions.push(remove_action(paths, name));
}
Ok(actions)
Expand Down Expand Up @@ -741,6 +851,27 @@ fn adopt_action(name: &str) -> Action {
}
}

/// berth's own action that gives a registered provisioner another name.
fn rename_action(name: &str) -> Action {
Action {
name: RENAME.into(),
label: "Rename".into(),
help: format!(
"Give {name} another name in berth. Its settings, the secrets it keeps of its own, the resources and machines it made or plans, and its jobs go with it. What it made is not touched, and a node's provisioner stays the node's."
),
group: "Set-Up".into(),
arguments: vec![Argument {
name: "name".into(),
label: "New name".into(),
required: true,
help: "Lowercase letters, digits, - and _, starting with a letter".into(),
..Argument::default()
}],
role: Role::Admin,
..Action::default()
}
}

/// berth's own action that forgets a registered provisioner, which it does
/// only when it holds nothing.
fn remove_action(paths: &Paths, name: &str) -> Action {
Expand Down Expand Up @@ -800,6 +931,7 @@ enum Doing {
Secrets(BTreeMap<String, String>, Store),
Remove(Store),
Adopt(String),
Rename(String, Store),
Create(Box<crate::resource::Creating>, Store),
Program {
dir: PathBuf,
Expand Down Expand Up @@ -918,6 +1050,22 @@ pub fn prepare(
work: Doing::Adopt(values.get("node").cloned().unwrap_or_default()),
});
}
if action == RENAME {
let values = values_of(name, &found.arguments, given, &BTreeMap::new())?;
let new = values
.get("name")
.map(|new| new.trim().to_owned())
.unwrap_or_default();
if let Some(problem) = rename_problem(paths, name, &new)? {
return Err(problem.into());
}
return Ok(Acting {
name: name.to_owned(),
hidden: vec![],
action: found,
work: Doing::Rename(new, store.clone()),
});
}
if let Some(kind) = action
.strip_prefix(NEW)
.filter(|kind| provisioner.kinds.iter().any(|known| known.id == *kind))
Expand Down Expand Up @@ -1092,6 +1240,14 @@ impl Acting {
details: vec![],
exit_code: None,
}),
Doing::Rename(new, store) => {
rename(paths, &store, &self.name, &new)?;
Ok(Answered {
message: format!("{} is called {new} now", self.name),
details: vec![],
exit_code: None,
})
}
Doing::Create(creating, store) => {
let id = creating.kept().record.id.clone();
let title = creating.kept().title();
Expand Down
68 changes: 66 additions & 2 deletions crates/berth-core/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ pub struct Filter<'a> {
pub limit: usize,
}

/// Which jobs a list has, newest first.
#[derive(Debug, Clone, Default)]
pub struct JobFilter<'a> {
/// Only those of this subject, as [`crate::runner::Subject::key`] says it.
pub subject: Option<&'a str>,
/// Only those about this host, alone or among others.
pub host: Option<&'a str>,
/// Only those that have not ended, or only those that have.
pub running: Option<bool>,
/// Only those that started before the job of this id.
pub before: Option<&'a str>,
pub limit: usize,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Entry {
pub key: String,
Expand Down Expand Up @@ -393,7 +407,9 @@ pub trait Backend: Send {
fn stop_job(&mut self, id: &str, by: &str) -> Result<()>;
fn end_lost_jobs(&mut self, ended: &str) -> Result<usize>;
fn job(&mut self, id: &str) -> Result<Option<Job>>;
fn jobs(&mut self, limit: usize) -> Result<Vec<Job>>;
/// The jobs `filter` lets through, newest first. A `before` that names
/// no job lets none through.
fn jobs(&mut self, filter: &JobFilter<'_>) -> Result<Vec<Job>>;

/// Drop messages from before `cutoff` and jobs that ended and started
/// before it, in one transaction. Returns the dropped jobs' output files.
Expand Down Expand Up @@ -428,6 +444,17 @@ pub trait Backend: Send {
writes: &[StateRow],
removals: &[(String, String, String)],
) -> Result<()>;
/// In one transaction: the change of [`Backend::change_states`], and the
/// jobs of the subject `old`, such as `provisioner:NAME`, given to `new`.
/// The tokens of jobs `new` had before go, so that asking again does not
/// find them.
fn rename_subject(
&mut self,
old: &str,
new: &str,
writes: &[StateRow],
removals: &[(String, String, String)],
) -> Result<()>;
/// In one transaction, while nobody else writes to the table: give each
/// row the value and `sealed` of its new version, leaving `updated` as
/// it is. False, with nothing changed, when the table no longer holds
Expand Down Expand Up @@ -643,7 +670,20 @@ impl Store {

/// Jobs, newest first.
pub fn jobs(&self, limit: usize) -> Result<Vec<Job>> {
self.backend().jobs(limit.clamp(1, 1000))
self.find_jobs(&JobFilter {
limit,
..JobFilter::default()
})
}

/// The jobs `filter` lets through, newest first: at most its limit,
/// which is between 1 and 1,000.
pub fn find_jobs(&self, filter: &JobFilter<'_>) -> Result<Vec<Job>> {
let filter = JobFilter {
limit: filter.limit.clamp(1, 1000),
..filter.clone()
};
self.backend().jobs(&filter)
}

// ── Pruning ─────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -816,6 +856,30 @@ impl Store {
self.backend().rename_host(old, new, &rows, &removals)
}

/// A change as [`Store::change`] makes it, in the same transaction as
/// giving the jobs of the subject `old` to `new`, both as
/// [`crate::runner::Subject::key`] says them.
pub fn rename_subject(
&self,
old: &str,
new: &str,
writes: &[(&str, &str, &str, Vec<u8>)],
removals: &[(&str, &str, &str)],
) -> Result<()> {
let updated = now();
let rows = writes
.iter()
.map(|(kind, scope, name, value)| self.row(kind, scope, name, value, &updated))
.collect::<Result<Vec<_>>>()?;
let removals: Vec<_> = removals
.iter()
.map(|(kind, scope, name)| {
((*kind).to_owned(), (*scope).to_owned(), (*name).to_owned())
})
.collect();
self.backend().rename_subject(old, new, &rows, &removals)
}

/// The ids of the keys the sealed rows of `state` were made with.
pub fn key_ids(&self) -> Result<BTreeSet<[u8; 8]>> {
let rows = self.backend().all_states()?;
Expand Down
50 changes: 45 additions & 5 deletions crates/berth-core/src/store/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//! answer.

use super::{
Backend, CursorRow, Entry, Error, Filter, Job, KvRow, MessageRow, Result, Rows, StateRow,
Stored, Table, When, json_value, keep_from, keep_name,
Backend, CursorRow, Entry, Error, Filter, Job, JobFilter, KvRow, MessageRow, Result, Rows,
StateRow, Stored, Table, When, json_value, keep_from, keep_name,
};
use berth_api::{From, Keep, Level, Message};
use postgres::config::SslMode;
Expand Down Expand Up @@ -743,11 +743,22 @@ impl Backend for PostgresBackend {
})
}

fn jobs(&mut self, limit: usize) -> Result<Vec<Job>> {
fn jobs(&mut self, filter: &JobFilter<'_>) -> Result<Vec<Job>> {
let subject = filter.subject.map(str::to_owned);
let host = filter.host.map(str::to_owned);
let (running, before) = (filter.running, filter.before.map(str::to_owned));
let limit = filter.limit as i64;
self.call(move |client| {
let rows = client.query(
&format!("SELECT {JOB_COLUMNS} FROM jobs ORDER BY started DESC, id DESC LIMIT $1"),
&[&(limit as i64)],
&format!(
"SELECT {JOB_COLUMNS} FROM jobs
WHERE ($1::TEXT IS NULL OR subject = $1)
AND ($2::TEXT IS NULL OR strpos(', ' || host || ', ', ', ' || $2 || ', ') > 0)
AND ($3::BOOLEAN IS NULL OR (ended IS NULL) = $3)
AND ($4::TEXT IS NULL OR (started, id) < (SELECT started, id FROM jobs WHERE id = $4))
ORDER BY started DESC, id DESC LIMIT $5"
),
&[&subject, &host, &running, &before, &limit],
)?;
Ok(rows.iter().map(job).collect())
})
Expand Down Expand Up @@ -901,6 +912,35 @@ impl Backend for PostgresBackend {
})
}

fn rename_subject(
&mut self,
old: &str,
new: &str,
writes: &[StateRow],
removals: &[(String, String, String)],
) -> Result<()> {
let (writes, removals) = (writes.to_vec(), removals.to_vec());
let (old, new) = (old.to_owned(), new.to_owned());
self.call(move |client| {
let mut transaction = client.transaction()?;
for row in &writes {
put_state(&mut transaction, row)?;
}
for (kind, scope, name) in &removals {
transaction.execute(
"DELETE FROM state WHERE kind = $1 AND scope = $2 AND name = $3",
&[kind, scope, name],
)?;
}
transaction.execute("UPDATE jobs SET token = NULL WHERE subject = $1", &[&new])?;
transaction.execute(
"UPDATE jobs SET subject = $2 WHERE subject = $1",
&[&old, &new],
)?;
transaction.commit()
})
}

fn swap_states(&mut self, rows: &[(StateRow, StateRow)]) -> Result<bool> {
let rows = rows.to_vec();
self.call(move |client| {
Expand Down
Loading
Loading