Skip to content

Commit

Permalink
tools/unitctl: unitctl export
Browse files Browse the repository at this point in the history
* new subcommand for "export" in CLI
* new cmd submodule for exporting config tarballs
* logic to also output to stdout
* README additions
* limitations documented

Signed-off-by: Ava Hahn <a.hahn@f5.com>
  • Loading branch information
avahahn authored and ava-affine committed Jun 19, 2024
1 parent 35a572c commit 798f392
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 22 deletions.
66 changes: 45 additions & 21 deletions tools/unitctl/Cargo.lock

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

14 changes: 14 additions & 0 deletions tools/unitctl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ Imported /opt/unit/config/put.json -> /config
Imported 3 files
```

### Export configuration from a running Unit instance
```
$ unitctl export -f config.tar
```

Addtionally, standard out can be used:
```
$ unitctl export -f -
$ unitctl export -f - | tar xf - config.json
$ unitctl export -f - > config.tar
```

*Note:* The exported configuration will be devoid of any installed certificates. The Unit configuration API does not support fetching certificates or private keys once installed. User's will need to add relevant certificates and keys to an export.

### Wait for socket to become available
```
$ unitctl --wait-timeout-seconds=3 --wait-max-tries=4 import /opt/unit/config`
Expand Down
1 change: 1 addition & 0 deletions tools/unitctl/unitctl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ hyperlocal = "0.8"
hyper-tls = "0.5"
tokio = { version = "1.35", features = ["macros"] }
futures = "0.3"
tar = "0.4.41"

[package.metadata.deb]
copyright = "2022, F5"
Expand Down
1 change: 1 addition & 0 deletions tools/unitctl/unitctl/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub(crate) mod import;
pub(crate) mod instances;
pub(crate) mod listeners;
pub(crate) mod status;
pub(crate) mod save;
52 changes: 52 additions & 0 deletions tools/unitctl/unitctl/src/cmd/save.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::unitctl::UnitCtl;
use crate::wait;
use crate::UnitctlError;
use crate::requests::send_empty_body_deserialize_response;
use unit_client_rs::unit_client::UnitClient;
use tar::{Builder, Header};
use std::fs::File;
use std::io::stdout;


pub async fn cmd(
cli: &UnitCtl,
filename: &String
) -> Result<(), UnitctlError> {
if !filename.ends_with(".tar") {
eprintln!("Warning: writing uncompressed tarball to {}", filename);
}

let control_socket = wait::wait_for_socket(cli).await?;
let client = UnitClient::new(control_socket);

let config_res = serde_json::to_string_pretty(
&send_empty_body_deserialize_response(&client, "GET", "/config").await?
);
if let Err(e) = config_res {
return Err(UnitctlError::DeserializationError{message: e.to_string()})
}

let current_config = config_res
.unwrap()
.into_bytes();

//let current_js_modules = send_empty_body_deserialize_response(&client, "GET", "/js_modules")
// .await?;

let mut conf_header = Header::new_gnu();
conf_header.set_size(current_config.len() as u64);
conf_header.set_mode(0o644);
conf_header.set_cksum();

// builder has a different type depending on output
if filename == "-" {
let mut ar = Builder::new(stdout());
ar.append_data(&mut conf_header, "config.json", current_config.as_slice()).unwrap();
} else {
let file = File::create(filename).unwrap();
let mut ar = Builder::new(file);
ar.append_data(&mut conf_header, "config.json", current_config.as_slice()).unwrap();
}

Ok(())
}
4 changes: 3 additions & 1 deletion tools/unitctl/unitctl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate unit_client_rs;

use clap::Parser;

use crate::cmd::{edit, execute as execute_cmd, import, instances, listeners, status};
use crate::cmd::{edit, execute as execute_cmd, import, instances, listeners, status, save};
use crate::output_format::OutputFormat;
use crate::unitctl::{Commands, UnitCtl};
use crate::unitctl_error::UnitctlError;
Expand Down Expand Up @@ -44,6 +44,8 @@ async fn main() -> Result<(), UnitctlError> {
Commands::Status { output_format } => status::cmd(&cli, output_format).await,

Commands::Listeners { output_format } => listeners::cmd(&cli, output_format).await,

Commands::Export { ref filename } => save::cmd(&cli, filename).await,
}
.map_err(|error| {
eprint_error(&error);
Expand Down
9 changes: 9 additions & 0 deletions tools/unitctl/unitctl/src/unitctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ pub(crate) enum Commands {
)]
output_format: OutputFormat,
},
#[command(about = "Export the current configuration of UNIT")]
Export {
#[arg(
required = true,
short = 'f',
help = "tarball filename to save configuration to"
)]
filename: String
},
}

#[derive(Debug, Args)]
Expand Down

0 comments on commit 798f392

Please sign in to comment.