Class: Spiceweasel::DataBags

Inherits:
Object
  • Object
show all
Includes:
CommandHelper
Defined in:
lib/spiceweasel/data_bags.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandHelper

#create_command, #delete_command

Constructor Details

#initialize(data_bags = []) ⇒ DataBags



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
# File 'lib/spiceweasel/data_bags.rb', line 28

def initialize(data_bags = [])
  @create = Array.new
  @delete = Array.new
  if data_bags
    Spiceweasel::Log.debug("data bags: #{data_bags}")
    data_bags.each do |data_bag|
      db = data_bag.keys.first
      #check directories
      if !File.directory?("data_bags") && !Spiceweasel::Config[:novalidation]
        STDERR.puts "ERROR: 'data_bags' directory not found, unable to validate or load data bag items"
        exit(-1)
      end
      if !File.directory?("data_bags/#{db}") && !Spiceweasel::Config[:novalidation]
        STDERR.puts "ERROR: 'data_bags/#{db}' directory not found, unable to validate or load data bag items"
        exit(-1)
      end
      create_command("knife data bag#{Spiceweasel::Config[:knife_options]} create #{db}")
      delete_command("knife data bag#{Spiceweasel::Config[:knife_options]} delete #{db} -y")
      if data_bag[db]
        items = data_bag[db]['items']
        secret = data_bag[db]['secret']
        if secret && !File.exists?(File.expand_path(secret)) && !Spiceweasel::Config[:novalidation]
          STDERR.puts "ERROR: secret key #{secret} not found, unable to load encrypted data bags for data bag #{db}."
          exit(-1)
        end
      end
      items = [] if items.nil?
      Spiceweasel::Log.debug("data bag: #{db} #{secret} #{items}")
      items.each do |item|
        Spiceweasel::Log.debug("data bag #{db} item: #{item}")
        if item =~ /\*/ #wildcard support, will fail if directory not present
          files = Dir.glob("data_bags/#{db}/#{item}")
          #remove anything not ending in .json
          files.delete_if {|x| !x.end_with?('.json')}
          items.concat(files.collect {|x| x["data_bags/#{db}/".length..-6]})
          Spiceweasel::Log.debug("found files '#{files}' for data bag: #{db} with wildcard #{item}")
          next
        end
        validateItem(db, item) unless Spiceweasel::Config[:novalidation]
      end
      items.delete_if {|x| x.include?("*")} #remove wildcards
      items.sort!.uniq!
      unless items.empty?
        if secret
          create_command("knife data bag#{Spiceweasel::Config[:knife_options]} from file #{db} #{items.join('.json ')}.json --secret-file #{secret}")
        else
          create_command("knife data bag#{Spiceweasel::Config[:knife_options]} from file #{db} #{items.join('.json ')}.json")
        end
      end
    end
  end
end

Instance Attribute Details

#createObject (readonly)

Returns the value of attribute create.



26
27
28
# File 'lib/spiceweasel/data_bags.rb', line 26

def create
  @create
end

#deleteObject (readonly)

Returns the value of attribute delete.



26
27
28
# File 'lib/spiceweasel/data_bags.rb', line 26

def delete
  @delete
end

Instance Method Details

#validateItem(db, item) ⇒ Object

validate the item to be loaded



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/spiceweasel/data_bags.rb', line 82

def validateItem(db, item)
  if !File.exists?("data_bags/#{db}/#{item}.json")
    STDERR.puts "ERROR: data bag '#{db}' item '#{item}' file 'data_bags/#{db}/#{item}.json' does not exist"
    exit(-1)
  end
  f = File.read("data_bags/#{db}/#{item}.json")
  itemfile = JSON.parse(f) #invalid JSON will throw a trace
  #validate the id matches the file name
  if item =~ /\// #pull out directories
    item = item.split('/').last
  end
  if !item.eql?(itemfile['id'])
    STDERR.puts "ERROR: data bag '#{db}' item '#{item}' listed in the manifest does not match the id '#{itemfile['id']}' within the 'data_bags/#{db}/#{item}.json' file."
    exit(-1)
  end
end