Signed-off-by: Li Ping <1477412247(a)qq.com>
---
lib/rpm-repo.rb | 38 +++++++++++++++++++++++++-------------
1 file changed, 25 insertions(+), 13 deletions(-)
diff --git a/lib/rpm-repo.rb b/lib/rpm-repo.rb
index 658a5270..f2344dcc 100755
--- a/lib/rpm-repo.rb
+++ b/lib/rpm-repo.rb
@@ -31,29 +31,41 @@ MQ_PORT = ENV['MQ_PORT'] || 5672
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)
- 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)
+ begin
+ rpm_info = JSON.parse(msg)
+ check_upload_rpms(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})")
+ dest = File.join(rpm_path.to_s, File.basename(rpm))
+ FileUtils.mv(rpm, dest)
+ system("createrepo --update $(dirname #{rpm_path})")
+ end
+ @mq.ack(info)
+ rescue StandardError => e
+ puts "error message: ", e.message
+ @mq.ack(info)
end
- update_pub_dir
- @mq.ack(info)
end
end
- def update_pub_dir
- @update.each do |rpm|
+ def check_upload_rpms(data)
+ raise JSON.dump({ 'errcode' => '200', 'errmsg' => 'no upload_rpms params' }) unless data.key?('upload_rpms')
+ raise JSON.dump({ 'errcode' => '200', 'errmsg' => "upload_rpms params type error" }) if data["upload_rpms"].class != Array
+ data["upload_rpms"].each do |rpm|
+ raise JSON.dump({ "errcode" => "200", "errmsg" => "#{rpm} not exist" }) unless File.exist?(rpm)
+ raise JSON.dump({ "errcode" => "200", "errmsg" => "the upload directory is incorrect" }) unless File.dirname(rpm).start_with?("/srv/rpm/upload/")
+ end
+ end
+
+ def update_pub_dir(update)
+ update.each do |rpm|
pub_path = File.dirname(rpm).sub("testing", "pub")
FileUtils.mkdir_p(pub_path) unless File.directory?(pub_path)
--
2.23.0