Project

General

Profile

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

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

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
      @job.content_view = Katello::ContentView.find(params[:content_view_id])
83
      @job.save!
84
      respond_for_show
85
    end
86

    
87
    api :PUT, "/organizations/:organization_id/jobs/:id/set_hostgroup", N_("Set hostgroup for job")
88
    param_group :job_id
89
    param :hostgroup_id, :number, :desc => N_("Hostgroup id which will be set"), :required => true
90
    def set_hostgroup
91
      @job.hostgroup = Hostgroup.find(params[:hostgroup_id])
92
      @job.compute_resource = nil
93
      @job.save!
94
      respond_for_show
95
    end
96

    
97
    api :PUT, "/organizations/:organization_id/jobs/:id/set_jenkins", N_("Set jenkins instance for job")
98
    param_group :job_id
99
    param :jenkins_instance_id, :number, :desc => N_("Jenkins Instance id which will be set"), :required => true
100
    def set_jenkins
101
      instance = JenkinsInstance.find(params[:jenkins_instance_id])
102
      @job.jenkins_instance = instance
103
      @job.save!
104
      respond_for_show
105
    end
106
    
107
    api :PUT, "/organizations/:organization_id/jobs/:id/set_environment", N_("Set environment for job")
108
    param_group :job_id
109
    param :environment_id, :number, :desc => N_("Environment id which will be set"), :required => true
110
    def set_environment
111
      @job.environment = Katello::KTEnvironment.find(params[:environment_id])
112
      @job.to_environments = []
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 set_to_environments
134
      is_ok = params[:to_environment_ids].map do |new_id|
135
        @job.environment.successors.map(&:id).include? new_id
136
      end.all?
137
      if is_ok
138
        @job.to_environment_ids
139
        @job.save!
140
        respond_for_show
141
      else
142
        fail Katello::HttpErrors::Conflict, "Only Environments that are direct successors of Job's Environment may be set as target Environments." 
143
      end
144
    end
145

    
146
    api :GET, "/organizations/:organization_id/jobs/:id/available_paths", N_("List environment paths available for a job")
147
    param_group :job_id
148
    def available_paths
149
      available = @job.environment.full_paths rescue []
150
      
151
      paths = available.inject([]) do |result, path|
152
        result << { :environments => path }
153
      end
154
      paths = [{ :environments => [] }] if paths.empty?
155

    
156
      collection = {
157
        :results => paths,
158
        :total => paths.size,
159
        :subtotal => paths.size
160
      }
161

    
162
      respond_for_index(:collection => collection)
163
    end
164

    
165
    api :GET, "/organizations/:organization_id/jobs/:id/available_resources", N_("List compute resources available for the job")
166
    param_group :job_id
167
    def available_resources
168
      @compute_resources = @job.available_compute_resources
169
      render "api/v2/compute_resources/index"
170
    end
171

    
172
    api :GET, "/organizations/:organization_id/jobs/:id/run_job", N_("Start job execution a job")
173
    param_group :job_id
174
    def run_job      
175
      if @job.manual_trigger
176
        task = async_task(::Actions::ForemanPipeline::Job::RunJobManually, @job)
177
        render :nothing => true            
178
      else
179
        fail ::Katello::HttpErrors::Forbidden, "Running manually not allowed for Job: #{@job.name}. Try setting it's :manual_trigger property."
180
      end
181
    end
182

    
183
    api :GET, "/organizations/:organization_id/jobs/:id/add_projects", N_("Add jenkins projects to the job")
184
    param_group :job_id
185
    param :projects, Array, :desc => N_("Names of the jenkins projects to be added to the job")
186
    def add_projects
187
      rollback = {:occured => false}
188
      Job.transaction do
189
        projects = params[:projects].map do |p|
190
          JenkinsProject.create(:name => p, :organization => @organization)
191
        end
192
        projects_to_add = projects.delete_if { |p| @job.jenkins_projects.include? p }
193
        @job.jenkins_projects = @job.jenkins_projects + projects_to_add
194
        @job.save!
195

    
196
        projects_to_add.each do |project|
197
          project.reload
198
          task = sync_task(::Actions::ForemanPipeline::Jenkins::GetBuildParams, :job_id => @job.id, :name => project.name)
199
          
200
          unless task.output[:build_params]
201
            raise ActiveRecord::Rollback
202
            rollback[:occured] = true
203
            rollback[:project_name] = project.name
204
          end
205
          task.output[:build_params].each do |param|
206
            new_param = JenkinsProjectParam.new(:name => param[:name],
207
                                                :type => param[:type],
208
                                                :description => param[:description],
209
                                                :value => param[:default])
210
            new_param.organization = @organization
211
            new_param.jenkins_project = project
212
            new_param.save!
213
          end
214
        end
215
      end
216
      if rollback[:occured]
217
        fail ::Katello::HttpErrors::NotFound, "Could not retrieve build params for Jenkins project: #{rollback[:project_name]}"
218
      else
219
        respond_for_show        
220
      end
221
    end
222

    
223
    api :GET, "/organizations/:organization_id/jobs/:id/remove_projects", N_("Remove jenkins projects from the job")
224
    param_group :job_id
225
    param :projects, Array, :desc => N_("Identifiers of the projects to be removed from the job")
226
    def remove_projects
227
      ids = params[:project_ids]
228
      jj_projects = JobJenkinsProject.where(:jenkins_project_id => ids)
229
      jj_projects.map(&:destroy)
230
      respond_for_show
231
    end
232

    
233
    protected
234

    
235
    def find_job
236
      @job = Job.find_by_id(params[:id])
237
      fail ::Katello::HttpErrors::NotFound, "Could not find job with id #{params[:id]}" if @job.nil?
238
      @job 
239
    end
240

    
241
    def job_params
242
      params.require(:job).permit(:name, :manual_trigger, :sync_trigger, :levelup_trigger, :projects, :promote)
243
    end
244

    
245
    def format_paths(paths)
246
      formated_paths = paths.inject([]) do |result, path|
247
        result << { :environments => path }
248
      end
249
      collection = {
250
        :results => formated_paths,
251
        :total => formated_paths.size,
252
        :subtotal => formated_paths.size
253
      }
254
      respond_for_index(:collection => collection)
255
    end
256

    
257
  end
258
end