2011-11-29 10:17:05 +00:00
|
|
|
class Platform < ActiveRecord::Base
|
2013-09-01 16:33:09 +01:00
|
|
|
extend FriendlyId
|
2014-03-14 19:36:32 +00:00
|
|
|
friendly_id :name, use: [:finders]
|
2013-09-01 16:33:09 +01:00
|
|
|
|
2014-03-11 08:58:36 +00:00
|
|
|
include FileStoreClean
|
|
|
|
include RegenerationStatus
|
|
|
|
include Owner
|
2014-03-11 07:39:25 +00:00
|
|
|
include EventLoggable
|
2014-09-30 21:05:53 +01:00
|
|
|
include EmptyMetadata
|
2015-02-18 23:34:07 +00:00
|
|
|
include DefaultBranchable
|
2015-03-12 22:43:13 +00:00
|
|
|
include Platform::Finders
|
2013-08-23 19:58:29 +01:00
|
|
|
|
2014-06-24 10:03:08 +01:00
|
|
|
self.per_page = 20
|
2011-10-23 22:39:44 +01:00
|
|
|
|
2014-09-30 21:05:53 +01:00
|
|
|
CACHED_CHROOT_PRODUCT_NAME = 'cached-chroot'
|
|
|
|
AUTOMATIC_METADATA_REGENERATIONS = %w(day week)
|
|
|
|
VISIBILITIES = [
|
|
|
|
VISIBILITY_OPEN = 'open',
|
|
|
|
VISIBILITY_HIDDEN = 'hidden'
|
|
|
|
]
|
|
|
|
NAME_PATTERN = /[\w\-\.]+/
|
|
|
|
HUMAN_STATUSES = HUMAN_STATUSES.clone.freeze
|
|
|
|
TYPES = [
|
|
|
|
TYPE_PERSONAL = 'personal',
|
|
|
|
TYPE_MAIN = 'main'
|
|
|
|
]
|
2013-08-23 19:58:29 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
belongs_to :parent, class_name: 'Platform', foreign_key: 'parent_platform_id'
|
|
|
|
belongs_to :owner, polymorphic: true
|
2011-10-18 16:00:06 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
has_many :repositories, dependent: :destroy
|
2014-10-27 21:22:35 +00:00
|
|
|
has_many :key_pairs, through: :repositories
|
2015-07-08 22:54:57 +01:00
|
|
|
has_many :projects, through: :repositories
|
2014-10-27 21:22:35 +00:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
has_many :products, dependent: :destroy
|
2014-03-11 12:40:44 +00:00
|
|
|
has_many :tokens, as: :subject, dependent: :destroy
|
|
|
|
has_many :platform_arch_settings, dependent: :destroy
|
2013-08-22 22:02:24 +01:00
|
|
|
has_many :repository_statuses
|
2011-03-10 10:45:38 +00: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'
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
has_many :packages, class_name: "BuildList::Package", dependent: :destroy
|
2012-05-14 20:08:31 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
has_many :mass_builds, foreign_key: :save_to_platform_id
|
2012-05-18 16:12:51 +01:00
|
|
|
|
2014-09-30 21:05:53 +01:00
|
|
|
validates :description,
|
2014-11-28 16:19:06 +00:00
|
|
|
presence: true,
|
|
|
|
length: { maximum: 10000 }
|
2014-09-30 21:05:53 +01:00
|
|
|
|
|
|
|
validates :visibility,
|
|
|
|
presence: true,
|
|
|
|
inclusion: { in: VISIBILITIES }
|
|
|
|
|
|
|
|
validates :platform_type,
|
|
|
|
presence: true,
|
|
|
|
inclusion: { in: TYPES }
|
|
|
|
|
|
|
|
validates :automatic_metadata_regeneration,
|
|
|
|
inclusion: { in: AUTOMATIC_METADATA_REGENERATIONS },
|
|
|
|
allow_blank: true
|
|
|
|
|
|
|
|
validates :name,
|
|
|
|
uniqueness: { case_sensitive: false },
|
|
|
|
presence: true,
|
2014-11-28 16:19:06 +00:00
|
|
|
format: { with: /\A#{NAME_PATTERN}\z/ },
|
2015-02-17 22:33:58 +00:00
|
|
|
length: { maximum: 100 }
|
|
|
|
|
|
|
|
validates :default_branch,
|
2015-02-18 23:34:07 +00:00
|
|
|
presence: true
|
2014-09-30 21:05:53 +01:00
|
|
|
|
|
|
|
validates :distrib_type,
|
|
|
|
presence: true,
|
|
|
|
inclusion: { in: APP_CONFIG['distr_types'] }
|
|
|
|
|
2014-03-11 07:39:25 +00:00
|
|
|
validate -> {
|
2012-10-10 18:45:56 +01:00
|
|
|
if released_was && !released
|
|
|
|
errors.add(:released, I18n.t('flash.platform.released_status_can_not_be_changed'))
|
|
|
|
end
|
|
|
|
}
|
2014-09-30 21:05:53 +01:00
|
|
|
|
2014-03-11 07:39:25 +00:00
|
|
|
validate -> {
|
2013-06-21 12:58:09 +01:00
|
|
|
if personal? && (owner_id_changed? || owner_type_changed?)
|
|
|
|
errors.add :owner, I18n.t('flash.platform.owner_can_not_be_changed')
|
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
}, on: :update
|
2011-03-09 17:38:21 +00:00
|
|
|
|
2012-12-14 17:19:23 +00:00
|
|
|
before_create :create_directory
|
2012-12-14 17:36:35 +00:00
|
|
|
before_destroy :detele_directory
|
2012-05-14 15:37:19 +01:00
|
|
|
|
2012-09-07 16:41:42 +01:00
|
|
|
after_update :freeze_platform_and_update_repos
|
|
|
|
after_update :update_owner_relation
|
2012-05-14 15:37:19 +01:00
|
|
|
|
2016-05-03 12:42:47 +01:00
|
|
|
after_commit -> { symlink_directory unless hidden? }, on: :create
|
2012-05-15 09:53:41 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
accepts_nested_attributes_for :platform_arch_settings, allow_destroy: true
|
2014-10-24 23:43:46 +01:00
|
|
|
|
2014-10-27 20:14:49 +00:00
|
|
|
attr_accessor :admin_id, :term
|
2014-09-30 21:05:53 +01:00
|
|
|
|
|
|
|
attr_readonly :name, :distrib_type, :parent_platform_id, :platform_type
|
2011-04-11 09:35:08 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
state_machine :status, initial: :ready do
|
2014-02-11 21:28:32 +00:00
|
|
|
|
|
|
|
after_transition on: :ready, do: :notify_users
|
|
|
|
|
2013-08-22 17:12:54 +01:00
|
|
|
event :ready do
|
2014-01-21 04:51:49 +00:00
|
|
|
transition regenerating: :ready
|
2013-08-22 17:12:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
event :regenerate do
|
2014-03-11 08:58:36 +00:00
|
|
|
transition ready: :waiting_for_regeneration, if: ->(p) { p.main? }
|
2013-08-22 17:12:54 +01:00
|
|
|
end
|
|
|
|
|
2013-08-23 19:58:29 +01:00
|
|
|
event :start_regeneration do
|
2014-01-21 04:51:49 +00:00
|
|
|
transition waiting_for_regeneration: :regenerating
|
2013-08-23 19:58:29 +01:00
|
|
|
end
|
|
|
|
|
2013-08-22 17:12:54 +01:00
|
|
|
HUMAN_STATUSES.each do |code,name|
|
2014-01-21 04:51:49 +00:00
|
|
|
state name, value: code
|
2013-08-22 17:12:54 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-21 08:15:20 +01:00
|
|
|
def clear
|
2012-06-20 19:02:42 +01:00
|
|
|
system("rm -Rf #{ APP_CONFIG['root_path'] }/platforms/#{ self.name }/repository/*")
|
|
|
|
end
|
|
|
|
|
2012-12-14 17:33:43 +00:00
|
|
|
def urpmi_list(host = nil, pair = nil, add_commands = true, repository_name = 'main')
|
|
|
|
host ||= default_host
|
2011-11-17 23:14:25 +00:00
|
|
|
urpmi_commands = ActiveSupport::OrderedHash.new
|
|
|
|
|
2012-12-25 10:44:40 +00:00
|
|
|
# TODO: rename method or create separate methods for mdv and rhel
|
2014-01-21 04:51:49 +00:00
|
|
|
# Platform.main.opened.where(distrib_type: APP_CONFIG['distr_types'].first).each do |pl|
|
2014-10-24 00:27:30 +01:00
|
|
|
arches = Arch.all.to_a
|
2012-12-25 10:44:40 +00:00
|
|
|
Platform.main.opened.each do |pl|
|
2012-03-19 14:46:12 +00:00
|
|
|
urpmi_commands[pl.name] = {}
|
2013-05-06 18:40:30 +01:00
|
|
|
# FIXME should support restricting access to the hidden platform
|
2014-10-24 00:27:30 +01:00
|
|
|
arches.each do |arch|
|
2012-12-14 17:33:43 +00:00
|
|
|
tail = "/#{arch.name}/#{repository_name}/release"
|
|
|
|
command = add_commands ? "urpmi.addmedia #{name} " : ''
|
2013-05-06 20:13:08 +01:00
|
|
|
command << "#{APP_CONFIG['downloads_url']}/#{name}/repository/#{pl.name}#{tail}"
|
2012-12-14 17:33:43 +00:00
|
|
|
urpmi_commands[pl.name][arch.name] = command
|
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-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
|
|
|
|
|
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
|
|
|
|
|
2013-05-16 18:44:56 +01:00
|
|
|
# Returns URL to repository, for example:
|
|
|
|
# - http://abf-downloads.rosalinux.ru/rosa-server2012/repository/x86_64/base/
|
|
|
|
# - http://abf-downloads.rosalinux.ru/uname_personal/repository/rosa-server2012/x86_64/base/
|
2013-05-16 16:25:03 +01:00
|
|
|
def public_downloads_url(subplatform_name = nil, arch = nil, repo = nil)
|
2013-05-06 18:40:30 +01:00
|
|
|
"#{APP_CONFIG['downloads_url']}/#{name}/repository/".tap do |url|
|
2013-05-16 16:25:03 +01:00
|
|
|
url << "#{subplatform_name}/" if subplatform_name.present?
|
2011-12-21 14:01:50 +00:00
|
|
|
url << "#{arch}/" if arch.present?
|
|
|
|
url << "#{repo}/" if repo.present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-28 08:20:13 +01:00
|
|
|
def hidden?
|
2014-09-30 21:05:53 +01:00
|
|
|
visibility == VISIBILITY_HIDDEN
|
2011-10-28 08:20:13 +01:00
|
|
|
end
|
|
|
|
|
2011-10-28 15:28:45 +01:00
|
|
|
def personal?
|
2014-09-30 21:05:53 +01:00
|
|
|
platform_type == TYPE_PERSONAL
|
2011-10-28 15:28:45 +01:00
|
|
|
end
|
|
|
|
|
2012-07-10 08:06:08 +01:00
|
|
|
def main?
|
2014-09-30 21:05:53 +01:00
|
|
|
platform_type == TYPE_MAIN
|
2012-07-10 08:06:08 +01:00
|
|
|
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)
|
2014-01-21 04:51:49 +00:00
|
|
|
self.repositories = from.repositories.map{|r| r.full_clone(platform_id: id)}
|
2013-02-27 21:55:30 +00:00
|
|
|
self.products = from.products.map(&:full_clone)
|
2012-02-22 12:35:40 +00:00
|
|
|
end
|
|
|
|
|
2012-02-22 20:24:29 +00:00
|
|
|
def full_clone(attrs = {})
|
|
|
|
base_clone(attrs).tap do |c|
|
2012-12-14 17:19:23 +00:00
|
|
|
with_skip {c.save} and c.clone_relations(self) and c.fs_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
|
2014-09-30 21:05:53 +01:00
|
|
|
if hidden?
|
|
|
|
update_attributes(visibility: VISIBILITY_OPEN)
|
2011-10-28 08:20:13 +01:00
|
|
|
else
|
2014-09-30 21:05:53 +01:00
|
|
|
update_attributes(visibility: VISIBILITY_HIDDEN)
|
2011-10-28 08:20:13 +01:00
|
|
|
end
|
|
|
|
end
|
2011-11-20 21:58:53 +00:00
|
|
|
|
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
|
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"
|
2016-05-22 13:25:00 +01:00
|
|
|
File.open(File.join(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
|
2014-09-30 21:05:53 +01:00
|
|
|
later :symlink_directory, queue: :middle
|
2011-11-20 21:58:53 +00:00
|
|
|
|
2012-05-15 09:53:41 +01:00
|
|
|
def remove_symlink_directory
|
2016-05-22 13:25:00 +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
|
2014-01-21 04:51:49 +00:00
|
|
|
r = relations.where(actor_id: owner_id_was, actor_type: owner_type_was).first
|
|
|
|
r.update_attributes(actor_id: owner_id, actor_type: owner_type)
|
2011-12-14 22:11:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-22 22:57:43 +00:00
|
|
|
def destroy
|
|
|
|
with_skip {super} # avoid cascade XML RPC requests
|
|
|
|
end
|
2014-04-15 19:41:06 +01:00
|
|
|
later :destroy, queue: :low
|
2012-02-22 22:57:43 +00:00
|
|
|
|
2013-03-22 16:58:53 +00:00
|
|
|
def default_host
|
|
|
|
EventLog.current_controller.request.host_with_port rescue ::Rosa::Application.config.action_mailer.default_url_options[:host]
|
|
|
|
end
|
2012-12-17 12:28:17 +00:00
|
|
|
|
2013-07-16 13:00:32 +01:00
|
|
|
# Checks access rights to platform and caching for 1 day.
|
2014-04-07 22:30:01 +01:00
|
|
|
def self.allowed?(path, token)
|
2013-07-16 13:00:32 +01:00
|
|
|
platform_name = path.gsub(/^[\/]+/, '')
|
|
|
|
.match(/^(#{NAME_PATTERN}\/|#{NAME_PATTERN}$)/)
|
|
|
|
|
|
|
|
return true unless platform_name
|
|
|
|
platform_name = platform_name[0].gsub(/\//, '')
|
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
Rails.cache.fetch([platform_name, token, :platform_allowed], expires_in: 2.minutes) do
|
2014-03-18 13:58:51 +00:00
|
|
|
platform = Platform.find_by name: platform_name
|
2013-07-16 13:00:32 +01:00
|
|
|
next false unless platform
|
|
|
|
next true unless platform.hidden?
|
2014-04-08 22:07:50 +01:00
|
|
|
return false if token.blank?
|
|
|
|
return true if platform.tokens.by_active.where(authentication_token: token).exists?
|
|
|
|
user = User.find_by(authentication_token: token)
|
2015-03-14 22:10:04 +00:00
|
|
|
!!(user && PlatformPolicy.new(user, platform).show?)
|
2014-04-07 22:30:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-08 22:07:50 +01:00
|
|
|
def cached_chroot(arch)
|
2014-04-07 22:30:01 +01:00
|
|
|
return false if personal?
|
2014-04-08 22:07:50 +01:00
|
|
|
Rails.cache.fetch([:cached_chroot, name, arch], expires_in: 10.minutes) do
|
2014-04-07 22:30:01 +01:00
|
|
|
product = products.where(name: CACHED_CHROOT_PRODUCT_NAME).first
|
|
|
|
next false unless product
|
|
|
|
pbl = product.product_build_lists.for_status(ProductBuildList::BUILD_COMPLETED).recent.first
|
|
|
|
next false unless pbl
|
2016-04-17 12:20:42 +01:00
|
|
|
result = pbl.results.find{ |r| r['file_name'] =~ /-#{arch}.tar.(gz|xz)$/ }
|
2014-04-08 22:07:50 +01:00
|
|
|
result.present? ? result['sha1'] : false
|
2013-07-16 13:00:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-11 20:37:14 +00:00
|
|
|
def self.autostart_metadata_regeneration(value)
|
|
|
|
Platform.main.where(automatic_metadata_regeneration: value).each(&:regenerate)
|
|
|
|
end
|
|
|
|
|
2015-03-26 00:26:24 +00:00
|
|
|
def self.availables_main_platforms(user)
|
2014-02-17 21:32:47 +00:00
|
|
|
p_ids = Rails.cache.fetch([:availables_main_platforms, user], expires_in: 10.minutes) do
|
2015-03-26 00:26:24 +00:00
|
|
|
PlatformPolicy::Scope.new(user, Platform).show.main.joins(:repositories).
|
2014-02-17 21:39:49 +00:00
|
|
|
where('repositories.id IS NOT NULL').uniq.pluck(:id)
|
2014-02-17 21:32:47 +00:00
|
|
|
end
|
|
|
|
Platform.preload(:repositories).where(id: p_ids).order(:name)
|
|
|
|
end
|
|
|
|
|
2011-03-09 17:38:21 +00:00
|
|
|
protected
|
|
|
|
|
2012-12-17 12:28:17 +00:00
|
|
|
def create_directory
|
|
|
|
system("mkdir -p -m 0777 #{build_path([name, 'repository'])}")
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2012-12-14 17:19:23 +00:00
|
|
|
def detele_directory
|
|
|
|
FileUtils.rm_rf path
|
2011-04-07 10:10:46 +01:00
|
|
|
end
|
|
|
|
|
2012-12-14 17:19:23 +00:00
|
|
|
def fs_clone(old_name = parent.name, new_name = name)
|
|
|
|
FileUtils.cp_r "#{parent.path}/repository", path
|
2011-05-30 10:04:32 +01:00
|
|
|
end
|
2014-04-15 19:41:06 +01:00
|
|
|
later :fs_clone, queue: :low
|
2011-05-30 10:04:32 +01:00
|
|
|
|
2012-09-07 16:41:42 +01:00
|
|
|
def freeze_platform_and_update_repos
|
2012-04-01 16:19:54 +01:00
|
|
|
if released_changed? && released == true
|
2014-01-21 04:51:49 +00:00
|
|
|
repositories.update_all(publish_without_qa: false)
|
2012-05-14 15:37:19 +01:00
|
|
|
end
|
2011-04-11 17:55:52 +01:00
|
|
|
end
|
2014-02-11 21:28:32 +00:00
|
|
|
|
|
|
|
def notify_users
|
|
|
|
users = members.includes(:notifier).select{ |u| u.notifier.can_notify? }
|
|
|
|
users.each{ |u| UserMailer.metadata_regeneration_notification(self, u).deliver }
|
|
|
|
end
|
|
|
|
|
2011-03-09 17:38:21 +00:00
|
|
|
end
|