1
+ implement Engridden;
2
+
3
+ include "sys.m";
4
+ sys: Sys;
5
+ include "draw.m";
6
+ include "string.m";
7
+ include "env.m";
8
+ include "arg.m";
9
+ include "bufio.m";
10
+
11
+ stderr: ref Sys->FD;
12
+
13
+ Engridden: module {
14
+ init: fn(nil: ref Draw->Context, argv: list of string);
15
+ };
16
+
17
+
18
+ # Mount ourselves into the public grid
19
+ # $pubgridreg sets the public grid registry dialstr
20
+ init(nil: ref Draw->Context, argv: list of string)
21
+ {
22
+ sys = load Sys Sys->PATH;
23
+ str := load String String->PATH;
24
+ env := load Env Env->PATH;
25
+ arg := load Arg Arg->PATH;
26
+ bio := load Bufio Bufio->PATH;
27
+ Iobuf: import bio;
28
+
29
+ reg := env->getenv("pubgridreg");
30
+ mtpt := "/mnt/registry";
31
+ ndir := "/n";
32
+
33
+ arg->init(argv);
34
+ arg->setusage("engridden [-r tcp!registry] [-m /mnt/registry] [-n /n]");
35
+ while((c := arg->opt()) != 0)
36
+ case c {
37
+ 'm' => mtpt = arg->earg();
38
+ 'r' => reg = arg->earg();
39
+ 'n' => ndir = arg->earg();
40
+ * => arg->usage();
41
+ }
42
+ argv = arg->argv();
43
+
44
+ if(reg == ""){
45
+ sys->print("err: $pubgridreg or '-r' must be provided to mount a registry\n");
46
+ exit;
47
+ }
48
+
49
+ sys->print("Mounting public grid registry %s…\n", reg);
50
+
51
+ if(dialmount(reg, mtpt) < 0)
52
+ exit;
53
+
54
+ buf := bio->open(mtpt + "/index", Bufio->OREAD);
55
+ if(buf == nil){
56
+ sys->print("err: open failed → %r\n");
57
+ exit;
58
+ }
59
+
60
+ Read:
61
+ for(;;){
62
+ line := buf.gets('\n');
63
+ if(line == "")
64
+ break Read;
65
+
66
+ (toks, err) := str->qtokenize(line);
67
+ if(err != nil){
68
+ sys->print("fail: can't parse line → %s\n", line);
69
+ continue Read;
70
+ }
71
+
72
+ if(len toks < 2){
73
+ sys->print("fail: not enough tokens in line → %s\n", line);
74
+ continue Read;
75
+ }
76
+
77
+ dialstr := hd toks;
78
+ name := "";
79
+ toks = tl toks;
80
+
81
+ # Modeled after 9p.zone's registry
82
+ for(; toks != nil; toks = tl toks){
83
+ case hd toks {
84
+ "description" =>
85
+ toks = tl toks;
86
+ name = hd toks;
87
+
88
+ * =>
89
+ ;
90
+ }
91
+ }
92
+
93
+ if(dialstr == ""){
94
+ sys->print("fail: no dialstring in line → %s\n", line);
95
+ continue Read;
96
+ }
97
+
98
+ if(name == ""){
99
+ sys->print("fail: could not find 'description' value in line → %s\n", line);
100
+ continue Read;
101
+ }
102
+ at := ndir + "/" + name;
103
+
104
+ sys->print("Mounting %s at %s…\n", dialstr, at);
105
+
106
+ if(dialmount(dialstr, at) < 0)
107
+ continue Read;
108
+ }
109
+
110
+ sys->print("Done.\n");
111
+
112
+ exit;
113
+ }
114
+
115
+ dialmount(dialstr, mtpt: string): int {
116
+ (ok, conn) := sys->dial(dialstr, nil);
117
+ if(ok < 0){
118
+ sys->print("err: dial failed for %s → %r\n", dialstr);
119
+ return ok;
120
+ }
121
+
122
+ ok = sys->mount(conn.dfd, nil, mtpt, Sys->MREPL|Sys->MCREATE, nil);
123
+ if(ok < 0){
124
+ sys->print("err: mount failed for %s → %r\n", mtpt);
125
+ return ok;
126
+ }
127
+
128
+ return 0;
129
+ }
0 commit comments