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'
|
16
|
|
17
|
has_many :job_jenkins_projects, :dependent => :destroy
|
18
|
has_many :jenkins_projects, :through => :job_jenkins_projects, :class_name => 'ForemanPipeline::JenkinsProject', :dependent => :restrict
|
19
|
|
20
|
has_many :content_view_repositories, :class_name=> 'Katello::ContentViewRepository',
|
21
|
:primary_key => :content_view_id, :foreign_key => :content_view_id
|
22
|
has_many :repositories, :through => :content_view_repositories
|
23
|
|
24
|
has_many :job_to_environments, :class_name => 'ForemanPipeline::JobToEnvironment', :dependent => :destroy
|
25
|
has_many :to_environments, :through => :job_to_environments, :class_name => 'Katello::KTEnvironment', :dependent => :nullify
|
26
|
|
27
|
validates :name, :presence => true
|
28
|
validates :organization, :presence => true
|
29
|
validate :no_composite_view, :check_env_succession
|
30
|
|
31
|
def is_valid?
|
32
|
!self.attributes.values.include?(nil) && !self.jenkins_instance.jenkins_user.nil?
|
33
|
end
|
34
|
|
35
|
def target_cv_version_avail?
|
36
|
!target_cv_version.nil?
|
37
|
end
|
38
|
|
39
|
def target_cv_version
|
40
|
fail "Cannot fetch target version, no environment set" if environment.nil?
|
41
|
fail "Cannot fetch target version, no content view set" if content_view.nil?
|
42
|
fail "Content view has no versions!" if content_view.content_view_versions.empty?
|
43
|
self.environment.content_view_versions.where(:content_view_id => self.content_view.id).first
|
44
|
end
|
45
|
|
46
|
def init_run
|
47
|
fail "Cannnot contact Jenkins server: no Jenkins Instance set for the job: #{name}" if jenkins_instance.nil?
|
48
|
fail "Cannot log in to Jenkins server:
|
49
|
no Jenkins User set for the Jenkins Instance: #{jenkins_instancej.name}" if jenkins_instance.jenkins_user.nil?
|
50
|
jenkins_instance.create_client(jenkins_instance.jenkins_user.name, jenkins_instance.jenkins_user.token)
|
51
|
end
|
52
|
|
53
|
|
54
|
def should_be_promoted?
|
55
|
!to_environments.empty?
|
56
|
end
|
57
|
|
58
|
|
59
|
def not_yet_promoted?
|
60
|
|
61
|
return true if to_environments.empty?
|
62
|
|
63
|
can_be_promoted?
|
64
|
end
|
65
|
|
66
|
|
67
|
def promotion_safe?
|
68
|
should_be_promoted? ? can_be_promoted? : false
|
69
|
end
|
70
|
|
71
|
|
72
|
def envs_for_promotion
|
73
|
to_environments.reject { |env| target_cv_version.environments.include?(env) }
|
74
|
end
|
75
|
|
76
|
def can_be_promoted?
|
77
|
!envs_for_promotion.empty?
|
78
|
end
|
79
|
|
80
|
def available_compute_resources
|
81
|
hostgroup.compute_profile.compute_attributes.map(&:compute_resource) rescue []
|
82
|
end
|
83
|
|
84
|
private
|
85
|
|
86
|
def no_composite_view
|
87
|
errors.add(:base,
|
88
|
"Cannot add content view, only non-composites allowed.") if !content_view.nil? && content_view.composite?
|
89
|
end
|
90
|
|
91
|
def check_env_succession
|
92
|
if environment && should_be_promoted?
|
93
|
to_environments.each do |to_env|
|
94
|
unless to_env.prior == environment
|
95
|
errors.add(:base, "Environment succession violation: #{to_env.name}")
|
96
|
end
|
97
|
end
|
98
|
end
|
99
|
end
|
100
|
end
|
101
|
end
|