summaryrefslogtreecommitdiff
blob: 23450a6857cc7851525b7c5d789ca46981d05d5d (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
#!/sbin/openrc-run
# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

extra_started_commands="attach resume suspend"

depend() {
	# we can use dns and net, but we can also in most cases live without them
	use dns net ntp-client ntpd
}


create_work_directory() {
	if [[ ! -d "${RUNTIMEDIR}" ]]; then
		einfo "Directory ${RUNTIMEDIR} does not exist, creating now."
		mkdir -p "${RUNTIMEDIR}"
		if [[ ! -d "${RUNTIMEDIR}" ]]; then
			eeror "Directory ${RUNTIMEDIR} could not be created!"
			return 1
		fi

		# ensure proper ownership
		chown "${USER}:${GROUP}" "${RUNTIMEDIR}"
	fi

	if [[ ! -e "${RUNTIMEDIR}"/ca-bundle.crt ]]; then
		ln -s /etc/ssl/certs/ca-certificates.crt "${RUNTIMEDIR}"/ca-bundle.crt
	fi

	return 0
}


cuda_check() {
	local libtarget="${RUNTIMEDIR}/libcudart.so"
	local libsource="$(ls -t /opt/cuda/lib*/libcudart.so 2>/dev/null | head -n 1)"

	# Remove a broken symlink
	if [[ -h "${libtarget}" ]] \
	&& [[ "${libsource}" != "$(readlink "${libtarget}")" ]]; then
		rm -f "${libtarget}"
	fi

	# symlink the correct path
	if [[ -n "${libsource}" ]] \
	&& [[ -f "${libsource}" ]] \
	&& [[ ! -h "${libtarget}" ]]; then
		ln -snf "$libsource" "${libtarget}"
	fi
}


env_check() {
	# Make sure the configuration is sane
	: ${USER:="boinc"}
	: ${GROUP:="boinc"}
	: ${RUNTIMEDIR:="/var/lib/boinc"}
	: ${BOINCBIN:="$(which boinc_client)"}
	: ${BOINC_PIDFILE:="/var/run/boinc_client.pid"}
	: ${BOINCCMD:="$(which /usr/bin/boinccmd)"}
	: ${ALLOW_REMOTE_RPC:="yes"}
	: ${NICELEVEL:="19"}
	# ARGS is not checked, it could have been explicitly set
	# to be empty by the user.

	# If the client was not found (how?) something is seriously wrong
	if [[ ! -x "$BOINCBIN" ]]; then
		eerror "No boinc_client found!"
		return 1
	fi

	# The boinccmd is crucial, or we can not attach, suspend or resume
	# the boinc client
	if [[ ! -x "$BOINCCMD" ]]; then
		eerror "No boinccmd_program found!"
		return 1
	fi

	return 0
}


need_passwd_arg() {
	local vers=$(${BOINCBIN} --version | tr -d .)
	[ -z "$vers" ] && vers="00"
	[ $(expr substr "$vers" 1 2) -lt 74 ] && return 0

	# From version 7.4 on, the default is to read
	# gui_rpc_auth.cfg for the password.

	return 1
}


start_pre() {
	env_check || return 1
	create_work_directory || return 1
	cuda_check

	if [[ ! -f "${RUNTIMEDIR}/lockfile" ]]; then
		einfo "File \"${RUNTIMEDIR}/lockfile\" does not exist, assuming first run."
		einfo "You need to setup an account on the BOINC project homepage beforehand!"
		einfo "Go to http://boinc.berkeley.edu/ and locate your project."
		einfo "Then either run ${RC_SERVICE} attach or connect with a gui client"
		einfo "and attach to a project with that."
		einfo ""
		ewarn "Note that for attaching to some project you need your network up and running."
		ewarn "network is needed only for jobs fetching afterwards"
	fi

	return 0
}


start() {
	if [[ "${ALLOW_REMOTE_RPC}" = "yes" ]]; then
		ARGS="${ARGS} --allow_remote_gui_rpc"
	fi

	ARGS="${ARGS} --dir "${RUNTIMEDIR}" --redirectio"

	ebegin "Starting ${RC_SVCNAME}"
	start-stop-daemon --start --nicelevel ${NICELEVEL} \
		--user "${USER}:${GROUP}" --quiet --make-pidfile \
		--pidfile "$BOINC_PIDFILE" --background \
		--exec "${BOINCBIN}" -- ${ARGS}
	eend $?
}


attach() {
	local password=""
	local url=""
	local key=""

	env_check || return 1

	einfo "If you can't find your account key just try to obtain it by using:"
	einfo "    boinccmd --passwd PASSWORD_FROM_GUI_RPC_AUTH --lookup_account URL EMAIL PASSWORD"

	printf "    Enter the Project URL: "
	read url
	printf "    Enter your Account Key: "
	read key

	if ! service_started; then
		"${RC_SERVICE}" start
	fi

	if need_passwd_arg; then
		password="--passwd \"$(cat "${RUNTIMEDIR}/gui_rpc_auth.cfg")\""
	fi

	ebegin "${RC_SVCNAME}: Attaching to project"
	start-stop-daemon --user "${USER}:${GROUP}" --quiet \
		--chdir "${RUNTIMEDIR}" --exec "${BOINCCMD}" \
		-- ${password} --project_attach ${url} ${key}
	eend $?

	sleep 10s
	tail "${RUNTIMEDIR}/stdoutdae.txt"
}


stop() {
	local password=""
	local stop_timeout="SIGTERM/60/SIGTERM/30/SIGKILL/30"

	env_check || return 1

	if need_passwd_arg; then
		password="--passwd \"$(cat "${RUNTIMEDIR}/gui_rpc_auth.cfg")\""
	fi

	ebegin "Stopping ${RC_SVCNAME}"
	start-stop-daemon --stop --quiet --progress \
		--retry $stop_timeout \
		--pidfile "${BOINC_PIDFILE}"
	eend $?
}


resume() {
	env_check || return 1

	local password=""

	if need_passwd_arg; then
		password="--passwd \"$(cat "${RUNTIMEDIR}/gui_rpc_auth.cfg")\""
	fi

	local master_urls=( \
		$(cd "${RUNTIMEDIR}" ; \
		  "${BOINCCMD}" ${password} --get_project_status | \
		  sed -n 's/\s*master URL: //p') \
	)

	for url in "${master_urls[@]}"; do
		ebegin "Resuming $url"
		start-stop-daemon --user "${USER}:${GROUP}" --quiet \
			--chdir "${RUNTIMEDIR}" --exec "${BOINCCMD}" \
			-- ${password} --project ${url} resume
		eend $?
	done
}


suspend() {
	env_check || return 1

	local password=""

	if need_passwd_arg; then
		password="--passwd \"$(cat "${RUNTIMEDIR}/gui_rpc_auth.cfg")\""
	fi

	local master_urls=( \
		$(cd "${RUNTIMEDIR}" ; \
		  "${BOINCCMD}" ${password} --get_project_status | \
		  sed -n 's/\s*master URL: //p') \
	)

	for url in "${master_urls[@]}"; do
		ebegin "Suspending $url"
		start-stop-daemon --user "${USER}:${GROUP}" --quiet \
			--chdir "${RUNTIMEDIR}" --exec "${BOINCCMD}" \
			-- ${password} --project ${url} suspend
		eend $?
	done
}