Skip to content

Commit

Permalink
Add flag and mechanism to skip startup of the supervisors gossip layer
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Peck <[email protected]>
  • Loading branch information
Matthew Peck committed Feb 19, 2019
1 parent e9f86c5 commit 97f88fb
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 164 deletions.
12 changes: 7 additions & 5 deletions components/hab/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use crate::{
command::studio,
common::{
cli_defaults::{
GOSSIP_DEFAULT_ADDR, GOSSIP_LISTEN_ADDRESS_ENVVAR, LISTEN_CTL_DEFAULT_ADDR_STRING,
LISTEN_HTTP_ADDRESS_ENVVAR, LISTEN_HTTP_DEFAULT_ADDR, RING_ENVVAR, RING_KEY_ENVVAR,
LISTEN_CTL_DEFAULT_ADDR_STRING, LISTEN_HTTP_ADDRESS_ENVVAR, LISTEN_HTTP_DEFAULT_ADDR,
RING_ENVVAR, RING_KEY_ENVVAR,
},
types::{EnvConfig, ListenCtlAddr},
},
Expand Down Expand Up @@ -887,9 +887,11 @@ pub fn sub_sup_run() -> App<'static, 'static> {
// set custom usage string, otherwise the binary
// is displayed confusingly as `hab-sup`
// see: https://github.com/kbknapp/clap-rs/blob/2724ec5399c500b12a1a24d356f4090f4816f5e2/src/app/mod.rs#L373-L394
(usage: "hab sup run [FLAGS] [OPTIONS] [--] [PKG_IDENT_OR_ARTIFACT]")
(@arg LISTEN_GOSSIP: --("listen-gossip") env(GOSSIP_LISTEN_ADDRESS_ENVVAR) default_value(&GOSSIP_DEFAULT_ADDR) {valid_socket_addr}
"The listen address for the Gossip System Gateway.")
(@group GOSSIP =>
(@arg NO_LISTEN_GOSSIP: --("no-listen-gossip") "Don't startup the gossip listener.")
(@arg LISTEN_GOSSIP: --("listen-gossip") +takes_value {valid_socket_addr}
"The listen address for the Gossip System Gateway.")
)
(@arg LISTEN_HTTP: --("listen-http") env(LISTEN_HTTP_ADDRESS_ENVVAR) default_value(&LISTEN_HTTP_DEFAULT_ADDR) {valid_socket_addr}
"The listen address for the HTTP Gateway.")
(@arg HTTP_DISABLE: --("http-disable") -D
Expand Down
15 changes: 15 additions & 0 deletions components/sup/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod test {

mod sup_run {
use super::*;
use std::iter::FromIterator;

assert_cli_cmd!(should_handle_multiple_peer_flags,
"hab-sup run --peer 1.1.1.1 --peer 2.2.2.2",
Expand Down Expand Up @@ -57,6 +58,20 @@ mod test {
"BIND" => ["service.group1", "service.group2"],
"PKG_IDENT_OR_ARTIFACT" => "core/redis");

#[test]
fn listen_gossip_and_no_listen_gossip_are_mutually_exclusive() {
let cmd_vec = Vec::from_iter(
"hab-sup run --no-listen-gossip --listen-gossip 1.1.1.1:1111".split_whitespace(),
);
let matches = cli().get_matches_from_safe(cmd_vec);

assert!(matches.is_err());

if let Err(clap::Error { kind, .. }) = matches {
assert_eq!(kind, ErrorKind::ArgumentConflict);
}
}

}

}
28 changes: 15 additions & 13 deletions components/sup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use crate::{
common::{
cli_defaults::GOSSIP_DEFAULT_PORT,
command::package::install::InstallSource,
types::EnvConfig,
ui::{Coloring, NONINTERACTIVE_ENVVAR, UI},
},
hcore::{
Expand Down Expand Up @@ -228,6 +229,17 @@ fn sub_term() -> Result<()> {
////////////////////////////////////////////////////////////////////////

fn mgrcfg_from_sup_run_matches(m: &ArgMatches) -> Result<ManagerConfig> {
let gossip_listen = if m.is_present("NO_LISTEN_GOSSIP") {
None
} else {
Some(
m.value_of("LISTEN_GOSSIP")
.map_or(Ok(sup::config::GossipListenAddr::configured_value()), |v| {
v.parse()
})?,
)
};

let cfg = ManagerConfig {
auto_update: m.is_present("AUTO_UPDATE"),
update_url: bldr_url(m),
Expand All @@ -239,17 +251,7 @@ fn mgrcfg_from_sup_run_matches(m: &ArgMatches) -> Result<ManagerConfig> {
gossip_peers: get_peers(m)?,
watch_peer_file: m.value_of("PEER_WATCH_FILE").map(str::to_string),
// TODO: Refactor this to remove the duplication
gossip_listen: m.value_of("LISTEN_GOSSIP").map_or_else(
|| {
let default = sup::config::GossipListenAddr::default();
error!(
"Value for LISTEN_GOSSIP has not been set. Using default: {}",
default
);
Ok(default)
},
|v| v.parse(),
)?,
gossip_listen,
ctl_listen: m.value_of("LISTEN_CTL").map_or_else(
|| {
let default = common::types::ListenCtlAddr::default();
Expand Down Expand Up @@ -601,14 +603,14 @@ mod test {
let config = config_from_cmd_str("hab-sup run --listen-gossip 1.1.1.1:1111");
let expected_addr = GossipListenAddr::from_str("1.1.1.1:1111")
.expect("Could not create GossipListenAddr");
assert_eq!(config.gossip_listen, expected_addr);
assert_eq!(config.gossip_listen, Some(expected_addr));
}

#[test]
fn gossip_listen_is_set_to_default_when_not_specified() {
let config = config_from_cmd_str("hab-sup run");
let expected_addr = GossipListenAddr::default();
assert_eq!(config.gossip_listen, expected_addr);
assert_eq!(config.gossip_listen, Some(expected_addr));
}

#[test]
Expand Down
77 changes: 28 additions & 49 deletions components/sup/src/manager/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,23 +157,10 @@ pub fn service_cfg_set(
version,
service_group,
);
let mut client = match butterfly::client::Client::new(
mgr.cfg.gossip_listen.local_addr(),
mgr.cfg.ring_key.clone(),
) {
Ok(client) => client,
Err(err) => {
outputln!("Failed to connect to own gossip server, {}", err);
return Err(net::err(ErrCode::Internal, err.to_string()));
}
};
match client.send_service_config(service_group, version, &cfg, is_encrypted) {
Ok(()) => {
req.reply_complete(net::ok());
return Ok(());
}
Err(e) => return Err(net::err(ErrCode::Internal, e.to_string())),
}
create_butterfly_client(mgr)?
.send_service_config(service_group, version, &cfg, is_encrypted)
.map(|_| req.reply_complete(net::ok()))
.map_err(|e| net::err(ErrCode::Internal, e.to_string()))
}

pub fn service_file_put(
Expand All @@ -195,22 +182,27 @@ pub fn service_file_put(
filename,
service_group,
);
let mut client = match butterfly::client::Client::new(
mgr.cfg.gossip_listen.local_addr(),
mgr.cfg.ring_key.clone(),
) {
Ok(client) => client,
Err(err) => {
outputln!("Failed to connect to own gossip server, {}", err);
return Err(net::err(ErrCode::Internal, err.to_string()));
}
};
match client.send_service_file(service_group, filename, version, &content, is_encrypted) {
Ok(()) => {
req.reply_complete(net::ok());
return Ok(());
create_butterfly_client(mgr)?
.send_service_file(service_group, filename, version, &content, is_encrypted)
.map(|_| req.reply_complete(net::ok()))
.map_err(|e| net::err(ErrCode::Internal, e.to_string()))
}

fn create_butterfly_client(mgr: &ManagerState) -> NetResult<butterfly::client::Client> {
if let Some(gossip_addr) = &mgr.cfg.gossip_listen {
match butterfly::client::Client::new(gossip_addr.local_addr(), mgr.cfg.ring_key.clone()) {
Ok(client) => Ok(client),
Err(err) => {
outputln!("Failed to connect to own gossip server, {}", err);
Err(net::err(ErrCode::Internal, err.to_string()))
}
}
Err(e) => return Err(net::err(ErrCode::Internal, e.to_string())),
} else {
outputln!("Cannot execute command without a running gossip server.");
Err(net::err(
ErrCode::Internal,
"Gossip not started".to_string(),
))
}
}

Expand Down Expand Up @@ -378,24 +370,11 @@ pub fn supervisor_depart(
opts: protocol::ctl::SupDepart,
) -> NetResult<()> {
let member_id = opts.member_id.ok_or_else(err_update_client)?;
let mut client = match butterfly::client::Client::new(
mgr.cfg.gossip_listen.local_addr(),
mgr.cfg.ring_key.clone(),
) {
Ok(client) => client,
Err(err) => {
outputln!("Failed to connect to own gossip server, {}", err);
return Err(net::err(ErrCode::Internal, err.to_string()));
}
};
outputln!("Attempting to depart member: {}", member_id);
match client.send_departure(member_id) {
Ok(()) => {
req.reply_complete(net::ok());
Ok(())
}
Err(e) => Err(net::err(ErrCode::Internal, e.to_string())),
}
create_butterfly_client(mgr)?
.send_departure(member_id)
.map(|_| req.reply_complete(net::ok()))
.map_err(|e| net::err(ErrCode::Internal, e.to_string()))
}

pub fn service_status(
Expand Down
Loading

0 comments on commit 97f88fb

Please sign in to comment.