Sha256: f5c9f0b3d935b16d6076111466666c8b7dcb8dc1a9776aba5504af1c445dae58

Contents?: true

Size: 1.06 KB

Versions: 10

Compression:

Stored size: 1.06 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|
              self.response << Base64.decode64(event.data)
            end

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

          self.response = self.response.join

          self
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rancher-api-0.4.0 lib/rancher/api/models/instance/action.rb
rancher-api-0.3.8 lib/rancher/api/models/instance/action.rb
rancher-api-0.3.7 lib/rancher/api/models/instance/action.rb
rancher-api-0.3.6 lib/rancher/api/models/instance/action.rb
rancher-api-0.3.5 lib/rancher/api/models/instance/action.rb
rancher-api-0.3.3 lib/rancher/api/models/instance/action.rb
rancher-api-0.3.2 lib/rancher/api/models/instance/action.rb
rancher-api-0.3.1 lib/rancher/api/models/instance/action.rb
rancher-api-0.3.0 lib/rancher/api/models/instance/action.rb
rancher-api-0.2.0 lib/rancher/api/models/instance/action.rb