Class: Aspera::Plugins::Server::Commands

Inherits:
Cli::Plugins::Base show all
Includes:
Cli::Plugins::BasicAuth, Xfer::AscpLogsActions, Xfer::SyncActions
Defined in:
lib/aspera/plugins/server/commands.rb

Overview

Operations on HSTS with SSH/FASP (ascmd/ascp)

Defined Under Namespace

Classes: LocalExecutor

Constant Summary collapse

LOCAL_SCHEME =
'local'
HTTPS_SCHEME =
'https'
SSH_OPTIONS =

Schema of option ssh_options, in the schemas of the bundle (manifest schemas)

'server_opts:components.schemas.SshOptions'

Constants included from Xfer::AscpLogsActions

Xfer::AscpLogsActions::OPTIONS_SCHEMA

Constants included from Xfer::SyncActions

Xfer::SyncActions::ADMIN_PATH_AND_INFO_ARGS, Xfer::SyncActions::PATH_AND_INFO_ARGS, Xfer::SyncActions::STATE_STR, Xfer::SyncActions::SYNC_LOG_ARGS

Constants inherited from Cli::Plugins::Base

Cli::Plugins::Base::FILTER_ARGS

Instance Attribute Summary

Attributes inherited from Cli::Plugins::Base

#context, #help_path

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Xfer::AscpLogsActions

#action_ascp_logs_fields, #action_ascp_logs_find, #action_ascp_logs_health, #action_ascp_logs_records, #action_ascp_logs_summary, included

Methods included from Xfer::SyncActions

#action_sync_admin_file_info, #action_sync_logs, #admin_sync_info, #async_info_from_args, #db_from_args, included, #readable_db_files, #run_sync_transfer

Methods included from Cli::Plugins::BasicAuth

#basic_auth_api, #basic_auth_params, included

Methods included from Cli::Option::Declarator

#declare_options, #option, #option_specs, #register_option_spec

Methods inherited from Cli::Plugins::Base

#action_for, application_name, #bulk_result, #check_argument_access, command, command_registry, commands_under, #config, #crud_api, crud_commands, declare_options, define_action_method, #dispatch_child, #dispatch_from_registry, #dispatch_leaf, #dispatch_mount, #entity_create, #entity_delete, #entity_list, #entity_modify, entity_noun, #entity_res_path, #entity_show, #execute_action, #execute_leaf, file_matcher, #formatter, #generate_help, #http_config, include_mounts, #invoke_action, operation_description, option_sources, option_specs, #options, #persistency, #poll_result, #presets, #progress_bar, #query_read_delete, register_option_spec, #resolve_argument, #resolve_arguments, #transfer, use_options, used_option_sources, #validate_registry

Constructor Details

#initialize(**_) ⇒ Commands

Returns a new instance of Commands.



97
98
99
100
101
102
103
104
105
# File 'lib/aspera/plugins/server/commands.rb', line 97

def initialize(**_)
  super
  @connection_type = :ssh
  @ascmd_executor = nil
  @server_transfer_spec = nil
  @ssh_opts = options.get_option(:ssh_options).symbolize_keys
  # After a failed authentication, Net::SSH asks for a password (or the passphrase of a key) in a terminal: only with option `interactive`
  @ssh_opts[:non_interactive] = !options.get_option(:interactive) unless @ssh_opts.key?(:non_interactive)
end

Class Method Details

.detect(address_or_url) ⇒ Hash, NilClass

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/aspera/plugins/server/commands.rb', line 53

def detect(address_or_url)
  urls = if address_or_url.match?(%r{^[a-z]{1,6}://})
    [address_or_url]
  else
    [
      "#{SSH_SCHEME}://#{address_or_url}:33001",
      "#{SSH_SCHEME}://#{address_or_url}:22"
    ]
    # wss not practical as it requires a token
  end
  error = nil
  urls.each do |base_url|
    server_uri = URI.parse(base_url)
    Log.log.debug { "URI=#{server_uri}, host=#{server_uri.hostname}, port=#{server_uri.port}, scheme=#{server_uri.scheme}" }
    next unless server_uri.scheme.eql?(SSH_SCHEME)
    socket = TCPSocket.new(server_uri.hostname, server_uri.port)
    socket.puts('SSH-2.0-Ascli_0.0')
    version = socket.gets.chomp
    return {version: version.gsub(/^SSH-2.0-/, ''), url: base_url} if version.match?(/^SSH-2.0-/)
  rescue StandardError => e
    error = e
    Log.log.debug { "detect error: #{e}" }
  end
  raise error if error
  return
end

Instance Method Details

#action_health_transfer ⇒ Object

--- health ---



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/aspera/plugins/server/commands.rb', line 303

def action_health_transfer(**)
  nagios = Nagios.new
  probe_ts = server_transfer_spec.merge({
    'direction'     => 'send',
    'cookie'        => 'aspera.sync', # hide in console
    'resume_policy' => 'none',
    'paths'         => [{'source' => 'faux:///pingfile?1k', 'destination' => '.fasping'}]
  })
  statuses = transfer.start(probe_ts)
  if statuses.is_a?(Aspera::Transfer::Result::Success)
    nagios.add_ok('transfer', 'ok')
  else
    nagios.add_critical('transfer', statuses.is_a?(Aspera::Transfer::Result::Error) ? statuses.exception.message : statuses.to_s)
  end
  Cli::Result::ObjectList.new(nagios.status_list)
end

#ascmd_available? ⇒ Boolean

--- conditions ---

Returns:

  • (Boolean)


272
273
274
# File 'lib/aspera/plugins/server/commands.rb', line 272

def ascmd_available?
  !ascmd_executor.nil?
end

#ascmd_executor ⇒ LocalExecutor, ...

Returns ascmd executor, nil with WSS (ascmd not available).

Returns:

  • (LocalExecutor, Ssh, nil) —

    ascmd executor, nil with WSS (ascmd not available)



285
286
287
288
# File 'lib/aspera/plugins/server/commands.rb', line 285

def ascmd_executor
  build_server_access if @server_transfer_spec.nil?
  @ascmd_executor
end

#build_server_access ⇒ Object

Build the transfer spec and ascmd executor from CLI options.



291
292
293
294
295
296
297
298
299
# File 'lib/aspera/plugins/server/commands.rb', line 291

def build_server_access
  @server_transfer_spec = options_to_base_transfer_spec
  @ascmd_executor = case @connection_type
  when :local then LocalExecutor.new
  when :wss   then nil
  when :ssh   then Ssh.new(@server_transfer_spec['remote_host'], @server_transfer_spec['remote_user'], @ssh_opts)
  else Aspera.error_unexpected_value(@connection_type) { 'connection type' }
  end
end

#options_to_base_transfer_spec ⇒ Hash

Read command line options

Returns:

  • (Hash) —

    transfer specification



109
110
111
112
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/aspera/plugins/server/commands.rb', line 109

def options_to_base_transfer_spec
  url = options.get_option(:url, mandatory: true)
  server_transfer_spec = {}
  server_uri = URI.parse(url)
  Log.log.debug { "URI=#{server_uri}, host=#{server_uri.hostname}, port=#{server_uri.port}, scheme=#{server_uri.scheme}" }
  server_transfer_spec['remote_host'] = server_uri.hostname
  unless URI_SCHEMES.include?(server_uri.scheme)
    Log.log.warn { "Scheme [#{server_uri.scheme}] not supported in #{url}, use one of: #{URI_SCHEMES.join(', ')}. Defaulting to #{SSH_SCHEME}." }
    server_uri.scheme = SSH_SCHEME
  end
  if server_uri.scheme.eql?(LOCAL_SCHEME)
    # Using local execution (mostly for testing)
    server_transfer_spec['remote_host'] = 'localhost'
    # simulate SSH environment, else ascmd will fail
    ENV['SSH_CLIENT'] = 'local 0 0'
    @connection_type = :local
    return server_transfer_spec
  elsif transfer.user_transfer_spec['token'].is_a?(String) && server_uri.scheme.eql?(HTTPS_SCHEME)
    server_transfer_spec['wss_enabled'] = true
    server_transfer_spec['wss_port'] = server_uri.port
    @connection_type = :wss
    # Using WSS
    return server_transfer_spec
  end
  if !server_uri.scheme.eql?(SSH_SCHEME)
    Log.log.warn('URL scheme is https but no token was provided in transfer spec.')
    Log.log.warn("If you want to access the server, not using WSS for session, then use a URL with scheme \"#{SSH_SCHEME}\" and proper SSH port")
    assumed_url = "#{SSH_SCHEME}://#{server_transfer_spec['remote_host']}:#{Aspera::Transfer::Spec::SSH_PORT}"
    Log.log.warn { "Assuming proper URL is: #{assumed_url}" }
    server_uri = URI.parse(assumed_url)
  end
  # Scheme is SSH
  if options.get_option(:username).nil?
    options.set_option(:username, Aspera::Transfer::Spec::ACCESS_KEY_TRANSFER_USER)
    Log.log.info { "No username provided: Assuming default transfer user: #{Aspera::Transfer::Spec::ACCESS_KEY_TRANSFER_USER}" }
  end
  server_transfer_spec['remote_user'] = options.get_option(:username, mandatory: true)
  if !server_uri.port.nil?
    @ssh_opts[:port] = server_uri.port
    server_transfer_spec['ssh_port'] = server_uri.port
  end
  cred_set = false
  password = options.get_option(:password)
  if !password.nil?
    @ssh_opts[:password] = password
    server_transfer_spec['remote_password'] = password
    cred_set = true
  end
  ssh_key_list = options.get_option(:ssh_keys)
  if !ssh_key_list.nil?
    ssh_key_list.map! { |p| File.expand_path(p) }
    Log.dump(:ssh_keys, ssh_key_list)
    if !ssh_key_list.empty?
      @ssh_opts[:keys] = ssh_key_list
      # PEM as per RFC 7468
      server_transfer_spec['ssh_private_key'] = File.read(ssh_key_list.first).strip
      Log.log.warn { 'Using only first SSH key for transfers' } unless ssh_key_list.length.eql?(1)
      cred_set = true
    end
  end
  ssh_passphrase = options.get_option(:passphrase)
  if !ssh_passphrase.nil?
    @ssh_opts[:passphrase] = ssh_passphrase
    server_transfer_spec['ssh_private_key_passphrase'] = ssh_passphrase
  end
  # if user provided transfer spec has a token, we will use bypass keys
  cred_set = true if transfer.user_transfer_spec['token'].is_a?(String)
  Aspera.assert(cred_set, 'Either password, key, or transfer spec token must be provided', type: Cli::BadArgument)
  return server_transfer_spec
end

#server_transfer_spec ⇒ Hash

Returns base transfer spec, built from CLI options on first use.

Returns:

  • (Hash) —

    base transfer spec, built from CLI options on first use



279
280
281
282
# File 'lib/aspera/plugins/server/commands.rb', line 279

def server_transfer_spec
  build_server_access if @server_transfer_spec.nil?
  @server_transfer_spec
end

#wizard(wizard, app_url) ⇒ Hash

Returns :preset_value, :test_args.

Parameters:

  • wizard (Wizard) —

    The wizard object

  • app_url (String) —

    Tested URL

Returns:

  • (Hash) —

    :preset_value, :test_args



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/aspera/plugins/server/commands.rb', line 84

def wizard(wizard, app_url)
  username = options.get_option(:username, mandatory: true)
  return {
    preset_ids:   [username],
    preset_value: {
      url:      app_url,
      username: username,
      password: options.get_option(:password, mandatory: true)
    },
    test_args:    'browse /'
  }
end