Sign part 2

This commit is contained in:
alexey
2025-03-21 23:15:46 +03:00
parent c6a89e7be1
commit e6d3a90231
17 changed files with 485 additions and 12 deletions

View File

@@ -90,4 +90,12 @@ class IniConfig
"keys"
end
end
def get_repoview_path()
unless @config["repoview"]["path"].nil?
@config["repoview"]["path"].to_s
else
"repoview"
end
end
end

View File

@@ -235,7 +235,7 @@ class DBase
#result = 0 (in progress), 1 (stopped - error), 2 (stopped - success)
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)
id = BuildTask.insert(repo_id: git_id.to_i, proj_id: prj_id.to_i, logpath: "", errlogpath: "", result: 0)
@last_id = id
BuildTask.where(id: id).update(logpath: File.join(proj_path, "#{id}"), errlogpath: File.join(proj_path, "#{id}", "process.log"))
end
@@ -359,4 +359,16 @@ class DBase
def projects_with_current_as_link(prj_id)
ProjectsProjects.where(proj_id_repository: prj_id.to_i).all
end
def get_rpm_info_by_hash(hash)
Rpms.where(filehash: hash).first
end
def update_rpm_sign(rpm_id, sign_path)
Rpms.where(id: rpm_id.to_i).update(sign: 1, signpath: sign_path)
end
def set_project_address(prj_id, address)
ProjectsProjects.where(proj_id: prj_id.to_i).update(remote_address: address)
end
end

View File

@@ -3,6 +3,7 @@ require_relative "db"
require_relative "repomanage"
require_relative "mock"
require_relative "utilities"
require "digest"
PROJECTS_STRUCTURE = {
:REPO => "repo",
@@ -371,4 +372,86 @@ class ProjectsActions
end
@error
end
def sign_project(prj_id, key_path, password, url, tpl_dir)
@error = nil
proj_path = get_project_path(prj_id)
sign_repo_path = File.join(proj_path, PROJECTS_STRUCTURE[:SIGNED])
repo_path = File.join(proj_path, PROJECTS_STRUCTURE[:REPO])
repo_sign = RepoManager.new(sign_repo_path)
repo_key = RepoManagerKeys.new(key_path)
if password.nil?
password = repo_key.check_password_exists
end
if password.nil?
@error = "Не указан пароль для подписи"
else
repo_lock = File.join(proj_path, PROJECTS_STRUCTURE[:CONFIGS], ".repolock")
sign_lock = File.join(proj_path, PROJECTS_STRUCTURE[:CONFIGS], ".signlock")
prj = @db.proj(prj_id)
if repo_key.check_key_exists
File.open(sign_lock, File::RDWR | File::CREAT) do |s|
s.flock(File::LOCK_EX)
File.open(repo_lock, File::RDWR | File::CREAT) do |f|
f.flock(File::LOCK_EX)
rpm_list = get_rpms_list(repo_path)
if prj[:public] == 0
rpm_list = rpm_list.reject do |item|
block = false
block = true if item =~ /\.src\.rpm$/ || item =~ /SRPMS/ || item =~ /Debug/ || item =~ /(debuginfo.+rpm$)|(debugsource.+rpm$)/
block
end
end
rpm_signed_list = get_rpms_list(sign_repo_path)
rpm_list = rpm_list.select do |item|
sign_repo_path_rpm = File.join(sign_repo_path, item)
unless File.exist?(sign_repo_path_rpm)
file_path_full = File.join(repo_path, item)
unless File.exist?(File.dirname(sign_repo_path_rpm))
FileUtils.mkdir_p(File.dirname(sign_repo_path_rpm))
end
FileUtils.cp_r(file_path_full, File.dirname(sign_repo_path_rpm), verbose: false, remove_destination: false)
sha256 = Digest::SHA256.file(file_path_full)
rpm_info = @db.get_rpm_info_by_hash(sha256.hexdigest)
unless rpm_info.nil?
@db.update_rpm_sign(rpm_info[:id], sign_repo_path_rpm)
end
repo_key.sign_package(sign_repo_path_rpm, password)
end
end
repo_url = "http://localhost/"
if prj[:remote_address].nil? || prj[:remote_address].strip == ""
repo_url = url
else
repo_url = prj[:remote_address]
end
if repo_url[-1] != "/"
repo_url = repo_url + "/"
end
repo_sign.repoview(repo_url, prj[:projname], tpl_dir)
repo_sign.create_repo
end
end
else
@error = "Ключ для подписи отсутствует"
end
end
@error
end
def set_address(prj_id, address)
@error = nil
if address.nil?
address = ""
else
address = address.strip
end
@db.set_project_address(prj_id, address)
@error
end
def get_sign_path(id)
path = get_project_path(id)
File.join(path, PROJECTS_STRUCTURE[:SIGNED])
end
end

View File

@@ -3,6 +3,9 @@ $LOAD_PATH.unshift File.expand_path(".", "locallibs/ruby-rpm-ffi/lib")
require "rpm"
require_relative "runner"
require "ptools"
require "fileutils"
require "erb"
class RPMReader
def get_rpm_info(path_to_rpm)
@@ -29,9 +32,29 @@ class RepoManagerKeys
key_file = File.join(@path, "public", "mockgui-gpg-key")
File.exist?(key_file)
end
end
#rpm --define "_gpg_sign_cmd_extra_args --pinentry-mode loopback --passphrase 1234" --addsign bayrepo-neuro-farm-0.1-2.x86_64.rpm
def check_password_exists()
passwd = nil
passwd_file = File.join(@path, "save")
if File.exist?(passwd_file)
unless File.binary?(passwd_file)
passwd = File.readlines(passwd_file).first.strip
end
end
passwd
end
def get_publick_key()
File.join(@path, "public", "mockgui-gpg-key")
end
def sign_package(rpm_path, password)
cmd_args = %Q(/usr/bin/rpm --define "_gpg_sign_cmd_extra_args --pinentry-mode loopback --passphrase #{password}" --addsign "#{rpm_path}" 2>/dev/null)
cmd = Runner.new(cmd_args)
cmd.run
cmd.exit_status
end
end
class RepoManager
attr :path, :error, :last_status, :last_pid
@@ -60,4 +83,53 @@ class RepoManager
def get_rpm_info(path_to_rpm)
@reader.get_rpm_info(path_to_rpm)
end
def repoview(url, repo_name, template_dir)
rpm_list = get_rpms_list(@path)
result = {}
rpm_list.each do |item|
full_rpm_path = File.join(@path, item)
info = @reader.get_rpm_info(full_rpm_path)
dirName = File.dirname(item)
fileName = File.basename(item)
if result[dirName].nil?
result[dirName] = []
end
pkg_info = {}
pkg_info[:fname] = fileName
pkg_info[:stat] = File.stat(full_rpm_path).ctime
if info[:error].nil?
pkg_info[:chlog] = info[:pkginfo].changelog.first(5)
else
pkg_info[:chlog] = []
end
result[dirName] << pkg_info
end
repo_name = repo_name
repo_url = url
pkg_num = rpm_list.length
repo_data = []
data_keys = []
result.each_pair do |key, value|
result[key.to_s].sort_by! { |item| item[:fname] }
data_keys << key.to_s
end
data_keys.sort!
data_keys.each do |item|
repo_data << result[item]
end
tpl_file = File.join(template_dir, "template.erb")
template = File.read(tpl_file)
renderer = ERB.new(template)
result_html = renderer.result(binding)
boots_trap_css = File.join(template_dir, "bootstrap.min.css")
boots_trap_js = File.join(template_dir, "bootstrap.bundle.min.js")
index_html = File.join(@path, "index.html")
File.open(index_html, "w") do |f|
f.write(result_html)
end
FileUtils.cp_r(boots_trap_css, @path, verbose: false, remove_destination: true)
FileUtils.cp_r(boots_trap_js, @path, verbose: false, remove_destination: true)
end
end

View File

@@ -50,3 +50,7 @@ end
def get_log_paths_success(directory)
Dir.glob(File.join(directory, "**", "*")).reject { |f| File.directory?(f) }.select { |f| File.extname(f) == ".log" }.reject { |f| File.basename(f) == "process.log" }
end
def get_rpms_list(directory)
Dir.glob(File.join(directory, "**", "*.rpm")).reject { |f| File.directory?(f) || f =~ /repodata\// }.map { |f| f.delete_prefix(directory + "/") }
end