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
- #rpm_database ⇒ 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
150 151 152 |
# File 'lib/system.rb', line 150 def locale @locale || "C" end |
Class Method Details
.for(host, remote_user = "root") ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/system.rb', line 36 def self.for(host, remote_user = "root") if host && host != "localhost" RemoteSystem.new(host, remote_user) else LocalSystem.new end end |
Instance Method Details
#arch ⇒ Object
146 147 148 |
# File 'lib/system.rb', line 146 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 |
# 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.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, 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
139 140 141 142 143 144 |
# File 'lib/system.rb', line 139 def has_command?(command) run_command("bash", "-c", "type -P #{command}", stdout: :capture) true rescue Cheetah::ExecutionFailed false end |
#rpm_database ⇒ Object
154 155 156 |
# File 'lib/system.rb', line 154 def rpm_database @rpm_database ||= RpmDatabase.new(self) end |
#run_script(*args) ⇒ Object
99 100 101 102 103 |
# File 'lib/system.rb', line 99 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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/system.rb', line 115 def run_script_with_progress(*script, &callback) output = "" error = "" write_io = StringIO.new(output, "a") error_io = StringIO.new(error, "a") read_io = StringIO.new(output, "r") inspect_thread = Thread.new do run_script(*script, stdout: write_io, stderr: error_io) end while inspect_thread.alive? sleep 0.1 chunk = read_io.read callback.call(chunk) if callback end if error.include?("password is required") raise Machinery::Errors::InsufficientPrivileges.new(remote_user, host) end output end |