2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
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
|
2012-04-26 02:38:33 +01:00
|
|
|
has_many :actors, :as => :target, :class_name => 'Relation', :dependent => :destroy
|
|
|
|
has_many :members, :through => :actors, :source => :actor, :source_type => 'User'
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2012-05-04 18:12:51 +01:00
|
|
|
has_and_belongs_to_many :advisories
|
|
|
|
|
2012-05-14 20:08:31 +01:00
|
|
|
has_many :packages, :class_name => "BuildList::Package", :dependent => :destroy
|
|
|
|
|
2012-05-18 16:12:51 +01:00
|
|
|
has_many :mass_builds
|
|
|
|
|
2012-02-22 12:35:40 +00:00
|
|
|
validates :description, :presence => true
|
2012-04-01 16:19:54 +01:00
|
|
|
validates :visibility, :presence => true, :inclusion => {:in => VISIBILITIES}
|
2012-06-08 19:20:02 +01:00
|
|
|
validates :name, :uniqueness => {:case_sensitive => false}, :presence => true, :format => { :with => /^[a-zA-Z0-9_\-\.]+$/ }
|
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
|
|
|
|
2012-02-22 22:57:43 +00:00
|
|
|
before_create :create_directory, :if => lambda {Thread.current[:skip]} # TODO remove this when core will be ready
|
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
|
2012-05-14 15:37:19 +01:00
|
|
|
|
2012-04-01 16:19:54 +01:00
|
|
|
after_update :freeze_platform
|
2012-05-14 15:37:19 +01:00
|
|
|
|
2012-05-15 09:53:41 +01:00
|
|
|
after_create lambda { symlink_directory unless hidden? }
|
|
|
|
after_destroy lambda { remove_symlink_directory unless hidden? }
|
|
|
|
|
2011-12-13 13:27:27 +00:00
|
|
|
after_update :update_owner_relation
|
2011-03-09 17:38:21 +00:00
|
|
|
|
2012-03-08 00:32:12 +00:00
|
|
|
scope :search_order, order("CHAR_LENGTH(name) ASC")
|
2012-04-05 18:11:02 +01:00
|
|
|
scope :search, lambda {|q| where("name ILIKE ?", "%#{q.to_s.strip}%")}
|
2012-03-06 15:53:04 +00:00
|
|
|
scope :by_visibilities, lambda {|v| where(:visibility => v)}
|
2012-03-28 00:58:03 +01:00
|
|
|
scope :opened, where(:visibility => 'open')
|
2011-11-03 08:02:55 +00:00
|
|
|
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
|
|
|
|
2012-05-03 15:08:02 +01:00
|
|
|
attr_accessible :name, :distrib_type, :parent_platform_id, :platform_type, :owner, :visibility, :description, :released
|
2012-04-01 16:19:54 +01:00
|
|
|
attr_readonly :name, :distrib_type, :parent_platform_id, :platform_type
|
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)
|
2012-05-15 09:53:41 +01:00
|
|
|
blank_pair = {:login => 'login', :pass => 'password'}
|
2011-11-03 08:02:55 +00:00
|
|
|
pair = blank_pair if pair.blank?
|
2011-11-17 23:14:25 +00:00
|
|
|
urpmi_commands = ActiveSupport::OrderedHash.new
|
|
|
|
|
2012-03-28 00:58:03 +01:00
|
|
|
Platform.main.opened.where(:distrib_type => APP_CONFIG['distr_types'].first).each do |pl|
|
2012-03-19 14:46:12 +00:00
|
|
|
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/"
|
2012-03-19 14:46:12 +00:00
|
|
|
Arch.all.each do |arch|
|
|
|
|
tail = "/#{arch.name}/main/release"
|
|
|
|
urpmi_commands[pl.name][arch.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
|
|
|
|
|
2012-05-15 10:33:27 +01:00
|
|
|
def symlink_path
|
2011-12-13 21:48:25 +00:00
|
|
|
Rails.root.join("public", "downloads", name)
|
|
|
|
end
|
|
|
|
|
2011-12-21 14:01:50 +00:00
|
|
|
def prefix_url(pub, options = {})
|
|
|
|
options[:host] ||= EventLog.current_controller.request.host_with_port rescue ::Rosa::Application.config.action_mailer.default_url_options[:host]
|
|
|
|
pub ? "http://#{options[:host]}/downloads" : "http://#{options[:login]}:#{options[:password]}@#{options[:host]}/private"
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_downloads_url(host = nil, arch = nil, repo = nil, suffix = nil)
|
|
|
|
downloads_url prefix_url(true, :host => host), arch, repo, suffix
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_downloads_url(login, password, host = nil, arch = nil, repo = nil, suffix = nil)
|
|
|
|
downloads_url prefix_url(false, :host => host, :login => login, :password => password), arch, repo, suffix
|
|
|
|
end
|
|
|
|
|
|
|
|
def downloads_url(prefix, arch = nil, repo = nil, suffix = nil)
|
|
|
|
"#{prefix}/#{name}/repository/".tap do |url|
|
|
|
|
url << "#{arch}/" if arch.present?
|
|
|
|
url << "#{repo}/" if repo.present?
|
|
|
|
url << "#{suffix}/" if suffix.present?
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2012-02-22 12:35:40 +00:00
|
|
|
def base_clone(attrs = {}) # :description, :name, :owner
|
2012-06-08 18:37:16 +01:00
|
|
|
dup.tap do |c|
|
|
|
|
attrs.each {|k,v| c.send("#{k}=", v)} # c.attributes = attrs
|
|
|
|
c.updated_at = nil; c.created_at = nil
|
|
|
|
c.parent = self; c.released = false
|
2011-11-01 14:20:53 +00:00
|
|
|
end
|
2011-11-03 00:32:01 +00:00
|
|
|
end
|
|
|
|
|
2012-02-22 12:35:40 +00:00
|
|
|
def clone_relations(from = parent)
|
2012-02-22 20:24:29 +00:00
|
|
|
self.repositories = from.repositories.map{|r| r.full_clone(:platform_id => id)}
|
2012-02-22 12:35:40 +00:00
|
|
|
self.products = from.products.map(&:full_clone)
|
|
|
|
end
|
|
|
|
|
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) and c.xml_rpc_clone # later with resque
|
2011-11-03 20:41:06 +00:00
|
|
|
end
|
2011-03-11 17:38:28 +00:00
|
|
|
end
|
2012-05-14 15:37:19 +01:00
|
|
|
|
2011-10-28 08:20:13 +01:00
|
|
|
def change_visibility
|
|
|
|
if !self.hidden?
|
|
|
|
self.update_attribute(:visibility, 'hidden')
|
2012-05-15 09:53:41 +01:00
|
|
|
remove_symlink_directory
|
2011-10-28 08:20:13 +01:00
|
|
|
else
|
|
|
|
self.update_attribute(:visibility, 'open')
|
2012-05-15 09:53:41 +01:00
|
|
|
symlink_directory
|
2011-10-28 08:20:13 +01:00
|
|
|
end
|
|
|
|
end
|
2011-11-20 21:58:53 +00:00
|
|
|
|
2012-02-22 22:57:43 +00:00
|
|
|
def create_directory
|
|
|
|
system("sudo mkdir -p -m 0777 #{path}")
|
|
|
|
end
|
|
|
|
|
2012-05-15 09:53:41 +01:00
|
|
|
def symlink_directory
|
2011-12-13 21:48:25 +00:00
|
|
|
# umount_directory_for_rsync # TODO ignore errors
|
2012-05-16 14:01:25 +01:00
|
|
|
system("ln -s #{path} #{symlink_path}")
|
2011-11-20 21:58:53 +00:00
|
|
|
Arch.all.each do |arch|
|
2011-12-21 14:01:50 +00:00
|
|
|
str = "country=Russian Federation,city=Moscow,latitude=52.18,longitude=48.88,bw=1GB,version=2011,arch=#{arch.name},type=distrib,url=#{public_downloads_url}\n"
|
2012-05-15 10:33:27 +01:00
|
|
|
File.open(File.join(symlink_path, "#{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
|
|
|
|
2012-05-15 09:53:41 +01:00
|
|
|
def remove_symlink_directory
|
2012-05-16 14:01:25 +01:00
|
|
|
system("rm -Rf #{symlink_path}")
|
2011-12-01 14:20:24 +00:00
|
|
|
end
|
|
|
|
|
2011-12-14 22:11:31 +00:00
|
|
|
def update_owner_relation
|
|
|
|
if owner_id_was != owner_id
|
2012-04-26 02:38:33 +01:00
|
|
|
r = relations.where(:actor_id => owner_id_was, :actor_type => owner_type_was)[0]
|
|
|
|
r.update_attributes(:actor_id => owner_id, :actor_type => owner_type)
|
2011-12-14 22:11:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-18 16:12:51 +01:00
|
|
|
def build_all(opts={})
|
2012-05-17 16:20:03 +01:00
|
|
|
# Set options to build all need
|
|
|
|
repositories = opts[:repositories] ? self.repositories.where(:id => opts[:repositories]) : self.repositories
|
|
|
|
arches = opts[:arches] ? Arch.where(:id => opts[:arches]) : Arch.all
|
2012-05-22 20:41:34 +01:00
|
|
|
auto_publish = opts[:auto_publish] || false
|
2012-05-17 16:20:03 +01:00
|
|
|
user = opts[:user]
|
2012-05-18 16:12:51 +01:00
|
|
|
mass_build_id = opts[:mass_build_id]
|
2012-05-17 16:20:03 +01:00
|
|
|
|
2012-04-26 10:15:57 +01:00
|
|
|
repositories.each do |rep|
|
|
|
|
rep.projects.find_in_batches(:batch_size => 2) do |group|
|
|
|
|
sleep 1
|
|
|
|
group.each do |p|
|
2012-05-17 16:20:03 +01:00
|
|
|
arches.map(&:name).each do |arch|
|
2012-04-26 10:15:57 +01:00
|
|
|
begin
|
2012-05-18 16:12:51 +01:00
|
|
|
p.build_for(self, user, arch, auto_publish, mass_build_id)
|
2012-04-26 10:15:57 +01:00
|
|
|
rescue RuntimeError, Exception
|
2012-06-16 19:27:46 +01:00
|
|
|
# p.async(:build_for, self, user, arch, auto_publish, mass_build_id) # TODO need this?
|
2012-04-26 10:15:57 +01:00
|
|
|
end
|
2012-03-23 23:53:07 +00:00
|
|
|
end
|
2012-02-23 18:32:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-06-18 15:16:53 +01:00
|
|
|
later :build_all, :loner => true, :queue => :clone_build
|
2012-02-23 18:32:56 +00:00
|
|
|
|
2012-02-22 22:57:43 +00:00
|
|
|
def destroy
|
|
|
|
with_skip {super} # avoid cascade XML RPC requests
|
|
|
|
end
|
2012-06-20 00:20:25 +01:00
|
|
|
later :destroy, :queue => :clone_build
|
2012-02-22 22:57:43 +00: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 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
|
|
|
|
2012-02-22 12:35:40 +00:00
|
|
|
def xml_rpc_clone(old_name = parent.name, new_name = name)
|
|
|
|
result = BuildServer.clone_platform new_name, old_name, APP_CONFIG['root_path'] + '/platforms'
|
2011-10-27 16:20:49 +01:00
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
2012-02-22 12:35:40 +00:00
|
|
|
raise "Failed to clone platform #{old_name} with code #{result}. Path: #{build_path(old_name)} to platform #{new_name}"
|
2011-10-27 16:20:49 +01:00
|
|
|
end
|
2011-05-30 10:04:32 +01:00
|
|
|
end
|
2012-06-18 15:16:53 +01:00
|
|
|
later :xml_rpc_clone, :loner => true, :queue => :clone_build
|
2011-05-30 10:04:32 +01:00
|
|
|
|
2012-04-01 16:19:54 +01:00
|
|
|
def freeze_platform
|
|
|
|
if released_changed? && released == true
|
|
|
|
result = BuildServer.freeze(name)
|
|
|
|
raise "Failed freeze platform #{name} with code #{result}" if result != BuildServer::SUCCESS
|
2012-05-14 15:37:19 +01:00
|
|
|
end
|
2011-04-11 17:55:52 +01:00
|
|
|
end
|
2011-03-09 17:38:21 +00:00
|
|
|
end
|