Skip to content

Commit

Permalink
Configure builder servers from file
Browse files Browse the repository at this point in the history
* sessionsrv, builder-api, and vault all configurable by file
* Retrieve detailed version from plan for builder services
* Move plans to a more appropriate location for builder services
* Remove config template from builder services plan's
  • Loading branch information
reset committed Apr 28, 2016
1 parent 366e912 commit d23c073
Show file tree
Hide file tree
Showing 54 changed files with 1,021 additions and 342 deletions.
62 changes: 62 additions & 0 deletions components/builder-api/Cargo.lock

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

5 changes: 5 additions & 0 deletions components/builder-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "habitat_builder_api"
version = "0.1.0"
authors = ["Adam Jacob <[email protected]>", "Jamie Winsor <[email protected]>", "Fletcher Nichol <[email protected]>", "Joshua Timberman <[email protected]>", "Dave Parfitt <[email protected]>"]
description = "Habitat-Builder HTTP API gateway"
build = "build.rs"

[[bin]]
name = "bldr-api"
Expand All @@ -16,6 +17,7 @@ protobuf = "*"
redis = "*"
router = "*"
rustc-serialize = "*"
toml = "*"
urlencoded = "*"

[dependencies.clap]
Expand All @@ -27,6 +29,9 @@ features = [ "suggestions", "color", "unstable" ]
git = "https://github.com/reset/rust-zmq.git"
branch = "build-rs"

[dependencies.habitat_core]
path = "../core"

[dependencies.habitat_builder_protocol]
path = "../builder-protocol"

Expand Down
10 changes: 10 additions & 0 deletions components/builder-api/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;

fn main() {
let version = env::var("PLAN_VERSION").unwrap_or(env::var("CARGO_PKG_VERSION").unwrap());
let mut f = File::create(Path::new(&env::var("OUT_DIR").unwrap()).join("VERSION")).unwrap();
f.write_all(version.as_bytes()).unwrap();
}
3 changes: 3 additions & 0 deletions components/builder-api/plan/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
http_addr = "127.0.0.1:9636"
sessionsrv_addr = "127.0.0.1:5560"
vaultsrv_addr = "127.0.0.1:5561"
59 changes: 59 additions & 0 deletions components/builder-api/plan/plan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
pkg_name=hab-builder-api
pkg_origin=core
pkg_version=0.4.0
pkg_maintainer="Jamie Winsor <[email protected]>"
pkg_license=('apachev2')
pkg_source=nosuchfile.tar.gz
pkg_bin_dirs=(bin)
pkg_deps=(core/glibc core/openssl core/gcc-libs core/zeromq core/libsodium core/libarchive)
pkg_build_deps=(core/protobuf core/protobuf-rust core/coreutils core/cacerts core/rust core/gcc core/pkg-config)
srv_bin="bldr-api"
pkg_service_run="bin/$srv_bin start -c ${pkg_svc_path}/config.toml"

do_build() {
# Used by the `build.rs` program to set the version of the binaries
export PLAN_VERSION="${pkg_version}/${pkg_release}"
build_line "Setting PLAN_VERSION=$PLAN_VERSION"

# Used by Cargo to fetch registries/crates/etc.
export SSL_CERT_FILE=$(pkg_path_for cacerts)/ssl/cert.pem
build_line "Setting SSL_CERT_FILE=$SSL_CERT_FILE"

export rustc_target="debug"
build_line "Setting rustc_target=$rustc_target"

export LIBARCHIVE_LIB_DIR=$(pkg_path_for libarchive)/lib
export LIBARCHIVE_INCLUDE_DIR=$(pkg_path_for libarchive)/include
export OPENSSL_LIB_DIR=$(pkg_path_for openssl)/lib
export OPENSSL_INCLUDE_DIR=$(pkg_path_for openssl)/include
export PROTOBUF_PREFIX=$(pkg_path_for protobuf)
export SODIUM_LIB_DIR=$(pkg_path_for libsodium)/lib
export LIBZMQ_PREFIX=$(pkg_path_for zeromq)

pushd $PLAN_CONTEXT/.. > /dev/null
cargo clean --target=$rustc_target --verbose
cargo build \
-j $(nproc) \
--verbose
popd > /dev/null
}

do_install() {
install -v -D $PLAN_CONTEXT/../target/$rustc_target/$srv_bin $pkg_prefix/bin/$srv_bin
}

do_download() {
return 0
}

do_verify() {
return 0
}

do_unpack() {
return 0
}

do_prepare() {
return 0
}
21 changes: 17 additions & 4 deletions components/builder-api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@

use std::net;

use core::config::{ConfigFile, ParseInto};
use toml;

use error::{Error, Result};

pub struct Config {
pub http_addr: net::SocketAddrV4,
sessionsrv_addr: net::SocketAddrV4,
vaultsrv_addr: net::SocketAddrV4,
}

impl Config {
pub fn new() -> Self {
Config::default()
}

pub fn sessionsrv_addr(&self) -> String {
format!("tcp://{}:{}",
self.sessionsrv_addr.ip(),
Expand Down Expand Up @@ -44,3 +45,15 @@ impl Default for Config {
}
}
}

impl ConfigFile for Config {
type Error = Error;

fn from_toml(toml: toml::Table) -> Result<Self> {
let mut cfg = Config::default();
try!(toml.parse_into("cfg.http_addr", &mut cfg.http_addr));
try!(toml.parse_into("cfg.sessionsrv_addr", &mut cfg.sessionsrv_addr));
try!(toml.parse_into("cfg.vaultsrv_addr", &mut cfg.vaultsrv_addr));
Ok(cfg)
}
}
35 changes: 35 additions & 0 deletions components/builder-api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,57 @@
// this file ("Licensee") apply to Licensee's use of the Software until such time that the Software
// is made available under an open source license such as the Apache 2.0 License.

use std::error;
use std::fmt;
use std::io;
use std::result;

use core;
use protobuf;
use zmq;

#[derive(Debug)]
pub enum Error {
BadPort(String),
HabitatCore(core::Error),
IO(io::Error),
Protobuf(protobuf::ProtobufError),
Zmq(zmq::Error),
}

pub type Result<T> = result::Result<T, Error>;

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let msg = match *self {
Error::BadPort(ref e) => format!("{} is an invalid port. Valid range 1-65535.", e),
Error::HabitatCore(ref e) => format!("{}", e),
Error::IO(ref e) => format!("{}", e),
Error::Protobuf(ref e) => format!("{}", e),
Error::Zmq(ref e) => format!("{}", e),
};
write!(f, "{}", msg)
}
}

impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::BadPort(_) => "Received an invalid port or a number outside of the valid range.",
Error::HabitatCore(ref err) => err.description(),
Error::IO(ref err) => err.description(),
Error::Protobuf(ref err) => err.description(),
Error::Zmq(ref err) => err.description(),
}
}
}

impl From<core::Error> for Error {
fn from(err: core::Error) -> Error {
Error::HabitatCore(err)
}
}

impl From<protobuf::ProtobufError> for Error {
fn from(err: protobuf::ProtobufError) -> Error {
Error::Protobuf(err)
Expand Down
2 changes: 2 additions & 0 deletions components/builder-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// this file ("Licensee") apply to Licensee's use of the Software until such time that the Software
// is made available under an open source license such as the Apache 2.0 License.

extern crate habitat_core as core;
extern crate habitat_net as hab_net;
extern crate habitat_builder_protocol as protocol;
extern crate iron;
Expand All @@ -14,6 +15,7 @@ extern crate redis;
#[macro_use]
extern crate router;
extern crate rustc_serialize;
extern crate toml;
extern crate urlencoded;
extern crate zmq;

Expand Down
20 changes: 14 additions & 6 deletions components/builder-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
#[macro_use]
extern crate clap;
extern crate env_logger;
extern crate habitat_core as core;
extern crate habitat_builder_api as api;
#[macro_use]
extern crate log;

use std::process;
use std::str::FromStr;

use core::config::ConfigFile;

use api::{Config, Error, Result};

const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const VERSION: &'static str = include_str!(concat!(env!("OUT_DIR"), "/VERSION"));
const CFG_DEFAULT_PATH: &'static str = "/hab/svc/hab-builder-api/config.toml";

fn main() {
env_logger::init().unwrap();
Expand All @@ -35,20 +39,24 @@ fn main() {
fn app<'a, 'b>() -> clap::App<'a, 'b> {
clap_app!(BuilderApi =>
(version: VERSION)
(about: "Manage a Habitat builder-api")
(about: "Habitat builder-api")
(@setting VersionlessSubcommands)
(@setting SubcommandRequiredElseHelp)
(@arg config: -c --config +takes_value +global "Filepath to configuration file. [default: /hab/svc/hab-builder-api/config.toml]")
(@arg port: --port +takes_value +global "Listen port. [default: 9632]")
(@subcommand start =>
(about: "Run a Habitat builder-api")
(@arg port: --port +takes_value "Listen port. [default: 9632]")
(about: "Run the builder-api server")
)
)
}

fn config_from_args(matches: &clap::ArgMatches) -> Result<Config> {
let cmd = matches.subcommand_name().unwrap();
let args = matches.subcommand_matches(cmd).unwrap();
let mut config = Config::new();
let mut config = match args.value_of("config") {
Some(cfg_path) => try!(Config::from_file(cfg_path)),
None => Config::from_file(CFG_DEFAULT_PATH).unwrap_or(Config::default()),
};
if let Some(port) = args.value_of("port") {
if u16::from_str(port).map(|p| config.set_port(p)).is_err() {
return Err(Error::BadPort(port.to_string()));
Expand All @@ -58,7 +66,7 @@ fn config_from_args(matches: &clap::ArgMatches) -> Result<Config> {
}

fn exit_with(err: Error, code: i32) {
println!("{:?}", err);
println!("{}", err);
process::exit(code)
}

Expand Down
Loading

0 comments on commit d23c073

Please sign in to comment.