Skip to content

Commit

Permalink
Merge pull request #5950 from habitat-sh/peck/listen-ctl
Browse files Browse the repository at this point in the history
ListenCtlAddr type
  • Loading branch information
mpeck committed Jan 4, 2019
2 parents 0d156d3 + 5462e48 commit f3f0fad
Show file tree
Hide file tree
Showing 20 changed files with 292 additions and 130 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.

Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,27 @@
//! 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.

pub const GOSSIP_DEFAULT_IP: &'static str = "0.0.0.0";
use types::ListenCtlAddr;

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 =
{ ListenCtlAddr::default().to_string() };
}
10 changes: 10 additions & 0 deletions components/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::env;
use std::error;
use std::fmt;
use std::io;
use std::net;
use std::result;
use std::str;
use std::string;
Expand All @@ -41,6 +42,7 @@ pub enum Error {
HabitatCore(hcore::Error),
/// Occurs when making lower level IO calls.
IO(io::Error),
NetParseError(net::AddrParseError),
OfflineArtifactNotFound(PackageIdent),
OfflineOriginKeyNotFound(String),
OfflinePackageNotFound(PackageIdent),
Expand Down Expand Up @@ -75,6 +77,7 @@ impl fmt::Display for Error {
Error::FileNameError => format!("Failed to extract a filename"),
Error::HabitatCore(ref e) => format!("{}", e),
Error::IO(ref err) => format!("{}", err),
Error::NetParseError(ref err) => format!("{}", err),
Error::OfflineArtifactNotFound(ref ident) => {
format!("Cached artifact not found in offline mode: {}", ident)
}
Expand Down Expand Up @@ -119,6 +122,7 @@ impl error::Error for Error {
Error::FileNameError => "Failed to extract a filename from a path",
Error::HabitatCore(ref err) => err.description(),
Error::IO(ref err) => err.description(),
Error::NetParseError(_) => "Can't parse IP:port",
Error::OfflineArtifactNotFound(_) => "Cached artifact not found in offline mode",
Error::OfflineOriginKeyNotFound(_) => "Cached origin key not found in offline mode",
Error::OfflinePackageNotFound(_) => {
Expand Down Expand Up @@ -172,3 +176,9 @@ impl From<toml::ser::Error> for Error {
Error::TomlSerializeError(err)
}
}

impl From<net::AddrParseError> for Error {
fn from(err: net::AddrParseError) -> Self {
Error::NetParseError(err)
}
}
2 changes: 1 addition & 1 deletion components/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ extern crate winapi;

pub use self::error::{Error, Result};

pub mod cli_defaults;
pub mod command;
pub mod defaults;
pub mod error;
pub mod locked_env_var;
pub mod package_graph;
Expand Down
3 changes: 2 additions & 1 deletion components/common/src/types/env_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{env::VarError, str::FromStr};
use std::env::VarError;
use std::str::FromStr;

use hcore::env as henv;

Expand Down
76 changes: 76 additions & 0 deletions components/common/src/types/listen_ctl_addr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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.

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

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

#[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)))
}

pub fn ip(&self) -> IpAddr {
self.0.ip()
}

pub fn port(&self) -> u16 {
self.0.port()
}
}

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

impl EnvConfig for ListenCtlAddr {
const ENVVAR: &'static str = "HAB_LISTEN_CTL";
}

impl FromStr for ListenCtlAddr {
type Err = Error;

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

impl fmt::Display for ListenCtlAddr {
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
write!(f, "{}", self.0)
}
}

impl From<SocketAddr> for ListenCtlAddr {
fn from(socket_addr: SocketAddr) -> Self {
ListenCtlAddr(socket_addr)
}
}

impl AsRef<SocketAddr> for ListenCtlAddr {
fn as_ref(&self) -> &SocketAddr {
&self.0
}
}
2 changes: 2 additions & 0 deletions components/common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
// limitations under the License.

mod env_config;
mod listen_ctl_addr;

pub use self::env_config::EnvConfig;
pub use self::listen_ctl_addr::ListenCtlAddr;
9 changes: 5 additions & 4 deletions components/hab/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ use protocol;
use url::Url;

use command::studio;
use common::defaults::{
GOSSIP_DEFAULT_ADDR, GOSSIP_LISTEN_ADDRESS_ENVVAR, LISTEN_HTTP_ADDRESS_ENVVAR,
LISTEN_HTTP_DEFAULT_ADDR, RING_ENVVAR, RING_KEY_ENVVAR,
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::types::{EnvConfig, ListenCtlAddr};
use feat;

pub fn get() -> App<'static, 'static> {
Expand Down Expand Up @@ -871,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 Down

0 comments on commit f3f0fad

Please sign in to comment.