lib/hyperkit/client/containers.rb in hyperkit-1.0.2 vs lib/hyperkit/client/containers.rb in hyperkit-1.1.0

- old
+ new

@@ -282,10 +282,13 @@ # @param container [String] Container name # @param command [Array|String] Command to execute # @param options [Hash] Additional data to be passed # @option options [Hash] :environment Environment variables to set prior to command execution # @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}. + # @option options [Boolean] :wait_for_websocket If <code>true</code> block and wait for a websocket connection to start. + # @option options [Boolean] :interactive If <code>true</code>a single websocket is returned and is mapped to a pts device for stdin, stdout and stderr of the execed process. If false, three pipes will be setup, one for each of stdin, stdout and stderr. + # @option options [Boolean] :record_output If <code>true</code>, captures the output of stdout and stderr. # @return [Sawyer::Resource] Operation or result, depending value of <code>:sync</code> parameter and/or {Hyperkit::Client::auto_sync} # # @example Run a command (passed as a string) in container "test-container" # Hyperkit.execute_command("test-container", "echo 'hello world'") # @@ -310,12 +313,13 @@ 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, - interactive: false + "wait-for-websocket" => options[:wait_for_websocket] || false, + interactive: options[:interactive] || false, + "record-output" => options[:record_output] || false }).metadata handle_async(response, options[:sync]) end @@ -830,28 +834,35 @@ # Copy a file from a container to the local system. The file will be # written with the same permissions assigned to it in the container. # # @param container [String] Container name # @param source_file [String] Full path to a file within the container - # @param dest_file [String] Full path of desired output file (will be created/overwritten) + # @param dest [String, IO] Full path of desired output file (will be created/overwritten), or an IO object to write to # @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) + # + # @example Copy /etc/passwd in container "test" to a StringIO object + # Hyperkit.pull_file("test", "/etc/passwd", StringIO.new) #=> <StringIO:0x007fd196061a70> + def pull_file(container, source_file, dest) 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 + if dest.respond_to? :write + dest.write(contents) + else + File.open(dest, "wb") do |f| + f.write(contents) + end - if headers["x-lxd-mode"] - File.chmod(headers["x-lxd-mode"].to_i(8), dest_file) + if headers["x-lxd-mode"] + File.chmod(headers["x-lxd-mode"].to_i(8), dest) + end end - dest_file + dest end # Write to a file in a container # @@ -907,31 +918,38 @@ # Copy a file from the local system to container # # @param container [String] Container name # @param source_file [String] Full path to a file within the container - # @param dest_file [String] Full path of desired output file (will be created/overwritten) + # @param dest [String, IO] Full path of desired output file (will be created/overwritten), or an IO object to write to # @param options [Hash] Additional data to be passed # @option options [Fixnum] :uid Owner to assign to the file # @option options [Fixnum] :gid Group to assign to the file # @option options [Fixnum] :mode File permissions (in octal) to assign to the file # @return [Sawyer::Resource] # # @example Copy /tmp/test.txt from the local system to /etc/passwd in the container # Hyperkit.push_file("/tmp/test.txt", "test-container", "/etc/passwd") # + # @example Write the contents of a StringIO object to /etc/passwd in the container + # Hyperkit.push_file(StringIO.new("test string"), "test-container", "/etc/passwd") + # # @example Assign uid, gid, and mode to a file: # Hyperkit.push_file("/tmp/test.txt", # "test-container", # "/etc/passwd", # uid: 1000, # gid: 1000, # mode: 0644 # ) - def push_file(source_file, container, dest_file, options={}) + def push_file(source, container, dest_file, options={}) write_file(container, dest_file, options) do |f| - f.write File.read(source_file) + if source.respond_to? :read + f.write source.read + else + f.write File.read(source) + end end end # @!endgroup