Project

General

Profile

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

foreman_pipeline / app / models / foreman_pipeline / job.rb @ 720671b8

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

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

    
9
    belongs_to :content_view, :class_name => 'Katello::ContentView', :inverse_of => :jobs
10
    belongs_to :hostgroup, :class_name => '::Hostgroup', :inverse_of => :jobs
11
    belongs_to :organization
12
    belongs_to :compute_resource, :class_name => '::ComputeResource', :inverse_of => :jobs
13
    belongs_to :jenkins_instance, :class_name => "ForemanPipeline::JenkinsInstance"
14
    belongs_to :environment, :class_name => 'Katello::KTEnvironment'
15

    
16
    has_many :job_jenkins_projects, :dependent => :destroy
17
    has_many :jenkins_projects, :through => :job_jenkins_projects, :class_name => 'ForemanPipeline::JenkinsProject'
18
    has_many :content_view_repositories, :class_name=> 'Katello::ContentViewRepository',
19
     :primary_key => :content_view_id, :foreign_key => :content_view_id
20
    has_many :repositories, :through => :content_view_repositories
21

    
22
    has_many :job_to_environments, :class_name => 'ForemanPipeline::JobToEnvironment', :dependent => :destroy
23
    has_many :to_environments, :through => :job_to_environments, :class_name => 'Katello::KTEnvironment', :dependent => :nullify
24

    
25
    validates :name, :presence => true
26
    validates :organization, :presence => true
27
    validate :no_composite_view, :check_env_succession
28

    
29
    attr_accessible :name, :content_view_id, :hostgroup_id, :organization_id, :compute_resource_id, :jenkins_instance_id,
30
      :environment_id, :manual_trigger, :levelup_trigger, :sync_trigger
31

    
32
    scoped_search :on => :name, :complete_value => true
33
    scoped_search :on => :organization_id, :complete_value => true
34
    scoped_search :on => :manual_trigger, :complete_value => true
35
    scoped_search :on => :levelup_trigger, :complete_value => true
36
    scoped_search :on => :sync_trigger, :complete_value => true
37

    
38
    def is_valid?
39
      !self.attributes.values.include?(nil) && !self.jenkins_instance.jenkins_user.nil?
40
    end
41

    
42
    def target_cv_version_avail?
43
      !target_cv_version.nil?
44
    end
45

    
46
    def target_cv_version
47
      fail "Cannot fetch target version, no environment set" if environment.nil?
48
      fail "Cannot fetch target version, no content view set" if content_view.nil?
49
      fail "Content view has no versions!" if content_view.content_view_versions.empty?
50
      self.environment.content_view_versions.where(:content_view_id => self.content_view.id).first
51
    end
52

    
53
    def init_run
54
      fail "Cannnot contact Jenkins server: no Jenkins Instance set for the job: #{name}" if jenkins_instance.nil?
55
      fail "Cannot log in to Jenkins server: no Jenkins User set for the Jenkins Instance: #{jenkins_instance.name}" if jenkins_instance.jenkins_user.nil?
56
      jenkins_instance.create_client(jenkins_instance.jenkins_user.name, jenkins_instance.jenkins_user.token)
57
    end
58

    
59
    #Is any to_env set? (Do we want to promote to any env?)
60
    def should_be_promoted?
61
      !to_environments.empty?
62
    end
63

    
64
    # this shlould make sure not to trigger cyclic runs in hook actions
65
    def not_yet_promoted?
66
      # no to_envs means we do not want to promote, no need to check further here
67
      return true if to_environments.empty?
68
      #we want to promote, but are any of to_envs safe to promote to?
69
      can_be_promoted?
70
    end
71

    
72
    #If we want to promote, is it safe (or could we get a cycle)?
73
    def promotion_safe?
74
      should_be_promoted? ? can_be_promoted? : false
75
    end
76

    
77
    #we have some to_envs set (== we want to promote), but cv version may already be in those envs
78
    def envs_for_promotion
79
      to_environments.reject { |env| target_cv_version.environments.include?(env) }
80
    end
81

    
82
    def can_be_promoted?
83
      !envs_for_promotion.empty?
84
    end
85

    
86
    def available_compute_resources
87
      hostgroup.compute_profile.compute_attributes.map(&:compute_resource) rescue []
88
    end
89

    
90
    private
91

    
92
    def no_composite_view
93
      errors.add(:base,
94
       "Cannot add content view, only non-composites allowed.") if !content_view.nil? && content_view.composite?
95
    end
96

    
97
    def check_env_succession
98
      if environment && should_be_promoted?
99
        to_environments.each do |to_env|
100
          unless to_env.prior == environment
101
            errors.add(:base, "Environment succession violation: #{to_env.name}")
102
          end
103
        end
104
      end
105
    end
106
  end
107
end