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)


52
53
54
# File 'lib/machinery_helper.rb', line 52

def can_help?
  File.exist?(local_helper_path)
end

#has_compatible_version?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'lib/machinery_helper.rb', line 78

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



56
57
58
# File 'lib/machinery_helper.rb', line 56

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

#local_helper_pathObject



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

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

#remote_helper_pathObject



42
43
44
45
46
47
48
# File 'lib/machinery_helper.rb', line 42

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



74
75
76
# File 'lib/machinery_helper.rb', line 74

def remove_helper
  @system.remove_file(remote_helper_path)
end

#run_helper(scope, *options) ⇒ Object



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

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



86
87
88
89
90
# File 'lib/machinery_helper.rb', line 86

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