Commit d33fdd60 authored by Heinz N. Gies's avatar Heinz N. Gies

Add updatge functionality

parent 63cd0a1b
target
*.fmt
\ No newline at end of file
......@@ -210,27 +210,36 @@ pub struct JailConfig {
pub max_lwps: u64,
// Metadata fields w/o effect on vmadm at the moment
/// Should be archived when deleted
#[serde(skip_serializing_if = "Option::is_none")]
archive_on_delete: Option<bool>,
pub archive_on_delete: Option<bool>,
/// Bulling ID for the jail
#[serde(skip_serializing_if = "Option::is_none")]
billing_id: Option<Uuid>,
pub billing_id: Option<Uuid>,
/// This jail should not be be part of inventories
#[serde(skip_serializing_if = "Option::is_none")]
do_not_inventory: Option<bool>,
pub do_not_inventory: Option<bool>,
// Currently has no effect
/// dns domain for the jail
#[serde(default = "dflt_dns_domain")]
dns_domain: String,
pub dns_domain: String,
// currently no effect
/// Prevent the jail delegate to be destroyed
#[serde(skip_serializing_if = "Option::is_none")]
indestructible_delegated: Option<bool>,
// currenlty no effect
/// Prevent the jail root to be destroyed
#[serde(skip_serializing_if = "Option::is_none")]
indestructible_zoneroot: Option<bool>,
pub indestructible_zoneroot: Option<bool>,
/// UUID of the owner of the jail
#[serde(skip_serializing_if = "Option::is_none")]
owner_uuid: Option<Uuid>,
pub owner_uuid: Option<Uuid>,
/// Name of the package used for this jail
#[serde(skip_serializing_if = "Option::is_none")]
package_name: Option<String>,
pub package_name: Option<String>,
/// Version of the package used for this jail
#[serde(skip_serializing_if = "Option::is_none")]
package_version: Option<String>,
pub package_version: Option<String>,
// TODO:
// customer_metadata: KV
// internal_metadata: KV
......
......@@ -153,6 +153,30 @@ impl<'a> JDB<'a> {
}
}
/// Inserts a config into the database, writes the config file
/// and adds it to the index.
pub fn update(self: &'a mut JDB<'a>, config: JailConfig) -> Result<i32, Box<Error>> {
debug!("Updating vm"; "vm" => &config.uuid.hyphenated().to_string());
match self.find(&config.uuid) {
None => {
warn!("Missing entry {}", config.uuid; "vm" => &config.uuid.hyphenated().to_string());
Err(NotFoundError::bx(&config.uuid))
}
Some(_) => {
let mut path = PathBuf::from(self.config.settings.conf_dir.as_str());
path.push(config.uuid.hyphenated().to_string());
path.set_extension("json");
debug!("Updating config file"; "file" => path.to_str(), "vm" => &config.uuid.hyphenated().to_string());
let file = File::create(path)?;
let mut root = String::from(self.config.settings.pool.as_str());
serde_json::to_writer(file, &config)?;
// This is ugly but I don't know any better.
Ok(0)
}
}
}
/// Removes a jail with a given uuid from the index and removes it's
/// config file.
pub fn remove(self: &'a mut JDB<'a>, uuid: &Uuid) -> Result<usize, Box<Error>> {
......
......@@ -55,6 +55,8 @@ mod zfs;
pub mod jails;
pub mod jail_config;
mod update;
use jail_config::JailConfig;
pub mod jdb;
......@@ -311,10 +313,6 @@ fn stop(conf: &Config, matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
}
}
fn update(_conf: &Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
Ok(0)
}
fn list(conf: &Config, matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
let db = JDB::open(conf)?;
db.print(
......@@ -323,6 +321,29 @@ fn list(conf: &Config, matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
)
}
fn update(conf: &Config, matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
let mut db = JDB::open(conf)?;
let uuid_string = value_t!(matches, "uuid", String).unwrap();
let uuid = Uuid::parse_str(uuid_string.as_str()).unwrap();
let update = match value_t!(matches, "file", String) {
Err(_) => {
debug!("Reading from STDIN");
update::JailUpdate::from_reader(io::stdin())?
}
Ok(file) => {
debug!("Reading from file"; "file" => file.clone() );
update::JailUpdate::from_reader(File::open(file)?)?
}
};
match db.get(&uuid) {
Err(e) => Err(e),
Ok(jdb::Jail { config: c, .. }) => {
let c = update.apply(c);
db.update(c)
}
}
}
fn create(conf: &Config, matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
let jail = match value_t!(matches, "file", String) {
Err(_) => {
......
//! Update for a jail
use config::Config;
use jail_config::JailConfig;
use std::error::Error;
use std::fs::File;
use std::io::Read;
use serde_json;
use uuid::Uuid;
/// Jail update
#[derive(Debug, Deserialize, Clone)]
pub struct JailUpdate {
/// readable alias for the jail
alias: Option<String>,
/// hostname of the jail
hostname: Option<String>,
/// weather to start this jail on --startup
autostart: Option<bool>,
/// max physical memory in MB (memoryuse)
max_physical_memory: Option<u64>,
/// mac cpu usage 100 = 1 core (pcpu)
cpu_cap: Option<u64>,
/// max quota (zfs quota)
// quota: u64,
/// SysV shared memory size, in bytes (shmsize)
max_shm_memory: Option<u64>,
/// locked memory (memorylocked)
max_locked_memory: Option<u64>,
/// maximum number of porocesses (maxproc)
max_lwps: Option<u64>,
// Metadata fields w/o effect on vmadm at the moment
archive_on_delete: Option<bool>,
billing_id: Option<Uuid>,
do_not_inventory: Option<bool>,
// Currently has no effect
dns_domain: Option<String>,
owner_uuid: Option<Uuid>,
package_name: Option<String>,
package_version: Option<String>,
}
macro_rules! update {
( $src:ident, $target:ident; $($field:ident),+) => (
$(
match $src.$field {
Some(ref value) => $target.$field = value.clone(),
_ => ()
}
)*
);
}
macro_rules! update_option {
( $src:ident, $target:ident; $($field:ident),+) => (
$(
match $src.$field {
Some(ref value) => $target.$field = Some(value.clone()),
_ => ()
}
)*
);
}
impl JailUpdate {
/// Reads a new config from a file
pub fn from_file(config_path: &str) -> Result<Self, Box<Error>> {
let config_file = File::open(config_path)?;
JailUpdate::from_reader(config_file)
}
/// Reads the config from a reader
pub fn from_reader<R>(reader: R) -> Result<Self, Box<Error>>
where
R: Read,
{
let update: JailUpdate = serde_json::from_reader(reader)?;
return Ok(update);
}
pub fn apply(&self, config: JailConfig) -> JailConfig {
let mut c = config.clone();
update!(self, c;
autostart,
alias,
hostname,
max_physical_memory,
cpu_cap,
max_lwps,
dns_domain
);
update_option!(self, c;
max_shm_memory,
max_locked_memory,
archive_on_delete,
billing_id,
do_not_inventory,
owner_uuid,
package_name,
package_version
);
return c;
}
}
\ No newline at end of file
{"alias":"update"}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment