This is tricky logic, should describe the full context/workflow/design as comments in the beginning of class definition.
Thanks, Fengguang
On Mon, Mar 29, 2021 at 10:11:34PM +0800, Li Ping wrote:
Signed-off-by: Li Ping 1477412247@qq.com
container/rpm-repo/rpm-repo.rb | 73 ++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 container/rpm-repo/rpm-repo.rb
diff --git a/container/rpm-repo/rpm-repo.rb b/container/rpm-repo/rpm-repo.rb new file mode 100755 index 0000000..18f0507 --- /dev/null +++ b/container/rpm-repo/rpm-repo.rb @@ -0,0 +1,73 @@ +#!/usr/bin/ruby +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved. +# frozen_string_literal: true
+require "bunny" +require "fileutils" +require "json"
+MQ_HOST = ENV['MQ_HOST'] || ENV['LKP_SERVER'] || '172.17.0.1' +MQ_PORT = ENV['MQ_PORT'] || 5672
+class MQClient
- def initialize(hostname = "172.17.0.1", port = "5672")
- @conn = Bunny.new(hostname: hostname, port: port)
- @conn.start
- @channel = @conn.create_channel
- end
- def queue(queue_name, opts = {})
- @channel.queue(queue_name, opts)
- end
- def ack(delivery_info)
- @channel.ack(delivery_info.delivery_tag)
- end
+end
+class HandleRepo
- def initialize
- @mq = MQClient.new(MQ_HOST, MQ_PORT)
- @update = []
- end
- def handle_new_rpm
- queue = @mq.queue("update_repo")
- queue.subscribe({:block => true, :manual_ack => true}) do |info, _pro, msg|
rpm_info = JSON.parse(msg)
puts rpm_info
rpm_info["upload_rpms"].each do |rpm|
rpm_path = File.dirname(rpm).sub("upload", "testing")
FileUtils.mkdir_p(rpm_path) unless File.directory?(rpm_path)
dest = File.join(rpm_path.to_s, File.basename(rpm))
@update << dest
FileUtils.mv(rpm, dest)
system("createrepo --update $(dirname #{rpm_path})")
end
update_pub_dir
@mq.ack(info)
- end
- end
- def update_pub_dir
- @update.each do |rpm|
pub_path = File.dirname(rpm).sub("testing", "pub")
FileUtils.mkdir_p(pub_path) unless File.directory?(pub_path)
dest = File.join(pub_path, File.basename(rpm))
FileUtils.cp(rpm, dest)
repodata_dest = File.join(File.dirname(pub_path), "repodata")
repodata_src = File.dirname(rpm).sub("Packages", "repodata")
FileUtils.rm_r(repodata_dest) if Dir.exist?(repodata_dest)
FileUtils.cp_r(repodata_src, File.dirname(repodata_dest))
- end
- end
+end
+hr = HandleRepo.new
+hr.handle_new_rpm
2.23.0