2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2011-04-07 14:20:21 +01:00
|
|
|
class BuildList < ActiveRecord::Base
|
|
|
|
belongs_to :project
|
|
|
|
belongs_to :arch
|
2012-05-04 18:12:51 +01:00
|
|
|
belongs_to :save_to_platform, :class_name => 'Platform'
|
|
|
|
belongs_to :build_for_platform, :class_name => 'Platform'
|
2011-12-20 17:09:29 +00:00
|
|
|
belongs_to :user
|
2012-05-04 18:12:51 +01:00
|
|
|
belongs_to :advisory
|
2012-05-21 11:18:29 +01:00
|
|
|
belongs_to :mass_build
|
2012-05-14 20:08:31 +01:00
|
|
|
has_many :items, :class_name => "BuildList::Item", :dependent => :destroy
|
|
|
|
has_many :packages, :class_name => "BuildList::Package", :dependent => :destroy
|
2012-05-04 18:12:51 +01:00
|
|
|
|
2011-10-22 16:28:41 +01:00
|
|
|
UPDATE_TYPES = %w[security bugfix enhancement recommended newpackage]
|
2012-05-04 18:12:51 +01:00
|
|
|
RELEASE_UPDATE_TYPES = %w[security bugfix]
|
|
|
|
|
2012-05-14 20:08:31 +01:00
|
|
|
validates :project_id, :project_version, :arch, :include_repos, :presence => true
|
|
|
|
validates_numericality_of :priority, :greater_than_or_equal_to => 0
|
2012-05-04 18:12:51 +01:00
|
|
|
validates :update_type, :inclusion => UPDATE_TYPES,
|
|
|
|
:unless => Proc.new { |b| b.save_to_platform.released }
|
|
|
|
validates :update_type, :inclusion => RELEASE_UPDATE_TYPES,
|
2012-05-21 22:10:00 +01:00
|
|
|
:if => Proc.new { |b| b.save_to_platform.released && b.mass_build_id.nil?}
|
2011-10-22 16:28:41 +01:00
|
|
|
validate lambda {
|
2012-05-04 18:12:51 +01:00
|
|
|
errors.add(:build_for_platform, I18n.t('flash.build_list.wrong_platform')) if save_to_platform.platform_type == 'main' && save_to_platform_id != build_for_platform_id
|
2011-10-22 16:28:41 +01:00
|
|
|
}
|
|
|
|
|
2012-05-11 18:44:19 +01:00
|
|
|
LIVE_TIME = 3.week
|
|
|
|
|
2011-12-12 12:34:20 +00:00
|
|
|
# The kernel does not send these statuses directly
|
2011-10-22 16:28:41 +01:00
|
|
|
BUILD_CANCELED = 5000
|
2011-04-11 17:37:09 +01:00
|
|
|
WAITING_FOR_RESPONSE = 4000
|
|
|
|
BUILD_PENDING = 2000
|
2011-11-11 10:38:17 +00:00
|
|
|
BUILD_PUBLISHED = 6000
|
2011-12-22 00:53:55 +00:00
|
|
|
BUILD_PUBLISH = 7000
|
|
|
|
FAILED_PUBLISH = 8000
|
2012-04-17 19:18:39 +01:00
|
|
|
REJECTED_PUBLISH = 9000
|
2011-04-11 17:37:09 +01:00
|
|
|
|
2011-12-12 12:34:20 +00:00
|
|
|
STATUSES = [ WAITING_FOR_RESPONSE,
|
|
|
|
BUILD_CANCELED,
|
|
|
|
BUILD_PENDING,
|
|
|
|
BUILD_PUBLISHED,
|
2011-12-22 00:53:55 +00:00
|
|
|
BUILD_PUBLISH,
|
|
|
|
FAILED_PUBLISH,
|
2012-04-17 19:18:39 +01:00
|
|
|
REJECTED_PUBLISH,
|
2011-12-12 12:34:20 +00:00
|
|
|
BuildServer::SUCCESS,
|
|
|
|
BuildServer::BUILD_STARTED,
|
|
|
|
BuildServer::BUILD_ERROR,
|
|
|
|
BuildServer::PLATFORM_NOT_FOUND,
|
|
|
|
BuildServer::PLATFORM_PENDING,
|
|
|
|
BuildServer::PROJECT_NOT_FOUND,
|
|
|
|
BuildServer::PROJECT_VERSION_NOT_FOUND,
|
2012-02-29 14:04:04 +00:00
|
|
|
# BuildServer::BINARY_TEST_FAILED,
|
|
|
|
# BuildServer::DEPENDENCY_TEST_FAILED
|
|
|
|
]
|
2011-04-07 15:56:28 +01:00
|
|
|
|
2011-12-12 12:34:20 +00:00
|
|
|
HUMAN_STATUSES = { WAITING_FOR_RESPONSE => :waiting_for_response,
|
2011-12-12 16:16:05 +00:00
|
|
|
BUILD_CANCELED => :build_canceled,
|
2011-04-07 15:56:28 +01:00
|
|
|
BUILD_PENDING => :build_pending,
|
2011-12-12 12:34:20 +00:00
|
|
|
BUILD_PUBLISHED => :build_published,
|
2011-12-22 00:53:55 +00:00
|
|
|
BUILD_PUBLISH => :build_publish,
|
|
|
|
FAILED_PUBLISH => :failed_publish,
|
2012-04-17 19:18:39 +01:00
|
|
|
REJECTED_PUBLISH => :rejected_publish,
|
2011-12-12 12:34:20 +00:00
|
|
|
BuildServer::BUILD_ERROR => :build_error,
|
|
|
|
BuildServer::BUILD_STARTED => :build_started,
|
2011-04-11 17:37:09 +01:00
|
|
|
BuildServer::SUCCESS => :success,
|
|
|
|
BuildServer::PLATFORM_NOT_FOUND => :platform_not_found,
|
|
|
|
BuildServer::PLATFORM_PENDING => :platform_pending,
|
|
|
|
BuildServer::PROJECT_NOT_FOUND => :project_not_found,
|
2011-10-29 18:50:47 +01:00
|
|
|
BuildServer::PROJECT_VERSION_NOT_FOUND => :project_version_not_found,
|
2012-02-29 14:04:04 +00:00
|
|
|
# BuildServer::DEPENDENCY_TEST_FAILED => :dependency_test_failed,
|
|
|
|
# BuildServer::BINARY_TEST_FAILED => :binary_test_failed
|
2011-04-07 14:20:21 +01:00
|
|
|
}
|
|
|
|
|
2011-12-13 13:00:15 +00:00
|
|
|
scope :recent, order("#{table_name}.updated_at DESC")
|
2011-04-07 14:20:21 +01:00
|
|
|
scope :for_status, lambda {|status| where(:status => status) }
|
2012-03-05 09:11:50 +00:00
|
|
|
scope :for_user, lambda { |user| where(:user_id => user.id) }
|
2012-05-17 16:20:03 +01:00
|
|
|
scope :for_platform, lambda { |platform| where(:build_for_platform_id => platform.id) }
|
2012-05-21 11:18:29 +01:00
|
|
|
scope :by_mass_build, lambda { |mass_build| where(:mass_build_id => mass_build.id) }
|
2011-04-07 14:20:21 +01:00
|
|
|
scope :scoped_to_arch, lambda {|arch| where(:arch_id => arch) }
|
2011-10-23 11:34:42 +01:00
|
|
|
scope :scoped_to_project_version, lambda {|project_version| where(:project_version => project_version) }
|
2011-04-07 15:56:28 +01:00
|
|
|
scope :scoped_to_is_circle, lambda {|is_circle| where(:is_circle => is_circle) }
|
2011-04-07 14:20:21 +01:00
|
|
|
scope :for_creation_date_period, lambda{|start_date, end_date|
|
2012-04-13 23:27:24 +01:00
|
|
|
s = scoped
|
|
|
|
s = s.where(["build_lists.created_at >= ?", start_date]) if start_date
|
|
|
|
s = s.where(["build_lists.created_at <= ?", end_date]) if end_date
|
|
|
|
s
|
2011-04-07 14:20:21 +01:00
|
|
|
}
|
|
|
|
scope :for_notified_date_period, lambda{|start_date, end_date|
|
2012-04-13 23:27:24 +01:00
|
|
|
s = scoped
|
|
|
|
s = s.where(["build_lists.updated_at >= ?", start_date]) if start_date
|
|
|
|
s = s.where(["build_lists.updated_at <= ?", end_date]) if end_date
|
|
|
|
s
|
2011-04-07 14:20:21 +01:00
|
|
|
}
|
2011-12-07 22:46:01 +00:00
|
|
|
scope :scoped_to_project_name, lambda {|project_name| joins(:project).where('projects.name LIKE ?', "%#{project_name}%")}
|
2012-05-11 18:44:19 +01:00
|
|
|
scope :outdated, where('updated_at < ? AND status <> ?', Time.now - LIVE_TIME, BUILD_PUBLISHED)
|
|
|
|
|
2011-04-11 11:47:57 +01:00
|
|
|
serialize :additional_repos
|
2011-12-21 14:01:50 +00:00
|
|
|
serialize :include_repos
|
2012-05-11 18:44:19 +01:00
|
|
|
|
2011-04-11 17:37:09 +01:00
|
|
|
before_create :set_default_status
|
|
|
|
after_create :place_build
|
2011-04-11 11:47:57 +01:00
|
|
|
|
2012-05-11 19:00:27 +01:00
|
|
|
def self.human_status(status)
|
|
|
|
I18n.t("layout.build_lists.statuses.#{HUMAN_STATUSES[status]}")
|
2011-04-07 14:20:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def human_status
|
|
|
|
self.class.human_status(status)
|
|
|
|
end
|
|
|
|
|
2011-04-11 11:47:57 +01:00
|
|
|
def set_items(items_hash)
|
|
|
|
self.items = []
|
|
|
|
|
|
|
|
items_hash.each do |level, items|
|
|
|
|
items.each do |item|
|
2011-10-29 15:05:40 +01:00
|
|
|
self.items << self.items.build(:name => item['name'], :version => item['version'], :level => level.to_i)
|
2011-04-11 11:47:57 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-12-21 23:31:05 +00:00
|
|
|
|
2012-05-17 22:19:57 +01:00
|
|
|
def set_packages(pkg_hash, project_name)
|
|
|
|
prj = Project.joins(:repositories => :platform).where('platforms.id = ?', save_to_platform.id).find_by_name!(project_name)
|
|
|
|
build_package(pkg_hash['srpm'], 'source', prj) {|p| p.save!}
|
2012-05-14 20:08:31 +01:00
|
|
|
pkg_hash['rpm'].each do |rpm_hash|
|
2012-05-17 22:19:57 +01:00
|
|
|
build_package(rpm_hash, 'binary', prj) {|p| p.save!}
|
2012-05-14 20:08:31 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-15 18:56:20 +01:00
|
|
|
def publish
|
2011-12-22 02:08:11 +00:00
|
|
|
return false unless can_publish?
|
2011-12-21 23:31:05 +00:00
|
|
|
has_published = BuildServer.publish_container bs_id
|
2011-12-29 02:37:34 +00:00
|
|
|
update_attribute(:status, has_published == 0 ? BUILD_PUBLISH : FAILED_PUBLISH)
|
2011-12-21 23:31:05 +00:00
|
|
|
return has_published == 0
|
2011-09-15 18:56:20 +01:00
|
|
|
end
|
2011-12-22 02:08:11 +00:00
|
|
|
|
2011-12-21 21:42:06 +00:00
|
|
|
def can_publish?
|
2011-12-22 00:53:55 +00:00
|
|
|
status == BuildServer::SUCCESS or status == FAILED_PUBLISH
|
2011-09-16 15:15:50 +01:00
|
|
|
end
|
2011-04-11 11:47:57 +01:00
|
|
|
|
2012-04-17 19:18:39 +01:00
|
|
|
def reject_publish
|
|
|
|
return false unless can_reject_publish?
|
|
|
|
update_attribute(:status, REJECTED_PUBLISH)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_reject_publish?
|
2012-05-04 18:12:51 +01:00
|
|
|
can_publish? and save_to_platform.released
|
2012-04-17 19:18:39 +01:00
|
|
|
end
|
|
|
|
|
2011-12-21 21:42:06 +00:00
|
|
|
def cancel
|
2011-12-22 02:08:11 +00:00
|
|
|
return false unless can_cancel?
|
2011-10-22 16:28:41 +01:00
|
|
|
has_canceled = BuildServer.delete_build_list bs_id
|
2011-10-30 14:06:33 +00:00
|
|
|
update_attribute(:status, BUILD_CANCELED) if has_canceled == 0
|
2011-10-22 16:28:41 +01:00
|
|
|
return has_canceled == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
#TODO: Share this checking on product owner.
|
2011-11-30 22:55:47 +00:00
|
|
|
def can_cancel?
|
2011-12-22 00:53:55 +00:00
|
|
|
status == BUILD_PENDING && bs_id
|
2011-10-22 16:28:41 +01:00
|
|
|
end
|
|
|
|
|
2011-10-29 15:50:22 +01:00
|
|
|
def event_log_message
|
2011-11-28 15:41:42 +00:00
|
|
|
{:project => project.name, :version => project_version, :arch => arch.name}.inspect
|
2011-10-29 15:50:22 +01:00
|
|
|
end
|
|
|
|
|
2012-04-12 15:55:32 +01:00
|
|
|
def current_duration
|
2012-04-13 21:49:29 +01:00
|
|
|
(Time.now.utc - started_at.utc).to_i
|
2012-04-12 15:55:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def human_current_duration
|
2012-04-13 22:20:56 +01:00
|
|
|
I18n.t("layout.build_lists.human_current_duration", {:hours => (current_duration/3600).to_i, :minutes => (current_duration%3600/60).to_i})
|
2012-04-12 15:55:32 +01:00
|
|
|
end
|
|
|
|
|
2012-04-12 11:29:04 +01:00
|
|
|
def human_duration
|
2012-04-13 22:20:56 +01:00
|
|
|
I18n.t("layout.build_lists.human_duration", {:hours => (duration/3600).to_i, :minutes => (duration%3600/60).to_i})
|
2012-04-12 11:29:04 +01:00
|
|
|
end
|
|
|
|
|
2012-04-13 21:49:29 +01:00
|
|
|
def in_work?
|
|
|
|
status == BuildServer::BUILD_STARTED
|
|
|
|
#[WAITING_FOR_RESPONSE, BuildServer::BUILD_PENDING, BuildServer::BUILD_STARTED].include?(status)
|
2012-04-12 11:29:04 +01:00
|
|
|
end
|
|
|
|
|
2012-05-14 20:08:31 +01:00
|
|
|
protected
|
2011-04-11 17:37:09 +01:00
|
|
|
|
2012-05-14 20:08:31 +01:00
|
|
|
def set_default_status
|
|
|
|
self.status = WAITING_FOR_RESPONSE unless self.status.present?
|
|
|
|
return true
|
|
|
|
end
|
2011-04-11 17:37:09 +01:00
|
|
|
|
2012-05-14 20:08:31 +01:00
|
|
|
def place_build
|
|
|
|
#XML-RPC params: project_name, project_version, plname, arch, bplname, update_type, build_requires, id_web, include_repos, priority
|
|
|
|
self.status = BuildServer.add_build_list project.name, project_version, save_to_platform.name, arch.name, (save_to_platform_id == build_for_platform_id ? '' : build_for_platform.name), update_type, build_requires, id, include_repos, priority
|
|
|
|
self.status = BUILD_PENDING if self.status == 0
|
|
|
|
save
|
|
|
|
end
|
|
|
|
|
2012-05-17 22:19:57 +01:00
|
|
|
def build_package(pkg_hash, package_type, prj)
|
2012-05-14 20:08:31 +01:00
|
|
|
packages.create(pkg_hash) do |p|
|
2012-05-17 22:19:57 +01:00
|
|
|
p.project = prj
|
2012-05-14 20:08:31 +01:00
|
|
|
p.platform = save_to_platform
|
|
|
|
p.package_type = package_type
|
|
|
|
yield p
|
|
|
|
end
|
|
|
|
end
|
2011-11-28 15:41:42 +00:00
|
|
|
end
|