1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::process::Command;
use std::fs;
use crate::sled_json::{ TreeWrapper, JSONEncoder };
use crate::public_struct::ImageLayerLayerDiffIDToLayerDigestJSONValue;

use sled::{Config, Mode, Db};

pub async fn create_sled_db() -> Option<Db> {
    let db = Config::new()
        .mode(Mode::HighThroughput)
        .path("/var/lib/AntKing/db")
        .open();
    return match db {
        Ok(res) => {
            Some(res)
        },
        Err(e) => {
            println!("Failed to create database because:{}", e);
            None
        }
    }
}


// pub async fn progress_bar(total_size:i64, path:String, layer_digest:String) {
//     // println!("path:{}",path.clone());
//     let mut downloaded = 0;
//     let total_size = total_size as u64;
//
//     let pb = ProgressBar::new(total_size);
//     pb.set_style(ProgressStyle::default_bar()
//         .template("{spinner:.green} [{elapsed_precise}] [{wide_bar.cyan/blue}] {bytes}/{total_bytes} ({eta})")
//         .progress_chars("#>-"));
//
//     while downloaded < total_size {
//         downloaded = compute_layer_size(path.clone()).parse().unwrap();
//         let new = min(downloaded, total_size);
//         downloaded = new;
//         pb.set_position(new);
//     }
//
//     let message = format!("{} downloaded",layer_digest.clone());
//     pb.finish_with_message(&*message.clone());
// }


pub fn determine_whether_image_layer_exists(db: &sled::Db,image_digest:String,layer_diff_id:String) -> bool {
    let tree = TreeWrapper::<JSONEncoder<ImageLayerLayerDiffIDToLayerDigestJSONValue>, JSONEncoder<ImageLayerLayerDiffIDToLayerDigestJSONValue>>::new(
        db.open_tree("image_digest_layerdiffid_to_layerdigest").unwrap(),
    );

    let value = tree
    .get(image_digest.clone());
    match value{
        Ok(res) => {
            match res {
                Some(res1) => {
                    match res1.decode() {
                        None => false,
                        Some(res2) => {
                            match res2.image_digest_layerdiffid_to_layerdigest.get(&*layer_diff_id.clone()){
                                None => false,
                                Some(_) => true
                            }
                        }
                    }
                }

                _ => false
            }
        }
        _ => false
    }
}



pub fn compute_layer_size(path:String) -> String{
    // println!("path:{}",path.clone());
    // fs::File::create(path.clone()).unwrap();
    let content = fs::read(path.clone()).unwrap();
    let size = content.len();

    let path1 = format!("{}.gz",path.clone());
    let cmd = format!("tar -zcvf {} {}",path1.clone(),path.clone());
    Command::new("sh").arg("-c").arg(cmd.clone()).output().unwrap();

    let cmd = format!("rm -rf {}",path);
    Command::new("sh").arg("-c").arg(cmd.clone()).output().unwrap();
    return format!("{}",size)
}


pub fn computer_layer_chain_id(layer_parent_chain_id:String,layer_diff_id:String) -> String {
    if layer_parent_chain_id != "".to_string() {
        let sha256_params = format!("echo -n \"sha256:{} sha256:{}\" | sha256sum",layer_parent_chain_id,layer_diff_id);
        let output1 = Command::new("sh").arg("-c").arg(sha256_params.clone()).output();
            match output1 {
                Ok(res) => {
                    match String::from_utf8(res.stdout){
                        Ok(res1) => {
                            let res1_1 = res1.split(' ');
                            let res1_2: Vec<&str> = res1_1.collect();
                            return format!("{}",res1_2[0])
                        },
                        Err(e) => {
                             println!("Execute sha256sum exception! Reason:{}",e);
                            "".to_string()
                        }
                    }
                }
                Err(e) => {
                    println!("Execute sha256sum exception! Reason:{}",e);
                    "".to_string()
                }
            }
        }else {
           layer_diff_id
        }
}


pub fn get_completed_digest(short_digest:String) -> String{
    let cmd = format!("ls /var/lib/AntKing/gz/ | grep {}",short_digest.clone());
    let output = Command::new("sh").arg("-c").arg(cmd.clone()).output().unwrap();
    let result = format!("{}",String::from_utf8(output.stdout).unwrap().trim());
    return result
}


pub fn remove_image_gz(path:String) {
    let cmd = format!("ls /var/lib/AntKing/gz/ | grep {} | xargs  -I xx  sh -c \"rm -rf /var/lib/AntKingImage/gz/xx\"",path.clone());
    Command::new("sh").arg("-c").arg(cmd.clone()).output().unwrap();
}


pub fn remove_image_rootfs(path:String) {
    let cmd = format!("ls /var/lib/AntKing/images/ | grep {} | xargs  -I xx  sh -c \"rm -rf /var/lib/AntKingImage/images/xx\"",path.clone());
    Command::new("sh").arg("-c").arg(cmd.clone()).output().unwrap();
}


pub fn computer_fs_info() -> (u64,u64) {
    let cmd1 = "du -shb /var/lib/AntKing/ | awk '{print $1}'".to_string();
    let output = Command::new("sh").arg("-c").arg(cmd1.clone()).output().unwrap();
    let used_bytes_1 = format!("{}",String::from_utf8(output.stdout).unwrap().trim());
    // println!("used_bytes_1:{}",used_bytes_1.clone());
    let used_bytes:u64 = used_bytes_1.parse::<u64>().unwrap();

    let cmd2 = "stat /var/lib/AntKing/ | grep Inode | awk '{print $2}' | cut -c 7-".to_string();
    let output = Command::new("sh").arg("-c").arg(cmd2.clone()).output().unwrap();
    let inodes_used_1 = format!("{}",String::from_utf8(output.stdout).unwrap().trim());
    // println!("inodes_used_1:{}",inodes_used_1.clone());
    let inodes_used:u64 = inodes_used_1.parse::<u64>().unwrap();

    return (used_bytes,inodes_used)
}