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 |
|
20 |
my @cloud; |
21 |
my $cloud_path = $ENV{CLOUD} || "etc/cloud"; |
22 |
die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n" unless -e $cloud_path; |
23 |
@cloud = read_file $cloud_path; |
24 |
@cloud = map { chomp $_; $_ } @cloud; |
25 |
|
26 |
warn "# cloud ",dump( @cloud ); |
27 |
|
28 |
my $listen_port = 4444; |
29 |
my $http_port = 4480; |
30 |
|
31 |
my $node_path = abs_path $0; |
32 |
$node_path =~ s{Server}{Client}; |
33 |
|
34 |
my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die "$listen_port $!"; |
35 |
my $www = IO::Socket::INET->new(Listen => 1, LocalPort => $http_port, Reuse => 1) or die "$http_port $!"; |
36 |
my $sel = IO::Select->new($lsn); |
37 |
$sel->add( $www ); |
38 |
|
39 |
my $info; |
40 |
sub info { |
41 |
my $port = shift; |
42 |
push @{ $info->{node}->{$port} }, [ @_ ]; |
43 |
} |
44 |
|
45 |
sub fork_ssh { |
46 |
my ( $port, $host ) = @_; |
47 |
|
48 |
if ( my $pid = fork ) { |
49 |
# parent |
50 |
info $port => 'forked', $pid; |
51 |
return $port; |
52 |
|
53 |
} elsif ( ! defined $pid ) { |
54 |
warn "can't fork $host $port"; |
55 |
return; |
56 |
} else { |
57 |
# child |
58 |
my $cmd = qq|ssh -F $cloud_path.ssh -R $port:127.0.0.1:$listen_port $host $node_path $port|; |
59 |
warn "# exec: $cmd\n"; |
60 |
exec $cmd; |
61 |
} |
62 |
} |
63 |
|
64 |
my $node_port = 4000; |
65 |
|
66 |
foreach my $host ( @cloud ) { |
67 |
system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional"; |
68 |
fork_ssh( $node_port++, $host ); |
69 |
} |
70 |
|
71 |
my $session; |
72 |
|
73 |
sub to_all { |
74 |
my $data = shift; |
75 |
foreach my $port ( keys %{ $session->{port} } ) { |
76 |
warn ">>>> [$port]\n"; |
77 |
Storable::store_fd( $data, $session->{port}->{$port} ); |
78 |
} |
79 |
} |
80 |
|
81 |
my @shard_paths; |
82 |
|
83 |
while (1) { |
84 |
for my $sock ($sel->can_read(1)) { |
85 |
if ($sock == $lsn) { |
86 |
my $new = $lsn->accept; |
87 |
$sel->add($new); |
88 |
$session->{peerport}->{ $new->peerport } = $new; |
89 |
warn "[socket] connect\n"; |
90 |
Storable::store_fd( { ping => 1 }, $new ); |
91 |
info 0 => 'ping', $new->peerport; |
92 |
} elsif ( $sock == $www ) { |
93 |
my $client = $www->accept; |
94 |
Sack::Server::HTTP::request( $client, sub { |
95 |
warn "XXX callback ",dump( @_ ); |
96 |
my ( $send, $method, $param ) = @_; |
97 |
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n" |
98 |
, dump( $info ) |
99 |
; |
100 |
} ); |
101 |
} else { |
102 |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
103 |
if ( $@ ) { |
104 |
delete $session->{$sock}; |
105 |
warn "[socket] disconnect: $@\n"; |
106 |
$sel->remove($sock); |
107 |
$sock->close; |
108 |
} else { |
109 |
warn "<<<< ", dump($data), $/; |
110 |
|
111 |
if ( my $path = $data->{shard} ) { |
112 |
$info->{shard}->{ $path } = $data->{port}; |
113 |
# FIXME will need push for multiple copies of shards |
114 |
} |
115 |
|
116 |
if ( my $repl = $data->{repl} ) { |
117 |
my $response = { repl_pid => $$ }; |
118 |
if ( $repl =~ m/ping/ ) { |
119 |
to_all { ping => 1 }; |
120 |
} elsif ( $repl =~ m/info/ ) { |
121 |
$response->{info} = $info; |
122 |
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
123 |
my $name = $1 || 'shard'; |
124 |
@shard_paths = glob "/tmp/sack/$name/*"; |
125 |
warn "loading shard $name from ", dump( @shard_paths ); |
126 |
to_all { load => $name }; |
127 |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
128 |
my $path = $1 || '/srv/Sack/views/00.demo.pl'; |
129 |
my $code = read_file $path; |
130 |
Sack::Merge->clean; |
131 |
to_all { view => $code, path => $path }; |
132 |
$response->{view}->{$path}->{running}; |
133 |
} elsif ( $repl =~ m{debug\s*(.+)?} ) { |
134 |
to_all { debug => $1 }; |
135 |
} elsif ( $repl =~ m{out} ) { |
136 |
my $out = Sack::Merge->out; |
137 |
warn "out ",dump( $out ); |
138 |
$response->{out} = $out; |
139 |
} elsif ( $repl =~ m{clean} ) { |
140 |
delete $info->{shard}; |
141 |
to_all { clean => 1 }; |
142 |
} else { |
143 |
$response->{error}->{unknown} = $data; |
144 |
} |
145 |
Storable::store_fd( $response, $sock ); |
146 |
} elsif ( $data->{ping} ) { |
147 |
my $port = $data->{port}; |
148 |
info $port => 'ping', $port; |
149 |
$session->{port}->{ $data->{port} } = $sock; |
150 |
} elsif ( $data->{load} eq 'shard' ) { |
151 |
if ( my $path = shift @shard_paths ) { |
152 |
warn "retrieve $path ", -s $path; |
153 |
my $shard = Storable::retrieve $path; |
154 |
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
155 |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
156 |
} else { |
157 |
warn "no more shards for [", $data->{port}, "]\n"; |
158 |
} |
159 |
} elsif ( defined $data->{out} ) { |
160 |
Sack::Merge->add( $data->{out} ); |
161 |
} else { |
162 |
warn "UNKNOWN ",dump($data); |
163 |
} |
164 |
} |
165 |
} |
166 |
} |
167 |
} |