Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
put config.json under the container_path, after tar was un archived
Browse files Browse the repository at this point in the history
  • Loading branch information
guni1192 committed Feb 6, 2019
1 parent d3041c1 commit a996c8b
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 9 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ edition = "2018"
nix = "0.11.0"
clap = "2.32.0"
reqwest = "0.9.9"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
tar = "0.4"
flate2 = "1.0"
14 changes: 12 additions & 2 deletions src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,38 @@ use nix::sys::wait::{waitpid, WaitStatus};
use nix::unistd::{chdir, chroot, fork, ForkResult};
use nix::unistd::{execve, sethostname, Uid};

use super::image::Image;
use super::mounts;

pub struct Container {
pub name: String,
pub path: String,
pub command: String,
pub uid: Uid,
pub image: Image,
}

impl Container {
pub fn new(name: String, command: String, uid: Uid) -> Container {
let path = format!("{}/{}", get_containers_path().unwrap(), name.clone());
let mut image = Image::new(name.clone());
image.pull().expect("Failed to cromwell pull");

let path = format!(
"{}/{}",
get_containers_path().unwrap(),
image.filename.clone()
);

Container {
name: name.clone(),
path,
command,
uid,
image,
}
}

pub fn prepare(&self) {
pub fn prepare(&mut self) {
println!("Started initialize Container!");
let c_hosts = format!("{}/etc/hosts", self.path);
let c_resolv = format!("{}/etc/resolv.conf", self.path);
Expand Down
41 changes: 36 additions & 5 deletions src/image.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use std::fs::{self, File};
use std::io;
use std::io::{self, Write};
use std::path::Path;

use flate2::read::GzDecoder;
use reqwest;
use serde_json::{self, Value};
use tar::Archive;

#[warn(unused_imports)]
#[derive(Serialize, Deserialize, Debug)]
pub struct Image {
name: String,
tag: String,
pub filename: String,
path: String,
}

impl Image {
Expand All @@ -21,14 +25,19 @@ impl Image {
Image {
name: n[0].to_string(),
tag: n[1].to_string(),
filename: "".to_string(),
path: "".to_string(),
}
}

pub fn tar_archive(&self, path: String) -> io::Result<()> {
pub fn tar_archive(&mut self, path: String) -> io::Result<()> {
println!("[INFO] tar unpack start {}", path);
let tar_gz = File::open(&path).expect("");
let tar = GzDecoder::new(tar_gz);
let mut ar = Archive::new(tar);
let filename = path.replace("/tmp/", "");
println!("[INFO] filename : {}", filename);

let container_path = format!(
"/var/lib/cromwell/containers/{}",
&filename.replace(".tar.gz", "")
Expand All @@ -38,12 +47,29 @@ impl Image {
fs::remove_dir_all(&container_path)?;
}

println!("[INFO] mkdir {}", container_path);
std::fs::create_dir(&container_path)?;

ar.unpack(&container_path)
println!("[INFO] unpacking {}", container_path);
ar.unpack(&container_path)?;
self.path = container_path;

Ok(())
}

pub fn put_config_json(&self) -> std::io::Result<()> {
let json_str = serde_json::to_string(&self)?;
let json_bytes = json_str.as_bytes();

// let path = format!("/var/lib/cromwell/containers/{}/config.json", self.filename);
let container_path = format!("{}/config.json", self.path);
let mut file = File::create(container_path)?;
file.write_all(json_bytes)?;

Ok(())
}

pub fn pull(&self) -> Result<(), reqwest::Error> {
pub fn pull(&mut self) -> Result<(), reqwest::Error> {
let auth_url = format!(
"https://auth.docker.io/token?service=registry.docker.io&scope=repository:{}:pull",
self.name
Expand All @@ -56,13 +82,15 @@ impl Image {
"https://registry.hub.docker.com/v2/{}/manifests/{}",
self.name, self.tag
);

let res = reqwest::Client::new()
.get(manifests_url.as_str())
.bearer_auth(token)
.send()?
.text()?;

let body: Value = serde_json::from_str(res.as_str()).expect("parse json failed");

if let Value::Array(fs_layers) = &body["fsLayers"] {
for fs_layer in fs_layers {
if let Value::String(blob_sum) = &fs_layer["blobSum"] {
Expand All @@ -79,8 +107,11 @@ impl Image {
format!("/tmp/{}.tar.gz", blob_sum.replace("sha256:", ""));
let mut out = File::create(&out_filename).expect("failed to create file");
io::copy(&mut res, &mut out).expect("failed to copy content");

self.tar_archive(out_filename)
.expect("failed to tar un archive");
.expect("failed to un archive tar.gz");

self.put_config_json().expect("failed to put config json");
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[macro_use]
extern crate serde_derive;

use std::process::exit;

use clap::{crate_name, crate_version, App, Arg, SubCommand};
Expand Down
4 changes: 2 additions & 2 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn run(sub_m: &ArgMatches) {

let uid = getuid();

let container = container::Container::new(container_name.to_string(), command, uid);
let mut container = container::Container::new(container_name.to_string(), command, uid);

if sub_m.is_present("del") {
container.delete().expect("Faild to remove container: ");
Expand Down Expand Up @@ -56,6 +56,6 @@ pub fn pull(sub_m: &ArgMatches) {
.value_of("image_name")
.expect("invalied arguments about image name");

let image = Image::new(image_name.to_string());
let mut image = Image::new(image_name.to_string());
image.pull().expect("Failed to image pull");
}

0 comments on commit a996c8b

Please sign in to comment.