Module: Unix::Pkg

Includes:
Beaker::CommandFactory
Included in:
Host
Defined in:
lib/beaker/host/unix/pkg.rb

Constant Summary collapse

DEBIAN_PLATFORM_CODENAMES =

Debian repositories contain packages for all architectures, so we need to map to an architecturless name for each platform

{
  'debian-6-amd64'     => 'squeeze',
  'debian-6-i386'      => 'squeeze',
  'debian-7-amd64'     => 'wheezy',
  'debian-7-i386'      => 'wheezy',
  'ubuntu-10.04-amd64' => 'lucid',
  'ubuntu-10.04-i386'  => 'lucid',
  'ubuntu-12.04-amd64' => 'precise',
  'ubuntu-12.04-i386'  => 'precise',
}

Instance Attribute Summary

Attributes included from Beaker::CommandFactory

#assertions

Instance Method Summary collapse

Methods included from Beaker::CommandFactory

#execute, #fail_test

Instance Method Details

#check_for_command(name) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/beaker/host/unix/pkg.rb', line 10

def check_for_command(name)
  result = exec(Beaker::Command.new("which #{name}"), :accept_all_exit_codes => true)
  case self['platform']
  when /solaris-10/
    result.stdout =~ %r|/.*/#{name}|
  else
    result.exit_code == 0
  end
end

#check_for_package(name, opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/beaker/host/unix/pkg.rb', line 20

def check_for_package(name, opts = {})
  opts = {:accept_all_exit_codes => true}.merge(opts)
  case self['platform']
    when /sles-10/
      result = execute("zypper se -i --match-exact #{name}", opts) { |result| result }
      result.stdout =~ /No packages found/ ? (return false) : (return result.exit_code == 0)
    when /sles-/
      result = execute("zypper se -i --match-exact #{name}", opts) { |result| result }
    when /el-4/
      @logger.debug("Package query not supported on rhel4")
      return false
    when /cisco|fedora|centos|eos|el-/
      result = execute("rpm -q #{name}", opts) { |result| result }
    when /ubuntu|debian|cumulus/
      result = execute("dpkg -s #{name}", opts) { |result| result }
    when /solaris-11/
      result = execute("pkg info #{name}", opts) { |result| result }
    when /solaris-10/
      result = execute("pkginfo #{name}", opts) { |result| result }
    when /freebsd-9/
      result = execute("pkg_info #{name}", opts) { |result| result }
    when /freebsd-10/
      result = execute("pkg info #{name}", opts) { |result| result }
    else
      raise "Package #{name} cannot be queried on #{self}"
  end
  result.exit_code == 0
end

#deploy_apt_repo(path, name, version) ⇒ Object

Note:

Due to the debian use of codenames in repos, the DEBIAN_PLATFORM_CODENAMES map must be kept up-to-date as support for new versions is added.

Deploy apt configuration generated by the packaging tooling



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/beaker/host/unix/pkg.rb', line 155

def deploy_apt_repo(path, name, version)
  codename = DEBIAN_PLATFORM_CODENAMES[self['platform']]
  if codename.nil?
    @logger.warning "Could not determine codename for debian platform #{self['platform']}. Skipping deployment of repo #{name}"
    return
  end

  repo_file = "#{path}/deb/pl-#{name}-#{version}-#{codename}.list"
  do_scp_to repo_file, "/etc/apt/sources.list.d/#{name}.list", {}
  @apt_needs_update = true
end

#deploy_package_repo(path, name, version) ⇒ Object

Deploy configuration generated by the packaging tooling to this host.

This method calls one of #deploy_apt_repo, #deploy_yum_repo, or #deploy_zyp_repo depending on the platform of this Host.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/beaker/host/unix/pkg.rb', line 195

def deploy_package_repo(path, name, version)
  if not File.exists? path
    @logger.warning "Was asked to deploy package repository from #{path}, but it doesn't exist!"
    return
  end

  case self['platform']
    when /el-4/
      @logger.debug("Package repo deploy is not supported on rhel4")
    when /fedora|centos|eos|el-/
      deploy_yum_repo(path, name, version)
    when /ubuntu|debian|cumulus/
      deploy_apt_repo(path, name, version)
    when /sles/
      deploy_zyp_repo(path, name, version)
    else
      # solaris, windows
      raise "Package repo cannot be deployed on #{self}; the platform is not supported"
  end
end

#deploy_yum_repo(path, name, version) ⇒ Object

Deploy yum configuration generated by the packaging tooling



171
172
173
174
# File 'lib/beaker/host/unix/pkg.rb', line 171

def deploy_yum_repo(path, name, version)
  repo_file = "#{path}/rpm/pl-#{name}-#{version}-repos-pe-#{self['platform']}.repo"
  do_scp_to repo_file, "/etc/yum.repos.d/#{name}.repo", {}
end

#deploy_zyp_repo(path, name, version) ⇒ Object

Deploy zypper repo configuration generated by the packaging tooling



180
181
182
183
184
185
186
# File 'lib/beaker/host/unix/pkg.rb', line 180

def deploy_zyp_repo(path, name, version)
  repo_file = "#{path}/rpm/pl-#{name}-#{version}-repos-pe-#{self['platform']}.repo"
  repo = IniFile.load(repo_file)
  repo_name = repo.sections[0]
  repo_url = repo[repo_name]["baseurl"]
  execute("zypper ar -t YUM #{repo_url} #{repo_name}")
end

#determine_if_x86_64Boolean

Examine the host system to determine the architecture

Returns:

  • (Boolean)

    true if x86_64, false otherwise



218
219
220
221
222
223
224
225
226
# File 'lib/beaker/host/unix/pkg.rb', line 218

def determine_if_x86_64
  if self[:platform] =~ /solaris/
    result = exec(Beaker::Command.new("uname -a | grep x86_64"), :accept_all_exit_codes => true)
      result.exit_code == 0
  else
    result = exec(Beaker::Command.new("arch | grep x86_64"), :accept_all_exit_codes => true)
    result.exit_code == 0
  end
end

#install_package(name, cmdline_args = '', version = nil, opts = {}) ⇒ Object



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
# File 'lib/beaker/host/unix/pkg.rb', line 60

def install_package(name, cmdline_args = '', version = nil, opts = {})
  case self['platform']
    when /sles-/
      execute("zypper --non-interactive in #{name}", opts)
    when /el-4/
      @logger.debug("Package installation not supported on rhel4")
    when /cisco|fedora|centos|eos|el-/
      if version
        name = "#{name}-#{version}"
      end
      execute("yum -y #{cmdline_args} install #{name}", opts)
    when /ubuntu|debian|cumulus/
      if version
        name = "#{name}=#{version}"
      end
      update_apt_if_needed
      execute("apt-get install --force-yes #{cmdline_args} -y #{name}", opts)
    when /solaris-11/
      execute("pkg #{cmdline_args} install #{name}", opts)
    when /solaris-10/
      execute("pkgutil -i -y #{cmdline_args} #{name}", opts)
    when /freebsd-9/
      execute("pkg_add -fr #{cmdline_args} #{name}", opts)
    when /freebsd-10/
      execute("pkg #{cmdline_args} install #{name}", opts)
    else
      raise "Package #{name} cannot be installed on #{self}"
  end
end

#pkg_initializeObject

This method overrides Beaker::Host#pkg_initialize to provide unix-specific package management setup



6
7
8
# File 'lib/beaker/host/unix/pkg.rb', line 6

def pkg_initialize
  @apt_needs_update = true
end

#uninstall_package(name, cmdline_args = '', opts = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/beaker/host/unix/pkg.rb', line 90

def uninstall_package(name, cmdline_args = '', opts = {})
  case self['platform']
    when /sles-/
      execute("zypper --non-interactive rm #{name}", opts)
    when /el-4/
      @logger.debug("Package uninstallation not supported on rhel4")
    when /cisco|fedora|centos|eos|el-/
      execute("yum -y #{cmdline_args} remove #{name}", opts)
    when /ubuntu|debian|cumulus/
      execute("apt-get purge #{cmdline_args} -y #{name}", opts)
    when /solaris-11/
      execute("pkg #{cmdline_args} uninstall #{name}", opts)
    when /solaris-10/
      execute("pkgutil -r -y #{cmdline_args} #{name}", opts)
    else
      raise "Package #{name} cannot be installed on #{self}"
  end
end

#update_apt_if_neededObject

If apt has not been updated since the last repo deployment it is updated. Otherwise this is a noop



51
52
53
54
55
56
57
58
# File 'lib/beaker/host/unix/pkg.rb', line 51

def update_apt_if_needed
  if self['platform'] =~ /debian|ubuntu|cumulus/
    if @apt_needs_update
      execute("apt-get update")
      @apt_needs_update = false
    end
  end
end

#upgrade_package(name, cmdline_args = '', opts = {}) ⇒ Object

Upgrade an installed package to the latest available version

Parameters:

  • name (String)

    The name of the package to update

  • cmdline_args (String) (defaults to: '')

    Additional command line arguments for the package manager



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/beaker/host/unix/pkg.rb', line 114

def upgrade_package(name, cmdline_args = '', opts = {})
  case self['platform']
    when /sles-/
      execute("zypper --non-interactive --no-gpg-checks up #{name}", opts)
    when /el-4/
      @logger.debug("Package upgrade is not supported on rhel4")
    when /cisco|fedora|centos|eos|el-/
      execute("yum -y #{cmdline_args} update #{name}", opts)
    when /ubuntu|debian|cumulus/
      update_apt_if_needed
      execute("apt-get install -o Dpkg::Options::='--force-confold' #{cmdline_args} -y --force-yes #{name}", opts)
    when /solaris-11/
      execute("pkg #{cmdline_args} update #{name}", opts)
    when /solaris-10/
      execute("pkgutil -u -y #{cmdline_args} ${name}", opts)
    else
      raise "Package #{name} cannot be upgraded on #{self}"
  end
end