initial commit

Signed-off-by: Ava Hahn <a.hahn@f5.com>
This commit is contained in:
Ava Hahn 2024-04-15 17:24:35 -07:00
parent 4fdfea80bf
commit 57e626f80a
5 changed files with 7035 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

380
Cargo.lock generated Normal file
View file

@ -0,0 +1,380 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anstream"
version = "0.6.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc"
[[package]]
name = "anstyle-parse"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "bytes"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
[[package]]
name = "cc"
version = "1.0.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17f6e324229dc011159fcc089755d1e2e216a90d43a7dea6853ca740b84f35e7"
[[package]]
name = "clap"
version = "4.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "curl"
version = "0.4.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e2161dd6eba090ff1594084e95fd67aeccf04382ffea77999ea94ed42ec67b6"
dependencies = [
"curl-sys",
"libc",
"openssl-probe",
"openssl-sys",
"schannel",
"socket2",
"windows-sys",
]
[[package]]
name = "curl-sys"
version = "0.4.72+curl-8.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29cbdc8314c447d11e8fd156dcdd031d9e02a7a976163e396b548c03153bc9ea"
dependencies = [
"cc",
"libc",
"libz-sys",
"openssl-sys",
"pkg-config",
"vcpkg",
"windows-sys",
]
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "http"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258"
dependencies = [
"bytes",
"fnv",
"itoa",
]
[[package]]
name = "itoa"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "libz-sys"
version = "1.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e143b5e666b2695d28f6bca6497720813f699c9602dd7f5cac91008b8ada7f9"
dependencies = [
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]]
name = "openssl-probe"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-sys"
version = "0.9.102"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c597637d56fbc83893a35eb0dd04b2b8e7a50c91e64e9493e398b5df4fb45fa2"
dependencies = [
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "pkg-config"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
[[package]]
name = "proc-macro2"
version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a56dea16b0a29e94408b9aa5e2940a4eedbd128a1ba20e8f7ae60fd3d465af0e"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "schannel"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534"
dependencies = [
"windows-sys",
]
[[package]]
name = "socket2"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05ffd9c0a93b7543e062e759284fcf5f5e3b098501104bfbdde4d404db792871"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a6531ffc7b071655e4ce2e04bd464c4830bb585a61cabb96cf808f05172615a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unitctl"
version = "0.1.0"
dependencies = [
"clap",
"curl",
"http",
"yaml-rust",
]
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6"
[[package]]
name = "windows_i686_gnu"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9"
[[package]]
name = "windows_i686_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"
[[package]]
name = "yaml-rust"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
dependencies = [
"linked-hash-map",
]

13
Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "unitctl"
version = "0.1.0"
edition = "2021"
about = "A client to operate the NGINX Unit control API"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
curl = "0.4.46"
http = "1.1.0"
yaml-rust = "0.4.5"

252
src/main.rs Normal file
View file

@ -0,0 +1,252 @@
use clap::{
Args, Parser, Subcommand
};
use std::{
fs, env,
io::Read,
process::Command
};
use yaml_rust::{
YamlLoader, YamlEmitter
};
use curl::easy::Easy;
use http::Uri;
const OAI_SPEC: &str = include_str!("../unit-openapi.yaml");
#[derive(Parser)]
#[command(name = "unitctl")]
#[command(version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand, Clone)]
enum Commands {
#[command(arg_required_else_help = true)]
Start(StartArgs),
// TODO : Modify status call to point to some specific API endpoint
#[command(arg_required_else_help = true)]
Status(StatusArgs),
// TODO : Validate calls with openapi spec
// TODO : Access over SSH
// TODO : Access within docker containers
// TODO : Support custom certs probably
// TODO : Turn on netrc support probably?
#[command(arg_required_else_help = true)]
API(APIArgs),
// TODO : Load spec from submodule instead of duplicate file
// TODO : Use an actual OpenAPI spec parser
#[command(arg_required_else_help = true)]
Schema(SchemaArgs),
}
#[derive(Args, Clone)]
struct StartArgs {
#[arg(
short, long,
required = true,
help = "path to desired control socket"
)]
socket: String
}
#[derive(Args, Clone)]
struct StatusArgs {
#[arg(
short, long,
required = true,
help = "URI the control API listens on"
)]
uri: String,
#[arg(
short, long,
help = "Unix Socket the control API listens on"
)]
socket: Option<String>,
#[arg(
short, long,
help = "switch to trigger verbose behavior in libcurl"
)]
verbose: bool,
}
#[derive(Args, Clone)]
struct APIArgs {
#[arg(
short, long,
required = true,
help = "URI for API operation"
)]
uri: String,
#[arg(
short, long,
help = "Unix Socket the control API listens on"
)]
socket: Option<String>,
#[arg(
short, long,
conflicts_with = "file",
help = "inline JSON data to post to API"
)]
json: Option<String>,
#[arg(
short, long,
help = "file containing data to post to API."
)]
file: Option<String>,
#[arg(
short, long,
help = "switch to trigger a delete operation on an API endpoint.",
conflicts_with_all = ["file", "json"]
)]
delete: bool,
#[arg(
short, long,
help = "switch to trigger verbose behavior in libcurl"
)]
verbose: bool,
}
#[derive(Args, Clone)]
struct SchemaArgs {
#[arg(
short, long,
required = true,
help = "path for schema query"
)]
path: String,
}
fn do_start(args: StartArgs) {
Command::new("docker")
.args(["pull", "unit"])
.spawn()
.expect("failed to call Docker")
.wait()
.expect("expected Docker to succeed");
let appmount = format!(
"type=bind,src={},dst=/www",
env::current_dir().unwrap().display()
);
let sockmount = format!(
"type=bind,src={},dst=/var/run/",
args.socket
);
Command::new("docker")
.args(["run", "-d",
"--mount", appmount.as_str(),
"--mount", sockmount.as_str(),
"--network", "host", "unit"])
.spawn()
.expect("failed to call Docker")
.wait()
.expect("expected Docker to succeed");
println!("Congratulations! NGINX Unit now running at {}/control.unit.sock", args.socket);
println!("NOTICE: Socket access is root only by default. Run chown.");
println!("Current directory mounted to /www in NGINX Unit container.");
}
fn get_status(args: StatusArgs) {
do_api_call(APIArgs {
socket: args.socket,
uri: args.uri,
verbose: args.verbose,
json: None, file: None,
delete: false,
})
}
fn do_api_call(args: APIArgs) {
let mut curl = Easy::new();
if let Some(path) = args.socket {
curl.unix_socket(path.as_str()).unwrap();
}
curl.url(args.uri.as_str()).unwrap();
curl.verbose(args.verbose).unwrap();
let contents: Option<String>;
if let Some(path) = args.file {
contents = Some(fs::read_to_string(path)
.expect("Should have been able to read the file"));
} else {
contents = args.json;
}
if let None = contents {
curl.get(true).unwrap();
if let Err(e) = curl.perform() {
eprintln!("error in API call: {}", e)
}
} else {
curl.post(true).unwrap();
curl.post_field_size(contents.clone().unwrap().len() as u64).unwrap();
let mut transfer = curl.transfer();
transfer.read_function(|buf| {
Ok(contents.clone().unwrap().as_bytes().read(buf).unwrap_or(0))
}).unwrap();
if let Err(e) = transfer.perform() {
eprintln!("error in API call: {}", e)
}
}
}
fn get_schema(args: SchemaArgs) {
let maybe_path = args.path.parse::<Uri>();
if let Err(e) = maybe_path {
eprintln!("Error: couldn't load path from uri: {}", e);
return
}
let path = maybe_path.unwrap();
let spec = YamlLoader::load_from_str(OAI_SPEC).unwrap();
if spec[0]["paths"].is_badvalue() {
eprintln!("Error: no paths in OpenAPI spec!")
}
if spec[0]["paths"][path.path()].is_badvalue() {
eprintln!("Error: requested path not found.");
eprintln!("\tConsider checking manually:");
eprintln!("\thttps://github.com/nginx/unit/blob/master/docs/unit-openapi.yaml");
return;
}
let pathspec = spec[0]["paths"][path.path()].clone();
let mut out_str = String::new();
{
let mut emitter = YamlEmitter::new(&mut out_str);
emitter.dump(&pathspec).unwrap(); // dump the YAML object to a String
}
}
fn main() {
let call = Cli::parse();
match call.command {
Commands::Start(args) => do_start(args),
Commands::Status(args) => get_status(args),
Commands::API(args) => do_api_call(args),
Commands::Schema(args) => get_schema(args),
}
}
#[test]
fn verify_cli() {
use clap::CommandFactory;
Cli::command().debug_assert()
}

6389
unit-openapi.yaml Normal file

File diff suppressed because it is too large Load diff