Sha256: 3415735ef572dd9675a996e824ad9073054aa5edb3ecebcaf8e02f226712d611

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

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

5 entries across 5 versions & 1 rubygems

Version Path
rancher-api-0.7.0 lib/rancher/api/models/instance/action.rb
rancher-api-0.6.0 lib/rancher/api/models/instance/action.rb
rancher-api-0.5.2 lib/rancher/api/models/instance/action.rb
rancher-api-0.5.1 lib/rancher/api/models/instance/action.rb
rancher-api-0.5.0 lib/rancher/api/models/instance/action.rb