Class: Machinery::System

Inherits:
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

DockerSystem, LocalSystem, RemoteSystem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#localeObject



136
137
138
# File 'lib/system.rb', line 136

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"
    Machinery::RemoteSystem.new(host, opts)
  else
    Machinery::LocalSystem.new
  end
end

Instance Method Details

#archObject



132
133
134
# File 'lib/system.rb', line 132

def arch
  run_command("uname", "-m", stdout: :capture).chomp
end

#check_create_archive_dependenciesObject



64
65
66
67
# File 'lib/system.rb', line 64

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
58
# 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 the '#{commands.join("' or '")}' #{Machinery.pluralize(commands.length, "command")}" \
      " to be available on the inspected system."
  )
end

#check_retrieve_files_dependenciesObject



60
61
62
# File 'lib/system.rb', line 60

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



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
99
# File 'lib/system.rb', line 72

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

Returns:

  • (Boolean)


125
126
127
128
129
130
# File 'lib/system.rb', line 125

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

#managed_files_databaseObject



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/system.rb', line 140

def managed_files_database
  if @managed_files_database
    return @managed_files_database
  elsif has_command?("rpm")
    @managed_files_database = Machinery::RpmDatabase.new(self)
  elsif has_command?("dpkg")
    @managed_files_database = Machinery::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



121
122
123
# File 'lib/system.rb', line 121

def run_command_with_progress(*command, &callback)
  run_with_progress(*command, :command, &callback)
end

#run_script(*args) ⇒ Object



101
102
103
104
105
# File 'lib/system.rb', line 101

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


117
118
119
# File 'lib/system.rb', line 117

def run_script_with_progress(*script, &callback)
  run_with_progress(*script, :script, &callback)
end