Class: Aptible::Api::Operation

Inherits:
Resource
  • Object
show all
Defined in:
lib/aptible/api/operation.rb

Instance Method Summary collapse

Methods inherited from Resource

#namespace, #root_url

Instance Method Details

#failed?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/aptible/api/operation.rb', line 73

def failed?
  status == 'failed'
end

#logs_urlObject



65
66
67
# File 'lib/aptible/api/operation.rb', line 65

def logs_url
  links['logs'].href
end

#succeeded?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/aptible/api/operation.rb', line 69

def succeeded?
  status == 'succeeded'
end

#userObject



58
59
60
61
62
63
# File 'lib/aptible/api/operation.rb', line 58

def user
  auth = Aptible::Auth::User.new(token: token, headers: headers)
  auth.find_by_url(links['user'].href)
rescue StandardError
  nil
end

#with_ssh_cmd(private_key_file, host: nil, port: nil, host_key: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/aptible/api/operation.rb', line 77

def with_ssh_cmd(private_key_file, host: nil, port: nil, host_key: nil)
  # We expect that the public key will be found next to the private key,
  # which is also what SSH itself expects. If that's not the case, then
  # we'll just fail. The Aptible CLI will always ensure credentials are
  # set up properly (other consumers are of course responsible for doing
  # the same!).
  public_key_file = "#{private_key_file}.pub"

  private_key = File.read(private_key_file)
  public_key = File.read(public_key_file)

  connection = create_ssh_portal_connection!(ssh_public_key: public_key)
  certificate = connection.ssh_certificate_body

  # Backups can be restored across stacks, and the operation executes
  # on the destiantion stack. Assuming all future cross-stack operations
  # will behave the same way, fingers crossed they'll "just work"
  stack = .try(&:stack) || .stack
  host ||= stack.ssh_portal_host
  port ||= stack.ssh_portal_port
  host_key ||= stack.ssh_host_rsa_public_key

  with_temporary_known_hosts(
    host, port, host_key
  ) do |known_hosts_file|
    with_temporary_id(private_key, public_key, certificate) do |id_file|
      cmd = [
        'ssh',
        "#{connection.ssh_user}@#{host}",
        '-p', port.to_s,
        '-i', id_file,
        '-o', 'IdentitiesOnly=yes',
        '-o', "UserKnownHostsFile=#{known_hosts_file}",
        '-o', 'StrictHostKeyChecking=yes'
      ]

      # If we aren't allowed to create a pty, then we shouldn't try to
      # allocate once, or we'll get an awkward error.
      cmd << '-T' unless connection.ssh_pty

      yield cmd, connection
    end
  end
end