#907: update "FileStoreClean" module, some refactoring
This commit is contained in:
parent
b7babdef76
commit
7bcbe39e61
|
@ -16,11 +16,12 @@ class Projects::Git::TreesController < Projects::Git::BaseController
|
|||
@commit = @project.repo.commits(@treeish, 1).first
|
||||
end
|
||||
raise Grit::NoSuchPathError unless @commit
|
||||
tag = @project.repo.tags.find{ |t| t.name == @treeish }
|
||||
if tag
|
||||
redirect_to "#{APP_CONFIG['file_store_url']}/api/v1/file_stores/#{@project.get_project_tag_sha1(tag, format)}"
|
||||
tag = @project.repo.tags.find{ |t| t.name == @treeish }
|
||||
sha1 = @project.get_project_tag_sha1(tag, format) if tag
|
||||
if sha1
|
||||
redirect_to "#{APP_CONFIG['file_store_url']}/api/v1/file_stores/#{sha1}"
|
||||
else
|
||||
archive = @project.create_archive @treeish, format
|
||||
archive = @project.archive_by_treeish_and_format @treeish, format
|
||||
send_file archive[:path], :disposition => 'attachment', :type => "application/#{format == 'zip' ? 'zip' : 'x-tar-gz'}", :filename => archive[:fullname]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -370,6 +370,10 @@ class BuildList < ActiveRecord::Base
|
|||
.recent
|
||||
end
|
||||
|
||||
def sha1_of_file_store_files
|
||||
packages.pluck(:sha1).compact | (results || []).map{ |r| r['sha1'] }.compact
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def create_container
|
||||
|
|
|
@ -130,6 +130,10 @@ class ProductBuildList < ActiveRecord::Base
|
|||
[BUILD_COMPLETED, BUILD_FAILED, BUILD_CANCELED].include? status
|
||||
end
|
||||
|
||||
def sha1_of_file_store_files
|
||||
(results || []).map{ |r| r['sha1'] }.compact
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def abf_worker_priority
|
||||
|
|
|
@ -191,32 +191,25 @@ class Project < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def create_archive(treeish, format)
|
||||
file_name = "#{name}-#{treeish}"
|
||||
fullname = "#{file_name}.#{tag_file_format(format)}"
|
||||
file = Tempfile.new fullname, 'tmp'
|
||||
system("cd #{path}; git archive --format=#{format == 'zip' ? 'zip' : 'tar'} --prefix=#{file_name}/ #{treeish} #{format == 'zip' ? '' : ' | gzip -9'} > #{file.path}")
|
||||
file.close
|
||||
{
|
||||
:path => file.path,
|
||||
:fullname => fullname
|
||||
}
|
||||
end
|
||||
|
||||
def get_project_tag_sha1(tag, format)
|
||||
format_id = ProjectTag::FORMATS["#{tag_file_format(format)}"]
|
||||
project_tag = project_tags.where(:tag_name => tag.name, :format_id => format_id).first
|
||||
|
||||
return project_tag.sha1 if project_tag && project_tag.commit_id == tag.commit.id
|
||||
return project_tag.sha1 if project_tag && project_tag.commit_id == tag.commit.id && Modules::Models::FileStoreClean.file_exist_on_file_store?(project_tag.sha1)
|
||||
|
||||
archive = create_archive(tag.name, format)
|
||||
archive = archive_by_treeish_and_format tag.name, format
|
||||
sha1 = Digest::SHA1.file(archive[:path]).hexdigest
|
||||
token = User.find_by_uname('rosa_system').authentication_token
|
||||
if %x[ curl #{APP_CONFIG['file_store_url']}/api/v1/file_stores.json?hash=#{sha1} ] == '[]'
|
||||
system "curl --user #{token}: -POST -F 'file_store[file]=@#{archive[:path]};filename=#{name}-#{tag.name}.#{tag_file_format(format)}' #{APP_CONFIG['file_store_url']}/api/v1/upload"
|
||||
unless Modules::Models::FileStoreClean.file_exist_on_file_store? sha1
|
||||
token = User.find_by_uname('rosa_system').authentication_token
|
||||
begin
|
||||
resp = JSON `curl --user #{token}: -POST -F 'file_store[file]=@#{archive[:path]};filename=#{name}-#{tag.name}.#{tag_file_format(format)}' #{APP_CONFIG['file_store_url']}/api/v1/upload`
|
||||
rescue # Dont care about it
|
||||
resp = {}
|
||||
end
|
||||
return nil if resp['sha1_hash'].nil?
|
||||
end
|
||||
if project_tag
|
||||
project_tag.remove_archive_from_file_store(project_tag.sha1)
|
||||
project_tag.destroy_file_from_file_store(project_tag.sha1)
|
||||
project_tag.update_attributes(:sha1 => sha1)
|
||||
else
|
||||
project_tags.create(
|
||||
|
@ -229,8 +222,24 @@ class Project < ActiveRecord::Base
|
|||
return sha1
|
||||
end
|
||||
|
||||
def archive_by_treeish_and_format(treeish, format)
|
||||
@archive ||= create_archive treeish, format
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def create_archive(treeish, format)
|
||||
file_name = "#{name}-#{treeish}"
|
||||
fullname = "#{file_name}.#{tag_file_format(format)}"
|
||||
file = Tempfile.new fullname, 'tmp'
|
||||
system("cd #{path}; git archive --format=#{format == 'zip' ? 'zip' : 'tar'} --prefix=#{file_name}/ #{treeish} #{format == 'zip' ? '' : ' | gzip -9'} > #{file.path}")
|
||||
file.close
|
||||
{
|
||||
:path => file.path,
|
||||
:fullname => fullname
|
||||
}
|
||||
end
|
||||
|
||||
def tag_file_format(format)
|
||||
format == 'zip' ? 'zip' : 'tar.gz'
|
||||
end
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
# -*- encoding : utf-8 -*-
|
||||
class ProjectTag < ActiveRecord::Base
|
||||
include Modules::Models::FileStoreClean
|
||||
|
||||
FORMATS = {
|
||||
'zip' => 0,
|
||||
'tar.gz' => 1
|
||||
|
@ -16,12 +18,8 @@ class ProjectTag < ActiveRecord::Base
|
|||
:tag_name,
|
||||
:format_id
|
||||
|
||||
after_destroy :remove_archive_from_file_store
|
||||
|
||||
def remove_archive_from_file_store(sha = sha1)
|
||||
token = User.find_by_uname('file_store').authentication_token
|
||||
system "curl --user #{token}: -X DELETE #{APP_CONFIG['file_store_url']}/api/v1/file_stores/#{sha}.json"
|
||||
def sha1_of_file_store_files
|
||||
[sha1]
|
||||
end
|
||||
later :remove_archive_from_file_store, :queue => :clone_build
|
||||
|
||||
end
|
||||
|
|
|
@ -11,28 +11,43 @@ module Modules
|
|||
end
|
||||
later :destroy, :queue => :clone_build
|
||||
|
||||
def sha1_of_file_store_files
|
||||
raise NotImplementedError, "You should implement this method"
|
||||
end
|
||||
|
||||
def destroy_files_from_file_store
|
||||
files = []
|
||||
self.results.each {|r| files << r['sha1'] if r['sha1'].present?}
|
||||
if self.respond_to? :packages
|
||||
self.packages.each {|pk| files << pk.sha1 if pk.sha1.present?}
|
||||
sha1_of_file_store_files.each do |sha1|
|
||||
destroy_file_from_file_store sha1
|
||||
end
|
||||
if files.count > 0
|
||||
token = User.system.find_by_uname('file_store').authentication_token
|
||||
uri = URI APP_CONFIG['file_store_url']
|
||||
Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
files.each do |sha1|
|
||||
begin
|
||||
req = Net::HTTP::Delete.new("/api/v1/file_stores/#{sha1}.json")
|
||||
req.basic_auth token, ''
|
||||
http.request(req)
|
||||
rescue # Dont care about it
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def destroy_file_from_file_store(sha1)
|
||||
token = User.system.find_by_uname('file_store').authentication_token
|
||||
uri = URI APP_CONFIG['file_store_url']
|
||||
Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
begin
|
||||
req = Net::HTTP::Delete.new("/api/v1/file_stores/#{sha1}.json")
|
||||
req.basic_auth token, ''
|
||||
http.request(req)
|
||||
rescue # Dont care about it
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.file_exist_on_file_store?(sha1)
|
||||
begin
|
||||
resp = JSON(RestClient.get "#{APP_CONFIG['file_store_url']}/api/v1/file_stores.json", :params => {:hash => sha1})
|
||||
rescue # Dont care about it
|
||||
resp = []
|
||||
end
|
||||
if resp[0].respond_to?('[]') && resp[0]['file_name'] && resp[0]['sha1_hash']
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue