Class: Bosh::Director::AgentClient

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/agent_client.rb

Constant Summary collapse

DEFAULT_POLL_INTERVAL =
1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_name, client_id, options = {}) ⇒ AgentClient

Returns a new instance of AgentClient.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bosh/director/agent_client.rb', line 23

def initialize(service_name, client_id, options = {})
  @service_name = service_name
  @client_id = client_id
  @nats_rpc = Config.nats_rpc
  @timeout = options[:timeout] || 45
  @logger = Config.logger
  @retry_methods = options[:retry_methods] || {}

  if options[:credentials]
    @encryption_handler =
      Bosh::Core::EncryptionHandler.new(@client_id, options[:credentials])
  end

  @resource_manager = Api::ResourceManager.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



39
40
41
# File 'lib/bosh/director/agent_client.rb', line 39

def method_missing(method_name, *args)
  send_message(method_name, *args)
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/bosh/director/agent_client.rb', line 7

def id
  @id
end

Class Method Details

.with_defaults(id, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bosh/director/agent_client.rb', line 9

def self.with_defaults(id, options = {})
  defaults = {
    retry_methods: { # in case of timeout errors
                     get_state: 2,
                     get_task: 2,
    }
  }

  credentials = Bosh::Director::Models::Vm.find(:agent_id => id).credentials
  defaults.merge!(credentials: credentials) if credentials

  self.new('agent', id, defaults.merge(options))
end

Instance Method Details

#apply(*args) ⇒ Object



47
48
49
# File 'lib/bosh/director/agent_client.rb', line 47

def apply(*args)
  send_long_running_message(:apply, *args)
end

#compile_package(*args) ⇒ Object



51
52
53
# File 'lib/bosh/director/agent_client.rb', line 51

def compile_package(*args)
  send_long_running_message(:compile_package, *args)
end

#drain(*args) ⇒ Object



55
56
57
# File 'lib/bosh/director/agent_client.rb', line 55

def drain(*args)
  send_long_running_message(:drain, *args)
end

#fetch_logs(*args) ⇒ Object



59
60
61
# File 'lib/bosh/director/agent_client.rb', line 59

def fetch_logs(*args)
  send_long_running_message(:fetch_logs, *args)
end

#format_exception(exception) ⇒ String

Returns formatted exception information

Parameters:

  • exception (Hash|#to_s)

    Serialized exception

Returns:

  • (String)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/bosh/director/agent_client.rb', line 154

def format_exception(exception)
  return exception.to_s unless exception.is_a?(Hash)

  msg = exception["message"].to_s

  if exception["backtrace"]
    msg += "\n"
    msg += Array(exception["backtrace"]).join("\n")
  end

  if exception["blobstore_id"]
    blob = download_and_delete_blob(exception["blobstore_id"])
    msg += "\n"
    msg += blob.to_s
  end

  msg
end

#handle_method(method_name, args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/bosh/director/agent_client.rb', line 97

def handle_method(method_name, args)
  result = {}
  result.extend(MonitorMixin)

  cond = result.new_cond
  timeout_time = Time.now.to_f + @timeout

  request = { :method => method_name, :arguments => args }

  if @encryption_handler
    @logger.info("Request: #{request}")
    request = { "encrypted_data" => @encryption_handler.encrypt(request) }
    request["session_id"] = @encryption_handler.session_id
  end

  recipient = "#{@service_name}.#{@client_id}"

  request_id = @nats_rpc.send_request(recipient, request) do |response|
    if @encryption_handler
      begin
        response = @encryption_handler.decrypt(response["encrypted_data"])
      rescue Bosh::Core::EncryptionHandler::CryptError => e
        response["exception"] = "CryptError: #{e.inspect} #{e.backtrace}"
      end
      @logger.info("Response: #{response}")
    end

    result.synchronize do
      inject_compile_log(response)
      result.merge!(response)
      cond.signal
    end
  end

  result.synchronize do
    while result.empty?
      timeout = timeout_time - Time.now.to_f
      unless timeout > 0
        @nats_rpc.cancel_request(request_id)
        raise RpcTimeout,
              "Timed out sending `#{method_name}' to #{@client_id} " +
                "after #{@timeout} seconds"
      end
      cond.wait(timeout)
    end
  end

  if result.has_key?("exception")
    raise RpcRemoteException, format_exception(result["exception"])
  end

  result["value"]
end

#migrate_disk(*args) ⇒ Object



63
64
65
# File 'lib/bosh/director/agent_client.rb', line 63

def migrate_disk(*args)
  send_long_running_message(:migrate_disk, *args)
end

#mount_disk(*args) ⇒ Object



67
68
69
# File 'lib/bosh/director/agent_client.rb', line 67

def mount_disk(*args)
  send_long_running_message(:mount_disk, *args)
end

#prepare(*args) ⇒ Object



43
44
45
# File 'lib/bosh/director/agent_client.rb', line 43

def prepare(*args)
  send_long_running_message(:prepare, *args)
end

#stop(*args) ⇒ Object



75
76
77
# File 'lib/bosh/director/agent_client.rb', line 75

def stop(*args)
  send_long_running_message(:stop, *args)
end

#unmount_disk(*args) ⇒ Object



71
72
73
# File 'lib/bosh/director/agent_client.rb', line 71

def unmount_disk(*args)
  send_long_running_message(:unmount_disk, *args)
end

#wait_until_ready(deadline = 600) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bosh/director/agent_client.rb', line 79

def wait_until_ready(deadline = 600)
  old_timeout = @timeout
  @timeout = 1.0
  @deadline = Time.now.to_i + deadline

  begin
    ping
  rescue RpcTimeout
    retry if @deadline - Time.now.to_i > 0
    raise RpcTimeout, "Timed out pinging to #{@client_id} after #{deadline} seconds"
  rescue RpcRemoteException => e
    retry if e.message =~ /^restarting agent/ && @deadline - Time.now.to_i > 0
    raise e
  ensure
    @timeout = old_timeout
  end
end