lib/hyperkit/client/images.rb in hyperkit-1.0.0 vs lib/hyperkit/client/images.rb in hyperkit-1.0.2
- old
+ new
@@ -1,25 +1,25 @@
module Hyperkit
class Client
# Methods for the images API
- #
+ #
# @see Hyperkit::Client
# @see https://github.com/lxc/lxd/blob/master/specs/rest-api.md
module Images
# @!group Retrieval
# List of images on the server (public or private)
#
# @return [Array<String>] An array of image fingerprints
#
- # @example Get list of images
+ # @example Get list of images
# Hyperkit.images #=> ["54c8caac1f61901ed86c68f24af5f5d3672bdc62c71d04f06df3a59e95684473",
# "97d97a3d1d053840ca19c86cdd0596cf1be060c5157d31407f2a4f9f350c78cc"]
- def images
+ def images
response = get(images_path)
response.metadata.map { |path| path.split('/').last }
end
# Get information on an image by its fingerprint
@@ -169,19 +169,19 @@
# @option options [String] :secret Secret to use to retrieve the image
# @option options [Boolean] :sync If <code>false</code>, returns an asynchronous operation that must be passed to {Hyperkit::Client::Operations#wait_for_operation}. If <code>true</code>, automatically waits and returns the result of the operation. Defaults to value of {Hyperkit::Configurable#auto_sync}.
# @return [Sawyer::Resource] Operation or result, depending value of <code>:sync</code> parameter and/or {Hyperkit::Client::auto_sync}
#
# @example Import image by alias
- # Hyperkit.create_image_from_remote("https://images.linuxcontainers.org:8443",
+ # Hyperkit.create_image_from_remote("https://images.linuxcontainers.org:8443",
# alias: "ubuntu/xenial/amd64")
- #
+ #
# @example Import image by fingerprint
- # Hyperkit.create_image_from_remote("https://images.linuxcontainers.org:8443",
+ # Hyperkit.create_image_from_remote("https://images.linuxcontainers.org:8443",
# fingerprint: "b1cf3d836196c316897d39872ff25e2d912ea933207b0c591334a67b290a5f1b")
#
# @example Import image and automatically update it when it is updated on the remote server
- # Hyperkit.create_image_from_remote("https://images.linuxcontainers.org:8443",
+ # Hyperkit.create_image_from_remote("https://images.linuxcontainers.org:8443",
# alias: "ubuntu/xenial/amd64",
# auto_update: true)
#
# @example Store properties with the imported image (will be applied on top of any source properties)
# Hyperkit.create_image_from_remote("https://images.linuxcontainers.org:8443",
@@ -198,34 +198,34 @@
raise Hyperkit::InvalidProtocol.new("Invalid protocol. Valid choices: lxd, simplestreams")
end
opts[:source] = options.slice(:secret, :protocol, :certificate)
opts[:source].merge!({
- type: "image",
+ type: "image",
mode: "pull",
server: server
})
if options[:alias].nil? && options[:fingerprint].nil?
raise Hyperkit::ImageIdentifierRequired.new("Please specify either :alias or :fingerprint")
end
- opts[:properties] = stringify_properties(options[:properties]) if options[:properties]
+ opts[:properties] = stringify_hash(options[:properties]) if options[:properties]
if options[:alias]
opts[:source][:alias] = options[:alias]
else
opts[:source][:fingerprint] = options[:fingerprint]
end
response = post(images_path, opts).metadata
- handle_async(response, options[:sync])
+ handle_async(response, options[:sync])
end
# Import an image from a remote URL.
#
- # Note: the URL passed to this method is <b>not</b> the URL of a tarball.
+ # Note: the URL passed to this method is <b>not</b> the URL of a tarball.
# Instead, the URL must return the following headers:
#
# * <code>LXD-Image-URL</code> - URL of the image tarball
# * <code>LXD-Image-Hash</code> - SHA-256 fingerprint of the image tarball
#
@@ -275,15 +275,15 @@
# Header set LXD-Image-Hash 097e75d6f7419d3a5e204d8125582f2d7bdd4ee4c35bd324513321c645f0c415
# </Files>
def create_image_from_url(url, options={})
opts = options.slice(:filename, :public)
- opts[:properties] = stringify_properties(options[:properties]) if options[:properties]
- opts[:source] = {
- type: "url",
- url: url
- }
+ opts[:properties] = stringify_hash(options[:properties]) if options[:properties]
+ opts[:source] = {
+ type: "url",
+ url: url
+ }
response = post(images_path, opts).metadata
handle_async(response, options[:sync])
end
@@ -315,18 +315,18 @@
# }
# )
def create_image_from_container(name, options={})
opts = options.slice(:filename, :public, :description)
- opts[:properties] = stringify_properties(options[:properties]) if options[:properties]
- opts[:source] = {
- type: "container",
- name: name
- }
+ opts[:properties] = stringify_hash(options[:properties]) if options[:properties]
+ opts[:source] = {
+ type: "container",
+ name: name
+ }
response = post(images_path, opts).metadata
- handle_async(response, options[:sync])
+ handle_async(response, options[:sync])
end
# Create an image from an existing snapshot.
#
# @async This method is asynchronous. See {Hyperkit::Configurable#auto_sync} for more information.
@@ -356,18 +356,18 @@
# }
# )
def create_image_from_snapshot(container, snapshot, options={})
opts = options.slice(:filename, :public, :description)
- opts[:properties] = stringify_properties(options[:properties]) if options[:properties]
- opts[:source] = {
- type: "snapshot",
- name: "#{container}/#{snapshot}"
- }
+ opts[:properties] = stringify_hash(options[:properties]) if options[:properties]
+ opts[:source] = {
+ type: "snapshot",
+ name: "#{container}/#{snapshot}"
+ }
response = post(images_path, opts).metadata
- handle_async(response, options[:sync])
+ handle_async(response, options[:sync])
end
# Delete an image
#
# @async This method is asynchronous. See {Hyperkit::Configurable#auto_sync} for more information.
@@ -383,11 +383,11 @@
#
# @example Delete an image using a prefix of its fingerprint
# Hyperkit.delete_image("b41")
def delete_image(fingerprint, options={})
response = delete(image_path(fingerprint)).metadata
- handle_async(response, options[:sync])
+ handle_async(response, options[:sync])
end
# @!endgroup
# @!group Editing
@@ -402,11 +402,11 @@
# @return [Sawyer::Resource]
#
# @example Set image to be publicly-accessible
# Hyperkit.update_image("b1cf3d836196c316897d39872ff25e2d912ea933207b0c591334a67b290a5f1b",
# public: true)
- #
+ #
# @example Overwrite image properties (removes all existing properties, sets "hello" property to "world")
# Hyperkit.update_image("b1cf3d836196c316897d39872ff25e2d912ea933207b0c591334a67b290a5f1b",
# properties: {
# hello: "world"
# }
@@ -422,30 +422,29 @@
# hello: "world"
# })
# )
def update_image(fingerprint, options={})
opts = options.slice(:public, :auto_update)
- opts[:properties] = stringify_properties(options[:properties]) if options[:properties]
+ opts[:properties] = stringify_hash(options[:properties]) if options[:properties]
put(image_path(fingerprint), opts).metadata
end
-
- # Generate a secret for an image that can be used by an untrusted client
+
+ # 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
+ # 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::Operations::cancel_operation}, as shown
# in the examples below.
#
# @param fingerprint [String] Fingerprint of the image. This can be a prefix of an image's fingerprint, as long as it is unambiguous.
# @return [Sawyer::Response] An asynchronous response containing the generated secret
- # @todo Add test for fingerprint prefix
#
# @example Generate a secret for an image
# response = Hyperkit.create_image_secret("878cf0c70e14fec80aaf4d5e923670e68c45aa89fb05a481019bf086aec42649") #=> {
# :id => "c8e949d4-0b6e-45de-83c9-5b886ed0256b",
# :class => "token",
@@ -496,11 +495,11 @@
# Hyperkit.export_image(image.fingerprint,
# "/tmp", filename: "test.tar.xz") => "/tmp/test.tar.xz"
#
# @example Export private image via secret (created by {#create_image_secret})
# image = Hyperkit.image_by_alias("busybox/default/amd64")
- # Hyperkit.export_image(image.fingerprint,
+ # Hyperkit.export_image(image.fingerprint,
# "/tmp", secret: "secret-issued-by-create_image_secret") => "/tmp/busybox-v1.21.1-lxc.tar.xz"
def export_image(fingerprint, output_dir, options={})
img = image(fingerprint)
filename = options[:filename] || img.filename
@@ -539,11 +538,11 @@
# "ubuntu/xenial/powerpc",
# "ubuntu/xenial/ppc64el/default",
# "ubuntu/xenial/ppc64el",
# "ubuntu/xenial/s390x/default",
# "ubuntu/xenial/s390x"
- # ]
+ # ]
def image_aliases
response = get(image_aliases_path)
response.metadata.map { |path| path.sub("#{image_aliases_path}/","") }
end
@@ -559,11 +558,11 @@
# :target=>"878cf0c70e14fec80aaf4d5e923670e68c45aa89fb05a481019bf086aec42649"
# }
def image_alias(alias_name)
get(image_alias_path(alias_name)).metadata
end
-
+
# Assign an alias for an image
#
# @param fingerprint [String] Fingerprint of the image
# @param alias_name [String] Alias to assign to the image
# @param options [Hash] Additional data to be passed
@@ -657,15 +656,9 @@
def images_path
"/1.0/images"
end
- # Stringify any property values. LXD returns an error if
- # integers are passed, for example
- def stringify_properties(properties)
- properties.inject({}){|h,(k,v)| h[k.to_s] = v.to_s; h}
- end
-
end
end
end