modules/aptpackage/aptpackage.rb in nuri-0.5.1 vs modules/aptpackage/aptpackage.rb in nuri-0.5.2

- old
+ new

@@ -1,5 +1,7 @@ +require 'thread' + class Sfp::Module::AptPackage include Sfp::Resource def update_state to_model @@ -26,10 +28,12 @@ # # Helper methods # ############################## + @@lock = Mutex.new + def self.installed?(package) package = package.to_s.strip return false if package.length <= 0 data = `/usr/bin/dpkg-query -W #{package} 2> /dev/null`.strip.chop.split(' ') return true if data[0].to_s == package @@ -44,36 +48,40 @@ nil end def self.install(package) return false if not package.is_a?(String) or package.length <= 0 - return true if Sfp::Module::Package.installed?(package) - system("dpkg --configure -a") - system("apt-get -y --purge autoremove") - return true if system("apt-get -y install #{package}") - system("dpkg --configure -a") - system("apt-get -y update") - !!system("apt-get -y install #{package}") + return true if Sfp::Module::AptPackage.installed?(package) + @@lock.synchronize { + system("dpkg --configure -a") + system("apt-get -y --purge autoremove") + return true if system("apt-get -y install #{package}") + system("dpkg --configure -a") + system("apt-get -y update") + !!system("apt-get -y install #{package}") + } end def self.uninstall(package) return false if not package.is_a?(String) or package.length <= 0 - return true if not Sfp::Module::Package.installed?(package) - system("dpkg --configure -a") - system("apt-get -y --purge autoremove") - return (!!system("sudo apt-get -y --purge remove #{package}") and - !!system("sudo apt-get -y --purge autoremove") and - !!system("sudo apt-get -y --purge autoremove")) + return true if not Sfp::Module::AptPackage.installed?(package) + @@lock.synchronize { + system("dpkg --configure -a") + system("apt-get -y --purge autoremove") + return (!!system("sudo apt-get -y --purge remove #{package}") and + !!system("sudo apt-get -y --purge autoremove") and + !!system("sudo apt-get -y --purge autoremove")) + } end protected def installed? Sfp::Module::AptPackage.installed?(@model['package_name'].to_s.strip) end def version? package = @model['package_name'].to_s.strip - return nil if package.length <= 0 + return "" if package.length <= 0 installed = `apt-cache policy #{package} | grep Installed`.strip.split(' ', 2)[1].to_s.strip return "" if installed.length <= 0 candidate = `apt-cache policy #{package} | grep Candidate`.strip.split(' ', 2)[1].to_s.strip return "latest" if candidate == installed installed