Skip to content

Commit

Permalink
Added is_file_root flag
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmcc committed Jun 11, 2014
1 parent cbdf386 commit efcfd0d
Showing 1 changed file with 53 additions and 17 deletions.
70 changes: 53 additions & 17 deletions lib/kitchen/provisioner/salt_solo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# limitations under the License.

require 'kitchen/provisioner/base'
require 'find'
require 'fileutils'
require 'yaml'

module Kitchen
Expand Down Expand Up @@ -51,6 +53,9 @@ class SaltSolo < Base
default_config :state_top, {}
default_config :state_top_from_file, false
default_config :salt_run_highstate, true
default_config :salt_copy_filter, ['.git', '.svn', '.kitchen']
default_config :is_file_root, false


# salt-call version that supports the undocumented --retcode-passthrough command
RETCODE_VERSION = '0.17.5'
Expand Down Expand Up @@ -146,7 +151,7 @@ def create_sandbox
prepare_state_top
prepare_pillars
prepare_grains
if config[:state_collection]
if config[:state_collection] || config[:is_file_root]
prepare_state_collection
else
prepare_formula
Expand Down Expand Up @@ -203,7 +208,8 @@ def prepare_data

tmpdata_dir = File.join(sandbox_path, "data")
FileUtils.mkdir_p(tmpdata_dir)
FileUtils.cp_r(Dir.glob("#{config[:data_path]}/*"), tmpdata_dir)
#FileUtils.cp_r(Dir.glob("#{config[:data_path]}/*"), tmpdata_dir)
cp_r_with_filter(config[:data_path], tmpdata_dir, config[:salt_copy_filter])
end

def prepare_minion
Expand Down Expand Up @@ -270,17 +276,9 @@ def prepare_pillars
info("Preparing pillars into #{config[:salt_pillar_root]}")
debug("Pillars Hash: #{config[:pillars]}")

# load any pillars from disk, if specified
if !config[:'pillars-from-files'].nil?
external_pillars = unsymbolize(config[:'pillars-from-files'])
debug("external_pillars (unsymbolize): #{external_pillars}")
external_pillars.each do |key, value|
debug("loading externalpillar: #{key}, #{value}")
config[:pillars][key] = YAML.load(File.read(value))
end
end
return if config[:pillars].nil? && config[:'pillars-from-files'].nil?


return if config[:pillars].nil?

# we get a hash with all the keys converted to symbols, salt doesn't like this
# to convert all the keys back to strings again
Expand Down Expand Up @@ -308,6 +306,22 @@ def prepare_pillars
file.write(pillar)
end
end

# copy the pillars from files straight across, as YAML.load/to_yaml and
# munge multiline strings
if !config[:'pillars-from-files'].nil?
external_pillars = unsymbolize(config[:'pillars-from-files'])
debug("external_pillars (unsymbolize): #{external_pillars}")
external_pillars.each do |key, srcfile|
debug("Copying external pillar: #{key}, #{srcfile}")
# generate the filename
sandbox_pillar_path = File.join(sandbox_path, config[:salt_pillar_root], key)
# create the directory where the pillar file will go
FileUtils.mkdir_p(File.dirname(sandbox_pillar_path))
# copy the file across
FileUtils.copy srcfile, sandbox_pillar_path
end
end
end

def prepare_grains
Expand Down Expand Up @@ -341,7 +355,8 @@ def prepare_formula

formula_dir = File.join(sandbox_path, config[:salt_file_root], config[:formula])
FileUtils.mkdir_p(formula_dir)
FileUtils.cp_r(Dir.glob(File.join(config[:kitchen_root], config[:formula], "*")), formula_dir)
#FileUtils.cp_r(Dir.glob(File.join(config[:kitchen_root], config[:formula], "*")), formula_dir)
cp_r_with_filter(File.join(config[:kitchen_root], config[:formula]), formula_dir, config[:salt_copy_filter])

# copy across the _modules etc directories for python implementation
['_modules', '_states', '_grains', '_renderers', '_returners'].each do |extrapath|
Expand All @@ -351,7 +366,8 @@ def prepare_formula
debug("prepare_formula: #{src} exists, copying..")
extrapath_dir = File.join(sandbox_path, config[:salt_file_root], extrapath)
FileUtils.mkdir_p(extrapath_dir)
FileUtils.cp_r(Dir.glob(File.join(src, "*")), extrapath_dir)
#FileUtils.cp_r(Dir.glob(File.join(src, "*")), extrapath_dir)
cp_r_with_filter(src, extrapath_dir, config[:salt_copy_filter])
else
debug("prepare_formula: #{src} doesn't exist, skipping.")
end
Expand All @@ -363,8 +379,8 @@ def prepare_state_collection
debug("Using config #{config}")

if config[:collection_name].nil? and config[:formula].nil?
error("neither collection_name or formula have been set!")
exit(2)
info("neither collection_name or formula have been set, assuming this is a pre-built collection")
config[:collection_name] = ""
else
if config[:collection_name].nil?
debug("collection_name not set, using #{config[:formula]}")
Expand All @@ -377,9 +393,29 @@ def prepare_state_collection
debug("collection_name = #{config[:collection_name]}")
collection_dir = File.join(sandbox_path, config[:salt_file_root], config[:collection_name])
FileUtils.mkdir_p(collection_dir)
FileUtils.cp_r(Dir.glob(File.join(config[:kitchen_root], "*")), collection_dir)
cp_r_with_filter(config[:kitchen_root], collection_dir, config[:salt_copy_filter])

end

def cp_r_with_filter(source_path, target_path, filter=[])
debug("cp_r_with_filter:source_path = #{source_path}")
debug("cp_r_with_filter:target_path = #{target_path}")
debug("cp_r_with_filter:filter = #{filter}")

Array(source_path).each do |source_path|
Find.find(source_path) do |source|
target = source.sub(/^#{source_path}/, target_path)
debug("cp_r_with_filter:source = #{source}")
debug("cp_r_with_filter:target = #{target}")
if File.directory? source
Find.prune if filter.include?(File.basename(source))
FileUtils.mkdir target unless File.exists? target
else
FileUtils.copy source, target
end
end
end
end
end
end
end

0 comments on commit efcfd0d

Please sign in to comment.