Class: Spiceweasel::Cookbooks

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

Overview

manages parsing of Cookbooks

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandHelper

#bundler?, #create_command, #delete_command

Constructor Details

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

rubocop:disable CyclomaticComplexity



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/spiceweasel/cookbooks.rb', line 29

def initialize(cookbooks = [], other_cookbook_list = {}) # rubocop:disable CyclomaticComplexity
  @create = []
  @delete = []
  @cookbook_list = other_cookbook_list
  @dependencies = []

  return unless cookbooks

  # validate each of the cookbooks specified in the manifest
  @loader = Chef::CookbookLoader.new(Spiceweasel::Config[:cookbook_dir])
  begin
    @loader.load_cookbooks
  rescue SyntaxError => e
    STDERR.puts 'ERROR: invalid cookbook metadata.'
    STDERR.puts e.message
    exit(-1)
  end
  Spiceweasel::Log.debug("cookbooks: #{cookbooks}")

  validate_cookbooks(cookbooks)
end

Instance Attribute Details

#cookbook_listObject (readonly)

Returns the value of attribute cookbook_list.



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

def cookbook_list
  @cookbook_list
end

#createObject (readonly)

Returns the value of attribute create.



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

def create
  @create
end

#deleteObject (readonly)

Returns the value of attribute delete.



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

def delete
  @delete
end

Instance Method Details

#get_knife_commands(name, options, version) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/spiceweasel/cookbooks.rb', line 94

def get_knife_commands(name, options, version)
  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

#member?(cookbook) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/spiceweasel/cookbooks.rb', line 137

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

#validate_cookbooks(cookbooks) ⇒ Object



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/cookbooks.rb', line 51

def validate_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}")

    (name, options, version)

    if options
      unless 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
  unless c_names.empty?
    create_command("knife cookbook#{Spiceweasel::Config[:knife_options]} upload #{c_names.join(' ')}")
  end
  validate_dependencies unless Spiceweasel::Config[:novalidation]
end

#validate_dependenciesObject

compare the list of cookbook deps with those specified



127
128
129
130
131
132
133
134
135
# File 'lib/spiceweasel/cookbooks.rb', line 127

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

#validate_metadata(cookbook, version) ⇒ Object

check the metadata for versions and gather deps



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/spiceweasel/cookbooks.rb', line 105

def (cookbook, version)
  # check metadata.rb for requested version
   = @loader.cookbooks_by_name[cookbook].
  Spiceweasel::Log.debug("validate_metadata: #{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

#validate_metadata_or_get_knife_commands_wrapper(name, options, version) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/spiceweasel/cookbooks.rb', line 81

def (name, options, version)
  if File.directory?('cookbooks')
    if @loader.cookbooks_by_name[name]
      (name, version) unless Spiceweasel::Config[:novalidation]
    else
      get_knife_commands(name, options, version)
    end
  elsif !Spiceweasel::Config[:novalidation]
    STDERR.puts "ERROR: 'cookbooks' directory not found, unable to validate, download and load cookbooks"
    exit(-1)
  end
end