Class: Chef::Knife::Blend

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/blender.rb

Instance Method Summary collapse

Instance Method Details

#berkshelf_mode(scheduler, tempdir, file) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/chef/knife/blender.rb', line 192

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) ⇒ Object



233
234
235
236
# File 'lib/chef/knife/blender.rb', line 233

def blender_mode(scheduler, file)
  job = File.read(file)
  scheduler.instance_eval(job, __FILE__, __LINE__)
end

#recipe_mode(scheduler, file) ⇒ Object



223
224
225
226
227
228
229
230
231
# File 'lib/chef/knife/blender.rb', line 223

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

#runObject



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
170
171
172
# File 'lib/chef/knife/blender.rb', line 129

def run
  scheduler_options = {
    config_file: config[:blender_config],
    no_doc: config[:quiet]
  }

  discovery_options = {
    attribute: config[:attribute]
  }

  Blender::Configuration[:noop] = config[:noop]

  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(: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_optionsObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/chef/knife/blender.rb', line 174

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