2012-05-18 16:12:51 +01:00
|
|
|
class MassBuild < ActiveRecord::Base
|
2014-06-27 17:51:16 +01:00
|
|
|
|
|
|
|
AUTO_PUBLISH_STATUSES = %w(none default testing)
|
|
|
|
|
2014-07-09 21:24:41 +01:00
|
|
|
STATUSES, HUMAN_STATUSES = [], {}
|
|
|
|
[
|
|
|
|
%w(SUCCESS 0),
|
|
|
|
%w(BUILD_STARTED 3000),
|
|
|
|
%w(BUILD_PENDING 2000),
|
|
|
|
].each do |kind, value|
|
|
|
|
value = value.to_i
|
|
|
|
const_set kind, value
|
|
|
|
STATUSES << value
|
|
|
|
HUMAN_STATUSES[value] = kind.downcase.to_sym
|
|
|
|
end
|
|
|
|
STATUSES.freeze
|
|
|
|
HUMAN_STATUSES.freeze
|
|
|
|
|
|
|
|
state_machine :status, initial: :build_pending do
|
|
|
|
event :start do
|
|
|
|
transition build_pending: :build_started
|
|
|
|
end
|
|
|
|
|
|
|
|
event :done do
|
|
|
|
transition build_started: :success
|
|
|
|
end
|
|
|
|
|
|
|
|
HUMAN_STATUSES.each do |code,name|
|
|
|
|
state name, value: code
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-11 12:39:57 +00:00
|
|
|
belongs_to :build_for_platform, -> { where(platform_type: 'main') }, class_name: 'Platform'
|
2014-01-21 04:51:49 +00:00
|
|
|
belongs_to :save_to_platform, class_name: 'Platform'
|
2012-05-23 15:08:11 +01:00
|
|
|
belongs_to :user
|
2014-05-12 20:55:13 +01:00
|
|
|
has_many :build_lists, dependent: :destroy
|
2012-05-18 16:12:51 +01:00
|
|
|
|
2013-06-04 20:59:08 +01:00
|
|
|
serialize :extra_repositories, Array
|
|
|
|
serialize :extra_build_lists, Array
|
2014-06-27 20:08:14 +01:00
|
|
|
serialize :extra_mass_builds, Array
|
2013-06-04 20:59:08 +01:00
|
|
|
|
2014-06-27 20:08:14 +01:00
|
|
|
scope :recent, -> { order(created_at: :desc) }
|
|
|
|
scope :outdated, -> { where("#{table_name}.created_at < ?", Time.now + 1.day - BuildList::MAX_LIVE_TIME) }
|
|
|
|
scope :search, -> (q) { where("#{table_name}.description ILIKE ?", "%#{q}%") if q.present? }
|
2012-05-21 11:18:29 +01:00
|
|
|
|
2012-12-10 19:35:56 +00:00
|
|
|
attr_accessor :arches
|
2014-06-27 17:51:16 +01:00
|
|
|
attr_accessible :arches, :auto_publish_status, :projects_list, :build_for_platform_id,
|
2014-05-12 20:55:13 +01:00
|
|
|
:extra_repositories, :extra_build_lists, :increase_release_tag,
|
2014-06-30 21:56:13 +01:00
|
|
|
:use_cached_chroot, :use_extra_tests, :description, :extra_mass_builds,
|
2014-07-01 18:32:44 +01:00
|
|
|
:include_testing_subrepository, :auto_create_container
|
2012-05-25 16:56:26 +01:00
|
|
|
|
2014-05-12 20:55:13 +01:00
|
|
|
validates :save_to_platform_id,
|
|
|
|
:build_for_platform_id,
|
|
|
|
:arch_names,
|
|
|
|
:name,
|
2014-05-12 20:58:50 +01:00
|
|
|
:user_id,
|
|
|
|
presence: true
|
2014-05-12 20:55:13 +01:00
|
|
|
|
|
|
|
validates :projects_list,
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: 500_000 }
|
|
|
|
|
2014-06-26 22:21:31 +01:00
|
|
|
validates :description,
|
|
|
|
length: { maximum: 255 }
|
|
|
|
|
2014-06-27 17:51:16 +01:00
|
|
|
validates :auto_publish_status,
|
|
|
|
inclusion: { in: AUTO_PUBLISH_STATUSES }
|
|
|
|
|
|
|
|
validates :increase_release_tag,
|
2014-05-12 20:55:13 +01:00
|
|
|
:use_cached_chroot,
|
2014-05-23 21:29:24 +01:00
|
|
|
:use_extra_tests,
|
2014-05-12 20:55:13 +01:00
|
|
|
inclusion: { in: [true, false] }
|
2012-05-25 16:56:26 +01:00
|
|
|
|
2014-07-09 21:24:41 +01:00
|
|
|
after_commit :build_all, on: :create, if: Proc.new { |mb| mb.extra_mass_builds.blank? }
|
2014-01-21 04:51:49 +00:00
|
|
|
before_validation :set_data, on: :create
|
2012-05-25 16:56:26 +01:00
|
|
|
|
2014-05-12 20:55:13 +01:00
|
|
|
COUNT_STATUSES = %i(
|
|
|
|
build_lists
|
|
|
|
build_published
|
|
|
|
build_pending
|
|
|
|
build_started
|
|
|
|
build_publish
|
|
|
|
build_error
|
|
|
|
success
|
|
|
|
build_canceled
|
|
|
|
)
|
2012-06-27 10:48:54 +01:00
|
|
|
|
2012-05-25 18:31:57 +01:00
|
|
|
def build_all
|
2014-07-10 18:51:37 +01:00
|
|
|
return unless start
|
2012-12-06 17:59:17 +00:00
|
|
|
# later with resque
|
2014-01-21 04:51:49 +00:00
|
|
|
arches_list = arch_names ? Arch.where(name: arch_names.split(', ')) : Arch.all
|
2012-12-10 19:35:56 +00:00
|
|
|
|
|
|
|
projects_list.lines.each do |name|
|
|
|
|
next if name.blank?
|
|
|
|
name.chomp!; name.strip!
|
|
|
|
|
2014-03-18 13:58:51 +00:00
|
|
|
if project = Project.joins(:repositories).where('repositories.id in (?)', save_to_platform.repository_ids).find_by(name: name)
|
2012-12-10 19:35:56 +00:00
|
|
|
begin
|
|
|
|
return if self.reload.stop_build
|
2014-01-14 19:13:26 +00:00
|
|
|
increase_rt = increase_release_tag?
|
2012-12-10 19:35:56 +00:00
|
|
|
arches_list.each do |arch|
|
2013-06-03 16:20:23 +01:00
|
|
|
rep_id = (project.repository_ids & save_to_platform.repository_ids).first
|
2014-05-23 21:29:24 +01:00
|
|
|
project.build_for(self, rep_id, arch, 0, increase_rt)
|
2014-01-14 19:13:26 +00:00
|
|
|
increase_rt = false
|
2012-12-10 19:35:56 +00:00
|
|
|
end
|
|
|
|
rescue RuntimeError, Exception
|
|
|
|
end
|
|
|
|
else
|
|
|
|
MassBuild.increment_counter :missed_projects_count, id
|
|
|
|
list = (missed_projects_list || '') << "#{name}\n"
|
|
|
|
update_column :missed_projects_list, list
|
|
|
|
end
|
2012-12-06 17:59:17 +00:00
|
|
|
end
|
2014-07-09 21:24:41 +01:00
|
|
|
done
|
2012-05-18 16:12:51 +01:00
|
|
|
end
|
2014-04-15 19:41:06 +01:00
|
|
|
later :build_all, queue: :low
|
2012-06-22 16:10:44 +01:00
|
|
|
|
|
|
|
def generate_failed_builds_list
|
2014-02-03 15:32:31 +00:00
|
|
|
generate_list BuildList::BUILD_ERROR
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_tests_failed_builds_list
|
|
|
|
generate_list BuildList::TESTS_FAILED
|
2012-06-22 16:10:44 +01:00
|
|
|
end
|
2012-06-28 16:23:33 +01:00
|
|
|
|
2014-06-26 18:20:26 +01:00
|
|
|
def generate_success_builds_list
|
|
|
|
generate_list BuildList::SUCCESS
|
|
|
|
end
|
|
|
|
|
2012-06-28 16:23:33 +01:00
|
|
|
def cancel_all
|
2012-09-06 11:53:03 +01:00
|
|
|
update_column(:stop_build, true)
|
2014-01-21 04:51:49 +00:00
|
|
|
build_lists.find_each(batch_size: 100) do |bl|
|
2012-06-29 17:01:32 +01:00
|
|
|
bl.cancel
|
|
|
|
end
|
2012-06-28 16:23:33 +01:00
|
|
|
end
|
2014-04-15 19:41:06 +01:00
|
|
|
later :cancel_all, queue: :low
|
2012-07-09 17:13:02 +01:00
|
|
|
|
2013-04-02 14:54:18 +01:00
|
|
|
def publish_success_builds(user)
|
|
|
|
publish user, BuildList::SUCCESS, BuildList::FAILED_PUBLISH
|
2013-03-21 14:41:45 +00:00
|
|
|
end
|
2014-04-15 19:41:06 +01:00
|
|
|
later :publish_success_builds, queue: :low
|
2013-03-21 14:41:45 +00:00
|
|
|
|
2013-08-02 12:23:10 +01:00
|
|
|
def publish_test_failed_builds(user)
|
2013-04-02 14:54:18 +01:00
|
|
|
publish user, BuildList::TESTS_FAILED
|
2013-03-21 14:41:45 +00:00
|
|
|
end
|
2014-04-15 19:41:06 +01:00
|
|
|
later :publish_test_failed_builds, queue: :low
|
2013-03-21 14:41:45 +00:00
|
|
|
|
2014-02-24 14:28:48 +00:00
|
|
|
COUNT_STATUSES.each do |stat|
|
2014-02-25 09:29:09 +00:00
|
|
|
stat_count = "#{stat}_count"
|
|
|
|
define_method stat_count do
|
2014-02-25 14:54:57 +00:00
|
|
|
Rails.cache.fetch([self, "cached_#{stat_count}"], expires_in: 5.minutes) do
|
|
|
|
build_lists.where(status: BuildList::HUMAN_STATUSES.key(stat)).count
|
|
|
|
end
|
2014-02-24 14:28:48 +00:00
|
|
|
end if stat != :build_lists
|
|
|
|
end
|
|
|
|
|
2012-07-09 17:13:02 +01:00
|
|
|
private
|
|
|
|
|
2014-02-03 15:32:31 +00:00
|
|
|
def generate_list(status)
|
|
|
|
report = ""
|
|
|
|
BuildList.select('build_lists.id, projects.name as project_name, arches.name as arch_name').
|
|
|
|
where(
|
|
|
|
status: status,
|
|
|
|
mass_build_id: self.id
|
2014-04-05 19:37:27 +01:00
|
|
|
).joins(:project, :arch).find_each(batch_size: 100) do |build_list|
|
|
|
|
report << "ID: #{build_list.id}; "
|
|
|
|
report << "PROJECT_NAME: #{build_list.project_name}; "
|
|
|
|
report << "ARCH: #{build_list.arch_name}\n"
|
2014-02-03 15:32:31 +00:00
|
|
|
end
|
|
|
|
report
|
|
|
|
end
|
|
|
|
|
2013-04-02 14:54:18 +01:00
|
|
|
def publish(user, *statuses)
|
2014-01-21 04:51:49 +00:00
|
|
|
builds = build_lists.where(status: statuses)
|
|
|
|
builds.update_all(publisher_id: user.id)
|
2014-04-05 19:37:27 +01:00
|
|
|
builds.find_each(batch_size: 50) do |bl|
|
|
|
|
bl.now_publish if bl.can_publish? && bl.has_new_packages?
|
2013-03-21 14:41:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-03 16:20:23 +01:00
|
|
|
def set_data
|
2013-06-21 11:39:03 +01:00
|
|
|
if save_to_platform
|
|
|
|
self.name = "#{Time.now.utc.to_date.strftime("%d.%b")}-#{save_to_platform.name}"
|
|
|
|
self.build_for_platform = save_to_platform if save_to_platform.main?
|
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
self.arch_names = Arch.where(id: arches).map(&:name).join(", ")
|
2013-06-21 11:39:03 +01:00
|
|
|
|
|
|
|
self.projects_list = projects_list.lines.map do |name|
|
|
|
|
name.chomp.strip if name.present?
|
|
|
|
end.compact.uniq.join("\r\n") if projects_list.present?
|
2013-06-03 16:20:23 +01:00
|
|
|
end
|
2012-05-18 16:12:51 +01:00
|
|
|
end
|