/
external
executable file
·514 lines (463 loc) · 13.7 KB
/
external
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
#!/usr/bin/env bash
source "$rvm_scripts_path/base"
external_automount()
{
external_mount_uniq $(
__rvm_remove_rvm_from_path
__rvm_which -a ruby
)
}
external_grep_existing()
{
typeset IFS
typeset -a existing
existing=( $(
for ext in $rvm_externals_path/*
do
if [[ -L "$ext" ]]
then
readlink "$ext"
fi
done
) )
IFS="|"
rvm_debug "existing:${existing[*]}:"
if (( ${#existing[@]} > 0 ))
then
GREP_OPTIONS="" \grep -vE "${existing[@]}" -
else
cat -
fi
}
external_mount_uniq()
{
typeset ruby_path
typeset -a ruby_paths
ruby_paths=( $(
for ruby_path in "$@"
do
if [[ -d "${ruby_path}" && -x "${ruby_path}/bin/ruby" ]] && "${ruby_path}/bin/ruby" -rrbconfig -e "" >/dev/null 2>&1
then
echo "${ruby_path}"
elif [[ ! -d "${ruby_path}" && -x "${ruby_path}" ]]
then
__rvm_ruby_config_get prefix "${ruby_path}"
fi
done | sort -u | external_grep_existing
) )
if (( ${#ruby_paths[@]} == 0 ))
then
rvm_error "The given paths '$*' either do not point to a ruby installation or are already mounted."
exit 1
else
for ruby_path in ${ruby_paths[@]}
do
( external_mount "${ruby_path}" ) || exit $?
done
fi
}
external_mount()
{
typeset _given_path ruby_path prefix_path
_given_path="$1"
if [[ ! -d "${_given_path}" && -x "${_given_path}" ]] && "${_given_path}" --version | GREP_OPTIONS="" \grep rub >/dev/null
then
ruby_path="${_given_path}"
prefix_path="$( __rvm_ruby_config_get prefix "${_given_path}" )"
elif [[ -d "${_given_path}" && -x "${_given_path}/bin/ruby" ]] && "${_given_path}/bin/ruby" --version | GREP_OPTIONS="" \grep rub >/dev/null
then
ruby_path="${_given_path}/bin/ruby"
prefix_path="$( __rvm_ruby_config_get prefix "${ruby_path}" )"
if [[ "${_given_path}" != "${prefix_path}" ]]
then
rvm_error "The given path '$_given_path' contains ruby but it has different prefix '$prefix_path'."
exit 2
fi
else
rvm_error "The given path '$_given_path' does not point to a ruby installation."
exit 3
fi
if [[ -z "${rvm_ruby_string:-}" ]]
then
if ! external_select_name "${ruby_path}" "${prefix_path}" ||
[[ -z "${rvm_ruby_string:-}" ]]
then
rvm_error "No name selected for ruby in '$prefix_path'."
exit 4
fi
fi
old_gem_home=$(
unset GEM_HOME
"${prefix_path}/bin/gem" env gemhome
)
rvm_ruby_string="ext-${rvm_ruby_string}"
echo "Mounting '${rvm_ruby_string}' from '${prefix_path}'"
mkdir -p "$rvm_externals_path"
ln -nfs "${prefix_path}" "$rvm_externals_path/$rvm_ruby_string"
mkdir -p "$rvm_rubies_path/$rvm_ruby_string/bin"
ln -nfs "${ruby_path}" "$rvm_rubies_path/$rvm_ruby_string/bin/ruby"
rvm_create_flag=1 __rvm_use
mkdir -p "$rvm_gems_path/$rvm_ruby_string@global"
ln -nfs "${old_gem_home}" "$rvm_gems_path/$rvm_ruby_string"
__rvm_bin_script
# Import the initial gemsets, unless skipped.
if (( ${rvm_skip_gemsets_flag:-0} == 0 ))
then
(
export rvm_gemset_name=global
__rvm_run_with_env "gemsets.initial" "$rvm_ruby_string" \
"'$rvm_scripts_path/gemsets' initial" \
"$rvm_ruby_string - #importing default gemsets ($rvm_gemsets_path/)"
)
else
rvm_log "Skipped importing default gemsets"
fi
__rvm_ruby_config_save "$ruby_path" "$rvm_rubies_path/$rvm_ruby_string/config"
__rvm_record_install "$rvm_ruby_string"
}
external_select_name()
{
typeset proposed_name ruby_version ruby_path prefix_path
export rvm_ruby_string
ruby_path="$1"
prefix_path="$2"
ruby_version="$( "${ruby_path}" --version)"
if [[ -x "${ruby_path}" ]] &&
proposed_name="$( external_propose_name "$ruby_version" )" &&
[[ -n "${proposed_name:-}" ]]
then
echo "Found '${ruby_version}' in '${prefix_path}'"
printf "\n# Please enter name [${proposed_name}]: "
read rvm_ruby_string
printf "\n"
: rvm_ruby_string:${rvm_ruby_string:=${proposed_name}}:
else
echo "Name not found for '${ruby_path}' in '${prefix_path}'"
false
fi
}
external_propose_name()
{
typeset parts __tmp1 __tmp2
parts="$( echo "$1" | sed 's/[()]//g; s/\[.*\]//;' )"
case "${parts}" in
(*Ruby[[:space:]]Enterprise[[:space:]]Edition*)
__tmp1="${parts#* }"
__tmp1="${__tmp1%% *}"
__tmp2="${parts##* }"
printf "ree-${__tmp1}-${__tmp2}"
;;
(ruby[[:space:]]*patchlevel[[:space:]]*)
__tmp1="${parts#* }"
__tmp1="${__tmp1%% *}"
__tmp2="${parts##*patchlevel }"
__tmp2="${__tmp2%% *}"
printf "ruby-${__tmp1}-p${__tmp2}"
;;
(ruby[[:space:]][[:digit:]].[[:digit:]].[[:digit:]]p[[:digit:]]*)
__tmp1="${parts#* }"
__tmp1="${__tmp1%% *}"
__tmp2="${__tmp1##+([[:digit:]\.])}"
__tmp1="${__tmp1%${__tmp2}}"
printf "ruby-${__tmp1}-${__tmp2}"
;;
(ruby[[:space:]]*revision[[:space:]]*|ruby[[:space:]]*trunk[[:space:]]*)
__tmp1="${parts#* }"
__tmp1="${__tmp1%% *}"
__tmp2="${parts##*trunk }"
__tmp2="${__tmp2##*revision }"
__tmp2="${__tmp2%% *}"
printf "ruby-${__tmp1}-r${__tmp2}"
;;
(ruby[[:space:]]*)
__tmp1="${parts#* }"
__tmp1="${__tmp1%% *}"
__tmp2="${__tmp1##+([[:digit:]\.])}"
__tmp1="${__tmp1%${__tmp2}}"
printf "ruby-${__tmp1}-${__tmp2}"
;;
(jruby[[:space:]]*)
__tmp1="${parts#* }"
__tmp1="${__tmp1%% *}"
__tmp2="${parts#* }"
__tmp2="${__tmp2#* }"
__tmp2="${__tmp2%% *}"
__tmp2="${__tmp2#ruby-}"
__tmp2="${__tmp2//-/_}"
printf "jruby-${__tmp1}-default_${__tmp2}"
;;
(maglev[[:space:]]*)
__tmp1="${parts#* }"
__tmp1="${__tmp1%% *}"
__tmp2="${parts#* }"
__tmp2="${__tmp2#* }"
__tmp2="${__tmp2#* }"
__tmp2="${__tmp2%% *}"
printf "maglev-${__tmp1}-default_${__tmp2}"
;;
(rubinius[[:space:]]*)
__tmp1="${parts#* }"
__tmp1="${__tmp1%% *}"
__tmp2="${parts#* }"
__tmp2="${__tmp2#* }"
__tmp2="${__tmp2%% *}"
printf "rbx-${__tmp1}-default_${__tmp2}"
;;
(*)
false
;;
esac
}
external_import_configure()
{
typeset platform architecture libc
if (( ${rvm_install_flag:-0} > 0 ))
then err_command=rvm_warn
else err_command=rvm_error
fi
rvm_ruby_url="$1"
rvm_ruby_string="${2:-${rvm_ruby_url}}"
[[ -r "${rvm_ruby_url}" ]] ||
case "${rvm_ruby_url}" in
(http*) true ;;
(*)
__rvm_ruby_string
rvm_ruby_url="$(__rvm_remote_server_path "${rvm_ruby_string}.${rvm_remote_extension:-tar.bz2}" )"
;;
esac
rvm_ruby_string="${rvm_ruby_string##*/}"
rvm_ruby_string="${rvm_ruby_string%.t*}"
rvm_ruby_package_file="bin-${rvm_ruby_string}.${rvm_remote_extension:-tar.bz2}"
}
external_import_download()
{
typeset downloaded_names
if
[[ -r "${rvm_ruby_url}" ]]
then
cp -f "${rvm_ruby_url}" "$rvm_archives_path/${rvm_ruby_package_file}" || {
$err_command "Copying ${rvm_ruby_url} failed."
return 3
}
else
"$rvm_scripts_path/fetch" "${rvm_ruby_url}?rvm=${rvm_version%% *}" "${rvm_ruby_package_file}" || {
$err_command "Downloading ${rvm_ruby_url} failed."
return 2
}
fi
downloaded_names="$(
tar tjf "$rvm_archives_path/${rvm_ruby_package_file}" | awk -F/ '{print $1}' | sort -u
)"
[[ "${rvm_ruby_string}" == "${downloaded_names}" ]] || {
$err_command "The downloaded package ${rvm_ruby_url} contains '${downloaded_names}' instead of '${rvm_ruby_string}'."
return 4
}
}
external_import_extract()
{
chpwd_functions="" builtin cd "${rvm_rubies_path}"
tar xjf "$rvm_archives_path/${rvm_ruby_package_file}" || {
$err_command "Unpacking ${rvm_ruby_package_file} failed."
return 6
}
[[ -d "${rvm_rubies_path}/${rvm_ruby_string}" ]] || {
$err_command "Ruby directory was not created, make "
return 8
}
}
# validate libraries - make sure everything can be loaded
external_import_validate()
{
typeset IFS
typeset -a missing_libs
export _libdir
_libdir="$( __rvm_ruby_config_get libdir "${rvm_rubies_path}/${rvm_ruby_string}/bin/ruby" )"
missing_libs=( $(
case $(uname) in
(Linux)
find "${rvm_rubies_path}/${rvm_ruby_string}" -name '*.so' |
xargs env LD_LIBRARY_PATH="${_libdir}" ldd 2>/dev/null |
awk '/not found/{print $1}' | sort -u
;;
(Darwin)
find "${rvm_rubies_path}/${rvm_ruby_string}" -name "*.bundle" |
xargs env LD_LIBRARY_PATH="${_libdir}" otool -L 2>&1 |
awk -F"[ :]" '{print $1}' |
sed 's#@executable_path#'"${rvm_rubies_path}/${rvm_ruby_string}"'/bin#' |
xargs ls -1 2>&1 1>/dev/null |
awk -F"[ :]" '{print $3}' | sort -u
;;
(*)
rvm_warn "$(uname) libraries validation not yet implemented" >&2
;;
esac
) )
unset _libdir
[[ -z "${missing_libs[*]}" ]] || {
IFS=","
$err_command "Libraries missing for ${rvm_ruby_string}: ${missing_libs}. Refer to your system manual for installing libraries"
return 10
}
}
__rvm_fix_wrappers()
{
typeset actual_file
find "$rvm_rubies_path/$rvm_ruby_string/bin/" -type f | while read actual_file
do
if
[[ -s "$actual_file" ]]
then
__rvm_sed_i "$actual_file" \
-e "/^ENV\[[\"']GEM_HOME[\"']\]=/ d" \
-e "/^ENV\[[\"']GEM_PATH[\"']\]=/ d" \
-e "/^ENV\[[\"']PATH[\"']\]=/ d"
fi
done
}
__rvm_fix_lib_files()
{
typeset __path __file
__path="$rvm_rubies_path/$rvm_ruby_string"
__file="$( find "${__path}" -name libyaml.la )"
__rvm_sed_i "${__file}" -e "s#^libdir=.*\$#libdir='$(dirname "${__file}")'#"
find "${__path}" -name "*.pc" | while read __file
do
__rvm_sed_i "${__file}" -e "s#^prefix=.*\$#prefix=${__path}#"
done
}
external_import_setup()
{
__rvm_ruby_string
"$rvm_scripts_path/rubygems" link
__rvm_fix_wrappers
__rvm_fix_lib_files
rvm_create_flag=1 __rvm_use
__rvm_bin_script # environment + wrappers
if (( ${rvm_skip_gemsets_flag:-0} == 0 ))
then
rvm_create_flag=1 __rvm_run_with_env "gemsets.initial" "$rvm_ruby_string" \
"'$rvm_scripts_path/gemsets' initial" \
"$rvm_ruby_string - #importing default gemsets ($rvm_gemsets_path/)"
else
mkdir -p "$rvm_gems_path/$rvm_ruby_string@global/bin"
rvm_log "Skipped importing default gemsets"
fi
__rvm_ruby_config_save "$rvm_rubies_path/$rvm_ruby_string/bin/ruby" "$rvm_rubies_path/$rvm_ruby_string/config"
__rvm_record_install "$rvm_ruby_string"
}
external_import()
{
typeset step err_command rvm_ruby_url rvm_ruby_string rvm_ruby_package_file
for step in configure download extract validate setup
do
rvm_log "${rvm_ruby_string:-$1} - #$step"
external_import_${step} "$@" || return $?
done
}
external_prepare()
{
typeset remote_shell
rvm_ruby_string="${1:-}"
remote_shell="${2:-}"
shift || [[ -n "${rvm_ruby_string}" ]] || {
rvm_error_help "Missing ruby name to export for prepare." external prepare
return 3
}
__rvm_ruby_string
external_prepare_package "${rvm_ruby_string}.tar.bz2" &&
external_prepare_upload "${rvm_ruby_string}.tar.bz2" "${remote_shell}"
}
external_prepare_package()
{
typeset file_name
file_name="$1"
if [[ ! -d "${rvm_rubies_path}/${rvm_ruby_string}" ]]
then
if
(( ${rvm_install_flag:-0} > 0 ))
then
rvm_configure_flags+=( --enable-shared --enable-load-relative --sysconfdir=/etc )
__rvm_manage_wrapper install "${rvm_ruby_string}" || return $?
else
rvm_error "Ruby ${rvm_ruby_string} does not exist, maybe install first?."
return 2
fi
fi
rvm_log "Preparing ${rvm_ruby_string} ..."
case "${file_name}" in
(*.tar.bz2)
tar cjf "${file_name}" -C "${rvm_rubies_path}" "${rvm_ruby_string}"
;;
(*.tar.gz|*.tgz)
tar czf "${file_name}" -C "${rvm_rubies_path}" "${rvm_ruby_string}"
;;
(*)
rvm_error "Do not know how to package '${file_name}'."
return 1
;;
esac
rvm_log "Prepared $1."
}
#TODO: move to SMF ! RVM2 ?
external_prepare_upload()
{
typeset remote_path remote_shell remote_directory system_path remote_file file_name local_file path_prefix
file_name="$1"
remote_path="${2:-rvm@rvm.io:~/shared/public/binaries}"
system_path="$(__rvm_system_path - )"
remote_directory="${remote_path#*:}/${system_path}"
remote_shell="${remote_path%:*}"
if
[[ ${rvm_path_flag:-0} -eq 1 ]]
then
path_prefix="${remote_path#*:}"
path_prefix="${path_prefix##*/}"
mkdir -p "${path_prefix}/${system_path}"
local_file="${path_prefix}/${system_path}/${file_name}"
mv "${file_name}" "${local_file}"
else
local_file="${file_name}"
fi
remote_file="$( __rvm_remote_server_path "${file_name}" )"
rvm_log "--- upload:"
printf "%b" "ssh \"$remote_shell\" \"mkdir -p $remote_directory/\"\n"
if
[[ ${rvm_path_flag:-0} -eq 1 ]]
then
printf "%b" "rsync -av --rsh=ssh \"${path_prefix}/\" \"${remote_path}/\"\n"
else
printf "%b" "scp \"${local_file}\" \"$remote_shell:$remote_directory/${file_name}\"\n"
fi
rvm_log "--- rvm/config/remote:"
printf "%b" "${remote_file}\n"
rvm_log "--- rvm/config/md5:"
printf "%b" "${remote_file}=$( __rvm_md5_calculate "${local_file}" )\n"
rvm_log "--- rvm/config/sha512:"
printf "%b" "${remote_file}=$( __rvm_sha__calculate 512 "${local_file}" )\n"
}
action="${1:-}"
shift || {
rvm_error_help "Missing action for external." external
exit 1
}
# skip first param if empty
[[ -n "${1:-}" ]] || (( $#==0 )) || shift
case "${action}" in
(automount|prepare)
external_$action "$@"
;;
(mount|*_name)
if
(( rvm_remote_flag > 0 )) ||
[[ "${next_token:-}" =~ ^http || "${next_token:-}" =~ tar.bz2$ ]]
then
external_import "$@"
else
external_mount_uniq "$@"
fi
;;
(*)
rvm_error_help "Wrong action for external '$action'." external $action
exit 2
;;
esac