Class: System
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
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #arch ⇒ Object
- #check_create_archive_dependencies ⇒ Object
-
#check_requirement(commands, *args) ⇒ Object
checks if the required command can be executed on the target system.
- #check_retrieve_files_dependencies ⇒ Object
-
#create_archive(file_list, archive, exclude = []) ⇒ Object
Retrieves files specified in filelist from the remote system and create an archive.
- #has_command?(command) ⇒ Boolean
- #managed_files_database ⇒ Object
- #run_command_with_progress(*command, &callback) ⇒ Object
- #run_script(*args) ⇒ Object
-
#run_script_with_progress(*script, &callback) ⇒ Object
Runs the given script on the inspected machine asynchronously and calls the callback method periodically with new output when it occurs.
Instance Attribute Details
#locale ⇒ Object
135 136 137 |
# File 'lib/system.rb', line 135 def locale @locale || "C" end |
Class Method Details
.for(host, opts = {}) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/system.rb', line 36 def self.for(host, opts = {}) if host && host != "localhost" RemoteSystem.new(host, opts) else LocalSystem.new end end |
Instance Method Details
#arch ⇒ Object
131 132 133 |
# File 'lib/system.rb', line 131 def arch run_command("uname", "-m", stdout: :capture).chomp end |
#check_create_archive_dependencies ⇒ Object
63 64 65 66 |
# File 'lib/system.rb', line 63 def check_create_archive_dependencies check_requirement("tar", "--version") check_requirement("gzip", "--version") end |
#check_requirement(commands, *args) ⇒ Object
checks if the required command can be executed on the target system
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/system.rb', line 45 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 |
#check_retrieve_files_dependencies ⇒ Object
59 60 61 |
# File 'lib/system.rb', line 59 def check_retrieve_files_dependencies check_requirement("rsync", "--version") 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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/system.rb', line 71 def create_archive(file_list, archive, exclude = []) Machinery.logger.info( "The following files are packaged in #{archive}: " + Array(file_list).join(", ") ) created = !File.exist?(archive) out = File.open(archive, "w") begin run_command( "tar", "--create", "--gzip", *exclude.flat_map { |f| ["--exclude", f]}, "--null", "--files-from=-", stdout: out, stdin: Array(file_list).join("\0"), privileged: true, disable_logging: 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
124 125 126 127 128 129 |
# File 'lib/system.rb', line 124 def has_command?(command) run_command("bash", "-c", "type -P #{command}", stdout: :capture) true rescue Cheetah::ExecutionFailed false end |
#managed_files_database ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/system.rb', line 139 def managed_files_database if @managed_files_database return @managed_files_database elsif has_command?("rpm") @managed_files_database = RpmDatabase.new(self) elsif has_command?("dpkg") @managed_files_database = DpkgDatabase.new(self) else raise Machinery::Errors::MissingRequirement.new( "Need binary 'rpm' or 'dpkg' to be available on the inspected system." ) end end |
#run_command_with_progress(*command, &callback) ⇒ Object
120 121 122 |
# File 'lib/system.rb', line 120 def run_command_with_progress(*command, &callback) run_with_progress(*command, :command, &callback) end |
#run_script(*args) ⇒ Object
100 101 102 103 104 |
# File 'lib/system.rb', line 100 def run_script(*args) script = File.read(File.join(Machinery::ROOT, "inspect_helpers", args.shift)) run_command("bash", "-c", script, *args) end |
#run_script_with_progress(*script, &callback) ⇒ Object
Runs the given script on the inspected machine asynchronously and calls the callback method periodically with new output when it occurs.
Example:
count = 0
raw_list = run_script_with_progress("changed_managed_files.sh") do |chunk|
count += chunk.lines.count
Machinery::Ui.progress("Found #{count} changed files...")
end
116 117 118 |
# File 'lib/system.rb', line 116 def run_script_with_progress(*script, &callback) run_with_progress(*script, :script, &callback) end |