init, shutdown and restart

This commit is contained in:
Aiden McClelland
2021-09-03 18:00:38 -06:00
committed by Aiden McClelland
parent 6e82b843a5
commit 483e02a41b
14 changed files with 585 additions and 98 deletions

View File

@@ -1,4 +1,3 @@
use std::ascii::AsciiExt;
use std::process::Stdio;
use std::time::{Duration, UNIX_EPOCH};
@@ -13,9 +12,7 @@ use tokio::process::Command;
use tokio_stream::wrappers::LinesStream;
use crate::action::docker::DockerAction;
use crate::context::RpcContext;
use crate::error::ResultExt;
use crate::id::Id;
use crate::s9pk::manifest::PackageId;
use crate::util::Reversible;
use crate::Error;
@@ -67,7 +64,7 @@ pub enum LogSource {
Container(PackageId),
}
fn display_logs(all: LogResponse, _: &ArgMatches<'_>) {
pub fn display_logs(all: LogResponse, _: &ArgMatches<'_>) {
for entry in all.entries.iter() {
println!("{}", entry);
}
@@ -75,13 +72,18 @@ fn display_logs(all: LogResponse, _: &ArgMatches<'_>) {
#[command(display(display_logs))]
pub async fn logs(
#[context] _: RpcContext,
#[arg] id: PackageId,
#[arg] limit: Option<usize>,
#[arg] cursor: Option<String>,
#[arg] before_flag: Option<bool>,
) -> Result<LogResponse, Error> {
Ok(fetch_logs(LogSource::Container(id), limit, cursor, before_flag.unwrap_or(false)).await?)
Ok(fetch_logs(
LogSource::Container(id),
limit,
cursor,
before_flag.unwrap_or(false),
)
.await?)
}
pub async fn fetch_logs(
@@ -93,13 +95,15 @@ pub async fn fetch_logs(
let limit = limit.unwrap_or(50);
let limit_formatted = format!("-n{}", limit);
let mut args = vec!["--output=json","--output-fields=MESSAGE",&limit_formatted,];
let mut args = vec!["--output=json", "--output-fields=MESSAGE", &limit_formatted];
let id_formatted = match id {
LogSource::Service(id)=> {
LogSource::Service(id) => {
args.push("-u");
id.to_owned()
},
LogSource::Container(id) => format!("CONTAINER_NAME={}", DockerAction::container_name(&id, None))
}
LogSource::Container(id) => {
format!("CONTAINER_NAME={}", DockerAction::container_name(&id, None))
}
};
args.push(&id_formatted);
@@ -169,18 +173,19 @@ pub async fn fetch_logs(
#[tokio::test]
pub async fn test_logs() {
let response =
fetch_logs(
// change `tor.service` to an actual journald unit on your machine
// LogSource::Service("tor.service"),
// first run `docker run --name=hello-world.embassy --log-driver=journald hello-world`
LogSource::Container("hello-world".parse().unwrap()),
// Some(5),
None,
None,
// Some("s=1b8c418e28534400856c27b211dd94fd;i=5a7;b=97571c13a1284f87bc0639b5cff5acbe;m=740e916;t=5ca073eea3445;x=f45bc233ca328348".to_owned()),
false,
).await.unwrap();
let response = fetch_logs(
// change `tor.service` to an actual journald unit on your machine
// LogSource::Service("tor.service"),
// first run `docker run --name=hello-world.embassy --log-driver=journald hello-world`
LogSource::Container("hello-world".parse().unwrap()),
// Some(5),
None,
None,
// Some("s=1b8c418e28534400856c27b211dd94fd;i=5a7;b=97571c13a1284f87bc0639b5cff5acbe;m=740e916;t=5ca073eea3445;x=f45bc233ca328348".to_owned()),
false,
)
.await
.unwrap();
let serialized = serde_json::to_string_pretty(&response).unwrap();
println!("{}", serialized);
}