class Chef::Knife::Blend

Public Instance Methods

berkshelf_mode(scheduler, tempdir, file) click to toggle source
# File lib/chef/knife/blender.rb, line 193
def berkshelf_mode(scheduler, tempdir, file)
  run_list = config[:run_list]
  scheduler.ruby_task 'generate cookbook tarball' do
    execute do
      berksfile = Berkshelf::Berksfile.from_file('Berksfile')
      berksfile.vendor(tempdir)
      File.open('/tmp/solo.rb', 'w') do |f|
        f.write("cookbook_path '/tmp/cookbooks'\n")
        f.write("file_cache_path '/var/cache/chef/cookbooks'\n")
      end
    end
  end
  scheduler.ssh_task 'nuke old cookbook directory if exist' do
    execute 'rm -rf /tmp/cookbooks'
  end
  scheduler.scp_upload 'upload cookbooks' do
    from tempdir
    to '/tmp/cookbooks'
    recursive true
  end
  scheduler.scp_upload '/tmp/solo.rb' do
    from '/tmp/solo.rb'
  end
  scheduler.ssh_task 'create cache directory' do
    execute 'sudo mkdir -p /var/cache/chef/cookbooks'
  end
  scheduler.ssh_task 'run chef solo' do
    execute "sudo chef-client -z -o #{run_list} -c /tmp/solo.rb --force-logger"
  end
end
blender_mode(scheduler, file) click to toggle source
# File lib/chef/knife/blender.rb, line 234
def blender_mode(scheduler, file)
  job = File.read(file)
  scheduler.instance_eval(job, __FILE__, __LINE__)
end
recipe_mode(scheduler, file) click to toggle source
# File lib/chef/knife/blender.rb, line 224
def recipe_mode(scheduler, file)
  remote_path = File.join('/tmp', SecureRandom.hex(10))
  scheduler.scp_upload('upload recipe') do
    to remote_path
    from file
  end
  scheduler.ssh_task "#{config[:chef_apply]} #{remote_path}"
  scheduler.ssh_task "rm #{remote_path}"
end
run() click to toggle source
# File lib/chef/knife/blender.rb, line 129
def run
  scheduler_options = {
    config_file: config[:blender_config],
    no_doc: config[:quiet],
    noop: config[:noop]
  }

  discovery_options = {
    attribute: config[:attribute]
  }


  if config[:hosts]
    members = config[:hosts].split(',')
  else
    members = Blender::Discovery::Chef.new(discovery_options).search(config[:search])
  end
  ssh_config = ssh_options
  Blender.blend('blender-chef', scheduler_options) do |scheduler|
    scheduler.strategy(config[:strategy])
    scheduler.config(:ssh, ssh_config)
    scheduler.config(:ssh_multi, ssh_config)
    scheduler.config(:scp, ssh_config)
    scheduler.members(members)
    @name_args.each do |file|
      case config[:mode]
      when :berkshelf
        begin
          require 'berkshelf'
        rescue LoadError
          raise RuntimeError, 'You must install berkshelf before using blender-chef in berkshelf mode'
        end
        tempdir = Dir.mktmpdir
        berkshelf_mode(scheduler, tempdir, file)
        FileUtils.rm_rf(tempdir)
      when :recipe
        recipe_mode(scheduler, file)
      when :blender
        blender_mode(scheduler, file)
      else
        raise ArgumentError, "Unknown mode: '#{config[:mode]}'"
      end
    end
  end
end
ssh_options() click to toggle source
# File lib/chef/knife/blender.rb, line 175
def ssh_options
  opts = {
    user: config[:user]
  }
  if config[:identity_file]
    opts[:keys] = Array(config[:identity_file])
  end
  if config[:stream] or (!config[:quiet])
    opts[:stdout] = $stdout
  end
  if config[:password]
    opts[:password] = config[:password]
  elsif config[:prompt]
    opts[:password] = ui.ask('SSH password: ') {|q|q.echo = false}
  end
  opts
end