24
24
has_feature :upgradeable
25
25
has_feature :supports_flavors
26
26
27
+ mk_resource_methods
28
+
27
29
def self . instances
28
30
packages = [ ]
29
31
@@ -46,12 +48,6 @@ def self.instances
46
48
47
49
packages << new ( hash )
48
50
hash = { }
49
- else
50
- unless line =~ /Updating the pkgdb/
51
- # Print a warning on lines we can't match, but move
52
- # on, since it should be non-fatal
53
- warning ( _ ( "Failed to match line %{line}" ) % { line : line } )
54
- end
55
51
end
56
52
}
57
53
end
@@ -67,26 +63,17 @@ def self.listcmd
67
63
end
68
64
69
65
def latest
70
- parse_pkgconf
71
-
72
- if @resource [ :source ] [ -1 , 1 ] == ::File ::SEPARATOR
73
- e_vars = { 'PKG_PATH' => @resource [ :source ] }
74
- else
75
- e_vars = { }
76
- end
77
-
78
66
if @resource [ :flavor ]
79
67
query = "#{ @resource [ :name ] } --#{ @resource [ :flavor ] } "
80
68
else
81
- query = @resource [ :name ]
69
+ query = @resource [ :name ] + "--"
82
70
end
83
71
84
- output = Puppet ::Util . withenv ( e_vars ) { pkginfo "-Q" , query }
85
- version = properties [ :ensure ]
72
+ output = Puppet ::Util . withenv ( { } ) { pkginfo "-Q" , query }
86
73
87
74
if output . nil? or output . size == 0 or output =~ /Error from /
88
75
debug "Failed to query for #{ resource [ :name ] } "
89
- return version
76
+ return properties [ :ensure ]
90
77
else
91
78
# Remove all fuzzy matches first.
92
79
output = output . split . select { |p | p =~ /^#{ resource [ :name ] } -(\d [^-]*)-?(\w *)/ } . join
@@ -95,21 +82,21 @@ def latest
95
82
96
83
if output =~ /^#{ resource [ :name ] } -(\d [^-]*)-?(\w *) \( installed\) $/
97
84
debug "Package is already the latest available"
98
- version
85
+ return properties [ :ensure ]
99
86
else
100
87
match = /^(.*)-(\d [^-]*)-?(\w *)$/ . match ( output )
101
88
debug "Latest available for #{ resource [ :name ] } : #{ match [ 2 ] } "
102
89
103
- if version . to_sym == :absent || version . to_sym == :purged
90
+ if properties [ :ensure ] . to_sym == :absent
104
91
return match [ 2 ]
105
92
end
106
93
107
- vcmp = version . split ( '.' ) . map ( & : to_i) <=> match [ 2 ] . split ( '.' ) . map ( & : to_i)
94
+ vcmp = properties [ :ensure ] . split ( '.' ) . map { | s | s . to_i } <=> match [ 2 ] . split ( '.' ) . map { | s | s . to_i }
108
95
if vcmp > 0
109
96
# The locally installed package may actually be newer than what a mirror
110
97
# has. Log it at debug, but ignore it otherwise.
111
- debug "Package #{ resource [ :name ] } #{ version } newer then available #{ match [ 2 ] } "
112
- version
98
+ debug "Package #{ resource [ :name ] } #{ properties [ :ensure ] } newer then available #{ match [ 2 ] } "
99
+ return properties [ :ensure ]
113
100
else
114
101
match [ 2 ]
115
102
end
@@ -120,57 +107,25 @@ def update
120
107
install ( true )
121
108
end
122
109
123
- def parse_pkgconf
124
- unless @resource [ :source ]
125
- if Puppet ::FileSystem . exist? ( "/etc/pkg.conf" )
126
- File . open ( "/etc/pkg.conf" , "rb" ) . readlines . each do |line |
127
- matchdata = line . match ( /^installpath\s *=\s *(.+)\s *$/i )
128
- if matchdata
129
- @resource [ :source ] = matchdata [ 1 ]
130
- else
131
- matchdata = line . match ( /^installpath\s *\+ =\s *(.+)\s *$/i )
132
- if matchdata
133
- if @resource [ :source ] . nil?
134
- @resource [ :source ] = matchdata [ 1 ]
135
- else
136
- @resource [ :source ] += ":" + matchdata [ 1 ]
137
- end
138
- end
139
- end
140
- end
141
-
142
- unless @resource [ :source ]
143
- raise Puppet ::Error ,
144
- _ ( "No valid installpath found in /etc/pkg.conf and no source was set" )
145
- end
146
- else
147
- raise Puppet ::Error ,
148
- _ ( "You must specify a package source or configure an installpath in /etc/pkg.conf" )
149
- end
150
- end
151
- end
152
-
153
110
def install ( latest = false )
154
111
cmd = [ ]
155
112
156
- parse_pkgconf
157
-
158
- if @resource [ :source ] [ -1 , 1 ] == ::File ::SEPARATOR
159
- e_vars = { 'PKG_PATH' => @resource [ :source ] }
160
- full_name = get_full_name ( latest )
161
- else
162
- e_vars = { }
163
- full_name = @resource [ :source ]
164
- end
165
-
113
+ cmd << '-r'
166
114
cmd << install_options
167
- cmd << full_name
115
+ cmd << get_full_name ( latest )
168
116
169
117
if latest
170
- cmd . unshift ( '-rz ' )
118
+ cmd . unshift ( '-z ' )
171
119
end
172
120
173
- Puppet ::Util . withenv ( e_vars ) { pkgadd cmd . flatten . compact }
121
+ # pkg_add(1) doesn't set the return value upon failure so we have to peek
122
+ # at it's output to see if something went wrong.
123
+ output = Puppet ::Util . withenv ( { } ) { pkgadd cmd . flatten . compact }
124
+ require 'pp'
125
+ pp output
126
+ if output =~ /Can't find /
127
+ self . fail "pkg_add returned: #{ output . chomp } "
128
+ end
174
129
end
175
130
176
131
def get_full_name ( latest = false )
@@ -179,11 +134,20 @@ def get_full_name(latest = false)
179
134
# installing with 'latest', we do need to handle the flavors. This is
180
135
# done so we can feed pkg_add(8) the full package name to install to
181
136
# prevent ambiguity.
182
- if latest && resource [ :flavor ]
183
- "#{ resource [ :name ] } --#{ resource [ :flavor ] } "
184
- elsif latest
185
- # Don't depend on get_version for updates.
186
- @resource [ :name ]
137
+ if resource [ :flavor ]
138
+ # If :ensure contains a version, use that instead of looking it up.
139
+ # This allows for installing packages with the same stem, but multiple
140
+ # version such as postfix-VERSION-flavor.
141
+ if @resource [ :ensure ] . to_s =~ /(\d [^-]*)$/
142
+ use_version = @resource [ :ensure ]
143
+ else
144
+ use_version = ''
145
+ end
146
+ "#{ resource [ :name ] } -#{ use_version } -#{ resource [ :flavor ] } "
147
+ elsif resource [ :name ] . to_s . match ( /[a-z0-9]%[0-9a-z]/i )
148
+ "#{ resource [ :name ] } "
149
+ elsif not latest
150
+ "#{ resource [ :name ] } --"
187
151
else
188
152
# If :ensure contains a version, use that instead of looking it up.
189
153
# This allows for installing packages with the same stem, but multiple
@@ -194,33 +158,41 @@ def get_full_name(latest = false)
194
158
use_version = get_version
195
159
end
196
160
197
- [ @resource [ :name ] , use_version , @resource [ :flavor ] ] . join ( '-' ) . gsub ( /-+$/ , '' )
161
+ if resource [ :flavor ]
162
+ [ @resource [ :name ] , use_version , @resource [ :flavor ] ] . join ( '-' ) . gsub ( /-+$/ , '' )
163
+ else
164
+ [ @resource [ :name ] , use_version ]
165
+ end
198
166
end
199
167
end
200
168
201
169
def get_version
202
- execpipe ( [ command ( :pkginfo ) , "-I" , @resource [ :name ] ] ) do |process |
203
- # our regex for matching pkg_info output
204
- regex = /^(.*)-(\d [^-]*)-?(\w *)(.*)$/
205
- master_version = 0
206
- version = -1
207
-
208
- process . each_line do |line |
209
- match = regex . match ( line . split [ 0 ] )
210
- next unless match
211
-
170
+ pkg_search_name = @resource [ :name ]
171
+ unless pkg_search_name . match ( /[a-z0-9]%[0-9a-z]/i ) and not @resource [ :flavor ]
172
+ # we are only called when no flavor is specified
173
+ # so append '--' to the :name to avoid patch versions on flavors
174
+ pkg_search_name << "--"
175
+ end
176
+ # our regex for matching pkg_info output
177
+ regex = /^(.*)-(\d [^-]*)[-]?(\w *)(.*)$/
178
+ master_version = 0
179
+ version = -1
180
+
181
+ # pkg_info -I might return multiple lines, i.e. flavors
182
+ matching_pkgs = pkginfo ( "-I" , "pkg_search_name" )
183
+ matching_pkgs . each_line do |line |
184
+ if match = regex . match ( line . split [ 0 ] )
212
185
# now we return the first version, unless ensure is latest
213
186
version = match . captures [ 1 ]
214
187
return version unless @resource [ :ensure ] == "latest"
215
-
216
188
master_version = version unless master_version > version
217
189
end
218
-
219
- return master_version unless master_version == 0
220
- return '' if version == -1
221
-
222
- raise Puppet ::Error , _ ( "%{version} is not available for this package" ) % { version : version }
223
190
end
191
+
192
+ return master_version unless master_version == 0
193
+ return '' if version == -1
194
+ raise Puppet ::Error , _ ( "%{version} is not available for this package" ) % { version : version }
195
+
224
196
rescue Puppet ::ExecutionFailure
225
197
nil
226
198
end
@@ -239,7 +211,7 @@ def install_options
239
211
end
240
212
241
213
def uninstall_options
242
- join_options ( resource [ :uninstall_options ] )
214
+ [ join_options ( resource [ :uninstall_options ] ) ]
243
215
end
244
216
245
217
def uninstall
0 commit comments