class TestLab::Provisioner::Chef::OmniBus

OmniBus Provisioner Class

@author Zachary Patten <zachary AT jovelabs DOT com>

Public Class Methods

new(config={}, ui=nil) click to toggle source
# File lib/testlab/provisioners/chef/omni_bus.rb, line 13
def initialize(config={}, ui=nil)
  @config = (config || Hash.new)
  @ui     = (ui || TestLab.ui)

  @config[:chef] ||= Hash.new
  @config[:chef][:client] ||= Hash.new
  @config[:chef][:client][:attach_to_container] ||= %(chef-server)
  @config[:chef][:client][:version]             ||= %(latest)
  @config[:chef][:client][:log_level]           ||= :info
  @config[:chef][:client][:attributes]          ||= Hash.new

  @chef_server = TestLab::Container.first(@config[:chef][:client][:attach_to_container])

  @config[:chef][:client][:server_url] ||= %(https://#{@chef_server.ip})

  @ui.logger.debug { "config(#{@config.inspect})" }
end

Public Instance Methods

on_container_deprovision(container) click to toggle source

OmniBus: Container Deprovision

@return [Boolean] True if successful.

# File lib/testlab/provisioners/chef/omni_bus.rb, line 62
def on_container_deprovision(container)
  if @chef_server.state == :running
    @chef_server.exec(%(knife node delete #{container.id} --yes), :ignore_exit_status => true)
    @chef_server.exec(%(knife client delete #{container.id} --yes), :ignore_exit_status => true)
  end

  true
end
on_container_provision(container) click to toggle source

OmniBus: Container Provision

Renders the defined script to a temporary file on the target container and proceeds to execute said script as root via lxc-attach.

@param [TestLab::Container] container The container which we want to

provision.

@return [Boolean] True if successful.

# File lib/testlab/provisioners/chef/omni_bus.rb, line 39
def on_container_provision(container)
  @config[:chef][:client][:node_name] ||= container.id

  omnibus_template = File.join(TestLab::Provisioner::Chef.template_dir, 'omni_bus.erb')

  config = {}.merge!({
    :chef_client_cli => chef_client_cli(container),
    :chef_client_rb => chef_client_rb(container),
    :validation_pem => validation_pem,
    :sudo_user => container.primary_user.username,
    :sudo_uid => container.primary_user.uid,
    :sudo_gid => container.primary_user.gid,
    :home_dir => container.primary_user.home_dir
  }).merge!(@config)

  container.bootstrap(ZTK::Template.render(omnibus_template, config))

  true
end

Private Instance Methods

chef_client_cli(container, *args) click to toggle source
# File lib/testlab/provisioners/chef/omni_bus.rb, line 73
def chef_client_cli(container, *args)
  arguments = Array.new

  arguments << %(chef-client)
  arguments << [args]
  arguments << %(--node-name #{container.id})
  arguments << %(--environment #{@config[:chef][:client][:environment]}) if !@config[:chef][:client][:environment].nil?
  arguments << %(--json-attributes /etc/chef/attributes.json)
  arguments << %(--server #{@config[:chef][:client][:server_url]})
  arguments << %(--once)

  arguments.flatten.compact.join(' ')
end
chef_client_rb(container) click to toggle source
# File lib/testlab/provisioners/chef/omni_bus.rb, line 87
        def chef_client_rb(container)
          <<-EOF
#{ZTK::Template.do_not_edit_notice(:message => "OmniBus TestLab Chef-Client Configuration")}
log_level               #{@config[:chef][:client][:log_level].inspect}
log_location            STDOUT
chef_server_url         #{@config[:chef][:client][:server_url].inspect}
validation_client_name  "chef-validator"
node_name               #{@config[:chef][:client][:node_name].inspect}
          EOF
        end
validation_pem() click to toggle source
# File lib/testlab/provisioners/chef/omni_bus.rb, line 98
def validation_pem
  @chef_server.exec(%((cat ~/.chef/validation.pem || cat ~/.chef/chef-validator.pem) 2> /dev/null)).output.strip
end