mock build
This commit is contained in:
@@ -27,6 +27,12 @@ end
|
||||
class BuildTask < Sequel::Model(:buildtask)
|
||||
end
|
||||
|
||||
class Rpms < Sequel::Model(:rpms)
|
||||
end
|
||||
|
||||
class BuildRpms < Sequel::Model(:build_rpm)
|
||||
end
|
||||
|
||||
class DBase
|
||||
attr :error, :last_id, :cfg
|
||||
|
||||
@@ -34,7 +40,7 @@ class DBase
|
||||
@cfg = cfg
|
||||
end
|
||||
|
||||
def creategit(project_name, description, cfg)
|
||||
def creategit(project_name, description)
|
||||
@error = nil
|
||||
data = Repos.where(reponame: project_name).first
|
||||
if data.nil?
|
||||
@@ -228,7 +234,7 @@ class DBase
|
||||
def create_build_task(prj_id, git_id, proj_path)
|
||||
id = BuildTask.insert(repo_id: git_id.to_i, proj_id: prj_id.to_i, signpath: "", logpath: "", errlogpath: "", result: 0)
|
||||
@last_id = id
|
||||
BuildTask.where(id: id).update(logpath: File.join(proj_path, "#{id}"))
|
||||
BuildTask.where(id: id).update(logpath: File.join(proj_path, "#{id}"), errlogpath: File.join(proj_path, "#{id}", "process.log"))
|
||||
end
|
||||
|
||||
def update_build_task_status(build_id, status)
|
||||
@@ -250,4 +256,10 @@ class DBase
|
||||
def after_fork()
|
||||
Sequel.connect(@cfg.get_db)
|
||||
end
|
||||
|
||||
def save_rpm(build_id, path_to_rpm, rpm_name, git_id)
|
||||
id = Rpms.insert(savepath: path_to_rpm, rpmname: rpm_name, sign: 0, signpath: "", repo_id: git_id.to_i)
|
||||
@last_id = id
|
||||
BuildRpms.insert(build_id: build_id.to_i, rpm_id: id)
|
||||
end
|
||||
end
|
||||
|
||||
161
classes/mock.rb
161
classes/mock.rb
@@ -2,6 +2,7 @@ require_relative "spork"
|
||||
require_relative "runner"
|
||||
require "fileutils"
|
||||
require "logger"
|
||||
require_relative "repomanage"
|
||||
|
||||
BUILD_STRUCTURE = {
|
||||
:SRC => "src",
|
||||
@@ -10,12 +11,10 @@ BUILD_STRUCTURE = {
|
||||
:RESULT_SRPM => "result_srpm",
|
||||
}
|
||||
|
||||
#mock -r /home/alexey/workspace/ruby-projects/mock-gui/projects/prjt1.prj/configs/prjt1.cfg result/bayrepo-neuro-farm-0.1-2.src.rpm --resultdir result2/ --isolation simple
|
||||
|
||||
class MockManager
|
||||
attr :path, :config, :error, :last_status, :last_pid, :prep_dir, :db, :resultpath, :process_log, :repo_path, :git_path, :build_id, :log, :recips, :spec, :repo_lock
|
||||
attr :path, :config, :error, :last_status, :last_pid, :prep_dir, :db, :resultpath, :process_log, :repo_path, :git_path, :build_id, :log, :recips, :spec, :repo_lock, :git_id
|
||||
|
||||
def initialize(path, config, cfg_counter_path, db, result_path, repo_path, git_path, build_id, recips, spec_file, repo_lock)
|
||||
def initialize(path, config, cfg_counter_path, db, result_path, repo_path, git_path, build_id, recips, spec_file, repo_lock, git_id)
|
||||
@error = nil
|
||||
unless File.exist? (path)
|
||||
Dir.mkdir(path)
|
||||
@@ -31,6 +30,7 @@ class MockManager
|
||||
@recips = recips
|
||||
@spec = spec_file
|
||||
@repo_lock = repo_lock
|
||||
@git_id = git_id
|
||||
|
||||
File.open(cfg_counter_path, "r+") do |f|
|
||||
f.flock(File::LOCK_EX)
|
||||
@@ -52,10 +52,6 @@ class MockManager
|
||||
@process_log
|
||||
end
|
||||
|
||||
def finalize_build_task()
|
||||
@log.close
|
||||
end
|
||||
|
||||
def clean_build
|
||||
@log.info("Удаление временной сборочной среды #{@path}")
|
||||
FileUtils.rm_rf(@path)
|
||||
@@ -91,7 +87,7 @@ class MockManager
|
||||
@recips.each_with_index do |item, index|
|
||||
@log.info("Формируем рецепт #{item[:filepath]}")
|
||||
rcp_name = "#{index}rcp_#{item[:filepath]}"
|
||||
File.open(File.join(@prep_dir, BUILD_STRUCTURE[:SRC], rcp_name)) do |f|
|
||||
File.open(File.join(@prep_dir, BUILD_STRUCTURE[:SRC], rcp_name), "w") do |f|
|
||||
f.write(item[:content])
|
||||
end
|
||||
Dir.chdir(File.join(@prep_dir, BUILD_STRUCTURE[:SRC])) do
|
||||
@@ -100,6 +96,7 @@ class MockManager
|
||||
cmd = Runner.new(cmd_args, @log)
|
||||
cmd.run_clean
|
||||
@error = true if cmd.exit_status != 0
|
||||
@log.error("Ошибка операции") if @error
|
||||
end
|
||||
break if @error
|
||||
end
|
||||
@@ -110,11 +107,11 @@ class MockManager
|
||||
spec_file = File.join(@prep_dir, BUILD_STRUCTURE[:SRC], @spec)
|
||||
if File.exist?(spec_file)
|
||||
Dir.chdir(File.join(@prep_dir, BUILD_STRUCTURE[:SRC])) do
|
||||
script = File.join(@prep_dir, BUILD_STRUCTURE[:SRC], rcp_name)
|
||||
cmd_args = %Q(/usr/bin/mock -r #{@config} --buildsrpm --spec #{spec_file} --sources #{File.join(@prep_dir, BUILD_STRUCTURE[:SRC])} --resultdir #{File.join(@prep_dir, BUILD_STRUCTURE[:RESULT_SRPM])} --isolation=simple --disable-plugin=ccache")
|
||||
cmd = Runner.new(cmd_args, @log)
|
||||
cmd.run_clean
|
||||
@error = true if cmd.exit_status != 0
|
||||
@log.error("Ошибка операции") if @error
|
||||
end
|
||||
else
|
||||
@error = true
|
||||
@@ -123,40 +120,138 @@ class MockManager
|
||||
end
|
||||
|
||||
def build_rpm()
|
||||
@log.info("Начало сборки пакетов")
|
||||
srpm_result_dir = File.join(@prep_dir, BUILD_STRUCTURE[:RESULT_SRPM])
|
||||
srpms = get_src_rpm_files_in_dir(srpm_result_dir)
|
||||
if srpms.nil? || srpms.length == 0
|
||||
@error = true
|
||||
@log.info("Нечего собирать, нет src.rpm пакетов")
|
||||
else
|
||||
srpm = srpms.first
|
||||
path_srpm = File.join(@prep_dir, srpm)
|
||||
cmd_args = %Q(/usr/bin/mock -r #{@config} #{path_srpm} --resultdir #{File.join(@prep_dir, BUILD_STRUCTURE[:RESULT])} --isolation simple)
|
||||
cmd = Runner.new(cmd_args, @log)
|
||||
cmd.run_clean
|
||||
@error = true if cmd.exit_status != 0
|
||||
@log.error("Ошибка операции") if @error
|
||||
end
|
||||
end
|
||||
|
||||
def save_logs()
|
||||
FileUtils.mkdir_p(File.join(@resultpath, "#{@build_id}"))
|
||||
src_result = File.join(@prep_dir, BUILD_STRUCTURE[:RESULT_SRPM])
|
||||
rpm_result = File.join(@prep_dir, BUILD_STRUCTURE[:RESULT])
|
||||
if File.exist?(src_result)
|
||||
logs = get_log_paths(src_result)
|
||||
logs.each do |item|
|
||||
src = File.join(src_result, item)
|
||||
dst_dir = File.dirname(item)
|
||||
dst_fname = File.basename(item)
|
||||
dst = File.join(@resultpath, "#{@build_id}", dst_dir, "srpms_build_#{dst_fname}")
|
||||
FileUtils.cp_r(src, dst, verbose: true, remove_destination: true)
|
||||
end
|
||||
end
|
||||
if File.exist?(rpm_result)
|
||||
logs = get_log_paths(rpm_result)
|
||||
logs.each do |item|
|
||||
src = File.join(rpm_result, item)
|
||||
dst_dir = File.dirname(item)
|
||||
dst_fname = File.basename(item)
|
||||
dst = File.join(@resultpath, "#{@build_id}", dst_dir, "rpm_build_#{dst_fname}")
|
||||
FileUtils.cp_r(src, dst, verbose: true, remove_destination: true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def save_rpms()
|
||||
File.open(@repo_lock, File::RDWR | File::CREAT) do |f|
|
||||
f.flock(File::LOCK_EX)
|
||||
# выклдака пакетов и пересоздание repodata
|
||||
@log.info("Формирование репозитория")
|
||||
repo = RepoManager.new(@repo_path)
|
||||
rpms = get_rpm_paths(File.join(@prep_dir, BUILD_STRUCTURE[:RESULT]))
|
||||
if rpms.nil? || rpms.length == 0
|
||||
@error = true
|
||||
@log.error("Пакеты не найдены")
|
||||
else
|
||||
prep_rpms = []
|
||||
rpms.each do |item|
|
||||
res = {}
|
||||
rpm_path = File.join(@prep_dir, BUILD_STRUCTURE[:RESULT], item)
|
||||
result = repo.get_rpm_info(rpm_path)
|
||||
if result[:error].nil?
|
||||
res[:src] = rpm_path
|
||||
res[:name] = result[:pkginfo].to_s
|
||||
arch = result[:pkginfo].arch
|
||||
if rpm_path =~ /\.src\.rpm$/
|
||||
res[:dst] = File.join(@repo_path, "SRPMS", File.basename(rpm_path))
|
||||
elsif rpm_path =~ /(debuginfo.+rpm$)|(debugsource.+rpm$)/
|
||||
res[:dst] = File.join(@repo_path, "Debug", File.basename(rpm_path))
|
||||
else
|
||||
if arch.nil? || arch.strip == ""
|
||||
arch = "noarch"
|
||||
end
|
||||
res[:dst] = File.join(@repo_path, arch, File.basename(rpm_path))
|
||||
end
|
||||
prep_rpms << res
|
||||
else
|
||||
@error = true
|
||||
@log.error("Ошибка пакета #{rpm_path}")
|
||||
break
|
||||
end
|
||||
end
|
||||
if @error == false
|
||||
File.open(@repo_lock, File::RDWR | File::CREAT) do |f|
|
||||
f.flock(File::LOCK_EX)
|
||||
# выклдака пакетов и пересоздание repodata
|
||||
prep_rpms.each do |item|
|
||||
FileUtils.cp_r(item[:src], item[:dst], verbose: true, remove_destination: true)
|
||||
@db.save_rpm(@build_id, item[:dst], item[:name], @git_id)
|
||||
@log.info("Копируется пакет #{item[:src]} в репозиторий #{item[:dst]}")
|
||||
end
|
||||
repo.create_repo
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def save_prg_log()
|
||||
FileUtils.mkdir_p(File.join(@resultpath, "#{@build_id}"))
|
||||
if File.exist?(@process_log)
|
||||
dst = File.join(@resultpath, "#{@build_id}")
|
||||
FileUtils.cp_r(@process_log, dst, verbose: true, remove_destination: true)
|
||||
end
|
||||
end
|
||||
|
||||
def build_task()
|
||||
@error = false
|
||||
@db.before_fork
|
||||
spock = Spork.spork(:logger => log) do
|
||||
@db.after_fork
|
||||
$stdout = File.open(@process_log, "w")
|
||||
@log = Logger.new($stdout)
|
||||
if @sepc == ""
|
||||
@error = true
|
||||
@log.error("Не могу найти spec файл")
|
||||
end
|
||||
prepare_structure if @error == false
|
||||
prepare_src if @error == false
|
||||
prepare_source if @error == false
|
||||
prepare_src_rpm if @error == false
|
||||
build_rpm if @error == false
|
||||
save_logs
|
||||
save_rpms if @error == false
|
||||
clean_build
|
||||
@log.close
|
||||
#@db.before_fork
|
||||
#spock = Spork.spork(:logger => log) do
|
||||
# @db.after_fork
|
||||
#$stdout = File.open(@process_log, "w")
|
||||
@log = Logger.new($stdout)
|
||||
if @spec == ""
|
||||
@error = true
|
||||
@log.error("Не могу найти spec файл")
|
||||
end
|
||||
@db.after_fork
|
||||
spock
|
||||
#begin
|
||||
prepare_structure if @error == false
|
||||
prepare_src if @error == false
|
||||
prepare_source if @error == false
|
||||
prepare_src_rpm if @error == false
|
||||
build_rpm if @error == false
|
||||
save_logs
|
||||
save_rpms if @error == false
|
||||
#rescue => e
|
||||
# puts e
|
||||
#end
|
||||
save_prg_log
|
||||
clean_build
|
||||
@log.close
|
||||
if @error
|
||||
@db.update_build_task_status(@build_id, 1)
|
||||
else
|
||||
@db.update_build_task_status(@build_id, 2)
|
||||
end
|
||||
#end
|
||||
#@db.after_fork
|
||||
#spock
|
||||
end
|
||||
end
|
||||
|
||||
@@ -305,11 +305,10 @@ class ProjectsActions
|
||||
git_source = File.join(proj_path, PROJECTS_STRUCTURE[:SRC], git_name[:reponame])
|
||||
@db.create_build_task(prj_id, git_id, build_path)
|
||||
build_id = @db.last_id
|
||||
mock = MockManager.new(prepare_path, get_project_config(prj_id), counter_file, @db, build_path, repo_path, git_source, build_id, prep_script, spec_file, repo_lock)
|
||||
mock = MockManager.new(prepare_path, get_project_config(prj_id), counter_file, @db, build_path, repo_path, git_source, build_id, prep_script, spec_file, repo_lock, git_id)
|
||||
bld_id = build_id
|
||||
@db.update_build_task_error_log(build_id, mock.get_build_process_log)
|
||||
mock.build_task
|
||||
mock.finalize_build_task
|
||||
f.flock(File::LOCK_UN)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
$LOAD_PATH.unshift File.expand_path(".", "locallibs/ruby-rpm-ffi/lib")
|
||||
|
||||
require "rpm"
|
||||
|
||||
require_relative "runner"
|
||||
|
||||
class RepoManager
|
||||
@@ -22,4 +26,15 @@ class RepoManager
|
||||
@last_status = cmd.exit_status
|
||||
@last_pid = cmd.pid
|
||||
end
|
||||
|
||||
def get_rpm_info(path_to_rpm)
|
||||
res = { :error => nil }
|
||||
if File.exist?(path_to_rpm)
|
||||
pkg = RPM::Package.open(path_to_rpm)
|
||||
res[:pkginfo] = pkg
|
||||
else
|
||||
res[:error] = "#{path_to_rpm} не существует"
|
||||
end
|
||||
res
|
||||
end
|
||||
end
|
||||
|
||||
@@ -34,3 +34,15 @@ end
|
||||
def get_spec_files_in_dir(directory)
|
||||
Dir.glob(File.join(directory, "**", "*")).reject { |f| File.directory?(f) }.select { |f| File.extname(f) == ".spec" }.map { |f| f.delete_prefix(directory + "/") }
|
||||
end
|
||||
|
||||
def get_src_rpm_files_in_dir(directory)
|
||||
Dir.glob(File.join(directory, "**", "*")).reject { |f| File.directory?(f) }.select { |f| f.end_with?(".src.rpm") }.map { |f| f.delete_prefix(directory + "/") }
|
||||
end
|
||||
|
||||
def get_log_paths(directory)
|
||||
Dir.glob(File.join(directory, "**", "*")).reject { |f| File.directory?(f) }.select { |f| File.extname(f) == ".log" }.map { |f| f.delete_prefix(directory + "/") }
|
||||
end
|
||||
|
||||
def get_rpm_paths(directory)
|
||||
Dir.glob(File.join(directory, "**", "*")).reject { |f| File.directory?(f) }.select { |f| File.extname(f) == ".rpm" }.map { |f| f.delete_prefix(directory + "/") }
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user