rosa-build/app/models/repository.rb

138 lines
3.6 KiB
Ruby
Raw Normal View History

2012-01-30 20:39:34 +00:00
# -*- encoding : utf-8 -*-
class Repository < ActiveRecord::Base
2013-07-26 09:44:15 +01:00
SORT = {'base' => 1, 'main' => 2, 'contrib' => 3, 'non-free' => 4, 'restricted' => 5}
belongs_to :platform
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'
has_many :project_to_repositories, :dependent => :destroy, :validate => true
has_many :projects, :through => :project_to_repositories
has_one :key_pair, :dependent => :destroy
has_many :build_lists, :foreign_key => :save_to_repository_id, :dependent => :destroy
validates :description, :presence => true
validates :name, :uniqueness => {:scope => :platform_id, :case_sensitive => false}, :presence => true, :format => {:with => /\A[a-z0-9_\-]+\z/}
2013-02-28 15:27:50 +00:00
scope :recent, order("#{table_name}.name ASC")
2013-04-23 22:49:40 +01:00
before_destroy :detele_directory
attr_accessible :name, :description, :publish_without_qa
attr_readonly :name, :platform_id
def base_clone(attrs = {})
2012-06-08 18:37:16 +01:00
dup.tap do |c|
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
end
end
def clone_relations(from)
with_skip do
from.projects.find_each {|p| self.projects << p}
end
end
2012-06-18 15:16:53 +01:00
later :clone_relations, :loner => true, :queue => :clone_build
def full_clone(attrs = {})
base_clone(attrs).tap do |c|
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
# Checks locking of sync
2013-07-26 15:41:39 +01:00
def sync_locked?
sync_actions :check
end
# Uses for locking sync
# Calls from UI
2013-07-26 15:41:39 +01:00
def lock_sync
sync_actions :lock
end
# Uses for unlocking sync
# Calls from UI
2013-07-26 15:41:39 +01:00
def unlock_sync
sync_actions :unlock
end
# Uses for locking publishing
# Calls from API
def start_sync
sync_actions :lock, '.repo.lock'
end
# Uses for unlocking publishing
# Calls from API
def stop_sync
sync_actions :unlock, '.repo.lock'
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
def destroy
with_skip {super} # avoid cascade XML RPC requests
end
later :destroy, :queue => :clone_build
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)
end
protected
2011-10-18 16:00:06 +01:00
def sync_actions(action, lock_file = '.sync.lock')
2013-07-26 15:41:39 +01:00
result = false
(['SRPMS'] << Arch.pluck(:name)).each do |arch|
path = "#{platform.path}/repository/#{arch}/#{name}/#{lock_file}"
2013-07-26 15:41:39 +01:00
case action
when :lock
result ||= FileUtils.touch(path) rescue nil
2013-07-26 15:41:39 +01:00
when :unlock
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
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'
if platform.personal?
Platform.main.pluck(:name).each do |main_platform_name|
detele_repositories_directory "#{repository_path}/#{main_platform_name}"
end
else
detele_repositories_directory repository_path
2011-04-07 10:27:50 +01:00
end
end
2011-12-01 09:29:04 +00:00
def detele_repositories_directory(repository_path)
srpm_and_arches = (['SRPM'] << Arch.pluck(:name)).join(',')
`bash -c 'rm -rf #{repository_path}/{#{srpm_and_arches}}/#{name}'`
end
end