Module: Hyperkit::Client::Images
- Included in:
- Hyperkit::Client
- Defined in:
- lib/hyperkit/client/images.rb
Overview
Methods for the images API
Retrieval collapse
-
#image(fingerprint, options = {}) ⇒ Sawyer::Resource
Get information on an image by its fingerprint.
-
#image_by_alias(alias_name, options = {}) ⇒ Sawyer::Resource
Get information on an image by one of its aliases.
-
#images ⇒ Array<String>
List of images on the server (public or private).
Creation and deletion collapse
-
#create_image_from_container(name, options = {}) ⇒ Sawyer::Resource
Create an image from an existing container.
-
#create_image_from_file(file, options = {}) ⇒ Sawyer::Resource
Upload an image from a local file.
-
#create_image_from_remote(server, options = {}) ⇒ Sawyer::Resource
Import an image from a remote server.
-
#create_image_from_snapshot(container, snapshot, options = {}) ⇒ Sawyer::Resource
Create an image from an existing snapshot.
-
#create_image_from_url(url, options = {}) ⇒ Sawyer::Resource
Import an image from a remote URL.
-
#delete_image(fingerprint, options = {}) ⇒ Sawyer::Resource
Delete an image.
Editing collapse
-
#create_image_secret(fingerprint) ⇒ Sawyer::Response
Generate a secret for an image that can be used by an untrusted client to retrieve information on and/or export a private image.
-
#update_image(fingerprint, options = {}) ⇒ Sawyer::Resource
Update the attributes of an image.
Export collapse
-
#export_image(fingerprint, output_dir, options = {}) ⇒ String
Export an image to a local file.
Aliases collapse
-
#create_image_alias(fingerprint, alias_name, options = {}) ⇒ Sawyer::Resource
Assign an alias for an image.
-
#delete_image_alias(alias_name) ⇒ Sawyer::Resource
Delete an alias for an image.
-
#image_alias(alias_name) ⇒ Sawyer::Resource
Get information on an image alias.
-
#image_aliases ⇒ Array<String>
List of image aliases on the server (public or private).
-
#rename_image_alias(old_alias, new_alias) ⇒ Sawyer::Resource
Rename an image alias.
-
#update_image_alias(alias_name, options = {}) ⇒ Sawyer::Resource
Update an image alias.
Instance Method Details
#create_image_alias(fingerprint, alias_name, options = {}) ⇒ Sawyer::Resource
Assign an alias for an image
582 583 584 585 586 587 588 589 |
# File 'lib/hyperkit/client/images.rb', line 582 def create_image_alias(fingerprint, alias_name, ={}) opts = .slice(:description).merge({ target: fingerprint, name: alias_name }) post(image_aliases_path, opts). end |
#create_image_from_container(name, options = {}) ⇒ Sawyer::Resource
Create an image from an existing container.
317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/hyperkit/client/images.rb', line 317 def create_image_from_container(name, ={}) opts = .slice(:filename, :public, :description) opts[:properties] = stringify_hash([:properties]) if [:properties] opts[:source] = { type: "container", name: name } response = post(images_path, opts). handle_async(response, [:sync]) end |
#create_image_from_file(file, options = {}) ⇒ Sawyer::Resource
Upload an image from a local file
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/hyperkit/client/images.rb', line 133 def create_image_from_file(file, ={}) headers = { "Content-Type" => "application/octet-stream" } headers["X-LXD-fingerprint"] = [:fingerprint] if [:fingerprint] headers["X-LXD-filename"] = [:filename] || File.basename(file) headers["X-LXD-public"] = 1.to_s if [:public] if [:properties] properties = [:properties].map do |k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}" end headers["X-LXD-properties"] = properties.join("&") end response = post(images_path, { raw_body: File.read(file), headers: headers }). handle_async(response, [:sync]) end |
#create_image_from_remote(server, options = {}) ⇒ Sawyer::Resource
Import an image from a remote server
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/hyperkit/client/images.rb', line 193 def create_image_from_remote(server, ={}) opts = .slice(:filename, :public, :auto_update) if [:protocol] && ! %w[lxd simplestreams].include?([:protocol]) raise Hyperkit::InvalidProtocol.new("Invalid protocol. Valid choices: lxd, simplestreams") end opts[:source] = .slice(:secret, :protocol, :certificate) opts[:source].merge!({ type: "image", mode: "pull", server: server }) if [:alias].nil? && [:fingerprint].nil? raise Hyperkit::ImageIdentifierRequired.new("Please specify either :alias or :fingerprint") end opts[:properties] = stringify_hash([:properties]) if [:properties] if [:alias] opts[:source][:alias] = [:alias] else opts[:source][:fingerprint] = [:fingerprint] end response = post(images_path, opts). handle_async(response, [:sync]) end |
#create_image_from_snapshot(container, snapshot, options = {}) ⇒ Sawyer::Resource
Create an image from an existing snapshot.
358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/hyperkit/client/images.rb', line 358 def create_image_from_snapshot(container, snapshot, ={}) opts = .slice(:filename, :public, :description) opts[:properties] = stringify_hash([:properties]) if [:properties] opts[:source] = { type: "snapshot", name: "#{container}/#{snapshot}" } response = post(images_path, opts). handle_async(response, [:sync]) end |
#create_image_from_url(url, options = {}) ⇒ Sawyer::Resource
Import an image from a remote URL.
Note: the URL passed to this method is not the URL of a tarball. Instead, the URL must return the following headers:
-
LXD-Image-URL
- URL of the image tarball -
LXD-Image-Hash
- SHA-256 fingerprint of the image tarball
The LXD server will first access the URL to obtain the values of these headers. It will then download the tarball specified in the LXD-Image-URL
header and verify that its fingerprint matches the value in the LXD-Image-Hash
header. If they match, the image will be imported.
In Apache, you can set this up using a .htaccess
file. See the Examples section for a sample.
277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/hyperkit/client/images.rb', line 277 def create_image_from_url(url, ={}) opts = .slice(:filename, :public) opts[:properties] = stringify_hash([:properties]) if [:properties] opts[:source] = { type: "url", url: url } response = post(images_path, opts). handle_async(response, [:sync]) end |
#create_image_secret(fingerprint) ⇒ Sawyer::Response
Generate a secret for an image that can be used by an untrusted client to retrieve information on and/or export a private image.
The secret is automatically invalidated 5 seconds after first using it (e.g. after calling Hyperkit.image(fingerprint, secret: “…”). This allows one to both retrieve the image information and then export it with the same secret.
If you wish to delete a created secret without using it, you can pass the operation ID returned by this method to Hyperkit::Client::Images#Hyperkit#Hyperkit::Operations#Hyperkit::Operations::cancel_operation, as shown in the examples below.
472 473 474 |
# File 'lib/hyperkit/client/images.rb', line 472 def create_image_secret(fingerprint) post(File.join(image_path(fingerprint), "secret")). end |
#delete_image(fingerprint, options = {}) ⇒ Sawyer::Resource
Delete an image
386 387 388 389 |
# File 'lib/hyperkit/client/images.rb', line 386 def delete_image(fingerprint, ={}) response = delete(image_path(fingerprint)). handle_async(response, [:sync]) end |
#delete_image_alias(alias_name) ⇒ Sawyer::Resource
Delete an alias for an image
598 599 600 |
# File 'lib/hyperkit/client/images.rb', line 598 def delete_image_alias(alias_name) delete(image_alias_path(alias_name)). end |
#export_image(fingerprint, output_dir, options = {}) ⇒ String
Export an image to a local file.
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/hyperkit/client/images.rb', line 502 def export_image(fingerprint, output_dir, ={}) img = image(fingerprint) filename = [:filename] || img.filename output_file = File.join(output_dir, filename) url = File.join(image_path(fingerprint), "export") url << "?secret=#{[:secret]}" if [:secret] response = get(url) File.open(output_file, "wb") do |f| f.write response end output_file end |
#image(fingerprint, options = {}) ⇒ Sawyer::Resource
Get information on an image by its fingerprint
53 54 55 56 57 58 59 |
# File 'lib/hyperkit/client/images.rb', line 53 def image(fingerprint, ={}) url = image_path(fingerprint) url << "?secret=#{[:secret]}" if [:secret] get(url). end |
#image_alias(alias_name) ⇒ Sawyer::Resource
Get information on an image alias
560 561 562 |
# File 'lib/hyperkit/client/images.rb', line 560 def image_alias(alias_name) get(image_alias_path(alias_name)). end |
#image_aliases ⇒ Array<String>
List of image aliases on the server (public or private)
544 545 546 547 |
# File 'lib/hyperkit/client/images.rb', line 544 def image_aliases response = get(image_aliases_path) response..map { |path| path.sub("#{image_aliases_path}/","") } end |
#image_by_alias(alias_name, options = {}) ⇒ Sawyer::Resource
Get information on an image by one of its aliases
92 93 94 95 |
# File 'lib/hyperkit/client/images.rb', line 92 def image_by_alias(alias_name, ={}) a = image_alias(alias_name) image(a.target, ) end |
#images ⇒ Array<String>
List of images on the server (public or private)
20 21 22 23 |
# File 'lib/hyperkit/client/images.rb', line 20 def images response = get(images_path) response..map { |path| path.split('/').last } end |
#rename_image_alias(old_alias, new_alias) ⇒ Sawyer::Resource
Rename an image alias
610 611 612 |
# File 'lib/hyperkit/client/images.rb', line 610 def rename_image_alias(old_alias, new_alias) post(image_alias_path(old_alias), { name: new_alias }). end |
#update_image(fingerprint, options = {}) ⇒ Sawyer::Resource
Update the attributes of an image
425 426 427 428 429 430 |
# File 'lib/hyperkit/client/images.rb', line 425 def update_image(fingerprint, ={}) opts = .slice(:public, :auto_update) opts[:properties] = stringify_hash([:properties]) if [:properties] put(image_path(fingerprint), opts). end |
#update_image_alias(alias_name, options = {}) ⇒ Sawyer::Resource
Update an image alias
628 629 630 631 632 633 634 635 636 637 638 639 |
# File 'lib/hyperkit/client/images.rb', line 628 def update_image_alias(alias_name, ={}) if .empty? raise Hyperkit::AliasAttributesRequired.new("At least one of :target or :description required") end = image_alias(alias_name).to_hash opts = .slice(:description, :target). merge(.slice(:description, :target)) put(image_alias_path(alias_name), opts). end |