Class: ChefApply::Action::ConvergeTarget

Inherits:
Base
  • Object
show all
Defined in:
lib/chef_apply/action/converge_target.rb

Defined Under Namespace

Classes: ConfigUploadFailed, HandlerUploadFailed, PolicyUploadFailed

Constant Summary

Constants inherited from Base

Base::PATH_MAPPING

Instance Attribute Summary

Attributes inherited from Base

#config, #target_host

Instance Method Summary collapse

Methods inherited from Base

#escape_windows_path, #initialize, #name, #notify, #run, #run_chef

Constructor Details

This class inherits a constructor from ChefApply::Action::Base

Instance Method Details

#create_remote_config(dir) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/chef_apply/action/converge_target.rb', line 68

def create_remote_config(dir)
  remote_config_path = File.join(dir, "workstation.rb")

  workstation_rb = "    local_mode true\n    color false\n    cache_path \"\#{cache_path}\"\n    chef_repo_path \"\#{cache_path}\"\n    require_relative \"reporter\"\n    reporter = ChefApply::Reporter.new\n    report_handlers << reporter\n    exception_handlers << reporter\n  EOM\n\n  # Maybe add data collector endpoint.\n  dc = ChefApply::Config.data_collector\n  if !dc.url.nil? && !dc.token.nil?\n    workstation_rb << <<~EOM\n      data_collector.server_url \"\#{dc.url}\"\n      data_collector.token \"\#{dc.token}\"\n      data_collector.mode :solo\n      data_collector.organization \"Chef Workstation\"\n    EOM\n  end\n\n  begin\n    config_file = Tempfile.new\n    config_file.write(workstation_rb)\n    config_file.close\n    target_host.upload_file(config_file.path, remote_config_path)\n  rescue RuntimeError\n    raise ConfigUploadFailed.new()\n  ensure\n    config_file.unlink\n  end\n  remote_config_path\nend\n"

#create_remote_handler(dir) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chef_apply/action/converge_target.rb', line 106

def create_remote_handler(dir)
  remote_handler_path = File.join(dir, "reporter.rb")
  begin
    handler_file = Tempfile.new
    handler_file.write(File.read(File.join(__dir__, "reporter.rb")))
    handler_file.close
    target_host.upload_file(handler_file.path, remote_handler_path)
  rescue RuntimeError
    raise HandlerUploadFailed.new()
  ensure
    handler_file.unlink
  end
  remote_handler_path
end

#create_remote_policy(local_policy_path, remote_dir_path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chef_apply/action/converge_target.rb', line 56

def create_remote_policy(local_policy_path, remote_dir_path)
  remote_policy_path = File.join(remote_dir_path, File.basename(local_policy_path))
  notify(:creating_remote_policy)
  begin
    target_host.upload_file(local_policy_path, remote_policy_path)
  rescue RuntimeError => e
    ChefApply::Log.error(e)
    raise PolicyUploadFailed.new()
  end
  remote_policy_path
end

#handle_ccr_errorObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/chef_apply/action/converge_target.rb', line 137

def handle_ccr_error
  require "chef_apply/errors/ccr_failure_mapper"
  mapper_opts = {}
  c = target_host.run_command(read_chef_report)
  if c.exit_status == 0
    report = JSON.parse(c.stdout)
    # We need to delete the stacktrace after copying it over. Otherwise if we get a
    # remote failure that does not write a chef stacktrace its possible to get an old
    # stale stacktrace.
    target_host.run_command!(delete_chef_report)
    ChefApply::Log.error("Remote chef-client error follows:")
    ChefApply::Log.error(report["exception"])
  else
    report = {}
    ChefApply::Log.error("Could not read remote report:")
    ChefApply::Log.error("stdout: #{c.stdout}")
    ChefApply::Log.error("stderr: #{c.stderr}")
    mapper_opts[:stdout] = c.stdout
    mapper_opts[:stderr] = c.stderr
  end
  mapper = ChefApply::Errors::CCRFailureMapper.new(report["exception"], mapper_opts)
  mapper.raise_mapped_exception!
end

#perform_actionObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef_apply/action/converge_target.rb', line 27

def perform_action
  local_policy_path = config.delete :local_policy_path
  remote_tmp = target_host.run_command!(mktemp, true)
  remote_dir_path = escape_windows_path(remote_tmp.stdout.strip)
  remote_policy_path = create_remote_policy(local_policy_path, remote_dir_path)
  remote_config_path = create_remote_config(remote_dir_path)
  create_remote_handler(remote_dir_path)
  upload_trusted_certs(remote_dir_path)

  notify(:running_chef)
  cmd_str = run_chef(remote_dir_path,
                     File.basename(remote_config_path),
                     File.basename(remote_policy_path))
  c = target_host.run_command(cmd_str)
  target_host.run_command!("#{delete_folder} #{remote_dir_path}")
  if c.exit_status == 0
    ChefApply::Log.debug(c.stdout)
    notify(:success)
  elsif c.exit_status == 35
    notify(:reboot)
  else
    notify(:converge_error)
    ChefApply::Log.error("Error running command [#{cmd_str}]")
    ChefApply::Log.error("stdout: #{c.stdout}")
    ChefApply::Log.error("stderr: #{c.stderr}")
    handle_ccr_error()
  end
end

#upload_trusted_certs(dir) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/chef_apply/action/converge_target.rb', line 121

def upload_trusted_certs(dir)
  local_tcd = Chef::Util::PathHelper.escape_glob_dir(ChefApply::Config.chef.trusted_certs_dir)
  certs = Dir.glob(File.join(local_tcd, "*.{crt,pem}"))
  return if certs.empty?
  notify(:uploading_trusted_certs)
  remote_tcd = "#{dir}/trusted_certs"
  # We create the trusted_certs dir with the connection user (instead of the root
  # user it would get as default since we run in sudo mode) because the `upload_file`
  # uploads as the connection user. Without this upload_file would fail because
  # it tries to write to a root-owned folder.
  target_host.run_command("#{mkdir} #{remote_tcd}", true)
  certs.each do |cert_file|
    target_host.upload_file(cert_file, "#{remote_tcd}/#{File.basename(cert_file)}")
  end
end