Class: MachineryHelper

Inherits:
Object show all
Defined in:
lib/machinery_helper.rb

Overview

The MachineryHelper class handles the helper binaries Machinery can use to do inspections. It provides methods to check, if a helper is available, to inject it to the target machine, run it there, and clean up after it’s done.

The inspection checks, if a binary helper is available on the machine where the inspection is started. It looks at the location

<machinery-installation-path>/machinery-helper/machinery-helper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ MachineryHelper

Returns a new instance of MachineryHelper.



30
31
32
33
34
# File 'lib/machinery_helper.rb', line 30

def initialize(s)
  @system = s

  @local_helpers_path = File.join(Machinery::ROOT, "machinery-helper")
end

Instance Attribute Details

#local_helpers_pathObject

Returns the value of attribute local_helpers_path.



28
29
30
# File 'lib/machinery_helper.rb', line 28

def local_helpers_path
  @local_helpers_path
end

Instance Method Details

#can_help?Boolean

Returns true, if there is a helper binary matching the architecture of the inspected system. Return false, if not.

Returns:

  • (Boolean)


50
51
52
# File 'lib/machinery_helper.rb', line 50

def can_help?
  File.exist?(local_helper_path)
end

#has_compatible_version?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/machinery_helper.rb', line 76

def has_compatible_version?
  output = @system.run_command(remote_helper_path, "--version", stdout: :capture).chomp

  version = output[/^Version: ([a-f0-9]{40})$/, 1]

  version == File.read(File.join(Machinery::ROOT, ".git_revision"))
end

#inject_helperObject



54
55
56
# File 'lib/machinery_helper.rb', line 54

def inject_helper
  @system.inject_file(local_helper_path, remote_helper_path)
end

#local_helper_pathObject



36
37
38
# File 'lib/machinery_helper.rb', line 36

def local_helper_path
  File.join(local_helpers_path, "machinery-helper-#{@system.arch}")
end

#remote_helper_pathObject



40
41
42
43
44
45
46
# File 'lib/machinery_helper.rb', line 40

def remote_helper_path
  @remote_helper_path ||= @system.run_command(
    # Expand Machinery::HELPER_REMOTE_PATH on remote machine
    "bash", "-c", "echo -n #{File.join(Machinery::HELPER_REMOTE_PATH, "machinery-helper")}",
      stdout: :capture
  )
end

#remove_helperObject



72
73
74
# File 'lib/machinery_helper.rb', line 72

def remove_helper
  @system.remove_file(remote_helper_path)
end

#run_helper(scope, *options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/machinery_helper.rb', line 58

def run_helper(scope, *options)
  error = TeeIO.new(STDERR, "sudo: a password is required\n")
  json = @system.run_command(
    remote_helper_path, *options, stdout: :capture, stderr: error, privileged: true
  )
  scope.insert(0, *JSON.parse(json)["files"])
rescue Cheetah::ExecutionFailed => e
  if error.string.include?("password is required")
    raise Machinery::Errors::InsufficientPrivileges.new(@system.remote_user, @system.host)
  else
    raise e
  end
end

#run_helper_subcommand(subcommand, *args) ⇒ Object



84
85
86
87
88
# File 'lib/machinery_helper.rb', line 84

def run_helper_subcommand(subcommand, *args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  options[:privileged] = true
  @system.run_command(remote_helper_path, subcommand, *args, options)
end