Class: System

Inherits:
Object
  • Object
show all
Defined in:
lib/system.rb

Overview

System and its subclasses are used to represent systems that are to be inspected.

It abstracts common inspection tasks that need to be run, like executing commands or running “kiwi –describe”. Different implementations, e.g. for local or ssh-accessed systems are done in the according subclasses.

Direct Known Subclasses

LocalSystem, RemoteSystem

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for(host, remote_user = "root") ⇒ Object



31
32
33
34
35
36
37
# File 'lib/system.rb', line 31

def self.for(host, remote_user = "root")
  if host && host != "localhost"
    RemoteSystem.new(host, remote_user)
  else
    LocalSystem.new
  end
end

Instance Method Details

#check_requirement(commands, *args) ⇒ Object

checks if the required command can be executed on the target system



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/system.rb', line 40

def check_requirement(commands, *args)
  commands = Array(commands)
  commands.each do |command|
    begin
      run_command(command, *args)
      return command
    rescue Cheetah::ExecutionFailed
    end
  end
  raise Machinery::Errors::MissingRequirement.new(
    "Need binary '#{commands.join("' or '")}' to be available on the inspected system."
  )
end

#create_archive(file_list, archive, exclude = []) ⇒ Object

Retrieves files specified in filelist from the remote system and create an archive. To be able to deal with arbitrary filenames we use zero-terminated filelist and the –null option of tar



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/system.rb', line 57

def create_archive(file_list, archive, exclude = [])
  created = !File.exists?(archive)
  out = File.open(archive, "w")
  begin
    run_command(
      "tar", "--create", "--gzip", "--null", "--files-from=-",
      *exclude.flat_map { |f| ["--exclude", f]},
      stdout: out,
      stdin: Array(file_list).join("\0"),
      privileged: true
    )
  rescue Cheetah::ExecutionFailed => e
    if e.status.exitstatus == 1
      # The tarball has been created successfully but some files were changed
      # on disk while being archived, so we just log the warning and go on
      Machinery.logger.info e.stderr
    else
      raise
    end
  end
  out.close
  File.chmod(0600, archive) if created
end

#has_command?(command) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/system.rb', line 87

def has_command?(command)
  run_command("bash", "-c", "type -P #{command}", stdout: :capture)
  true
rescue Cheetah::ExecutionFailed
  false
end

#run_script(*args) ⇒ Object



81
82
83
84
85
# File 'lib/system.rb', line 81

def run_script(*args)
  script = File.read(File.join(Machinery::ROOT, "helpers", args.shift))

  run_command("bash", "-c", script, *args)
end