Class: RightScale::Platform::Shell

Inherits:
PlatformHelperBase show all
Defined in:
lib/right_agent/platform.rb,
lib/right_agent/platform/unix/platform.rb,
lib/right_agent/platform/windows/platform.rb,
lib/right_agent/platform/unix/linux/platform.rb,
lib/right_agent/platform/unix/darwin/platform.rb

Overview

VolumeManager

Constant Summary collapse

NULL_OUTPUT_NAME =

defined for backward compatibility; use Shell#null_output_name

'NUL'
POWERSHELL_V1x0_EXECUTABLE_PATH =
'powershell.exe'
POWERSHELL_V1x0_SCRIPT_EXTENSION =
'.ps1'
RUBY_SCRIPT_EXTENSION =
'.rb'

Constants inherited from PlatformHelperBase

PlatformHelperBase::API_FALSE, PlatformHelperBase::API_NULL, PlatformHelperBase::API_TRUE, PlatformHelperBase::SIZEOF_DWORD, PlatformHelperBase::SIZEOF_QWORD, PlatformHelperBase::WIDE

Instance Method Summary collapse

Methods inherited from PlatformHelperBase

#copy_to_string_buffer, #with_unicode_buffer

Instance Method Details

#booted_atObject

Overrides base Shell#booted_at



562
563
564
# File 'lib/right_agent/platform.rb', line 562

def booted_at
  must_be_overridden
end

#executable_extensionsArray

Returns list of dot-prefixed executable file extensions from PATHEXT.

Returns:

  • (Array)

    list of dot-prefixed executable file extensions from PATHEXT



1390
1391
1392
# File 'lib/right_agent/platform/windows/platform.rb', line 1390

def executable_extensions
  @executable_extensions ||= ::ENV['PATHEXT'].downcase.split(';')
end

#format_executable_command(executable_file_path, *arguments) ⇒ String

Formats an executable command by quoting any of the arguments as needed and building an executable command string.

Parameters:

  • executable_file_path (String)

    for formatting

  • arguments (Array)

    for command or empty

Returns:

  • (String)

    executable command string



487
488
489
# File 'lib/right_agent/platform.rb', line 487

def format_executable_command(executable_file_path, *arguments)
  must_be_overridden
end

#format_powershell_command(shell_script_file_path, *arguments) ⇒ String

Formats a powershell command using the given script path and arguments. Allows for specifying powershell from a specific installed location. This method is only implemented for Windows.

Parameters:

  • shell_script_file_path (String)

    for formatting

  • arguments (Array)

    for command or empty

Returns:

  • (String)

    executable command string



1311
1312
1313
1314
1315
1316
1317
1318
# File 'lib/right_agent/platform/windows/platform.rb', line 1311

def format_powershell_command(shell_script_file_path, *arguments)
  return format_powershell_command4(
    POWERSHELL_V1x0_EXECUTABLE_PATH,
    lines_before_script = nil,
    lines_after_script = nil,
    shell_script_file_path,
    *arguments)
end

#format_powershell_command4(powershell_exe_path, lines_before_script, lines_after_script, shell_script_file_path, *arguments) ⇒ String

Formats a powershell command using the given script path and arguments. Allows for specifying powershell from a specific installed location. This method is only implemented for Windows.

Parameters:

  • powershell_exe_path (String)

    for formatting

  • shell_script_file_path (String)

    for formatting

  • arguments (Array)

    for command or empty

Returns:

  • (String)

    executable command string



1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
# File 'lib/right_agent/platform/windows/platform.rb', line 1329

def format_powershell_command4(powershell_exe_path,
                               lines_before_script,
                               lines_after_script,
                               shell_script_file_path,
                               *arguments)
  # special case for powershell scripts.
  escaped = []
  [shell_script_file_path, arguments].flatten.each do |arg|
    value = arg.to_s
    # note that literal ampersand must be quoted on the powershell command
    # line because it otherwise means 'execute what follows'.
    escaped << ((value.index(' ') || value.index('&')) ? "'#{value.gsub("'", "''")}'" : value)
  end

  # resolve lines before & after script.
  defaulted_lines_after_script = lines_after_script.nil?
  lines_before_script ||= []
  lines_after_script ||= []

  # execute powershell with RemoteSigned execution policy. the issue
  # is that powershell by default will only run digitally-signed
  # scripts.
  # FIX: search for any attempt to alter execution policy in lines
  # before insertion.
  # FIX: support digitally signed scripts and/or signing on the fly by
  # checking for a signature file side-by-side with script.
  lines_before_script.insert(0, 'set-executionpolicy -executionPolicy RemoteSigned -Scope Process')

  # insert error checking only in case of defaulted "lines after script"
  # to be backward compatible with existing scripts.
  if defaulted_lines_after_script
    # ensure for a generic powershell script that any errors left in the
    # global $Error list are noted and result in script failure. the
    # best practice is for the script to handle errors itself (and clear
    # the $Error list if necessary), so this is a catch-all for any
    # script which does not handle errors "properly".
    lines_after_script << 'if ($NULL -eq $LastExitCode) { $LastExitCode = 0 }'
    lines_after_script << "if ((0 -eq $LastExitCode) -and ($Error.Count -gt 0)) { $RS_message = 'Script exited successfully but $Error contained '+($Error.Count)+' error(s):'; write-output $RS_message; write-output $Error; $LastExitCode = 1 }"
  end

  # ensure last exit code gets marshalled.
  marshall_last_exit_code_cmd = 'exit $LastExitCode'
  if defaulted_lines_after_script || (lines_after_script.last != marshall_last_exit_code_cmd)
    lines_after_script << marshall_last_exit_code_cmd
  end

  # format powershell command string.
  powershell_command = "&{#{lines_before_script.join('; ')}; &#{escaped.join(' ')}; #{lines_after_script.join('; ')}}"

  # in order to run 64-bit powershell from this 32-bit ruby process, we need to launch it using
  # our special RightRun utility from program files, if it is installed (it is not installed for
  # 32-bit instances and perhaps not for test/dev environments).
  executable_path = powershell_exe_path
  executable_arguments = ['-command', powershell_command]
  executable_path, executable_arguments = format_right_run_path(executable_path, executable_arguments)

  # combine command string with powershell executable and arguments.
  return format_executable_command(executable_path, executable_arguments)
end

#format_redirect_both(cmd, target = nil) ⇒ String

Appends STDERR redirection to the given shell command.

Parameters:

  • cmd (String)

    to format

  • redirection (String)

    target (Default = null output)

Returns:

  • (String)

    formatted for redirection



542
543
544
545
# File 'lib/right_agent/platform.rb', line 542

def format_redirect_both(cmd, target = nil)
  target ||= null_output_name
  return cmd + " 1>#{target} 2>&1"
end

#format_redirect_stderr(cmd, target = nil) ⇒ String

Appends STDERR redirection to the given shell command.

Parameters:

  • cmd (String)

    to format

  • redirection (String)

    target (Default = null output)

Returns:

  • (String)

    formatted for redirection



531
532
533
534
# File 'lib/right_agent/platform.rb', line 531

def format_redirect_stderr(cmd, target = nil)
  target ||= null_output_name
  return cmd + " 2>#{target}"
end

#format_redirect_stdout(cmd, target = nil) ⇒ String

Appends STDOUT redirection to the given shell command.

Parameters:

  • cmd (String)

    to format

  • redirection (String)

    target (Default = null output)

Returns:

  • (String)

    formatted for redirection



520
521
522
523
# File 'lib/right_agent/platform.rb', line 520

def format_redirect_stdout(cmd, target = nil)
  target ||= null_output_name
  return cmd + " 1>#{target}"
end

#format_right_run_path(executable_file_path, executable_arguments) ⇒ Array

Formats an executable path and arguments by inserting a reference to RightRun.exe only when necessary.

Parameters:

  • executable_file_path (String)

    for formatting

  • arguments (Array)

    for command or empty

Returns:

  • (Array)

    tuple for updated [executable_path, executable_arguments]



1270
1271
1272
1273
1274
1275
1276
1277
# File 'lib/right_agent/platform/windows/platform.rb', line 1270

def format_right_run_path(executable_file_path, executable_arguments)
  unless right_run_path.empty?
    executable_arguments.unshift(executable_file_path)
    executable_file_path = right_run_path
  end

  return executable_file_path, executable_arguments
end

#format_ruby_command(shell_script_file_path, *arguments) ⇒ String

Formats a ruby command using the given script path and arguments and the sandbox ruby path.

Parameters:

  • shell_script_file_path (String)

    for formatting

  • arguments (Array)

    for command or empty

Returns:

  • (String)

    executable command string



510
511
512
# File 'lib/right_agent/platform.rb', line 510

def format_ruby_command(shell_script_file_path, *arguments)
  return format_executable_command(sandbox_ruby, [shell_script_file_path, arguments])
end

#format_script_file_name(partial_script_file_path, default_extension = nil) ⇒ Object

Overrides base Shell#format_script_file_name



476
477
478
# File 'lib/right_agent/platform.rb', line 476

def format_script_file_name(partial_script_file_path, default_extension = nil)
  must_be_overridden
end

#format_shell_command(shell_script_file_path, *arguments) ⇒ Object

Overrides base Shell#format_shell_command



499
500
501
# File 'lib/right_agent/platform.rb', line 499

def format_shell_command(shell_script_file_path, *arguments)
  must_be_overridden
end

#null_output_nameObject

Overrides base Shell#null_output_name



463
464
465
# File 'lib/right_agent/platform.rb', line 463

def null_output_name
  must_be_overridden
end

#right_run_pathString

Returns path to RightRun.exe or empty in cases where it is unneeded.

Returns:

  • (String)

    path to RightRun.exe or empty in cases where it is unneeded



1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
# File 'lib/right_agent/platform/windows/platform.rb', line 1395

def right_run_path
  unless @right_run_path
    @right_run_path = ''
    if ::ENV['ProgramW6432'] && (@right_run_path = ::ENV['RS_RIGHT_RUN_EXE'].to_s).empty?
      temp_path = ::File.join(
        ::ENV['ProgramW6432'],
        ::RightScale::Platform::Filesystem::COMPANY_DIR_NAME,
        'Shared',
        'RightRun.exe')
      if ::File.file?(temp_path)
        @right_run_path = ::File.normalize_path(temp_path).gsub('/', "\\")
      end
    end
  end
  @right_run_path
end

#sandbox_rubyObject

Overrides base Shell#sandbox_ruby



548
549
550
# File 'lib/right_agent/platform.rb', line 548

def sandbox_ruby
  must_be_overridden
end

#uptimeObject

Overrides base Shell#uptime



555
556
557
# File 'lib/right_agent/platform.rb', line 555

def uptime
  must_be_overridden
end