Class: VagrantWindows::Provisioners::ChefCommandBuilder

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/vagrant-windows/provisioners/chef_command_builder.rb

Overview

Builds scripts and ultimately the command to execute Chef solo or Chef client on Windows guests using a scheduled task.

Instance Method Summary collapse

Methods included from Helper

#wait_if_rebooting, #win_friendly_path, #win_friendly_share_id

Constructor Details

#initialize(windows_machine, chef_config, client_type) ⇒ ChefCommandBuilder

Returns a new instance of ChefCommandBuilder.



14
15
16
17
18
19
20
21
22
23
# File 'lib/vagrant-windows/provisioners/chef_command_builder.rb', line 14

def initialize(windows_machine, chef_config, client_type)
  @windows_machine = windows_machine
  @config = chef_config

  if client_type != :solo && client_type != :client
    raise 'Invalid client_type, expected solo or client'
  end

  @client_type = client_type
end

Instance Method Details

#chef_binary_pathObject

Returns the path to the Chef binary, taking into account the ‘binary_path` configuration option.



93
94
95
96
97
# File 'lib/vagrant-windows/provisioners/chef_command_builder.rb', line 93

def chef_binary_path()
  binary = "chef-#{@client_type}"
  return binary if !@config.binary_path
  return win_friendly_path(File.join(@config.binary_path, binary))
end

#chef_task_ps1_pathObject



87
88
89
# File 'lib/vagrant-windows/provisioners/chef_command_builder.rb', line 87

def chef_task_ps1_path()
  provisioning_path('cheftask.ps1')
end

#create_chef_argumentsObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/vagrant-windows/provisioners/chef_command_builder.rb', line 76

def create_chef_arguments()
  command_args = @config.arguments ? @config.arguments : ''
  chef_path = provisioning_path("#{@client_type}.rb")
  chef_dna_path = provisioning_path('dna.json')

  chef_arguments = "-c #{chef_path}"
  chef_arguments << " -j #{chef_dna_path}"
  chef_arguments << " #{command_args}"
  chef_arguments.strip
end

#create_chef_optionsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vagrant-windows/provisioners/chef_command_builder.rb', line 59

def create_chef_options
  command_env = @config.binary_env ? "#{@config.binary_env} " : ''
  return {
    :user => @windows_machine.winrm_config.username,
    :pass => @windows_machine.winrm_config.password,
    :chef_arguments => create_chef_arguments(),
    :chef_task_xml => provisioning_path('cheftask.xml'),
    :chef_task_running => provisioning_path('cheftask.running'),
    :chef_task_exitcode => provisioning_path('cheftask.exitcode'),
    :chef_task_ps1 => chef_task_ps1_path(),
    :chef_task_run_ps1 => provisioning_path('cheftaskrun.ps1'),
    :chef_stdout_log => provisioning_path("chef-#{@client_type}.log"),
    :chef_stderr_log => provisioning_path("chef-#{@client_type}.err.log"),
    :chef_binary_path => win_friendly_path("#{command_env}#{chef_binary_path}")
  }
end

#prepare_for_chef_runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vagrant-windows/provisioners/chef_command_builder.rb', line 25

def prepare_for_chef_run()
  options = create_chef_options()

  # create cheftaskrun.ps1 that the scheduled task will invoke when run
  render_file_and_upload('cheftaskrun.ps1', options[:chef_task_run_ps1],
    :options => options)

  # create cheftask.xml that the scheduled task will be created with
  render_file_and_upload('cheftask.xml', options[:chef_task_xml],
    :options => options)

  # create cheftask.ps1 that will immediately invoke the scheduled task and wait for completion
  render_file_and_upload('cheftask.ps1', options[:chef_task_ps1],
    :options => options)
end

#provisioning_path(file_name) ⇒ Object



99
100
101
# File 'lib/vagrant-windows/provisioners/chef_command_builder.rb', line 99

def provisioning_path(file_name)
  win_friendly_path("#{@config.provisioning_path}/#{file_name}")
end

#render_file_and_upload(script_name, dest_file, options) ⇒ Object



52
53
54
55
56
57
# File 'lib/vagrant-windows/provisioners/chef_command_builder.rb', line 52

def render_file_and_upload(script_name, dest_file, options)
  # render the script file to a local temp file and then upload
  script_local = Tempfile.new(script_name)
  IO.write(script_local, VagrantWindows.load_script_template(script_name, options))
  @windows_machine.winrmshell.upload(script_local, dest_file)
end

#run_chef_commandObject



41
42
43
44
45
46
47
48
# File 'lib/vagrant-windows/provisioners/chef_command_builder.rb', line 41

def run_chef_command()
  return "  $old = Get-ExecutionPolicy;\n  Set-ExecutionPolicy Unrestricted -force;\n  \#{chef_task_ps1_path};\n  Set-ExecutionPolicy $old -force\n  EOH\nend\n"