Skip to content

Commit

Permalink
Move help to client
Browse files Browse the repository at this point in the history
rename console -> command_processor
  • Loading branch information
marcmo committed Aug 17, 2020
1 parent 7befad3 commit 51e6e25
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 653 deletions.
35 changes: 2 additions & 33 deletions north/src/console.rs → north/src/command_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,13 @@ use north_api::proto::api;
use protobuf::{parse_from_bytes, Message};
use std::time;

/// Helptext displayed on the `help` command. The `dcon` tool parses this text
/// and creates suggestions and completions. Ensure to a correct helptext when
/// adding/removing/changing commands.
const HELP: &str = "\
help: Display help text\n\
list: List all loaded images\n\
ps: List running instances\n\
shutdown: Stop the north runtime\n\
settings: Dump north configuration\n\
start: PATTERN Start containers matching PATTERN e.g 'start hello*'. Omit PATTERN to start all containers\n\
stop: PATTERN Stop all containers matching PATTERN. Omit PATTERN to stop all running containers\n\
uninstall: PATTERN: Unmount and remove all containers matching PATTERN\n\
update: Run update with provided ressources\n\
versions: Version list of installed applications";

pub async fn init(tx: &EventTx) -> Result<()> {
let rx = serve().await?;
let tx = tx.clone();
// Spawn a task that handles lines received on the debug port.
task::spawn(async move {
while let Ok((line, tx_reply)) = rx.recv().await {
tx.send(Event::Console(line, tx_reply)).await;
tx.send(Event::Command(line, tx_reply)).await;
}
});

Expand All @@ -54,21 +39,15 @@ pub async fn process(
command: &[u8],
reply: sync::Sender<Vec<u8>>,
) -> Result<()> {
debug!("process an incomming command: {:#X?}", command);
match parse_from_bytes::<api::Command>(&command) {
Ok(_) => (),
Err(e) => debug!("could not parse from bytes: {}", e),
Err(e) => debug!("Could not parse from bytes: {}", e),
}
let command = parse_from_bytes::<api::Command>(&command)?;
debug!("parsing done");
info!("Running command:\'{}\'", command.get_description());
// let mut commands = command.split_whitespace();

// if let Some(cmd) = commands.next() {
// let args = commands.collect::<Vec<&str>>();
let start_timestamp = time::Instant::now();
let response: Result<api::Response> = match command.instruction {
Some(api::Command_oneof_instruction::help_text(_)) => help(),
Some(api::Command_oneof_instruction::list_containers(_)) => list(state).await,
Some(api::Command_oneof_instruction::running_containers(_)) => ps(state).await,
Some(api::Command_oneof_instruction::start_container(ref id)) => {
Expand Down Expand Up @@ -123,16 +102,6 @@ pub async fn process(
Ok(())
}

/// Return the help text
fn help() -> Result<api::Response> {
debug!("sending back help");
let mut response = api::Response::new();
let mut help_info = api::HelpInfo::new();
help_info.set_content(HELP.into());
response.content = Some(api::Response_oneof_content::help_response(help_info));
Ok(response)
}

/// List all known containers instances and their state.
async fn list(state: &State) -> Result<api::Response> {
let mut response = api::Response::new();
Expand Down
11 changes: 5 additions & 6 deletions north/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use nix::unistd::{self, chown};
use north_common::manifest::Name;
use std::collections::HashMap;

mod console;
mod command_processor;
#[cfg(any(target_os = "android", target_os = "linux"))]
pub mod linux;
mod npk;
Expand All @@ -46,8 +46,8 @@ pub type EventTx = sync::Sender<Event>;
#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
pub enum Event {
/// Incomming console command
Console(Vec<u8>, sync::Sender<Vec<u8>>),
/// Incomming command
Command(Vec<u8>, sync::Sender<Vec<u8>>),
/// A instance exited with return code
Exit(Name, i32),
/// Out of memory event occured
Expand Down Expand Up @@ -155,7 +155,7 @@ async fn main() -> Result<()> {
);

// Initialize console
console::init(&tx).await?;
command_processor::init(&tx).await?;

// Autostart flagged containers. Each container with the `autostart` option
// set to true in the manifest is started.
Expand All @@ -174,14 +174,13 @@ async fn main() -> Result<()> {

// Enter main loop
while let Ok(event) = rx.recv().await {
debug!("received event: {:?}", event);
match event {
Event::ChildOutput { name, fd, line } => on_child_output(&mut state, &name, fd, &line),
// Debug console commands are handled via the main loop in order to get access
// to the global state. Therefore the console server receives a tx handle to the
// main loop and issues `Event::Console`. Processing of the command takes place
// in the console module again but with access to `state`.
Event::Console(cmd, txr) => console::process(&mut state, &cmd, txr).await?,
Event::Command(cmd, txr) => command_processor::process(&mut state, &cmd, txr).await?,
// The OOM event is signaled by the cgroup memory monitor if configured in a manifest.
// If a out of memory condition occours this is signaled with `Event::Oom` which
// carries the id of the container that is oom.
Expand Down
25 changes: 10 additions & 15 deletions north_api/src/proto/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ message ProcessInfo {
optional int64 uptime = 9; // in us
}

message HelpInfo { required string content = 1; }

message ProcessList { repeated ProcessInfo processes = 1; }

message ContainerList { repeated ContainerInfo containers = 1; }
Expand Down Expand Up @@ -84,14 +82,13 @@ message Response {
oneof content {
ContainerList container_list = 1;
ProcessList process_list = 2;
HelpInfo help_response = 3;
StartedResponse start_response = 4;
StoppedResponse stop_response = 5;
StatusResponse default_status = 6;
UninstallResponse uninstall_response = 7;
UpdateResponse update_response = 8;
Settings settings_response = 9;
Versions versions_response = 10;
StartedResponse start_response = 3;
StoppedResponse stop_response = 4;
StatusResponse default_status = 5;
UninstallResponse uninstall_response = 6;
UpdateResponse update_response = 7;
Settings settings_response = 8;
Versions versions_response = 9;
}
optional int64 duration = 20;
}
Expand All @@ -102,7 +99,6 @@ message Command {
message Ps {}
message ContainerIdentification { optional string regex = 1; }
message Update { required string update_dir = 1; }
message Help {}
message Shutdown {}
message GetSettings {}
message GetVersions {}
Expand All @@ -114,10 +110,9 @@ message Command {
ContainerIdentification stop_container = 5;
ContainerIdentification uninstall_container = 6;
Update update = 7;
Help help_text = 8;
Shutdown shutdown = 9;
GetSettings get_settings = 10;
GetVersions get_versions = 11;
Shutdown shutdown = 8;
GetSettings get_settings = 9;
GetVersions get_versions = 10;
}
}

Expand Down
Loading

0 comments on commit 51e6e25

Please sign in to comment.