/
functions
executable file
·576 lines (457 loc) · 14.4 KB
/
functions
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#!/bin/sh
__sm.package.gnu.install()
{
trace_filter package || set -o xtrace
(( $# == 0 )) || __sm.package.cli $@
__sm.package.set.cflags
__sm.package.set.cpath
__sm.package.set.cc
__sm.package.set.ldflags
if __sm.package.versions.installed | grep -q "${package_version}" && (( force_flag != 1 ))
then
log "The ${package_name} ${package_version} package is already installed.
To force it to reinstall, call:\n\tsm ${package_name} package install force\n"
return 0
fi
paths create "${source_path}"
paths enter "${source_path}"
if command exists "${package_name}_dependencies"
then
__sm.log.step "${package_name} ${package_version} ~ dependencies"
"${package_name}_dependencies"
__sm.log.step success
else
__sm.log.step "${package_name} ${package_version} ~ dependencies"
__sm.package.dependencies
__sm.log.step success
fi
if command exists "${package_name}_prefetch"
then
__sm.log.step "${package_name} ${package_version} ~ prefetch"
"${package_name}_prefetch"
__sm.log.step success
fi
if command exists "${package_name}_fetch"
then
__sm.log.step "${package_name} ${package_version} ~ fetch"
"${package_name}_fetch"
__sm.log.step success
else
__sm.log.step "${package_name} ${package_version} ~ fetch"
__sm.package.fetch
__sm.log.step success
fi
paths enter "${source_path}/${package_dir}"
if command exists "${package_name}_postfetch"
then
__sm.log.step "${package_name} ${package_version} ~ postfetch"
"${package_name}_postfetch"
__sm.log.step success
fi
if command exists "${package_name}_patch"
then
__sm.log.step "${package_name} ${package_version} ~ patch"
"${package_name}_patch"
__sm.log.step success
else
__sm.log.step "${package_name} ${package_version} ~ patch"
__sm.package.patch
__sm.log.step success
fi
if command exists "${package_name}_preconfigure"
then
__sm.log.step "${package_name} ${package_version} ~ preconfigure"
"${package_name}_preconfigure"
__sm.log.step success
fi
if command exists "${package_name}_configure"
then
"${package_name}_configure"
else
__sm.log.step "${package_name} ${package_version} ~ configure"
__sm.package.configure
__sm.log.step success
fi
if command exists "${package_name}_postconfigure"
then
__sm.log.step "${package_name} ${package_version} ~ postconfigure"
"${package_name}_postconfigure"
__sm.log.step success
fi
if command exists "${package_name}_build"
then
__sm.log.step "${package_name} ${package_version} ~ build"
"${package_name}_build"
__sm.log.step success
else
__sm.log.step "${package_name} ${package_version} ~ build"
__sm.package.gnu.make
__sm.log.step success
fi
if command exists "${package_name}_preinstall"
then
__sm.log.step "${package_name} ${package_version} ~ preinstall"
"${package_name}_preinstall"
__sm.log.step success
else
__sm.log.step "${package_name} ${package_version} ~ preinstall"
__sm.package.preinstall
__sm.log.step success
fi
versionedfs add \
path "${packages_path}" name "${package_name}" version "${package_version}" \
source "${install_path}"
if command exists "${package_name}_install"
then
__sm.log.step "${package_name} ${package_version} ~ install"
"${package_name}_install"
__sm.log.step success
else
__sm.log.step "${package_name} ${package_version} ~ install"
__sm.package.make.install
__sm.log.step success
fi
files link symbolic force \
from "${source_path}/${package_dir}" \
to "${install_path}/src"
if command exists "${package_name}_postinstall"
then
__sm.log.step "${package_name} ${package_version} ~ postinstall"
"${package_name}_postinstall"
__sm.log.step success
else
__sm.log.step "${package_name} ${package_version} ~ postinstall"
__sm.package.postinstall
__sm.log.step success
fi
if ! __sm.string.contains.word '__sm.package.update' "${FUNCNAME[*]}"
then
__sm.package.activate.first "${package_name}" "${package_version}"
fi
if command exists "${package_name}_postactivate"
then
__sm.log.step "${package_name} ${package_version} ~ postactivate"
"${package_name}_postactivate"
__sm.log.step success
fi
__sm.package.setup
}
__sm.package.binary.install()
{
trace_filter package || set -o xtrace
NIY "Binary package strategy"
# TODO: fetch, extract, copy to install path.
}
__sm.package.java.install()
{
trace_filter package || set -o xtrace
NIY "Java package strategy"
# TODO: fetch, extract, copy to install path, setup java launcher.
}
__sm.package.fetch.checksum()
{
trace_filter package || set -o xtrace
typeset download_url
(( $# == 0 )) || __sm.package.cli $@
true "${package_file:="${package_name}-${package_version}.${archive_format}"}"
if [[ -z "${package_checksum_url:-}" ]]
then
for download_url in ${=package_base_url}
do
package_checksum_url="${package_checksum_url:-} ${download_url}/${package_file}.checksum"
done
fi
[[ -n "${package_checksum}" ]] || __sm.config.read "${package_name}" checksum "${package_file}" package_checksum
[[ -n "${package_checksum}" ]] ||
for download_url in ${=package_checksum_url}
do
if [[ -f "${download_url}" ]]
then
__sm.file.copy "${download_url}" "${archives_path}/${package_file}" -f
else
# TODO: we can not relay on downloaded checksum, validate with another source ?
${curl} -L "${download_url}" -o "${archives_path}/${package_file}.checksum" 2>/dev/null || return 1
fi
package_checksum=$(cat "${archives_path}/${package_file}.checksum")
if [[ -f "${archives_path}/${package_file}.checksum" ]]
then rm "${archives_path}/${package_file}.checksum"
fi
if (( ${#package_checksum} == 32 ))
then
return 0
else
package_checksum=""
fi
done
}
__sm.package.fetch()
{
trace_filter package || set -o xtrace
typeset _entries _url
typeset -a _args
(( $# == 0 )) || __sm.package.cli $@
true "${package_file:="${package_name}-${package_version}.${archive_format}"}"
if [[ -z "${package_url:-}" ]]
then
for _url in ${=package_base_url}
do
package_url="${package_url:-} ${_url}/${package_file}"
done
fi
if [[ -n "${scm_type:-}" ]]
then
_args+=( "scm_type" "${scm_type}" )
fi
__sm.package.fetch.checksum
if [[ -n "${package_checksum:-}" ]]
then _args+=( "checksum" "${package_checksum}" )
fi
fetch_uris "${package_url}" "${source_path}/${package_dir}" ${_args[@]} || return 1
[[ -z "${__vcs_failed}" ]] || return 1
[[ -d "${source_path}/${package_dir}/${package_dir}" ]] || return 0
mv "${source_path}/${package_dir}/${package_dir}"/* "${source_path}/${package_dir}/"
rm -rf "${source_path}/${package_dir}/${package_dir}/"
}
__sm.package.gnu.update()
{
trace_filter package || set -o xtrace
(( $# == 0 )) || __sm.package.cli $@
# TODO: check if newer version exists, if so then...
if __sm.package.versions.installed | grep -q "${package_version}" \
&& [[ "${force_flag}" != "1" ]]
then
log "The latest version (${package_version}) of the ${package_name} package is already installed."
else
__sm.package.${package_strategy:=gnu}.install \
${package_name} ${package_version:-} ${archive_format:-} force
__sm.package.activate ${package_name} ${package_version}
fi
}
__sm.package.configure()
{
trace_filter package || set -o xtrace
typeset _command
typeset -x PKG_CONFIG_PATH PREFIX
command exists gcc || command exists cc || __sm.log.fail "No C compiler found."
_command="${configure_command:-./configure}"
if [[ ! -x "${_command%% *}" ]]
then
__sm.log.debug package "Configure command '${_command%% *}' not available."
return 0
fi
__sm.package.configure.flag "--prefix" "${install_path}"
if "${_command%% *}" --help 2>/dev/null | \grep -- --sysconfdir >/dev/null
then
__sm.package.configure.flag "--sysconfdir" "/etc"
fi
_command="${_command} ${configure_flags[*]}"
__sm.log.debug package "__sm.package.configure: ${_command}"
PREFIX="${install_base_path}/${package_version}"
PKG_CONFIG_PATH=${active_path}/lib/pkgconfig
${=_command} > configure.log 2>&1 ||
__sm.package.error \
"Configuration of ${package_name} ${package_version} failed." \
"configure.log"
}
__sm.package.gnu.make()
{
trace_filter package || set -o xtrace
typeset _command
if [[ -n ${build_command:-} ]]
then
_command="${build_command}"
else
if ! __sm.string.contains.word "-j" "${make_flags[*]}"
then
make_flags+=( "-j" "$(__sm.os.cpu.count)" )
fi
_command="make ${make_flags[@]}"
fi
__sm.log.debug package "__sm.package.gnu.make: ${_command}"
env | grep -E '^C.*=|PATH.*=|FLAGS.*=' | __sm.log.debug package -
${=_command} > build.log 2>&1 ||
__sm.package.error \
"Compilation of ${package_name} ${package_version} failed! " "$PWD/build.log"
}
__sm.package.preinstall()
{
trace_filter package || set -o xtrace
true # nothing to be done for now.
}
__sm.package.make.install()
{
trace_filter package || set -o xtrace
typeset _command
if [[ -n ${install_command:-} ]]
then
_command="${install_command}"
else
_command="make ${make_install_flags[@]:-install}"
fi
${=_command} > make.install.log 2>&1 ||
__sm.package.error \
"Installation of ${package_name} ${package_version} failed! " \
"$PWD/make.install.log"
}
__sm.package.postinstall()
{
trace_filter package || set -o xtrace
true
}
__sm.package.gnu.activate()
{
trace_filter package || set -o xtrace
(( $# == 0 )) || __sm.package.cli $@
versionedfs activate \
path "${packages_path}" name "${package_name}" version "${package_version}"
__sm.package.ldconfig "${package_name}"
__sm.package.setup "${package_name}"
}
__sm.package.setup()
{
trace_filter package || set -o xtrace
typeset _package
_package="${1:-${package_name}}"
__sm.package.profile.d "${_package}"
if __sm.module.is.loaded "internal/service"
then
__sm.service.setup "${_package}"
fi
if __sm.module.is.loaded "internal/database"
then
__sm.database.setup "${_package}"
fi
}
__sm.package.gnu.deactivate()
{
trace_filter package || set -o xtrace
(( $# == 0 )) || __sm.package.cli $@
__sm.log.step "${package_name} ${_version:-${package_version}} ~ deactivate"
versionedfs deactivate path "${packages_path}" name "${package_name}" >/dev/null
__sm.log.step success
if ! __sm.string.contains.word '__sm.package.activate' "${FUNCNAME[*]}"
then
__sm.package.ldconfig "${_package}"
fi
}
__sm.package.activate.first()
{
trace_filter package || set -o xtrace
typeset _package _version
_package="${1:-}"
_version="${2:-}"
(( $# )) && shift || __sm.log.fail "Package name must be given as first parameter."
(( $# )) && shift || __sm.log.fail "Package version must be given as second parameter."
if paths exist "${install_path}"
then
__sm.package.activate "${_package}" "${_version}"
else
log "'${install_path}' not found; skipping activation."
fi
}
__sm.package.ldconfig()
{
trace_filter package || set -o xtrace
typeset _package _path _files _ldconfig
_package="${1:-${package_name}}"
_ldconfig="/sbin/ldconfig"
if [[ -z "${_package}" ]]
then
__sm.log.fail "Package name must be given."
fi
# paths exist "${packages_path}/versions/${_package}/active/lib" ||
# return 0 # no lib/ directory for activated package, no need to update ldconfig
user is root || return 0
# TODO: Figure out if solaris and freebsd have an analog to this?
if __sm.os.type.is linux
then
if paths exist "/etc/ld.so.conf.d"
then
files write \
string "${active_path}/lib\n" \
to "/etc/ld.so.conf.d/sm.conf" \
mode 0444
fi
if [[ -d /selinux ]] && command exists restorecon
then
restorecon -R "${active_path}/lib" # -v ~ verbose
restorecon -R "${packages_path}/versions/${package_name}" # -v ~ verbose
fi
command exists "${_ldconfig}" || ldconfig="ldconfig"
__sm.log.step "${package_name} ${package_version} ~ ldconfig"
"${_ldconfig}" "${active_path}/lib"
__sm.log.step success
elif __sm.os.type.is darwin
then # Cluster Fuck!
true
# This should be handled by the profile.d?
# _files=($(find "${packages_path}/versions/${_package}/active" -mindepth 1 -maxdepth 1 -type d))
# (( ${#_files[@]} > 0 )) && copy_files to "${packages_path}" "${_files[@]}"
fi
}
__sm.package.profile.d()
{
trace_filter package || set -o xtrace
typeset _package
_package="${1:-${package_name}}"
if [[ -z "${_package}" ]]
then
__sm.log.fail "Package name must be given."
fi
templates exist "profile.d.template" || return 0
__sm.log.step "Updating shell profile for ${_package}"
paths create "${profile_d_path}"
files create "${profile_path}"
templates install "profile.d" to "${profile_d_path}/${_package}.sh" mode 0755
__sm.log.step success
}
__sm.package.gnu.uninstall()
{
trace_filter package || set -o xtrace
(( $# == 0 )) || __sm.package.cli $@
if __sm.package.is.active "${package_name}" "${package_version}"
then # Deactivate the package if active.
__sm.package.deactivate "${package_name}" "${package_version}"
fi
__sm.log.step "${package_name} ${package_version} ~ uninstall"
paths remove "${install_path}"
if __sm.module.is.loaded "internal/service"
then
__sm.service.uninstall "${_package}" # Remove any service reminants
fi
__sm.log.step success
}
__sm.package.patch()
{
trace_filter package || set -o xtrace
typeset _patches _patch _path _paths
_paths=(
"${extension_patches_path}"
"${extension_patches_path}/$(__sm.os.type)"
"${extension_patches_path}/$(__sm.os.type)/${package_version}"
)
for _path in "${_paths[@]}"
do
paths exists "${_path}" || continue # No patch path
_patches=( ${_path}/*.patch ${_path}/*.diff )
if (( ${#_patches[@]} ))
then
__sm.package.patches.apply "${_patches[@]}"
fi
done
}
__sm.package.patches.apply()
{
trace_filter package || set -o xtrace
typeset _patch _patches
_patches=("$@")
(( $# )) && shift || __sm.log.fail "No patches were given."
for _patch in "${_patches[@]}"
do
[[ -f "${_patch}" && -s "${_patch}" ]] || continue
patch -F 3 -p 1 -N -f < ${_patch} > patch.install.log 2>&1 ||
__sm.package.error \
"Patching of ${package_name} ${package_version} with ${_patch} failed! " \
"$PWD/patch.install.log"
done
}