2011-09-15 18:56:20 +01:00
#require 'lib/build_server.rb'
2011-03-09 17:38:21 +00:00
class Platform < ActiveRecord :: Base
2011-10-31 14:37:13 +00:00
DOWNLOADS_PATH = RAILS_ROOT + '/public/downloads'
2011-10-23 22:39:44 +01:00
VISIBILITIES = [ 'open' , 'hidden' ]
2011-10-28 08:20:13 +01:00
2011-10-23 22:39:44 +01:00
relationable :as = > :target
2011-03-31 00:27:14 +01:00
belongs_to :parent , :class_name = > 'Platform' , :foreign_key = > 'parent_platform_id'
2011-10-17 15:27:07 +01:00
belongs_to :owner , :polymorphic = > true
2011-10-18 16:00:06 +01:00
2011-03-11 16:08:41 +00:00
has_many :repositories , :dependent = > :destroy
2011-04-11 09:35:08 +01:00
has_many :products , :dependent = > :destroy
2011-03-10 10:45:38 +00:00
2011-11-01 22:33:20 +00:00
has_many :objects , :as = > :target , :class_name = > 'Relation' , :dependent = > :destroy
2011-10-13 16:55:03 +01:00
has_many :members , :through = > :objects , :source = > :object , :source_type = > 'User'
has_many :groups , :through = > :objects , :source = > :object , :source_type = > 'Group'
2011-03-31 00:34:13 +01:00
validates :name , :presence = > true , :uniqueness = > true
2011-10-18 16:00:06 +01:00
validates :unixname , :uniqueness = > true , :presence = > true , :format = > { :with = > / ^[a-zA-Z0-9_]+$ / } , :allow_nil = > false , :allow_blank = > false
2011-10-27 17:31:54 +01:00
validates :distrib_type , :presence = > true , :allow_nil = > :false , :allow_blank = > false , :inclusion = > { :in = > APP_CONFIG [ 'distr_types' ] }
2011-03-09 17:38:21 +00:00
2011-10-30 22:59:03 +00:00
after_create :make_owner_rel
before_save :check_owner_rel
2011-10-31 00:11:49 +00:00
# before_save :create_directory
2011-10-27 22:24:38 +01:00
# after_destroy :remove_directory
2011-11-03 20:41:06 +00:00
before_create :xml_rpc_create , :unless = > lambda { Thread . current [ :skip ] }
2011-10-27 16:20:49 +01:00
before_destroy :xml_rpc_destroy
2011-10-16 21:50:56 +01:00
# before_update :check_freezing
2011-11-07 11:17:32 +00:00
after_create lambda {
unless self . hidden?
2011-11-07 13:38:22 +00:00
#add_downloads_symlink
2011-11-07 11:17:32 +00:00
mount_directory_for_rsync
end
}
2011-11-07 11:58:44 +00:00
after_destroy lambda {
unless self . hidden?
2011-11-07 13:38:22 +00:00
#remove_downloads_symlink
2011-11-07 11:58:44 +00:00
umount_directory_for_rsync
end
}
2011-03-09 17:38:21 +00:00
2011-10-23 22:39:44 +01:00
scope :by_visibilities , lambda { | v | { :conditions = > [ 'visibility in (?)' , v . join ( ',' ) ] } }
2011-11-03 08:02:55 +00:00
scope :open , where ( :visibility = > 'open' )
scope :hidden , where ( :visibility = > 'hidden' )
2011-10-27 14:04:03 +01:00
scope :main , where ( :platform_type = > 'main' )
scope :personal , where ( :platform_type = > 'personal' )
2011-10-23 22:39:44 +01:00
2011-10-27 17:52:07 +01:00
#attr_accessible :visibility
2011-04-11 09:35:08 +01:00
2011-11-01 13:22:41 +00:00
def urpmi_list ( host , pair = nil )
2011-11-03 08:02:55 +00:00
blank_pair = { :login = > 'login' , :pass = > 'password' }
pair = blank_pair if pair . blank?
2011-11-01 13:22:41 +00:00
urpmi_commands = [ ]
2011-11-03 08:02:55 +00:00
pls = Platform . main . open
2011-11-10 14:46:41 +00:00
#pls << self
2011-11-03 08:02:55 +00:00
pls . each do | pl |
local_pair = pl . id != self . id ? blank_pair : pair
2011-11-10 14:46:41 +00:00
#tail = (pl.id != self.id && pl.distrib_type == APP_CONFIG['distr_types'].first) ? "/$ARCH/main/release" : ""
tail = ( pl . distrib_type == APP_CONFIG [ 'distr_types' ] . first ) ? " /$ARCH/main/release " : " "
head = self . hidden? ? " http:// #{ local_pair [ :login ] } @ #{ local_pair [ :pass ] } : #{ host } /private/ " : " http:// #{ host } /downloads/ "
2011-11-03 08:02:55 +00:00
urpmi_commands << [
2011-11-10 14:37:39 +00:00
" urpmi.addmedia #{ head } #{ self . unixname } /repository/ #{ pl . unixname } #{ tail } " ,
2011-11-03 08:02:55 +00:00
pl . name
]
2011-11-01 13:22:41 +00:00
end
return urpmi_commands
end
2011-03-11 09:39:34 +00:00
def path
2011-03-11 10:25:50 +00:00
build_path ( unixname )
2011-03-11 09:39:34 +00:00
end
2011-10-28 08:20:13 +01:00
def hidden?
self . visibility == 'hidden'
end
2011-10-28 15:28:45 +01:00
def personal?
platform_type == 'personal'
end
2011-11-03 00:32:01 +00:00
def full_clone ( attrs ) # :name, :unixname, :owner
clone . tap do | c |
c . attributes = attrs
c . updated_at = nil ; c . created_at = nil # :id = nil
c . parent = self
2011-11-03 11:27:35 +00:00
new_attrs = { :platform_id = > nil }
c . repositories = repositories . map { | r | r . full_clone ( new_attrs . merge ( :owner_id = > attrs [ :owner_id ] , :owner_type = > attrs [ :owner_type ] ) ) }
2011-11-03 00:32:01 +00:00
c . products = products . map { | p | p . full_clone ( new_attrs ) }
2011-11-01 14:20:53 +00:00
end
2011-11-03 00:32:01 +00:00
end
# TODO * make it Delayed Job *
def make_clone ( attrs )
p = full_clone ( attrs )
2011-11-03 20:41:06 +00:00
begin
Thread . current [ :skip ] = true
p . save and xml_rpc_clone ( attrs [ :unixname ] )
ensure
Thread . current [ :skip ] = false
end
# (Thread.current[:skip] = true) and p.save and (Thread.current[:skip] = false or true) and xml_rpc_clone(attrs[:unixname])
2011-11-03 00:32:01 +00:00
p
2011-03-11 17:38:28 +00:00
end
2011-03-17 14:47:16 +00:00
def name
released? ? " #{ self [ :name ] } #{ I18n . t ( " layout.platforms.released_suffix " ) } " : self [ :name ]
end
2011-10-17 15:27:07 +01:00
def roles_of ( user )
objects . where ( :object_id = > user . id , :object_type = > user . class ) . map { | rel | rel . role } . reject { | r | r . nil? }
end
def add_role ( user , role )
roles = objects . where ( :object_id = > user . id , :object_type = > user . class ) . map { | rel | rel . role } . reject { | r | r . nil? }
unless roles . include? role
rel = Relation . create ( :object_type = > user . class . to_s , :object_id = > user . id ,
:target_type = > self . class . to_s , :target_id = > id )
rel . role = role
rel . save
end
end
2011-10-28 08:20:13 +01:00
def change_visibility
if ! self . hidden?
self . update_attribute ( :visibility , 'hidden' )
2011-11-07 13:38:22 +00:00
#remove_downloads_symlink
2011-11-07 11:17:32 +00:00
umount_directory_for_rsync
2011-10-28 08:20:13 +01:00
else
self . update_attribute ( :visibility , 'open' )
2011-11-07 13:38:22 +00:00
#add_downloads_symlink
2011-11-07 11:17:32 +00:00
mount_directory_for_rsync
2011-10-28 08:20:13 +01:00
end
end
2011-10-31 07:44:37 +00:00
2011-11-07 13:38:22 +00:00
#def add_downloads_symlink
# #raise "Personal platform path #{ symlink_downloads_path } already exists!" if File.exists?(symlink_downloads_path) && File.directory?(symlink_downloads_path)
# return true if File.exists?(symlink_downloads_path) && File.directory?(symlink_downloads_path)
# FileUtils.symlink path, symlink_downloads_path
#end
2011-10-31 07:44:37 +00:00
2011-11-07 11:17:32 +00:00
def mount_directory_for_rsync
#system("touch #{ Rails.root.join('tmp') }/mount_rsync")
2011-11-07 14:48:45 +00:00
FileUtils . rm_f " #{ Rails . root . join ( 'tmp' , 'umount' , self . unixname ) } " if File . exist? " #{ Rails . root . join ( 'tmp' , 'umount' , self . unixname ) } "
2011-11-07 13:38:22 +00:00
FileUtils . mkdir " #{ Rails . root . join ( 'tmp' , 'mount' ) } " unless File . exist? " #{ Rails . root . join ( 'tmp' , 'mount' ) } "
system ( " touch #{ Rails . root . join ( 'tmp' , 'mount' , self . unixname ) } " )
2011-11-07 11:17:32 +00:00
end
2011-11-07 13:38:22 +00:00
#def remove_downloads_symlink
# #raise "Personal platform path #{ symlink_downloads_path } does not exists!" if !(File.exists?(symlink_downloads_path) && File.directory?(symlink_downloads_path))
# return true if !(File.exists?(symlink_downloads_path) && File.directory?(symlink_downloads_path))
# FileUtils.rm_rf symlink_downloads_path
#end
2011-11-07 11:17:32 +00:00
def umount_directory_for_rsync
#system("touch #{ Rails.root.join('tmp') }/unmount_rsync")
2011-11-07 14:48:45 +00:00
FileUtils . rm_f " #{ Rails . root . join ( 'tmp' , 'mount' , self . unixname ) } " if File . exist? " #{ Rails . root . join ( 'tmp' , 'mount' , self . unixname ) } "
2011-11-07 13:38:22 +00:00
FileUtils . mkdir " #{ Rails . root . join ( 'tmp' , 'umount' ) } " unless File . exist? " #{ Rails . root . join ( 'tmp' , 'umount' ) } "
system ( " touch #{ Rails . root . join ( 'tmp' , 'umount' , self . unixname ) } " )
2011-11-07 11:17:32 +00:00
end
2011-10-17 15:27:07 +01:00
2011-03-09 17:38:21 +00:00
protected
2011-03-11 10:25:50 +00:00
def build_path ( dir )
2011-10-18 16:00:06 +01:00
File . join ( APP_CONFIG [ 'root_path' ] , 'platforms' , dir )
2011-03-11 10:25:50 +00:00
end
2011-04-07 10:10:46 +01:00
def git_path ( dir )
File . join ( build_path ( dir ) , 'git' )
end
2011-03-11 10:25:50 +00:00
def create_directory
exists = File . exists? ( path ) && File . directory? ( path )
raise " Directory #{ path } already exists " if exists
if new_record?
FileUtils . mkdir_p ( path )
elsif unixname_changed?
2011-04-07 10:10:46 +01:00
FileUtils . mv ( build_path ( unixname_was ) , build_path ( unixname ) )
2011-03-11 10:25:50 +00:00
end
2011-03-09 17:38:21 +00:00
end
2011-04-07 10:10:46 +01:00
2011-10-18 16:00:06 +01:00
def remove_directory
exists = File . exists? ( path ) && File . directory? ( path )
raise " Directory #{ path } didn't exists " unless exists
FileUtils . rm_rf ( path )
end
2011-04-07 10:10:46 +01:00
def xml_rpc_create
2011-10-27 23:13:24 +01:00
result = BuildServer . add_platform unixname , APP_CONFIG [ 'root_path' ] + '/platforms' , distrib_type
2011-10-27 16:20:49 +01:00
if result == BuildServer :: SUCCESS
return true
else
2011-10-28 00:39:37 +01:00
raise " Failed to create platform #{ name } with code #{ result } . Path: #{ build_path ( unixname ) } "
2011-10-27 16:20:49 +01:00
end
2011-04-07 10:10:46 +01:00
end
def xml_rpc_destroy
2011-10-27 16:20:49 +01:00
result = BuildServer . delete_platform unixname
if result == BuildServer :: SUCCESS
return true
else
2011-10-28 00:39:37 +01:00
raise " Failed to delete platform #{ unixname } with code #{ result } . "
2011-10-27 16:20:49 +01:00
end
2011-04-07 10:10:46 +01:00
end
2011-04-11 17:55:52 +01:00
2011-05-30 10:04:32 +01:00
def xml_rpc_clone ( new_unixname )
2011-10-27 22:14:08 +01:00
result = BuildServer . clone_platform new_unixname , self . unixname , APP_CONFIG [ 'root_path' ] + '/platforms'
2011-10-27 16:20:49 +01:00
if result == BuildServer :: SUCCESS
return true
else
2011-10-28 00:39:37 +01:00
raise " Failed to clone platform #{ name } with code #{ result } . Path: #{ build_path ( unixname ) } to platform #{ new_unixname } "
2011-10-27 16:20:49 +01:00
end
2011-05-30 10:04:32 +01:00
end
2011-04-11 17:55:52 +01:00
def check_freezing
if released_changed?
2011-04-14 12:24:41 +01:00
BuildServer . freeze_platform self . unixname
2011-04-11 17:55:52 +01:00
end
end
2011-10-28 08:20:13 +01:00
def symlink_downloads_path
" #{ DOWNLOADS_PATH } / #{ self . unixname } "
end
2011-10-30 22:59:03 +00:00
def make_owner_rel
add_owner owner
end
def check_owner_rel
if ! new_record? and owner_id_changed?
remove_owner owner_type_was . classify . find ( owner_id_was ) if owner_type_was
add_owner owner if owner
2011-10-29 16:56:01 +01:00
end
end
2011-03-09 17:38:21 +00:00
end