15 |
|
|
16 |
use lib '/srv/Sack/lib'; |
use lib '/srv/Sack/lib'; |
17 |
use Sack::Merge; |
use Sack::Merge; |
18 |
|
use Sack::Server::HTTP; |
19 |
|
use Sack::Server::HTML; |
20 |
|
|
21 |
my @cloud; |
my @cloud; |
22 |
my $cloud_path = $ENV{CLOUD} || die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n"; |
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; |
@cloud = read_file $cloud_path; |
25 |
@cloud = map { chomp $_; $_ } @cloud; |
@cloud = map { chomp $_; $_ } @cloud; |
26 |
|
|
27 |
warn "# cloud ",dump( @cloud ); |
warn "# cloud ",dump( @cloud ); |
28 |
|
|
29 |
my $listen_port = 4444; |
my $listen_port = 4444; |
30 |
|
my $http_port = 4480; |
31 |
|
|
32 |
my $node_path = abs_path $0; |
my $node_path = abs_path $0; |
33 |
$node_path =~ s{Server}{Client}; |
$node_path =~ s{Server}{Client}; |
34 |
|
|
35 |
my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die $!; |
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); |
my $sel = IO::Select->new($lsn); |
38 |
|
$sel->add( $www ); |
39 |
|
|
40 |
my $info; |
my $info; |
41 |
sub info { |
sub info { |
42 |
my $port = shift; |
my $port = shift; |
43 |
push @{ $info->{$port} }, [ @_ ]; |
push @{ $info->{node}->{$port} }, [ @_ ]; |
44 |
} |
} |
45 |
|
|
46 |
sub fork_ssh { |
sub fork_ssh { |
90 |
warn "[socket] connect\n"; |
warn "[socket] connect\n"; |
91 |
Storable::store_fd( { ping => 1 }, $new ); |
Storable::store_fd( { ping => 1 }, $new ); |
92 |
info 0 => 'ping', $new->peerport; |
info 0 => 'ping', $new->peerport; |
93 |
|
} elsif ( $sock == $www ) { |
94 |
|
my $client = $www->accept; |
95 |
|
Sack::Server::HTTP::request( $client, sub { |
96 |
|
my ( $send, $method, $param ) = @_; |
97 |
|
|
98 |
|
if ( $method =~ m{views} ) { |
99 |
|
warn "$method ", -s $method, " bytes"; |
100 |
|
my $code = read_file $method; |
101 |
|
Sack::Merge->clean; |
102 |
|
to_all { view => $code, path => $method }; |
103 |
|
print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n"; |
104 |
|
return 1; |
105 |
|
} elsif ( $method =~ m{^/tmp/sack} ) { |
106 |
|
@shard_paths = glob "$method/*"; |
107 |
|
warn "loading shard $method from ", dump( @shard_paths ); |
108 |
|
to_all { load => $method }; |
109 |
|
print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n"; |
110 |
|
return 1; |
111 |
|
} elsif ( $method =~ m{^/out/(.+)} ) { |
112 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
113 |
|
Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param ); |
114 |
|
return 1; |
115 |
|
} |
116 |
|
|
117 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
118 |
|
|
119 |
|
print $send qq|<h1>Views</h1><ul>| |
120 |
|
, join("\n", map { qq|<li><a href="$_">$_</a>| } glob '/srv/Sack/views/*/*.pl' ) |
121 |
|
, qq|</ul>| |
122 |
|
; |
123 |
|
|
124 |
|
my $out = Sack::Merge->out; |
125 |
|
print $send qq|<h1>Results</h1><ul>| |
126 |
|
, join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out ) |
127 |
|
, qq|</ul>| |
128 |
|
; |
129 |
|
|
130 |
|
print $send '<h1>Info</h1><pre>' . dump( $info ) . '</pre>'; |
131 |
|
|
132 |
|
print $send qq|<h1>Data</h1><ul>| |
133 |
|
, join("\n", map { qq|<li><a href="$_">$_</a>| } glob '/tmp/sack/*' ) |
134 |
|
, qq|</ul>| |
135 |
|
; |
136 |
|
} ); |
137 |
} else { |
} else { |
138 |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
139 |
if ( $@ ) { |
if ( $@ ) { |
145 |
warn "<<<< ", dump($data), $/; |
warn "<<<< ", dump($data), $/; |
146 |
|
|
147 |
if ( my $path = $data->{shard} ) { |
if ( my $path = $data->{shard} ) { |
148 |
push @{ $info->{shard}->{ $data->{port} } }, $path; |
$info->{shard}->{ $path } = $data->{port}; |
149 |
|
# FIXME will need push for multiple copies of shards |
150 |
} |
} |
151 |
|
|
152 |
if ( my $repl = $data->{repl} ) { |
if ( my $repl = $data->{repl} ) { |