2011-11-29 10:17:05 +00:00
|
|
|
#require 'lib/build_server.rb'
|
|
|
|
class Platform < ActiveRecord::Base
|
2011-10-23 22:39:44 +01:00
|
|
|
VISIBILITIES = ['open', 'hidden']
|
|
|
|
|
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-17 19:34:02 +00:00
|
|
|
has_many :relations, :as => :target, :dependent => :destroy
|
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-11-28 15:41:42 +00:00
|
|
|
validates :description, :presence => true, :uniqueness => true
|
2011-12-12 14:10:39 +00:00
|
|
|
if !Rails.env.development?
|
|
|
|
validates :name, :uniqueness => true, :presence => true, :format => { :with => /^[a-zA-Z0-9_]+$/ }
|
|
|
|
end
|
2011-11-24 21:46:19 +00:00
|
|
|
validates :distrib_type, :presence => true, :inclusion => {:in => APP_CONFIG['distr_types']}
|
2011-03-09 17:38:21 +00:00
|
|
|
|
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-24 21:46:19 +00:00
|
|
|
after_create lambda { mount_directory_for_rsync unless hidden? }
|
|
|
|
after_destroy lambda { umount_directory_for_rsync unless hidden? }
|
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-24 21:46:19 +00:00
|
|
|
include Modules::Models::Owner
|
|
|
|
|
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-17 23:14:25 +00:00
|
|
|
urpmi_commands = ActiveSupport::OrderedHash.new
|
|
|
|
|
|
|
|
Platform.main.open.each do |pl|
|
|
|
|
urpmi_commands[pl.name] = []
|
2011-11-03 08:02:55 +00:00
|
|
|
local_pair = pl.id != self.id ? blank_pair : pair
|
2011-11-17 23:14:25 +00:00
|
|
|
head = hidden? ? "http://#{local_pair[:login]}@#{local_pair[:pass]}:#{host}/private/" : "http://#{host}/downloads/"
|
|
|
|
if pl.distrib_type == APP_CONFIG['distr_types'].first
|
|
|
|
Arch.all.each do |arch|
|
|
|
|
tail = "/#{arch.name}/main/release"
|
2011-11-28 15:41:42 +00:00
|
|
|
urpmi_commands[pl.name] << "urpmi.addmedia #{name} #{head}#{name}/repository/#{pl.name}#{tail}"
|
2011-11-17 23:14:25 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
tail = ''
|
2011-11-28 15:41:42 +00:00
|
|
|
urpmi_commands[pl.name] << "urpmi.addmedia #{name} #{head}#{name}/repository/#{pl.name}#{tail}"
|
2011-11-17 23:14:25 +00:00
|
|
|
end
|
2011-11-01 13:22:41 +00:00
|
|
|
end
|
2011-11-17 23:14:25 +00:00
|
|
|
|
2011-11-01 13:22:41 +00:00
|
|
|
return urpmi_commands
|
|
|
|
end
|
|
|
|
|
2011-03-11 09:39:34 +00:00
|
|
|
def path
|
2011-11-28 15:41:42 +00:00
|
|
|
build_path(name)
|
2011-03-11 09:39:34 +00:00
|
|
|
end
|
|
|
|
|
2011-10-28 08:20:13 +01:00
|
|
|
def hidden?
|
2011-11-24 21:46:19 +00:00
|
|
|
visibility == 'hidden'
|
2011-10-28 08:20:13 +01:00
|
|
|
end
|
|
|
|
|
2011-10-28 15:28:45 +01:00
|
|
|
def personal?
|
|
|
|
platform_type == 'personal'
|
|
|
|
end
|
|
|
|
|
2011-11-28 15:41:42 +00:00
|
|
|
def full_clone(attrs) # :description, :name, :owner
|
2011-11-03 00:32:01 +00:00
|
|
|
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
|
2011-11-28 15:41:42 +00:00
|
|
|
p.save and xml_rpc_clone(attrs[:name])
|
2011-11-03 20:41:06 +00:00
|
|
|
ensure
|
|
|
|
Thread.current[:skip] = false
|
|
|
|
end
|
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-28 08:20:13 +01:00
|
|
|
|
|
|
|
def change_visibility
|
|
|
|
if !self.hidden?
|
|
|
|
self.update_attribute(:visibility, 'hidden')
|
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 11:17:32 +00:00
|
|
|
mount_directory_for_rsync
|
2011-10-28 08:20:13 +01:00
|
|
|
end
|
|
|
|
end
|
2011-11-20 21:58:53 +00:00
|
|
|
|
2011-11-07 11:17:32 +00:00
|
|
|
def mount_directory_for_rsync
|
2011-12-08 07:42:12 +00:00
|
|
|
#FileUtils.rm_rf "#{ Rails.root.join('tmp', 'umount', self.name) }" if File.exist? "#{ Rails.root.join('tmp', 'umount', name) }"
|
|
|
|
#FileUtils.mkdir_p "#{ Rails.root.join('tmp', 'mount', name) }"
|
2011-12-08 20:41:01 +00:00
|
|
|
system("sudo mkdir -p \"#{ Rails.root.join("public", "downloads") }/#{ name }\"")
|
|
|
|
system("sudo mount --bind \"/home/share/platforms/#{ name }\" \"#{ Rails.root.join("public", "downloads") }/#{ name }\"")
|
|
|
|
#system("sudo cp -f /srv/rosa_build/current/tmp/mount/#{ name }/* /home/share/platforms/#{ name }/repository/")
|
|
|
|
#system("sudo rm -Rf \"/srv/rosa_build/current/tmp/mount/#{ name }\"")
|
2011-11-20 21:58:53 +00:00
|
|
|
Arch.all.each do |arch|
|
2011-11-22 18:47:45 +00:00
|
|
|
host = EventLog.current_controller.request.host_with_port rescue ::Rosa::Application.config.action_mailer.default_url_options[:host]
|
2011-11-28 15:41:42 +00:00
|
|
|
url = "http://#{host}/downloads/#{name}/repository/"
|
2011-11-20 21:58:53 +00:00
|
|
|
str = "country=Russian Federation,city=Moscow,latitude=52.18,longitude=48.88,bw=1GB,version=2011,arch=#{arch.name},type=distrib,url=#{url}\n"
|
2011-12-08 20:41:01 +00:00
|
|
|
File.open(Rails.root.join("public", 'downloads', name, "#{name}.#{arch.name}.list"), 'w') {|f| f.write(str) }
|
2011-11-20 21:58:53 +00:00
|
|
|
end
|
2011-11-07 11:17:32 +00:00
|
|
|
end
|
2011-11-20 21:58:53 +00:00
|
|
|
|
2011-11-07 11:17:32 +00:00
|
|
|
def umount_directory_for_rsync
|
2011-12-08 20:41:01 +00:00
|
|
|
system("umount \"#{ Rails.root.join("public", "downloads") }/#{ name }\"")
|
|
|
|
system("rm -Rf \"#{ Rails.root.join("public", "downloads") }/#{ name }\"")
|
|
|
|
#system("rm -Rf \"/srv/rosa_build/current/tmp/umount/#{ name }\"")
|
2011-12-08 07:42:12 +00:00
|
|
|
#FileUtils.rm_rf "#{ Rails.root.join('tmp', 'mount', name) }" if File.exist? "#{ Rails.root.join('tmp', 'mount', name) }"
|
|
|
|
#FileUtils.mkdir_p "#{ Rails.root.join('tmp', 'umount', name) }"
|
2011-11-07 11:17:32 +00:00
|
|
|
end
|
2011-10-17 15:27:07 +01:00
|
|
|
|
2011-12-01 14:20:24 +00:00
|
|
|
def make_admin_relation(user_id)
|
|
|
|
r = self.relations.build :object_id => user_id, :object_type => 'User', :role => 'admin'
|
|
|
|
r.save
|
|
|
|
end
|
|
|
|
|
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 xml_rpc_create
|
2011-11-28 15:41:42 +00:00
|
|
|
result = BuildServer.add_platform name, APP_CONFIG['root_path'] + '/platforms' , distrib_type
|
2011-10-27 16:20:49 +01:00
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
2011-11-28 15:41:42 +00:00
|
|
|
raise "Failed to create platform #{name} with code #{result}. Path: #{build_path(name)}"
|
2011-10-27 16:20:49 +01:00
|
|
|
end
|
2011-04-07 10:10:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def xml_rpc_destroy
|
2011-11-28 15:41:42 +00:00
|
|
|
result = BuildServer.delete_platform name
|
2011-10-27 16:20:49 +01:00
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
2011-11-28 15:41:42 +00:00
|
|
|
raise "Failed to delete platform #{name} 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-11-28 15:41:42 +00:00
|
|
|
def xml_rpc_clone(new_name)
|
|
|
|
result = BuildServer.clone_platform new_name, self.name, APP_CONFIG['root_path'] + '/platforms'
|
2011-10-27 16:20:49 +01:00
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
2011-11-28 15:41:42 +00:00
|
|
|
raise "Failed to clone platform #{name} with code #{result}. Path: #{build_path(name)} to platform #{new_name}"
|
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-11-28 15:41:42 +00:00
|
|
|
BuildServer.freeze_platform self.name
|
2011-04-11 17:55:52 +01:00
|
|
|
end
|
|
|
|
end
|
2011-03-09 17:38:21 +00:00
|
|
|
end
|