Class: Tpkg::OS::FreeBSD

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tpkg::OS

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

Constructor Details

#initialize(options = {}) ⇒ FreeBSD

Returns a new instance of FreeBSD.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/tpkg/os/freebsd.rb', line 13

def initialize(options={})
  @pkginfocmd = options[:pkginfocmd] || options[:testcmd] || 'pkg_info'
  @pkgaddcmd = options[:pkgaddcmd] || options[:testcmd] || 'pkg_add'
  @pkgdeletecmd = options[:pkgdeletecmd] || options[:testcmd] || 'pkg_delete'
  # pkg_add -r defaults to the Latest/ directory, which just has generic
  # versionless symlinks like curl.tbz.  We want to use the All/ directory
  # so that we can specify a versioned file like curl-7.24.0.tbz.
  @packagesite = options[:packagesite] ||
    'ftp://ftp.freebsd.org/pub/FreeBSD/ports/<%= arch %>/packages-<%= os_version %>-stable/All/'
  super
end

Class Method Details

.supported?Boolean

Returns:

  • (Boolean)


7
8
9
10
# File 'lib/tpkg/os/freebsd.rb', line 7

def self.supported?
  Facter.loadfacts
  Facter['operatingsystem'].value == 'FreeBSD'
end

Instance Method Details

#available_native_packages(pkgname) ⇒ 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
# File 'lib/tpkg/os/freebsd.rb', line 42

def available_native_packages(pkgname)
  native_packages = []
  cmd = "#{@pkginfocmd} -E #{pkgname}-*"
  puts "available_native_packages running '#{cmd}'" if @debug
  Open3.popen3(cmd) do |stdin, stdout, stderr|
    stdin.close
    stdout.each_line do |line|
      fbversion = line.sub("#{pkgname}-", '').chomp
      # Seems to be FreeBSD convention that if the package has a
      # package version you seperate that from the upstream version
      # with an underscore.
      version, package_version = fbversion.split('_', 2)
      native_packages <<
        Tpkg.pkg_for_native_package(
          pkgname, version, package_version, :native_installed)
    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 match'
      raise "available_native_packages error running pkg_info"
    end
  end
  # FIXME: FreeBSD available packages
  # We could either poke around in the ports tree (if installed), or
  # try to recreate the URL "pkg_add -r" would use and pull a
  # directory listing.
  native_packages
end


32
33
34
35
36
37
38
39
40
41
# File 'lib/tpkg/os/freebsd.rb', line 32

def init_links(installed_path, tpkgfile)
  links = []
  init_directory = '/usr/local/etc/rc.d'
  if tpkgfile[:init][:levels] && tpkgfile[:init][:levels].empty?
    # User doesn't want the init script linked in to auto-start
  else
    links << File.join(init_directory, File.basename(installed_path))
  end
  links
end

#install_native_package(pkg) ⇒ Object



83
84
85
86
87
88
# File 'lib/tpkg/os/freebsd.rb', line 83

def install_native_package(pkg)
  pkgname = native_pkg_to_install_string(pkg)
  cmd = "PACKAGESITE=#{packagesite} #{@pkgaddcmd} -r #{pkgname}"
  puts "Running '#{cmd}' to install native package" if @debug
  system('sh', '-c', cmd)
end

#native_pkg_to_install_string(pkg) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/tpkg/os/freebsd.rb', line 73

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



105
106
107
108
109
110
111
112
# File 'lib/tpkg/os/freebsd.rb', line 105

def os_version
  if !@os_version
    # Extract 7 from 7.1-RELEASE, for example
    fbver = Facter['operatingsystemrelease'].value
    @os_version = fbver.split('.').first
  end
  super
end

#packagesiteObject



25
26
27
28
29
30
# File 'lib/tpkg/os/freebsd.rb', line 25

def packagesite
  url = ERB.new(@packagesite).result(binding)
  # pkg_add expects the URL to end with a /
  url << '/' if (url[-1] != '/')
  url
end

#upgrade_native_package(pkg) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tpkg/os/freebsd.rb', line 89

def upgrade_native_package(pkg)
  pkgname = native_pkg_to_install_string(pkg)
  # This is not very ideal.  It would be better to download the
  # new package, and if the download is successful remove the
  # old package and install the new one.  The way we're doing it
  # here we risk leaving the system with neither version
  # installed if the download of the new package fails.
  # However, the FreeBSD package tools don't make it easy to
  # handle things properly.
  deletecmd = "#{@pkgdeletecmd} #{pkgname}"
  addcmd = "PACKAGESITE=#{packagesite} #{@pkgaddcmd} -r #{pkgname}"
  puts "Running '#{deletecmd}' and '#{addcmd}' to upgrade native package" if @debug
  system(deletecmd)
  system('sh', '-c', addcmd)
end