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



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
179
180
181
182
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 148

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



91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 91

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

#hostsObject



120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 120

def hosts
  set_hosts_and_template_invocations
  @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
  set_statuses_and_smart_proxies
  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



107
108
109
110
111
112
113
114
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 107

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



205
206
207
208
209
210
211
212
213
214
215
216
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 205

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



135
136
137
138
139
140
141
142
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 135

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



188
189
190
191
192
193
194
195
196
197
198
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 188

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



218
219
220
221
222
223
224
225
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 218

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

#showObject



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

def show
  @pattern_template_invocations = @job_invocation.pattern_template_invocations.includes(:input_values)
  @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 include_hosts?
    set_hosts_and_template_invocations
    set_statuses_and_smart_proxies if Foreman::Cast.to_bool(params[:host_status])
  end
end