Class: LocalSystem
Overview
Copyright © 2013-2015 SUSE LLC
This program is free software; you can redistribute it and/or modify it under the terms of version 3 of the GNU General Public License as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, contact SUSE LLC.
To contact SUSE about this file by physical or electronic mail, you may find current contact information at www.suse.com
Constant Summary collapse
- @@os =
nil
Instance Attribute Summary
Attributes inherited from System
Class Method Summary collapse
- .matches_architecture?(arch) ⇒ Boolean
- .os ⇒ Object
- .validate_architecture(arch) ⇒ Object
- .validate_existence_of_command(command, package) ⇒ Object
- .validate_existence_of_package(package) ⇒ Object
- .validate_existence_of_packages(packages) ⇒ Object
Instance Method Summary collapse
-
#inject_file(source, destination) ⇒ Object
Copies a file to the local system.
-
#read_file(file) ⇒ Object
Reads a file from the System.
-
#remove_file(file) ⇒ Object
Removes a file from the System.
- #requires_root? ⇒ Boolean
-
#retrieve_files(filelist, destination) ⇒ Object
Retrieves files specified in filelist from the local system and raises an Machinery::Errors::RsyncFailed exception when it’s not successful.
- #run_command(*args) ⇒ Object
- #type ⇒ Object
Methods inherited from System
#arch, #check_create_archive_dependencies, #check_requirement, #check_retrieve_files_dependencies, #create_archive, for, #has_command?, #rpm_database, #run_script, #run_script_with_progress
Class Method Details
.matches_architecture?(arch) ⇒ Boolean
80 81 82 |
# File 'lib/local_system.rb', line 80 def matches_architecture?(arch) os.architecture == arch end |
.os ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/local_system.rb', line 22 def os if !@@os description = SystemDescription.new("localhost", SystemDescriptionMemoryStore.new) inspector = OsInspector.new(System.for("localhost"), description) inspector.inspect(nil) @@os = description.os end @@os end |
.validate_architecture(arch) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/local_system.rb', line 84 def validate_architecture(arch) if !matches_architecture?(arch) raise(Machinery::Errors::UnsupportedArchitecture.new( "This operation is not supported on architecture '#{os.architecture}'." )) end end |
.validate_existence_of_command(command, package) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/local_system.rb', line 70 def validate_existence_of_command(command, package) Cheetah.run("which", command) rescue Cheetah::ExecutionFailed output = <<-EOF You need the command '#{command}' from package '#{package}'. You can install it by running `zypper install #{package}`. EOF raise(Machinery::Errors::MissingRequirement.new(output)) end |
.validate_existence_of_package(package) ⇒ Object
32 33 34 |
# File 'lib/local_system.rb', line 32 def validate_existence_of_package(package) validate_existence_of_packages([package]) end |
.validate_existence_of_packages(packages) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/local_system.rb', line 36 def validate_existence_of_packages(packages) missing_packages = [] needed_modules = [] packages.each do |package| begin Cheetah.run("rpm", "-q", package) rescue Cheetah::ExecutionFailed missing_packages << package needed_module = os.module_required_by_package(package) if needed_module needed_modules << package if needed_module output = <<-EOF You need the package '#{package}' from module '#{needed_module}'. You can install it as follows: If you haven't selected the module '#{needed_module}' before, run `yast2 scc` and choose 'Select Extensions' and activate '#{needed_module}'. Run `zypper install #{package}` to install the package. EOF raise(Machinery::Errors::MissingRequirement.new(output)) end end end end if !missing_packages.empty? count = missing_packages.count error_string = <<-EOF You need the #{Machinery::pluralize(count, "package")} '#{missing_packages.join("\',\'")}'. You can install it by running `zypper install #{missing_packages.join(" ")}`. EOF raise(Machinery::Errors::MissingRequirement.new(error_string)) end end |
Instance Method Details
#inject_file(source, destination) ⇒ Object
Copies a file to the local system
149 150 151 152 153 154 155 156 |
# File 'lib/local_system.rb', line 149 def inject_file(source, destination) FileUtils.copy(source, destination) rescue => e raise Machinery::Errors::InjectFileFailed.new( "Could not inject file '#{source}' to local system.\n" \ "Error: #{e}" ) end |
#read_file(file) ⇒ Object
Reads a file from the System. Returns nil if it does not exist.
141 142 143 144 145 146 |
# File 'lib/local_system.rb', line 141 def read_file(file) File.read(file) rescue Errno::ENOENT # File not found, return nil return end |
#remove_file(file) ⇒ Object
Removes a file from the System
159 160 161 162 163 164 165 166 |
# File 'lib/local_system.rb', line 159 def remove_file(file) File.delete(file) if File.exist?(file) rescue => e raise Machinery::Errors::RemoveFileFailed.new( "Could not remove file '#{file}' on local system'.\n" \ "Error: #{e}" ) end |
#requires_root? ⇒ Boolean
97 98 99 |
# File 'lib/local_system.rb', line 97 def requires_root? true end |
#retrieve_files(filelist, destination) ⇒ Object
Retrieves files specified in filelist from the local system and raises an Machinery::Errors::RsyncFailed exception when it’s not successful. Destination is the directory where to put the files.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/local_system.rb', line 119 def retrieve_files(filelist, destination) begin LoggedCheetah.run( "rsync", "--chmod=go-rwx", "--files-from=-", "/", destination, stdout: :capture, stdin: filelist.join("\n") ) rescue Cheetah::ExecutionFailed => e raise Machinery::Errors::RsyncFailed.new( "Could not rsync files from localhost. \n" \ "Error: #{e}\n" \ "If you lack read permissions on some files you may want to retry as user root or specify\n" \ "the fully qualified host name instead of localhost in order to connect as root via ssh." ) end end |
#run_command(*args) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/local_system.rb', line 101 def run_command(*args) if args.last.is_a?(Hash) && args.last[:disable_logging] cheetah_class = Cheetah else cheetah_class = LoggedCheetah end with_env( "LANGUAGE" => "", "LC_ALL" => locale ) do cheetah_class.run(*args) end end |
#type ⇒ Object
93 94 95 |
# File 'lib/local_system.rb', line 93 def type "local" end |