Class: Envoi::Aspera::WatchService::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/envoi/aspera/watch_service/client.rb

Defined Under Namespace

Classes: Expressions, SnapshotEntry

Constant Summary collapse

DEFAULT_ASWATCHADMIN_EXECUTABLE_PATH =
'/Library/Aspera/bin/d'
DAEMON_NAME_FIELD_NAME =
'name'
DAEMON_CONFIGURATION_FIELD_NAME =
'configuration'
DAEMONS_QUERY_LINE_PART_TO_SUB =
'configuration: '
DAEMONS_QUERY_LINE_START =
"#{DAEMONS_QUERY_LINE_PART_TO_SUB} {"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/envoi/aspera/watch_service/client.rb', line 92

def initialize(args = {})
  @initial_args = args.clone
  initialize_logger(args)

  @config = args[:config]

  @aswatchadmin_executable_path = args[:aswatchadmin_executable_path] ||
                                  self.class.detect_executable ||
                                  DEFAULT_ASWATCHADMIN_EXECUTABLE_PATH
  @default_daemon_name          = args[:default_daemon_name] || args[:daemon_name] || default_daemon_name_get
  @dry_run                      = args.fetch(:dry_run, false)
end

Instance Attribute Details

#aswatchadmin_executable_pathObject

Returns the value of attribute aswatchadmin_executable_path.



14
15
16
# File 'lib/envoi/aspera/watch_service/client.rb', line 14

def aswatchadmin_executable_path
  @aswatchadmin_executable_path
end

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/envoi/aspera/watch_service/client.rb', line 12

def config
  @config
end

#default_usernameObject

Returns the value of attribute default_username.



14
15
16
# File 'lib/envoi/aspera/watch_service/client.rb', line 14

def default_username
  @default_username
end

#initial_argsObject

Returns the value of attribute initial_args.



12
13
14
# File 'lib/envoi/aspera/watch_service/client.rb', line 12

def initial_args
  @initial_args
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/envoi/aspera/watch_service/client.rb', line 12

def logger
  @logger
end

Class Method Details

.current_os_typeObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/envoi/aspera/watch_service/client.rb', line 61

def self.current_os_type
  case RUBY_PLATFORM
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
    return :windows
  when /darwin|mac os/
    return :mac
  else  # unix family
    return :unix
  end
end

.detect_executableObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/envoi/aspera/watch_service/client.rb', line 72

def self.detect_executable
  executable_name = 'aswatchadmin'
  paths = case current_os_type
  when :windows
    executable_name += '.exe'
    [
      File.join(ENV['LOCALAPPDATA'], 'Aspera', 'bin', executable_name)
    ]
  when :mac
    [
      "/Library/Aspera/bin/#{executable_name}",
    ]
  else
    [
      File.join("/opt/aspera/bin/#{executable_name}")
    ]
  end
  paths.find { |p| File.executable?(p) }
end

Instance Method Details

#daemons_query(args = {}) ⇒ Array

Returns:

  • (Array)


158
159
160
161
162
163
164
165
# File 'lib/envoi/aspera/watch_service/client.rb', line 158

def daemons_query(args = {})
  command  = [aswatchadmin_executable_path, 'query-daemons']
  response = shell_execute(command)

  return response unless args.fetch(:parse_response, true)

  daemons_query_response_parse(response)
end

#daemons_query_response_parse(response) ⇒ Array[Hash]

Returns:

  • (Array[Hash])


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/envoi/aspera/watch_service/client.rb', line 168

def daemons_query_response_parse(response)
  response_ary = response.split("\n")
  first_line   = response_ary.shift
  daemon       = nil
  daemons      = response_ary.each_with_object([]) do |line, _daemons|
    _line = line.strip

    if _line.start_with?(DAEMONS_QUERY_LINE_START)
      configuration_json                      = _line.sub(DAEMONS_QUERY_LINE_PART_TO_SUB, '')
      configuration                           = JSON.parse(configuration_json)
      daemon[DAEMON_CONFIGURATION_FIELD_NAME] = configuration
    else
      _daemons << daemon if daemon
      daemon_name = _line
      daemon      = { DAEMON_NAME_FIELD_NAME => daemon_name }
    end
  end
  daemons << daemon if daemon
end

#default_daemon_getHash

Returns:

  • (Hash)


144
145
146
147
148
# File 'lib/envoi/aspera/watch_service/client.rb', line 144

def default_daemon_get
  daemons = daemons_query
  return nil unless daemons.length == 1
  default_daemon = daemons.first
end

#default_daemon_name_getString|nil

Returns:

  • (String|nil)


151
152
153
154
155
# File 'lib/envoi/aspera/watch_service/client.rb', line 151

def default_daemon_name_get
  _default_daemon = default_daemon_get
  return nil unless _default_daemon
  _default_daemon[DAEMON_NAME_FIELD_NAME]
end

#dry_run?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/envoi/aspera/watch_service/client.rb', line 109

def dry_run?;
  @dry_run
end

#initialize_logger(args = {}) ⇒ Object



105
106
107
# File 'lib/envoi/aspera/watch_service/client.rb', line 105

def initialize_logger(args = {})
  @logger = args[:logger] || Logger.new(STDOUT)
end

#shell_execute(command, dry_run = @dry_run) ⇒ Object



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
# File 'lib/envoi/aspera/watch_service/client.rb', line 113

def shell_execute(command, dry_run = @dry_run)
  command = command.shelljoin if command.is_a?(Array)
  if dry_run
    logger.debug { "Skipping Execution of Command: '#{command}' " }
    return
  end
  logger.debug { "Executing Command: '#{command}'" }

  response = ''
  Open3.popen3(command) do |stdin, stdout, stderr, thread|
    # stdin.sync = true
    # stdout.sync = true
    # stderr.sync = true
    output = ''
    loop do
      output << stdout.read #rescue nil
      output << stderr.read # rescue nil
      unless output.empty?
        # print output
        response << output.dup
        output.clear
      end
      break if thread.stop?
    end
  end

  logger.debug { "RESPONSE: #{response.empty? ? '' : "\n#{response}"}" }
  response
end

#subscription_create(args = {}) ⇒ Object

Raises:

  • (ArgumentError)


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/envoi/aspera/watch_service/client.rb', line 188

def subscription_create(args = {})
  daemon_name = args[:daemon_name] || args[:daemon] || @default_daemon_name
  raise ArgumentError, 'Missing argument for required parameter daemon_name' unless daemon_name

  watch_folder_path = args[:watch_folder_path]
  raise ArgumentError, 'Missing argument for required parameter watch_folder_path' unless watch_folder_path

  scan_period = args[:scan_period]
  expire_in   = args[:expire_in]

  command = [aswatchadmin_executable_path, 'subscribe', daemon_name, watch_folder_path]
  command << '--scan-period' << scan_period if scan_period
  command << '--expire_in' << expire_in if expire_in
  response = shell_execute(command)

  return response unless args.fetch(:parse_response, true)

  subscription_create_response_parse(response)
end

#subscription_create_response_parse(response) ⇒ Object

Parameters:

  • response (Object)

Returns:

  • (Object)


210
211
212
213
214
215
# File 'lib/envoi/aspera/watch_service/client.rb', line 210

def subscription_create_response_parse(response)
  subscription_json_match = Expressions::SUBSCRIPTION_CREATE_PARSE.match(response)
  raise "Failed to parse new subscription information from response. '#{response}'" unless subscription_json_match
  subscription_json = subscription_json_match.to_s
  subscription      = JSON.parse(subscription_json)
end

#subscription_find_for_path(args = {}) ⇒ Object



387
388
389
390
391
# File 'lib/envoi/aspera/watch_service/client.rb', line 387

def subscription_find_for_path(args = {})
  path          = args[:path]
  subscriptions = args[:subscriptions] || subscriptions_get(args)
  subscriptions.keep_if { |s| s['path'] =~ /(?:\w|-)*:\/{2,3}#{Regexp.escape(path)}/ }
end

#subscription_resubscribe(args = {}) ⇒ Object

Raises:

  • (ArgumentError)


217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/envoi/aspera/watch_service/client.rb', line 217

def subscription_resubscribe(args = {})
  daemon_name = args[:daemon_name] || args[:daemon] || @default_daemon_name
  raise ArgumentError, 'Missing argument for required parameter daemon_name' unless daemon_name

  subscription_id = args[:subscription_id]
  raise ArgumentError, 'Missing argument for required parameter subscription_id' unless subscription_id

  command  = [aswatchadmin_executable_path, 'resubscribe', daemon_name, subscription_id]
  response = shell_execute(command)

  return response unless args.fetch(:parse_response, true)

  subscription_resubscribe_parse_response(response)
end

#subscription_resubscribe_parse_response(response) ⇒ Object



232
233
234
235
# File 'lib/envoi/aspera/watch_service/client.rb', line 232

def subscription_resubscribe_parse_response(response)
  raise response unless Expressions::SUBSCRIPTION_RESUBSCRIBE_SUCCESS_PARSE.match(response)
  true
end

#subscription_snapshot_create(args = {}) ⇒ Object

Create a snapshot using the subscription

“‘ $ /Library/Aspera/bin/aswatchadmin create-snapshot jw f090e8b5-d9e2-474b-acb1-68c4f76c8c53

aswatchadmin create-snapshot

Successfully created snapshot 0.

“‘

Raises:

  • (ArgumentError)


243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/envoi/aspera/watch_service/client.rb', line 243

def subscription_snapshot_create(args = {})
  daemon_name = args[:daemon_name] || args[:daemon] || @default_daemon_name
  raise ArgumentError, 'Missing argument for required parameter daemon_name' unless daemon_name

  subscription_id = args[:subscription_id]
  raise ArgumentError, 'Missing argument for required parameter subscription_id' unless subscription_id

  command  = [aswatchadmin_executable_path, 'create-snapshot', daemon_name, subscription_id]
  response = shell_execute(command)

  return response unless args.fetch(:parse_response, true)

  subscription_snapshot_create_response_parse(response)
end

#subscription_snapshot_create_response_parse(response) ⇒ Object



258
259
260
261
262
# File 'lib/envoi/aspera/watch_service/client.rb', line 258

def subscription_snapshot_create_response_parse(response)
  snapshot_version = Expressions::SNAPSHOT_CREATE_VERSION_PARSE.match(response)
  raise "Failed to parse snapshot version from response. '#{response}'" unless snapshot_version
  snapshot_version
end

#subscription_snapshot_differential(args = {}) ⇒ Object

Raises:

  • (ArgumentError)


264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/envoi/aspera/watch_service/client.rb', line 264

def subscription_snapshot_differential(args = {})
  daemon_name = args[:daemon_name] || args[:daemon] || @default_daemon_name
  raise ArgumentError, 'Missing argument for required parameter daemon_name' unless daemon_name

  subscription_id = args[:subscription_id]
  raise ArgumentError, 'Missing argument for required parameter subscription_id' unless subscription_id

  from = args[:from]

  to = args[:to]

  path = args[:path]
  identifier = args[:identifier]
  format = args[:format]
  escape = args[:escape]
  exclude_create = args[:exclude_create]
  exclude_modify = args[:exclude_modify]
  exclude_remove = args[:exclude_remove]
  leafs_only = args[:leafs_only]

  command  = [aswatchadmin_executable_path, 'snapshot-differential', daemon_name, subscription_id]
  command << from if from
  command << to if to
  command << '--path' << path if path && !path.empty?
  command << '--identifier' << identifier if identifier && !identifier.empty?
  command << '--format' << format if format && !format.empty?
  command << '--escape' << escape if escape && !escape.empty?
  command << '--exclude_create' if exclude_create
  command << '--exclude_modify' if exclude_modify
  command << '--exclude-remove' if exclude_remove
  command << '--leafs-only' if leafs_only
  response = shell_execute(command)

  return response unless args.fetch(:parse_response, true)

  subscription_snapshot_differential_response_parse(response)
end

#subscription_snapshot_differential_response_parse(response) ⇒ Object



302
303
304
305
306
307
# File 'lib/envoi/aspera/watch_service/client.rb', line 302

def subscription_snapshot_differential_response_parse(response)
  response.split.map do |v|
    m = v.match(Expressions::SNAPSHOT_DIFFERENTIAL_PARSE)
    m ? Hash[m.names.zip(m.captures)] : v
  end.join('\n')
end

#subscription_snapshot_entries_get(args = { }) ⇒ Object



333
334
335
336
# File 'lib/envoi/aspera/watch_service/client.rb', line 333

def subscription_snapshot_entries_get(args = { })
  response = subscription_snapshot_print(args)
  subscription_snapshot_print_response_parse(response)
end

#subscription_snapshot_print(args = {}) ⇒ Object

Raises:

  • (ArgumentError)


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/envoi/aspera/watch_service/client.rb', line 309

def subscription_snapshot_print(args = {})
  daemon_name = args[:daemon_name] || args[:daemon] || @default_daemon_name
  raise ArgumentError, 'Missing argument for required parameter daemon_name' unless daemon_name

  subscription_id = args[:subscription_id]
  raise ArgumentError, 'Missing argument for required parameter subscription_id' unless subscription_id

  snapshot_version = args[:snapshot_version]
  command          = [aswatchadmin_executable_path, 'print-snapshot', daemon_name, subscription_id]
  command << '--snapshot' << snapshot_version if snapshot_version

  response = shell_execute(command)

  return response unless args.fetch(:parse_response, false)

  subscription_snapshot_print_response_parse(response)
end

#subscription_snapshot_print_response_parse(response) ⇒ Object



327
328
329
330
331
# File 'lib/envoi/aspera/watch_service/client.rb', line 327

def subscription_snapshot_print_response_parse(response)
  response
      .to_enum(:scan, Expressions::SNAPSHOT_PRINT_PARSE)
      .map { SnapshotEntry.new_from_match_data(Regexp.last_match) }
end

#subscription_unsubscribe(args = {}) ⇒ Object

Unsubscribe from a watch

$ /Library/Aspera/bin/aswatchadmin unsubscribe daemonname 3b758f2d-97f9-4054-80d5-ed67dcaa8daa

aswatchadmin unsubscribe

Successfully unsubscribed subscription ‘3b758f2d-97f9-4054-80d5-ed67dcaa8daa’ from daemon ‘daemonname’.



364
365
366
367
368
369
370
371
372
373
# File 'lib/envoi/aspera/watch_service/client.rb', line 364

def subscription_unsubscribe(args = {})
  daemon_name = args[:daemon_name] || args[:daemon] || @default_daemon_name
  raise ArgumentError, 'Missing argument for required parameter daemon_name' unless daemon_name

  subscription_id = args[:subscription_id]
  raise ArgumentError, 'Missing argument for required parameter subscription_id' unless subscription_id

  command  = [aswatchadmin_executable_path, 'unsubscribe', daemon_name, subscription_id]
  response = shell_execute(command)
end

#subscriptions_get(args = {}) ⇒ Object

List subscriptions for a specific daemon



341
342
343
344
345
346
347
348
349
350
351
# File 'lib/envoi/aspera/watch_service/client.rb', line 341

def subscriptions_get(args = {})
  daemon_name = args[:daemon_name] || args[:daemon] || @default_daemon_name
  raise ArgumentError, 'Missing argument for required parameter daemon_name' unless daemon_name

  command  = [aswatchadmin_executable_path, 'query-subscriptions', daemon_name]
  response = shell_execute(command)

  return response unless args.fetch(:parse_response, true)

  subscriptions_get_response_parse(response)
end

#subscriptions_get_response_parse(response) ⇒ Object



353
354
355
356
# File 'lib/envoi/aspera/watch_service/client.rb', line 353

def subscriptions_get_response_parse(response)
  response_json = "[#{response.split.join(",\n")}]"
  subscriptions = JSON.parse(response_json)
end

#subscriptions_unsubscribe_all(args = {}) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
# File 'lib/envoi/aspera/watch_service/client.rb', line 375

def subscriptions_unsubscribe_all(args = {})
  subscriptions = subscriptions_get(args)
  args_out      = args.dup
  path          = args[:path]
  subscriptions.each do |subscription|
    next unless subscription['path'] == path if path
    subscription_id            = subscription['identifier']
    args_out[:subscription_id] = subscription_id
    subscription_unsubscribe(args_out)
  end
end