Skip to content

Commit

Permalink
fix: save image metadata (#7)
Browse files Browse the repository at this point in the history
* fix: clyppy

* fix: create image metadata directory

* build: bump version
  • Loading branch information
easymikey authored Apr 9, 2023
1 parent ece1526 commit eb74d99
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["easymikey <[email protected]>"]
edition = "2021"
name = "lxc-tool"
version = "0.1.0"
version = "0.1.1"

[profile.release]
lto = true
Expand Down
5 changes: 1 addition & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ impl Iterator for ImageFilterIntoIterator {
];

let result = match image_filter_slice.get(self.index) {
Some((key, value)) => match value {
Some(value) => (key.to_string(), value.to_string()),
None => return None,
},
Some((key, Some(value))) => (key.to_string(), value.to_string()),
_ => return None,
};

Expand Down
1 change: 0 additions & 1 deletion src/repodata/lxc_image_download.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::{anyhow, Result};
use futures_util::StreamExt;
use indicatif::{ProgressBar, ProgressStyle};
use reqwest;
use slog_scope::info;
use std::{cmp::min, io::Write};
use tempfile::{Builder, NamedTempFile};
Expand Down
4 changes: 2 additions & 2 deletions src/repodata/lxc_image_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl FilterBy for Vec<LXCImageMetadata> {
image_filters: Vec<ImageFilter>,
) -> Result<Vec<(LXCImageMetadata, Option<PathBuf>)>> {
let filtered_containers: Vec<_> = self
.into_iter()
.iter()
.flat_map(|lxc_container_metadata| {
image_filters
.iter()
Expand All @@ -82,7 +82,7 @@ impl FilterBy for Vec<LXCImageMetadata> {
})
.collect();

if filtered_containers.len() != 0 {
if !filtered_containers.is_empty() {
Ok(filtered_containers)
} else {
bail!("Filter LXC images failed. Filter images error. Images is equal 0.")
Expand Down
2 changes: 1 addition & 1 deletion src/repodata/lxc_image_metadata_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl LXCImageMetadataCollection {
.text()
.await?
.lines()
.filter_map(|line| Some(LXCImageMetadata::of_metadata(line)))
.map(LXCImageMetadata::of_metadata)
.collect::<Result<Vec<_>>>()?;

info!("Download LXC images metadata from '{}' done.", self.url);
Expand Down
11 changes: 9 additions & 2 deletions src/repodata/lxc_image_metadata_save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nix::unistd;
use pwd::Passwd;
use slog_scope::info;
use std::{
fs::{copy, File},
fs::{self, File},
io::Write,
path::PathBuf,
time::Duration,
Expand All @@ -20,6 +20,13 @@ pub fn save_image_metadata(
info!("Save LXC image metadata started.");

let image_metadata_path = root_dir.join(&metadata_path);

if let Some(parent_dir_path) = &image_metadata_path.parent() {
if !parent_dir_path.exists() {
fs::create_dir_all(parent_dir_path)?;
}
}

let mut file = File::create(&image_metadata_path)?;

image_entries.sort_by(|a, b| a.0.name.cmp(&b.0.name).reverse());
Expand Down Expand Up @@ -51,7 +58,7 @@ pub fn save_image_metadata(

let copy_image_metadata_path = image_metadata_path.with_extension("7");

copy(&image_metadata_path, &copy_image_metadata_path)?;
fs::copy(&image_metadata_path, &copy_image_metadata_path)?;

match Passwd::from_name(&username)? {
Some(passwd) => {
Expand Down
2 changes: 1 addition & 1 deletion src/repodata/lxc_image_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn patch_image(
);
}

let mut child = Command::new(&path_to_script)
let mut child = Command::new(path_to_script)
.args(["-path", tempfile])
.args(["-d", &metadata.dist])
.args(["-r", &metadata.release])
Expand Down
19 changes: 10 additions & 9 deletions src/repodata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,21 @@ pub async fn download_images(config: config::Config) -> Result<()> {
continue;
}

fs::create_dir_all(&image_dir_path)?;
fs::create_dir_all(image_dir_path)?;

for image_file in &config.repodata.image_files {
let image_dir = lxc_image_metadata.path.to_str().ok_or_else(|| {
anyhow!(
"Download LXC image failed. Convert path to string error. Path: {:?}",
lxc_image_metadata.path
)
})?;
let download_url = config
.repodata
.target_url
.origin
.join(&lxc_image_metadata.path.to_str().ok_or_else(|| {
anyhow!(
"Download LXC image failed. Convert path to string error. Path: {:?}",
lxc_image_metadata.path
)
})?)?
.join(&image_file)?;
.join(image_dir)?
.join(image_file)?;
let tempfile = download_image(download_url).await?;

if image_file == "rootfs.tar.xz" {
Expand All @@ -70,7 +71,7 @@ pub async fn download_images(config: config::Config) -> Result<()> {
tempfile.as_file().metadata()?.permissions().set_mode(0o644);
}

fs::rename(&image_tempdir_path, &image_dir_path)?;
fs::rename(&image_tempdir_path, image_dir_path)?;
image_dir_path.metadata()?.permissions().set_mode(0o755);
}

Expand Down

0 comments on commit eb74d99

Please sign in to comment.