Project

General

Profile

Download (10.6 KB) Statistics
| Branch: | Tag: | Revision:

foreman_pipeline / app / controllers / foreman_pipeline / api / jobs_controller.rb @ 937eb715

1 be52ca64 Ondrej Prazak
module ForemanPipeline
2 ea1ab5c2 Ondrej Prazak
  class Api::JobsController < Katello::Api::V2::ApiController
3
    respond_to :json
4
5 de23393f Ondrej Prazak
    include Api::Rendering
6
7 d410fbc2 Ondrej Prazak
    before_filter :find_organization, :only => [:create, :index, :add_projects, :available_paths]
8 0a6391e7 Ondrej Prazak
9 06bb534f Ondrej Prazak
    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 abf1341e Ondrej Prazak
                                       :add_projects, :remove_projects, :set_to_environments,
13
                                       :available_paths]
14 0a6391e7 Ondrej Prazak
15 06bb534f Ondrej Prazak
    before_filter :load_search_service, :only => [:index]
16 ea1ab5c2 Ondrej Prazak
17 f3b70644 Ondrej Prazak
    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 ea1ab5c2 Ondrej Prazak
    def index
34 18680924 Ondrej Prazak
      ids = Job.readable.where(:organization_id => @organization.id).pluck(:id)
35 f3b70644 Ondrej Prazak
      filters = [:terms => {:id => ids}]
36
      filters << {:term => {:name => params[:name]}} if params[:name]
37 ea1ab5c2 Ondrej Prazak
38
      options = {
39 7b6cf472 Ondrej Prazak
         :filters => filters,
40
         :load_records? => true
41 ea1ab5c2 Ondrej Prazak
      }
42
      respond_for_index(:collection => item_search(Job, params, options))
43
    end
44
45 f3b70644 Ondrej Prazak
    api :GET, "/organizations/:organization_id/jobs/:id", N_("Get job by identifier")
46
    param_group :job_id
47 ea1ab5c2 Ondrej Prazak
    def show
48
      respond_for_show(:resource => @job)
49
    end
50
51 f3b70644 Ondrej Prazak
    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 ea1ab5c2 Ondrej Prazak
    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 f3b70644 Ondrej Prazak
    api :PUT, "/organizations/:organization_id/jobs/:id", N_("Update job")
63
    param_group :job_id
64
    param_group :job
65 ea1ab5c2 Ondrej Prazak
    def update
66
      @job.update_attributes!(job_params)
67
      @job.save!
68
      respond_for_show(:resource => @job)
69
    end
70
71 f3b70644 Ondrej Prazak
    api :DELETE, "/organizations/:organization_id/jobs/:id", N_("Delete job")
72
    param_group :job_id
73 ea1ab5c2 Ondrej Prazak
    def destroy
74
      @job.destroy
75
      respond_for_show(:resource => @job)
76
    end
77
78 f3b70644 Ondrej Prazak
    api :PUT, "/organizations/:organization_id/jobs/:id/set_content_view", N_("Set content view for job")
79
    param_group :job_id
80
    param :content_view_id, :number, :desc => N_("Content view id which will be set"), :required => true
81 2277c4ed Ondrej Prazak
    def set_content_view
82 937eb715 Ondrej Prazak
      cv = Katello::ContentView.find(params[:content_view_id])
83
      fail Katello::HttpErrors::Conflict, "Only non-composite views are accepted" if cv.composite?
84
      @job.content_view = cv
85 2277c4ed Ondrej Prazak
      @job.save!
86
      respond_for_show
87
    end
88 ea1ab5c2 Ondrej Prazak
89 f3b70644 Ondrej Prazak
    api :PUT, "/organizations/:organization_id/jobs/:id/set_hostgroup", N_("Set hostgroup for job")
90
    param_group :job_id
91
    param :hostgroup_id, :number, :desc => N_("Hostgroup id which will be set"), :required => true
92 2adcdb99 Ondrej Prazak
    def set_hostgroup
93
      @job.hostgroup = Hostgroup.find(params[:hostgroup_id])
94 b761d3c7 Ondrej Prazak
      @job.compute_resource = nil
95 2adcdb99 Ondrej Prazak
      @job.save!
96
      respond_for_show
97
    end
98 8abd41ba Ondrej Prazak
99 f3b70644 Ondrej Prazak
    api :PUT, "/organizations/:organization_id/jobs/:id/set_jenkins", N_("Set jenkins instance for job")
100
    param_group :job_id
101
    param :jenkins_instance_id, :number, :desc => N_("Jenkins Instance id which will be set"), :required => true
102 8abd41ba Ondrej Prazak
    def set_jenkins
103 dc3d2c3e Ondrej Prazak
      instance = JenkinsInstance.find(params[:jenkins_instance_id])
104
      @job.jenkins_instance = instance
105 8abd41ba Ondrej Prazak
      @job.save!
106
      respond_for_show
107
    end
108 1d480d25 Ondrej Prazak
    
109 f3b70644 Ondrej Prazak
    api :PUT, "/organizations/:organization_id/jobs/:id/set_environment", N_("Set environment for job")
110
    param_group :job_id
111
    param :environment_id, :number, :desc => N_("Environment id which will be set"), :required => true
112 6fea9601 Ondrej Prazak
    def set_environment
113 6560f8e3 Ondrej Prazak
      @job.environment = Katello::KTEnvironment.find(params[:environment_id])
114 abf1341e Ondrej Prazak
      @job.to_environments = []
115 6560f8e3 Ondrej Prazak
      @job.save!
116
      respond_for_show
117 05ebbe77 Ondrej Prazak
    end    
118 18680924 Ondrej Prazak
119 f3b70644 Ondrej Prazak
    api :PUT, "/organizations/:organization_id/jobs/:id/set_resource", N_("Set compute resource for job")
120
    param_group :job_id
121
    param :resource_id, :number, :desc => N_("Compute resource id which will be set"), :required => true
122 25162855 Ondrej Prazak
    def set_resource
123 a79ed76a Ondrej Prazak
      if @job.available_compute_resources.map(&:id).include? params[:resource_id]
124
        @job.compute_resource = ComputeResource.find(params[:resource_id])
125
        @job.save!
126
        respond_for_show
127
      else
128
        fail Katello::HttpErrors::Conflict, "Only a Compute Resource configured for Job's Hostgroup may be set." 
129
      end
130 25162855 Ondrej Prazak
    end
131
132 d30875b6 Ondrej Prazak
    api :PUT, "/organizations/:organization_id/jobs/:id/set_to_environments", N_("Set 'to environments' for the job")
133
    param_group :job_id
134
    param :path_ids, Array, :desc => N_("Identifiers of environments which are successors of job's environment")
135 abf1341e Ondrej Prazak
    def set_to_environments
136 937eb715 Ondrej Prazak
      fail Katello::HttpErrors::Conflict, "Job's environment must be assigned before setting 'to environments'." if @job.environment.nil?
137
      fail Katello::HttpErrors::Conflict, "Job's environment does not have any successors" if @job.environment.successors.empty?
138 abf1341e Ondrej Prazak
      is_ok = params[:to_environment_ids].map do |new_id|
139
        @job.environment.successors.map(&:id).include? new_id
140
      end.all?
141
      if is_ok
142 d30875b6 Ondrej Prazak
        @job.to_environment_ids = params[:to_environment_ids]
143 abf1341e Ondrej Prazak
        @job.save!
144
        respond_for_show
145
      else
146 d30875b6 Ondrej Prazak
        fail Katello::HttpErrors::Conflict, "Only environments that are direct successors of Job's Environment may be set as 'to environments'." 
147 abf1341e Ondrej Prazak
      end
148 98442264 Ondrej Prazak
    end
149
150 f3b70644 Ondrej Prazak
    api :GET, "/organizations/:organization_id/jobs/:id/available_paths", N_("List environment paths available for a job")
151
    param_group :job_id
152 d410fbc2 Ondrej Prazak
    def available_paths
153 937eb715 Ondrej Prazak
      available = []
154
      paths = []
155
      if params[:all_paths]
156
        available = @organization.readable_promotion_paths
157
        paths = available.inject([]) do |result, path|
158
          result << { :environments => [@organization.library] + path }
159
        end
160
        paths = [{ :environments => [@organization.library] }] if paths.empty?
161
      else
162
        available = @job.environment.full_paths rescue []
163
        paths = available.inject([]) do |result, path|
164
          result << { :environments => path }
165
        end
166
        paths = [{ :environments => [] }] if paths.empty?
167 d410fbc2 Ondrej Prazak
      end
168 937eb715 Ondrej Prazak
      
169 d410fbc2 Ondrej Prazak
      collection = {
170
        :results => paths,
171
        :total => paths.size,
172
        :subtotal => paths.size
173
      }
174 6041a6ec Ondrej Prazak
175 d410fbc2 Ondrej Prazak
      respond_for_index(:collection => collection)
176
    end
177
178 f3b70644 Ondrej Prazak
    api :GET, "/organizations/:organization_id/jobs/:id/available_resources", N_("List compute resources available for the job")
179
    param_group :job_id
180 25162855 Ondrej Prazak
    def available_resources
181 a79ed76a Ondrej Prazak
      @compute_resources = @job.available_compute_resources
182 25162855 Ondrej Prazak
      render "api/v2/compute_resources/index"
183
    end
184
185 d30875b6 Ondrej Prazak
    api :GET, "/organizations/:organization_id/jobs/:id/run_job", N_("Start job execution")
186 f3b70644 Ondrej Prazak
    param_group :job_id
187 af01aa23 Ondrej Prazak
    def run_job      
188 85aedf5d Ondrej Prazak
      if @job.manual_trigger
189 be52ca64 Ondrej Prazak
        task = async_task(::Actions::ForemanPipeline::Job::RunJobManually, @job)
190 937eb715 Ondrej Prazak
        render :nothing => true
191 85aedf5d Ondrej Prazak
      else
192
        fail ::Katello::HttpErrors::Forbidden, "Running manually not allowed for Job: #{@job.name}. Try setting it's :manual_trigger property."
193
      end
194 430dff62 Ondrej Prazak
    end
195
196 f3b70644 Ondrej Prazak
    api :GET, "/organizations/:organization_id/jobs/:id/add_projects", N_("Add jenkins projects to the job")
197
    param_group :job_id
198
    param :projects, Array, :desc => N_("Names of the jenkins projects to be added to the job")
199 5b013d21 Ondrej Prazak
    def add_projects
200 986503f0 Ondrej Prazak
      rollback = {:occured => false}
201
      Job.transaction do
202
        projects = params[:projects].map do |p|
203 f3b70644 Ondrej Prazak
          JenkinsProject.create(:name => p, :organization => @organization)
204 986503f0 Ondrej Prazak
        end
205
        projects_to_add = projects.delete_if { |p| @job.jenkins_projects.include? p }
206
        @job.jenkins_projects = @job.jenkins_projects + projects_to_add
207
        @job.save!
208
209
        projects_to_add.each do |project|
210
          project.reload
211 be52ca64 Ondrej Prazak
          task = sync_task(::Actions::ForemanPipeline::Jenkins::GetBuildParams, :job_id => @job.id, :name => project.name)
212 986503f0 Ondrej Prazak
          
213
          unless task.output[:build_params]
214
            raise ActiveRecord::Rollback
215
            rollback[:occured] = true
216
            rollback[:project_name] = project.name
217
          end
218
          task.output[:build_params].each do |param|
219
            new_param = JenkinsProjectParam.new(:name => param[:name],
220
                                                :type => param[:type],
221
                                                :description => param[:description],
222
                                                :value => param[:default])
223
            new_param.organization = @organization
224 ef7883e9 Ondrej Prazak
            new_param.jenkins_project = project
225 986503f0 Ondrej Prazak
            new_param.save!
226
          end
227
        end
228
      end
229
      if rollback[:occured]
230
        fail ::Katello::HttpErrors::NotFound, "Could not retrieve build params for Jenkins project: #{rollback[:project_name]}"
231
      else
232 937eb715 Ondrej Prazak
        respond_for_show
233 5b013d21 Ondrej Prazak
      end
234
    end
235
236 f3b70644 Ondrej Prazak
    api :GET, "/organizations/:organization_id/jobs/:id/remove_projects", N_("Remove jenkins projects from the job")
237
    param_group :job_id
238
    param :projects, Array, :desc => N_("Identifiers of the projects to be removed from the job")
239 cca7afad Ondrej Prazak
    def remove_projects
240
      ids = params[:project_ids]
241 6eff8e9a Ondrej Prazak
      jj_projects = JobJenkinsProject.where(:jenkins_project_id => ids)
242
      jj_projects.map(&:destroy)
243 cca7afad Ondrej Prazak
      respond_for_show
244
    end
245
246 ea1ab5c2 Ondrej Prazak
    protected
247
248
    def find_job
249
      @job = Job.find_by_id(params[:id])
250 d0a24d0f Ondrej Prazak
      fail ::Katello::HttpErrors::NotFound, "Could not find job with id #{params[:id]}" if @job.nil?
251 ea1ab5c2 Ondrej Prazak
      @job 
252
    end
253
254
    def job_params
255 30b3ef57 Ondrej Prazak
      params.require(:job).permit(:name, :manual_trigger, :sync_trigger, :levelup_trigger, :projects, :promote)
256 ea1ab5c2 Ondrej Prazak
    end
257 d410fbc2 Ondrej Prazak
258
    def format_paths(paths)
259
      formated_paths = paths.inject([]) do |result, path|
260
        result << { :environments => path }
261
      end
262
      collection = {
263
        :results => formated_paths,
264
        :total => formated_paths.size,
265
        :subtotal => formated_paths.size
266
      }
267
      respond_for_index(:collection => collection)
268
    end
269 f3b70644 Ondrej Prazak
270 ea1ab5c2 Ondrej Prazak
  end
271
end