liftinstall/src/installer.rs

463 lines
15 KiB
Rust
Raw Normal View History

2018-01-27 04:27:41 +01:00
/// installer.rs
///
/// Contains the main installer structure, as well as high-level means of controlling it.
2018-01-29 13:28:14 +01:00
use regex::Regex;
2018-01-30 08:29:34 +01:00
use zip::ZipArchive;
2018-01-31 04:36:29 +01:00
use serde_json;
2018-01-30 08:29:34 +01:00
use number_prefix::{decimal_prefix, Prefixed, Standalone};
use std::fs::create_dir_all;
use std::fs::read_dir;
2018-01-30 08:29:34 +01:00
use std::fs::File;
use std::env::consts::OS;
use std::env::current_exe;
2018-01-27 05:14:56 +01:00
use std::env::home_dir;
use std::env::var;
2018-05-03 05:30:58 +02:00
use std::path::Path;
2018-01-27 05:14:56 +01:00
use std::path::PathBuf;
2018-01-30 08:29:34 +01:00
use std::io::copy;
use std::io::Cursor;
2018-01-30 08:29:34 +01:00
use std::sync::mpsc::Sender;
2018-01-30 05:53:28 +01:00
use std::sync::Arc;
use std::sync::Mutex;
2018-01-27 04:27:41 +01:00
use config::Config;
2018-01-30 05:53:28 +01:00
use http::stream_file;
2018-01-31 04:36:29 +01:00
use sources::types::Version;
use std::fs::OpenOptions;
2018-01-31 04:36:29 +01:00
/// A message thrown during the installation of packages.
#[derive(Serialize)]
pub enum InstallMessage {
Status(String, f64),
Error(String),
EOF,
}
2018-01-27 04:27:41 +01:00
/// The installer framework contains metadata about packages, what is installable, what isn't,
/// etc.
pub struct InstallerFramework {
2018-01-27 12:58:56 +01:00
config: Config,
2018-05-03 05:30:58 +02:00
database: Vec<LocalInstallation>,
install_path: Option<PathBuf>,
preexisting_install: bool,
2018-01-27 04:27:41 +01:00
}
/// Contains basic properties on the status of the session. Subset of InstallationFramework.
#[derive(Serialize)]
pub struct InstallationStatus {
database: Vec<LocalInstallation>,
install_path: Option<String>,
preexisting_install: bool,
}
2018-01-30 08:30:18 +01:00
/// Used to track the amount of data that has been downloaded during a HTTP request.
2018-01-30 05:53:28 +01:00
struct DownloadProgress {
2018-01-30 05:54:44 +01:00
downloaded: usize,
2018-01-30 05:53:28 +01:00
}
2018-01-31 04:36:29 +01:00
/// Tracks the state of a local installation
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LocalInstallation {
2018-01-31 04:36:29 +01:00
name: String,
version: Version,
files: Vec<String>,
}
2018-01-27 04:27:41 +01:00
impl InstallerFramework {
/// Returns a copy of the configuration.
pub fn get_config(&self) -> Config {
self.config.clone()
}
2018-01-27 05:14:56 +01:00
/// Returns the default install path.
pub fn get_default_path(&self) -> Option<String> {
let app_name = &self.config.general.name;
let base_dir = match var("LOCALAPPDATA") {
Ok(path) => PathBuf::from(path),
2018-01-27 12:58:56 +01:00
Err(_) => home_dir()?,
2018-01-27 05:14:56 +01:00
};
let file = base_dir.join(app_name);
Some(file.to_str()?.to_owned())
}
/// Sends a request for something to be installed.
2018-05-03 05:30:58 +02:00
/// items: Array of named packages to be installed
/// messages: Channel used to send progress messages
/// fresh_install: If the install directory must be empty
pub fn install(
2018-05-03 05:30:58 +02:00
&mut self,
items: Vec<String>,
messages: &Sender<InstallMessage>,
fresh_install: bool,
) -> Result<(), String> {
2018-05-03 05:30:58 +02:00
// We have to have a install path for us to be able to do anything
let path = match self.install_path.clone() {
Some(v) => v,
None => return Err(format!("No install directory for installer")),
2018-05-03 05:30:58 +02:00
};
println!("Framework: Installing {:?} to {:?}", items, path);
// Create our install directory
if !path.exists() {
match create_dir_all(&path) {
Ok(_) => {}
Err(v) => return Err(format!("Failed to create install directory: {:?}", v)),
}
}
if !path.is_dir() {
return Err(format!("Install destination is not a directory."));
}
// Make sure it is empty
2018-05-03 05:30:58 +02:00
if fresh_install {
let paths = match read_dir(&path) {
Ok(v) => v,
Err(v) => return Err(format!("Failed to read install destination: {:?}", v)),
};
2018-05-03 05:30:58 +02:00
if paths.count() != 0 {
return Err(format!("Install destination is not empty."));
}
}
2018-01-29 13:28:14 +01:00
// Resolve items in config
let mut to_install = Vec::new();
for description in &self.config.packages {
if items.contains(&description.name) {
to_install.push(description.clone());
}
}
println!("Resolved to {:?}", to_install);
2018-05-03 05:30:58 +02:00
// TODO: Uninstall pre-existing packages
2018-01-29 13:28:14 +01:00
// Install packages
let mut count = 0.0 as f64;
let max = to_install.len() as f64;
2018-01-29 13:28:14 +01:00
for package in to_install.iter() {
let base_package_percentage = count / max;
let base_package_range = ((count + 1.0) / max) - base_package_percentage;
2018-01-29 13:28:14 +01:00
println!("Installing {}", package.name);
2018-01-30 08:29:34 +01:00
// 10%: polling
messages
.send(InstallMessage::Status(
format!(
"Polling {} for latest version of {}",
package.source.name, package.name
),
2018-01-30 08:29:34 +01:00
base_package_percentage + base_package_range * 0.10,
))
.unwrap();
let results = package.source.get_current_releases()?;
2018-01-30 08:29:34 +01:00
// 20%: waiting for parse/HTTP
messages
.send(InstallMessage::Status(
format!("Resolving dependency for {}", package.name),
2018-01-30 08:29:34 +01:00
base_package_percentage + base_package_range * 0.20,
))
.unwrap();
2018-01-29 13:28:14 +01:00
let filtered_regex = package.source.match_regex.replace("#PLATFORM#", OS);
let regex = match Regex::new(&filtered_regex) {
Ok(v) => v,
Err(v) => return Err(format!("An error occured while compiling regex: {:?}", v)),
};
2018-01-29 13:28:14 +01:00
2018-05-03 05:30:58 +02:00
println!("Releases: {:?}", results);
2018-01-29 13:28:14 +01:00
// Find the latest release in here
let latest_result = results
.into_iter()
.filter(|f| f.files.iter().filter(|x| regex.is_match(&x.name)).count() > 0)
.max_by_key(|f| f.version.clone());
let latest_result = match latest_result {
Some(v) => v,
None => return Err(format!("No release with correct file found")),
};
2018-01-31 04:36:29 +01:00
let latest_version = latest_result.version.clone();
// Find the matching file in here
2018-01-29 13:37:30 +01:00
let latest_file = latest_result
.files
.into_iter()
.filter(|x| regex.is_match(&x.name))
2018-01-29 13:37:30 +01:00
.next()
.unwrap();
println!("{:?}", latest_file);
2018-01-30 05:53:28 +01:00
// Download this file
2018-01-30 05:54:44 +01:00
let lock = Arc::new(Mutex::new(DownloadProgress { downloaded: 0 }));
let data_storage: Arc<Mutex<Vec<u8>>> = Arc::new(Mutex::new(Vec::new()));
2018-01-30 05:53:28 +01:00
2018-01-30 08:29:34 +01:00
// 33-66%: downloading file
2018-01-30 05:53:28 +01:00
stream_file(latest_file.url, |data, size| {
2018-01-30 08:29:34 +01:00
{
let mut data_lock = data_storage.lock().unwrap();
data_lock.extend_from_slice(&data);
}
2018-01-30 05:53:28 +01:00
let mut reference = lock.lock().unwrap();
reference.downloaded += data.len();
2018-01-30 08:29:34 +01:00
let base_percentage = base_package_percentage + base_package_range * 0.33;
let range_percentage = base_package_range / 3.0;
2018-01-30 05:53:28 +01:00
let global_percentage = if size == 0 {
base_percentage
} else {
let download_percentage = (reference.downloaded as f64) / (size as f64);
// Split up the bar for this download in half (for metadata download + parse), then
// add on our current percentage
base_percentage + range_percentage * download_percentage
};
// Pretty print data volumes
let pretty_current = match decimal_prefix(reference.downloaded as f64) {
2018-01-30 05:54:44 +01:00
Standalone(bytes) => format!("{} bytes", bytes),
2018-01-30 05:53:28 +01:00
Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
};
let pretty_total = match decimal_prefix(size as f64) {
2018-01-30 05:54:44 +01:00
Standalone(bytes) => format!("{} bytes", bytes),
2018-01-30 05:53:28 +01:00
Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
};
messages
.send(InstallMessage::Status(
2018-01-30 05:54:44 +01:00
format!(
"Downloading {} ({} of {})",
package.name, pretty_current, pretty_total
),
2018-01-30 05:53:28 +01:00
global_percentage,
))
.unwrap();
})?;
println!("File downloaded successfully");
2018-01-30 08:29:34 +01:00
// Extract this downloaded file
2018-01-31 04:36:29 +01:00
let mut installed_files = Vec::new();
2018-01-30 08:29:34 +01:00
// TODO: Handle files other then zips
// TODO: Make database for uninstall
let data = data_storage.lock().unwrap();
let data_cursor = Cursor::new(data.as_slice());
let mut zip = match ZipArchive::new(data_cursor) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open .zip file: {:?}", v)),
};
let extract_base_percentage = base_package_percentage + base_package_range * 0.66;
let extract_range_percentage = base_package_range / 3.0;
let zip_size = zip.len();
for i in 0..zip_size {
let mut file = zip.by_index(i).unwrap();
let percentage =
extract_base_percentage + extract_range_percentage / zip_size as f64 * i as f64;
2018-01-30 08:29:34 +01:00
messages
.send(InstallMessage::Status(
format!("Extracting {} ({} of {})", file.name(), i + 1, zip_size),
2018-01-30 08:29:34 +01:00
percentage,
))
.unwrap();
// Create target file
let target_path = path.join(file.name());
println!("target_path: {:?}", target_path);
2018-01-31 04:36:29 +01:00
installed_files.push(file.name().to_owned());
2018-01-30 08:29:34 +01:00
// Check to make sure this isn't a directory
if file.name().ends_with("/") || file.name().ends_with("\\") {
// Create this directory and move on
match create_dir_all(target_path) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open file: {:?}", v)),
}
continue;
}
match target_path.parent() {
Some(v) => match create_dir_all(v) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open file: {:?}", v)),
},
None => {}
2018-01-30 08:29:34 +01:00
}
let mut target_file = match File::create(target_path) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open file handle: {:?}", v)),
};
// Cross the streams
match copy(&mut file, &mut target_file) {
Ok(v) => v,
2018-01-31 04:36:29 +01:00
Err(v) => return Err(format!("Unable to write to file: {:?}", v)),
2018-01-30 08:29:34 +01:00
};
}
2018-01-31 04:36:29 +01:00
// Save metadata about this package
2018-05-03 05:30:58 +02:00
// TODO: Check if this package already exists
self.database.push(LocalInstallation {
2018-01-31 04:36:29 +01:00
name: package.name.to_owned(),
version: latest_version,
files: installed_files,
});
count += 1.0;
2018-01-29 13:28:14 +01:00
}
2018-05-03 05:30:58 +02:00
self.save_database()?;
2018-01-31 04:36:29 +01:00
// Copy installer binary to target directory
messages
.send(InstallMessage::Status(
format!("Copying installer binary"),
0.99,
))
.unwrap();
let current_app = match current_exe() {
Ok(v) => v,
Err(v) => return Err(format!("Unable to locate installer binary: {:?}", v)),
};
let mut current_app_file = match File::open(current_app) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open installer binary: {:?}", v)),
};
let platform_extension = if cfg!(windows) {
"maintenancetool.exe"
} else {
"maintenancetool"
};
let new_app = path.join(platform_extension);
let mut file_metadata = OpenOptions::new();
file_metadata.write(true).create_new(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
file_metadata.mode(0o770);
}
let mut new_app_file = match file_metadata.open(new_app) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open installer binary: {:?}", v)),
};
match copy(&mut current_app_file, &mut new_app_file) {
Err(v) => return Err(format!("Unable to copy installer binary: {:?}", v)),
_ => {}
};
Ok(())
}
2018-05-03 05:30:58 +02:00
/// Saves the applications database.
pub fn save_database(&self) -> Result<(), String> {
// We have to have a install path for us to be able to do anything
let path = match self.install_path.clone() {
Some(v) => v,
None => return Err(format!("No install directory for installer")),
2018-05-03 05:30:58 +02:00
};
let metadata_path = path.join("metadata.json");
2018-05-03 05:30:58 +02:00
let metadata_file = match File::create(metadata_path) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open file handle: {:?}", v)),
};
match serde_json::to_writer(metadata_file, &self.database) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to write to file: {:?}", v)),
};
Ok(())
}
/// Configures this installer to install to the specified location.
/// If there was a currently configured install path, this will be left as-is.
pub fn set_install_dir(&mut self, dir: &str) {
self.install_path = Some(Path::new(dir).to_owned());
2018-05-03 05:30:58 +02:00
}
/// Returns metadata on the current status of the installation.
pub fn get_installation_status(&self) -> InstallationStatus {
InstallationStatus {
database: self.database.clone(),
install_path: match self.install_path.clone() {
Some(v) => Some(v.display().to_string()),
None => None,
},
preexisting_install: self.preexisting_install,
}
}
2018-01-27 04:27:41 +01:00
/// Creates a new instance of the Installer Framework with a specified Config.
2018-01-27 12:58:56 +01:00
pub fn new(config: Config) -> Self {
2018-05-03 05:30:58 +02:00
InstallerFramework {
config,
database: Vec::new(),
install_path: None,
preexisting_install: false,
2018-05-03 05:30:58 +02:00
}
}
/// Creates a new instance of the Installer Framework with a specified Config, managing
/// a pre-existing installation.
pub fn new_with_db(config: Config, install_path: &Path) -> Result<Self, String> {
let path = install_path.to_owned();
let metadata_path = path.join("metadata.json");
2018-05-03 05:30:58 +02:00
let metadata_file = match File::open(metadata_path) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open file handle: {:?}", v)),
};
let database: Vec<LocalInstallation> = match serde_json::from_reader(metadata_file) {
2018-05-03 05:30:58 +02:00
Ok(v) => v,
Err(v) => return Err(format!("Unable to read metadata file: {:?}", v)),
};
Ok(InstallerFramework {
config,
database,
install_path: Some(path),
preexisting_install: true,
2018-05-03 05:30:58 +02:00
})
2018-01-27 04:27:41 +01:00
}
}