Class: Bosh::Director::AgentClient

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

Constant Summary collapse

PROTOCOL_VERSION =
2
DEFAULT_POLL_INTERVAL =
1.0
GET_TASK_MAX_RETRIES =

in case of timeout errors

2
GET_STATE_MAX_RETRIES =

get_task should retry at least once because some long running tasks (e.g. configure_networks) will restart the agent (current implementation) which most likely will result in first get_task message being lost because agent was not listening on NATS and second retry message will probably be received because agent came back up.

2

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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bosh/director/agent_client.rb', line 41

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



57
58
59
# File 'lib/bosh/director/agent_client.rb', line 57

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

Instance Attribute Details

#idObject

Returns the value of attribute id.



20
21
22
# File 'lib/bosh/director/agent_client.rb', line 20

def id
  @id
end

Class Method Details

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



22
23
24
25
# File 'lib/bosh/director/agent_client.rb', line 22

def self.with_defaults(id, options = {})
  vm = Bosh::Director::Models::Vm.find(:agent_id => id)
  with_vm(vm, options)
end

.with_vm(vm, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bosh/director/agent_client.rb', line 27

def self.with_vm(vm, options = {})
  defaults = {
    retry_methods: {
      get_state: GET_STATE_MAX_RETRIES,
      get_task: GET_TASK_MAX_RETRIES,
    }
  }

  credentials = vm.credentials
  defaults.merge!(credentials: credentials) if credentials

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

Instance Method Details

#apply(*args) ⇒ Object



89
90
91
# File 'lib/bosh/director/agent_client.rb', line 89

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

#cancel_task(*args) ⇒ Object



65
66
67
# File 'lib/bosh/director/agent_client.rb', line 65

def cancel_task(*args)
  send_message(:cancel_task, *args)
end

#compile_package(*args) ⇒ Object



93
94
95
# File 'lib/bosh/director/agent_client.rb', line 93

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

#configure_networks(*args) ⇒ Object



161
162
163
# File 'lib/bosh/director/agent_client.rb', line 161

def configure_networks(*args)
  send_message(:configure_networks, *args)
end

#drain(*args) ⇒ Object



97
98
99
# File 'lib/bosh/director/agent_client.rb', line 97

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

#fetch_logs(*args) ⇒ Object



101
102
103
# File 'lib/bosh/director/agent_client.rb', line 101

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

#format_exception(exception) ⇒ String

Returns formatted exception information

Parameters:

  • exception (Hash|#to_s)

    Serialized exception

Returns:

  • (String)


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/bosh/director/agent_client.rb', line 240

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

#get_state(*args) ⇒ Object



61
62
63
# File 'lib/bosh/director/agent_client.rb', line 61

def get_state(*args)
  send_message(:get_state, *args)
end

#handle_method(method_name, args) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/bosh/director/agent_client.rb', line 183

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

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

  request = { :protocol => PROTOCOL_VERSION, :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

#list_disk(*args) ⇒ Object



69
70
71
# File 'lib/bosh/director/agent_client.rb', line 69

def list_disk(*args)
  send_message(:list_disk, *args)
end

#migrate_disk(*args) ⇒ Object



105
106
107
# File 'lib/bosh/director/agent_client.rb', line 105

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

#mount_disk(*args) ⇒ Object



109
110
111
# File 'lib/bosh/director/agent_client.rb', line 109

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

#prepare(*args) ⇒ Object



85
86
87
# File 'lib/bosh/director/agent_client.rb', line 85

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

#prepare_configure_networks(*args) ⇒ Object



73
74
75
# File 'lib/bosh/director/agent_client.rb', line 73

def prepare_configure_networks(*args)
  send_message(:prepare_configure_networks, *args)
end

#prepare_network_change(*args) ⇒ Object



77
78
79
# File 'lib/bosh/director/agent_client.rb', line 77

def prepare_network_change(*args)
  send_message(:prepare_network_change, *args)
end

#run_errand(*args) ⇒ Object



145
146
147
# File 'lib/bosh/director/agent_client.rb', line 145

def run_errand(*args)
  start_task(:run_errand, *args)
end

#run_script(script_name, options) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/bosh/director/agent_client.rb', line 129

def run_script(script_name, options)
  begin
    send_message(:run_script, script_name, options)
  rescue RpcRemoteException => e
    if e.message =~ /unknown message/
      @logger.warn("Ignoring run_script 'unknown message' error from the agent: #{e.inspect}. Received while trying to run: #{script_name}")
    else
      raise
    end
  end
end

#start(*args) ⇒ Object



81
82
83
# File 'lib/bosh/director/agent_client.rb', line 81

def start(*args)
  send_message(:start, *args)
end

#stop(*args) ⇒ Object



141
142
143
# File 'lib/bosh/director/agent_client.rb', line 141

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

#unmount_disk(*args) ⇒ Object



113
114
115
# File 'lib/bosh/director/agent_client.rb', line 113

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

#update_settings(certs) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/bosh/director/agent_client.rb', line 117

def update_settings(certs)
  begin
    send_message(:update_settings, {"trusted_certs" => certs})
  rescue RpcRemoteException => e
    if e.message =~ /unknown message/
      @logger.warn("Ignoring update_settings 'unknown message' error from the agent: #{e.inspect}")
    else
      raise
    end
  end
end

#wait_for_task(agent_task_id, &blk) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/bosh/director/agent_client.rb', line 149

def wait_for_task(agent_task_id, &blk)
  task = get_task_status(agent_task_id)

  while task['state'] == 'running'
    blk.call if block_given?
    sleep(DEFAULT_POLL_INTERVAL)
    task = get_task_status(agent_task_id)
  end

  task['value']
end

#wait_until_ready(deadline = 600) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/bosh/director/agent_client.rb', line 165

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