Project

General

Profile

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

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

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_to_environments,
13
                                       :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
    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
    def set_content_view
82
      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
      @job.save!
86
      respond_for_show
87
    end
88

    
89
    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
    def set_hostgroup
93
      @job.hostgroup = Hostgroup.find(params[:hostgroup_id])
94
      @job.compute_resource = nil
95
      @job.save!
96
      respond_for_show
97
    end
98

    
99
    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
    def set_jenkins
103
      instance = JenkinsInstance.find(params[:jenkins_instance_id])
104
      @job.jenkins_instance = instance
105
      @job.save!
106
      respond_for_show
107
    end
108
    
109
    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
    def set_environment
113
      @job.environment = Katello::KTEnvironment.find(params[:environment_id])
114
      @job.to_environments = []
115
      @job.save!
116
      respond_for_show
117
    end    
118

    
119
    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
    def set_resource
123
      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
    end
131

    
132
    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
    def set_to_environments
136
      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
      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
        @job.to_environment_ids = params[:to_environment_ids]
143
        @job.save!
144
        respond_for_show
145
      else
146
        fail Katello::HttpErrors::Conflict, "Only environments that are direct successors of Job's Environment may be set as 'to environments'." 
147
      end
148
    end
149

    
150
    api :GET, "/organizations/:organization_id/jobs/:id/available_paths", N_("List environment paths available for a job")
151
    param_group :job_id
152
    def available_paths
153
      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
      end
168
      
169
      collection = {
170
        :results => paths,
171
        :total => paths.size,
172
        :subtotal => paths.size
173
      }
174

    
175
      respond_for_index(:collection => collection)
176
    end
177

    
178
    api :GET, "/organizations/:organization_id/jobs/:id/available_resources", N_("List compute resources available for the job")
179
    param_group :job_id
180
    def available_resources
181
      @compute_resources = @job.available_compute_resources
182
      render "api/v2/compute_resources/index"
183
    end
184

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

    
196
    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
    def add_projects
200
      rollback = {:occured => false}
201
      Job.transaction do
202
        projects = params[:projects].map do |p|
203
          JenkinsProject.create(:name => p, :organization => @organization)
204
        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
          task = sync_task(::Actions::ForemanPipeline::Jenkins::GetBuildParams, :job_id => @job.id, :name => project.name)
212
          
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
            new_param.jenkins_project = project
225
            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
        respond_for_show
233
      end
234
    end
235

    
236
    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
    def remove_projects
240
      ids = params[:project_ids]
241
      jj_projects = JobJenkinsProject.where(:jenkins_project_id => ids)
242
      jj_projects.map(&:destroy)
243
      respond_for_show
244
    end
245

    
246
    protected
247

    
248
    def find_job
249
      @job = Job.find_by_id(params[:id])
250
      fail ::Katello::HttpErrors::NotFound, "Could not find job with id #{params[:id]}" if @job.nil?
251
      @job 
252
    end
253

    
254
    def job_params
255
      params.require(:job).permit(:name, :manual_trigger, :sync_trigger, :levelup_trigger, :projects, :promote)
256
    end
257

    
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

    
270
  end
271
end