Sha256: 22ea114721c2af6efd3e93db86f13b649931619cfd1748819b99389d33d7692f

Contents?: true

Size: 1.75 KB

Versions: 24

Compression:

Stored size: 1.75 KB

Contents

require 'aptible/api'

module Aptible
  module CLI
    module Helpers
      module Operation
        include Helpers::Ssh

        POLL_INTERVAL = 1

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

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

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

        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

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

        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
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
aptible-cli-0.19.3 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.19.2 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.19.1 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.19.0 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.18.3 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.18.2 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.18.1 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.18.0 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.17.2 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.17.1 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.17.0 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.16.9 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.16.8 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.16.7 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.16.6 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.16.5 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.16.4 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.16.3 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.16.2 lib/aptible/cli/helpers/operation.rb
aptible-cli-0.16.1 lib/aptible/cli/helpers/operation.rb