Project

General

Profile

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

foreman_pipeline / app / models / foreman_pipeline / job.rb @ be52ca64

1
module ForemanPipeline
2
  class Job < Katello::Model
3
    self.include_root_in_json = false
4

    
5
    include Katello::Glue
6
    include Glue::ElasticSearch::Job
7
    include ForemanPipeline::Authorization::Job
8
    include ActiveModel::Validations
9

    
10
    belongs_to :content_view, :class_name => 'Katello::ContentView', :inverse_of => :jobs
11
    belongs_to :hostgroup, :class_name => '::Hostgroup', :inverse_of => :jobs
12
    belongs_to :organization
13
    belongs_to :compute_resource, :class_name => '::ComputeResource', :inverse_of => :jobs
14
    belongs_to :jenkins_instance, :class_name => "ForemanPipeline::JenkinsInstance"
15
    belongs_to :environment, :class_name => 'Katello::KTEnvironment', :inverse_of => :jobs
16
    belongs_to :jenkins_user, :class_name => "ForemanPipeline::JenkinsUser"
17
    
18
    has_many :job_jenkins_projects, :dependent => :destroy
19
    has_many :jenkins_projects, :through => :job_jenkins_projects, :class_name => 'ForemanPipeline::JenkinsProject', :dependent => :restrict
20

    
21
    has_many :content_view_repositories, :class_name=> 'Katello::ContentViewRepository',
22
     :primary_key => :content_view_id, :foreign_key => :content_view_id
23
    has_many :repositories, :through => :content_view_repositories
24
    
25
    validates :name, :presence => true
26
    validates :organization, :presence => true
27
    validate :no_composite_view
28

    
29
    def no_composite_view
30
      errors.add(:base,
31
       "Cannot add content view, only non-composites allowed.") if !content_view.nil? && content_view.composite?
32
    end
33

    
34
    def is_valid?
35
      !self.attributes.values.include? nil
36
    end
37

    
38
    def target_cv_version_avail?
39
      !target_cv_version.nil?
40
    end
41

    
42
    def target_cv_version
43
      fail "Cannot fetch target version, no environment set" if environment.nil?
44
      fail "Cannot fetch target version, no content view set" if content_view.nil?
45
      self.environment.content_view_versions.where(:content_view_id => self.content_view.id).first
46
    end
47

    
48
    def init_run
49
      fail "Cannnot contact Jenkins server: no Jenkins Instance set" if jenkins_instance.nil?
50
      fail "Cannot log in to Jenkins server: no Jenkins User set" if jenkins_user.nil? 
51
      jenkins_instance.create_client(jenkins_user.name, jenkins_user.token)
52
    end
53

    
54
    def version_already_promoted?
55
      self.target_cv_version.environments.include?(self.environment.successor)
56
    end
57
    
58
  end
59
end