class Envoi::Aspera::WatchService::Client

Constants

DAEMONS_QUERY_LINE_PART_TO_SUB
DAEMONS_QUERY_LINE_START
DAEMON_CONFIGURATION_FIELD_NAME
DAEMON_NAME_FIELD_NAME
DEFAULT_ASWATCHADMIN_EXECUTABLE_PATH

Attributes

aswatchadmin_executable_path[RW]
config[RW]
default_username[RW]
initial_args[RW]
logger[RW]

Public Class Methods

current_os_type() click to toggle source
# 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_executable() click to toggle source
# 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
new(args = {}) click to toggle source
# 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

Public Instance Methods

daemons_query(args = {}) click to toggle source

@return [Array]

# 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) click to toggle source

@return [Array]

# 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_get() click to toggle source

@return [Hash]

# 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_get() click to toggle source

@return [String|nil]

# 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?() click to toggle source
# File lib/envoi/aspera/watch_service/client.rb, line 109
def dry_run?;
  @dry_run
end
initialize_logger(args = {}) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source

@param [Object] response @return [Object]

# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source

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.

“`

# 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) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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 = { }) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source

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'.

@see download.asperasoft.com/download/docs/entsrv/3.8.1/cs_admin_osx/webhelp/index.html#dita-watchfolder/watchd_subscriptions.html

# 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 = {}) click to toggle source

List subscriptions for a specific daemon @see download.asperasoft.com/download/docs/entsrv/3.8.1/cs_admin_osx/webhelp/index.html#dita-watchfolder/watchd_subscriptions.html

# 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) click to toggle source
# 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 = {}) click to toggle source
# 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