From d27b09cc1a31901f4abf534947271b2d4030104d Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Wed, 1 Apr 2020 01:59:15 +0800 Subject: [PATCH] use result --- src/main.rs | 282 +++++++++++++++++++++--------------------------- src/opt.rs | 7 +- src/oss_util.rs | 1 + 3 files changed, 129 insertions(+), 161 deletions(-) diff --git a/src/main.rs b/src/main.rs index e7a6646..aa39a7c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ -#[macro_use] -extern crate lazy_static; +// #[macro_use] +// extern crate lazy_static; extern crate sequoia_openpgp as openpgp; pub mod oss_util; pub mod pgp_util; @@ -51,178 +51,146 @@ fn main() -> XResult<()> { for config_item in &oss_backupd_config.items { item_index += 1; - if options.verbose { - print_message(MessageType::DEBUG, &format!("Process config item index: {}, config: {:?}", item_index, config_item)); + if let Err(e) = process_config_item(&options, &config_item, &oss_backupd_config, item_index) { + print_message(MessageType::ERROR, &format!("Config {} not found, at item index: {}", e, item_index)); } - - let encrypt_pubkey_file = match &config_item.encrypt_pubkey_file { - None => { - print_message(MessageType::ERROR, &format!("Config encrypt_pubkey_file not found, at item index: {}", item_index)); - continue; - }, - Some(encrypt_pubkey_file) => encrypt_pubkey_file, - }; - if options.verbose { - print_message(MessageType::DEBUG, &format!("Encrypt pubkey file: {}", encrypt_pubkey_file)); - } - - let target = match &config_item.target { - None => { - print_message(MessageType::ERROR, &format!("Config target not found, at item index: {}", item_index)); - continue; - }, - Some(target) => target, - }; - if options.verbose { - print_message(MessageType::DEBUG, &format!("Target file: {}", iff!(target.is_empty(), "", target))); - } - - let oss_config = match &config_item.oss_config { - None => { - print_message(MessageType::ERROR, &format!("Config oss_config not found, at item index: {}", item_index)); - continue; - }, - Some(oss_config) => oss_config, - }; - let endpoint = match &oss_config.endpoint { - None => { - print_message(MessageType::ERROR, &format!("Config oss_config#endpoint not found, at item index: {}", item_index)); - continue; - } - Some(endpoint) => endpoint, - }; - if options.verbose { - print_message(MessageType::DEBUG, &format!("Endpoint: {}", endpoint)); - } - - let access_key_id = match &oss_config.access_key_id { - None => { - print_message(MessageType::ERROR, &format!("Config oss_config#access_key_id not found, at item index: {}", item_index)); - continue; - }, - Some(access_key_id) => access_key_id, - }; - if options.verbose { - print_message(MessageType::DEBUG, &format!("Access key id: {}", access_key_id)); - } - - let access_key_secret = match &oss_config.access_key_secret { - None => { - print_message(MessageType::ERROR, &format!("Config oss_config#access_key_secret not found, at item index: {}", item_index)); - continue; - }, - Some(access_key_secret) => access_key_secret, - }; - let bucket = match &oss_config.bucket { - None => { - print_message(MessageType::ERROR, &format!("Config oss_config#bucket not found, at item index: {}", item_index)); - continue; - }, - Some(bucket) => bucket, - }; - if options.verbose { - print_message(MessageType::DEBUG, &format!("Bucket: {}", bucket)); - } - - let path = &match &oss_config.path { - None => format!("default_path_at_{}", item_index), - Some(path) => path.to_owned(), - }; - if options.verbose { - print_message(MessageType::DEBUG, &format!("Path: {}", path)); - } - - let oss_client = OSSClient::new(endpoint, access_key_id, access_key_secret); - let default_limit = 10_usize; // TODO read from config! - let meta_file_name = &format!("{}/ossbackupd_meta_{}_{}.json", &oss_backupd_config.get_prefix(), &oss_backupd_config.get_host(), &config_item.get_file_name()); - let new_file = format!("{}/{}", path, config_item.make_oss_key(&oss_backupd_config, "gpg")); - - if options.verbose { - print_message(MessageType::DEBUG, &format!("Default limit: {}", default_limit)); - print_message(MessageType::DEBUG, &format!("Meta file name: {}", meta_file_name)); - print_message(MessageType::DEBUG, &format!("New backup file: {}", new_file)); - } - - let open_pgp_tool = match OpenPGPTool::from_file(encrypt_pubkey_file) { - Err(e) => { - print_message(MessageType::ERROR, &format!("Error in load pgp file: {}, at item index: {}", e, item_index)); - continue; - }, - Ok(open_pgp_tool) => open_pgp_tool, - }; - - let temp_zip_file = &format!("temp_file_{}.zip", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()); - let temp_pgp_file = &format!("temp_file_{}.gpg", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()); - if options.verbose { - print_message(MessageType::DEBUG, &format!("Compress file: {} -> {}", target, temp_zip_file)); - } - if let Err(e) = zip_util::zip_file(target, temp_zip_file) { - print_message(MessageType::ERROR, &format!("Error in zip file: {}, at item index: {}", e, item_index)); - continue; - }; - - if options.verbose { - print_message(MessageType::DEBUG, &format!("Encrypt file: {} -> {}", temp_zip_file, temp_pgp_file)); - } - if let Err(e) = open_pgp_tool.encrypt_file(temp_zip_file, temp_pgp_file, false) { - print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index)); - fs::remove_file(temp_zip_file).ok(); - continue; - }; - - let file_temp_pgp_file = match File::open(temp_pgp_file) { - Err(e) => { - print_message(MessageType::ERROR, &format!("Error in open file: {}, at item index: {}", e, item_index)); - fs::remove_file(temp_zip_file).ok(); - fs::remove_file(temp_pgp_file).ok(); - continue; - }, - Ok(file_temp_pgp_file) => file_temp_pgp_file, - }; - - if options.verbose { - print_message(MessageType::DEBUG, &format!("Upload file: {}", temp_pgp_file)); - } - if let Err(e) = oss_client.put_file(bucket, &new_file, 1000, file_temp_pgp_file) { - print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index)); - fs::remove_file(temp_zip_file).ok(); - fs::remove_file(temp_pgp_file).ok(); - continue; - } - - if options.verbose { - print_message(MessageType::DEBUG, &format!("Processing meta file: {}", meta_file_name)); - } - match process_oss_files(&oss_client, bucket, path, meta_file_name, &new_file, default_limit) { - Err(e) => { - print_message(MessageType::ERROR, &format!("Error: {}, at item index: {}", e, item_index)); - }, - Ok(_) => { - print_message(MessageType::OK, &format!("Success, at item index: {}", item_index)); - }, - }; - fs::remove_file(temp_zip_file).ok(); - fs::remove_file(temp_pgp_file).ok(); } print_message(MessageType::OK, "Backup all file(s) finished!"); Ok(()) } -pub fn process_oss_files(oss_client: &OSSClient, bucket_name: &str, path: &str, meta_file_name: &str, new_file: &str, limit: usize) -> XResult<()> { +pub fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, + oss_backupd_config :&OSSBackupdConfig, item_index: i32) -> Result<(), String> { + if options.verbose { + print_message(MessageType::DEBUG, &format!("Process config item index: {}, config: {:?}", item_index, config_item)); + } + + let encrypt_pubkey_file = config_item.encrypt_pubkey_file.as_ref().ok_or("encrypt_pubkey_file".to_owned())?; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Encrypt pubkey file: {}", encrypt_pubkey_file)); + } + + let target = config_item.target.as_ref().ok_or("target".to_owned())?; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Target file: {}", iff!(target.is_empty(), "", target))); + } + + let oss_config = config_item.oss_config.as_ref().ok_or("oss_config".to_owned())?; + + let endpoint = oss_config.endpoint.as_ref().ok_or("oss_config#endpoint".to_owned())?; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Endpoint: {}", endpoint)); + } + + let access_key_id = oss_config.access_key_id.as_ref().ok_or("oss_config#access_key_id".to_owned())?; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Access key id: {}", access_key_id)); + } + + let access_key_secret = oss_config.access_key_secret.as_ref().ok_or("oss_config#access_key_secret".to_owned())?; + let bucket = &oss_config.bucket.as_ref().ok_or("oss_config#bucket".to_owned())?; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Bucket: {}", bucket)); + } + + let path = &match &oss_config.path { + None => format!("default_path_at_{}", item_index), + Some(path) => path.to_owned(), + }; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Path: {}", path)); + } + + let oss_client = OSSClient::new(endpoint, access_key_id, access_key_secret); + let default_limit = 10_usize; // TODO read from config! + let meta_file_name = &format!("{}/ossbackupd_meta_{}_{}.json", &oss_backupd_config.get_prefix(), &oss_backupd_config.get_host(), &config_item.get_file_name()); + let new_file = format!("{}/{}", path, config_item.make_oss_key(&oss_backupd_config, "gpg")); + + if options.verbose { + print_message(MessageType::DEBUG, &format!("Default limit: {}", default_limit)); + print_message(MessageType::DEBUG, &format!("Meta file name: {}", meta_file_name)); + print_message(MessageType::DEBUG, &format!("New backup file: {}", new_file)); + } + + let open_pgp_tool = match OpenPGPTool::from_file(encrypt_pubkey_file) { + Err(e) => { + print_message(MessageType::ERROR, &format!("Error in load pgp file: {}, at item index: {}", e, item_index)); + return Ok(()); + }, + Ok(open_pgp_tool) => open_pgp_tool, + }; + + let temp_zip_file = &format!("temp_file_{}.zip", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()); + let temp_pgp_file = &format!("temp_file_{}.gpg", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()); + if options.verbose { + print_message(MessageType::DEBUG, &format!("Compress file: {} -> {}", target, temp_zip_file)); + } + if let Err(e) = zip_util::zip_file(target, temp_zip_file) { + print_message(MessageType::ERROR, &format!("Error in zip file: {}, at item index: {}", e, item_index)); + return Ok(()); + }; + + if options.verbose { + print_message(MessageType::DEBUG, &format!("Encrypt file: {} -> {}", temp_zip_file, temp_pgp_file)); + } + if let Err(e) = open_pgp_tool.encrypt_file(temp_zip_file, temp_pgp_file, false) { + print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index)); + fs::remove_file(temp_zip_file).ok(); + return Ok(()); + }; + + let file_temp_pgp_file = match File::open(temp_pgp_file) { + Err(e) => { + print_message(MessageType::ERROR, &format!("Error in open file: {}, at item index: {}", e, item_index)); + fs::remove_file(temp_zip_file).ok(); + fs::remove_file(temp_pgp_file).ok(); + return Ok(()); + }, + Ok(file_temp_pgp_file) => file_temp_pgp_file, + }; + + if options.verbose { + print_message(MessageType::DEBUG, &format!("Upload file: {}", temp_pgp_file)); + } + if let Err(e) = oss_client.put_file(bucket, &new_file, 1000, file_temp_pgp_file) { + print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index)); + fs::remove_file(temp_zip_file).ok(); + fs::remove_file(temp_pgp_file).ok(); + return Ok(()); + } + + if options.verbose { + print_message(MessageType::DEBUG, &format!("Processing meta file: {}", meta_file_name)); + } + match process_oss_files(&options, &oss_client, bucket, path, meta_file_name, &new_file, default_limit) { + Err(e) => { + print_message(MessageType::ERROR, &format!("Error: {}, at item index: {}", e, item_index)); + }, + Ok(_) => { + print_message(MessageType::OK, &format!("Success, at item index: {}", item_index)); + }, + }; + fs::remove_file(temp_zip_file).ok(); + fs::remove_file(temp_pgp_file).ok(); + + Ok(()) +} + +pub fn process_oss_files(options: &Options, oss_client: &OSSClient, bucket_name: &str, path: &str, meta_file_name: &str, new_file: &str, limit: usize) -> XResult<()> { let meta_file_key = &format!("{}/{}", path, meta_file_name); - if *opt::IS_DEBUG { + if options.verbose { print_message(MessageType::DEBUG, &format!("Read meta file: {}", meta_file_key)); } let meta_file_content = match oss_client.get_file_content(bucket_name, meta_file_key)? { None => "[]".to_owned(), Some(c) => c, }; - if *opt::IS_DEBUG { + if options.verbose { print_message(MessageType::DEBUG, &format!("Read meta file content: {}", &meta_file_content)); } let (removed_file, new_meta_file_content) = process_new_backup_file(&meta_file_content, new_file, limit)?; - if *opt::IS_DEBUG { + if options.verbose { print_message(MessageType::DEBUG, &format!("Processed meta file content: {}", &new_meta_file_content)); } oss_client.put_file_content(bucket_name, meta_file_key, &new_meta_file_content)?; diff --git a/src/opt.rs b/src/opt.rs index f41965b..3c46499 100644 --- a/src/opt.rs +++ b/src/opt.rs @@ -1,12 +1,11 @@ use rust_util::{ XResult, - util_env::is_env_on, }; use argparse::{ArgumentParser, StoreTrue, Store}; -lazy_static! { - pub static ref IS_DEBUG: bool = is_env_on("DEBUG"); -} +// lazy_static! { +// pub static ref IS_DEBUG: bool = rust_util::util_env::is_env_on("DEBUG"); +// } pub struct Options { pub version: bool, diff --git a/src/oss_util.rs b/src/oss_util.rs index 4f7c7af..47f4b70 100644 --- a/src/oss_util.rs +++ b/src/oss_util.rs @@ -98,6 +98,7 @@ impl<'a> OSSClient<'a> { } } +// https://endpoint, or http://endpoint -> endpoint fn remove_endpoint_http_or_s(endpoint: &str) -> String { let mut endpoint = endpoint.to_owned(); if endpoint.starts_with("http://") {