You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

require_relative "spork"
require_relative "runner"
require "fileutils"
require "logger"
BUILD_STRUCTURE = {
:SRC => "src",
:RPMS => "rpms",
:RESULT => "result",
: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
def initialize(path, config, cfg_counter_path, db, result_path, repo_path, git_path, build_id, recips, spec_file, repo_lock)
@error = nil
unless File.exist? (path)
Dir.mkdir(path)
end
@path = path
@config = config
cntr = 0
@db = db
@resultpath = result_path
@repo_path = repo_path
@git_path = git_path
@build_id = build_id
@recips = recips
@spec = spec_file
@repo_lock = repo_lock
File.open(cfg_counter_path, "r+") do |f|
f.flock(File::LOCK_EX)
counter = f.gets.strip
i_counter = counter.to_i
i_counter = i_counter + 1
f.puts("#{i_counter}")
cnt = i_counter
end
tmp_name = (0...10).map { ("a".."z").to_a[rand(26)] }.join
@prep_dir = File.join(path, "#{cntr}_#{tmp_name}")
@process_log = File.join(@prep_dir, "process.log")
Dir.mkdir(@prep_dir)
FileUtils.touch(@process_log)
@log = nil
end
def get_build_process_log()
@process_log
end
def finalize_build_task()
@log.close
end
def clean_build
@log.info("Удаление временной сборочной среды #{@path}")
FileUtils.rm_rf(@path)
end
def prepare_structure()
@log.info("Подготовка структуры каталогов")
BUILD_STRUCTURE.each_pair do |key, value|
new_path = File.join(@prep_dir, value)
@log.info("Создан каталог #{new_path}")
Dir.mkdir(new_path)
end
end
def prepare_src()
@log.info("Подготовка исходных файлов проекта к формированию SRPMS")
if File.exist?(@git_path)
if File.directory?(@git_path)
FileUtils.cp_r(@git_path, File.join(@prep_dir, BUILD_STRUCTURE[:SRC]), verbose: true, remove_destination: true)
FileUtils.rm_rf(File.join(@prep_dir, BUILD_STRUCTURE[:SRC], ".git"), secure: true)
else
@log.error("Это файл #{@git_path}, а не каталог")
@error = true
end
else
@log.error("Каталог #{@git_path} не существует")
@error = true
end
end
def prepare_source()
@log.info("Запукс подготовительных скриптов")
@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|
f.write(item[:content])
end
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/bash -x "#{script}")
cmd = Runner.new(cmd_args, @log)
cmd.run_clean
@error = true if cmd.exit_status != 0
end
break if @error
end
end
def prepare_src_rpm()
@log.info("Подготовка SRCRPM")
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
end
else
@error = true
@log.error("Не могу найти sepc файл #{spec_file}")
end
end
def build_rpm()
end
def save_logs()
end
def save_rpms()
File.open(@repo_lock, File::RDWR | File::CREAT) do |f|
f.flock(File::LOCK_EX)
# выклдака пакетов и пересоздание repodata
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
end
@db.after_fork
spock
end
end