aboutsummaryrefslogtreecommitdiff
blob: f1d451287fb88da4854bce3005055ff7a160232e (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
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
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

DESCRIPTION="Manage /etc/init.d scripts in runlevels"
MAINTAINER="Danny van Dyk <kugelfang@gentoo.org>"
SVN_DATE='$Date$'
VERSION=$(svn_date_to_version "${SVN_DATE}")

# global setting
RC_SOFTLEVEL=/var/lib/init.d/softlevel

# list_runlevels PRIVATE
# list runlevels for file $1
list_runlevels() {
	[[ ${#@} -eq 1 ]] || return
	local runlevels
	for x in ${ROOT}/etc/runlevels/* ; do
		[[ -d ${x} ]] || continue
		[[ -L ${ROOT}/etc/runlevels/${x##*/}/${file} ]] \
			&& runlevels=(${runlevels[@]} "${x##*/}")
	done
	echo -ne "${runlevels[@]}"
}

# is_script PRIVATE
# check if file $1 is a valid init script
is_script() {
	local file=${1}
	[[ -n ${file} ]] \
		|| return 1
	( [[ -L ${file} ]] \
		&& [[ ! -e $(readlink -f ${file}) ]] ) \
		&& return 1
	[[ -e ${file} ]] \
		&& [[ ${file%%.sh} == ${file} ]] \
		&& [[ ${file%%\~} == ${file} ]] \
		&& [[ -n `grep "^#\!/sbin/runscript" ${file}` ]]
}

# find_scripts PRIVATE
# browse directory $1 for init scripts and return a list
find_scripts() {
	local ret
	for file in ${1}/* ; do
		is_script ${file} \
			&& ret=(${ret[@]} "${file##*/}")
	done
	echo -ne ${ret[@]}
}

# run_runscript PRIVATE
# run RC_RUNSCRIPT with script $2- and command $1
run_runscript() {
	local command=${1}
	shift
	write_list_start "${1}"
	shift
	for script in ${@} ; do
		is_script ${ROOT}/etc/init.d/${script} \
			&& /sbin/runscript ${ROOT}/etc/init.d/${script} ${command}
	done
}

### add action

describe_add() {
	echo "Add script to existing runlevel(s)"
}

describe_add_parameters() {
	echo "<script>"
	echo "<runlevels>"
}

describe_add_options() {
	echo "script : Init script (from 'list' action)"
	echo "runlevels : Runlevels to add to (defaults to 'default')"
}

do_add() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be added!"
	local script=${1##*/}
	[[ -e ${ROOT}/etc/init.d/${script} ]] \
		|| die -q "Please specify a valid init script!"
	shift 1
	local runlevels=${@:-default}
	write_list_start "Adding $(highlight ${script}) to following runlevels"
	for runlevel in ${runlevels} ; do
		if [[ ! -d ${ROOT}/etc/runlevels/${runlevel} ]] ; then
			write_kv_list_entry ${runlevel} "[invalid]"
			continue
		else
			if [[ -L ${ROOT}/etc/runlevels/${runlevel}/${script} ]] ; then
				write_kv_list_entry ${runlevel} "[skipped]"
			else
				ln -sf \
					${ROOT}/etc/init.d/${script} \
					${ROOTi}/etc/runlevels/${runlevel}/${script} \
					&& write_kv_list_entry ${runlevel} "[done]" \
					|| write_kv_list_entry ${runlevel} "[failed]"
			fi
		fi
	done
}

### delete action

describe_delete() {
	echo "Delete script from existing runlevel(s)"
}

describe_delete_parameters() {
	echo "<script>"
	echo "<runlevels>"
}

describe_delete_options() {
	echo "script : Init script (from 'list' action)"
	echo "runlevels : Runlevels to delete from (defaults to 'default')"
}

do_delete() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be deleted!"
	local script=${1##*/}
	[[ -e ${ROOT}/etc/init.d/${script} ]] \
		|| die -q "Please specify a valid init script!"
	shift 1
	local runlevels=${@:-default}
	write_list_start "Deleting $(highlight ${script}) from following runlevels"
	for runlevel  in ${runlevels} ; do
		if [[ ! -d ${ROOT}/etc/runlevels/${runlevel} ]] ; then
			write_kv_list_entry ${runlevel} "[invalid]"
			continue
		else
			if [[ -L ${ROOT}/etc/runlevels/${runlevel}/${script} ]] ; then
				rm ${ROOT}/etc/runlevels/${runlevel}/${script} \
					&& write_kv_list_entry ${runlevel} "[done]" \
					|| write_kv_list_entry ${runlevel} "[failed]"
			else
				write_kv_list_entry ${runlevel} "[skipped]"
			fi
		fi
	done
}

### list action

describe_list() {
	echo "List all available init scripts"
}

describe_list_parameters() {
	echo "<runlevel>"
}

describe_list_options() {
	echo "runlevel : Runlevel to list (defaults to all)"
}

do_list() {
	local dir file item
	if [[ -n ${1} ]] && [[ -d ${ROOT}/etc/runlevels/${1} ]] ; then
		dir=${ROOT}/etc/runlevels/${1}
		write_list_start "Init scripts to be started by runlevel $(highlight ${1})"
	elif [[ -z ${1} ]] ; then
		dir=${ROOT}/etc/init.d
		write_list_start "Available init scripts"
	else
		die -q "${1} is no valid runlevel!"
	fi
	
	for file in $(find_scripts ${dir}) ; do
			[[ ${dir##*/} = init.d ]] \
				&& write_kv_list_entry ${file} "$(list_runlevels ${file})" \
				|| echo "  ${file}"
#				&& echo "  ${file} $(space $((20 - ${#file}))) | $(list_runlevels ${file})" \
#				|| echo "  ${file}"
	done
}

### show action

describe_show() {
	echo "Show init script status for current runlevel"
}

do_show() {
	local runlevel=$(< ${RC_SOFTLEVEL}) script stopped
	write_list_start "Status of init scripts in runlevel $(highlight ${runlevel})"
	for script in $(find_scripts ${ROOT}/etc/runlevels/${runlevel}) ; do
		stopped=true
		for x in started starting stopping failed broken ; do
			if [[ -L ${ROOT}/var/lib/init.d/${x}/${script} ]] ; then
				write_kv_list_entry ${script} "[${x}]"
				stopped=false
			fi
		done
		[[ ${stopped} == true ]] && write_kv_list_entry ${script} "[stopped]"
	done
}

### start action

describe_start() {
	echo "Start given set of init scripts manually"
}

describe_start_parameters() {
	echo "<scripts>"
}

describe_start_options() {
	echo "scripts : Init scripts to start"
}

do_start() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be started!"
	run_runscript start "Starting init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
}

### stop action

describe_stop() {
	echo "Stop given set of init scripts manually"
}

describe_stop_parameters() {
	echo "<scripts>"
}

describe_stop_options() {
	echo "scripts : Init scripts to stop"
}

do_stop() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be stopped!"
	run_runscript stop "Stopping init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
}

### pause action

describe_pause() {
	echo "Pauses given set of init scripts manually"
}

describe_pause_parameters() {
	echo "<scripts>"
}

describe_pause_options() {
	echo "scripts : Init scripts to pause"
}

do_pause() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be paused!"
	run_runscript pause "Pausing init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
}

### restart action

describe_restart() {
	echo "Restart given set of init scripts"
}

describe_restart_parameters() {
	echo "<scripts>"
}

describe_restart_options() {
	echo "scripts : Init scripts to restart"
}

do_restart() {
	[[ ${#@} -gt 0 ]] \
		|| die -q "Please specify the init script to be restarted!"
	run_runscript restart "Restarting init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
}

# vim: set ft=eselect :