Class: JobInvocationComposer

Inherits:
Object
  • Object
show all
Defined in:
app/models/job_invocation_composer.rb

Defined Under Namespace

Classes: ApiParams, ParamsForFeature, ParamsFromJobInvocation, UiParams

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, set_defaults = false) ⇒ JobInvocationComposer

Returns a new instance of JobInvocationComposer.



260
261
262
263
264
265
266
267
268
269
# File 'app/models/job_invocation_composer.rb', line 260

def initialize(params, set_defaults = false)
  @params = params
  @set_defaults = set_defaults
  @job_invocation = JobInvocation.new
  @job_invocation.task_group = JobInvocationTaskGroup.new
  compose

  @host_ids = validate_host_ids(params[:host_ids])
  @search_query = job_invocation.targeting.search_query unless job_invocation.targeting.bookmark_id.present?
end

Instance Attribute Details

#host_idsObject

Returns the value of attribute host_ids.



257
258
259
# File 'app/models/job_invocation_composer.rb', line 257

def host_ids
  @host_ids
end

#job_invocationObject

Returns the value of attribute job_invocation.



257
258
259
# File 'app/models/job_invocation_composer.rb', line 257

def job_invocation
  @job_invocation
end

#paramsObject

Returns the value of attribute params.



257
258
259
# File 'app/models/job_invocation_composer.rb', line 257

def params
  @params
end

#search_queryObject

Returns the value of attribute search_query.



257
258
259
# File 'app/models/job_invocation_composer.rb', line 257

def search_query
  @search_query
end

Class Method Details

.for_feature(feature_label, hosts, provided_inputs = {}) ⇒ Object



283
284
285
# File 'app/models/job_invocation_composer.rb', line 283

def self.for_feature(feature_label, hosts, provided_inputs = {})
  self.new(ParamsForFeature.new(feature_label, hosts, provided_inputs).params)
end

.from_api_params(api_params) ⇒ Object



279
280
281
# File 'app/models/job_invocation_composer.rb', line 279

def self.from_api_params(api_params)
  self.new(ApiParams.new(api_params).params)
end

.from_job_invocation(job_invocation) ⇒ Object



271
272
273
# File 'app/models/job_invocation_composer.rb', line 271

def self.from_job_invocation(job_invocation)
  self.new(ParamsFromJobInvocation.new(job_invocation).params)
end

.from_ui_params(ui_params) ⇒ Object



275
276
277
# File 'app/models/job_invocation_composer.rb', line 275

def self.from_ui_params(ui_params)
  self.new(UiParams.new(ui_params).params, true)
end

Instance Method Details

#available_bookmarksObject



388
389
390
# File 'app/models/job_invocation_composer.rb', line 388

def available_bookmarks
  Bookmark.authorized(:view_bookmarks).my_bookmarks.where(:controller => ['hosts', 'dashboard'])
end

#available_job_categoriesObject



339
340
341
# File 'app/models/job_invocation_composer.rb', line 339

def available_job_categories
  available_templates.reorder(:job_category).group(:job_category).pluck(:job_category)
end

#available_provider_typesObject



343
344
345
# File 'app/models/job_invocation_composer.rb', line 343

def available_provider_types
  available_templates_for(job_category).reorder(:provider_type).group(:provider_type).pluck(:provider_type)
end

#available_template_inputsObject



347
348
349
# File 'app/models/job_invocation_composer.rb', line 347

def available_template_inputs
  TemplateInput.where(:template_id => job_template_ids.empty? ? available_templates_for(job_category).map(&:id) : job_template_ids)
end

#available_templatesObject



331
332
333
# File 'app/models/job_invocation_composer.rb', line 331

def available_templates
  JobTemplate.authorized(:view_job_templates).where(:snippet => false)
end

#available_templates_for(job_category) ⇒ Object



335
336
337
# File 'app/models/job_invocation_composer.rb', line 335

def available_templates_for(job_category)
  available_templates.where(:job_category => job_category)
end

#composeObject



287
288
289
290
291
292
293
294
295
296
297
298
# File 'app/models/job_invocation_composer.rb', line 287

def compose
  job_invocation.job_category = validate_job_category(params[:job_category])
  job_invocation.job_category ||= available_job_categories.first if @set_defaults
  job_invocation.targeting = build_targeting
  job_invocation.triggering = build_triggering
  job_invocation.pattern_template_invocations = build_template_invocations
  job_invocation.description_format = params[:description_format]
  job_invocation.time_span = params[:concurrency_control][:time_span].to_i unless params[:concurrency_control][:time_span].blank?
  job_invocation.concurrency_level = params[:concurrency_control][:level].to_i unless params[:concurrency_control][:level].blank?

  self
end

#displayed_provider_typesObject



355
356
357
358
# File 'app/models/job_invocation_composer.rb', line 355

def displayed_provider_types
  # TODO available_provider_types based on targets
  available_provider_types
end

#displayed_search_queryObject



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'app/models/job_invocation_composer.rb', line 372

def displayed_search_query
  if @search_query.present?
    @search_query
  elsif host_ids.present?
    Targeting.build_query_from_hosts(host_ids)
  elsif targeting.bookmark_id
    if (bookmark = available_bookmarks.find_by(:id => targeting.bookmark_id))
      bookmark.query
    else
      ''
    end
  else
    ''
  end
end

#job_template_idsObject



420
421
422
# File 'app/models/job_invocation_composer.rb', line 420

def job_template_ids
  job_invocation.pattern_template_invocations.map(&:template_id)
end

#needs_provider_type_selection?Boolean

Returns:

  • (Boolean)


351
352
353
# File 'app/models/job_invocation_composer.rb', line 351

def needs_provider_type_selection?
  available_provider_types.size > 1
end

#preselected_template_for_provider(provider_type) ⇒ Object



368
369
370
# File 'app/models/job_invocation_composer.rb', line 368

def preselected_template_for_provider(provider_type)
  (templates_for_provider(provider_type) & selected_job_templates).first
end

#saveObject



319
320
321
# File 'app/models/job_invocation_composer.rb', line 319

def save
  valid? && job_invocation.save
end

#save!Object



323
324
325
326
327
328
329
# File 'app/models/job_invocation_composer.rb', line 323

def save!
  if valid?
    job_invocation.save!
  else
    raise job_invocation.flattened_validation_exception
  end
end

#selected_job_templatesObject



364
365
366
# File 'app/models/job_invocation_composer.rb', line 364

def selected_job_templates
  available_templates_for(job_category).where(:id => job_template_ids)
end

#targeted_hostsObject



392
393
394
395
396
397
398
# File 'app/models/job_invocation_composer.rb', line 392

def targeted_hosts
  if displayed_search_query.blank?
    Host.where('1 = 0')
  else
    Host.authorized(Targeting::RESOLVE_PERMISSION, Host).search_for(displayed_search_query)
  end
end

#targeted_hosts_countObject



400
401
402
403
404
# File 'app/models/job_invocation_composer.rb', line 400

def targeted_hosts_count
  targeted_hosts.count
rescue
  0
end

#template_invocation(job_template) ⇒ Object



406
407
408
# File 'app/models/job_invocation_composer.rb', line 406

def template_invocation(job_template)
  pattern_template_invocations.find { |invocation| invocation.template == job_template }
end

#template_invocation_input_value_for(job_template, input) ⇒ Object



410
411
412
413
414
415
416
417
418
# File 'app/models/job_invocation_composer.rb', line 410

def template_invocation_input_value_for(job_template, input)
  invocations = pattern_template_invocations
  default = TemplateInvocationInputValue.new
  if (invocation = invocations.detect { |i| i.template_id == job_template.id })
    invocation.input_values.detect { |iv| iv.template_input_id == input.id } || default
  else
    default
  end
end

#templates_for_provider(provider_type) ⇒ Object



360
361
362
# File 'app/models/job_invocation_composer.rb', line 360

def templates_for_provider(provider_type)
  available_templates_for(job_category).select { |t| t.provider_type == provider_type }
end

#trigger(raise_on_error = false) ⇒ Object



300
301
302
303
304
305
306
307
308
# File 'app/models/job_invocation_composer.rb', line 300

def trigger(raise_on_error = false)
  generate_description
  if raise_on_error
    save!
  else
    return false unless save
  end
  triggering.trigger(::Actions::RemoteExecution::RunHostsJob, job_invocation)
end

#trigger!Object



310
311
312
# File 'app/models/job_invocation_composer.rb', line 310

def trigger!
  trigger(true)
end

#valid?Boolean

Returns:

  • (Boolean)


314
315
316
317
# File 'app/models/job_invocation_composer.rb', line 314

def valid?
  targeting.valid? & job_invocation.valid? & !pattern_template_invocations.map(&:valid?).include?(false) &
    triggering.valid?
end