Skip to content

Commit

Permalink
deploy and run mostly work
Browse files Browse the repository at this point in the history
  • Loading branch information
tiptenbrink committed Dec 15, 2023
1 parent b922a91 commit daca234
Show file tree
Hide file tree
Showing 11 changed files with 537 additions and 61 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
@@ -1,6 +1,6 @@
[package]
name = "tidploy"
version = "0.10.0"
version = "0.11.0"
edition = "2021"
license-file="LICENSE"
authors=["Tip ten Brink"]
Expand Down
4 changes: 2 additions & 2 deletions bws-dployer/entrypoint.nu
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def from_deploy [file, pipe: string] {
# open the file and load it as Nu's JSON representation
let j = open $file --raw | decode utf-8 | from json
# we get the value of secrets.ids, which is an array of id values
let secrets = $j | get secrets | get ids
let secrets = $j | get secrets
# we call the secret_to_pipe function for each id in parallel and join them
# with new lines
let output = $secrets | par-each { |e| secret_to_pipe $e } | str join "\n"
Expand All @@ -33,7 +33,7 @@ def from_deploy [file, pipe: string] {
# main entrypoint
def main [] {
# we call the from_deploy function with arguments tidploy.json and ti_dploy_pipe
from_deploy tidploy.json ti_dploy_pipe
from_deploy secrets.json ti_dploy_pipe
# once we're done we append a newline and TIDPLOY_READY=1 to the named pipe
echo "\nTIDPLOY_READY=1" | save ti_dploy_pipe --append
}
115 changes: 115 additions & 0 deletions src/archives.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use crate::errors::{RepoError, TarError};
use crate::filesystem::{FileError, FileErrorKind};
use crate::process::process_out;
use std::fs;

use std::path::{Path, PathBuf};
use std::process::{Command as Cmd};


pub(crate) fn make_archive(
archives_path: &Path,
current_dir: &Path,
source_name: &str,
target_name: &str,
) -> Result<PathBuf, RepoError> {
if !archives_path.exists() {
fs::create_dir_all(archives_path).map_err(|e| {
RepoError::from_io(
e,
format!("Couldn't create archives directory {:?}!", archives_path),
)
})?;
}

let archive_name = format!("{}.tar.gz", target_name);

let archive_path = archives_path.join(archive_name);
let archive_path_name = archive_path.to_str().ok_or(FileError {
msg: format!("Cannot represent path {:?} as string!", archive_path),
source: FileErrorKind::InvalidPath,
})?;

if archive_path.exists() {
return Ok(archive_path);
}

let mut output_archive_prog = Cmd::new("tar");
let output_archive = output_archive_prog
.current_dir(current_dir)
.arg("-czf")
.arg(archive_path_name)
.arg(source_name);

let archive_output = output_archive
.output()
.map_err(|e| TarError::from_io(e, "IO failure for tar archive!".to_owned()))?;

if !archive_output.status.success() {
return Err(
TarError::from_f(archive_output.status, "Tar archive failed!".to_owned()).into(),
);
}

println!("Saved deploy archive in tmp.");

Ok(archive_path)
}

pub(crate) fn extract_archive(
archive_path: &Path,
current_dir: &Path, // TMP DIR
target_name: &str,
) -> Result<(), RepoError> {
let archive_path_name = archive_path.to_str().ok_or(FileError {
msg: format!("Cannot represent path {:?} as string!", archive_path),
source: FileErrorKind::InvalidPath,
})?;

let target_path = current_dir.join(target_name);
if target_path.exists() {
fs::remove_dir_all(&target_path).map_err(|e| {
RepoError::from_io(
e,
format!(
"Couldn't remove target directory before recreation {:?}!",
target_name
),
)
})?;
}
fs::create_dir_all(&target_path).map_err(|e| {
RepoError::from_io(
e,
format!("Couldn't create target directory {:?}!", target_name),
)
})?;

let mut output_archive_prog = Cmd::new("tar");
let output_archive = output_archive_prog
.current_dir(current_dir)
.arg("-xzf")
.arg(archive_path_name)
.arg("-C")
.arg(target_name)
.arg("--strip-components")
.arg("1");

let archive_output = output_archive
.output()
.map_err(|e| TarError::from_io(e, "IO failure for extract archive!".to_owned()))?;

if !archive_output.status.success() {
let err_out = process_out(
archive_output.stderr,
"Tar extract failed! Could not decode output!".to_owned(),
)
.map_err(TarError::Process)?;
let msg = format!("Tar exctract failed! err: {}", err_out);
return Err(TarError::from_f(archive_output.status, msg).into());
}

println!("Extracted archive.");

Ok(())
}
Loading

0 comments on commit daca234

Please sign in to comment.