Class: Machinery::LocalSystem

Inherits:
System show all
Defined in:
lib/local_system.rb

Overview

Copyright © 2013-2016 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

#locale

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from System

#arch, #check_create_archive_dependencies, #check_requirement, #check_retrieve_files_dependencies, #create_archive, for, #has_command?, #managed_files_database, #run_command_with_progress, #run_script, #run_script_with_progress

Class Method Details

.matches_architecture?(arch) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/local_system.rb', line 86

def matches_architecture?(arch)
  os.architecture == arch
end

.osObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/local_system.rb', line 22

def os
  unless @@os
    description = Machinery::SystemDescription.new(
      "localhost",
      Machinery::SystemDescriptionMemoryStore.new
    )
    inspector = Machinery::OsInspector.new(
      Machinery::System.for("localhost"),
      description
    )
    inspector.inspect(nil)
    @@os = description.os
  end
  @@os
end

.validate_architecture(arch) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/local_system.rb', line 90

def validate_architecture(arch)
  unless 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



76
77
78
79
80
81
82
83
84
# File 'lib/local_system.rb', line 76

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



38
39
40
# File 'lib/local_system.rb', line 38

def validate_existence_of_package(package)
  validate_existence_of_packages([package])
end

.validate_existence_of_packages(packages) ⇒ Object



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
69
70
71
72
73
74
# File 'lib/local_system.rb', line 42

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

  unless 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



155
156
157
158
159
160
161
162
# File 'lib/local_system.rb', line 155

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, _options = {}) ⇒ Object

Reads a file from the System. Returns nil if it does not exist.



147
148
149
150
151
152
# File 'lib/local_system.rb', line 147

def read_file(file, _options = {})
  File.read(file)
rescue Errno::ENOENT
  # File not found, return nil
  return
end

#remove_file(file) ⇒ Object

Removes a file from the System



165
166
167
168
169
170
171
172
# File 'lib/local_system.rb', line 165

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

Returns:

  • (Boolean)


103
104
105
# File 'lib/local_system.rb', line 103

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.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/local_system.rb', line 125

def retrieve_files(filelist, destination)
  begin
    Machinery::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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/local_system.rb', line 107

def run_command(*args)
  if args.last.is_a?(Hash) && args.last[:disable_logging]
    cheetah_class = Cheetah
  else
    cheetah_class = Machinery::LoggedCheetah
  end

  with_env(
    "LANGUAGE" => "",
    "LC_ALL" => locale
  ) do
    cheetah_class.run(*args)
  end
end

#typeObject



99
100
101
# File 'lib/local_system.rb', line 99

def type
  "local"
end