Skip to content

Commit

Permalink
Set remaning ManagerConfig values in the declaration block
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Peck <mpeck@chef.io>
  • Loading branch information
Matthew Peck committed Jan 3, 2019
1 parent 1feafb9 commit ba52b88
Show file tree
Hide file tree
Showing 19 changed files with 194 additions and 158 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

14 changes: 7 additions & 7 deletions components/common/src/cli_defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@
//! Eventually this will be composed of fully typed default values. But as a first step we
//! need a spot to consolidate those values and help simplify some of the logic around them.

use ListenCtlAddr;
use types::ListenCtlAddr;

pub const GOSSIP_DEFAULT_IP: &'static str = "0.0.0.0";
pub const GOSSIP_DEFAULT_IP: &str = "0.0.0.0";
pub const GOSSIP_DEFAULT_PORT: u16 = 9638;
lazy_static! {
pub static ref GOSSIP_DEFAULT_ADDR: String =
{ format!("{}:{}", GOSSIP_DEFAULT_IP, GOSSIP_DEFAULT_PORT) };
}
pub const GOSSIP_LISTEN_ADDRESS_ENVVAR: &'static str = "HAB_LISTEN_GOSSIP";
pub const RING_ENVVAR: &'static str = "HAB_RING";
pub const RING_KEY_ENVVAR: &'static str = "HAB_RING_KEY";
pub const GOSSIP_LISTEN_ADDRESS_ENVVAR: &str = "HAB_LISTEN_GOSSIP";
pub const RING_ENVVAR: &str = "HAB_RING";
pub const RING_KEY_ENVVAR: &str = "HAB_RING_KEY";

pub const LISTEN_HTTP_DEFAULT_PORT: u16 = 9631;
pub const LISTEN_HTTP_DEFAULT_IP: &'static str = "0.0.0.0";
pub const LISTEN_HTTP_DEFAULT_IP: &str = "0.0.0.0";
lazy_static! {
pub static ref LISTEN_HTTP_DEFAULT_ADDR: String =
{ format!("{}:{}", LISTEN_HTTP_DEFAULT_IP, LISTEN_HTTP_DEFAULT_PORT) };
}
pub const LISTEN_HTTP_ADDRESS_ENVVAR: &'static str = "HAB_LISTEN_HTTP";
pub const LISTEN_HTTP_ADDRESS_ENVVAR: &str = "HAB_LISTEN_HTTP";

lazy_static! {
pub static ref LISTEN_CTL_DEFAULT_ADDR_STRING: String =
Expand Down
6 changes: 1 addition & 5 deletions components/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,5 @@ pub mod command;
pub mod error;
pub mod locked_env_var;
pub mod package_graph;
pub mod types;
pub mod ui;

mod listen_ctl_addr;
pub use listen_ctl_addr::ListenCtlAddr;
mod env_config;
pub use env_config::EnvConfig;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2016-2018 Chef Software Inc. and/or applicable contributors
// Copyright (c) 2018 Chef Software Inc. and/or applicable contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,14 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Configuration for the Supervisor.
//!
//! This module is populated from the CLI options in `main.rs`, and then passed through to the
//! [command](../command) modules. Check out the `config_from_args(..)` function there for more
//! details.
//!
//! See the [Config](struct.Config.html) struct for the specific options available.

use std::env::VarError;
use std::str::FromStr;

Expand Down Expand Up @@ -63,7 +55,7 @@ pub trait EnvConfig: Default + FromStr {
/// value was found and was successfully parsed as a `Self`.
///
/// By default, we log a message at the `warn` level.
fn log_parsable(env_value: &String) {
fn log_parsable(env_value: &str) {
warn!(
"Found '{}' in environment; using value '{}'",
Self::ENVVAR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
// limitations under the License.

use std::fmt;
use std::io;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4, ToSocketAddrs};
use std::option;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4};
use std::result;
use std::str::FromStr;

use super::env_config::EnvConfig;
use error::{Error, Result};
use EnvConfig;

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ListenCtlAddr(SocketAddr);

impl ListenCtlAddr {
pub const DEFAULT_PORT: u16 = 9632;

pub fn new(ip: Ipv4Addr, port: u16) -> Self {
ListenCtlAddr(SocketAddr::V4(SocketAddrV4::new(ip, port)))
}
Expand All @@ -41,7 +41,7 @@ impl ListenCtlAddr {

impl Default for ListenCtlAddr {
fn default() -> ListenCtlAddr {
ListenCtlAddr::new(Ipv4Addr::LOCALHOST, 9632)
ListenCtlAddr::new(Ipv4Addr::LOCALHOST, ListenCtlAddr::DEFAULT_PORT)
}
}

Expand All @@ -53,20 +53,24 @@ impl FromStr for ListenCtlAddr {
type Err = Error;

fn from_str(val: &str) -> Result<Self> {
Ok(ListenCtlAddr(SocketAddr::from_str(val)?))
Ok(val.parse::<SocketAddr>()?.into())
}
}

impl ToSocketAddrs for ListenCtlAddr {
type Iter = option::IntoIter<SocketAddr>;
impl fmt::Display for ListenCtlAddr {
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
write!(f, "{}", self.0)
}
}

fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
self.0.to_socket_addrs()
impl From<SocketAddr> for ListenCtlAddr {
fn from(socket_addr: SocketAddr) -> Self {
ListenCtlAddr(socket_addr)
}
}

impl fmt::Display for ListenCtlAddr {
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
write!(f, "{}", self.0)
impl AsRef<SocketAddr> for ListenCtlAddr {
fn as_ref(&self) -> &SocketAddr {
&self.0
}
}
19 changes: 19 additions & 0 deletions components/common/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2018 Chef Software Inc. and/or applicable contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod env_config;
mod listen_ctl_addr;

pub use self::env_config::EnvConfig;
pub use self::listen_ctl_addr::ListenCtlAddr;
33 changes: 2 additions & 31 deletions components/hab/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ use 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,
};
use common::EnvConfig;
use common::ListenCtlAddr;
use common::types::{EnvConfig, ListenCtlAddr};
use feat;

pub fn get() -> App<'static, 'static> {
Expand Down Expand Up @@ -862,7 +861,6 @@ pub fn sub_sup_bash() -> App<'static, 'static> {

pub fn sub_sup_run() -> App<'static, 'static> {
clap_app!(@subcommand run =>
<<<<<<< HEAD
(about: "Run the Habitat Supervisor")
// set custom usage string, otherwise the binary
// is displayed confusingly as `hab-sup`
Expand All @@ -874,7 +872,7 @@ pub fn sub_sup_run() -> App<'static, 'static> {
"The listen address for the HTTP Gateway.")
(@arg HTTP_DISABLE: --("http-disable") -D
"Disable the HTTP Gateway completely [default: false]")
(@arg LISTEN_CTL: --("listen-ctl") +takes_value {valid_socket_addr}
(@arg LISTEN_CTL: --("listen-ctl") env(ListenCtlAddr::ENVVAR) default_value(&LISTEN_CTL_DEFAULT_ADDR_STRING) {valid_socket_addr}
"The listen address for the Control Gateway. If not specified, the value will \
be taken from the HAB_LISTEN_CTL environment variable if defined. [default: 127.0.0.1:9632]")
(@arg ORGANIZATION: --org +takes_value
Expand All @@ -890,33 +888,6 @@ pub fn sub_sup_run() -> App<'static, 'static> {
(ex: hab sup run --ring myring)")
(@arg RING_KEY: --("ring-key") env(RING_KEY_ENVVAR) conflicts_with("RING") +hidden
"The contents of the ring key when running with wire encryption. \
(about: "Run the Habitat Supervisor")
// 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.")
(@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
"Disable the HTTP Gateway completely [default: false]")
(@arg LISTEN_CTL: --("listen-ctl") env(ListenCtlAddr::ENVVAR) default_value(&LISTEN_CTL_DEFAULT_ADDR_STRING) {valid_socket_addr}
"The listen address for the Control Gateway. If not specified, the value will \
be taken from the HAB_LISTEN_CTL environment variable if defined. [default: 127.0.0.1:9632]")
(@arg ORGANIZATION: --org +takes_value
"The organization that the Supervisor and its subsequent services are part of.")
(@arg PEER: --peer +takes_value +multiple
"The listen address of one or more initial peers (IP[:PORT])")
(@arg PERMANENT_PEER: --("permanent-peer") -I "If this Supervisor is a permanent peer")
(@arg PEER_WATCH_FILE: --("peer-watch-file") +takes_value conflicts_with[peer]
"Watch this file for connecting to the ring"
)
(@arg RING: --ring -r env(RING_ENVVAR) conflicts_with("RING_KEY")
"The name of the ring used by the Supervisor when running with wire encryption. \
(ex: hab sup run --ring myring)")
(@arg RING_KEY: --("ring-key") env(RING_KEY_ENVVAR) conflicts_with("RING") +hidden
"The contents of the ring key when running with wire encryption. \
(Note: This option is explicitly undocumented and for testing purposes only. Do not use it in a production system. Use the corresponding environment variable instead.)
(ex: hab sup run --ring-key 'SYM-SEC-1 \
foo-20181113185935 \
Expand Down

0 comments on commit ba52b88

Please sign in to comment.