lib/hyperkit/client/containers.rb in hyperkit-1.0.0 vs lib/hyperkit/client/containers.rb in hyperkit-1.0.2
- old
+ new
@@ -4,11 +4,11 @@
module Hyperkit
class Client
# Methods for the containers API
- #
+ #
# @see Hyperkit::Client
# @see https://github.com/lxc/lxd/blob/master/specs/rest-api.md
module Containers
# @!group Retrieval
@@ -196,11 +196,11 @@
}
}.merge(extract_container_options(target_name, options))
response = post(containers_path, opts).metadata
handle_async(response, options[:sync])
-
+
end
# @!endgroup
# @!group Editing
@@ -231,11 +231,11 @@
#
# @example Change container to be ephemeral (i.e. it will be deleted when stopped)
# container = Hyperkit.container("test-container")
# container.ephemeral = true
# Hyperkit.update_container("test-container", container)
- #
+ #
# @example Change container's AppArmor profile to 'unconfined'.
# container = Hyperkit.container("test-container")
#
# # Note: due to a bug in Sawyer::Resource, the following will fail
# container.config[:"raw.lxc"] = "lxc.aa_profile=unconfined"
@@ -246,15 +246,11 @@
#
# Hyperkit.update_container("test-container", container)
def update_container(name, config, options={})
config = config.to_hash
-
- # Stringify values in the config hash, since LXD chokes on non-String values
- if config[:config]
- config[:config] = config[:config].inject({}){|h,(k,v)| h[k.to_s] = v.to_s; h}
- end
+ config[:config] = stringify_hash(config[:config]) if config[:config]
response = put(container_path(name), config).metadata
handle_async(response, options[:sync])
end
@@ -269,11 +265,11 @@
# @return [Sawyer::Resource] Operation or result, depending value of <code>:sync</code> parameter and/or {Hyperkit::Client::auto_sync}
#
# @example Rename container "test" to "test2"
# Hyperkit.rename_container("test", "test2")
def rename_container(old_name, new_name, options={})
- response = post(container_path(old_name), { "name": new_name }).metadata
+ response = post(container_path(old_name), { name: new_name }).metadata
handle_async(response, options[:sync])
end
# @!endgroup
@@ -292,11 +288,11 @@
#
# @example Run a command (passed as a string) in container "test-container"
# Hyperkit.execute_command("test-container", "echo 'hello world'")
#
# @example Run a command (passed as an array) in container "test-container"
- # Hyperkit.execute_command("test-container",
+ # Hyperkit.execute_command("test-container",
# ["bash", "-c", "echo 'hello world' > /tmp/test.txt"]
# )
#
# @example Run a command and pass environment variables
# Hyperkit.execute_command("test-container",
@@ -309,14 +305,11 @@
def execute_command(container, command, options={})
opts = options.slice(:environment)
command = Shellwords.shellsplit(command) if command.is_a?(String)
- # Stringify any environment values since LXD croaks on non-String values
- if opts[:environment]
- opts[:environment] = opts[:environment].inject({}){|h,(k,v)| h[k.to_s] = v.to_s; h}
- end
+ opts[:environment] = stringify_hash(opts[:environment]) if opts[:environment]
response = post(File.join(container_path(container), "exec"), {
command: command,
environment: opts[:environment] || {},
"wait-for-websocket" => false,
@@ -502,11 +495,11 @@
# @!group Migration
# Prepare to migrate a container or snapshot. Generates source data to be passed to {#migrate}.
#
# Note that CRIU must be installed on the server to migrate a running container, or LXD will
- # return a 500 error. On Ubuntu, you can install it with
+ # return a 500 error. On Ubuntu, you can install it with
# <code>sudo apt-get install criu</code>.
#
# @param name [String] Container name
# @return [Sawyer::Resource] Source data to be passed to {#migrate}
#
@@ -560,11 +553,11 @@
else
url = container_path(container)
source = container(container)
end
- response = post(url, { "migration": true })
+ response = post(url, { migration: true })
agent = response.agent
source_data = {
architecture: source.architecture,
config: source.config.to_hash,
@@ -581,11 +574,11 @@
end
# Migrate a remote container or snapshot to the server
#
# Note that CRIU must be installed on the server to migrate a running container, or LXD will
- # return a 500 error. On Ubuntu, you can install it with
+ # return a 500 error. On Ubuntu, you can install it with
# <code>sudo apt-get install criu</code>.
#
# Also note that, unless overridden with the <code>profiles</code> parameter, if the source
# container has profiles applied to it that do not exist on the target LXD instance, this
# method will throw an exception.
@@ -639,11 +632,11 @@
if ! source.snapshot
opts["base-image"] = source.config["volatile.base_image"]
opts[:config] = options[:config] || source.config.to_hash
- # If we're only copying the container, and configuration was explicitly
+ # If we're only copying the container, and configuration was explicitly
# overridden, then remove the volatile entries
if ! options[:move] && ! options.has_key?(:config)
opts[:config].delete_if { |k,v| k.to_s.start_with?("volatile") }
end
@@ -660,11 +653,11 @@
if (source.profiles - dest_profiles).empty?
opts[:profiles] = source.profiles
else
raise Hyperkit::MissingProfiles.new("Not all profiles applied to source container exist on the target LXD instance")
end
-
+
end
if options.has_key?(:ephemeral)
opts[:ephemeral] = options[:ephemeral]
else
@@ -733,15 +726,15 @@
get(snapshot_path(container, snapshot)).metadata
end
# Create a snapshot of a container
#
- # If <code>stateful: true</code> is passed when creating a snapshot of a
+ # If <code>stateful: true</code> is passed when creating a snapshot of a
# running container, the container's runtime state will be stored in the
- # snapshot. Note that CRIU must be installed on the server to create a
+ # snapshot. Note that CRIU must be installed on the server to create a
# stateful snapshot, or LXD will return a 500 error. On Ubuntu, you can
- # install it with
+ # install it with
# <code>sudo apt-get install criu</code>.
#
# @async This method is asynchronous. See {Hyperkit::Configurable#auto_sync} for more information.
#
# @param container [String] Container name
@@ -793,11 +786,11 @@
# @return [Sawyer::Resource] Operation or result, depending value of <code>:sync</code> parameter and/or {Hyperkit::Client::auto_sync}
#
# @example Rename snapshot "test/snap1" to "snap2"
# Hyperkit.rename_snapshot("test", "snap1", "snap2")
def rename_snapshot(container, old_name, new_name, options={})
- response = post(snapshot_path(container, old_name), { "name": new_name }).metadata
+ response = post(snapshot_path(container, old_name), { name: new_name }).metadata
handle_async(response, options[:sync])
end
# Restore a snapshot
#
@@ -810,11 +803,11 @@
# @return [Sawyer::Resource] Operation or result, depending value of <code>:sync</code> parameter and/or {Hyperkit::Client::auto_sync}
#
# @example Restore container "test" back to snapshot "snap1"
# Hyperkit.restore_snapshot("test", "snap1")
def restore_snapshot(container, snapshot, options={})
- response = put(container_path(container), { "restore": snapshot }).metadata
+ response = put(container_path(container), { restore: snapshot }).metadata
handle_async(response, options[:sync])
end
alias_method :revert_to_snapshot, :restore_snapshot
@@ -842,25 +835,25 @@
# @param dest_file [String] Full path of desired output file (will be created/overwritten)
# @return [String] Full path to the local output file
#
# @example Copy /etc/passwd in container "test" to the local file /tmp/passwd
# Hyperkit.pull_file("test", "/etc/passwd", "/tmp/passwd") #=> "/tmp/passwd"
- def pull_file(container, source_file, dest_file)
- contents = get(file_path(container, source_file), url_encode: false)
- headers = last_response.headers
+ def pull_file(container, source_file, dest_file)
+ contents = get(file_path(container, source_file), url_encode: false)
+ headers = last_response.headers
- File.open(dest_file, "wb") do |f|
- f.write(contents)
- end
+ File.open(dest_file, "wb") do |f|
+ f.write(contents)
+ end
- if headers["x-lxd-mode"]
- File.chmod(headers["x-lxd-mode"].to_i(8), dest_file)
- end
+ if headers["x-lxd-mode"]
+ File.chmod(headers["x-lxd-mode"].to_i(8), dest_file)
+ end
- dest_file
+ dest_file
- end
+ end
# Write to a file in a container
#
# @param container [String] Container name
# @param dest_file [String] Path to the output file in the container
@@ -894,18 +887,18 @@
headers = { "Content-Type" => "application/octet-stream" }
headers["X-LXD-uid"] = options[:uid].to_s if options[:uid]
headers["X-LXD-gid"] = options[:gid].to_s if options[:gid]
headers["X-LXD-mode"] = options[:mode].to_s(8).rjust(4, "0") if options[:mode]
- if ! block_given?
- content = options[:content].to_s
- else
- io = StringIO.new
- yield io
- io.rewind
- content = io.read
- end
+ if ! block_given?
+ content = options[:content].to_s
+ else
+ io = StringIO.new
+ yield io
+ io.rewind
+ content = io.read
+ end
post(file_path(container, dest_file), {
raw_body: content,
headers: headers
})
@@ -947,11 +940,11 @@
# @!group Logs
# Retrieve a list of logs for a container
#
# @param container [String] Container name
- # @return [Array<String>] An array of log filenames
+ # @return [Array<String>] An array of log filenames
#
# @example Get list of logs for container "test-container"
# Hyperkit.logs("test-container")
def logs(container)
response = get(logs_path(container))
@@ -1020,16 +1013,13 @@
end
def extract_container_options(name, options)
opts = options.slice(:architecture, :profiles, :ephemeral, :config).
merge({ name: name })
-
- # Stringify any config values since LXD croaks on non-String values
- if opts[:config]
- opts[:config] = opts[:config].inject({}){|h,(k,v)| h[k.to_s] = v.to_s; h}
- end
+ opts[:config] = stringify_hash(opts[:config]) if opts[:config]
+
opts
end
def container_source_attribute(options)
@@ -1090,10 +1080,10 @@
end
opts
end
-
+
end
end
end