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
|
;;; node.scm -- Distributed computing node
;; Copyright (C) 2015, 2016, 2017 Artyom V. Poptsov <poptsov.artyom@gmail.com>
;;
;; This file is a part of Guile-SSH.
;;
;; Guile-SSH is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; Guile-SSH is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Guile-SSH. If not, see
;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This module describes the distributed node object. This object holds an
;; SSH tunnel to a remote host and remote REPL port, thus it can be used to
;; execute jobs (see (ssh dist job)).
;;
;; The module provides the following procedures:
;; node?
;; node-session
;; node-repl-port
;; make-node
;; node-eval
;; node-open-rrepl
;; node-guile-version
;; node-run-server
;; node-stop-server
;; node-server-running?
;; node-loadavg
;; with-ssh
;;
;; rrepl-eval
;; rrepl-skip-to-prompt
;;
;; There are two specific exceptions that the module procedures can throw:
;; node-error
;; node-repl-error
;;
;; See the Info documentation for detailed description of these exceptions and
;; aforementioned procedures.
;;; Code:
(define-module (ssh dist node)
#:use-module (ice-9 rdelim)
#:use-module (ice-9 regex)
#:use-module (ice-9 receive)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-9 gnu)
#:use-module (ssh session)
#:use-module (ssh session)
#:use-module (ssh channel)
#:use-module (ssh popen)
#:use-module (ssh tunnel)
#:use-module (ssh log)
#:use-module (ssh shell)
#:export (node?
node-session
node-tunnel
node-repl-port
make-node
node-eval
node-eval-1
node-guile-version
node-run-server
node-stop-server
node-server-running?
node-loadavg
with-ssh
node-open-rrepl
rrepl-eval
rrepl-skip-to-prompt
rrepl-get-result))
(define %guile-default-repl-port 37146)
(define (eof-or-null? str)
"Return #t if a STR is an EOF object or an empty string, #f otherwise."
(or (eof-object? str) (string-null? str)))
;;; Error reporting
(define (node-error . args)
"Raise a node error."
(apply throw (cons 'node-error args)))
(define (node-repl-error . args)
"Raise a REPL error."
(apply throw (cons 'node-repl-error args)))
;;; Node type
(define-record-type <node>
(%make-node tunnel repl-port start-repl-server? stop-repl-server?)
node?
(tunnel node-tunnel) ; <tunnel>
(repl-port node-repl-port) ; number
(start-repl-server? node-start-repl-server?) ; boolean
(stop-repl-server? node-stop-repl-server?)) ; boolean
(define (node-session node)
"Get node session."
(tunnel-session (node-tunnel node)))
(set-record-type-printer!
<node>
(lambda (node port)
(let ((s (node-session node)))
(format port "#<node ~a@~a:~a/~a ~a>"
(session-get s 'user)
(session-get s 'host)
(session-get s 'port)
(node-repl-port node)
(number->string (object-address node) 16)))))
(define* (make-node session #:optional (repl-port 37146)
#:key (start-repl-server? #t)
(stop-repl-server? #f))
"Make a new distributed computing node. If START-REPL-SERVER? is set to
#t (which is by default) then start a REPL server on a remote host
automatically in case when it is not started yet. If STOP-REPL-SERVER? is set
to #t then a REPL server will be stopped as soon as an evaluation is done."
(let ((tunnel (make-tunnel session
#:port 0 ;Won't be used
#:host "localhost"
#:host-port repl-port)))
(%make-node tunnel repl-port start-repl-server? stop-repl-server?)))
;;; Remote REPL (RREPL)
(define (read-string str)
"Read a string STR."
(call-with-input-string str read))
(define (rrepl-skip-to-prompt repl-channel)
"Read from REPL-CHANNEL until REPL is observed. Throw 'node-error' on an
error."
(let loop ((line (read-line repl-channel)))
(when (eof-object? line)
(node-error "Could not locate REPL prompt" repl-channel))
(or (string=? "Enter `,help' for help." line)
(loop (read-line repl-channel)))))
;; Regexp for parsing a result of evaluation of an expression that returns a
;; value.
(define %repl-result-regexp
(make-regexp "^(.*)@(.*)> \\$([0-9]+) = (.*)"))
;; Regexp for parsing a result of evaluation of an expression that returns
;; multiple values.
(define %repl-result-2-regexp
(make-regexp "^\\$([0-9]+) = (.*)"))
;; Regexp for parsing a result of evaluation of an expression which return
;; value is unspecified.
(define %repl-undefined-result-regexp
(make-regexp "^(.*)@(.*)> "))
;; Regexp for parsing an evaluation error.
(define %repl-error-regexp
(make-regexp "^(.*)@(.*)> ERROR: .*"))
(define %repl-error-regexp-2
(make-regexp "^ERROR: .*"))
(define (rrepl-get-result repl-channel)
"Get result of evaluation form REPL-CHANNEL, return four values: an
evaluation result, a number of the evaluation, a module name and a language
name. Throw 'node-repl-error' on an error."
(define (raise-repl-error result)
"Raise an REPL error with a RESULT of evaluation."
(node-repl-error "Evaluation failed" result))
(define (parse-result matches lines)
(if (null? lines)
(reverse matches)
(let ((line (car lines)))
(if (or (eof-or-null? line)
(regexp-exec %repl-undefined-result-regexp line))
(reverse matches)
(parse-result (cons (regexp-exec %repl-result-2-regexp line) matches)
(cdr lines))))))
(define (read-result match rest)
(let* ((matches (parse-result (list match) rest))
(len (length matches)))
(if (= len 1)
(let ((m (car matches)))
(values (read-string (match:substring m 4))
(string->number (match:substring m 3))))
(let ((rv (make-vector len))
(nv (make-vector len)))
;; The 1st match also contains a module name and language name,
;; but we want only the evaluation result and the result number.
(let ((m (car matches)))
(vector-set! rv 0 (read-string (match:substring m 4)))
(vector-set! nv 0 (string->number (match:substring m 3))))
(do ((i 1 (1+ i)))
((= i len))
(let ((m (list-ref matches i)))
(vector-set! rv i (read-string (match:substring m 2)))
(vector-set! nv i (string->number (match:substring m 1)))))
(values rv nv)))))
(define (error? line)
"Does a LINE contain an REPL error message?"
(or (regexp-exec %repl-error-regexp line)
(regexp-exec %repl-error-regexp-2 line)))
(define (error-message? result)
"Does a RESULT of evaluation contains a REPL error message?"
(find error? result))
(define (handle-response result)
(cond
((error-message? result)
(raise-repl-error (string-join result "\n")))
((regexp-exec %repl-result-regexp (car result)) =>
(lambda (match)
(receive (result eval-num)
(read-result match (cdr result))
(values
result ; Result
eval-num ; # of evaluation
(match:substring match 2) ; Module
(match:substring match 1))))) ; Language
((regexp-exec %repl-undefined-result-regexp (car result)) =>
(lambda (match)
(values
*unspecified* ; Result
*unspecified* ; # of evaluation
(match:substring match 2) ; Module
(match:substring match 1)))) ; Language
(else
(raise-repl-error (string-join result "\n")))))
(define (read-response result)
(let ((line (read-line repl-channel)))
(if (eof-or-null? line)
(handle-response (reverse result))
(read-response (cons line result)))))
(read-response '()))
(define (rrepl-eval rrepl-channel quoted-exp)
"Evaluate QUOTED-EXP using RREPL-CHANNEL, return four values: an evaluation
result, a number of the evaluation, a module name and a language name. Throw
'node-repl-error' on an error."
(write quoted-exp rrepl-channel)
(newline rrepl-channel)
(write-line '(newline) rrepl-channel)
(write-line '(exit) rrepl-channel)
(rrepl-get-result rrepl-channel))
;;;
(define (procps-available? node)
"Check if procps is available on a NODE."
(command-available? (node-session node) "pgrep"))
(define (issue-procps-warning function node missing-procps-tool)
"Issue procps warning due to a MISSING-PROCPS-TOOL on a NODE to the libssh
log."
(format-log 'rare
function
(string-append
"WARNING: '~a' from procps is not available on the node"
" ~a; falling back to the Guile-SSH '~a' implementation")
missing-procps-tool node missing-procps-tool))
(define (node-server-running? node)
"Check if a RREPL is running on a NODE, return #t if it is running and
listens on an expected port, return #f otherwise."
(define (guile-up-and-running?)
(let ((rp (tunnel-open-forward-channel (node-tunnel node))))
(and (channel-open? rp)
(let ((line (read-line rp)))
(close rp)
(and (not (eof-object? line))
(string-match "^GNU Guile .*" line))))))
(let* ((pgrep? (procps-available? node))
(pgrep (if pgrep? pgrep fallback-pgrep)))
(unless pgrep?
(issue-procps-warning "node-server-running?" node "pgrep"))
(receive (result rc)
(pgrep (node-session node)
(format #f "guile --listen=~a"
(node-repl-port node))
#:full? #t)
(or (and (zero? rc)
(guile-up-and-running?))
;; Check the default port.
(and (= (node-repl-port node) %guile-default-repl-port)
(receive (result rc)
(pgrep (node-session node)
"guile --listen"
#:full? #t)
(and (zero? rc)
(guile-up-and-running?))))))))
(define (node-run-server node)
"Run a RREPL server on a NODE."
(open-remote-input-pipe (node-session node)
(format #f "nohup guile --listen=~a 0<&- &>/dev/null"
(node-repl-port node)))
(while (not (node-server-running? node))
(usleep 100)))
(define (node-stop-server node)
"Stop a RREPL server on a NODE."
(format-log 'functions "[scm] node-stop-server"
"trying to SIGTERM the RREPL server on ~a ..." node)
(let* ((pkill? (procps-available? node))
(pkill (if pkill? pkill fallback-pkill)))
(unless pkill?
(issue-procps-warning "node-stop-server" node "pkill"))
(pkill (node-session node)
(format #f "guile --listen=~a" (node-repl-port node))
#:full? #t)
(while (node-server-running? node)
(format-log 'functions "[scm] node-stop-server"
"trying to SIGKILL the RREPL server on ~a ..."
node)
(pkill (node-session node)
(format #f "guile --listen=~a" (node-repl-port node))
#:signal 'SIGKILL
#:full? #t)
(sleep 1))))
(define (node-open-rrepl node)
"Open a RREPL. Return a new RREPL channel."
(and (node-start-repl-server? node)
(not (node-server-running? node))
(node-run-server node))
(tunnel-open-forward-channel (node-tunnel node)))
(define (node-eval node quoted-exp)
"Evaluate QUOTED-EXP on the node and return the evaluated result."
(let ((repl-channel (node-open-rrepl node)))
(rrepl-skip-to-prompt repl-channel)
(call-with-values (lambda () (rrepl-eval repl-channel quoted-exp))
(lambda vals
(and (node-stop-repl-server? node)
(node-stop-server node))
(apply values vals)))))
(define (node-eval-1 node quoted-exp)
"Evaluate QUOTED-EXP on the node and return the evaluated result. The
procedure returns the 1st evaluated value if multiple values were returned."
(receive (result eval-num)
(node-eval node quoted-exp)
(if (vector? eval-num)
(vector-ref result 0)
result)))
(define (node-guile-version node)
"Get Guile version installed on a NODE, return the version string. Return
#f if Guile is not installed."
(receive (result rc)
(rexec node "which guile > /dev/null && guile --version")
(and (zero? rc)
(car result))))
(define-syntax-rule (with-ssh node exp ...)
"Evaluate expressions on a remote REPL using a NODE, return four values: an
evaluation result, a number of the evaluation, a module name and a language
name. Throw 'node-error' or 'node-repl-error' on an error."
(node-eval node (quote (begin exp ...))))
(define (node-loadavg node)
"Get average load of a NODE. Return an alist of five elements as described in
proc(5) man page."
(with-ssh node
(use-modules (ice-9 rdelim))
(define (list-element->number l n)
(string->number (list-ref l n)))
(let* ((p (open-input-file "/proc/loadavg"))
(raw (read-line p)))
(close p)
(let ((raw-list (string-split raw #\space)))
`((one . ,(list-element->number raw-list 0))
(five . ,(list-element->number raw-list 1))
(fifteen . ,(list-element->number raw-list 2))
(scheduling-entities . ,(map string->number
(string-split (list-ref raw-list 3)
#\/)))
(last-pid . ,(list-element->number raw-list 4)))))))
;;; node.scm ends here
|