Class: Cucumber::Chef::Containers

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/chef/containers.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ui = ZTK::UI.new, test_lab = nil) ⇒ Containers

Returns a new instance of Containers.



32
33
34
# File 'lib/cucumber/chef/containers.rb', line 32

def initialize(ui=ZTK::UI.new, test_lab=nil)
  @ui, @test_lab = ui, test_lab
end

Class Method Details

.generate_ipObject



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/cucumber/chef/containers.rb', line 161

def generate_ip
  octets = [ 192..192,
             168..168,
             0..254,
             1..254 ]
  ip = Array.new
  for x in 1..4 do
    ip << octets[x-1].to_a[rand(octets[x-1].count)].to_s
  end
  ip.join(".")
end

.generate_macObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/cucumber/chef/containers.rb', line 173

def generate_mac
  digits = [ %w(0),
             %w(0),
             %w(0),
             %w(0),
             %w(5),
             %w(e),
             %w(0 1 2 3 4 5 6 7 8 9 a b c d e f),
             %w(0 1 2 3 4 5 6 7 8 9 a b c d e f),
             %w(5 6 7 8 9 a b c d e f),
             %w(3 4 5 6 7 8 9 a b c d e f),
             %w(0 1 2 3 4 5 6 7 8 9 a b c d e f),
             %w(0 1 2 3 4 5 6 7 8 9 a b c d e f) ]
  mac = ""
  for x in 1..12 do
    mac += digits[x-1][rand(digits[x-1].count)]
    mac += ":" if (x.modulo(2) == 0) && (x != 12)
  end
  mac
end

Instance Method Details

#chef_run_client(container, *args) ⇒ Object



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
# File 'lib/cucumber/chef/containers.rb', line 117

def chef_run_client(container, *args)
  chef_config_client(container)

  @ui.logger.info { "Removing artifacts #{Cucumber::Chef::Config[:artifacts].values.collect{|z| "'#{z}'" }.join(' ')}." }
  (@test_lab.proxy_ssh(container.id).exec("/bin/rm -fv #{Cucumber::Chef::Config[:artifacts].values.join(' ')}", :silence => true) rescue nil)

  @ui.logger.info { "Running chef client on container '#{container.id}'." }

  arguments = {
    "--node-name" => container.id,
    "--json-attributes" => File.join("/etc", "chef", "attributes.json").to_s,
    "--log_level" => @chef_client_config[:log_level],
    "--logfile" => @chef_client_config[:log_location],
    "--server" => @chef_client_config[:chef_server_url],
    "--environment" => (container.chef_client[:environment] || @chef_client_config[:environment])
  }.reject{ |k,v| v.nil? }.sort

  output = nil
  bm = ::Benchmark.realtime do
    output = @test_lab.proxy_ssh(container.id).exec(["/usr/bin/chef-client", arguments, args, "--once"].flatten.join(" "), :silence => true, :ignore_exit_status => true)
  end
  @ui.logger.info { "Chef client run on container '#{container.id}' took %0.4f seconds." % bm }

  chef_client_artifacts(container)

  output
end

#chef_set_client_config(config = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cucumber/chef/containers.rb', line 100

def chef_set_client_config(config={})
  @chef_client_config = (@chef_client_config || {
    :log_level => :debug,
    :log_location => "/var/log/chef/client.log",
    :chef_server_url => "https://api.opscode.com/organizations/#{config[:orgname]}",
    :validation_client_name => "#{config[:orgname]}-validator",
    :ssl_verify_mode => :verify_none,
    :environment => nil # use default; i.e. set no value
  }).merge(config)

  @ui.logger.info { "Setting chef client config '#{@chef_client_config.inspect}'." }

  true
end

#countObject



38
39
40
# File 'lib/cucumber/chef/containers.rb', line 38

def count
  Container.count
end

#create(container) ⇒ Object



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
# File 'lib/cucumber/chef/containers.rb', line 44

def create(container)
  # if this is a new or non-persistent container destroy it
  destroy(container.id) if !container.persist

  container.ip ||= self.generate_ip
  container.mac ||= self.generate_mac
  container.persist ||= false
  container.distro ||= "ubuntu"
  container.release ||= "lucid"
  container.arch = detect_arch(container.distro || "ubuntu")

  if running?(container.id)
    @ui.logger.info { "Container '#{container.id}' is already running." }
  else
    @ui.logger.info { "Please wait, creating container #{container.inspect}." }
    bm = ::Benchmark.realtime do
      test_lab_config_dhcpd
      config_network(container)
      _create(container.id, container.distro, container.release, container.arch)
    end
    @ui.logger.info { "Container '#{container.id}' creation took %0.4f seconds." % bm }

    bm = ::Benchmark.realtime do
      ZTK::RescueRetry.try(:tries => 32) do
        @test_lab.ssh.exec("host #{container.id}", :silence => true)
      end
      ZTK::RescueRetry.try(:tries => 32) do
        @test_lab.proxy_ssh(container.id).exec("uptime", :silence => true)
      end
    end
    @ui.logger.info { "Container '#{container.id}' SSHD responded after %0.4f seconds." % bm }
  end
end

#destroy(name) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cucumber/chef/containers.rb', line 80

def destroy(name)
  @test_lab.knife_cli("node delete #{name}", :ignore_exit_status => true)
  @test_lab.knife_cli("client delete #{name}", :ignore_exit_status => true)

  if exists?(name)
    stop(name)
    @test_lab.bootstrap_ssh.exec("sudo lxc-destroy -n #{name}", :silence => true)
    @ui.logger.info { "Destroyed container '#{name}'." }
    test_lab_config_dhcpd
  end
end

#enable_minitest(name) ⇒ Object



147
148
149
# File 'lib/cucumber/chef/containers.rb', line 147

def enable_minitest(name)
  @chef_client_attributes[:run_list].unshift("recipe[minitest-handler]")
end

#listObject



198
199
200
# File 'lib/cucumber/chef/containers.rb', line 198

def list
  @test_lab.bootstrap_ssh.exec("sudo lxc-ls 2>&1", :silence => true).output.strip.split(" ").uniq
end

#provision(container, *args) ⇒ Object



94
95
96
# File 'lib/cucumber/chef/containers.rb', line 94

def provision(container, *args)
  @test_lab.containers.chef_run_client(container, *args)
end

#run_minitests(name) ⇒ Object



151
152
153
154
155
156
# File 'lib/cucumber/chef/containers.rb', line 151

def run_minitests(name)
  chef_run = chef_run_client(name, "-l info")
  test_result = chef_run.drop_while {|e| e !~ /^# Running tests/}.take_while {|e| e !~ /^[.*] INFO/}
  puts test_result
  test_result
end