Module: Hyperkit::Client::Operations

Included in:
Hyperkit::Client
Defined in:
lib/hyperkit/client/operations.rb

Overview

Methods for the operations API

Instance Method Summary collapse

Instance Method Details

#cancel_operation(uuid) ⇒ Sawyer::Resource

Cancel a running operation

Calling this will change the state of the operation to cancelling. Note that the operation must be cancelable, which can be ascertained by calling #operation and checking the may_cancel property.

Examples:

Cancel an operation

Hyperkit.cancel_operation("8b3dd0c2-9dad-4964-b00d-e21481a47fb8") => {}

Parameters:

  • uuid (String)

    UUID of the operation

Returns:

  • (Sawyer::Resource)


67
68
69
# File 'lib/hyperkit/client/operations.rb', line 67

def cancel_operation(uuid)
  delete(operation_path(uuid)).
end

#operation(uuid) ⇒ Sawyer::Resource

Retrieve information about an operation

Examples:

Retrieve information about an operation

Hyperkit.operation("d5f359ae-ddcb-4f09-a8f8-0cc2f3c8b0df") #=> {
  :id => "d5f359ae-ddcb-4f09-a8f8-0cc2f3c8b0df",
  :class => "task",
  :created_at => 2016-04-14 21:30:59 UTC,
  :updated_at => 2016-04-14 21:30:59 UTC,
  :status => "Running",
  :status_code => 103,
  :resources => {
    :containers => ["/1.0/containers/test-container"]
  },
  :metadata => nil,
  :may_cancel => false,
  :err => ""
}

Parameters:

  • uuid (String)

    UUID of the operation

Returns:

  • (Sawyer::Resource)

    Operation information



51
52
53
# File 'lib/hyperkit/client/operations.rb', line 51

def operation(uuid)
  get(operation_path(uuid)).
end

#operationsArray<String>

List of operations active on the server

This will include operations that are currently executing, as well as operations that are paused until #wait_for_operation is called, at which time they will begin executing.

Additionally, since LXD keeps completed operations around for 5 seconds, the list returned may include recently completed operations.

Examples:

Get list of operations

Hyperkit.operations #=> ["931e27fb-2057-4cbe-a49d-fd114713fa74"]

Returns:

  • (Array<String>)

    An array of UUIDs identifying waiting, active, and recently completed (<5 seconds) operations



26
27
28
29
# File 'lib/hyperkit/client/operations.rb', line 26

def operations
  response = get(operations_path)
  response..to_h.values.flatten.map { |path| path.split('/').last }
end

#wait_for_operation(uuid, timeout = nil) ⇒ Sawyer::Resource

Wait for an asynchronous operation to complete

Note that this is only needed if Hyperkit::Configurable#auto_sync has been set to false, or if the option sync: false has been passed to an asynchronous method.

Note that, after an operation completes, LXD keeps it around for only 5 seconds, so if you wait too long to call wait_for_operation, you’ll get an exception when you eventually do call it.

Examples:

Wait for the creation of a container

Hyperkit.auto_sync = false
op = Hyperkit.create_container("test-container", alias: "ubuntu/amd64/default")
Hyperkit.wait_for_operation(op.id)

Wait, but time out if the operation is not complete after 30 seconds

op = Hyperkit.copy_container("test1", "test2", sync: false)
Hyperkit.wait_for_operation(op.id, timeout: 30)

Parameters:

  • uuid (String)

    UUID of the operation

  • timeout (Fixnum) (defaults to: nil)

    Maximum time to wait (default: indefinite)

Returns:

  • (Sawyer::Resource)

    Operation result



94
95
96
97
98
99
# File 'lib/hyperkit/client/operations.rb', line 94

def wait_for_operation(uuid, timeout=nil)
  url = File.join(operation_path(uuid), "wait")
  url += "?timeout=#{timeout}" if timeout.to_i > 0

  get(url).
end