Class: Hawkular::Operations::Client

Inherits:
BaseClient show all
Includes:
MonitorMixin, WebSocket::Client
Defined in:
lib/hawkular/operations/operations_api.rb

Overview

Client class to interact with the agent via websockets

Constant Summary

Constants inherited from BaseClient

BaseClient::HawkularConnectionException, BaseClient::HawkularException

Instance Attribute Summary collapse

Attributes inherited from BaseClient

#tenants

Instance Method Summary collapse

Methods inherited from BaseClient

#admin_header, #base_64_credentials, #generate_query_params, #http_delete, #http_get, #http_post, #http_put, #normalize_entrypoint_url, #now, #url, #url_with_websocket_scheme

Methods included from ClientUtils

#hawk_escape, #hawk_escape_id

Constructor Details

#initialize(args) ⇒ Client

Initialize new Client

There are two ways of passing in the target host/port: via :host and via :entrypoint. If both are given, then :entrypoint will be used.

Examples:

Hawkular::Operations::Client.new(credentials: {username: 'jdoe', password: 'password'})

Parameters:

  • args (Hash)

    Arguments for client.

Options Hash (args):

  • :entrypoint (String)

    Base URL of the hawkular server e.g. localhost:8080.

  • :host (String)

    base host:port pair of Hawkular - e.g localhost:8080

  • :use_secure_connection (Boolean)

    if no entrypoint is provided, determines if use a secure connection defaults to false

  • :credentials (Hash{String=>String})

    Hash of (username password) or token

  • :options (Hash{String=>String})

    Additional rest client options

  • :wait_time (Fixnum)

    Time in seconds describing how long the constructor should block - handshake



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hawkular/operations/operations_api.rb', line 86

def initialize(args)
  args = {
    credentials: {},
    options: {},
    wait_time: 0.5,
    use_secure_connection: false,
    entrypoint: nil
  }.merge(args)

  if args[:entrypoint]
    uri = URI.parse(args[:entrypoint].to_s)
    args[:host] = "#{uri.host}:#{uri.port}"
    args[:use_secure_connection] = %w[https wss].include?(uri.scheme) ? true : false
  end

  fail Hawkular::ArgumentError, 'no parameter ":host" or ":entrypoint" given' if args[:host].nil?

  super(args[:host], args[:credentials], args[:options])

  @logger = Hawkular::Logger.new

  @url = "ws#{args[:use_secure_connection] ? 's' : ''}://#{args[:host]}/hawkular/command-gateway/ui/ws"
  @credentials = args[:credentials]
  @tenant = args[:options][:tenant]
  @wait_time = args[:wait_time]
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



31
32
33
# File 'lib/hawkular/operations/operations_api.rb', line 31

def logger
  @logger
end

#wsObject

Returns the value of attribute ws.



31
32
33
# File 'lib/hawkular/operations/operations_api.rb', line 31

def ws
  @ws
end

Instance Method Details

#add_datasource(hash, &callback) ⇒ Object

Adds a new datasource

Parameters:

  • hash (Hash)

    Arguments for the datasource

  • callback (Block)

    callback that is run after the operation is done

Options Hash (hash):

  • :resourceId (String)

    ID of the WildFly server into which we add datasource

  • :feedId (String)

    ID of the feed containing the WildFly server

  • :xaDatasource (String)

    XA DS or normal

  • :datasourceName (String)

    name of the datasource

  • :jndiName (String)

    JNDI name

  • :driverName (String)

    this is internal name of the driver in Hawkular

  • :driverClass (String)

    class of driver

  • :connectionUrl (String)

    jdbc connection string

  • :datasourceProperties (String)

    optional properties

  • :username (String)

    username to DB

  • :password (String)

    password to DB



294
295
296
297
298
299
300
# File 'lib/hawkular/operations/operations_api.rb', line 294

def add_datasource(hash, &callback)
  required = %i[resourceId feedId xaDatasource datasourceName jndiName
                driverName driverClass connectionUrl]
  check_pre_conditions hash, required, &callback

  invoke_specific_operation(hash, 'AddDatasource', &callback)
end

#add_deployment(hash, &callback) ⇒ Object

Deploys an archive file into WildFly

Parameters:

  • hash (Hash)

    Arguments for deployment

  • callback (Block)

    callback that is run after the operation is done

Options Hash (hash):

  • :resource_id (String)

    ID of the WildFly server into which we deploy or of the domain controller if we deploy into a server group (in case of domain mode)

  • :feed_id (String)

    feed containing this resource

  • :destination_file_name (String)

    resulting file name

  • :binary_content (String)

    binary content representing the war file

  • :enabled (String)

    whether the deployment should be enabled immediately, or not (default = true)

  • :force_deploy (String)

    whether to replace existing content or not (default = false)

  • :server_groups (String)

    comma-separated list of server groups for the operation (default = ignored)



190
191
192
193
194
195
196
197
198
# File 'lib/hawkular/operations/operations_api.rb', line 190

def add_deployment(hash, &callback)
  hash[:enabled] = hash.key?(:enabled) ? hash[:enabled] : true
  hash[:force_deploy] = hash.key?(:force_deploy) ? hash[:force_deploy] : false
  required = %i[resource_id feed_id destination_file_name binary_content]
  check_pre_conditions hash, required, &callback

  operation_payload = prepare_payload_hash([:binary_content], hash)
  invoke_operation_helper(operation_payload, 'DeployApplication', hash[:binary_content], &callback)
end

#add_jdbc_driver(hash, &callback) ⇒ Object

Adds a new datasource

Parameters:

  • hash (Hash)

    Arguments for the datasource

  • callback (Block)

    callback that is run after the operation is done

Options Hash (hash):

  • :resource_id (String)

    ID of the WildFly server into which we add driver

  • :feed_id (String)

    ID of the feed containing the WildFly server

  • :driver_jar_name (String)

    name of the jar file

  • :driver_name (String)

    name of the jdbc driver (when adding datasource, this is the driverName)

  • :module_name (String)

    name of the JBoss module into which the driver will be installed - ‘foo.bar’

  • :driver_class (String)

    fully specified java class of the driver - e.q. ‘com.mysql.jdbc.Driver’

  • :binary_content (String)

    driver jar file bits



314
315
316
317
318
319
320
321
# File 'lib/hawkular/operations/operations_api.rb', line 314

def add_jdbc_driver(hash, &callback)
  required = %i[resource_id feed_id driver_jar_name driver_name module_name
                driver_class binary_content]
  check_pre_conditions hash, required, &callback

  operation_payload = prepare_payload_hash([:binary_content], hash)
  invoke_operation_helper(operation_payload, 'AddJdbcDriver', hash[:binary_content], &callback)
end

#base64_credentialsObject



113
114
115
# File 'lib/hawkular/operations/operations_api.rb', line 113

def base64_credentials
  ["#{@credentials[:username]}:#{@credentials[:password]}"].pack('m').delete("\r\n")
end

#close_connection!Object

Closes the WebSocket connection



145
146
147
# File 'lib/hawkular/operations/operations_api.rb', line 145

def close_connection!
  @ws && @ws.close
end

#connectObject



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
# File 'lib/hawkular/operations/operations_api.rb', line 117

def connect
  return if @connecting || (@ws && @ws.open?)

  @connecting = true

  ws_options = {
    headers:  {
      'Authorization' => 'Basic ' + base64_credentials,
      'Hawkular-Tenant' => @tenant,
      'Accept' => 'application/json'
    }
  }

  @ws = Simple.connect @url, ws_options do |client|
    client.on(:message, once: true) do |msg|
      parsed_message = msg.data.to_msg_hash

      logger = Hawkular::Logger.new
      logger.log("Sent WebSocket message: #{parsed_message}")
    end
  end

  Timeout.timeout(@wait_time) { sleep 0.1 until @ws.open? }
ensure
  @connecting = false
end

#disable_deployment(hash, &callback) ⇒ Object

Disable a WildFly deployment

Parameters:

  • hash (Hash)

    Arguments for disable deployment

  • callback (Block)

    callback that is run after the operation is done

Options Hash (hash):

  • :resource_id (String)

    ID of the WildFly server from which to disable the deployment

  • :feed_id (String)

    feed containing this resource

  • :deployment_name (String)

    name of deployment to disable

  • :server_groups (String)

    comma-separated list of server groups for the operation (default = ignored)



249
250
251
252
253
254
255
256
257
# File 'lib/hawkular/operations/operations_api.rb', line 249

def disable_deployment(hash, &callback)
  required = %i[resource_id feed_id deployment_name]
  check_pre_conditions hash, required, &callback

  hash[:destination_file_name] = hash[:deployment_name]

  operation_payload = prepare_payload_hash([:deployment_name], hash)
  invoke_operation_helper(operation_payload, 'DisableApplication', &callback)
end

#enable_deployment(hash, &callback) ⇒ Object

Enable a WildFly deployment

Parameters:

  • hash (Hash)

    Arguments for enable deployment

  • callback (Block)

    callback that is run after the operation is done

Options Hash (hash):

  • :resource_id (String)

    ID of the WildFly server from which to enable the deployment

  • :feed_id (String)

    feed containing this resource

  • :deployment_name (String)

    name of deployment to enable

  • :server_groups (String)

    comma-separated list of server groups for the operation (default = ignored)



230
231
232
233
234
235
236
237
238
# File 'lib/hawkular/operations/operations_api.rb', line 230

def enable_deployment(hash, &callback)
  required = %i[resource_id feed_id deployment_name]
  check_pre_conditions hash, required, &callback

  hash[:destination_file_name] = hash[:deployment_name]

  operation_payload = prepare_payload_hash([:deployment_name], hash)
  invoke_operation_helper(operation_payload, 'EnableApplication', &callback)
end

#export_jdr(resource_id, feed_id, delete_immediately = false, sender_request_id = nil, &callback) ⇒ Object

Exports the JDR report

server should be deleted. False, by default.

Parameters:

  • resource_id (String)

    ID of the WildFly server

  • feed_id (String)

    ID of the feed containing the WildFly server

  • delete_immediately (Boolean) (defaults to: false)

    specifies whether the temporary file at the remote

  • callback (Block)

    callback that is run after the operation is done



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/hawkular/operations/operations_api.rb', line 330

def export_jdr(resource_id, feed_id, delete_immediately = false, sender_request_id = nil, &callback)
  fail Hawkular::ArgumentError, 'resource_id must be specified' if resource_id.nil?
  fail Hawkular::ArgumentError, 'feed_id must be specified' if feed_id.nil?
  check_pre_conditions(&callback)

  invoke_specific_operation({ resourceId: resource_id,
                              feedId: feed_id,
                              deleteImmediately: delete_immediately,
                              senderRequestId: sender_request_id },
                            'ExportJdr', &callback)
end

#invoke_generic_operation(hash, &callback) ⇒ Object

Invokes a generic operation on the WildFly agent (the operation name must be specified in the hash) Note: if success and failure callbacks are omitted, the client will not wait for the Response message which the operation is about to run, feedId [String], operationName [String]

Parameters:

  • hash (Hash{String=>Object})

    a hash containing: resourceId [String] denoting the resource on

  • callback (Block)

    callback that is run after the operation is done



155
156
157
158
159
160
# File 'lib/hawkular/operations/operations_api.rb', line 155

def invoke_generic_operation(hash, &callback)
  required = %i[resourceId feedId operationName]
  check_pre_conditions hash, required, &callback

  invoke_operation_helper(hash, &callback)
end

#invoke_specific_operation(operation_payload, operation_name, &callback) ⇒ Object

Invokes operation on the WildFly agent that has it’s own message type the resource on which the operation is about to run, feedId [String] found here git.io/v2h1a (Use only the first part of the name without the Request/Response suffix), e.g. RemoveDatasource (and not RemoveDatasourceRequest)

Parameters:

  • operation_payload (Hash{String=>Object})

    a hash containing: resourceId [String] denoting

  • operation_name (String)

    the name of the operation. This must correspond with the message type, they can be

  • callback (Block)

    callback that is run after the operation is done



169
170
171
172
173
174
175
# File 'lib/hawkular/operations/operations_api.rb', line 169

def invoke_specific_operation(operation_payload, operation_name, &callback)
  fail Hawkular::ArgumentError, 'Operation must be specified' if operation_name.nil?
  required = %i[resourceId feedId]
  check_pre_conditions operation_payload, required, &callback

  invoke_operation_helper(operation_payload, operation_name, &callback)
end

#restart_deployment(hash, &callback) ⇒ Object

Restart a WildFly deployment

Parameters:

  • hash (Hash)

    Arguments for restart deployment

  • callback (Block)

    callback that is run after the operation is done

Options Hash (hash):

  • :resource_id (String)

    ID of the WildFly server from which to restart the deployment

  • :feed_id (String)

    feed containing this resource

  • :deployment_name (String)

    name of deployment to restart

  • :server_groups (String)

    comma-separated list of server groups for the operation (default = ignored)



268
269
270
271
272
273
274
275
276
# File 'lib/hawkular/operations/operations_api.rb', line 268

def restart_deployment(hash, &callback)
  required = %i[resource_id feed_id deployment_name]
  check_pre_conditions hash, required, &callback

  hash[:destination_file_name] = hash[:deployment_name]

  operation_payload = prepare_payload_hash([:deployment_name], hash)
  invoke_operation_helper(operation_payload, 'RestartApplication', &callback)
end

#undeploy(hash, &callback) ⇒ Object

Undeploy a WildFly deployment

Parameters:

  • hash (Hash)

    Arguments for deployment removal

  • callback (Block)

    callback that is run after the operation is done

Options Hash (hash):

  • :resource_id (String)

    ID of the WildFly server from which to undeploy the deployment

  • :feed_id (String)

    feed containing this resource

  • :deployment_name (String)

    name of deployment to undeploy

  • :remove_content (String)

    whether to remove the deployment content or not (default = true)

  • :server_groups (String)

    comma-separated list of server groups for the operation (default = ignored)



210
211
212
213
214
215
216
217
218
219
# File 'lib/hawkular/operations/operations_api.rb', line 210

def undeploy(hash, &callback)
  hash[:remove_content] = hash.key?(:remove_content) ? hash[:remove_content] : true
  required = %i[resource_id feed_id deployment_name]
  check_pre_conditions hash, required, &callback

  hash[:destination_file_name] = hash[:deployment_name]

  operation_payload = prepare_payload_hash([:deployment_name], hash)
  invoke_operation_helper(operation_payload, 'UndeployApplication', &callback)
end

#update_collection_intervals(hash, &callback) ⇒ Object

Updates the collection intervals.

MetricTypeId must be of form MetricTypeSet~MetricTypeName AvailTypeId must be of form AvailTypeSet~AvailTypeName

Parameters:

  • hash (Hash)

    Arguments for update collection intervals

  • callback (Block)

    callback that is run after the operation is done

Options Hash (hash):

  • a (resourceId)

    resource managed by the target agent

  • the (feedId)

    related feed ID

  • A (metricTypes)

    map with key=MetricTypeId, value=interval (seconds).

  • A (availTypes)

    map with key=AvailTypeId, value=interval (seconds).



353
354
355
356
357
# File 'lib/hawkular/operations/operations_api.rb', line 353

def update_collection_intervals(hash, &callback)
  required = %i[resourceId feedId metricTypes availTypes]
  check_pre_conditions hash, required, &callback
  invoke_specific_operation(hash, 'UpdateCollectionIntervals', &callback)
end