/[Sack]/trunk/lib/Sack/Server.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Sack/Server.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 209 - (show annotations)
Sat Nov 21 13:59:41 2009 UTC (14 years, 6 months ago) by dpavlin
File size: 3955 byte(s)
use etc/cloud as default if it exists

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26