Module: Aptible::CLI::Helpers::Operation

Includes:
Ssh
Defined in:
lib/aptible/cli/helpers/operation.rb

Constant Summary collapse

POLL_INTERVAL =
1

Instance Method Summary collapse

Methods included from Ssh

#connect_to_ssh_portal, #exit_with_ssh_portal, #with_ssh_cmd

Instance Method Details

#attach_to_operation_logs(operation) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/aptible/cli/helpers/operation.rb', line 25

def attach_to_operation_logs(operation)
  # TODO: This isn't actually guaranteed to connect to the operation
  # logs, since the action will depend on what operation we're actually
  # connecting for. There might be ways to make this better.
  ENV['ACCESS_TOKEN'] = fetch_token

  code = connect_to_ssh_portal(
    operation,
    '-o', 'SendEnv=ACCESS_TOKEN'
  )

  # If the portal is down, fall back to polling for success. If the
  # operation failed, poll_for_success will immediately fall through to
  # the error message.
  unless code == 0
    e = 'Disconnected from logs, waiting for operation to complete'
    CLI.logger.warn e
    poll_for_success(operation)
  end
end

#cancel_operation(operation) ⇒ Object



46
47
48
49
# File 'lib/aptible/cli/helpers/operation.rb', line 46

def cancel_operation(operation)
  CLI.logger.info "Cancelling #{prettify_operation(operation)}..."
  operation.update!(cancelled: true)
end

#poll_for_success(operation) ⇒ Object

Raises:

  • (Thor::Error)


11
12
13
14
15
16
# File 'lib/aptible/cli/helpers/operation.rb', line 11

def poll_for_success(operation)
  wait_for_completion operation
  return if operation.status == 'succeeded'

  raise Thor::Error, "Operation ##{operation.id} failed."
end

#prettify_operation(o) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/aptible/cli/helpers/operation.rb', line 51

def prettify_operation(o)
  bits = [o.status, o.type, "##{o.id}"]
  if o.resource.respond_to?(:handle)
    bits.concat ['on', o.resource.handle]
  end
  bits.join ' '
end

#wait_for_completion(operation) ⇒ Object



18
19
20
21
22
23
# File 'lib/aptible/cli/helpers/operation.rb', line 18

def wait_for_completion(operation)
  while %w(queued running).include? operation.status
    sleep 1
    operation.get
  end
end