Class: JobInvocationComposer

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

Defined Under Namespace

Classes: ApiParams, FeatureNotFound, HostIdsTranslator, JobTemplateNotFound, 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.



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

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

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

Instance Attribute Details

#host_idsObject

Returns the value of attribute host_ids.



371
372
373
# File 'app/models/job_invocation_composer.rb', line 371

def host_ids
  @host_ids
end

#job_invocationObject

Returns the value of attribute job_invocation.



371
372
373
# File 'app/models/job_invocation_composer.rb', line 371

def job_invocation
  @job_invocation
end

#paramsObject

Returns the value of attribute params.



371
372
373
# File 'app/models/job_invocation_composer.rb', line 371

def params
  @params
end

#rerunsObject (readonly)

Returns the value of attribute reruns.



372
373
374
# File 'app/models/job_invocation_composer.rb', line 372

def reruns
  @reruns
end

#search_queryObject

Returns the value of attribute search_query.



371
372
373
# File 'app/models/job_invocation_composer.rb', line 371

def search_query
  @search_query
end

Class Method Details

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



399
400
401
# File 'app/models/job_invocation_composer.rb', line 399

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



395
396
397
# File 'app/models/job_invocation_composer.rb', line 395

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

.from_job_invocation(job_invocation, params = {}) ⇒ Object



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

def self.from_job_invocation(job_invocation, params = {})
  self.new(ParamsFromJobInvocation.new(job_invocation, params).params)
end

.from_ui_params(ui_params) ⇒ Object



391
392
393
# File 'app/models/job_invocation_composer.rb', line 391

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

Instance Method Details

#available_bookmarksObject



530
531
532
# File 'app/models/job_invocation_composer.rb', line 530

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

#available_job_categoriesObject



471
472
473
# File 'app/models/job_invocation_composer.rb', line 471

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

#available_provider_typesObject



475
476
477
# File 'app/models/job_invocation_composer.rb', line 475

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

#available_template_inputsObject



479
480
481
# File 'app/models/job_invocation_composer.rb', line 479

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



463
464
465
# File 'app/models/job_invocation_composer.rb', line 463

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

#available_templates_for(job_category) ⇒ Object



467
468
469
# File 'app/models/job_invocation_composer.rb', line 467

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

#composeObject



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'app/models/job_invocation_composer.rb', line 403

def compose
  job_invocation.job_category = validate_job_category(params[:job_category])
  job_invocation.job_category ||= resolve_job_category(available_job_categories.first) { |template| template.job_category } if @set_defaults
  job_invocation.remote_execution_feature_id = params[:remote_execution_feature_id]
  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 if params[:concurrency_control][:time_span].present?
  job_invocation.concurrency_level = params[:concurrency_control][:level].to_i if params[:concurrency_control][:level].present?
  job_invocation.execution_timeout_interval = params[:execution_timeout_interval]
  job_invocation.password = params[:password]
  job_invocation.key_passphrase = params[:key_passphrase]
  job_invocation.effective_user_password = params[:effective_user_password]
  job_invocation.ssh_user = params[:ssh_user]

  if @reruns && job_invocation.targeting.static?
    job_invocation.targeting.assign_host_ids(JobInvocation.find(@reruns).targeting.host_ids)
    job_invocation.targeting.mark_resolved!
  end

  job_invocation.job_category = nil unless rerun_possible?

  self
end

#displayed_provider_typesObject



487
488
489
490
# File 'app/models/job_invocation_composer.rb', line 487

def displayed_provider_types
  # TODO available_provider_types based on targets
  available_provider_types
end

#displayed_search_queryObject



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'app/models/job_invocation_composer.rb', line 514

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

#input_value_for(input) ⇒ Object



552
553
554
555
556
# File 'app/models/job_invocation_composer.rb', line 552

def input_value_for(input)
  invocations = pattern_template_invocations
  default = TemplateInvocationInputValue.new(:template_input_id => input.id, :value => input.default)
  invocations.map(&:input_values).flatten.detect { |iv| iv.template_input_id == input.id } || default
end

#job_template_idsObject



558
559
560
# File 'app/models/job_invocation_composer.rb', line 558

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

#needs_provider_type_selection?Boolean

Returns:

  • (Boolean)


483
484
485
# File 'app/models/job_invocation_composer.rb', line 483

def needs_provider_type_selection?
  available_provider_types.size > 1
end

#preselected_template_for_provider(provider_type) ⇒ Object



500
501
502
# File 'app/models/job_invocation_composer.rb', line 500

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

#rerun_possible?Boolean

Returns:

  • (Boolean)


562
563
564
# File 'app/models/job_invocation_composer.rb', line 562

def rerun_possible?
  !(reruns && job_invocation.pattern_template_invocations.empty?)
end

#resolve_job_category(default_category) ⇒ Object



504
505
506
# File 'app/models/job_invocation_composer.rb', line 504

def resolve_job_category(default_category)
  resolve_for_composer(default_category) { |form_template| form_template.job_category }
end

#resolve_job_template(provider_templates) ⇒ Object



508
509
510
511
512
# File 'app/models/job_invocation_composer.rb', line 508

def resolve_job_template(provider_templates)
  resolve_for_composer(provider_templates.first) do |form_template|
    provider_templates.include?(form_template) ? form_template : provider_templates.first
  end
end

#saveObject



451
452
453
# File 'app/models/job_invocation_composer.rb', line 451

def save
  valid? && job_invocation.save
end

#save!Object



455
456
457
458
459
460
461
# File 'app/models/job_invocation_composer.rb', line 455

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

#selected_job_templatesObject



496
497
498
# File 'app/models/job_invocation_composer.rb', line 496

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

#targeted_hostsObject



534
535
536
537
538
539
540
# File 'app/models/job_invocation_composer.rb', line 534

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

#targeted_hosts_countObject



542
543
544
545
546
# File 'app/models/job_invocation_composer.rb', line 542

def targeted_hosts_count
  targeted_hosts.count
rescue
  0
end

#template_invocation(job_template) ⇒ Object



548
549
550
# File 'app/models/job_invocation_composer.rb', line 548

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

#templates_for_provider(provider_type) ⇒ Object



492
493
494
# File 'app/models/job_invocation_composer.rb', line 492

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



429
430
431
432
433
434
435
436
437
# File 'app/models/job_invocation_composer.rb', line 429

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



439
440
441
# File 'app/models/job_invocation_composer.rb', line 439

def trigger!
  trigger(true)
end

#valid?Boolean

Returns:

  • (Boolean)


443
444
445
446
447
448
449
# File 'app/models/job_invocation_composer.rb', line 443

def valid?
  unless triggering.valid?
    job_invocation.errors.add(:triggering, 'is invalid')
    return false
  end
  targeting.valid? & job_invocation.valid? & !pattern_template_invocations.map(&:valid?).include?(false)
end