Skip to content

Commit

Permalink
Configuration file injection
Browse files Browse the repository at this point in the history
* Adds a new 'Inject' protocol for injecting rumors without being a
  member
* Adds a new config_file and config_file_list, for tracking
  configuration files shared by gossip
* Writes out any files for our service group, and runs a new file_updated
  hook when they are updated
* Changes the hook display output to use the hook name as the preamble
* If the file is named 'gossip.toml', we wire it through to the service
  configuration, re-ender our hooks, and execute the reconfigure hook.
  • Loading branch information
fnichol authored and adamhjk committed Mar 26, 2016
1 parent 1f6c0f2 commit 6c37bef
Show file tree
Hide file tree
Showing 15 changed files with 773 additions and 37 deletions.
72 changes: 72 additions & 0 deletions components/bldr/src/command/inject.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright:: Copyright (c) 2015-2016 Chef Software, Inc.
//
// The terms of the Evaluation Agreement (Bldr) between Chef Software Inc. and the party accessing
// 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::path::Path;
use std::thread;
use std::time::Duration;

use config::Config;
use config_file::{ConfigFile, ServiceGroup};
use error::BldrResult;
use gossip::client::Client;
use gossip::rumor::{Rumor, RumorList};

static LOGKEY: &'static str = "IJ";

pub fn inject(config: &Config) -> BldrResult<()> {
let sg = try!(ServiceGroup::from(&config.service_group()));
let vn = *config.version_number();
let cf = try!(ConfigFile::from_file(sg, Path::new(&config.file_path()), vn));
let mut rumor_list = RumorList::new();
let rumor = Rumor::config_file(cf);
rumor_list.add_rumor(rumor);
initial_peers(config.gossip_peer(), &rumor_list);
Ok(())
}

pub fn initial_peers(peer_listeners: &[String], rumor_list: &RumorList) -> BldrResult<()> {
let fail_after = 10;
let mut count = 0;

if peer_listeners.len() > 0 {
while count < fail_after {
if try_peers(peer_listeners, rumor_list) {
return Ok(());
} else {
count = count + 1;
outputln!("Could not connect to any initial peers; attempt {} of {}.",
count,
fail_after);
}
}
}
Ok(())
}

fn try_peers(peer_listeners: &[String], rumor_list: &RumorList) -> bool {
let mut initialized = false;
for to in peer_listeners {
outputln!("Joining gossip peer at {}", to);
let mut c = match Client::new(&to[..]) {
Ok(c) => c,
Err(e) => {
debug!("Error creating gossip client - {:?}", e);
outputln!("Failed to create a gossip client for {}", to);
continue;
}
};

match c.inject(rumor_list.clone()) {
Ok(_) => outputln!("Rumors injected at {}", to),
Err(e) => {
outputln!("Failed to ping {:?}: {:?}", to, e);
continue;
}
}
initialized = true;
}
initialized
}
1 change: 1 addition & 0 deletions components/bldr/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ pub mod start;
pub mod key;
pub mod upload;
pub mod configure;
pub mod inject;
38 changes: 38 additions & 0 deletions components/bldr/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum Command {
Config,
Start,
ImportKey,
InjectConfigFile,
ExportKey,
UploadDepotKey,
DownloadDepotKey,
Expand Down Expand Up @@ -75,6 +76,7 @@ impl FromStr for Command {
"generate-service-key" => Ok(Command::GenerateServiceKey),
"generate-user-key" => Ok(Command::GenerateUserKey),
"import-key" => Ok(Command::ImportKey),
"inject-config-file" => Ok(Command::InjectConfigFile),
"install" => Ok(Command::Install),
"list-keys" => Ok(Command::ListKeys),
"sh" => Ok(Command::Shell),
Expand Down Expand Up @@ -116,6 +118,9 @@ pub struct Config {
gossip_peer: Vec<String>,
gossip_permanent: bool,
update_strategy: UpdateStrategy,
service_group: String,
file_path: String,
version_number: u64,
}

impl Config {
Expand Down Expand Up @@ -320,6 +325,39 @@ impl Config {
&self.gossip_peer
}

/// Set the service group
pub fn set_service_group(&mut self, sg: String) -> &mut Config {
self.service_group = sg;
self
}

/// Return the service group
pub fn service_group(&self) -> &str {
&self.service_group
}

/// Set the file path
pub fn set_file_path(&mut self, fp: String) -> &mut Config {
self.file_path = fp;
self
}

/// Return the file path
pub fn file_path(&self) -> &str {
&self.file_path
}

/// Set the version number
pub fn set_version_number(&mut self, vn: u64) -> &mut Config {
self.version_number = vn;
self
}

/// Return the version number
pub fn version_number(&self) -> &u64 {
&self.version_number
}

pub fn set_gossip_peer(&mut self, mut gp: Vec<String>) -> &mut Config {
for p in gp.iter_mut() {
if p.find(':').is_none() {
Expand Down
Loading

1 comment on commit 6c37bef

@chef-delivery
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.