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



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
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chef_apply/action/converge_target.rb', line 70

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

  workstation_rb = <<~EOM
    local_mode true
    color false
    cache_path "#{cache_path}"
    chef_repo_path "#{cache_path}"
    require_relative "reporter"
    reporter = ChefApply::Reporter.new
    report_handlers << reporter
    exception_handlers << reporter
  EOM

  # add the target host's log level value
  # (we don't set a location because we want output to
  #   go in stdout for reporting back to chef-apply)
  log_settings = ChefApply::Config.log
  if !log_settings.target_level.nil?
    workstation_rb << <<~EOM
      log_level :#{log_settings.target_level}
    EOM
  end

  # Maybe add data collector endpoint.
  dc = ChefApply::Config.data_collector
  if !dc.url.nil? && !dc.token.nil?
    workstation_rb << <<~EOM
      data_collector.server_url "#{dc.url}"
      data_collector.token "#{dc.token}"
      data_collector.mode :solo
      data_collector.organization "Chef Workstation"
    EOM
  end

  begin
    config_file = Tempfile.new
    config_file.write(workstation_rb)
    config_file.close
    target_host.upload_file(config_file.path, remote_config_path)
  rescue RuntimeError
    raise ConfigUploadFailed.new()
  ensure
    config_file.unlink
  end
  remote_config_path
end

#create_remote_handler(dir) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/chef_apply/action/converge_target.rb', line 118

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



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

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/chef_apply/action/converge_target.rb', line 145

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
55
56
# 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.mktemp()
  remote_dir_path = escape_windows_path(remote_tmp)
  # Ensure the directory is owned by the connecting user,
  # otherwise we won't be able to put things into it over scp as that user.
  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.info(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



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/chef_apply/action/converge_target.rb', line 133

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"
  target_host.mkdir(remote_tcd)
  certs.each do |cert_file|
    target_host.upload_file(cert_file, "#{remote_tcd}/#{File.basename(cert_file)}")
  end
end