Skip to content

Commit

Permalink
✨ feature: 0.0.2: HostFile/HostDir!
Browse files Browse the repository at this point in the history
  • Loading branch information
queer committed May 25, 2023
1 parent 4703367 commit e0b23a1
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 13 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/host-file-dir-functionality.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: "Validate host file/dir functionality"
on:
push:
branches:
- "mistress"
paths:
- "**.rs"
- "**.yaml"
- "**.toml"
pull_request:
branches:
- "mistress"
paths:
- "**.rs"
- "**.yaml"
- "**.toml"
jobs:
run-host-file-dir-functionality-tests:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v2"
- name: "Install latest stable Rust"
uses: "actions-rs/toolchain@v1"
with:
toolchain: "stable"
profile: "minimal"
override: true
target: "x86_64-unknown-linux-musl"
- uses: "Swatinem/rust-cache@v2"
with:
key: "cli-functionality"
cache-on-failure: true
- name: "Install musl-tools"
run: "sudo apt-get update && sudo apt-get install -y musl-tools"
- name: "Build release binary!"
uses: "actions-rs/cargo@v1"
with:
command: "build"
args: "--release --target=x86_64-unknown-linux-musl"
- name: "Run release binary"
run: "pwd && ls -lah && env RUST_LOG=debug ./target/x86_64-unknown-linux-musl/release/peckish -c ./test-configs/peckish.host_file_dir.yaml"
- name: "Validate file contents"
run: "tar -tvf ./test_output_pls_ignore/host_file_dir | grep hosts2"
- name: "Validate directory contents"
run: "tar -tvf ./test_output_pls_ignore/host_file_dir | grep docs2"
110 changes: 108 additions & 2 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "peckish"
version = "0.0.1"
version = "0.0.2"
description = "Create, manipulate, and transform Linux package formats!"
license = "Apache-2.0"
edition = "2021"
Expand All @@ -27,6 +27,7 @@ indoc = "2.0.1"
itertools = "0.10.5"
md5 = "0.7.0"
nix = "0.26.2"
nyoom = "0.0.6"
rand = "0.8.5"
regex = "1.8.2"
rpm = { version = "0.10.0", features = ["with-file-async-tokio"] }
Expand Down
22 changes: 22 additions & 0 deletions docs/injections.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,25 @@ responsibility.
path: "/hello.txt"
content: "hello world"
```

- host file `"host_file"`

Copies a file from the host to the given location in the artifact.

```yaml
injections:
- type: "host_file"
src: "/etc/hosts"
dest: "/etc/hosts2"
```

- host directory `"host_dir"`

Copies a directory from the host to the given location in the artifact.

```yaml
injections:
- type: "host_dir"
src: "/etc"
dest: "/etc2"
```
1 change: 1 addition & 0 deletions src/artifact/deb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl DebArtifact {
fs.copy_files_from_paths(
&vec![output_tmp.path_view()],
Some(output_tmp.path_view()),
None,
)
.await?;

Expand Down
2 changes: 1 addition & 1 deletion src/artifact/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Artifact for DockerArtifact {
debug!("copying docker layers to memfs!");

let memfs_paths = vec![tmp.path_view()];
fs.copy_files_from_paths(&memfs_paths, Some(tmp.path_view()))
fs.copy_files_from_paths(&memfs_paths, Some(tmp.path_view()), None)
.await?;

Ok(fs)
Expand Down
4 changes: 2 additions & 2 deletions src/artifact/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ impl Artifact for FileArtifact {
} else {
path
};
fs.copy_files_from_paths(&vec![path.clone()], Some(prefix.into()))
fs.copy_files_from_paths(&vec![path.clone()], Some(prefix.into()), None)
.await?;
}
} else {
fs.copy_files_from_paths(&self.paths, None).await?;
fs.copy_files_from_paths(&self.paths, None, None).await?;
}

Ok(fs)
Expand Down
2 changes: 1 addition & 1 deletion src/artifact/tarball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Artifact for TarballArtifact {

debug!("copying {} paths to memfs!", paths.len());

fs.copy_files_from_paths(&paths, Some(tmp.path_view()))
fs.copy_files_from_paths(&paths, Some(tmp.path_view()), None)
.await?;

Ok(fs)
Expand Down
19 changes: 13 additions & 6 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl MemFS {
&self,
paths: &Vec<PathBuf>,
view_of: Option<PathBuf>,
dest: Option<PathBuf>,
) -> Result<()> {
for path in paths {
let memfs_path = if let Some(ref view) = view_of {
Expand All @@ -103,13 +104,19 @@ impl MemFS {
} else {
path
};
let memfs_path = if let Some(ref dest) = dest {
dest.join(memfs_path)
} else {
memfs_path.to_path_buf()
};
info!("{} -> {}", path.display(), memfs_path.display());
let file_type = self.determine_file_type_from_filesystem(path).await?;
if file_type == InternalFileType::Dir {
self.copy_dir_to_memfs(path, memfs_path).await?;
self.copy_dir_to_memfs(path, &memfs_path).await?;
} else if file_type == InternalFileType::File {
self.copy_file_to_memfs(path, memfs_path).await?;
self.copy_file_to_memfs(path, &memfs_path).await?;
} else if file_type == InternalFileType::Symlink {
self.add_symlink_to_memfs(path, memfs_path).await?;
self.add_symlink_to_memfs(path, &memfs_path).await?;
} else {
error!("unknown file type for path {path:?}");
}
Expand All @@ -118,7 +125,7 @@ impl MemFS {
Ok(())
}

async fn copy_file_to_memfs(&self, path: &Path, memfs_path: &Path) -> Result<()> {
pub(crate) async fn copy_file_to_memfs(&self, path: &Path, memfs_path: &Path) -> Result<()> {
use rsfs_tokio::unix_ext::PermissionsExt;

debug!("creating file {path:?}");
Expand Down Expand Up @@ -149,7 +156,7 @@ impl MemFS {
}

#[async_recursion::async_recursion]
async fn copy_dir_to_memfs(&self, path: &Path, memfs_path: &Path) -> Result<()> {
pub(crate) async fn copy_dir_to_memfs(&self, path: &Path, memfs_path: &Path) -> Result<()> {
use rsfs_tokio::unix_ext::PermissionsExt;

self.fs.create_dir_all(memfs_path).await?;
Expand Down Expand Up @@ -180,7 +187,7 @@ impl MemFS {
Ok(())
}

async fn add_symlink_to_memfs(&self, path: &Path, memfs_path: &Path) -> Result<()> {
pub(crate) async fn add_symlink_to_memfs(&self, path: &Path, memfs_path: &Path) -> Result<()> {
let link = read_link(&path).await.map_err(Fix::Io)?;
debug!("linking {memfs_path:?} to {link:?}");
self.fs.symlink(link, memfs_path).await?;
Expand Down
12 changes: 12 additions & 0 deletions src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ pub enum Injection {
Touch { path: PathBuf },
Delete { path: PathBuf },
Create { path: PathBuf, content: Vec<u8> },
HostFile { src: PathBuf, dest: PathBuf },
HostDir { src: PathBuf, dest: PathBuf },
}

impl Injection {
Expand Down Expand Up @@ -450,6 +452,16 @@ impl Injection {
let mut file = fs.create_file(path).await?;
file.write_all(content).await?;
}

Injection::HostFile { src, dest } => {
debug!("copying host file {:?} to {:?}", src, dest);
memfs.copy_file_to_memfs(src, dest).await?;
}

Injection::HostDir { src, dest } => {
debug!("copying host directory {:?} to {:?}", src, dest);
memfs.copy_dir_to_memfs(src, dest).await?;
}
}

Ok(())
Expand Down
27 changes: 27 additions & 0 deletions test-configs/peckish.host_file_dir.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pipeline: false

metadata:
name: "peckish"
version: "0.0.1-1"
description: "peckish transforms software artifacts"
author: "amy null"
arch: "amd64"
license: "Apache-2.0"

input:
name: "Cargo.toml"
type: "file"
paths:
- "./Cargo.toml"

output:
- name: "tarball"
type: "tarball"
path: "./test_output_pls_ignore/host_file_dir.tar"
injections:
- type: "host_file"
src: "/etc/hosts"
dest: "/etc/hosts2"
- type: "host_dir"
src: "./docs"
dest: "/docs2"

0 comments on commit e0b23a1

Please sign in to comment.