Class: Cangallo::Repo
- Inherits:
-
Object
- Object
- Cangallo::Repo
- Defined in:
- lib/cangallo/repo.rb
Constant Summary collapse
- VERSION =
0
Instance Attribute Summary collapse
-
#images ⇒ Object
readonly
Returns the value of attribute images.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
- #add(name, data) ⇒ Object
- #add_image(file, data = {}) ⇒ Object
- #add_tag(tag, image) ⇒ Object
- #ancestors(name) ⇒ Object
- #del_image(image) ⇒ Object
- #del_tag(tag) ⇒ Object
- #fetch ⇒ Object
- #find(name, search_tags = true) ⇒ Object
- #get(name) ⇒ Object
- #image_path(name) ⇒ Object
- #index_data(images = {}, tags = {}, version = VERSION) ⇒ Object
- #index_path ⇒ Object
-
#initialize(conf) ⇒ Repo
constructor
A new instance of Repo.
- #metadata_path(name) ⇒ Object
- #pull(name) ⇒ Object
- #read_index(index = nil) ⇒ Object
- #remote_image_url(name) ⇒ Object
- #remote_url(name) ⇒ Object
- #short_name(sha256) ⇒ Object
- #sign ⇒ Object
- #url ⇒ Object
- #verify ⇒ Object
- #write_index ⇒ Object
Constructor Details
#initialize(conf) ⇒ Repo
Returns a new instance of Repo.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cangallo/repo.rb', line 29 def initialize(conf) @conf = conf @path = File.(@conf["path"]) @name = @conf["name"] @type = @conf["type"] @url = @conf["url"] if !@type && @url @type = "remote" else @type = "local" end read_index end |
Instance Attribute Details
#images ⇒ Object (readonly)
Returns the value of attribute images.
25 26 27 |
# File 'lib/cangallo/repo.rb', line 25 def images @images end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
25 26 27 |
# File 'lib/cangallo/repo.rb', line 25 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
25 26 27 |
# File 'lib/cangallo/repo.rb', line 25 def path @path end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
25 26 27 |
# File 'lib/cangallo/repo.rb', line 25 def @tags end |
Instance Method Details
#add(name, data) ⇒ Object
97 98 99 100 101 |
# File 'lib/cangallo/repo.rb', line 97 def add(name, data) data["creation-time"] = Time.now data["sha256"] = name @images[name] = data end |
#add_image(file, data = {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/cangallo/repo.rb', line 103 def add_image(file, data = {}) parent_sha256 = nil parent = nil parent_path = nil only_copy = data.delete("only_copy") if data["parent"] parent_sha256 = data["parent"] parent = self.images[parent_sha256] if !parent raise "Parent not found" end parent_path = File.(self.image_path(parent_sha256)) end puts "Calculating image sha256 with libguestfs (it will take some time)" qcow2 = Cangallo::Qcow2.new(file) sha256 = qcow2.sha256 sha256.strip! if sha256 puts "Image SHA256: #{sha256}" puts "Copying file to repository" image_path = self.image_path(sha256) qcow2.copy(image_path, parent: parent_path, only_copy: only_copy) qcow2 = Cangallo::Qcow2.new(image_path) info = qcow2.info info_data = info.select do |k,v| %w{virtual-size format actual-size format-specific}.include?(k) end data.merge!(info_data) data["file-sha256"] = Digest::SHA256.file(file).hexdigest if parent qcow2.rebase("#{parent_sha256}.qcow2") data["parent"] = parent_sha256 end self.add(sha256, data) self.write_index sha256 end |
#add_tag(tag, image) ⇒ Object
175 176 177 178 179 |
# File 'lib/cangallo/repo.rb', line 175 def add_tag(tag, image) img = find(image) @tags[tag] = img write_index end |
#ancestors(name) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/cangallo/repo.rb', line 217 def ancestors(name) ancestors = [] image = get(name) ancestors << image["sha256"] while image["parent"] image = get(image["parent"]) ancestors << image["sha256"] end ancestors end |
#del_image(image) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/cangallo/repo.rb', line 154 def del_image(image) sha256 = find(image) raise %Q{Image "#{image}" does not exist} if !sha256 path = image_path(sha256) begin File.delete(path) rescue Errno::ENOENT STDERR.puts "Image file not found" end @images.delete(sha256) @tags.select {|key, value| value == sha256 }.keys.each do |tag| @tags.delete(tag) end write_index end |
#del_tag(tag) ⇒ Object
181 182 183 184 |
# File 'lib/cangallo/repo.rb', line 181 def del_tag(tag) @tags.delete(tag) write_index end |
#fetch ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/cangallo/repo.rb', line 235 def fetch return nil if @conf["type"] != "remote" uri = remote_url("index.yaml") open(uri, "r") do |f| data = f.read read_index(data) end write_index end |
#find(name, search_tags = true) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/cangallo/repo.rb', line 186 def find(name, = true) length = name.length found = @images.select do |sha256, data| sha256[0, length] == name end if found && found.length > 0 return found.first.first end if found = @tags.select do |tag, sha256| tag == name end end if found && found.length > 0 return found.first[1] end nil end |
#get(name) ⇒ Object
209 210 211 212 213 214 215 |
# File 'lib/cangallo/repo.rb', line 209 def get(name) image = find(name) return nil if !image @images[image] end |
#image_path(name) ⇒ Object
85 86 87 |
# File 'lib/cangallo/repo.rb', line 85 def image_path(name) File.join(@path, "#{name}.qcow2") end |
#index_data(images = {}, tags = {}, version = VERSION) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/cangallo/repo.rb', line 45 def index_data(images = {}, = {}, version = VERSION) { "version" => version, "images" => images, "tags" => } end |
#index_path ⇒ Object
53 54 55 |
# File 'lib/cangallo/repo.rb', line 53 def index_path ("index") end |
#metadata_path(name) ⇒ Object
81 82 83 |
# File 'lib/cangallo/repo.rb', line 81 def (name) File.join(@path, "#{name}.yaml") end |
#pull(name) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/cangallo/repo.rb', line 256 def pull(name) image = get(name) raise "Image not found" if !image sha256 = image["sha256"] image_url = remote_image_url(sha256) image_path = image_path(sha256) cmd = "curl -o '#{image_path}' '#{image_url}'" STDERR.puts(cmd) system(cmd) end |
#read_index(index = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cangallo/repo.rb', line 57 def read_index(index = nil) if !index if File.exist?(index_path) data = YAML.load(File.read(index_path)) else data = index_data() end else data = YAML.load(index) end @images = data["images"] @tags = data["tags"] @reverse_tags = @tags.invert end |
#remote_image_url(name) ⇒ Object
93 94 95 |
# File 'lib/cangallo/repo.rb', line 93 def remote_image_url(name) remote_url("#{name}.qcow2") end |
#remote_url(name) ⇒ Object
89 90 91 |
# File 'lib/cangallo/repo.rb', line 89 def remote_url(name) URI.join(@url, name) end |
#short_name(sha256) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/cangallo/repo.rb', line 271 def short_name(sha256) tag = @reverse_tags[sha256] if tag name = "#{tag}" else name = "#{sha256[0..15]}" end name end |
#sign ⇒ Object
248 249 250 |
# File 'lib/cangallo/repo.rb', line 248 def sign Keybase.sign(index_path) end |
#url ⇒ Object
231 232 233 |
# File 'lib/cangallo/repo.rb', line 231 def url @conf["url"] end |
#verify ⇒ Object
252 253 254 |
# File 'lib/cangallo/repo.rb', line 252 def verify Keybase.verify(index_path) end |
#write_index ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/cangallo/repo.rb', line 73 def write_index data = index_data(@images, @tags) open(("index"), "w") do |f| f.write(data.to_yaml) end end |