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) ⇒ Object



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

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

Instance Method Details

#check_requirement(command, *args) ⇒ Object

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



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

def check_requirement(command, *args)
  begin
    run_command(command, *args)
  rescue Cheetah::ExecutionFailed
    raise Machinery::Errors::MissingRequirement.new(
      "Need binary '#{command}' to be available on the inspected system."
    )
  end
end

#create_archive(filelist, 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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/system.rb', line 53

def create_archive(filelist, 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 => filelist
    )
  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)


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

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

#run_script(*args) ⇒ Object



76
77
78
79
80
# File 'lib/system.rb', line 76

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

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