Class: Hawkular::Operations::Client

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

Overview

Client class to interact with the agent via websockets

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_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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 63

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 'no parameter ":host" or ":entrypoint" given' if args[:host].nil?

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

  url = "ws#{args[:use_secure_connection] ? 's' : ''}://#{args[:host]}/hawkular/command-gateway/ui/ws"

  creds = args[:credentials]
  base64_creds = ["#{creds[:username]}:#{creds[:password]}"].pack('m').delete("\r\n")

  ws_options = {
    headers:  {
      'Authorization' => 'Basic ' + base64_creds,
      'Hawkular-Tenant' => args[:options][:tenant],
      'Accept' => 'application/json'
    }
  }

  @logger = Hawkular::Logger.new

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

      logger.log("Sent WebSocket message: #{parsed_message}")

      case parsed_message[:operationName]
      when 'WelcomeResponse'
        @session_id = parsed_message[:data]['sessionId']
      end
    end
  end

  sleep args[:wait_time]
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger



34
35
36
# File 'lib/hawkular/operations/operations_api.rb', line 34

def logger
  @logger
end

#session_idObject

Returns the value of attribute session_id



34
35
36
# File 'lib/hawkular/operations/operations_api.rb', line 34

def session_id
  @session_id
end

#wsObject

Returns the value of attribute ws



34
35
36
# File 'lib/hawkular/operations/operations_api.rb', line 34

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):

  • :resourcePath (String)

    canonical path of the WildFly server into which we add datasource

  • :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



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

def add_datasource(hash, &callback)
  required = [:resourcePath, :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_path (String)

    canonical path 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)

  • :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)



158
159
160
161
162
163
164
165
166
# File 'lib/hawkular/operations/operations_api.rb', line 158

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 = [:resource_path, :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_path (String)

    canonical path of the WildFly server into which we add driver

  • :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



287
288
289
290
291
292
293
# File 'lib/hawkular/operations/operations_api.rb', line 287

def add_jdbc_driver(hash, &callback)
  required = [:resource_path, :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

#close_connection!Object

Closes the WebSocket connection



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

def close_connection!
  @ws.close
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_path (String)

    canonical path of the WildFly server from which to disable the deployment

  • :deployment_name (String)

    name of deployment to disable

  • :server_groups (String)

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



220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/hawkular/operations/operations_api.rb', line 220

def disable_deployment(hash, &callback)
  required = [:resource_path, :deployment_name]
  check_pre_conditions hash, required, &callback

  cp = ::Hawkular::Inventory::CanonicalPath.parse hash[:resource_path]
  server_path = cp.up.to_s
  hash[:resource_path] = server_path
  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_path (String)

    canonical path of the WildFly server from which to enable the deployment

  • :deployment_name (String)

    name of deployment to enable

  • :server_groups (String)

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



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/hawkular/operations/operations_api.rb', line 199

def enable_deployment(hash, &callback)
  required = [:resource_path, :deployment_name]
  check_pre_conditions hash, required, &callback

  cp = ::Hawkular::Inventory::CanonicalPath.parse hash[:resource_path]
  server_path = cp.up.to_s
  hash[:resource_path] = server_path
  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_path, &callback) ⇒ Object

Exports the JDR report

Parameters:

  • resource_path (String)

    canonical path of the WildFly server

  • callback (Block)

    callback that is run after the operation is done



299
300
301
302
303
304
# File 'lib/hawkular/operations/operations_api.rb', line 299

def export_jdr(resource_path, &callback)
  fail 'resource_path must be specified' if resource_path.nil?
  check_pre_conditions(&callback)

  invoke_specific_operation({ resourcePath: resource_path }, '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, operationName [String]

Parameters:

  • hash (Hash{String=>Object})

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

  • callback (Block)

    callback that is run after the operation is done



124
125
126
127
128
129
# File 'lib/hawkular/operations/operations_api.rb', line 124

def invoke_generic_operation(hash, &callback)
  required = [:resourcePath, :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 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: resourcePath [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



138
139
140
141
142
143
144
# File 'lib/hawkular/operations/operations_api.rb', line 138

def invoke_specific_operation(operation_payload, operation_name, &callback)
  fail 'Operation must be specified' if operation_name.nil?
  required = [:resourcePath]
  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_path (String)

    canonical path of the WildFly server from which to restart the deployment

  • :deployment_name (String)

    name of deployment to restart

  • :server_groups (String)

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



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/hawkular/operations/operations_api.rb', line 241

def restart_deployment(hash, &callback)
  required = [:resource_path, :deployment_name]
  check_pre_conditions hash, required, &callback

  cp = ::Hawkular::Inventory::CanonicalPath.parse hash[:resource_path]
  server_path = cp.up.to_s
  hash[:resource_path] = server_path
  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_path (String)

    canonical path of the WildFly server from which to undeploy the deployment

  • :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)



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/hawkular/operations/operations_api.rb', line 177

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

  cp = ::Hawkular::Inventory::CanonicalPath.parse hash[:resource_path]
  server_path = cp.up.to_s
  hash[:resource_path] = server_path
  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 (resourcePath)

    resource managed by the target agent

  • A (metricTypes)

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

  • A (availTypes)

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



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

def update_collection_intervals(hash, &callback)
  required = [:resourcePath, :metricTypes, :availTypes]
  check_pre_conditions hash, required, &callback
  invoke_specific_operation(hash, 'UpdateCollectionIntervals', &callback)
end