2011-03-11 16:08:41 +00:00
|
|
|
class Repository < ActiveRecord::Base
|
2013-09-01 16:33:09 +01:00
|
|
|
extend FriendlyId
|
|
|
|
friendly_id :name
|
|
|
|
|
2014-03-11 07:39:25 +00:00
|
|
|
include EventLoggable
|
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
LOCK_FILE_NAMES = {sync: '.sync.lock', repo: '.repo.lock'}
|
2013-07-26 09:44:15 +01:00
|
|
|
SORT = {'base' => 1, 'main' => 2, 'contrib' => 3, 'non-free' => 4, 'restricted' => 5}
|
2013-07-25 19:00:42 +01:00
|
|
|
|
2011-03-11 16:08:41 +00:00
|
|
|
belongs_to :platform
|
2011-10-13 23:35:16 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
has_many :relations, as: :target, dependent: :destroy
|
|
|
|
has_many :actors, as: :target, class_name: 'Relation', dependent: :destroy
|
|
|
|
has_many :members, through: :actors, source: :actor, source_type: 'User'
|
2012-09-11 16:30:14 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
has_many :project_to_repositories, dependent: :destroy, validate: true
|
|
|
|
has_many :projects, through: :project_to_repositories
|
|
|
|
has_many :repository_statuses, dependent: :destroy
|
|
|
|
has_one :key_pair, dependent: :destroy
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
has_many :build_lists, foreign_key: :save_to_repository_id, dependent: :destroy
|
2013-01-24 13:55:04 +00:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
validates :description, presence: true
|
2014-03-11 07:39:25 +00:00
|
|
|
validates :name, uniqueness: {scope: :platform_id, case_sensitive: false}, presence: true,
|
|
|
|
format: {with: /\A[a-z0-9_\-]+\z/}
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2014-03-11 07:39:25 +00:00
|
|
|
scope :recent, -> { order(:name) }
|
|
|
|
scope :main, -> { where(name: %w(main base)) }
|
2011-03-31 01:25:18 +01:00
|
|
|
|
2013-04-23 22:49:40 +01:00
|
|
|
before_destroy :detele_directory
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2014-01-13 22:37:27 +00:00
|
|
|
attr_accessible :name, :description, :publish_without_qa, :synchronizing_publications
|
2012-04-02 16:43:59 +01:00
|
|
|
attr_readonly :name, :platform_id
|
2013-11-12 12:04:40 +00:00
|
|
|
attr_accessor :projects_list
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2013-08-26 15:08:56 +01:00
|
|
|
def regenerate(build_for_platform_id = nil)
|
2013-08-22 22:02:24 +01:00
|
|
|
build_for_platform = Platform.main.find build_for_platform_id if platform.personal?
|
|
|
|
status = repository_statuses.find_or_create_by_platform_id(build_for_platform.try(:id) || platform_id)
|
|
|
|
status.regenerate
|
|
|
|
end
|
|
|
|
|
|
|
|
def resign
|
2013-08-25 17:30:23 +01:00
|
|
|
if platform.main?
|
2013-08-22 22:02:24 +01:00
|
|
|
status = repository_statuses.find_or_create_by_platform_id(platform_id)
|
|
|
|
status.resign
|
|
|
|
end
|
|
|
|
end
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2012-02-22 20:24:29 +00:00
|
|
|
def base_clone(attrs = {})
|
2012-06-08 18:37:16 +01:00
|
|
|
dup.tap do |c|
|
2012-02-22 20:24:29 +00:00
|
|
|
c.platform_id = nil
|
|
|
|
attrs.each {|k,v| c.send("#{k}=", v)}
|
2012-06-08 18:37:16 +01:00
|
|
|
c.updated_at = nil; c.created_at = nil
|
2012-02-22 20:24:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clone_relations(from)
|
|
|
|
with_skip do
|
|
|
|
from.projects.find_each {|p| self.projects << p}
|
|
|
|
end
|
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
later :clone_relations, loner: true, queue: :clone_build
|
2012-02-22 20:24:29 +00:00
|
|
|
|
2013-11-12 12:04:40 +00:00
|
|
|
def add_projects(list, user)
|
|
|
|
current_ability = Ability.new(user)
|
|
|
|
list.lines.each do |line|
|
|
|
|
begin
|
|
|
|
line.chomp!; line.strip!
|
|
|
|
owner, name = line.split('/')
|
|
|
|
next if owner.blank? || name.blank?
|
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
project = Project.where(owner_uname: owner, name: name).accessible_by(current_ability, :read).first
|
2013-11-12 12:04:40 +00:00
|
|
|
projects << project if project
|
|
|
|
rescue RuntimeError, Exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
later :add_projects, queue: :clone_build
|
2013-11-12 12:04:40 +00:00
|
|
|
|
|
|
|
def remove_projects(list)
|
|
|
|
list.lines.each do |name|
|
|
|
|
begin
|
|
|
|
name.chomp!; name.strip!
|
|
|
|
next if name.blank?
|
2014-01-21 04:51:49 +00:00
|
|
|
project_to_repositories.where(projects: { name: name }).joins(:project).destroy_all
|
2013-11-12 12:04:40 +00:00
|
|
|
rescue RuntimeError, Exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
later :remove_projects, queue: :clone_build
|
2013-11-12 12:04:40 +00:00
|
|
|
|
2012-02-22 20:24:29 +00:00
|
|
|
def full_clone(attrs = {})
|
|
|
|
base_clone(attrs).tap do |c|
|
2012-06-16 23:51:02 +01:00
|
|
|
with_skip {c.save} and c.clone_relations(self) # later with resque
|
2011-11-03 00:32:01 +00:00
|
|
|
end
|
2011-03-11 17:38:28 +00:00
|
|
|
end
|
|
|
|
|
2013-07-29 11:46:49 +01:00
|
|
|
# Checks locking of sync
|
2013-07-29 13:24:41 +01:00
|
|
|
def sync_lock_file_exists?
|
2013-07-29 13:33:31 +01:00
|
|
|
lock_file_actions :check, :sync
|
2013-07-26 15:41:39 +01:00
|
|
|
end
|
|
|
|
|
2013-07-29 11:46:49 +01:00
|
|
|
# Uses for locking sync
|
|
|
|
# Calls from UI
|
2013-07-29 13:24:41 +01:00
|
|
|
def add_sync_lock_file
|
2013-07-29 13:33:31 +01:00
|
|
|
lock_file_actions :add, :sync
|
2013-07-26 15:41:39 +01:00
|
|
|
end
|
|
|
|
|
2013-07-29 11:46:49 +01:00
|
|
|
# Uses for unlocking sync
|
|
|
|
# Calls from UI
|
2013-07-29 13:24:41 +01:00
|
|
|
def remove_sync_lock_file
|
2013-07-29 13:33:31 +01:00
|
|
|
lock_file_actions :remove, :sync
|
2013-07-26 15:41:39 +01:00
|
|
|
end
|
|
|
|
|
2013-07-29 11:46:49 +01:00
|
|
|
# Uses for locking publishing
|
|
|
|
# Calls from API
|
2013-07-29 13:24:41 +01:00
|
|
|
def add_repo_lock_file
|
2013-07-29 13:33:31 +01:00
|
|
|
lock_file_actions :add, :repo
|
2013-07-26 16:38:39 +01:00
|
|
|
end
|
|
|
|
|
2013-07-29 11:46:49 +01:00
|
|
|
# Uses for unlocking publishing
|
|
|
|
# Calls from API
|
2013-07-29 13:24:41 +01:00
|
|
|
def remove_repo_lock_file
|
2013-07-29 13:33:31 +01:00
|
|
|
lock_file_actions :remove, :repo
|
2013-07-26 16:38:39 +01:00
|
|
|
end
|
|
|
|
|
2013-07-29 16:35:16 +01:00
|
|
|
# Presence of `.repo.lock` file means that mirror is currently synchronising the repository state.
|
|
|
|
def repo_lock_file_exists?
|
|
|
|
lock_file_actions :check, :repo
|
|
|
|
end
|
|
|
|
|
2012-09-12 21:47:39 +01:00
|
|
|
def add_member(member, role = 'admin')
|
|
|
|
Relation.add_member(member, self, role)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_member(member)
|
|
|
|
Relation.remove_member(member, self)
|
|
|
|
end
|
|
|
|
|
2011-12-02 15:25:59 +00:00
|
|
|
class << self
|
|
|
|
def build_stub(platform)
|
|
|
|
rep = Repository.new
|
|
|
|
rep.platform = platform
|
|
|
|
rep
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-17 12:28:17 +00:00
|
|
|
def destroy
|
|
|
|
with_skip {super} # avoid cascade XML RPC requests
|
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
later :destroy, queue: :clone_build
|
2012-12-17 12:28:17 +00:00
|
|
|
|
2013-07-26 09:44:15 +01:00
|
|
|
def self.custom_sort(repos)
|
|
|
|
repos.select{ |r| SORT.keys.include?(r.name) }.sort{ |a,b| SORT[a.name] <=> SORT[b.name] } | repos.sort_by(&:name)
|
2013-07-25 19:00:42 +01:00
|
|
|
end
|
|
|
|
|
2011-11-24 21:46:19 +00:00
|
|
|
protected
|
2011-10-18 16:00:06 +01:00
|
|
|
|
2013-07-29 13:33:31 +01:00
|
|
|
def lock_file_actions(action, lock_file)
|
2013-07-26 15:41:39 +01:00
|
|
|
result = false
|
|
|
|
(['SRPMS'] << Arch.pluck(:name)).each do |arch|
|
2013-07-29 13:33:31 +01:00
|
|
|
path = "#{platform.path}/repository/#{arch}/#{name}/#{LOCK_FILE_NAMES[lock_file]}"
|
2013-07-26 15:41:39 +01:00
|
|
|
case action
|
2013-07-29 13:33:31 +01:00
|
|
|
when :add
|
2013-07-29 11:46:49 +01:00
|
|
|
result ||= FileUtils.touch(path) rescue nil
|
2013-07-29 13:33:31 +01:00
|
|
|
when :remove
|
2013-07-29 11:46:49 +01:00
|
|
|
result ||= FileUtils.rm_f(path)
|
2013-07-26 15:41:39 +01:00
|
|
|
when :check
|
|
|
|
return true if File.exist?(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2012-12-17 12:28:17 +00:00
|
|
|
def detele_directory
|
2013-04-25 17:37:07 +01:00
|
|
|
return unless platform
|
2013-01-17 20:51:12 +00:00
|
|
|
repository_path = platform.path << '/repository'
|
2012-12-17 12:28:17 +00:00
|
|
|
if platform.personal?
|
|
|
|
Platform.main.pluck(:name).each do |main_platform_name|
|
|
|
|
detele_repositories_directory "#{repository_path}/#{main_platform_name}"
|
|
|
|
end
|
2012-02-20 23:13:05 +00:00
|
|
|
else
|
2012-12-17 12:28:17 +00:00
|
|
|
detele_repositories_directory repository_path
|
2011-04-07 10:27:50 +01:00
|
|
|
end
|
2012-02-20 23:13:05 +00:00
|
|
|
end
|
2011-12-01 09:29:04 +00:00
|
|
|
|
2012-12-17 12:28:17 +00:00
|
|
|
def detele_repositories_directory(repository_path)
|
2013-01-18 12:32:47 +00:00
|
|
|
srpm_and_arches = (['SRPM'] << Arch.pluck(:name)).join(',')
|
|
|
|
`bash -c 'rm -rf #{repository_path}/{#{srpm_and_arches}}/#{name}'`
|
2012-02-20 23:13:05 +00:00
|
|
|
end
|
2012-12-17 12:28:17 +00:00
|
|
|
|
2011-03-11 16:08:41 +00:00
|
|
|
end
|