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 <[email protected]>
  • Loading branch information
avahahn committed Jun 19, 2024
1 parent e0c15ae commit fe2eef9
Show file tree
Hide file tree
Showing 7 changed files with 130 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 @@ -177,6 +177,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 omits certificates.

### 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 @@ -5,3 +5,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(())
}
8 changes: 7 additions & 1 deletion tools/unitctl/unitctl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ extern crate unit_client_rs;

use clap::Parser;

use crate::cmd::{applications, edit, execute as execute_cmd, import, instances, listeners, status};
use crate::cmd::{
applications, 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 @@ -46,6 +50,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
10 changes: 10 additions & 0 deletions tools/unitctl/unitctl/src/unitctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ pub(crate) enum Commands {
},
#[command(about = "List all configured Unit applications")]
App(ApplicationArgs),

#[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 fe2eef9

Please sign in to comment.