Class: ForemanOpenbolt::TaskController

Inherits:
ApplicationController
  • Object
show all
Includes:
Foreman::Controller::AutoCompleteSearch
Defined in:
app/controllers/foreman_openbolt/task_controller.rb

Constant Summary collapse

ENCRYPTED_PLACEHOLDER =

For passing to/from the UI

'[Use saved encrypted default]'
REDACTED_PLACEHOLDER =

For saving to the database

'*****'

Instance Method Summary collapse

Instance Method Details

#encrypted_settingsObject



246
247
248
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 246

def encrypted_settings
  openbolt_settings.select(&:encrypted?)
end

#fetch_openbolt_optionsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 44

def fetch_openbolt_options
  options = @openbolt_api.openbolt_options

  # Get defaults from Foreman settings.
  # For encrypted settings, show the placeholder only if a non-empty value
  # has been saved, so the UI shows an empty field for unconfigured passwords
  # instead of a misleading placeholder.
  defaults = {}
  openbolt_settings.each do |setting|
    key = setting.name.sub(/^openbolt_/, '')
    if setting.encrypted?
      defaults[key] = ENCRYPTED_PLACEHOLDER unless setting.value.to_s.empty?
    elsif !setting.value.to_s.empty?
      defaults[key] = setting.value
    end
  end

  # Merge the defaults into the options metadata
  result = {}
  options.each do |name, meta|
    result[name] = meta.dup
    result[name]['default'] = defaults[name] if defaults.key?(name)
  end

  render json: result
rescue ProxyAPI::ProxyException => e
  log_exception('fetch_openbolt_options', e)
  render_error("Smart Proxy error: #{e.message}", :bad_gateway)
rescue StandardError => e
  log_exception('fetch_openbolt_options', e)
  render_error("Internal server error: #{e.message}", :internal_server_error)
end

#fetch_task_historyObject

List of all task history



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 179

def fetch_task_history
  per_page = [(params[:per_page] || 20).to_i, 100].min
  @task_history = TaskJob.includes(:smart_proxy)
                         .recent
                         .paginate(page: params[:page], per_page: per_page)

  render json: {
    results: @task_history.map { |job| serialize_task_job(job) },
    total: @task_history.total_entries,
    page: @task_history.current_page,
    per_page: @task_history.per_page,
  }
rescue StandardError => e
  log_exception('fetch_task_history', e)
  render_error("Error loading task history: #{e.message}", :internal_server_error)
end

#fetch_tasksObject



36
37
38
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 36

def fetch_tasks
  render_openbolt_api_call(:tasks)
end

#job_resultObject



167
168
169
170
171
172
173
174
175
176
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 167

def job_result
  return render_error('Task job not found', :not_found) unless @task_job

  render json: {
    status: @task_job.status,
    command: @task_job.command,
    value: @task_job.result,
    log: @task_job.log,
  }
end

#job_statusObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 148

def job_status
  return render_error('Task job not found', :not_found) unless @task_job

  render json: {
    status: @task_job.status,
    submitted_at: @task_job.,
    completed_at: @task_job.completed_at,
    duration: @task_job.duration,
    task_name: @task_job.task_name,
    task_description: @task_job.task_description,
    task_parameters: @task_job.task_parameters,
    targets: @task_job.targets,
    smart_proxy: {
      id: @task_job.smart_proxy_id,
      name: @task_job.smart_proxy&.name || '(unknown)',
    },
  }
end

#launch_taskObject



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 77

def launch_task
  required_args = [:task_name, :targets]
  missing_args = required_args.select { |arg| params[arg].blank? }

  if missing_args.any?
    return render_error("Missing required arguments to the launch_task function: #{missing_args.join(', ')}",
      :bad_request)
  end

  begin
    task_name = params[:task_name].to_s.strip
    targets = params[:targets].to_s.strip
    task_params = params[:params] || {}
    options = params[:options] || {}
    options = merge_encrypted_defaults(options)

    return render_error('Task name and targets cannot be empty', :bad_request) if task_name.empty? || targets.empty?

    logger.info("Launching OpenBolt task '#{task_name}' on targets '#{targets}' via proxy #{@smart_proxy.name}")

    response = @openbolt_api.launch_task(
      name: task_name,
      targets: targets,
      parameters: task_params,
      options: options
    )

    logger.debug("Task execution response: #{response.inspect}")

    if response['error']
      error_detail = response['error'].is_a?(Hash) ? response['error']['message'] : response['error']
      return render_error("Task execution failed: #{error_detail}", :bad_request)
    end
    return render_error('Task execution failed: No job ID returned', :bad_request) unless response['id']

     = @openbolt_api.tasks[task_name] || {}
    TaskJob.create_from_execution!(
      proxy: @smart_proxy,
      task_name: task_name,
      task_description: ['description'] || '',
      targets: targets.split(',').map(&:strip),
      parameters: task_params,
      options: scrub_options_for_storage(options),
      job_id: response['id']
    )

    # Start background polling to update status
    ForemanTasks.async_task(Actions::ForemanOpenbolt::PollTaskStatus,
      response['id'],
      @smart_proxy.id)

    render json: {
      job_id: response['id'],
    }
  rescue ArgumentError => e
    # From merge_encrypted_defaults when a user submits the encrypted
    # placeholder for an option that has no saved Foreman setting.
    log_exception('launch_task', e)
    render_error(e.message, :bad_request)
  rescue ActiveRecord::RecordInvalid => e
    log_exception('launch_task', e)
    render_error("Database error: #{e.message}", :internal_server_error)
  rescue ProxyAPI::ProxyException => e
    log_exception('launch_task', e)
    render_error("Smart Proxy error: #{e.message}", :bad_gateway)
  rescue StandardError => e
    log_exception('launch_task', e)
    render_error("Error launching task: #{e.message}", :internal_server_error)
  end
end

#load_openbolt_apiObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 215

def load_openbolt_api
  return false unless @smart_proxy
  return true if @openbolt_api && @openbolt_api.url == @smart_proxy.url

  begin
    @openbolt_api = ProxyAPI::Openbolt.new(url: @smart_proxy.url)
  rescue StandardError => e
    log_exception("load_openbolt_api for proxy #{@smart_proxy.name}", e)
    render_error("Failed to connect to Smart Proxy", :bad_gateway)
    return false
  end

  true
end

#load_smart_proxyObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 196

def load_smart_proxy
  proxy_id = params[:proxy_id]
  if proxy_id.blank?
    render_error('Smart Proxy ID is required', :bad_request)
    return false
  end

  return true if @smart_proxy && @smart_proxy.id.to_s == proxy_id.to_s

  @smart_proxy = SmartProxy.authorized(:view_smart_proxies).find_by(id: proxy_id)

  unless @smart_proxy
    render_error("Smart Proxy with ID #{proxy_id} not found or not authorized", :not_found)
    return false
  end

  true
end

#load_task_jobObject



230
231
232
233
234
235
236
237
238
239
240
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 230

def load_task_job
  job_id = params[:job_id]
  logger.debug("load_task_job - Job ID: #{job_id}")
  if job_id.blank?
    render_error('Job ID is required', :bad_request)
    return false
  end

  @task_job = TaskJob.find_by(job_id: job_id)
  logger.debug("load_task_job - Task Job: #{@task_job.inspect}")
end

#log_exception(message, exception) ⇒ Object



286
287
288
289
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 286

def log_exception(message, exception)
  logger.error("#{message}: #{exception.class}: #{exception.message}")
  logger.error(exception.backtrace.join("\n")) if exception.backtrace
end

#merge_encrypted_defaults(options) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 250

def merge_encrypted_defaults(options)
  merged = options.dup
  merged.each do |key, value|
    next unless value == ENCRYPTED_PLACEHOLDER

    saved = Setting["openbolt_#{key}"]
    if saved.nil? || saved.to_s.empty?
      raise ArgumentError,
        "No saved value for encrypted option '#{key}'. Configure it in Administer > Settings or provide a value."
    end
    merged[key] = saved
  end
  merged
end

#openbolt_settingsObject



242
243
244
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 242

def openbolt_settings
  @openbolt_settings ||= Foreman.settings.select { |s| s.name.start_with?('openbolt_') }
end

#page_launch_taskObject

React-rendered pages



24
25
26
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 24

def page_launch_task
  render 'foreman_openbolt/react_page'
end

#page_task_executionObject



28
29
30
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 28

def page_task_execution
  render 'foreman_openbolt/react_page'
end

#page_task_historyObject



32
33
34
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 32

def page_task_history
  render 'foreman_openbolt/react_page'
end

#reload_tasksObject



40
41
42
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 40

def reload_tasks
  render_openbolt_api_call(:reload_tasks)
end

#render_error(message, status) ⇒ Object



291
292
293
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 291

def render_error(message, status)
  render json: { error: message }, status: status
end

#render_openbolt_api_call(method_name, **args) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 274

def render_openbolt_api_call(method_name, **args)
  result = @openbolt_api.send(method_name, **args)
  logger.debug("OpenBolt API call #{method_name} successful for proxy #{@smart_proxy.name}")
  render json: result
rescue ProxyAPI::ProxyException => e
  log_exception(method_name, e)
  render_error("Smart Proxy error: #{e.message}", :bad_gateway)
rescue StandardError => e
  log_exception(method_name, e)
  render_error("Internal server error: #{e.message}", :internal_server_error)
end

#scrub_options_for_storage(options) ⇒ Object



265
266
267
268
269
270
271
272
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 265

def scrub_options_for_storage(options)
  scrubbed = options.dup
  encrypted_settings.each do |setting|
    option_name = setting.name.sub(/^openbolt_/, '')
    scrubbed[option_name] = REDACTED_PLACEHOLDER if scrubbed.key?(option_name)
  end
  scrubbed
end

#serialize_task_job(task_job) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'app/controllers/foreman_openbolt/task_controller.rb', line 295

def serialize_task_job(task_job)
  {
    job_id: task_job.job_id,
    task_name: task_job.task_name,
    task_description: task_job.task_description,
    task_parameters: task_job.task_parameters,
    targets: task_job.targets,
    status: task_job.status,
    smart_proxy: {
      id: task_job.smart_proxy_id,
      name: task_job.smart_proxy&.name || '(unknown)',
    },
    submitted_at: task_job.,
    completed_at: task_job.completed_at,
    duration: task_job.duration,
  }
end