Sha256: b37a7a15155c2b516cf2144301eec4462330095986b157dea76fad1faa89f30c

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require 'faye/websocket'
require 'eventmachine'

module Rancher
  module Api
    class Instance
      # Few possible actions are:
      #   - execute shell command
      #   - render logs
      #   - restart
      #
      class Action
        include Her::Model

        attributes :token, :url, :response

        def run!
          uri = URI.parse(url)
          uri.userinfo = "#{Rancher::Api.configuration.access_key}:#{Rancher::Api.configuration.secret_key}"
          uri.query = "token=#{token}"

          self.response = []

          EM.run do
            # 15 seconds timeout in case of slow operation
            #
            EM.add_timer(15) do
              EM.stop
            end

            ws = Faye::WebSocket::Client.new(uri.to_s)

            ws.on :message do |event|
              response << Base64.decode64(event.data)
            end

            ws.on :close do |_event|
              ws = nil
              EM.stop
            end
          end

          self.response = response.join

          self
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rancher-api-beta-0.8.1 lib/rancher/api/models/instance/action.rb
rancher-api-beta-0.8.0.pre.beta lib/rancher/api/models/instance/action.rb