Class: Spiceweasel::Cookbooks

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandHelper

#create_command, #delete_command

Constructor Details

#initialize(cookbooks = [], other_cookbook_list = {}) ⇒ Cookbooks

Returns a new instance of Cookbooks.



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
80
81
82
# File 'lib/spiceweasel/cookbooks.rb', line 28

def initialize(cookbooks = [], other_cookbook_list = {})
  @create = Array.new
  @delete = Array.new
  @cookbook_list = other_cookbook_list
  @dependencies = Array.new
  #validate each of the cookbooks specified in the manifest
  if cookbooks
    @loader = Chef::CookbookLoader.new(Spiceweasel::Config[:cookbook_dir])
    @loader.load_cookbooks
    Spiceweasel::Log.debug("cookbooks: #{cookbooks}")

    c_names = []
    cookbooks.each do |cookbook|
      name = cookbook.keys.first
      if cookbook[name]
        version = cookbook[name]['version']
        options = cookbook[name]['options']
      end
      Spiceweasel::Log.debug("cookbook: #{name} #{version} #{options}")
      if File.directory?("cookbooks")

        if @loader.cookbooks_by_name[name]
          (name,version) unless Spiceweasel::Config[:novalidation]
        else
          if Spiceweasel::Config[:siteinstall] #use knife cookbook site install
            create_command("knife cookbook#{Spiceweasel::Config[:knife_options]} site install #{name} #{version} #{options}")
          else #use knife cookbook site download, untar and then remove the tarball
            create_command("knife cookbook#{Spiceweasel::Config[:knife_options]} site download #{name} #{version} --file cookbooks/#{name}.tgz #{options}")
            create_command("tar -C cookbooks/ -xf cookbooks/#{name}.tgz")
            create_command("rm -f cookbooks/#{name}.tgz")
          end
        end
      elsif !Spiceweasel::Config[:novalidation]
        STDERR.puts "ERROR: 'cookbooks' directory not found, unable to validate, download and load cookbooks"
        exit(-1)
      end

      if(options)
        if !c_names.empty?
          create_command("knife cookbook#{Spiceweasel::Config[:knife_options]} upload #{c_names.join(' ')}")
          c_names = []
        end
        create_command("knife cookbook#{Spiceweasel::Config[:knife_options]} upload #{name} #{options}")
      else
        c_names.push(name)
      end
      delete_command("knife cookbook#{Spiceweasel::Config[:knife_options]} delete #{name} #{version} -a -y")
      @cookbook_list[name] = version #used for validation
    end
    if !c_names.empty?
      create_command("knife cookbook#{Spiceweasel::Config[:knife_options]} upload #{c_names.join(' ')}")
    end
    validateDependencies() unless Spiceweasel::Config[:novalidation]
  end
end

Instance Attribute Details

#cookbook_listObject (readonly)

Returns the value of attribute cookbook_list.



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

def cookbook_list
  @cookbook_list
end

#createObject (readonly)

Returns the value of attribute create.



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

def create
  @create
end

#deleteObject (readonly)

Returns the value of attribute delete.



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

def delete
  @delete
end

Instance Method Details

#member?(cookbook) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/spiceweasel/cookbooks.rb', line 117

def member?(cookbook)
  cookbook_list.keys.include?(cookbook)
end

#validateDependenciesObject

compare the list of cookbook deps with those specified



107
108
109
110
111
112
113
114
115
# File 'lib/spiceweasel/cookbooks.rb', line 107

def validateDependencies()
  Spiceweasel::Log.debug("cookbook validateDependencies: '#{@dependencies}'")
  @dependencies.each do |dep|
    if !member?(dep)
      STDERR.puts "ERROR: Cookbook dependency '#{dep}' is missing from the list of cookbooks in the manifest."
      exit(-1)
    end
  end
end

#validateMetadata(cookbook, version) ⇒ Object

check the metadata for versions and gather deps



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/spiceweasel/cookbooks.rb', line 85

def (cookbook,version)
  #check metadata.rb for requested version
   = @loader.cookbooks_by_name[cookbook].
  Spiceweasel::Log.debug("validateMetadata: #{cookbook} #{.name} #{.version}")
  # Should the cookbook directory match the name in the metadata?
  if .name.empty?
    Spiceweasel::Log.warn("No cookbook name in the #{cookbook} metadata.rb.")
  elsif cookbook != .name
    STDERR.puts "ERROR: Cookbook '#{cookbook}' does not match the name '#{.name}' in #{cookbook}/metadata.rb."
    exit(-1)
  end
  if version && .version != version
    STDERR.puts "ERROR: Invalid version '#{version}' of '#{cookbook}' requested, '#{.version}' is already in the cookbooks directory."
    exit(-1)
  end
  .dependencies.each do |dependency|
    Spiceweasel::Log.debug("cookbook #{cookbook} metadata dependency: #{dependency}")
    @dependencies.push(dependency[0])
  end
end