Skip to content

Commit

Permalink
Use id for start/stop/uninstall
Browse files Browse the repository at this point in the history
no regex is constructed anymore
makes it unambigous which component is meant
  • Loading branch information
marcmo committed Aug 28, 2020
1 parent f4e3bc9 commit 9f07e91
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 178 deletions.
71 changes: 27 additions & 44 deletions north/src/command_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub async fn init(tx: &EventTx) -> Result<()> {
let tx = tx.clone();
// Spawn a task that handles messages received on the command port.
task::spawn(async move {
while let Ok((line, tx_reply)) = rx.recv().await {
tx.send(Event::Command(line, tx_reply)).await;
while let Ok((command, tx_reply)) = rx.recv().await {
tx.send(Event::Command(command, tx_reply)).await;
}
});

Expand All @@ -52,28 +52,16 @@ pub async fn process(
Some(api::Request_oneof_instruction::list_containers(_)) => list(state).await,
Some(api::Request_oneof_instruction::running_containers(_)) => ps(state).await,
Some(api::Request_oneof_instruction::start_container(ref id)) => {
let regex = if id.has_regex() {
Some(id.get_regex())
} else {
None
};
start(state, regex).await
let id_str = if id.has_id() { Some(id.get_id()) } else { None };
start(state, id_str).await
}
Some(api::Request_oneof_instruction::stop_container(ref id)) => {
let regex = if id.has_regex() {
Some(id.get_regex())
} else {
None
};
stop(state, regex).await
let id_str = if id.has_id() { Some(id.get_id()) } else { None };
stop(state, id_str).await
}
Some(api::Request_oneof_instruction::uninstall_container(ref id)) => {
let regex = if id.has_regex() {
Some(id.get_regex())
} else {
None
};
uninstall(state, regex).await
let id_str = if id.has_id() { Some(id.get_id()) } else { None };
uninstall(state, id_str).await
}
Some(api::Request_oneof_instruction::update(ref dir)) => {
update(state, dir.get_update_dir()).await
Expand Down Expand Up @@ -156,15 +144,13 @@ async fn ps(state: &State) -> Result<api::Response> {
Ok(response)
}

/// Start applications. If no regex string is provided, *all* known applications that
/// are not in a running state are started. If a regex string is supplied it
/// is used to construct a Regex and all container (names) matching that
/// Regex are attempted to be started.
async fn start(state: &mut State, regex_str: Option<&str>) -> Result<api::Response> {
/// Start applications. If no id is provided, *all* known applications that
/// are not in a running state are started. If an id is supplied only a container
/// with this id is started
async fn start(state: &mut State, id_str: Option<&str>) -> Result<api::Response> {
let mut response = api::Response::new();
let mut started_containers = api::StartedResponse::default();
let startup_list = started_containers.mut_startup_list();
let re = id_to_regex(regex_str)?;

let apps = state
.applications()
Expand All @@ -173,7 +159,10 @@ async fn start(state: &mut State, regex_str: Option<&str>) -> Result<api::Respon
// Filter ressource container that are not startable
.filter(|app| !app.container().is_resource_container())
// Filter matching container
.filter(|app| re.is_match(app.name()))
.filter(|app| match id_str {
Some(id) => id == app.name(),
None => true,
})
// Sort container by name
.sorted_by_key(|app| app.name().clone())
.map(|app| app.name().clone())
Expand Down Expand Up @@ -228,15 +217,18 @@ async fn versions(state: &mut State) -> Result<api::Response> {
}

/// Stop one, some or all containers. See start for the argument handling.
async fn stop(state: &mut State, regex_str: Option<&str>) -> Result<api::Response> {
async fn stop(state: &mut State, id_str: Option<&str>) -> Result<api::Response> {
let mut response = api::Response::new();
let mut stopped_containers = api::StoppedResponse::default();
let stop_list = stopped_containers.mut_stop_list();
let re = id_to_regex(regex_str)?;

let apps = state
.applications()
.filter(|app| app.process_context().is_some())
.filter(|app| re.is_match(app.name()))
.filter(|app| match id_str {
Some(id) => id == app.name(),
None => true,
})
.map(|app| app.name().clone())
.collect::<Vec<Name>>();
for app in &apps {
Expand Down Expand Up @@ -265,17 +257,19 @@ async fn stop(state: &mut State, regex_str: Option<&str>) -> Result<api::Respons

/// Umount and remove a containers. See `start` for the argument handling.
/// The data directory is not removed. This needs discussion.
async fn uninstall(state: &mut State, regex_str: Option<&str>) -> Result<api::Response> {
async fn uninstall(state: &mut State, id_str: Option<&str>) -> Result<api::Response> {
let mut response = api::Response::new();
let mut uninstalled_containers = api::UninstallResponse::default();
let uninstall_list = uninstalled_containers.mut_status();
let re = id_to_regex(regex_str)?;

let to_uninstall = state
.applications
.values()
.filter(|app| app.process_context().is_none())
.filter(|app| re.is_match(app.name()))
.filter(|app| match id_str {
Some(id) => id == app.name(),
None => true,
})
.map(|app| app.name())
.cloned()
.collect::<Vec<Name>>();
Expand Down Expand Up @@ -391,14 +385,3 @@ async fn serve() -> Result<sync::Receiver<(Vec<u8>, sync::Sender<Vec<u8>>)>> {
});
Ok(rx)
}

fn id_to_regex(regex_str: Option<&str>) -> Result<regex::Regex> {
match regex_str {
Some(s) => {
regex::Regex::new(s).with_context(|| format!("Invalid container name regex {}", s))
}
None => {
regex::Regex::new(".*").with_context(|| "Invalid container name regex .*".to_string())
}
}
}
6 changes: 3 additions & 3 deletions north_api/src/proto/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ message Response {

message ListRequest {}
message PsRequest {}
message StartRequest { optional string regex = 1; }
message StopRequest { optional string regex = 1; }
message UninstallRequest { optional string regex = 1; }
message StartRequest { optional string id = 1; }
message StopRequest { optional string id = 1; }
message UninstallRequest { optional string id = 1; }
message UpdateRequest { required string update_dir = 1; }
message ShutdownRequest {}
message GetSettingsRequest {}
Expand Down
Loading

0 comments on commit 9f07e91

Please sign in to comment.