GitList
Repositories
Help
Report an Issue
rcm
Code
Commits
Branches
Tags
Search
Tree:
97a2c49
Branches
Tags
master
rcm
managers
rcm_package_manager.rb
Converging the state as defined by config.
Dev
commited
97a2c49
at 2018-06-16 23:34:21
rcm_package_manager.rb
Blame
History
Raw
require_relative '../rcm_utils' module RCM class Apt APT_GET = '/usr/bin/apt-get'.freeze DPKG_QUERY = '/usr/bin/dpkg-query'.freeze APT_CACHE = '/usr/bin/apt-cache'.freeze APT_PARTIAL_DIR = '/var/lib/apt/lists/partial'.freeze def initialize(logger) @logger = logger end def update @logger.info('Updating apt cache.') unless ::File.readable?(APT_PARTIAL_DIR) @logger.warn('Skipping apt cache update as we are not running with right privilege.') return end status = ::RCM.cmd("#{APT_GET} update") unless status[:exit_code] == 0 @logger.warn("Updating apt cache failed. \n\n#{status[:error]}\n\nstdout:\n#{status[:output]}") end end def status(pkg_name) ::RCM.cmd("#{DPKG_QUERY} --status #{pkg_name}") end def install(pkgs) raise "\n\nPlease ensure you are running with root privileges\n\n" unless ::File.readable?(APT_PARTIAL_DIR) # If we only get one obejct, add to it an array and process it like that packages = pkgs.is_a?(::RCM::Package) ? [pkgs] : pkgs @logger.info("Installing #{packages.join(', ')}") # Construct string of packages to install in one go. pkgs_string = '' packages.each do |pkg| version_string = pkg.version.empty? ? '' : "=#{pkg.version}" pkgs_string += "#{pkg.name}#{version_string} " end cmd_string = "#{APT_GET} install --yes #{pkgs_string}" status = ::RCM.cmd(cmd_string) if status[:exit_code] == 0 @logger.info("Success: #{cmd_string}.") # Update state packages.each { |pkg| pkg.changed = true; pkg.state = ::RCM::Package::INSTALLED } else @logger.error("Failed: #{cmd_string}\n\nstderr:\n#{status[:error]}\n\nstdout:\n#{status[:output]}") end end def get_versions(pkg_name) name = '' if pkg_name.is_a?(String) name = pkg_name elsif pkg_name.is_a?(::RCM::Package) name = pkg_name.name end status = ::RCM.cmd("#{APT_CACHE} madison #{name}") return [] if !status[:output] || status[:output] =~ /Unable to locate package/ # Collect all versions by splitting on new line, and then collecting second # field from every line with delimiter ' | ' available_versions = status[:output].split("\n").collect { |l| l.split(' | ')[1] } available_versions.uniq end def remove(pkg) raise "\n\nPlease ensure you are running with root privileges\n\n" unless ::File.readable?(APT_PARTIAL_DIR) @logger.info("Uninstalling #{pkg.name}.") # Remove is idempotent and returns with success even if package is not installed. status = ::RCM.cmd("#{APT_GET} remove --yes #{pkg.name}") @logger.debug("\nstderr:\n#{status[:error]}\n\nstdout:\n#{status[:output]}") raise "Removing #{pkg.name} failed!" unless status[:exit_code] == 0 pkg.changed = true pkg.state = ::RCM::Package::REMOVED end def get_current_state(wanted_packages) got = {} wanted_packages.each do |name, p| state = ::RCM::Package::REMOVED version = 'idk' status = status(p.name) if status[:exit_code] == 0 state = ::RCM::Package::INSTALLED if status[:output] =~ /.+ok installed/ version = /Version: (?<version>\d+.+)/.match(status[:output])[:version] end current_pkg = ::RCM::Package.new(name, version, state) got[name] = current_pkg end got end end end