Class: Tpkg::OS::Debian

Inherits:
Tpkg::OS show all
Defined in:
lib/tpkg/os/debian.rb

Overview

tpkg package management system License: MIT (www.opensource.org/licenses/mit-license.php)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tpkg::OS

#arch, create, #fqdn, #os, #os_name, register_implementation, #remove_native_stub_pkg, #stub_native_pkg, #sudo_default?, #sys_v_init_links

Constructor Details

#initialize(options = {}) ⇒ Debian

Returns a new instance of Debian.



11
12
13
14
15
16
# File 'lib/tpkg/os/debian.rb', line 11

def initialize(options={})
  @dpkgquerycmd = options[:dpkgquerycmd] || options[:testcmd] || 'dpkg-query'
  @aptcachecmd = options[:aptcachecmd] || options[:testcmd] || 'apt-cache'
  @aptgetcmd = options[:aptgetcmd] || options[:testcmd] || 'apt-get'
  super
end

Class Method Details

.supported?Boolean

Returns:

  • (Boolean)


5
6
7
8
# File 'lib/tpkg/os/debian.rb', line 5

def self.supported?
  Facter.loadfacts
  ['Debian', 'Ubuntu'].include?(Facter['operatingsystem'].value)
end

Instance Method Details

#available_native_packages(pkgname) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
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
69
70
71
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
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/tpkg/os/debian.rb', line 24

def available_native_packages(pkgname)
  native_packages = []
  # The default 'dpkg -l' format has an optional third column for errors
  # which makes it hard to parse reliably, so we specify a custom format.
  cmd = "#{@dpkgquerycmd} -W -f='${Package} ${Version} ${Status}\n' #{pkgname}"
  puts "available_native_packages running #{cmd}" if @debug
  stderr_first_line = nil
  Open3.popen3(cmd) do |stdin, stdout, stderr|
    stdin.close
    stdout.each_line do |line|
      name, debversion, status = line.split(' ', 3)
      # Seems to be Debian convention that if the package has a
      # package version you seperate that from the upstream version
      # with a hyphen.
      version = nil
      package_version = nil
      if debversion =~ /-/
        version, package_version = debversion.split('-', 2)
      else
        version = debversion
      end
      # We want packages with a state of "installed".  However,
      # there's also a state of "not-installed", and the state
      # field contains several space-seperated values, so we have
      # to be somewhat careful to pick out "installed".
      if status.split(' ').include?('installed')
        pkg = Tpkg.pkg_for_native_package(name, version, package_version, :native_installed)
        native_packages << pkg
      end
    end
    stderr_first_line = stderr.gets
  end
  # FIXME: popen3 doesn't set $?
  if !$?.success?
    # Ignore 'no matching packages', raise anything else
    if stderr_first_line !~ 'No packages found matching'
      raise "available_native_packages error running dpkg-query"
    end
  end
  
  cmd = "#{@aptcachecmd} show #{pkgname}"
  puts "available_native_packages running '#{cmd}'" if @debug
  stderr_first_line = nil
  Open3.popen3(cmd) do |stdin, stdout, stderr|
    stdin.close
    name = nil
    version = nil
    package_version = nil
    skip = false
    stdout.each_line do |line|
      if line =~ /^Package: (.*)/
        name = $1
        version = nil
        package_version = nil
        skip = false
      elsif line =~ /Status: (.*)/
        # Packages with status are coming from dpkg rather than apt. They're
        # either already installed (and thus captured by the dpkg-query
        # above) or uninstalled and not really available.  This seems to be
        # a new feature of apt-get in Debian 7.  On older systems these
        # packages don't show up in apt-get show, which is why we still need
        # the dpkg-query command above.
        skip = true
      elsif line =~ /^Version: (.*)/ && !skip
        debversion = $1
        # Seems to be Debian convention that if the package has a
        # package version you seperate that from the upstream version
        # with a hyphen.
        if debversion =~ /-/
          version, package_version = debversion.split('-', 2)
        else
          version = debversion
        end
        pkg = Tpkg.pkg_for_native_package(name, version, package_version, :native_available)
        native_packages << pkg
      end
    end
    stderr_first_line = stderr.gets
  end
  # FIXME: popen3 doesn't set $?
  if !$?.success?
    # Ignore 'no matching packages', raise anything else
    if stderr_first_line !~ 'No packages found'
      raise "available_native_packages error running apt-cache"
    end
  end
  native_packages
end

#cron_dot_d_directoryObject



21
22
23
# File 'lib/tpkg/os/debian.rb', line 21

def cron_dot_d_directory
  '/etc/cron.d'
end


18
19
20
# File 'lib/tpkg/os/debian.rb', line 18

def init_links(installed_path, tpkgfile)
  sys_v_init_links(installed_path, tpkgfile, ['2', '3', '4', '5'], '/etc')
end

#install_native_package(pkg) ⇒ Object



122
123
124
125
126
127
# File 'lib/tpkg/os/debian.rb', line 122

def install_native_package(pkg)
  pkgname = native_pkg_to_install_string(pkg)
  cmd = "#{@aptgetcmd} -y install #{pkgname}"
  puts "Running '#{cmd}' to install native package" if @debug
  system(cmd)
end

#native_pkg_to_install_string(pkg) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/tpkg/os/debian.rb', line 112

def native_pkg_to_install_string(pkg)
  name = pkg[:metadata][:name]
  version = pkg[:metadata][:version]
  package_version = pkg[:metadata][:package_version]
  pkgname = "#{name}=#{version}"
  if package_version
     pkgname << "-#{package_version}"
  end
  pkgname
end

#os_versionObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/tpkg/os/debian.rb', line 135

def os_version
  if !@os_version
    if Facter['lsbmajdistrelease'] &&
       Facter['lsbmajdistrelease'].value &&
       !Facter['lsbmajdistrelease'].value.empty?
      lsbmajdistrelease = Facter['lsbmajdistrelease'].value
      # Normal wheezy beta returns 'testing', but Raspian on the
      # Raspberry Pi returns this uglier string.  Normalize it.
      if lsbmajdistrelease == 'testing/unstable'
        lsbmajdistrelease = 'testing'
      end
      @os_version = lsbmajdistrelease
    elsif Facter['lsbdistrelease'] &&
          Facter['lsbdistrelease'].value &&
          !Facter['lsbdistrelease'].value.empty? &&
      # Work around lack of lsbmajdistrelease on older versions of Ubuntu
      # due to older version of facter.  Support for lsbmajdistrelease on
      # Ubuntu was added in facter 1.5.3, but there's no good way to force
      # older Ubuntu systems to a newer version of facter.
      @os_version = Facter['lsbdistrelease'].value.split('.').first
    end
  end
  super
end

#upgrade_native_package(pkg) ⇒ Object



128
129
130
131
132
133
# File 'lib/tpkg/os/debian.rb', line 128

def upgrade_native_package(pkg)
  pkgname = native_pkg_to_install_string(pkg)
  cmd = "#{@aptgetcmd} -y install #{pkgname}"
  puts "Running '#{cmd}' to upgrade native package" if @debug
  system(cmd)
end