Class: Api::V2::JobInvocationsController

Inherits:
BaseController
  • Object
show all
Includes:
Api::Version2, Foreman::Renderer, RemoteExecutionHelper
Defined in:
app/controllers/api/v2/job_invocations_controller.rb

Instance Method Summary collapse

Methods included from RemoteExecutionHelper

#description_checkbox_f, #description_format_help, #description_format_textarea_f, #host_counter, #host_tasks_authorizer, #invocation_count, #invocation_description, #invocation_result, #job_hosts_authorizer, #job_invocation_task_buttons, #job_invocations_buttons, #job_report_template, #job_report_template_parameters, #link_to_invocation_task_if_authorized, #load_template_from_task, #normalize_line_sets, #preview_box, #providers_options, #remote_execution_provider_for, #targeting_hosts, #template_invocation_actions, #template_invocation_status, #template_invocation_task_buttons, #time_in_words_span

Instance Method Details

#cancelObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 144

def cancel
  force = params.fetch('force', false)
  search = params[:search]

  if search.present?
    begin
      hosts_scope = @job_invocation.targeting.hosts.authorized(:view_hosts, Host)
      matching_hosts = hosts_scope.search_for(search)
      tasks_to_process = @job_invocation.template_invocations
                                        .where(host_id: matching_hosts.select(:id))
                                        .includes(:run_host_job_task)

      cancelled, skipped = tasks_to_process.partition { |ti| ti.run_host_job_task&.cancellable? }
      cancelled.each do |ti|
        if force
          ti.run_host_job_task.abort
        else
          ti.run_host_job_task.cancel
        end
      end
      render json: {
        total: tasks_to_process.size,
        cancelled: cancelled.map(&:run_host_job_task_id),
        skipped: skipped.map(&:run_host_job_task_id),
      }
    rescue StandardError => e
      render json: { error: { message: "Failed to cancel tasks on hosts: #{e.message}" } }, status: :unprocessable_entity
    end
  elsif @job_invocation.task.cancellable?
    result = @job_invocation.cancel(force)
    render json: { cancelled: result, id: @job_invocation.id }
  else
    render json: { message: _('The job could not be cancelled.') }, status: :unprocessable_entity
  end
end

#createObject



87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 87

def create
  composer = JobInvocationComposer.from_api_params(
    job_invocation_params
  )
  composer.trigger!
  @job_invocation = composer.job_invocation
  @hosts = @job_invocation.targeting.hosts
  process_response @job_invocation
rescue JobInvocationComposer::JobTemplateNotFound, JobInvocationComposer::FeatureNotFound => e
  not_found(error: { message: e.message })
end

#hostsObject



116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 116

def hosts
  set_hosts_and_template_invocations
  set_statuses_and_smart_proxies
  @total = @hosts.size
  @hosts = @hosts.search_for(params[:search], :order => params[:order]).paginate(:page => params[:page], :per_page => params[:per_page])
  @subtotal = @hosts.total_entries
  if params[:awaiting]
    @hosts = @hosts.select { |host| @host_statuses[host.id] == 'N/A' }
  end
  render :hosts, :layout => 'api/v2/layouts/index_layout'
end

#indexObject



16
17
18
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 16

def index
  @job_invocations = resource_scope_for_index
end

#outputObject



103
104
105
106
107
108
109
110
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 103

def output
  if @nested_obj.task.scheduled?
    render :json => delayed_task_output(@nested_obj.task, :default => [])
    return
  end

  render :json => host_output(@nested_obj, @host, :default => [], :since => params[:since])
end

#outputsObject



201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 201

def outputs
  hosts = @job_invocation.targeting.hosts.authorized(:view_hosts, Host)
  hosts = hosts.search_for(params['search_query']) if params['search_query']
  raw = ActiveRecord::Type::Boolean.new.cast params['raw']
  default_value = raw ? '' : []
  outputs = hosts.map do |host|
    host_output(@job_invocation, host, :default => default_value, :since => params['since'], :raw => raw)
      .merge(host_id: host.id)
  end

  render :json => { :outputs => outputs }
end

#raw_outputObject



131
132
133
134
135
136
137
138
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 131

def raw_output
  if @nested_obj.task.scheduled?
    render :json => delayed_task_output(@nested_obj.task)
    return
  end

  render :json => host_output(@nested_obj, @host, :raw => true)
end

#rerunObject



184
185
186
187
188
189
190
191
192
193
194
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 184

def rerun
  composer = JobInvocationComposer.from_job_invocation(@job_invocation, params)
  if composer.rerun_possible?
    composer.trigger!
    @job_invocation = composer.job_invocation
    process_response @job_invocation
  else
    render :json => { :error => _('Could not rerun job %{id} because its template could not be found') % { :id => composer.reruns } },
      :status => :not_found
  end
end

#resource_name(resource = controller_name) ⇒ Object



214
215
216
217
218
219
220
221
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 214

def resource_name(resource = controller_name)
  case resource
  when 'organization', 'location'
    nil
  else
    'job_invocation'
  end
end

#showObject



23
24
25
26
27
28
29
30
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 23

def show
  set_hosts_and_template_invocations
  @job_organization = Taxonomy.find_by(id: @job_invocation.task.input[:current_organization_id])
  @job_location = Taxonomy.find_by(id: @job_invocation.task.input[:current_location_id])
  if params[:host_status] == 'true'
    set_statuses_and_smart_proxies
  end
end