Class: Aspera::Cli::Plugins::Server

Inherits:
BasicAuthPlugin show all
Defined in:
lib/aspera/cli/plugins/server.rb

Defined Under Namespace

Classes: LocalExecutor

Constant Summary collapse

LOCAL_SCHEME =
'local'
HTTPS_SCHEME =
'https'
BASE_ACTIONS =

actions without ascmd

%i[health].concat(TRANSFER_COMMANDS).freeze
ACTIONS =

all actions

[BASE_ACTIONS, Aspera::AsCmd::OPERATIONS, ASCMD_ALIASES.keys].flatten.freeze

Constants inherited from Aspera::Cli::Plugin

Aspera::Cli::Plugin::ALL_OPS, Aspera::Cli::Plugin::GLOBAL_OPS, Aspera::Cli::Plugin::INSTANCE_OPS, Aspera::Cli::Plugin::MAX_ITEMS, Aspera::Cli::Plugin::MAX_PAGES, Aspera::Cli::Plugin::REGEX_LOOKUP_ID_BY_FIELD, Aspera::Cli::Plugin::VAL_ALL

Instance Method Summary collapse

Methods inherited from BasicAuthPlugin

#basic_auth_api, #basic_auth_params, register_options

Methods inherited from Aspera::Cli::Plugin

#do_bulk_operation, #entity_action, #entity_command, #instance_identifier, #old_query_read_delete, #query_read_delete, #value_create_modify, #value_or_query

Constructor Details

#initialize(env) ⇒ Server

Returns a new instance of Server.



52
53
54
55
56
57
58
59
# File 'lib/aspera/cli/plugins/server.rb', line 52

def initialize(env)
  super(env)
  options.declare(:ssh_keys, 'SSH key path list (Array or single)')
  options.declare(:passphrase, 'SSH private key passphrase')
  options.declare(:ssh_options, 'SSH options', types: Hash, default: {})
  options.parse_options!
  @ssh_opts = options.get_option(:ssh_options).symbolize_keys
end

Instance Method Details

#execute_actionObject



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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/aspera/cli/plugins/server.rb', line 151

def execute_action
  server_transfer_spec = options_to_base_transfer_spec
  ascmd_executor = if !@ssh_opts.empty?
    Ssh.new(server_transfer_spec['remote_host'], server_transfer_spec['remote_user'], @ssh_opts)
  elsif server_transfer_spec.key?('wss_enabled')
    nil
  else
    LocalExecutor.new
  end
  # the set of available commands depends on SSH executor availability (i.e. no WSS)
  available_commands = ascmd_executor.nil? ? BASE_ACTIONS : ACTIONS
  # get command and translate aliases
  command = options.get_next_command(available_commands)
  command = ASCMD_ALIASES[command] if ASCMD_ALIASES.key?(command)
  case command
  when :health
    nagios = Nagios.new
    command_nagios = options.get_next_command(%i[transfer])
    case command_nagios
    when :transfer
      file = Tempfile.new('transfer_test')
      filepath = file.path
      file.write('This is a test file for transfer test')
      file.close
      probe_ts = server_transfer_spec.merge({
        'direction'     => 'send',
        'cookie'        => 'aspera.sync', # hide in console
        'resume_policy' => 'none',
        'paths'         => [{'source' => filepath, 'destination' => '.fasping'}]
      })
      statuses = transfer.start(probe_ts)
      file.unlink
      if TransferAgent.session_status(statuses).eql?(:success)
        nagios.add_ok('transfer', 'ok')
      else
        nagios.add_critical('transfer', statuses.reject{|i|i.eql?(:success)}.first.to_s)
      end
    else raise 'ERROR'
    end
    return nagios.result
  when *TRANSFER_COMMANDS
    return execute_transfer(command, server_transfer_spec)
  when *Aspera::AsCmd::OPERATIONS
    args = options.get_next_argument('ascmd command arguments', expected: :multiple, mandatory: false)
    ascmd = Aspera::AsCmd.new(ascmd_executor)
    begin
      result = ascmd.send(:execute_single, command, args)
      case command
      when :mkdir, :mv, :cp, :rm
        return Main.result_success
      when :ls
        return {type: :object_list, data: result.map(&:stringify_keys), fields: %w[zmode zuid zgid size mtime name]}
      when :df
        return {type: :object_list, data: result.map(&:stringify_keys)}
      when :du, :md5sum, :info
        return {type: :single_object, data: result.stringify_keys}
      end
    rescue Aspera::AsCmd::Error => e
      raise CliBadArgument, e.extended_message
    end
  else raise 'internal error: unexpected action'
  end
end

#execute_transfer(command, transfer_spec) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/aspera/cli/plugins/server.rb', line 135

def execute_transfer(command, transfer_spec)
  case command
  when :upload, :download
    Fasp::TransferSpec.action_to_direction(transfer_spec, command)
    return Main.result_transfer(transfer.start(transfer_spec))
  when :sync
    sync_plugin = Plugins::Sync.new(@agents, sync_spec: SyncSpecServer.new(transfer_spec))
    return sync_plugin.execute_action
  end
end

#options_to_base_transfer_specHash

Read command line options

Returns:

  • (Hash)

    transfer specification



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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/aspera/cli/plugins/server.rb', line 63

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 ascp will fail
    ENV['SSH_CLIENT'] = 'local 0 0'
    return server_transfer_spec
  elsif transfer.option_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
    # 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::Fasp::TransferSpec::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::Fasp::TransferSpec::ACCESS_KEY_TRANSFER_USER)
    Log.log.info{"No username provided: Assuming default transfer user: #{Aspera::Fasp::TransferSpec::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?
    raise 'Expecting single value or array for ssh_keys' unless ssh_key_list.is_a?(Array) || ssh_key_list.is_a?(String)
    ssh_key_list = [ssh_key_list] if ssh_key_list.is_a?(String)
    ssh_key_list.map!{|p|File.expand_path(p)}
    Log.log.debug{"SSH keys=#{ssh_key_list}"}
    if !ssh_key_list.empty?
      @ssh_opts[:keys] = ssh_key_list
      server_transfer_spec['EX_ssh_key_paths'] = ssh_key_list
      ssh_key_list.each do |k|
        Log.log.warn{"No such key file: #{k}"} unless File.exist?(k)
      end
      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.option_transfer_spec['token'].is_a?(String)
  raise 'Either password, key , or transfer spec token must be provided' if !cred_set
  return server_transfer_spec
end