1 |
#!/usr/bin/perl |
2 |
|
3 |
package Sack::Server; |
4 |
|
5 |
use warnings; |
6 |
use strict; |
7 |
|
8 |
use IO::Socket::INET; |
9 |
use IO::Select; |
10 |
|
11 |
use Data::Dump qw(dump); |
12 |
use Storable qw(); |
13 |
use File::Slurp; |
14 |
use Cwd qw(abs_path); |
15 |
|
16 |
use lib '/srv/Sack/lib'; |
17 |
use Sack::Merge; |
18 |
use Sack::Server::HTTP; |
19 |
use Sack::Server::HTML; |
20 |
|
21 |
my @cloud; |
22 |
my $cloud_path = $ENV{CLOUD} || "etc/cloud"; |
23 |
die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n" unless -e $cloud_path; |
24 |
@cloud = read_file $cloud_path; |
25 |
@cloud = map { chomp $_; $_ } @cloud; |
26 |
|
27 |
warn "# cloud ",dump( @cloud ); |
28 |
|
29 |
my $listen_port = 4444; |
30 |
my $http_port = 4480; |
31 |
|
32 |
my $node_path = abs_path $0; |
33 |
$node_path =~ s{Server}{Client}; |
34 |
|
35 |
my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die "$listen_port $!"; |
36 |
my $www = IO::Socket::INET->new(Listen => 1, LocalPort => $http_port, Reuse => 1) or die "$http_port $!"; |
37 |
my $sel = IO::Select->new($lsn); |
38 |
$sel->add( $www ); |
39 |
|
40 |
my $info; |
41 |
sub info { |
42 |
my $port = shift; |
43 |
push @{ $info->{node}->{$port} }, [ @_ ]; |
44 |
} |
45 |
|
46 |
sub fork_ssh { |
47 |
my ( $port, $host ) = @_; |
48 |
|
49 |
if ( my $pid = fork ) { |
50 |
# parent |
51 |
info $port => 'forked', $pid; |
52 |
return $port; |
53 |
|
54 |
} elsif ( ! defined $pid ) { |
55 |
warn "can't fork $host $port"; |
56 |
return; |
57 |
} else { |
58 |
# child |
59 |
my $cmd = qq|ssh -F $cloud_path.ssh -R $port:127.0.0.1:$listen_port $host $node_path $port|; |
60 |
warn "# exec: $cmd\n"; |
61 |
exec $cmd; |
62 |
} |
63 |
} |
64 |
|
65 |
my $node_port = 4000; |
66 |
|
67 |
foreach my $host ( @cloud ) { |
68 |
system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional"; |
69 |
fork_ssh( $node_port++, $host ); |
70 |
} |
71 |
|
72 |
my $session; |
73 |
|
74 |
sub to_all { |
75 |
my $data = shift; |
76 |
foreach my $port ( keys %{ $session->{port} } ) { |
77 |
warn ">>>> [$port]\n"; |
78 |
Storable::store_fd( $data, $session->{port}->{$port} ); |
79 |
} |
80 |
} |
81 |
|
82 |
our @shard_load_queue; |
83 |
sub load_shard { |
84 |
my $shard = shift @_ || return; |
85 |
|
86 |
warn "# load_shard $shard\n"; |
87 |
|
88 |
my @shards = glob "$shard/*"; |
89 |
|
90 |
if ( ! @shards ) { |
91 |
warn "no shards for $shard\n"; |
92 |
return; |
93 |
} |
94 |
|
95 |
$info->{shard}->{$_} = 'wait' foreach @shards; |
96 |
warn "loading shard $shard from ", dump( @shards ); |
97 |
|
98 |
push @shard_load_queue, @shards; |
99 |
to_all { load => $shard }; |
100 |
} |
101 |
|
102 |
while (1) { |
103 |
for my $sock ($sel->can_read(1)) { |
104 |
if ($sock == $lsn) { |
105 |
my $new = $lsn->accept; |
106 |
$sel->add($new); |
107 |
$session->{peerport}->{ $new->peerport } = $new; |
108 |
warn "[socket] connect\n"; |
109 |
Storable::store_fd( { ping => 1 }, $new ); |
110 |
} elsif ( $sock == $www ) { |
111 |
my $client = $www->accept; |
112 |
Sack::Server::HTTP::request( $client, sub { |
113 |
my ( $send, $method, $param ) = @_; |
114 |
|
115 |
if ( $method =~ m{views} ) { |
116 |
warn "$method ", -s $method, " bytes"; |
117 |
my $code = read_file $method; |
118 |
Sack::Merge->clean; |
119 |
to_all { view => $code, path => $method }; |
120 |
print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n"; |
121 |
return 1; |
122 |
} elsif ( $method =~ m{^/tmp/sack} ) { |
123 |
load_shard $method; |
124 |
print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n"; |
125 |
return 1; |
126 |
} elsif ( $method =~ m{^/out/(.+)} ) { |
127 |
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
128 |
Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param ); |
129 |
return 1; |
130 |
} |
131 |
|
132 |
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
133 |
|
134 |
print $send qq|<h1>Views</h1><ul>| |
135 |
, join("\n", map { qq|<li><a href="$_">$_</a>| } glob '/srv/Sack/views/*/*.pl' ) |
136 |
, qq|</ul>| |
137 |
; |
138 |
|
139 |
my $out = Sack::Merge->out; |
140 |
print $send qq|<h1>Results</h1><ul>| |
141 |
, join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out ) |
142 |
, qq|</ul>| |
143 |
; |
144 |
|
145 |
print $send qq|<h1>Nodes</h1> |
146 |
<style type="text/css"> |
147 |
.forked { |
148 |
color: #f00; |
149 |
} |
150 |
|
151 |
.ping { |
152 |
color: #0f0; |
153 |
} |
154 |
|
155 |
.shards { |
156 |
font-family: monospace; |
157 |
line-height: 0.8em; |
158 |
color: #0f0; |
159 |
} |
160 |
|
161 |
.send { |
162 |
color: #ff0; |
163 |
} |
164 |
|
165 |
.wait { |
166 |
color: #f00; |
167 |
} |
168 |
|
169 |
|
170 |
</style> |
171 |
|, join("\n", map { |
172 |
my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} }); |
173 |
qq|<span class="$class">$_</span>|; |
174 |
} sort keys %{ $info->{node} } ); |
175 |
|
176 |
print $send qq|<h1>Data</h1><ul>|; |
177 |
foreach my $path ( glob '/tmp/sack/*' ) { |
178 |
print $send qq|<li><a href="$path">$path</a><div class="shards">|; |
179 |
foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) { |
180 |
my $i = $info->{shard}->{$s}; |
181 |
print $send qq|<span class=$i>$i</span> |; |
182 |
} |
183 |
print $send qq|</div>|; |
184 |
} |
185 |
print $send qq|</ul>|; |
186 |
|
187 |
# print $send '<pre>', dump($info), '</pre>'; |
188 |
|
189 |
} ); |
190 |
} else { |
191 |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
192 |
if ( $@ ) { |
193 |
delete $session->{$sock}; |
194 |
warn "[socket] disconnect: $@\n"; |
195 |
$sel->remove($sock); |
196 |
$sock->close; |
197 |
} else { |
198 |
warn "<<<< ", dump($data), $/; |
199 |
|
200 |
if ( my $path = $data->{shard} ) { |
201 |
$info->{shard}->{ $path } = $data->{port}; |
202 |
# FIXME will need push for multiple copies of shards |
203 |
} |
204 |
|
205 |
if ( my $repl = $data->{repl} ) { |
206 |
my $response = { repl_pid => $$ }; |
207 |
if ( $repl =~ m/ping/ ) { |
208 |
to_all { ping => 1 }; |
209 |
} elsif ( $repl =~ m/info/ ) { |
210 |
$response->{info} = $info; |
211 |
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
212 |
load_shard $1; |
213 |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
214 |
my $path = $1 || '/srv/Sack/views/00.demo.pl'; |
215 |
my $code = read_file $path; |
216 |
Sack::Merge->clean; |
217 |
to_all { view => $code, path => $path }; |
218 |
$response->{view}->{$path}->{running}; |
219 |
} elsif ( $repl =~ m{debug\s*(.+)?} ) { |
220 |
to_all { debug => $1 }; |
221 |
} elsif ( $repl =~ m{out} ) { |
222 |
my $out = Sack::Merge->out; |
223 |
warn "out ",dump( $out ); |
224 |
$response->{out} = $out; |
225 |
} elsif ( $repl =~ m{clean} ) { |
226 |
delete $info->{shard}; |
227 |
to_all { clean => 1 }; |
228 |
} else { |
229 |
$response->{error}->{unknown} = $data; |
230 |
} |
231 |
Storable::store_fd( $response, $sock ); |
232 |
} elsif ( $data->{ping} ) { |
233 |
my $port = $data->{port}; |
234 |
info $port => 'ping', $port; |
235 |
$session->{port}->{ $data->{port} } = $sock; |
236 |
} elsif ( $data->{load} eq 'shard' ) { |
237 |
if ( my $path = shift @shard_load_queue ) { |
238 |
$info->{shard}->{$path} = 'read'; |
239 |
my $shard = Storable::retrieve $path; |
240 |
$info->{shard}->{$path} = 'send'; |
241 |
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
242 |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
243 |
} else { |
244 |
warn "no more shards for [", $data->{port}, "]\n"; |
245 |
} |
246 |
} elsif ( defined $data->{out} ) { |
247 |
Sack::Merge->add( $data->{out} ); |
248 |
} else { |
249 |
warn "UNKNOWN ",dump($data); |
250 |
} |
251 |
} |
252 |
} |
253 |
} |
254 |
} |