This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an iml-config cli tool (#1994)
* Create an iml-config cli tool Create an iml-config cli tool that will be used to perform config related tasks. In this patch, a drop-in will be added to nginx that will load all iml environment variables. These environment variables will then be used to generate the nginx config file from the chroma template any time nginx is restarted. This will allow the template variables to change in the nginx config based on environment variable overloading. Signed-off-by: johnsonw <wjohnson@whamcloud.com> * -cleanups Signed-off-by: johnsonw <wjohnson@whamcloud.com> * - remove nginx setup from service config Signed-off-by: johnsonw <wjohnson@whamcloud.com> * - set_nginx_config is being used by the services test so i'm adding this back in, but i'm not calling if from the setup function in the service config. Signed-off-by: johnsonw <wjohnson@whamcloud.com> * - Updates Signed-off-by: johnsonw <wjohnson@whamcloud.com> * - CR Changes Signed-off-by: johnsonw <wjohnson@whamcloud.com> * cleanups Signed-off-by: johnsonw <wjohnson@whamcloud.com> * Add failing test Add a failing test case for when there is more than 1 var to be replaced on a line. Also pass in a HashMap of vars instead of an fn to make testing different cases easier * - Move nginx config command into existing iml-manager-cli crate and create a separate binary. - Fix failing test Signed-off-by: johnsonw <wjohnson@whamcloud.com> * - use iml_tracing Signed-off-by: johnsonw <wjohnson@whamcloud.com> * - Fix clippy errors Signed-off-by: johnsonw <wjohnson@whamcloud.com> * - Tweak the regex a bit Signed-off-by: johnsonw <wjohnson@whamcloud.com> * - Remove set_config_config from service_config. Signed-off-by: johnsonw <wjohnson@whamcloud.com> * - test1 Signed-off-by: johnsonw <wjohnson@whamcloud.com> Co-authored-by: Joe Grund <jgrund@whamcloud.io>
- Loading branch information
Showing
16 changed files
with
652 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2020 DDN. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
use iml_manager_cli::{ | ||
display_utils::display_error, | ||
nginx::{self, nginx_cli}, | ||
}; | ||
use std::process::exit; | ||
use structopt::StructOpt; | ||
|
||
#[derive(Debug, StructOpt)] | ||
#[structopt(name = "iml-config", setting = structopt::clap::AppSettings::ColoredHelp)] | ||
/// The IML Config CLI | ||
pub enum App { | ||
#[structopt(name = "nginx")] | ||
/// Nginx config file generator | ||
Nginx { | ||
#[structopt(subcommand)] | ||
command: nginx::NginxCommand, | ||
}, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
iml_tracing::init(); | ||
|
||
let matches = App::from_args(); | ||
|
||
tracing::debug!("Matching args {:?}", matches); | ||
|
||
let r = match matches { | ||
App::Nginx { command } => nginx_cli(command).await, | ||
}; | ||
|
||
if let Err(e) = r { | ||
display_error(e); | ||
exit(1); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright (c) 2020 DDN. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
use crate::error::ImlManagerCliError; | ||
use console::Term; | ||
use lazy_static::*; | ||
use regex::{Captures, Regex}; | ||
use std::{collections::HashMap, env, str}; | ||
use structopt::StructOpt; | ||
use tokio::fs; | ||
|
||
#[derive(Debug, StructOpt)] | ||
pub enum NginxCommand { | ||
/// Generate Nginx conf | ||
#[structopt(name = "generate-config")] | ||
GenerateConfig { | ||
/// Set the nginx config path | ||
#[structopt(short = "p", long = "path")] | ||
template_path: String, | ||
|
||
#[structopt(short = "o", long = "output")] | ||
output_path: Option<String>, | ||
}, | ||
} | ||
|
||
fn replace_template_variables(contents: &str, vars: HashMap<String, String>) -> String { | ||
lazy_static! { | ||
static ref RE: Regex = Regex::new(r"(\{\{\w+\}\})").unwrap(); | ||
} | ||
|
||
let config: String = contents | ||
.lines() | ||
.map(|l| { | ||
RE.replace_all(l, |caps: &Captures| { | ||
let key = &caps[1].replace("{", "").replace("}", ""); | ||
let val = vars | ||
.get(key) | ||
.unwrap_or_else(|| panic!("{} variable not set", key)); | ||
|
||
caps[0].replace(&caps[1], &val) | ||
}) | ||
.to_string() | ||
}) | ||
.collect::<Vec<String>>() | ||
.join("\n"); | ||
|
||
config | ||
} | ||
|
||
pub async fn nginx_cli(command: NginxCommand) -> Result<(), ImlManagerCliError> { | ||
match command { | ||
NginxCommand::GenerateConfig { | ||
template_path, | ||
output_path, | ||
} => { | ||
let nginx_template_bytes = fs::read(template_path).await?; | ||
let nginx_template = String::from_utf8(nginx_template_bytes)?; | ||
|
||
let vars: HashMap<String, String> = env::vars().collect(); | ||
|
||
let config = replace_template_variables(&nginx_template, vars); | ||
|
||
if let Some(path) = output_path { | ||
fs::write(path, config).await?; | ||
} else { | ||
let term = Term::stdout(); | ||
|
||
iml_tracing::tracing::debug!("Nginx Config: {}", config); | ||
|
||
term.write_line(&config).unwrap(); | ||
} | ||
} | ||
}; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use insta::*; | ||
use std::collections::HashMap; | ||
|
||
#[test] | ||
fn test_replace_template_variables() { | ||
let template: &[u8] = include_bytes!("../../chroma-manager.conf.template"); | ||
|
||
let vars: HashMap<String, String> = [ | ||
("REPO_PATH", "/var/lib/chroma/repo"), | ||
("HTTP_FRONTEND_PORT", "80"), | ||
("HTTPS_FRONTEND_PORT", "443"), | ||
("HTTP_AGENT_PROXY_PASS", "http://127.0.0.1:8002"), | ||
("HTTP_AGENT2_PROXY_PASS", "http://127.0.0.1:8003"), | ||
("HTTP_API_PROXY_PASS", "http://127.0.0.1:8001"), | ||
("IML_API_PROXY_PASS", "http://127.0.0.1:8004"), | ||
("WARP_DRIVE_PROXY_PASS", "http://127.0.0.1:8890"), | ||
("MAILBOX_PATH", "/var/spool/iml/mailbox"), | ||
("MAILBOX_PROXY_PASS", "http://127.0.0.1:8891"), | ||
("SSL_PATH", "/var/lib/chroma"), | ||
("DEVICE_AGGREGATOR_PORT", "8008"), | ||
("DEVICE_AGGREGATOR_PROXY_PASS", "http://127.0.0.1:8008"), | ||
( | ||
"UPDATE_HANDLER_PROXY_PASS", | ||
"http://unix:/var/run/iml-update-handler.sock", | ||
), | ||
("GRAFANA_PORT", "3000"), | ||
("GRAFANA_PROXY_PASS", "http://127.0.0.1:3000"), | ||
("INFLUXDB_PROXY_PASS", "http://127.0.0.1:8086"), | ||
("TIMER_PROXY_PASS", "http://127.0.0.1:8892"), | ||
("INCLUDES", ""), | ||
] | ||
.iter() | ||
.map(|(k, v)| (k.to_string(), v.to_string())) | ||
.collect(); | ||
|
||
let config = replace_template_variables( | ||
&str::from_utf8(template).expect("Couldn't parse template"), | ||
vars, | ||
); | ||
|
||
assert_display_snapshot!(config); | ||
} | ||
|
||
#[test] | ||
fn test_multiple_replacements() { | ||
let vars: HashMap<String, String> = [("FOO", "foo"), ("BAR", "bar")] | ||
.iter() | ||
.map(|(k, v)| (k.to_string(), v.to_string())) | ||
.collect(); | ||
|
||
let cfg = replace_template_variables("{{FOO}}/{{BAR}}", vars); | ||
|
||
assert_eq!(cfg, "foo/bar"); | ||
} | ||
} |
Oops, something went wrong.