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

Commit

Permalink
archive docker tarball
Browse files Browse the repository at this point in the history
  • Loading branch information
guni1192 committed Feb 5, 2019
1 parent bea6d72 commit 06e1a60
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 5 deletions.
92 changes: 92 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 @@ -13,3 +13,5 @@ nix = "0.11.0"
clap = "2.32.0"
reqwest = "0.9.9"
serde_json = "1.0"
tar = "0.4"
flate2 = "1.0"
27 changes: 22 additions & 5 deletions src/image.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::fs::File;
use std::io;

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

pub struct Image {
name: String,
Expand All @@ -13,8 +15,22 @@ impl Image {
Image { name }
}

pub fn tar_archive(&self, path: String) {
let tar_gz = File::open(&path).expect("");
let tar = GzDecoder::new(tar_gz);
let mut ar = Archive::new(tar);
let filename = path.replace("/tmp/", "");
let container_path = format!(
"/var/lib/cromwell/containers/{}",
&filename.replace(".tar.gz", "")
);
std::fs::create_dir(&container_path).unwrap();

ar.unpack(&container_path)
.expect("Failed to unpack filename");
}

pub fn pull(&self) -> Result<(), reqwest::Error> {
// TODO: pull from dockerhub
let auth_url = format!(
"https://auth.docker.io/token?service=registry.docker.io&scope=repository:{}:pull",
self.name
Expand All @@ -23,7 +39,6 @@ impl Image {
let body: Value = serde_json::from_str(res_json.as_str()).expect("parse json failed");

if let Value::String(token) = &body["token"] {
// println!("{:#?}", token);
let manifests_url = format!(
"https://registry.hub.docker.com/v2/{}/manifests/latest",
self.name
Expand All @@ -37,7 +52,6 @@ impl Image {
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 {
// println!("{}", fs_layer["blobSum"]);
if let Value::String(blob_sum) = &fs_layer["blobSum"] {
let url = format!(
"https://registry.hub.docker.com/v2/{}/blobs/{}",
Expand All @@ -48,9 +62,12 @@ impl Image {
.get(url.as_str())
.bearer_auth(token)
.send()?;
let mut out = File::create(format!("{}.tar.gz", blob_sum))
.expect("failed to create file");
let out_filename =
format!("/tmp/{}.tar.gz", blob_sum.replace("sha256:", ""));
println!("{}", out_filename);
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);
}
}
}
Expand Down

0 comments on commit 06e1a60

Please sign in to comment.