aboutsummaryrefslogtreecommitdiff
blob: 7c46ff159c7d21be0761722f2511389670189d10 (plain)
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
# -*-eselect-*-  vim: ft=eselect
# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

DESCRIPTION="Manage the Java system and user VM"
MAINTAINER="java@gentoo.org"
VERSION="@VERSION@"

VM_BASE="${EROOT}/usr/lib/jvm/"
VM_SYSTEM="${EROOT}/etc/java-config-2/current-system-vm"
VM_USER="${HOME}/.gentoo${EPREFIX}/java-config-2/current-user-vm"
VM_CONFIG="${EROOT}/usr/share/java-config-2/vm"

find_targets() {
	local TARGETS=() target

	for target in "${VM_BASE}"/*; do
		[[ -L "${target}" ]] && TARGETS+=( ${target##*/} )
	done

	printf "%s\n" "${TARGETS[@]}" | sort -uV
}

sym_to_vm() {
	basename $(readlink "${1}")
}

### show action ###

describe_show() {
	echo "Show the current VM"
}

describe_show_parameters() {
	echo "[user|system]"
}

do_show() {
	if [[ ${1} == "system" ]]; then
		my_show "${VM_SYSTEM}" 'system-vm'
	elif [[ ${1} == "user" ]]; then
		my_show "${VM_USER}" 'user-vm'
	else
		my_show "${VM_SYSTEM}" 'system-vm'
		my_show "${VM_USER}" 'user-vm'
	fi
}

my_show() {
	local symlink=${1} vm_type=${2}
	write_list_start "Current ${vm_type}"
	if [[ -L "${symlink}" ]] ; then
		write_kv_list_entry "$(sym_to_vm ${symlink})" ""
	else
		write_kv_list_entry "(unset)" ""
	fi
}

### list action ###

describe_list() {
	echo "List Available Virtual Machines"
}

do_list() {
	targets=( $(find_targets) )
	write_list_start "Available Java Virtual Machines:"
	local found_build_only
	if [[ -n ${targets[@]} ]] ; then
		local i system_name user_name

		[[ -L ${VM_SYSTEM} ]] && system_name=$(sym_to_vm "${VM_SYSTEM}")
		[[ -L ${VM_USER} ]] && user_name=$(sym_to_vm "${VM_USER}")

		for (( i = 0 ; i < ${#targets[@]} ; i = i + 1 )) ; do
			local build_only=$(grep 'BUILD_ONLY' "${VM_CONFIG}/${targets[${i}]}" | cut -c 13-16 )
			local mark=""

			if [[ "${build_only}" == "TRUE" || "{build_only}" == "true" ]]; then
				mark="$(highlight_warning 'Build Only')"
				found_build_only="TRUE"
			fi

			if [[ ${targets[${i}]} == ${system_name} ]]; then
				mark="${mark} $(highlight 'system-vm')"
			fi
			if [[ ${targets[${i}]} == ${user_name} ]]; then
				mark="${mark} $(highlight 'user-vm' )"
			fi
			targets[${i}]="${targets[${i}]} ${mark}"
		done
		write_numbered_list "${targets[@]}"
	else
		write_kv_list_entry "(none found)" ""
	fi
	echo
	if [[ "${found_build_only}" == "TRUE" ]]; then
		write_warning_msg "VMs marked as Build Only may contain Security Vulnerabilities and/or be EOL."
		write_warning_msg "Gentoo recommends not setting these VMs as either your System or User VM."
		write_warning_msg "Please see https://wiki.gentoo.org/wiki/Java#Build_Only_VM for more information."
	fi
}

### set action ###

describe_set() {
	echo "Set a new system or user VM"
}

describe_set_options() {
	echo "--if-unset : do not change if already set"
}

describe_set_parameters() {
	echo "<user|system> <VM>"
}

do_set() {
	local usage="Usage [user|system] <VM>"
	local ifunset=0

	if [[ ${1} == "--if-unset" ]]; then
		ifunset=1
		shift
	fi

	# Automatically decide, based in the invoking user's UID, if the
	# user or system Java VM should be modified.
	if [[ ${#} -eq 1 ]]; then
		local kind
		if [[ ${UID} -eq 0 ]]; then
			kind="system"
		else
			kind="user"
		fi
		set -- ${kind} ${@}
	fi

	if [[ ${#} != 2 ]]; then
		die -q ${usage}
	elif [[ ${1} == "system" ]]; then
		if [[ -w ${VM_SYSTEM%/*} ]]; then
			my_set "${VM_SYSTEM}" "${2}" ${ifunset}
		else
			die -q "Sorry, you don't have enough permission to set system"
		fi
	elif [[ ${1} == "user" ]]; then
		if [[ ${UID} != 0 ]]; then
			my_set "${VM_USER}" "${2}" ${ifunset}
		else
			die -q "Sorry, you cannot set a user VM as root. Set the system VM instead"
		fi
	else
		die -q ${usage}
	fi
}

my_set() {
	local symlink=${1} target=${2} ifunset=${3}
	if [[ -z ${target} ]] ; then
		die -q "You didn't tell me what to set the symlink to"

	elif [[ -L "${symlink}" ]] ; then
		if [[ ${ifunset} == 0 || ! -d ${symlink} ]]; then
			set_symlink "${target}" "${symlink}"  || die -q "Couldn't set a new symlink"
		fi

	elif [[ -e ${symlink} ]] ; then
		die -q "Target file already exists and is not a symlink: ${symlink}"

	else
		set_symlink "${target}" "${symlink}" || die -q "Couldn't set a new symlink"
	fi
}

set_symlink() {
	local target=${1} symlink=${2}
	if is_number "${target}" ; then
		targets=( $(find_targets) )
		target=${targets[$(( ${target} - 1 ))]}
	fi
	if [[ -z ${target} ]] ; then
		die -q "Target \"${1}\" doesn't appear to be valid!"
	elif [[ -d ${VM_BASE}/${target} ]] ; then
		local sym_dir=${symlink%/*}
		mkdir -p "${sym_dir}" || die -q "Could not create ${sym_dir}"
		ln -snf "${VM_BASE}/${target}" "${symlink}"
	else
		die -q "Target \"${1}\" doesn't appear to be valid!"
	fi
}

describe_update() {
	echo "Automatically update the Java system VM"
}

do_update() {
	local targets
	targets=( $(find_targets) )

	if [[ ${#targets[@]} -eq 0 ]]; then
		echo "No installed Java VMs found, can not update"
		return
	fi

	if [[ -e "${VM_SYSTEM}" ]]; then
		local current_system_vm_name=$(sym_to_vm "${VM_SYSTEM}")
		echo "Current Java system VM ${current_system_vm_name} is valid, no need to update"
		return
	fi

	local new_target old_system_vm_name
	if [[ -L "${VM_SYSTEM}" ]]; then
		# There exists a Java system VM symlink that has become stale,
		# try to find another available VM with the same slot.
		old_system_vm_name=$(sym_to_vm "${VM_SYSTEM}")

		local old_system_vm_slot="${old_system_vm_name##*-}"

		local target
		for target in "${targets[@]}"; do
			local target_slot="${target##*-}"
			if [[ ${target_slot} -eq ${old_system_vm_slot} ]]; then
				new_target="${target}"
				break
			fi
		done
	fi

	if [[ -z "${new_target}" ]]; then
		# There is no Java system VM symlink or we could not find a
		# slot-matching replacement. But there are potential targets,
		# simply choose the first.
		# TODO: We could get more sophisticated here to select the "best"
		# target, but that is far from trivial.
		new_target="${targets[0]}"
	fi

	local from_vm_text=""
	if [[ -n "${old_system_vm_name}" ]]; then
		from_vm_text="from ${old_system_vm_name} "
	fi

	echo "Updating Java system VM ${from_vm_text}to ${new_target}"
	set_symlink "${new_target}" "${VM_SYSTEM}"
}