Class: Vagabond::Kitchen

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions, Actions::Status, Helpers
Defined in:
lib/vagabond/kitchen.rb

Constant Summary

Constants included from Helpers

Helpers::GEN_NAME_LENGTH, Helpers::RAND_CHARS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Actions::Status

#_status, included

Methods included from Helpers

included

Constructor Details

#initialize(*args) ⇒ Kitchen

Returns a new instance of Kitchen.



35
36
37
# File 'lib/vagabond/kitchen.rb', line 35

def initialize(*args)
  super
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



32
33
34
# File 'lib/vagabond/kitchen.rb', line 32

def action
  @action
end

#internal_configObject (readonly)

Returns the value of attribute internal_config.



33
34
35
# File 'lib/vagabond/kitchen.rb', line 33

def internal_config
  @internal_config
end

#kitchenObject (readonly)

Returns the value of attribute kitchen.



27
28
29
# File 'lib/vagabond/kitchen.rb', line 27

def kitchen
  @kitchen
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/vagabond/kitchen.rb', line 31

def name
  @name
end

#uiObject (readonly)

Returns the value of attribute ui.



30
31
32
# File 'lib/vagabond/kitchen.rb', line 30

def ui
  @ui
end

#vagabondfileObject (readonly)

Returns the value of attribute vagabondfile.



29
30
31
# File 'lib/vagabond/kitchen.rb', line 29

def vagabondfile
  @vagabondfile
end

Class Method Details

.basenameObject



20
21
22
# File 'lib/vagabond/kitchen.rb', line 20

def basename
  'vagabond kitchen'
end

Instance Method Details

#status(name = nil) ⇒ Object



172
173
174
175
# File 'lib/vagabond/kitchen.rb', line 172

def status(name=nil)
  setup(name, :status)
  _status
end

#teardown(cookbook) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/vagabond/kitchen.rb', line 48

def teardown(cookbook)
  ui.info "#{ui.color('Vagabond:', :bold)} - Kitchen teardown for cookbook #{ui.color(name, :red)}"
  plats = [platform || options[:platform] || platform_map.keys].flatten
  plats.each do |plat|
    validate_platform!(plat)
    ui.info ui.color("  -> Tearing down platform: #{plat}", :red)
    vagabond_instance(:destroy, plat).send(:execute)
    ui.info ui.color("  -> Teardown of platform: #{plat} - COMPLETE!", :red)
  end
end

#test(*args) ⇒ Object

Raises:

  • (VagabondError::KitchenTestFailed)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/vagabond/kitchen.rb', line 82

def test(*args)
  cookbook = args.first
  setup(cookbook, :test)

  ui.info "#{ui.color('Vagabond:', :bold)} - Kitchen testing for cookbook #{ui.color(name, :cyan)}"
  platforms = [options[:platform] || platform_map.keys].flatten
  @results = Mash[*platforms.zip([[]] * platforms.size).flatten(1)]
  if(cluster_name = options.delete(:cluster))
    ui.info ui.color("  -> Cluster Testing #{cluster_name}!", :yellow)
    if(kitchen.clusters.empty? || kitchen.clusters[cluster_name].nil?)
      ui.fatal "Requested cluster is not defined: #{options[:cluster]}"
      raise VagabondError::ClusterInvalid.new(cluster_name)
    end
    serv = Server.new
    if(@solo && serv.vagabondfile[:local_chef_server].empty?)
      serv.vagabondfile[:local_chef_server].update(:enabled => true, :zero => true)
    end
    internal_config.make_knife_config_if_required(:force)
    serv.options = options.update(:auto_upload => false)
    serv.send(:do_create)
    load_cookbooks(:upload)
    suites = kitchen.clusters[cluster_name]
    platforms.each do |platform|
      begin
        suites.each do |suite_name|
          unless(local_server_provision_node(platform, suite_name))
            ui.error "Failed to provision #{suite_name} in platform #{platform}"
            @results[platform] << {
              :suite_name => suite_name,
              :result => false
            } 
            raise VagabondError::HostProvisionFailed.new("#{platform}[#{suite_name}]")
          end
        end
        suites.each do |suite_name|
          @results[platform] << {
            :suite_name => suite_name,
            :result => test_node(platform, suite_name)
          }
        end
      rescue VagabondError::HostProvisionFailed => e
        debug("Caught failed provision error. Scrubbing nodes. - #{e}")
      ensure
        suites.each do |suite_name|
          destroy_node(platform, suite_name)
        end
      end
    end
    serv.destroy
  else
    load_cookbooks
    suites = options[:suites] ? options[:suites].split(',') : kitchen.suites.map(&:name)
    runners = []
    platforms.each do |platform|
      suites.each do |suite_name|
        runner = lambda do
          provision_node(platform, suite_name)
          @results[platform] << {
            :suite_name => suite_name,
            :result => test_node(platform, suite_name)
          }
          destroy_node(platform, suite_name)
        end
        if(options[:parallel])
          runners << Thread.new{ runner.call }
        else
          runner.call
        end
      end
    end
    if(options[:parallel])
      until(runners.empty?)
        runners.each do |runner|
          runner.join
          runners.delete(runner)
        end
      end
    end
  end
  ui.info ui.color('Kitchen Test Results:', :bold)
  @results.each do |platform, infos|
    ui.info "  Platform: #{ui.color(platform, :blue, :bold)}"
    infos.each do |res|
      ui.info "    Suite: #{res[:suite_name]} -> #{res[:result] ? ui.color('SUCCESS!', :green) : ui.color('FAILED!', :red)}"
    end
  end
  raise VagabondError::KitchenTestFailed.new(@results.values.flatten.map{|res| res[:suite_name] unless res[:result]}.compact)
end