forked from chef/omnibus
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpkg.rb
More file actions
334 lines (296 loc) · 9.17 KB
/
pkg.rb
File metadata and controls
334 lines (296 loc) · 9.17 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#
# Copyright 2014 Chef Software, Inc.
#
# 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.
#
module Omnibus
class Packager::PKG < Packager::Base
# @return [Hash]
SCRIPT_MAP = {
# Default Omnibus naming
preinst: "preinstall",
postinst: "postinstall",
# Default PKG naming
preinstall: "preinstall",
postinstall: "postinstall",
}.freeze
id :pkg
setup do
# Create the resources directory
create_directory(resources_dir)
# Create the scripts directory
create_directory(scripts_dir)
# Render the license
render_template(resource_path("license.html.erb"),
destination: "#{resources_dir}/license.html",
variables: {
name: project.name,
friendly_name: project.friendly_name,
maintainer: project.maintainer,
build_version: project.build_version,
package_name: project.package_name,
})
# Render the welcome template
render_template(resource_path("welcome.html.erb"),
destination: "#{resources_dir}/welcome.html",
variables: {
name: project.name,
friendly_name: project.friendly_name,
maintainer: project.maintainer,
build_version: project.build_version,
package_name: project.package_name,
})
# "Render" the assets
copy_file(resource_path("background.png"), "#{resources_dir}/background.png")
end
build do
write_scripts
build_component_pkg
write_distribution_file
build_product_pkg
end
#
# @!group DSL methods
# --------------------------------------------------
#
# The identifer for the PKG package.
#
# @example
# identifier 'com.getchef.chefdk'
#
# @param [String] val
# the package identifier
#
# @return [String]
#
def identifier(val = NULL)
if null?(val)
@identifier
else
@identifier = val
end
end
expose :identifier
#
# Set or return the signing identity. If this value is provided, Omnibus
# will attempt to sign the PKG.
#
# @example
# signing_identity "foo"
#
# @param [String] val
# the identity to use when signing the PKG
#
# @return [String]
# the PKG-signing identity
#
def signing_identity(val = NULL)
if null?(val)
@signing_identity
else
@signing_identity = val
end
end
expose :signing_identity
#
# @!endgroup
# --------------------------------------------------
# @see Base#package_name
def package_name
"#{safe_base_package_name}-#{safe_version}-#{safe_build_iteration}.pkg"
end
#
# The full path where the product package was/will be written.
#
# @return [String]
#
def final_pkg
File.expand_path("#{Config.package_dir}/#{package_name}")
end
#
# The path where the product package resources will live. We cannot store
# resources in the top-level staging dir, because +productbuild+'s
# +--resources+ flag expects a directory that does not contain the parent
# package.
#
# @return [String]
#
def resources_dir
File.expand_path("#{staging_dir}/Resources")
end
#
# The path where the package scripts will live. We cannot store
# scripts in the top-level staging dir, because +pkgbuild+'s
# +--scripts+ flag expects a directory that does not contain the parent
# package.
#
# @return [String]
#
def scripts_dir
File.expand_path("#{staging_dir}/Scripts")
end
#
# Copy all scripts in {Project#package_scripts_path} to the package
# directory.
#
# @return [void]
#
def write_scripts
SCRIPT_MAP.each do |source, destination|
source_path = File.join(project.package_scripts_path, source.to_s)
if File.file?(source_path)
destination_path = File.join(scripts_dir, destination)
log.debug(log_key) { "Adding script `#{source}' to `#{destination_path}'" }
copy_file(source_path, destination_path)
end
end
end
#
# Construct the intermediate build product. It can be installed with the
# Installer.app, but doesn't contain the data needed to customize the
# installer UI.
#
# @return [void]
#
def build_component_pkg
command = <<-EOH.gsub(/^ {8}/, "")
pkgbuild \\
--identifier "#{safe_identifier}" \\
--version "#{safe_version}" \\
--scripts "#{scripts_dir}" \\
--root "#{project.install_dir}" \\
--install-location "#{project.install_dir}" \\
"#{component_pkg}"
EOH
Dir.chdir(staging_dir) do
shellout!(command)
end
end
#
# Write the Distribution file to the staging area. This method generates the
# content of the Distribution file, which is used by +productbuild+ to
# select the component packages to include in the product package.
#
# It also includes information used to customize the UI of the Mac OS X
# installer.
#
# @return [void]
#
def write_distribution_file
render_template(resource_path("distribution.xml.erb"),
destination: "#{staging_dir}/Distribution",
mode: 0600,
variables: {
friendly_name: project.friendly_name,
identifier: safe_identifier,
version: safe_version,
component_pkg: component_pkg,
host_architecture: safe_architecture,
})
end
#
# Construct the product package. The generated package is the final build
# product that is shipped to end users.
#
# @return [void]
#
def build_product_pkg
command = <<-EOH.gsub(/^ {8}/, "")
productbuild \\
--distribution "#{staging_dir}/Distribution" \\
--resources "#{resources_dir}" \\
EOH
command << %Q{ --sign "#{signing_identity}" \\\n} if signing_identity
command << %Q{ "#{final_pkg}"}
command << %Q{\n}
Dir.chdir(staging_dir) do
shellout!(command)
end
end
#
# The name of the (only) component package.
#
# @return [String] the filename of the component .pkg file to create.
#
def component_pkg
"#{safe_base_package_name}-core.pkg"
end
#
# Return the architecture
#
# @return [String]
#
def safe_architecture
@safe_architecture ||= Ohai["kernel"]["machine"]
end
#
# Return the PKG-ready base package name, removing any invalid characters.
#
# @return [String]
#
def safe_base_package_name
if project.package_name =~ /\A[[:alnum:]-]+\z/
project.package_name.dup
else
converted = project.package_name.downcase.gsub(/[^[:alnum:]+]/, "")
log.warn(log_key) do
"The `name' component of Mac package names can only include " \
"alphabetical characters (a-z, A-Z), numbers (0-9), and -. Converting " \
"`#{project.package_name}' to `#{converted}'."
end
converted
end
end
#
# The identifier for this mac package (the com.whatever.thing.whatever).
# This is a configurable project value, but a default value is calculated if
# one is not given.
#
# @return [String]
#
def safe_identifier
return identifier if identifier
maintainer = project.maintainer.gsub(/[^[:alnum:]+]/, "").downcase
"test.#{maintainer}.pkg.#{safe_base_package_name}"
end
#
# This is actually just the regular build_iternation, but it felt lonely
# among all the other +safe_*+ methods.
#
# @return [String]
#
def safe_build_iteration
project.build_iteration
end
#
# Return the PKG-ready version, converting any invalid characters to
# dashes (+-+).
#
# @return [String]
#
def safe_version
if project.build_version =~ /\A[a-zA-Z0-9\.\+\-]+\z/
project.build_version.dup
else
converted = project.build_version.gsub(/[^a-zA-Z0-9\.\+\-]+/, "-")
log.warn(log_key) do
"The `version' component of Mac package names can only include " \
"alphabetical characters (a-z, A-Z), numbers (0-9), dots (.), " \
"plus signs (+), and dashes (-). Converting " \
"`#{project.build_version}' to `#{converted}'."
end
converted
end
end
end
end