Method: Hyperkit::Client::Containers#write_file

Defined in:
lib/hyperkit/client/containers.rb

#write_file(container, dest_file, options = {}) {|io| ... } ⇒ Sawyer::Resource

Write to a file in a container

Examples:

Write string “hello” to /tmp/test.txt in container test-container

Hyperkit.write_file("test-container", "/tmp/test.txt", content: "hello")

Write to file using a block

Hyperkit.write_file("test-container", "/tmp/test.txt") do |io|
  io.print "Hello "
  io.puts "world"
end

Assign uid, gid, and mode to a file:

Hyperkit.write_file("test-container",
  "/tmp/test.txt",
  content: "hello",
  uid: 1000,
  gid: 1000,
  mode: 0644
)

Options Hash (options):

  • :uid (Fixnum)

    Owner to assign to the file

  • :gid (Fixnum)

    Group to assign to the file

  • :mode (Fixnum)

    File permissions (in octal) to assign to the file

  • :content (Fixnum)

    Content to write to the file (if no block given)

Yield Parameters:

  • io (StringIO)

    IO to be used to write to the file from a block



896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
# File 'lib/hyperkit/client/containers.rb', line 896

def write_file(container, dest_file, options={}, &block)

  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

  post(file_path(container, dest_file), {
    raw_body: content,
    headers: headers
  })

end