Class: BerkshelfStore::Backends::Filesystem
- Inherits:
-
Object
- Object
- BerkshelfStore::Backends::Filesystem
- Defined in:
- lib/berkshelf-store/backends/filesystem.rb
Instance Method Summary collapse
- #check_filename(filename) ⇒ Object
- #cookbook_data(cookbook) ⇒ Object
- #get_catalog(prefix) ⇒ Object
- #get_json(name, version) ⇒ Object
- #get_tarball(name, version) ⇒ Object
-
#initialize(path, tmp) ⇒ Filesystem
constructor
A new instance of Filesystem.
- #last_error ⇒ Object
- #store(content) ⇒ Object
- #write_tmp_file(name, content) ⇒ Object
Constructor Details
#initialize(path, tmp) ⇒ Filesystem
Returns a new instance of Filesystem.
11 12 13 14 |
# File 'lib/berkshelf-store/backends/filesystem.rb', line 11 def initialize(path, tmp) @path = path @tmp = tmp end |
Instance Method Details
#check_filename(filename) ⇒ Object
16 17 18 19 |
# File 'lib/berkshelf-store/backends/filesystem.rb', line 16 def check_filename(filename) #do minimal checks true end |
#cookbook_data(cookbook) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/berkshelf-store/backends/filesystem.rb', line 83 def cookbook_data(cookbook) platforms = cookbook..platforms || Hash.new dependencies = cookbook..dependencies || Hash.new name = cookbook.cookbook_name.to_s version = cookbook.version.to_s path = "/#{name}/#{version}/#{name}-#{version}.tgz" retval = { "name" => name, "version" => version, "data" => { "endpoint_priority" => 0, "platforms" => platforms, "dependencies" => dependencies, "location_type" => 'uri', "location_path" => path } } return retval end |
#get_catalog(prefix) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/berkshelf-store/backends/filesystem.rb', line 130 def get_catalog(prefix) retval = {} Dir["#{@path}/*/*/data.json"].each do |json_file| data = JSON.parse(File.read("#{json_file}")) data['data']['location_path'] = "#{prefix}#{data['data']['location_path']}" retval[data['name']] ||= Hash.new retval[data['name']][data['version']] = data['data'] end retval end |
#get_json(name, version) ⇒ Object
122 123 124 |
# File 'lib/berkshelf-store/backends/filesystem.rb', line 122 def get_json(name, version) File.open("#{@path}/#{name}/#{version}/data.json", "r").read end |
#get_tarball(name, version) ⇒ Object
126 127 128 |
# File 'lib/berkshelf-store/backends/filesystem.rb', line 126 def get_tarball(name, version) File.open("#{@path}/#{name}/#{version}/#{name}-#{version}.tgz").read end |
#last_error ⇒ Object
118 119 120 |
# File 'lib/berkshelf-store/backends/filesystem.rb', line 118 def last_error @error end |
#store(content) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/berkshelf-store/backends/filesystem.rb', line 21 def store(content) data = {} digest = Digest::SHA256.hexdigest(content) tmp_dir = "#{@tmp}/#{digest}" FileUtils.mkdir_p "#{tmp_dir}/cookbook" #extract cookbook if write_tmp_file("#{tmp_dir}/cookbook.tgz", content) cmd = "tar --gzip --extract --file=\"#{tmp_dir}/cookbook.tgz\" -C \"#{tmp_dir}/cookbook\"" %x{#{cmd}} if $?.exitstatus != 0 return {"fail" => "Failed to extract metadata.rb from tarball"} end else return {"fail" => "Unable to write cookbook #{tmp_dir}/cookbook.tgz"} end #locate check cookbook search_root = Dir["#{tmp_dir}/cookbook/*/metadata.rb"] if search_root.size != 1 return {"fail" => "tgz needs to contain ./a_dir/metadata.rb"} end cookbook_dir = File.dirname(search_root[0]) #Extract information begin cookbook = Ridley::Chef::Cookbook.from_path(cookbook_dir) data = cookbook_data(cookbook) name = data['name'] version = data['version'] rescue Exception => e return {"fail" => "Information extraction failed #{e}"} end begin data_dir = "#{@path}/#{name}/#{version}" FileUtils.mkdir_p "#{data_dir}" rescue SystemCallError => e return {"fail" => "#{data_dir} creation failed #{e}"} end begin json_file = File.open("#{data_dir}/data.json", "w") json_file.write(data.to_json) json_file.close rescue Exception => e return {"fail" => "#{data_dir}/data.json creation failed #{e}"} end begin File.rename("#{tmp_dir}/cookbook.tgz", "#{data_dir}/#{name}-#{version}.tgz") rescue Exception => e return {"fail" => "tarball store failed #{e}"} end FileUtils.rm_rf("#{tmp_dir}") return data end |
#write_tmp_file(name, content) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/berkshelf-store/backends/filesystem.rb', line 106 def write_tmp_file(name, content) begin tarfile = File.open("#{name}", "w") tarfile.write(content) rescue IOError => e @error="Failed to write temp file #{name}" return false ensure tarfile.close unless tarfile == nil end end |