Skip to content

Commit

Permalink
Add log subcmd.
Browse files Browse the repository at this point in the history
Signed-off-by: Klaus Ma <klausm@nvidia.com>
  • Loading branch information
k82cn committed Oct 16, 2024
1 parent 3af01c7 commit 4a4edb9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 17 deletions.
33 changes: 29 additions & 4 deletions src/core/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ pub enum Commands {
pod: Option<String>,
},

/// Print the log of a container.
Log {
/// The name of conatiner.
#[arg(short, long)]
container: Option<String>,
/// The name of pod.
#[arg(short, long)]
pod: Option<String>,
},

/// Run a Pod directly.
Runp,

Expand Down Expand Up @@ -67,11 +77,26 @@ impl Context {
self.work_dir.clone()
}

pub fn image_dir(&self) -> String {
format!("{}/images", self.work_dir)
pub fn image_dir(&self, image: &str) -> String {
format!("{}/images/{}", self.work_dir, image)
}

pub fn image_manifest(&self, image: &str) -> String {
format!("{}/images/{}/manifest.json", self.work_dir, image)
}

pub fn container_dir(&self, container: &str) -> String {
format!("{}/containers/{}", self.work_dir, container)
}

pub fn container_rootfs(&self, container: &str) -> String {
format!("{}/containers/{}/rootfs", self.work_dir, container)
}

pub fn container_dir(&self) -> String {
format!("{}/containers", self.work_dir)
pub fn container_log(&self, container: &str) -> String {
format!(
"{}/containers/{}/{}.log",
self.work_dir, container, container
)
}
}
2 changes: 1 addition & 1 deletion src/core/cmd/delete/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub async fn run(
_pod: Option<String>,
) -> apis::ChariotResult<()> {
if let Some(container) = container {
let container_root = format!("{}/{}", cxt.container_dir(), container);
let container_root = cxt.container_dir(&container);
fs::remove_dir_all(&container_root)?;
tracing::debug!("The container <{}> was cleared", container_root);
}
Expand Down
31 changes: 31 additions & 0 deletions src/core/cmd/log/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2024 The openBCE Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use std::fs;

use crate::cfg;
use chariot::apis;

pub async fn run(
cxt: cfg::Context,
container: Option<String>,
_pod: Option<String>,
) -> apis::ChariotResult<()> {
if let Some(container) = container {
// TODO: print the log line by line.
let log = fs::read_to_string(cxt.container_log(&container))?;
print!("{log}");
}

Ok(())
}
1 change: 1 addition & 0 deletions src/core/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ limitations under the License.
*/

pub mod delete;
pub mod log;
pub mod runc;
pub mod runp;
pub mod start;
29 changes: 17 additions & 12 deletions src/core/cmd/runc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

use std::ffi::{CString, CStr};
use std::ffi::{CStr, CString};
use std::fs;

use nix::{
fcntl::{open, OFlag},
libc,
mount::{mount, umount2, MntFlags, MsFlags},
sched::CloneFlags,
sys::{stat::Mode, wait::{wait, WaitStatus}},
sys::{
stat::Mode,
wait::{wait, WaitStatus},
},
unistd::{chdir, dup2, execve, getpid, getuid, pivot_root},
};

Expand All @@ -35,13 +38,12 @@ pub async fn run(cxt: cfg::Context, file: String) -> ChariotResult<()> {

tracing::debug!("Parent pid is <{}>.", getpid());

let manifest_path = format!("{}/{}/manifest.json", cxt.image_dir(), container.image);
let manifest_path = cxt.image_manifest(&container.image);
tracing::debug!("Loading image manifest <{}>.", manifest_path);
let image_manifest = ImageManifest::from_file(manifest_path)?;

let container_root = format!("{}/{}", cxt.container_dir(), container.name);
let rootfs = format!("{}/rootfs", container_root);
let imagefs = format!("{}/{}", cxt.image_dir(), container.name);
let rootfs = cxt.container_rootfs(&container.name);
let imagefs = cxt.image_dir(&container.name);

for layer in image_manifest.layers() {
// TODO: detect mediaType and select unpack tools accordingly.
Expand Down Expand Up @@ -80,13 +82,17 @@ pub async fn run(cxt: cfg::Context, file: String) -> ChariotResult<()> {
let status = wait()?;
match status {
WaitStatus::Exited(pid, rc) => {
tracing::debug!("The container <{}> was exited in <{}>", pid, rc);
if rc == 0 {
tracing::info!("The container <{}> was exited successfully.", pid);
} else {
tracing::error!("The container <{}> was exited in <{}>.", pid, rc);
}
}
WaitStatus::Signaled(pid, sig, _) => {
tracing::debug!("The container <{}> got a signal <{}>", pid, sig);
tracing::info!("The container <{}> got a signal <{}>.", pid, sig);
}
WaitStatus::Stopped(pid, sig) => {
tracing::debug!("The container <{}> was stopped by signal <{}>", pid, sig);
tracing::info!("The container <{}> was stopped by signal <{}>.", pid, sig);
}
_ => {}
}
Expand All @@ -101,18 +107,17 @@ fn run_container(cxt: cfg::Context, container: Container) -> ChariotResult<()> {
getpid(),
getuid()
);
let container_root = format!("{}/{}", cxt.container_dir(), container.name);

// Re-direct the stdout/stderr to log file.
let logfile = format!("{}/{}.log", container_root, container.name);
let logfile = cxt.container_log(&container.name);
let log = open(
logfile.as_str(),
OFlag::empty().union(OFlag::O_CREAT).union(OFlag::O_RDWR),
Mode::from_bits(0o755).unwrap(),
)?;

// Create rootfs.
let rootfs = format!("{}/rootfs", container_root);
let rootfs = cxt.container_rootfs(&container.name);
tracing::debug!("Change root to <{}>", rootfs);

// Ensure that 'new_root' and its parent mount don't have
Expand Down
4 changes: 4 additions & 0 deletions src/core/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
cfg::Commands::Delete { container, pod } => {
cmd::delete::run(cxt, container, pod).await?;
}

cfg::Commands::Log { container, pod } => {
cmd::log::run(cxt, container, pod).await?;
}
}

Ok(())
Expand Down

0 comments on commit 4a4edb9

Please sign in to comment.