/
main.rs
396 lines (364 loc) · 13.8 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright (c) 2016 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.
#[macro_use]
extern crate habitat_sup as sup;
extern crate habitat_core as hcore;
extern crate rustc_serialize;
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate ansi_term;
extern crate libc;
#[macro_use]
extern crate clap;
use std::path::Path;
use std::process;
use std::result;
use std::str::FromStr;
use ansi_term::Colour::Yellow;
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
use hcore::env as henv;
use hcore::fs;
use hcore::crypto::{default_cache_key_path, SymKey};
use hcore::crypto::init as crypto_init;
use hcore::package::{PackageArchive, PackageIdent};
use hcore::url::{DEFAULT_DEPOT_URL, DEPOT_URL_ENVVAR};
use sup::config::{Command, Config, UpdateStrategy};
use sup::error::{Error, Result, SupError};
use sup::command::*;
use sup::topology::Topology;
use sup::util::parse_ip_port_with_defaults;
use sup::util::path::busybox_paths;
use sup::util::sys::ip;
/// Our output key
static LOGKEY: &'static str = "MN";
/// The version number
const VERSION: &'static str = include_str!(concat!(env!("OUT_DIR"), "/VERSION"));
/// CLI defaults
static DEFAULT_GROUP: &'static str = "default";
static DEFAULT_HTTP_LISTEN_IP: &'static str = "0.0.0.0";
static DEFAULT_HTTP_LISTEN_PORT: u16 = 9631;
const DEFAULT_GOSSIP_LISTEN_PORT: u16 = 9634;
static RING_ENVVAR: &'static str = "HAB_RING";
static RING_KEY_ENVVAR: &'static str = "HAB_RING_KEY";
/// Creates a [Config](config/struct.Config.html) from global args
/// and subcommand args.
fn config_from_args(subcommand: &str, sub_args: &ArgMatches) -> Result<Config> {
let mut config = Config::new();
let command = try!(Command::from_str(subcommand));
config.set_command(command);
if let Some(ref strategy) = sub_args.value_of("strategy") {
config.set_update_strategy(UpdateStrategy::from_str(strategy));
}
if let Some(ref archive) = sub_args.value_of("archive") {
config.set_archive(archive.to_string());
}
if let Some(ref ident_or_artifact) = sub_args.value_of("pkg_ident_or_artifact") {
if Path::new(ident_or_artifact).is_file() {
let ident = try!(PackageArchive::new(Path::new(ident_or_artifact)).ident());
config.set_package(ident);
config.set_local_artifact(ident_or_artifact.to_string());
} else {
let ident = try!(PackageIdent::from_str(ident_or_artifact));
config.set_package(ident);
}
}
if let Some(key) = sub_args.value_of("key") {
config.set_key(key.to_string());
}
if let Some(email) = sub_args.value_of("email") {
config.set_email(email.to_string());
}
if let Some(user) = sub_args.value_of("user") {
config.set_user_key(user.to_string());
}
if let Some(service) = sub_args.value_of("service") {
config.set_service_key(service.to_string());
}
if let Some(infile) = sub_args.value_of("infile") {
config.set_infile(infile.to_string());
}
if let Some(outfile) = sub_args.value_of("outfile") {
config.set_outfile(outfile.to_string());
}
if let Some(topology) = sub_args.value_of("topology") {
match topology.as_ref() {
"standalone" => {
config.set_topology(Topology::Standalone);
}
"leader" => {
config.set_topology(Topology::Leader);
}
"initializer" => {
config.set_topology(Topology::Initializer);
}
t => return Err(sup_error!(Error::UnknownTopology(String::from(t)))),
}
}
if sub_args.value_of("expire-days").is_some() {
let ed = value_t!(sub_args.value_of("expire-days"), u16).unwrap_or_else(|e| e.exit());
config.set_expire_days(ed);
}
let env_or_default = henv::var(DEPOT_URL_ENVVAR).unwrap_or(DEFAULT_DEPOT_URL.to_string());
let url = sub_args.value_of("url").unwrap_or(&env_or_default);
config.set_url(url.to_string());
config.set_group(sub_args.value_of("group").unwrap_or(DEFAULT_GROUP).to_string());
let bindings = match sub_args.values_of("bind") {
Some(bind) => bind.map(|s| s.to_string()).collect(),
None => vec![],
};
config.set_bind(bindings);
config.set_path(sub_args.value_of("path")
.unwrap_or(fs::svc_path(sup::PROGRAM_NAME.as_str())
.join("data")
.to_string_lossy()
.as_ref())
.to_string());
let mut env_path = String::new();
for path in try!(busybox_paths()) {
if env_path.len() > 0 {
env_path.push(':');
}
env_path.push_str(path.to_string_lossy().as_ref());
}
let default_gossip_ip = try!(ip(Some(&env_path)));
let (gossip_ip, gossip_port) = try!(parse_ip_port_with_defaults(
sub_args.value_of("listen-peer"),
&default_gossip_ip,
DEFAULT_GOSSIP_LISTEN_PORT));
debug!("Gossip IP = {}", &gossip_ip);
debug!("Gossip port = {}", &gossip_port);
config.set_gossip_listen_ip(gossip_ip);
config.set_gossip_listen_port(gossip_port);
let (sidecar_ip, sidecar_port) = try!(parse_ip_port_with_defaults(
sub_args.value_of("listen-http"),
DEFAULT_HTTP_LISTEN_IP,
DEFAULT_HTTP_LISTEN_PORT));
debug!("HTTP IP = {}", &sidecar_ip);
debug!("HTTP port = {}", &sidecar_port);
config.set_http_listen_ip(sidecar_ip);
config.set_http_listen_port(sidecar_port);
let gossip_peers = match sub_args.values_of("peer") {
Some(gp) => gp.map(|s| s.to_string()).collect(),
None => vec![],
};
config.set_gossip_peer(gossip_peers);
if sub_args.is_present("permanent-peer") {
config.set_gossip_permanent(true);
}
if let Some(sg) = sub_args.value_of("service-group") {
config.set_service_group(sg.to_string());
}
if let Some(fp) = sub_args.value_of("file-path") {
config.set_file_path(fp.to_string());
}
config.set_version_number(value_t!(sub_args, "version-number", u64).unwrap_or(0));
let ring = match sub_args.value_of("ring") {
Some(val) => Some(try!(SymKey::get_latest_pair_for(&val, &default_cache_key_path(None)))),
None => {
match henv::var(RING_KEY_ENVVAR) {
Ok(val) => {
let (key, _) = try!(SymKey::write_file_from_str(&val,
&default_cache_key_path(None)));
Some(key)
}
Err(_) => {
match henv::var(RING_ENVVAR) {
Ok(val) => {
Some(try!(SymKey::get_latest_pair_for(&val,
&default_cache_key_path(None))))
}
Err(_) => None,
}
}
}
}
};
if let Some(ring) = ring {
config.set_ring(ring.name_with_rev());
}
if sub_args.is_present("verbose") {
sup::output::set_verbose(true);
}
if sub_args.is_present("no-color") {
sup::output::set_no_color(true);
}
if let Some(org) = sub_args.value_of("organization") {
config.set_organization(org.to_string());
}
debug!("Config:\n{:?}", config);
Ok(config)
}
type Handler = fn(&Config) -> result::Result<(), sup::error::SupError>;
/// The entrypoint for the Supervisor.
///
/// * Set up the logger
/// * Pull in the arguments from the Command Line, push through clap
/// * Dispatch to a function that handles that action called
/// * Exit cleanly, or if we return an `Error`, call `exit_with(E, 1)`
#[allow(dead_code)]
fn main() {
env_logger::init().unwrap();
crypto_init();
let arg_url = || {
Arg::with_name("url")
.short("u")
.long("url")
.takes_value(true)
.help("Use the specified package depot url")
};
let arg_group = || {
Arg::with_name("group")
.long("group")
.takes_value(true)
.help("The service group; shared config and topology [default: default].")
};
let arg_org = || {
Arg::with_name("organization")
.long("org")
.takes_value(true)
.help("The organization that a service is part of")
};
let arg_strategy = || {
Arg::with_name("strategy")
.long("strategy")
.short("s")
.takes_value(true)
.possible_values(&["none", "at-once"])
.help("The update strategy; [default: none].")
};
let sub_start = SubCommand::with_name("start")
.about("Start a Habitat-supervised service from a package or artifact")
.aliases(&["st", "sta", "star"])
.arg(Arg::with_name("pkg_ident_or_artifact")
.index(1)
.required(true)
.help("A Habitat package identifier (ex: acme/redis) or a filepath to a Habitat \
Artifact (ex: /home/acme-redis-3.0.7-21120102031201-x86_64-linux.hart)"))
.arg(arg_url())
.arg(arg_group())
.arg(arg_org())
.arg(arg_strategy())
.arg(Arg::with_name("topology")
.short("t")
.long("topology")
.value_name("topology")
.help("Service topology"))
.arg(Arg::with_name("bind")
.long("bind")
.value_name("bind")
.multiple(true)
.help("One or more service groups to bind to a configuration"))
.arg(Arg::with_name("ring")
.short("r")
.long("ring")
.value_name("ring")
.help("Ring key name"))
.arg(Arg::with_name("peer")
.long("peer")
.value_name("ip:port")
.multiple(true)
.help("The listen address of an initial peer"))
.arg(Arg::with_name("listen-peer")
.long("listen-peer")
.value_name("ip:port")
.help("The listen address [default: ip_with_default_route:9634]"))
.arg(Arg::with_name("listen-http")
.long("listen-http")
.value_name("ip:port")
.help("The HTTP API listen address [default: 0.0.0.0:9631]"))
.arg(Arg::with_name("permanent-peer")
.short("I")
.long("permanent-peer")
.help("If this service is a permanent peer"));
let sub_bash = SubCommand::with_name("bash")
.about("Start an interactive shell (bash)")
.aliases(&["b", "ba", "bas"]);
let sub_sh = SubCommand::with_name("sh").about("Start an interactive shell (sh)");
let sub_config = SubCommand::with_name("config")
.about("Print the default.toml for a given package")
.aliases(&["c", "co", "con", "conf", "confi"])
.arg(Arg::with_name("package")
.index(1)
.required(true)
.help("Name of package"));
let args = App::new(sup::PROGRAM_NAME.as_str())
.version(VERSION)
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::SubcommandRequiredElseHelp)
.arg(Arg::with_name("verbose")
.short("v")
.global(true)
.help("Verbose output; shows line numbers"))
.arg(Arg::with_name("no-color")
.long("no-color")
.global(true)
.help("Turn ANSI color off :("))
.subcommand(sub_start)
.subcommand(sub_bash)
.subcommand(sub_sh)
.subcommand(sub_config);
let matches = args.get_matches();
debug!("clap matches {:?}", matches);
let subcommand_name = matches.subcommand_name().unwrap();
let subcommand_matches = matches.subcommand_matches(subcommand_name).unwrap();
debug!("subcommand name {:?}", &subcommand_name);
debug!("Subcommand matches {:?}", &subcommand_matches);
let config = match config_from_args(subcommand_name, &subcommand_matches) {
Ok(config) => config,
Err(e) => return exit_with(e, 1),
};
let result = match config.command() {
Command::ShellBash => shell_bash(&config),
Command::ShellSh => shell_sh(&config),
Command::Config => configure(&config),
Command::Start => start(&config),
};
match result {
Ok(_) => std::process::exit(0),
Err(e) => exit_with(e, 1),
}
}
/// Exit with an error message and the right status code
#[allow(dead_code)]
fn exit_with(e: SupError, code: i32) {
println!("{}", e.to_string());
process::exit(code)
}
/// Start a sh shell
#[allow(dead_code)]
fn shell_sh(_config: &Config) -> Result<()> {
shell::sh()
}
/// Start a bash shell
#[allow(dead_code)]
fn shell_bash(_config: &Config) -> Result<()> {
shell::bash()
}
/// Show the configuration options for a service
#[allow(dead_code)]
fn configure(config: &Config) -> Result<()> {
try!(configure::display(config));
Ok(())
}
/// Start a service
#[allow(dead_code)]
fn start(config: &Config) -> Result<()> {
outputln!("Starting {}",
Yellow.bold().paint(config.package().to_string()));
try!(start::package(config));
outputln!("Finished with {}",
Yellow.bold().paint(config.package().to_string()));
Ok(())
}