xref: /inferno-os/lib/sh/owen (revision 46439007cf417cbd9ac8049bb4122c890097a0fa)
1load std sexprs
2
3# load a job.  result is the new job id.
4subfn job {
5	{
6		id := "{read}
7		result=$id
8		or {echo load ${quote $*} >[1=0]} {
9			raise 'load failed'
10		}
11	} $* <> /n/remote/admin/clone
12}
13
14# load a job.  print the new job id.
15fn job {
16	echo ${job $*}
17}
18
19# load a job, then start it.
20fn start {
21	id := ${job $*}
22	ctl $id start
23	echo $id
24}
25
26# send a control message to a job.
27fn ctl {
28	if {~ $#* 0 1} {
29		echo usage: ctl job-id request... >[1=2]
30		raise usage
31	}
32	(id args) := $*
33	echo ${quote $args} > /n/remote/admin/$id/ctl
34}
35
36# mount the scheduler name space
37fn mountsched {
38	configfile := $configfile		# stop changes propagating out.
39	if{no $root}{
40		root=/grid/slave
41	}
42	opts := ()
43	fsopts := ()
44	schedaddr := ()
45	schedfsaddr := ()
46	readconfig {
47		if{~ $attr schedaddr}{
48			schedaddr=$val
49		}{~ $attr auth}{
50			if{~ $val 0}{
51				opts=($opts -A)
52			}
53		}{~ $attr keyfile}{
54			opts=($opts -k $val)
55		}{~ $attr schedfsaddr}{
56			schedfsaddr=$val
57		}{~ $attr fsauth}{
58			if{~ $val 0}{
59				fsopts=($fsopts -A)
60			}
61		}{~ $attr fskey fskeyfile}{		# first form is deprecated
62			fsopts=($opts -k $val)
63		}
64	}
65	if{no $schedaddr}{
66		ifs='
67	'
68		schedaddr=`{cat /grid/slave/schedaddr}
69		if{no $schedaddr}{
70			echo no scheduler address found >[1=2]
71			raise error
72		}
73	}
74	mount $opts $schedaddr /n/remote
75	no $schedfsaddr ||
76		mount $fsopts $schedfsaddr /n/gridfs
77}
78
79# print a format(2) file with the given format
80fn fmtcat {
81	if {! ~ $#* 2} {
82		echo usage: fmtread fmt file >[1=2]
83		raise usage
84	}
85	(fmt file) := $*
86	{echo $fmt >[1=0]; read -o 0; cat} <> $file
87}
88
89# readconfig command.
90# on entry $configfile is name of configuration file, or empty for default.
91# $root is default root directory.
92# run command for each entry in the config file, setting $attr and $val
93# to the attribute and the value in the entry.
94fn readconfig {
95	(cmd nil) := $*
96	if{no $configfile}{
97		if{ftest -f $root/config}{
98			configfile = $root/config
99		}
100	} {
101		if{! ~ $configfile '/*' './*'} {
102			configfile = $root/$configfile
103		}
104		if{! ftest -f $configfile}{
105			echo cannot find config file $configfile >[1=2]
106			raise 'config error'
107		}
108	}
109	if{! no $configfile} {
110		< $configfile getsexprs {
111			(attr sval) := ${els $sexp}
112			if{! ~ $#sval 1}{
113				echo bad config line $sexp >[1=2]
114				raise continue;
115			}
116			attr = ${text $attr}
117			val := ${text $sval}
118			$cmd
119		}
120	}
121}
122