1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef/provider/package'
require 'chef/mixin/command'
require 'chef/resource/package'
class Chef
class Provider
class Package
class Apt < Chef::Provider::Package
provides :apt_package, os: "linux"
attr_accessor :is_virtual_package
def load_current_resource
@current_resource = Chef::Resource::Package.new(@new_resource.name)
@current_resource.package_name(@new_resource.package_name)
check_package_state(@new_resource.package_name)
@current_resource
end
def define_resource_requirements
super
requirements.assert(:all_actions) do |a|
a.assertion { !@new_resource.source }
a.failure_message(Chef::Exceptions::Package, 'apt package provider cannot handle source attribute. Use dpkg provider instead')
end
end
def default_release_options
# Use apt::Default-Release option only if provider supports it
"-o APT::Default-Release=#{@new_resource.default_release}" if @new_resource.respond_to?(:default_release) && @new_resource.default_release
end
def check_package_state(package)
final_installed_version = []
final_candidate_version = []
final_installed = []
final_virtual = []
installed = virtual = false
installed_version = candidate_version = nil
[package].flatten.each do |pkg|
installed = virtual = false
installed_version = candidate_version = nil
shell_out!("apt-cache#{expand_options(default_release_options)} policy #{pkg}", {:timeout=>900}).stdout.each_line do |line|
case line
when /^\s{2}Installed: (.+)$/
installed_version = $1
if installed_version == '(none)'
Chef::Log.debug("#{@new_resource} current version is nil")
installed_version = nil
else
Chef::Log.debug("#{@new_resource} current version is #{installed_version}")
installed = true
end
when /^\s{2}Candidate: (.+)$/
candidate_version = $1
if candidate_version == '(none)'
# This may not be an appropriate assumption, but it shouldn't break anything that already worked -- btm
virtual = true
showpkg = shell_out!("apt-cache showpkg #{package}", {:timeout => 900}).stdout
providers = Hash.new
showpkg.rpartition(/Reverse Provides: ?#{$/}/)[2].each_line do |line|
provider, version = line.split
providers[provider] = version
end
# Check if the package providing this virtual package is installed
num_providers = providers.length
raise Chef::Exceptions::Package, "#{@new_resource.package_name} has no candidate in the apt-cache" if num_providers == 0
# apt will only install a virtual package if there is a single providing package
raise Chef::Exceptions::Package, "#{@new_resource.package_name} is a virtual package provided by #{num_providers} packages, you must explicitly select one to install" if num_providers > 1
# Check if the package providing this virtual package is installed
Chef::Log.info("#{@new_resource} is a virtual package, actually acting on package[#{providers.keys.first}]")
installed = check_package_state(providers.keys.first)
else
Chef::Log.debug("#{@new_resource} candidate version is #{$1}")
end
end
end
if package.is_a?(Array)
final_installed_version << installed_version
final_candidate_version << candidate_version
final_installed << installed
final_virtual << virtual
else
final_installed_version = installed_version
final_candidate_version = candidate_version
final_installed = installed
final_virtual = virtual
end
end
@candidate_version = final_candidate_version
@current_resource.version(final_installed_version)
@is_virtual_package = final_virtual
return final_installed.is_a?(Array) ? final_installed.any? : final_installed
end
def install_package(name, version)
if name.is_a?(Array)
index = 0
package_name = name.zip(version).map do |x, y|
namestr = nil
if @is_virtual_package[index]
namestr = x
else
namestr = "#{x}=#{y}"
end
index += 1
namestr
end.join(' ')
else
package_name = "#{name}=#{version}"
package_name = name if @is_virtual_package
end
run_noninteractive("apt-get -q -y#{expand_options(default_release_options)}#{expand_options(@new_resource.options)} install #{package_name}")
end
def upgrade_package(name, version)
install_package(name, version)
end
def remove_package(name, version)
if name.is_a?(Array)
package_name = name.join(' ')
else
package_name = name
end
run_noninteractive("apt-get -q -y#{expand_options(@new_resource.options)} remove #{package_name}")
end
def purge_package(name, version)
if name.is_a?(Array)
package_name = name.join(' ')
else
package_name = "#{name}"
end
run_noninteractive("apt-get -q -y#{expand_options(@new_resource.options)} purge #{package_name}")
end
def preseed_package(preseed_file)
Chef::Log.info("#{@new_resource} pre-seeding package installation instructions")
run_noninteractive("debconf-set-selections #{preseed_file}")
end
def reconfig_package(name, version)
if name.is_a?(Array)
package_name = name.join(' ')
else
package_name = "#{name}"
end
Chef::Log.info("#{@new_resource} reconfiguring")
run_noninteractive("dpkg-reconfigure #{package_name}")
end
private
# Runs command via shell_out with magic environment to disable
# interactive prompts. Command is run with default localization rather
# than forcing locale to "C", so command output may not be stable.
def run_noninteractive(command)
shell_out!(command, :env => { "DEBIAN_FRONTEND" => "noninteractive", "LC_ALL" => nil }, :timeout => @new_resource.timeout)
end
end
end
end
end
|