From cdc037ed47e6d2a2816601e55e3966c4240e432a Mon Sep 17 00:00:00 2001 From: elijah Date: Sat, 5 Apr 2014 13:58:37 -0700 Subject: more graceful handling of error for `leap node add` (closes https://leap.se/code/issues/3725) --- lib/leap_cli.rb | 1 + lib/leap_cli/commands/node.rb | 26 ++++++++++++++++---------- lib/leap_cli/config/manager.rb | 18 +++++++++++++----- lib/leap_cli/exceptions.rb | 11 +++++++++++ 4 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 lib/leap_cli/exceptions.rb diff --git a/lib/leap_cli.rb b/lib/leap_cli.rb index ed5932a..70727b7 100644 --- a/lib/leap_cli.rb +++ b/lib/leap_cli.rb @@ -7,6 +7,7 @@ require 'leap/platform.rb' require 'leap_cli/version.rb' require 'leap_cli/constants.rb' require 'leap_cli/requirements.rb' +require 'leap_cli/exceptions.rb' require 'leap_cli/leapfile.rb' require 'core_ext/hash' diff --git a/lib/leap_cli/commands/node.rb b/lib/leap_cli/commands/node.rb index 5f5c4b8..304d86b 100644 --- a/lib/leap_cli/commands/node.rb +++ b/lib/leap_cli/commands/node.rb @@ -32,12 +32,14 @@ module LeapCli; module Commands end seed_node_data(node, args[1..-1]) validate_ip_address(node) - - # write the file - write_file! [:node_config, name], node.dump_json + "\n" - node['name'] = name - if file_exists? :ca_cert, :ca_key - generate_cert_for_node(manager.reload_node(node)) + begin + write_file! [:node_config, name], node.dump_json + "\n" + node['name'] = name + if file_exists? :ca_cert, :ca_key + generate_cert_for_node(manager.reload_node!(node)) + end + rescue LeapCli::ConfigError => exc + remove_node_files(name) end end end @@ -102,9 +104,7 @@ module LeapCli; module Commands node.command :rm do |rm| rm.action do |global_options,options,args| node = get_node_from_args(args) - (Leap::Platform.node_files + [:node_files_dir]).each do |path| - remove_file! [path, node.name] - end + remove_node_files(node.name) if node.vagrant? vagrant_command("destroy --force", [node.name]) end @@ -236,8 +236,14 @@ module LeapCli; module Commands end end + def remove_node_files(node_name) + (Leap::Platform.node_files + [:node_files_dir]).each do |path| + remove_file! [path, node_name] + end + end + # - # conversations: + # conversions: # # "x,y,z" => ["x","y","z"] # diff --git a/lib/leap_cli/config/manager.rb b/lib/leap_cli/config/manager.rb index 5076b63..7969d40 100644 --- a/lib/leap_cli/config/manager.rb +++ b/lib/leap_cli/config/manager.rb @@ -226,8 +226,8 @@ module LeapCli nodes.each_node &block end - def reload_node(node) - @nodes[node.name] = apply_inheritance(node) + def reload_node!(node) + @nodes[node.name] = apply_inheritance!(node) end private @@ -307,7 +307,7 @@ module LeapCli # # makes a node inherit options from appropriate the common, service, and tag json files. # - def apply_inheritance(node) + def apply_inheritance(node, throw_exceptions=false) new_node = Config::Node.new(self) name = node.name @@ -319,7 +319,9 @@ module LeapCli node['services'].to_a.each do |node_service| service = @services[node_service] if service.nil? - log 0, :error, 'in node "%s": the service "%s" does not exist.' % [node['name'], node_service] + msg = 'in node "%s": the service "%s" does not exist.' % [node['name'], node_service] + log 0, :error, msg + raise LeapCli::ConfigError.new(node, "error " + msg) if throw_exceptions else new_node.deep_merge!(service) service.node_list.add(name, new_node) @@ -335,7 +337,9 @@ module LeapCli node['tags'].to_a.each do |node_tag| tag = @tags[node_tag] if tag.nil? - log 0, :error, 'in node "%s": the tag "%s" does not exist.' % [node['name'], node_tag] + msg = 'in node "%s": the tag "%s" does not exist.' % [node['name'], node_tag] + log 0, :error, msg + raise LeapCli::ConfigError.new(node, "error " + msg) if throw_exceptions else new_node.deep_merge!(tag) tag.node_list.add(name, new_node) @@ -348,6 +352,10 @@ module LeapCli return new_node end + def apply_inheritance!(node) + apply_inheritance(node, true) + end + def remove_disabled_nodes @disabled_nodes = Config::ObjectList.new @nodes.each do |name, node| diff --git a/lib/leap_cli/exceptions.rb b/lib/leap_cli/exceptions.rb new file mode 100644 index 0000000..cd27f14 --- /dev/null +++ b/lib/leap_cli/exceptions.rb @@ -0,0 +1,11 @@ +module LeapCli + + class ConfigError < StandardError + attr_accessor :node + def initialize(node, msg) + @node = node + super(msg) + end + end + +end \ No newline at end of file -- cgit v1.2.3