2018-12-11 11:19:12 -05:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
2018-12-31 18:27:17 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-12-11 11:19:12 -05:00
|
|
|
|
|
|
|
use toml;
|
|
|
|
|
2018-12-14 10:10:15 -05:00
|
|
|
use libcitadel::Result;
|
|
|
|
|
2018-12-11 11:19:12 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct BuildConfig {
|
2018-12-31 18:27:17 -05:00
|
|
|
#[serde(rename = "image-type")]
|
2018-12-11 11:19:12 -05:00
|
|
|
image_type: String,
|
|
|
|
channel: String,
|
|
|
|
version: usize,
|
|
|
|
source: String,
|
2018-12-31 18:27:17 -05:00
|
|
|
#[serde(rename = "kernel-version")]
|
2018-12-11 11:19:12 -05:00
|
|
|
kernel_version: Option<String>,
|
|
|
|
|
|
|
|
#[serde(skip)]
|
|
|
|
basedir: PathBuf,
|
|
|
|
#[serde(skip)]
|
|
|
|
src_path: PathBuf,
|
|
|
|
#[serde(skip)]
|
|
|
|
img_name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BuildConfig {
|
|
|
|
pub fn load<P: AsRef<Path>>(path: P) -> Result<BuildConfig> {
|
|
|
|
let mut path = path.as_ref().to_owned();
|
|
|
|
if path.is_dir() {
|
|
|
|
path.push("mkimage.conf");
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut config = match BuildConfig::from_path(&path) {
|
|
|
|
Ok(config) => config,
|
|
|
|
Err(e) => bail!("Failed to load config file {}: {}", path.display(), e),
|
|
|
|
};
|
|
|
|
|
|
|
|
path.pop();
|
|
|
|
config.basedir = path;
|
|
|
|
config.src_path = PathBuf::from(&config.source);
|
|
|
|
config.img_name = match config.kernel_version {
|
|
|
|
Some(ref version) => format!("{}-{}", &config.image_type, version),
|
|
|
|
None => config.image_type.to_owned(),
|
|
|
|
};
|
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_path(path: &Path) -> Result<BuildConfig> {
|
|
|
|
let mut f = File::open(path)?;
|
|
|
|
let mut s = String::new();
|
|
|
|
f.read_to_string(&mut s)?;
|
|
|
|
let config = toml::from_str::<BuildConfig>(&s)?;
|
|
|
|
config.validate()?;
|
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate(&self) -> Result<()> {
|
|
|
|
let itype = self.image_type.as_str();
|
|
|
|
if itype != "extra" && itype != "rootfs" && itype != "modules" {
|
|
|
|
bail!("Invalid image type '{}'", self.image_type);
|
|
|
|
};
|
|
|
|
let src = Path::new(&self.source);
|
|
|
|
if !src.is_file() {
|
2018-12-31 18:27:17 -05:00
|
|
|
bail!(
|
|
|
|
"Source path '{}' does not exist or is not a regular file",
|
|
|
|
src.display()
|
|
|
|
);
|
2018-12-11 11:19:12 -05:00
|
|
|
}
|
|
|
|
if self.image_type == "modules" && self.kernel_version.is_none() {
|
|
|
|
bail!("Cannot build 'modules' image without kernel-version field");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn source(&self) -> &Path {
|
|
|
|
&self.src_path
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn workdir_path(&self, filename: &str) -> PathBuf {
|
|
|
|
self.basedir.join(filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn img_name(&self) -> &str {
|
|
|
|
&self.img_name
|
|
|
|
}
|
|
|
|
|
2019-01-02 12:20:29 -05:00
|
|
|
pub fn kernel_version(&self) -> Option<&str> {
|
|
|
|
self.kernel_version.as_ref().map(|s| s.as_str())
|
|
|
|
}
|
|
|
|
|
2018-12-11 11:19:12 -05:00
|
|
|
pub fn version(&self) -> usize {
|
|
|
|
self.version
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn channel(&self) -> &str {
|
|
|
|
&self.channel
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn image_type(&self) -> &str {
|
|
|
|
&self.image_type
|
|
|
|
}
|
|
|
|
}
|