Project

General

Profile

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

foreman_pipeline / app / controllers / foreman_pipeline / api / jobs_controller.rb @ a7978929

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
    # TODO?: refactor and remove repetitive methods -> map set actions onto update
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
      available = [@job.environment.full_path] rescue []
170

    
171
      paths = available.inject([]) do |result, path|
172
        result << { :environments => path }
173
      end
174
      paths = [{ :environments => [] }] if paths.empty?
175

    
176
      collection = {
177
        :results => paths,
178
        :total => paths.size,
179
        :subtotal => paths.size
180
      }
181
      
182
      respond_for_index(:collection => collection)
183
    end
184

    
185
    api :GET, "/organizations/:organization_id/jobs/:id/available_resources", N_("List compute resources available for the job")
186
    param_group :job_id
187
    def available_resources
188
      @compute_resources = @job.available_compute_resources
189
      render "api/v2/compute_resources/index"
190
    end
191

    
192
    api :GET, "/organizations/:organization_id/jobs/:id/run_job", N_("Start job execution a job")
193
    param_group :job_id
194
    def run_job      
195
      if @job.manual_trigger
196
        task = async_task(::Actions::ForemanPipeline::Job::RunJobManually, @job)
197
        render :nothing => true            
198
      else
199
        fail ::Katello::HttpErrors::Forbidden, "Running manually not allowed for Job: #{@job.name}. Try setting it's :manual_trigger property."
200
      end
201
    end
202

    
203
    api :GET, "/organizations/:organization_id/jobs/:id/add_projects", N_("Add jenkins projects to the job")
204
    param_group :job_id
205
    param :projects, Array, :desc => N_("Names of the jenkins projects to be added to the job")
206
    def add_projects
207
      rollback = {:occured => false}
208
      Job.transaction do
209
        projects = params[:projects].map do |p|
210
          JenkinsProject.create(:name => p, :organization => @organization)
211
        end
212
        projects_to_add = projects.delete_if { |p| @job.jenkins_projects.include? p }
213
        @job.jenkins_projects = @job.jenkins_projects + projects_to_add
214
        @job.save!
215

    
216
        projects_to_add.each do |project|
217
          project.reload
218
          task = sync_task(::Actions::ForemanPipeline::Jenkins::GetBuildParams, :job_id => @job.id, :name => project.name)
219
          
220
          unless task.output[:build_params]
221
            raise ActiveRecord::Rollback
222
            rollback[:occured] = true
223
            rollback[:project_name] = project.name
224
          end
225
          task.output[:build_params].each do |param|
226
            new_param = JenkinsProjectParam.new(:name => param[:name],
227
                                                :type => param[:type],
228
                                                :description => param[:description],
229
                                                :value => param[:default])
230
            new_param.organization = @organization
231
            new_param.jenkins_project = project
232
            new_param.save!
233
          end
234
        end
235
      end
236
      if rollback[:occured]
237
        fail ::Katello::HttpErrors::NotFound, "Could not retrieve build params for Jenkins project: #{rollback[:project_name]}"
238
      else
239
        respond_for_show        
240
      end
241
    end
242

    
243
    api :GET, "/organizations/:organization_id/jobs/:id/remove_projects", N_("Remove jenkins projects from the job")
244
    param_group :job_id
245
    param :projects, Array, :desc => N_("Identifiers of the projects to be removed from the job")
246
    def remove_projects
247
      ids = params[:project_ids]
248
      jj_projects = JobJenkinsProject.where(:jenkins_project_id => ids)
249
      jj_projects.map(&:destroy)
250
      respond_for_show
251
    end
252

    
253
    protected
254

    
255
    def find_job
256
      @job = Job.find_by_id(params[:id])
257
      fail ::Katello::HttpErrors::NotFound, "Could not find job with id #{params[:id]}" if @job.nil?
258
      @job 
259
    end
260

    
261
    def job_params
262
      params.require(:job).permit(:name, :manual_trigger, :sync_trigger, :levelup_trigger, :projects, :promote)
263
    end
264

    
265
    def format_paths(paths)
266
      formated_paths = paths.inject([]) do |result, path|
267
        result << { :environments => path }
268
      end
269
      collection = {
270
        :results => formated_paths,
271
        :total => formated_paths.size,
272
        :subtotal => formated_paths.size
273
      }
274
      respond_for_index(:collection => collection)
275
    end
276

    
277
  end
278
end