]> gitweb.fluxo.info Git - leap/leap_cli.git/commitdiff
more graceful handling of error for `leap node add` (closes https://leap.se/code...
authorelijah <elijah@riseup.net>
Sat, 5 Apr 2014 20:58:37 +0000 (13:58 -0700)
committerelijah <elijah@riseup.net>
Sat, 5 Apr 2014 20:58:37 +0000 (13:58 -0700)
lib/leap_cli.rb
lib/leap_cli/commands/node.rb
lib/leap_cli/config/manager.rb
lib/leap_cli/exceptions.rb [new file with mode: 0644]

index ed5932a24fbbce6a3889c4114d2fcd4b8bfd0055..70727b72903c2dad7d61441607e8c6e95f3e6f43 100644 (file)
@@ -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'
index 5f5c4b800271632fee02088f0f995bab9d9e0654..304d86bfe803d022d943f8a59540f394177b3d5d 100644 (file)
@@ -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"]
   #
index 5076b6323d967ce0df9694d15ccc3e9f6a3a262c..7969d40c229d84676a192363c77be8e6033d1db7 100644 (file)
@@ -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 (file)
index 0000000..cd27f14
--- /dev/null
@@ -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