1
|
module ForemanPipeline
|
2
|
class Api::JobsController < Katello::Api::V2::ApiController
|
3
|
respond_to :json
|
4
|
|
5
|
include Api::Rendering
|
6
|
|
7
|
before_filter :find_organization, :only => [:create, :index, :add_projects, :available_paths]
|
8
|
|
9
|
before_filter :find_job, :only => [:update, :show, :destroy, :set_content_view,
|
10
|
:set_hostgroup, :set_resource, :available_resources,
|
11
|
:set_jenkins, :set_environment, :run_job,
|
12
|
:add_projects, :remove_projects, :set_paths,
|
13
|
:remove_paths, :add_paths, :current_paths, :available_paths]
|
14
|
|
15
|
before_filter :load_search_service, :only => [:index]
|
16
|
|
17
|
def_param_group :job do
|
18
|
param :name, String, :desc => N_("Name of the job"), :required => true
|
19
|
param :manual_trigger, :bool, :desc => N_("Allow starting job manually"), :required => false
|
20
|
param :sync_trigger, :bool, :desc => N_("Allow starting job on successful repo sync"), :required => false
|
21
|
param :levelup_trigger, :bool, :desc => N_("Allow starting job on successful content view publish/promote"), :required => false
|
22
|
param :promote, :bool, :desc => N_("Allow starting job manually"), :required => false
|
23
|
end
|
24
|
|
25
|
def_param_group :job_id do
|
26
|
param :organization_id, :number, :desc => N_("organization identifier"), :required => true
|
27
|
param :id, :number, :desc => N_("job identifier"), :required => true
|
28
|
end
|
29
|
|
30
|
api :GET, "/organizations/:organization_id/jobs", N_("List jobs")
|
31
|
param :organization_id, :number, :desc => N_("organization identifier"), :required => true
|
32
|
param :name, String, :desc => N_("Name of the job")
|
33
|
def index
|
34
|
ids = Job.readable.where(:organization_id => @organization.id).pluck(:id)
|
35
|
filters = [:terms => {:id => ids}]
|
36
|
filters << {:term => {:name => params[:name]}} if params[:name]
|
37
|
|
38
|
options = {
|
39
|
:filters => filters,
|
40
|
:load_records? => true
|
41
|
}
|
42
|
respond_for_index(:collection => item_search(Job, params, options))
|
43
|
end
|
44
|
|
45
|
api :GET, "/organizations/:organization_id/jobs/:id", N_("Get job by identifier")
|
46
|
param_group :job_id
|
47
|
def show
|
48
|
respond_for_show(:resource => @job)
|
49
|
end
|
50
|
|
51
|
api :POST, "/organizations/:organization_id/jobs/", N_("Create new job")
|
52
|
param :organization_id, :number, :desc => N_("Organization identifier"), :required => true
|
53
|
param_group :job
|
54
|
def create
|
55
|
@job = Job.new(job_params)
|
56
|
@job.organization = @organization
|
57
|
@job.save!
|
58
|
|
59
|
respond_for_show(:resource => @job)
|
60
|
end
|
61
|
|
62
|
api :PUT, "/organizations/:organization_id/jobs/:id", N_("Update job")
|
63
|
param_group :job_id
|
64
|
param_group :job
|
65
|
def update
|
66
|
@job.update_attributes!(job_params)
|
67
|
@job.save!
|
68
|
respond_for_show(:resource => @job)
|
69
|
end
|
70
|
|
71
|
api :DELETE, "/organizations/:organization_id/jobs/:id", N_("Delete job")
|
72
|
param_group :job_id
|
73
|
def destroy
|
74
|
@job.destroy
|
75
|
respond_for_show(:resource => @job)
|
76
|
end
|
77
|
|
78
|
|
79
|
api :PUT, "/organizations/:organization_id/jobs/:id/set_content_view", N_("Set content view for job")
|
80
|
param_group :job_id
|
81
|
param :content_view_id, :number, :desc => N_("Content view id which will be set"), :required => true
|
82
|
def set_content_view
|
83
|
@job.content_view = Katello::ContentView.find(params[:content_view_id])
|
84
|
@job.save!
|
85
|
respond_for_show
|
86
|
end
|
87
|
|
88
|
api :PUT, "/organizations/:organization_id/jobs/:id/set_hostgroup", N_("Set hostgroup for job")
|
89
|
param_group :job_id
|
90
|
param :hostgroup_id, :number, :desc => N_("Hostgroup id which will be set"), :required => true
|
91
|
def set_hostgroup
|
92
|
@job.hostgroup = Hostgroup.find(params[:hostgroup_id])
|
93
|
@job.compute_resource = nil
|
94
|
@job.save!
|
95
|
respond_for_show
|
96
|
end
|
97
|
|
98
|
api :PUT, "/organizations/:organization_id/jobs/:id/set_jenkins", N_("Set jenkins instance for job")
|
99
|
param_group :job_id
|
100
|
param :jenkins_instance_id, :number, :desc => N_("Jenkins Instance id which will be set"), :required => true
|
101
|
def set_jenkins
|
102
|
instance = JenkinsInstance.find(params[:jenkins_instance_id])
|
103
|
@job.jenkins_instance = instance
|
104
|
@job.save!
|
105
|
respond_for_show
|
106
|
end
|
107
|
|
108
|
api :PUT, "/organizations/:organization_id/jobs/:id/set_environment", N_("Set environment for job")
|
109
|
param_group :job_id
|
110
|
param :environment_id, :number, :desc => N_("Environment id which will be set"), :required => true
|
111
|
def set_environment
|
112
|
@job.environment = Katello::KTEnvironment.find(params[:environment_id])
|
113
|
@job.save!
|
114
|
respond_for_show
|
115
|
end
|
116
|
|
117
|
api :PUT, "/organizations/:organization_id/jobs/:id/set_resource", N_("Set compute resource for job")
|
118
|
param_group :job_id
|
119
|
param :resource_id, :number, :desc => N_("Compute resource id which will be set"), :required => true
|
120
|
def set_resource
|
121
|
if @job.available_compute_resources.map(&:id).include? params[:resource_id]
|
122
|
@job.compute_resource = ComputeResource.find(params[:resource_id])
|
123
|
@job.save!
|
124
|
respond_for_show
|
125
|
else
|
126
|
fail Katello::HttpErrors::Conflict, "Only a Compute Resource configured for Job's Hostgroup may be set."
|
127
|
end
|
128
|
end
|
129
|
|
130
|
api :PUT, "/organizations/:organization_id/jobs/:id/add_paths", N_("Add environment paths for job")
|
131
|
param_group :job_id
|
132
|
param :path_ids, Array, :desc => N_("Identifiers of environments which are library's successors in corresponding paths")
|
133
|
def add_paths
|
134
|
@job.path_ids = (@job.path_ids + params[:path_ids]).uniq
|
135
|
@job.save!
|
136
|
respond_for_show
|
137
|
end
|
138
|
|
139
|
api :PUT, "/organizations/:organization_id/jobs/:id/remove_paths", N_("Remove environment paths for job")
|
140
|
param_group :job_id
|
141
|
param :path_ids, Array, :desc => N_("Identifiers of environments which are library's successors in corresponding paths")
|
142
|
def remove_paths
|
143
|
@job.path_ids = (@job.path_ids - params[:path_ids]).uniq
|
144
|
@job.environment = nil unless @job.environment_in_paths? @job.environment_id
|
145
|
@job.save!
|
146
|
respond_for_show
|
147
|
end
|
148
|
|
149
|
api :GET, "/organizations/:organization_id/jobs/:id/current_paths", N_("List environment paths of a job")
|
150
|
param_group :job_id
|
151
|
def current_paths
|
152
|
paths = @job.paths.map(&:full_path)
|
153
|
|
154
|
current = paths.inject([]) do |result, path|
|
155
|
result << { :environments => path }
|
156
|
end
|
157
|
|
158
|
collection = {
|
159
|
:results => current,
|
160
|
:total => current.size,
|
161
|
:subtotal => current.size
|
162
|
}
|
163
|
respond_for_index(:collection => collection)
|
164
|
end
|
165
|
|
166
|
api :GET, "/organizations/:organization_id/jobs/:id/available_paths", N_("List environment paths available for a job")
|
167
|
param_group :job_id
|
168
|
def available_paths
|
169
|
all_paths = @organization.promotion_paths.map(&:shift)
|
170
|
available = (all_paths - @job.paths).map(&:full_path)
|
171
|
|
172
|
paths = available.inject([]) do |result, path|
|
173
|
result << { :environments => path }
|
174
|
end
|
175
|
|
176
|
collection = {
|
177
|
:results => paths,
|
178
|
:total => paths.size,
|
179
|
:subtotal => paths.size
|
180
|
}
|
181
|
respond_for_index(:collection => collection)
|
182
|
end
|
183
|
|
184
|
api :GET, "/organizations/:organization_id/jobs/:id/available_resources", N_("List compute resources available for the job")
|
185
|
param_group :job_id
|
186
|
def available_resources
|
187
|
@compute_resources = @job.available_compute_resources
|
188
|
render "api/v2/compute_resources/index"
|
189
|
end
|
190
|
|
191
|
api :GET, "/organizations/:organization_id/jobs/:id/run_job", N_("Start job execution a job")
|
192
|
param_group :job_id
|
193
|
def run_job
|
194
|
if @job.manual_trigger
|
195
|
task = async_task(::Actions::ForemanPipeline::Job::RunJobManually, @job)
|
196
|
render :nothing => true
|
197
|
else
|
198
|
fail ::Katello::HttpErrors::Forbidden, "Running manually not allowed for Job: #{@job.name}. Try setting it's :manual_trigger property."
|
199
|
end
|
200
|
end
|
201
|
|
202
|
api :GET, "/organizations/:organization_id/jobs/:id/add_projects", N_("Add jenkins projects to the job")
|
203
|
param_group :job_id
|
204
|
param :projects, Array, :desc => N_("Names of the jenkins projects to be added to the job")
|
205
|
def add_projects
|
206
|
rollback = {:occured => false}
|
207
|
Job.transaction do
|
208
|
projects = params[:projects].map do |p|
|
209
|
JenkinsProject.create(:name => p, :organization => @organization)
|
210
|
end
|
211
|
projects_to_add = projects.delete_if { |p| @job.jenkins_projects.include? p }
|
212
|
@job.jenkins_projects = @job.jenkins_projects + projects_to_add
|
213
|
@job.save!
|
214
|
|
215
|
projects_to_add.each do |project|
|
216
|
project.reload
|
217
|
task = sync_task(::Actions::ForemanPipeline::Jenkins::GetBuildParams, :job_id => @job.id, :name => project.name)
|
218
|
|
219
|
unless task.output[:build_params]
|
220
|
raise ActiveRecord::Rollback
|
221
|
rollback[:occured] = true
|
222
|
rollback[:project_name] = project.name
|
223
|
end
|
224
|
task.output[:build_params].each do |param|
|
225
|
new_param = JenkinsProjectParam.new(:name => param[:name],
|
226
|
:type => param[:type],
|
227
|
:description => param[:description],
|
228
|
:value => param[:default])
|
229
|
new_param.organization = @organization
|
230
|
new_param.jenkins_project = project
|
231
|
new_param.save!
|
232
|
end
|
233
|
end
|
234
|
end
|
235
|
if rollback[:occured]
|
236
|
fail ::Katello::HttpErrors::NotFound, "Could not retrieve build params for Jenkins project: #{rollback[:project_name]}"
|
237
|
else
|
238
|
respond_for_show
|
239
|
end
|
240
|
end
|
241
|
|
242
|
api :GET, "/organizations/:organization_id/jobs/:id/remove_projects", N_("Remove jenkins projects from the job")
|
243
|
param_group :job_id
|
244
|
param :projects, Array, :desc => N_("Identifiers of the projects to be removed from the job")
|
245
|
def remove_projects
|
246
|
ids = params[:project_ids]
|
247
|
jj_projects = JobJenkinsProject.where(:jenkins_project_id => ids)
|
248
|
jj_projects.map(&:destroy)
|
249
|
respond_for_show
|
250
|
end
|
251
|
|
252
|
protected
|
253
|
|
254
|
def find_job
|
255
|
@job = Job.find_by_id(params[:id])
|
256
|
fail ::Katello::HttpErrors::NotFound, "Could not find job with id #{params[:id]}" if @job.nil?
|
257
|
@job
|
258
|
end
|
259
|
|
260
|
def job_params
|
261
|
params.require(:job).permit(:name, :manual_trigger, :sync_trigger, :levelup_trigger, :projects, :promote)
|
262
|
end
|
263
|
|
264
|
def format_paths(paths)
|
265
|
formated_paths = paths.inject([]) do |result, path|
|
266
|
result << { :environments => path }
|
267
|
end
|
268
|
collection = {
|
269
|
:results => formated_paths,
|
270
|
:total => formated_paths.size,
|
271
|
:subtotal => formated_paths.size
|
272
|
}
|
273
|
respond_for_index(:collection => collection)
|
274
|
end
|
275
|
|
276
|
end
|
277
|
end
|