From 1509910a1f70deabd7c99b3b839a5f1cd61e7552 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Thu, 13 Dec 2012 20:13:13 +0400 Subject: [PATCH] #778: add file_system_worker for repositories and platforms --- app/models/build_list.rb | 2 +- lib/abf_worker/file_system_worker.rb | 19 +++++++ lib/abf_worker/runners/base.rb | 20 +++++++ lib/abf_worker/runners/platform.rb | 25 +++++++++ lib/abf_worker/runners/repository.rb | 82 ++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 lib/abf_worker/file_system_worker.rb create mode 100644 lib/abf_worker/runners/base.rb create mode 100644 lib/abf_worker/runners/platform.rb create mode 100644 lib/abf_worker/runners/repository.rb diff --git a/app/models/build_list.rb b/app/models/build_list.rb index d3af2ffa6..238e18fbf 100644 --- a/app/models/build_list.rb +++ b/app/models/build_list.rb @@ -268,7 +268,7 @@ class BuildList < ActiveRecord::Base type = save_to_platform.distrib_type archive = results.select{ |r| r['file_name'] =~ /.*\.tar\.gz$/}[0] - platform_path = "#{APP_CONFIG['root_path']}/platforms/#{save_to_platform.name}/repository" + platform_path = "#{save_to_platform.path}/repository" if save_to_platform.personal? platform_path << '/' platform_path << build_for_platform.name diff --git a/lib/abf_worker/file_system_worker.rb b/lib/abf_worker/file_system_worker.rb new file mode 100644 index 000000000..580602043 --- /dev/null +++ b/lib/abf_worker/file_system_worker.rb @@ -0,0 +1,19 @@ +module AbfWorker + class FileSystemWorker + @queue = :file_system_worker + + def self.perform(options) + id, action = options['id'], options['action'] + case options['type'] + when 'platform' + @runner = AbfWorker::Runners::Platform.new id, action + when 'repository' + @runner = AbfWorker::Runners::Repository.new id, action + when 'project' + @runner = AbfWorker::Runners::Project.new id, action + end + @runner.run + end + + end +end \ No newline at end of file diff --git a/lib/abf_worker/runners/base.rb b/lib/abf_worker/runners/base.rb new file mode 100644 index 000000000..2c8ecadab --- /dev/null +++ b/lib/abf_worker/runners/base.rb @@ -0,0 +1,20 @@ +module AbfWorker + module Runners + class Base + + # @param [String] action The action which should be run (create/destroy) + def initialize(action) + @action = action + end + + def run + send @action + end + + def self.mk_dir(path) + Dir.mkdir(path) unless File.exists?(path) + end + + end + end +end \ No newline at end of file diff --git a/lib/abf_worker/runners/platform.rb b/lib/abf_worker/runners/platform.rb new file mode 100644 index 000000000..6abea46bc --- /dev/null +++ b/lib/abf_worker/runners/platform.rb @@ -0,0 +1,25 @@ +module AbfWorker + module Runners + class Platform < AbfWorker::Runners::Base + + # @param [String] id The id of platform + def initialize(id, action) + super action + @platform = Platform.find id + end + + protected + + def create + platform_path = @platform.path + mk_dir(platform_path) + ['/projects', '/repository'].each{ |f| mk_dir(platform_path + f) } + end + + def destroy + system("rm -rf #{@platform.path}") + end + + end + end +end \ No newline at end of file diff --git a/lib/abf_worker/runners/repository.rb b/lib/abf_worker/runners/repository.rb new file mode 100644 index 000000000..c7ccecb89 --- /dev/null +++ b/lib/abf_worker/runners/repository.rb @@ -0,0 +1,82 @@ +module AbfWorker + module Runners + class Repository < AbfWorker::Runners::Base + + # @param [String] id The id of repository + def initialize(id, action) + super action + @repository = Repository.find id + end + + protected + + def create + platform = @repository.platform + repository_path = platform.path + repository_path << '/repository' + if platform.personal? + Platform.main.pluck.each do |main_platform_name| + create_file_tree "#{repository_path}/#{main_platform_name}", true + end + else + create_file_tree repository_path + end + end + + def destroy + platform = @repository.platform + repository_path = platform.path + repository_path << '/repository' + if platform.personal? + Platform.main.pluck.each do |main_platform_name| + destroy_repositories "#{repository_path}/#{main_platform_name}" + end + else + destroy_repositories repository_path + end + end + + def destroy_repositories(repository_path) + Arch.pluck(:name).each do |arch| + system("rm -rf #{repository_path}/#{arch}/#{@repository.name}") + end + system("rm -rf #{repository_path}/SRPMS/#{@repository.name}") + end + + def create_file_tree(repository_path, personal = false) + # platforms/rosa2012.1/repository + # platforms/test_personal/repository/rosa2012.1 + mk_dir repository_path + Arch.pluck(:name).each do |arch| + path = "#{repository_path}/#{arch}" + # platforms/rosa2012.1/repository/i586 + # platforms/test_personal/repository/rosa2012.1/i586 + mk_dir path + path << '/' << @repository.name + # platforms/rosa2012.1/repository/i586/main + # platforms/test_personal/repository/rosa2012.1/i586/main + mk_dir path + # platforms/rosa2012.1/repository/i586/main/release + # platforms/test_personal/repository/rosa2012.1/i586/main/release + mk_dir "#{path}/release" + # platforms/rosa2012.1/repository/i586/main/updates + mk_dir "#{path}/updates" unless personal + end + path = "#{repository_path}/SRPMS" + # platforms/rosa2012.1/repository/SRPMS + # platforms/test_personal/repository/rosa2012.1/SRPMS + mk_dir path + path << '/' << @repository.name + # platforms/rosa2012.1/repository/SRPMS/main + # platforms/test_personal/repository/rosa2012.1/SRPMS/main + mk_dir path + # platforms/rosa2012.1/repository/SRPMS/main/release + # platforms/test_personal/repository/rosa2012.1/SRPMS/main/release + mk_dir "#{path}/release" + # platforms/rosa2012.1/repository/SRPMS/main/updates + mk_dir "#{path}/updates" unless personal + end + + end + end +end \ No newline at end of file