]> gitweb.fluxo.info Git - leap/leap_cli.git/commitdiff
auto remove files/nodes/x when nodes/x.json doesn't exist anymore.
authorelijah <elijah@riseup.net>
Thu, 15 Nov 2012 09:54:31 +0000 (01:54 -0800)
committerelijah <elijah@riseup.net>
Thu, 15 Nov 2012 09:54:31 +0000 (01:54 -0800)
lib/leap_cli/config/manager.rb
lib/leap_cli/path.rb
lib/leap_cli/util.rb

index e90b5898794a5e9cf92345c076ebd80d7c624bdd..3d09e091f62a82f4904b272a4f5e566f949c6435 100644 (file)
@@ -53,18 +53,24 @@ module LeapCli
       #
       # save compiled hiera .yaml files
       #
-      def export_nodes(destination_directory = nil)
-        dir = destination_directory || Path.named_path(:hiera_dir, @provider_dir)
-        existing_files = Dir.glob(dir + '/*.yaml')
+      def export_nodes
+        existing_hiera = Dir.glob(Path.named_path([:hiera, '*'], @provider_dir))
+        existing_files = Dir.glob(Path.named_path([:node_files_dir, '*'], @provider_dir))
+        updated_hiera = []
         updated_files = []
-        @nodes.each do |name, node|
-          filepath = "#{dir}/#{name}.yaml"
+        self.each_node do |node|
+          filepath = Path.named_path([:node_files_dir, node.name], @provider_dir)
           updated_files << filepath
-          Util::write_file!(filepath, node.dump)
+          hierapath = Path.named_path([:hiera, node.name], @provider_dir)
+          updated_hiera << hierapath
+          Util::write_file!(hierapath, node.dump)
         end
-        (existing_files - updated_files).each do |filepath|
+        (existing_hiera - updated_hiera).each do |filepath|
           Util::remove_file!(filepath)
         end
+        (existing_files - updated_files).each do |filepath|
+          Util::remove_directory!(filepath)
+        end
       end
 
       def export_secrets(destination_file = nil)
index a783a918109cf3e69bf3a1bf0a1325ef736598d5..bf4c89f394512866086d7b0205a96ed4db620a20 100644 (file)
@@ -9,6 +9,7 @@ module LeapCli; module Path
     :nodes_dir        => 'nodes',
     :services_dir     => 'services',
     :tags_dir         => 'tags',
+    :node_files_dir   => 'files/nodes/#{arg}',
 
     # input config files
     :common_config    => 'common.json',
index 6b62be5c3f612e8761e40359dc60711e4106d952..9b0489412fe5bd84295b6fcbe04b574e1ed61f8a 100644 (file)
@@ -1,5 +1,6 @@
 require 'digest/md5'
 require 'paint'
+require 'fileutils'
 
 module LeapCli
 
@@ -197,6 +198,17 @@ module LeapCli
       end
     end
 
+    def remove_directory!(filepath)
+      filepath = Path.named_path(filepath)
+      if filepath !~ /^#{Regexp.escape(Path.provider)}/ || filepath =~ /\.\./
+        raise "sanity check on rm -r did not pass for #{filepath}"
+      end
+      if File.directory?(filepath)
+        FileUtils.rm_r(filepath)
+        log :removed, filepath
+      end
+    end
+
     def write_file!(filepath, contents)
       filepath = Path.named_path(filepath)
       ensure_dir File.dirname(filepath)