ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/bin/aemp
Revision: 1.57
Committed: Wed Mar 21 21:02:04 2012 UTC (12 years, 2 months ago) by root
Branch: MAIN
Changes since 1.56: +14 -5 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.38 #!/opt/bin/perl
2 root 1.1
3     =head1 NAME
4    
5     aemp - AnyEvent:MP utility
6    
7     =head1 SYNOPSIS
8    
9     aemp command args...
10    
11     # protocol commands
12 root 1.19 aemp snd <port> <arg...> # send a message
13     aemp mon <port> # wait till port is killed
14 root 1.41 aemp cal <port> <arg...> # send message, append reply
15 root 1.19 aemp eval <node> <expr...> # evaluate expression
16 root 1.42 aemp shell [<node>] # run an interactive shell
17 root 1.37 aemp trace <node> # trace the network topology
18 root 1.8
19     # run a node
20 root 1.26 aemp run configure_args... # run a node
21 root 1.37 aemp restart <node> # restart a node if running under watchdog
22 root 1.8
23 root 1.23 # node configuration: node ID
24 root 1.19 aemp setnodeid <nodeid> # configure the real node id
25     aemp delnodeid # reset node id to default (= inherit)
26 root 1.1
27 root 1.47 # node configuration: authentication
28 root 1.19 aemp gensecret # generate a random shared secret
29     aemp setsecret <secret> # set the shared secret
30     aemp delsecret # remove the secret (= inherit)
31     aemp gencert # generate a random certificate
32     aemp setcert <file> # set a certificate (key.pem + certificate.pem)
33     aemp delcert # remove certificate (= inherit)
34 root 1.8
35 root 1.17 # node configuration: seed addresses for bootstrapping
36 root 1.19 aemp setseeds <host:port>,... # set seeds
37     aemp delseeds # clear all seeds (= inherit)
38     aemp addseed <host:port> # add a seed
39     aemp delseed <host:port> # remove seed
40 root 1.17
41     # node configuration: bind addresses
42 root 1.19 aemp setbinds <host:port>,... # set binds
43     aemp delbinds # clear all binds (= inherit)
44     aemp addbind <host:port> # add a bind address
45     aemp delbind <host:port> # remove a bind address
46 root 1.8
47     # node configuration: services
48 root 1.19 aemp setservices initfunc,... # set service functions
49     aemp delservices # clear all services (= inherit)
50     aemp addservice <initfunc> # add an instance of a service
51     aemp delservice <initfunc> # delete one instance of a service
52 root 1.6
53 root 1.47 # profile management
54 root 1.10 aemp profile <name> <command>... # apply command to profile only
55 root 1.26 aemp setparent <name> # specify a parent profile
56 root 1.25 aemp delparent # clear parent again
57 root 1.19 aemp delprofile <name> # eradicate the named profile
58     aemp showprofile <name> # display given profile
59 root 1.25 aemp showconfig <name> ... # display effective config
60 root 1.10
61 root 1.47 # node configuration: low-level protocol
62 root 1.54 aemp [set|del]secure <boolean>
63 root 1.47 aemp [set|del]monitor_timeout <seconds>
64     aemp [set|del]connect_interval <seconds>
65     aemp [set|del]framing_format [array]
66     aemp [set|del]auth_offer [array]
67     aemp [set|del]auth_accept [array]
68     aemp [set|del]autocork <boolean>
69     aemp [set|del]nodelay <boolean>
70    
71 root 1.1 =head1 DESCRIPTION
72    
73 root 1.8 With aemp you can configure various aspects of AnyEvent::MP and its
74 root 1.22 protocol, send various messages and even run a node.
75 root 1.1
76 root 1.23 The F<aemp> utility works like F<cvs>, F<svn> or other commands: the first
77     argument defines which operation (subcommand) is requested, after which
78     arguments for this operation are expected. When a subcommand does not eat
79     all remaining arguments, the remaining arguments will again be interpreted
80     as subcommand and so on.
81    
82     This means you can chain multiple commands, which is handy for profile
83     configuration, e.g.:
84    
85 root 1.27 aemp gensecret profile xyzzy binds 4040,4041 nodeid anon/
86    
87     Please note that all C<setxxx> subcommands have an alias without the
88     C<set> prefix.
89 root 1.23
90 root 1.34 All configuration data is stored in a human-readable (JSON) config file
91 root 1.46 stored in F<~/.perl-anyevent-mp> (or F<%appdata%/perl-anyevent-mp> on
92 root 1.49 loser systems, or wherever C<$ENV{PERL_ANYEVENT_MP_RC}> points to). Feel
93 root 1.46 free to look at it or edit it, the format is relatively simple.
94 root 1.34
95 root 1.35 =head2 SPECIFYING ARGUMENTS
96    
97     Arguments can be specified just as with any other shell command, with a
98     few special cases:
99    
100     If the I<first> argument starts with a literal C<[>-character, then it is
101     interpreted as a UTF-8 encoded JSON text. The resulting array replaces all
102     arguments.
103    
104     Otherwise, if I<any> argument starts with one of C<[>, C<{> or C<">, then
105     it is interpreted as UTF-8 encoded JSON text (or a single value in case of
106     C<">), and the resulting reference or scalar replaces the argument.
107    
108     This allows you, for example, to specify binds in F<aemp run> (using POSIX
109     shell syntax):
110    
111     aemp run binds '["*:4040"]'
112    
113 root 1.23 =head2 RUNNING A NODE
114    
115     This can be used to run a node - together with some services, this makes
116 elmex 1.32 it unnecessary to write any wrapper programs.
117 root 1.23
118     =over 4
119    
120 root 1.30 =item run <configure_args>...
121 root 1.23
122 root 1.26 Runs a node by calling C<AnyEvent::MP::Kernel::configure> with the given
123 root 1.23 arguments. The node runs under L<AnyEvent::Watchdog>, can be restarted
124 root 1.30 (and autorestarted, see the L<AnyEvent::Watchdog> manual). A very common
125 root 1.33 invocation is to just specify a profile using the profile name
126 root 1.30
127 root 1.33 aemp run database-backend
128 root 1.30
129     ... but you can use most arguments that C<configure> understands:
130    
131     aemp run nodeid mynode2 profile someprofile
132 root 1.23
133     Care has been taken to load (almost) no modules other than
134     L<AnyEvent::Watchdog> and the modules it loads, so everything (including
135     the L<AnyEvent::MP> modules themselves) will be freshly loaded on restart,
136     which makes upgrading everything except the perl binary easy.
137    
138 root 1.37 =item restart <node>
139    
140     Restarts the node using C<AnyEvent::Watchdog::Util::restart>. This works
141     for nodes started by C<aemp run>, but also for any other node that uses
142     L<AnyEvent::Watchdog>.
143    
144 root 1.23 =back
145    
146     =head2 PROTOCOL COMMANDS
147    
148     These commands actually communicate with other nodes. They all use a node
149 elmex 1.32 profile name of C<aemp> (specifying a default node ID of C<anon/> and a
150 root 1.25 binds list containing C<*:*> only).
151 root 1.23
152     They all use a timeout of five seconds, after which they give up.
153    
154     =over 4
155    
156     =item snd <port> <arguments...>
157    
158     Simply send a message to the given port - where you get the port ID from
159     is your problem.
160    
161     Exits after ensuring that the message has been delivered to its node.
162    
163 elmex 1.32 Most useful to take advantage of some undocumented functionality inside
164 root 1.23 nodes, such as node ports being able to call any method:
165    
166     aemp snd doomed AnyEvent::Watchdog::restart 1
167    
168 root 1.41 =item cal <port> <arg...>
169 root 1.23
170 root 1.41 Like F<aemp cal>: appends a local reply port to the message and waits for
171     a message to it.
172 root 1.23
173     Any return values will be JSON-encoded and printed separated by commas
174     (kind of like a JSON array without []-brackets).
175    
176 root 1.41 Example: ask the (undocumented) time service of a node for its current
177 root 1.23 time.
178    
179 root 1.41 aemp cal mynode time
180 root 1.23
181     =item mon <port>
182    
183     Monitors the port and exits when it's monitorign callback is called. Most
184     useful to monitor node ports.
185    
186     Example: monitor some node.
187    
188     aemp mon doomed
189    
190     =item eval <node> <expr...>
191    
192     Joins all remaining arguments into a string and evaluates it on the given
193 root 1.41 node. Return values are handled as with F<aemp cal>.
194 root 1.23
195     Example: find the unix process ID of the node called posicks.
196    
197     aemp eval posicks '$$'
198    
199     =item trace <node>
200    
201     Asks the given node for all currently connected nodes, then asks those
202     nodes for the same, thus tracing all node connections.
203    
204 root 1.31 =back
205 root 1.23
206     =head2 CONFIGURATION/NODE ID/SECRET/CERTIFICATE
207    
208     These commands deal with rather basic settings, the node ID, the shared
209     secret and the TLS certificate.
210    
211     =over 4
212    
213     =item setnodeid <nodeid>
214    
215 root 1.50 Set the node ID to the given string. If it ends with a slash (C</>), then
216     a random string is appended to make it unique.
217    
218     If no nodeid is specified in any profile, then the profile name, plus
219     appended slash, is used.
220 root 1.23
221     =item delnodeid
222    
223     Removes the node ID again, which means it is inherited again from it's
224     parent profile, or stays unset.
225    
226     =item gensecret
227    
228 root 1.51 Generates a random shared secret (currently 1071 bits) and sets it. The
229     shared secret is used to authenticate nodes to each other when TLS is not
230     required.
231 root 1.23
232     =item setsecret <secret>
233    
234 root 1.51 Sets the shared secret to the given string, which can be anything.
235 root 1.23
236     =item delsecret
237    
238     Removes the shared secret again, which means it is inherited again from
239     it's parent profile, or stays unset.
240    
241     =item gencert
242    
243 elmex 1.32 Generates a self-signed certificate and key, and sets it. This works
244 root 1.23 similarly to a shared secret: when all nodes have it, TLS will be used to
245     authenticate and encrypt all traffic.
246    
247     =item setcert <file>
248    
249     Set a node certificate (and optionally any CA certificates) from the given
250     file. The file must contain the key, followed by the certificate, followed
251     by any CA certificates you want to trust, all in PEM format.
252    
253     See L<AnyEvent::TLS> for some more details - this sets the C<cert> and
254     C<ca_cert> options.
255    
256     =item delcert
257    
258     Removes the certificate(s) again, which means it is inherited again from
259     it's parent profile, or stays unset.
260    
261     =back
262    
263     =head2 CONFIGURATION/SEEDS
264    
265     To discover the network you have to specify some seed addresses, which are
266     basically C<host:port> pairs where you expect some long-running nodes. It
267     does no harm to have a node as its own seed (they will eventually be
268     ignored).
269    
270 root 1.31 =over 4
271    
272 root 1.23 =item setseeds <host:port>,...
273    
274     Sets or replaces the list of seeds, which must be specified as a
275     comma-separated list of C<host:port> pairs. The C<host> can be a hostname,
276 root 1.36 an IP address, or C<*> to signify all local host addresses (which makes
277     little sense for seeds, outside some examples, but a lot of sense for
278     binds).
279 root 1.23
280     An empty list is allowed.
281    
282     Example: use C<doomed> with default port as only seednode.
283    
284     aemp setseeds doomed
285    
286     =item delseeds
287    
288     Removes the seed list again, which means it is inherited again from it's
289     parent profile, or stays unset.
290    
291     =item addseed <host:port>
292    
293     Adds a single seed address.
294    
295     =item delseed <host:port>
296    
297     Deletes the given seed address, if it exists.
298    
299     =back
300    
301     =head2 CONFIGURATION/BINDS
302    
303     To be able to be reached from other nodes, a node must I<bind> itself
304     to some listening socket(s). The list of these can either bs specified
305     manually, or AnyEvent::MP can guess them. Nodes without any binds are
306     possible to some extent.
307    
308     =over 4
309    
310     =item setbinds <host:port>,...
311    
312     Sets the list of bind addresses explicitly - see the F<aemp setseeds>
313 root 1.36 command for the exact syntax. In addition, a value of C<*> for the port,
314     or not specifying a port, means to use a dynamically-assigned port.
315 root 1.23
316 root 1.36 Note that the C<*>, C<*:*> or C<*:port> patterns are very useful here.
317 root 1.23
318 root 1.36 Example: bind on a ephemeral port on all local interfaces.
319 root 1.23
320     aemp setbinds "*"
321    
322     Example: bind on a random port on all local interfaces.
323    
324 root 1.25 aemp setbinds "*:*"
325 root 1.23
326     Example: resolve "doomed.mydomain" and try to bind on port C<4040> of all
327     IP addressess returned.
328    
329 root 1.36 aep setbinds doomed.mydomain:4040
330 root 1.23
331     =item delbinds
332    
333     Removes the bind list again, which means it is inherited again from it's
334     parent profile, or stays unset.
335    
336     =item addbind <host:port>
337    
338     Adds a single bind address.
339    
340     =item delbind <host:port>
341    
342     Deletes the given bind address, if it exists.
343    
344     =back
345    
346     =head2 CONFIGURATION/SERVICES
347    
348     Services are modules (or functions) that are automatically loaded (or
349     executed) when a node starts. They are especially useful when used in
350     conjunction with F<aemp run>, to configure which services a node should
351     run.
352    
353 root 1.47 Despite the daunting name, services really I<are> nothing more than a
354     module name or a function name with arguments. The "service" aspect comes
355     only from the behaviour of the module or function, which is supposed to
356     implement, well, some kind of service for the node, network etc.
357    
358     Instead of writing a standalone program for each different node type in
359     your network, you can simply put your code into a module, and then let the
360     configuration decide which node runs which "services". This also makes it
361     easy to combine multiple different services within the same node.
362    
363 root 1.23 =over 4
364    
365     =item setservices <initfunc>...
366    
367     Sets or replaces the list of services, which must be specified as a
368 root 1.37 comma-separated list or a JSON array.
369    
370     Each string entry in the list is interpreted as either a module name to
371     load (when it ends with C<::>) or a function to call (all other cases).
372    
373     Each entry which is an array itself (you need to use JSON format to
374     specify those) is interpreted as a function name and the arguments to
375     pass.
376 root 1.23
377 root 1.37 The algorithm to find the function is the same as used for C<<
378 root 1.23 L<AnyEvent::MP>::spawn >>.
379    
380 root 1.37 Example: run the globa service (which runs anyway, but this doesn't hurt).
381    
382     aemp setservices AnyEvent::MP::Global::
383    
384     Example: call the mymod::myfun function with arguments 1, 2 and 3.
385    
386     aemp setservices '[["mymod::myfun", 1,2,3]]'
387    
388 root 1.23 =item delservices
389    
390     Removes the service list again, which means it is inherited again from
391     it's parent profile, or stays unset.
392    
393     =item addservice <initfunc>
394    
395     Adds a single service.
396    
397     =item delservice <initfunc>
398    
399     Deletes the given service, if it exists.
400    
401     =back
402    
403     =head2 CONFIGURATION/PROFILE MANAGEMENT
404    
405     All the above configuration functions by default affect the I<global
406     default configuration>, which is basically used to augment every profile
407     and node configuration.
408    
409     =over 4
410    
411     =item profile <name> ...
412    
413     This subcommand makes the following subcommands act only on a specific
414     named profile, instead of on the global default. The profile is created if
415     necessary.
416    
417     Example: create a C<server> profile, give it a random node name, some seed
418     nodes and bind it on an unspecified port on all local interfaces. You
419     should add some services then and run the node...
420    
421 root 1.30 aemp profile server nodeid anon/ seeds doomed,10.0.0.2:5000 binds "*:*"
422 root 1.23
423     =item delprofile <name>
424    
425     Deletes the profile of the given name.
426    
427 root 1.26 =item setparent <name>
428 root 1.24
429     Sets the parent profile to use - values not specified in a profile will be
430     taken from the parent profile (even recursively, with the global default
431     config being the default parent). This is useful to configure profile
432     I<classes> and then to inherit from them for individual nodes.
433    
434     Note that you can specify circular parent chains and even a parent for the
435     global configuration. Neither will do you any good, however.
436    
437     Example: inherit all values not specified in the C<doomed> profile from
438     the C<server> profile.
439    
440 root 1.26 aemp profile doomed setparent server
441 root 1.24
442     =item delparent
443    
444     Removes the parent again from the profile, if any was set, so the profile
445     inherits directly from the global default config again.
446    
447 root 1.23 =item showprofile <name>
448    
449     Shows the values of the given profile, and only those, no inherited
450     values.
451    
452 root 1.25 =item showconfig <name> <key value...>
453 root 1.23
454 root 1.24 Shows the I<effective> config, i.e. the values as used by a node started
455 root 1.25 with the given profile name. Any additional key-value pairs specified
456 root 1.26 augment the configuration, just as with C<configure>.
457 root 1.24
458 root 1.25 If all arguments are omitted, show the global default config.
459 root 1.23
460     =back
461    
462 root 1.47 =head2 LOW-LEVEL TRANSPORT PROTOCOL
463    
464     The low-level transport protocol betwene two nodes also has a number of
465     configurable options, most of which should not be touched unless you know
466     what you are doing.
467    
468     =over 4
469    
470 root 1.54 =item [set|del]secure <boolean>
471    
472     Normally, nodes allow anything to be done to them by remote nodes,
473     including remotely-triggered execution of code.
474    
475     Sometimes a more secure mode is desired - this can be achieved by setting
476     the secure option to a true value.
477    
478 root 1.56 When secure mode is enabled, then the node will not execute code locally,
479     at least not via the normal node protocol. All other messages are still
480     allowed. This means remote nodes can monitor, kill or local ports (port
481     names can be easily guessed). Specifically, note that the very common
482     "send me a list that I prepend to my reply message" idiom can easily
483     be used to subvert this security mechanism by asking a trusted node to
484     "reply" to some other message.
485 root 1.54
486 root 1.56 At the moment, this setting affects C<snd_on>, C<eval_on> and C<spawn>
487 root 1.55 functionality.
488 root 1.54
489     The C<configure> function additionally allows you to specify a callback
490     that can grant or suppress such requests on a per-node basis.
491    
492 root 1.47 =item [set|del]monitor_timeout <seconds>
493    
494     Sets the default monitor timeout, that is, when a connection to a node
495     cannot be established within this many seconds, the node is declared
496     unreachable and all monitors will fire.
497    
498     C<30> seconds are usually a good time span for this.
499    
500     =item [set|del]connect_interval <seconds>
501    
502     When a connection cannot be established successfully within this many
503     seconds, try the next transport address (e.g. the next IP address). If
504     your nodes have a lot of transports, you might have to set this to a low
505     value so that they will actually all be tried within the monitor timeout
506     interval.
507    
508     C<2> is usually a good value, unless you live in new zealand.
509    
510     =item [set|del]framing_format [array]
511    
512     Configures the list of framing formats offered to the other side. This is
513     simply a list of formatted read/write types used with L<AnyEvent::Handle>,
514     in order of decreasing preference.
515    
516     Nodes support both C<json> and C<storable> framing formats for data
517     packets out of the box, and usually choose C<json> because it is first in
518     the list.
519    
520     Example: prefer the C<My::Personal::Format> framing format over JSON over
521     Storable.
522    
523     aemp setframing_format '["My::Personal::Format", "json", "storable"]'
524    
525     =item [set|del]auth_offer [array]
526    
527     Configures the list of authentication types that the node offers to the
528     other side as acceptable, in order of decreasing preference. Only auth
529     methods that the node can actually support will be offered.
530    
531     The default is '["tls_md6_64_256", "hmac_md6_64_256"]' and is usually good
532     enough.
533    
534     =item [set|del]auth_accept [array]
535    
536     Configures the list of authentication types that remote nodes can use to
537     authenticate, in order of decreasing preference.
538    
539     The default is '["tls_md6_64_256", "hmac_md6_64_256", "tls_anon",
540     "cleartext"]' and is usually good enough.
541    
542     =item [set|del]autocork <boolean>
543    
544     Sets the default C<autocork> option value for the L<AnyEvent::Handle>
545     object used by transports. By default, autocorking is off.
546    
547     =item [set|del]nodelay <boolean>
548    
549     Sets the default C<nodelay> option value for the L<AnyEvent::Handle>
550     object used by transports. By default, nodelay is on.
551    
552     =back
553    
554 root 1.1 =cut
555    
556     use common::sense;
557    
558 root 1.19 # should come before anything else, so all modules
559     # will be loaded on each restart
560 root 1.22 BEGIN {
561 root 1.35 if (@ARGV == 1 && $ARGV[0] =~ /^\[/) {
562     require JSON::XS;
563     @ARGV = @{ JSON::XS->new->utf8->decode (shift) };
564     } else {
565     for (@ARGV) {
566     if (/^[\[\{\"]/) {
567     require JSON::XS;
568     $_ = JSON::XS->new->utf8->allow_nonref->decode ($_);
569     }
570     }
571     }
572    
573 root 1.8 if ($ARGV[0] eq "run") {
574     shift;
575    
576 root 1.13 # d'oh
577 root 1.22 require AnyEvent::Watchdog;
578 root 1.48 # only now can we load additional modules
579    
580     require AnyEvent;
581 root 1.19
582 root 1.29 require AnyEvent::Watchdog::Util;
583     AnyEvent::Watchdog::Util::autorestart (1);
584     AnyEvent::Watchdog::Util::heartbeat (300);
585 root 1.13
586 root 1.25 require AnyEvent::MP::Kernel;
587 root 1.26 AnyEvent::MP::Kernel::configure (@ARGV);
588 root 1.8
589 root 1.16 AnyEvent::detect () eq "AnyEvent::Impl::EV"
590 root 1.8 ? EV::loop ()
591     : AE::cv ()->recv;
592     }
593 root 1.22 }
594 root 1.8
595 root 1.1 use Carp ();
596    
597 root 1.17 use JSON::XS;
598    
599 root 1.1 use AnyEvent;
600 root 1.4 use AnyEvent::Util;
601    
602 root 1.8 use AnyEvent::MP;
603 root 1.1 use AnyEvent::MP::Config;
604    
605     sub my_run_cmd {
606     my ($cmd) = @_;
607    
608     my $cv = &run_cmd;
609     my $status = $cv->recv;
610    
611     $status
612     and die "@$cmd: command failed with exit status $status.";
613     }
614    
615     sub gen_cert {
616 root 1.2 my_run_cmd [qw(openssl req
617     -new -nodes -x509 -days 3650
618     -newkey rsa:2048 -keyout /dev/fd/3
619     -batch -subj /CN=AnyEvent::MP
620     )],
621 root 1.5 "<", "/dev/null",
622 root 1.1 ">" , \my $cert,
623     "3>", \my $key,
624 root 1.4 "2>", "/dev/null";
625 root 1.1
626     "$cert$key"
627     }
628    
629 root 1.25 sub init {
630 root 1.53 configure profile => "aemp", nodeid => "aemp/%n/%u";
631 root 1.25 }
632    
633 root 1.10 our $cfg = AnyEvent::MP::Config::config;
634 root 1.8 our $profile = $cfg;
635 root 1.1
636 root 1.11 sub trace {
637 root 1.56 my ($seed) = @_;
638 root 1.11 my $cv = AE::cv;
639     my %seen;
640 root 1.57 my $exit;
641 root 1.11
642 root 1.57 my %to;
643 root 1.11
644 root 1.25 init;
645 root 1.11
646     my $reply = port {
647 root 1.55 my ($node, undef, @neigh) = @_;
648 root 1.11
649 root 1.57 delete $to{$node};
650    
651 root 1.12 @neigh = grep $_ ne $NODE, @neigh;
652    
653 root 1.55 print $node, " -> ", (join " ", @neigh), "\n";
654 root 1.12
655 root 1.57 for my $neigh (@neigh) {
656     unless ($seen{$neigh}++) {
657 root 1.11 $cv->begin;
658 root 1.57 $to{$neigh} = AE::timer 15, 0, sub {
659     print "$neigh (timeout)\n";
660     $exit = 1;
661     $cv->end;
662     };
663     AnyEvent::MP::Kernel::eval_on $neigh, "AnyEvent::MP::Kernel::up_nodes" => $SELF => $neigh;
664 root 1.11 }
665     }
666    
667     $cv->end;
668     };
669    
670     $cv->begin;
671 root 1.56 snd $reply, seed => undef, $seed;
672 root 1.11
673     $cv->recv;
674 root 1.57
675     exit $exit;
676 root 1.11 }
677    
678 root 1.42 sub shell {
679     init;
680    
681     my $node = shift @ARGV || $NODE;
682     $| = 1;
683    
684     print <<EOF;
685     Entering interactive shell - no commandline editing of course (use rlfe etc.).
686    
687 root 1.43 \= display a list of nodes
688     \=name switch to another node
689 root 1.51 package P switch to package P when evaluating
690     \$ECHO contains the name of a port that echos everything sent to it
691 root 1.42
692     EOF
693 root 1.52 my $pkg = "AnyEvent::MP::Kernel";
694 root 1.42 my $cv = AE::cv;
695 root 1.51 my $echo = port {
696 root 1.56 print "\n ECHO<$AnyEvent::MP::Kernel::SRCNODE> ", JSON::XS::encode_json \@_, "\n$node $pkg> ";
697 root 1.51 };
698 root 1.52 print "$node $pkg> ";
699 root 1.42 my $t = AE::io *STDIN, 0, sub {
700     chomp (my $line = <STDIN>);
701    
702     if ($line =~ s/^=//) {
703     if (length $line) {
704     $node = $line;
705     } else {
706     print +(join " ", AnyEvent::MP::Kernel::up_nodes), "\n";
707     }
708 root 1.51 } elsif ($line =~ /^\s*package\s+(\S+)\s*;?\s*$/) {
709 root 1.52 $pkg = $1;
710 root 1.51 } elsif ($line =~ /\S/) {
711 root 1.42 my $time = AE::time;
712 root 1.52 AnyEvent::MP::Kernel::eval_on
713     $node,
714     "package $pkg; my \$ECHO = '$echo'; $line",
715     port {
716     kil $SELF;
717    
718     my ($err, @res) = @_;
719    
720     $time = AE::time - $time;
721    
722     print "\n $node: $line\n";
723     if (length $err) {
724     print " $err @res";
725     } else {
726     print " ", JSON::XS::encode_json \@res;
727     }
728     printf "\n %0.3fs\n", $time;
729     print "$node $pkg> ";
730 root 1.42 }
731 root 1.52 ;
732 root 1.42 }
733    
734 root 1.52 print "$node $pkg> ";
735 root 1.42 };
736     $cv->recv;
737     }
738    
739 root 1.37 sub node_eval {
740     my ($node, $expr) = @_;
741    
742     init;
743    
744     my $cv = AE::cv;
745     my $to = AE::timer 5, 0, sub { exit 1 };
746     AnyEvent::MP::Kernel::eval_on $node, $expr, port { &$cv };
747     mon $node, $cv;
748    
749     my ($err, @res) = $cv->recv;
750    
751 root 1.39 die "$err @res" if length $err;
752 root 1.37
753     print +(substr JSON::XS->new->encode (\@res), 1, -1), "\n";
754     }
755    
756 root 1.10 sub docmd;
757    
758 root 1.1 our %CMD = (
759     snd => sub {
760 root 1.17 my $port = shift @ARGV;
761 root 1.25 init;
762 root 1.1
763 root 1.7 snd $port, @ARGV; @ARGV = ();
764 root 1.1
765     my $cv = AE::cv;
766 root 1.28 my $to = AE::timer 5, 0, sub { exit 1 };
767 root 1.1 mon $port, $cv;
768 root 1.19 my $reply = port sub { &$cv };
769     snd node_of $port, snd => $reply, "message sent successfully";
770 root 1.1
771     print join " ", $cv->recv, "\n";
772     },
773    
774 root 1.41 cal => sub {
775 root 1.17 my $port = shift @ARGV;
776 root 1.25 init;
777 root 1.7
778     my $cv = AE::cv;
779 root 1.41 cal $port, @ARGV, sub { &$cv }; @ARGV = ();
780 root 1.7
781 root 1.23 print +(substr JSON::XS->new->encode ([$cv->recv]), 1, -1), "\n";
782 root 1.7 },
783    
784 root 1.1 mon => sub {
785 root 1.17 my $port = shift @ARGV;
786 root 1.25 init;
787 root 1.1
788     mon $port, my $cv = AE::cv;
789     print join " ", $cv->recv, "\n";
790     },
791    
792 root 1.19 eval => sub {
793     my $node = node_of shift @ARGV;
794     my $expr = join " ", @ARGV; @ARGV = ();
795 root 1.37 node_eval $node, $expr;
796 root 1.19 },
797    
798 root 1.42 shell => \&shell,
799    
800 root 1.11 trace => sub {
801     @ARGV >= 1
802 root 1.17 or die "node id missing\n";
803 root 1.11
804 root 1.17 trace shift @ARGV;
805 root 1.11 },
806 root 1.37 restart => sub {
807     my $node = node_of shift @ARGV;
808 root 1.44 node_eval $node, 'my $w; $w = AE::idle sub { '
809     . 'undef $w; '
810     . 'use AnyEvent::Watchdog::Util ();'
811     . 'AnyEvent::Watchdog::Util::restart'
812     . '}; ()';
813 root 1.37 },
814 root 1.11
815 root 1.17 setnodeid => sub {
816 root 1.8 @ARGV >= 1
817     or die "shared secret missing\n";
818    
819 root 1.17 $profile->{nodeid} = shift @ARGV;
820 root 1.8 ++$cfg->{dirty};
821     },
822 root 1.17 delnodeid => sub {
823     delete $profile->{nodeid};
824 root 1.8 ++$cfg->{dirty};
825     },
826    
827 root 1.1 setsecret => sub {
828 root 1.8 @ARGV >= 1
829 root 1.1 or die "shared secret missing\n";
830    
831 root 1.8 $profile->{secret} = shift @ARGV;
832 root 1.1 ++$cfg->{dirty};
833     },
834     gensecret => sub {
835 root 1.51 $profile->{secret} = AnyEvent::MP::Kernel::nonce62 180; # ~1071 bits
836 root 1.1 ++$cfg->{dirty};
837     },
838 root 1.17 delsecret => sub {
839 root 1.8 delete $profile->{secret};
840 root 1.1 ++$cfg->{dirty};
841     },
842    
843     setcert => sub {
844 root 1.8 @ARGV >= 1
845 root 1.1 or die "key+certificate pem filename missing\n";
846    
847 elmex 1.45 my $certfile = shift @ARGV;
848    
849     open my $fh, "<", $certfile
850     or die "$certfile: $!";
851 root 1.1
852     local $/;
853 root 1.8 $profile->{cert} = <$fh>;
854 root 1.1 ++$cfg->{dirty};
855     },
856     gencert => sub {
857 root 1.8 $profile->{cert} = gen_cert;
858 root 1.1 ++$cfg->{dirty};
859     },
860 root 1.17 delcert => sub {
861 root 1.8 delete $profile->{cert};
862 root 1.1 ++$cfg->{dirty};
863     },
864 root 1.6
865 root 1.17 setbinds => sub {
866 root 1.19 @ARGV >= 1
867     or die "bind addresses missing\n";
868 root 1.38 my $list = shift @ARGV;
869     $profile->{binds} = ref $list ? $list : [split /,/, $list];
870 root 1.17 ++$cfg->{dirty};
871     },
872     delbinds => sub {
873     delete $profile->{binds};
874     ++$cfg->{dirty};
875     },
876     addbind => sub {
877     @ARGV >= 1
878     or die "bind address missing\n";
879     my $bind = shift @ARGV;
880    
881     @{ $profile->{binds} } = grep $_ ne $bind, @{ $profile->{binds} };
882     push @{ $profile->{binds} }, $bind;
883     ++$cfg->{dirty};
884     },
885     delbind => sub {
886     @ARGV >= 1
887     or die "bind address missing\n";
888     my $bind = shift @ARGV;
889    
890     @{ $profile->{binds} } = grep $_ ne $bind, @{ $profile->{binds} };
891     ++$cfg->{dirty};
892     },
893    
894 root 1.6 setseeds => sub {
895 root 1.19 @ARGV >= 1
896     or die "seed addresses missing\n";
897 root 1.38 my $list = shift @ARGV;
898     $profile->{seeds} = ref $list ? $list : [split /,/, $list];
899 root 1.6 ++$cfg->{dirty};
900     },
901 root 1.17 delseeds => sub {
902     delete $profile->{seeds};
903     ++$cfg->{dirty};
904     },
905 root 1.6 addseed => sub {
906 root 1.10 @ARGV >= 1
907 root 1.17 or die "seed address missing\n";
908 root 1.6 my $seed = shift @ARGV;
909 root 1.10
910 root 1.8 @{ $profile->{seeds} } = grep $_ ne $seed, @{ $profile->{seeds} };
911     push @{ $profile->{seeds} }, $seed;
912     ++$cfg->{dirty};
913     },
914     delseed => sub {
915 root 1.10 @ARGV >= 1
916 root 1.17 or die "seed address missing\n";
917 root 1.8 my $seed = shift @ARGV;
918 root 1.10
919 root 1.8 @{ $profile->{seeds} } = grep $_ ne $seed, @{ $profile->{seeds} };
920     ++$cfg->{dirty};
921     },
922    
923     setservices => sub {
924 root 1.19 @ARGV >= 1
925     or die "service specifications missing\n";
926 root 1.38 my $list = shift @ARGV;
927     $profile->{services} = ref $list ? $list : [split /,/, $list];
928 root 1.6 ++$cfg->{dirty};
929     },
930 root 1.17 delservices => sub {
931     delete $profile->{services};
932     ++$cfg->{dirty};
933     },
934 root 1.9 addservice => sub {
935 root 1.10 @ARGV >= 1
936     or die "service specification missing\n";
937 root 1.8 my $service = shift @ARGV;
938     push @{ $profile->{services} }, $service;
939     ++$cfg->{dirty};
940     },
941 root 1.9 delservice => sub {
942 root 1.10 @ARGV >= 1
943     or die "service specification missing\n";
944 root 1.8 my $service = shift @ARGV;
945     for (0 .. $#{ $profile->{services} }) {
946     next unless $profile->{services}[$_] eq $service;
947     splice @{ $profile->{services} }, $_, 1;
948     last;
949     }
950 root 1.6 ++$cfg->{dirty};
951     },
952 root 1.10
953     profile => sub {
954 root 1.19 @ARGV >= 1
955     or die "profile name is missing\n";
956 root 1.10 my $name = shift @ARGV;
957    
958     $profile = $cfg->{profile}{$name} ||= {};
959 root 1.14 ++$cfg->{dirty};
960 root 1.10 },
961     delprofile => sub {
962     @ARGV >= 1
963     or die "profile name is missing\n";
964     my $name = shift @ARGV;
965    
966     delete $cfg->{profile}{$name};
967 root 1.14 ++$cfg->{dirty};
968 root 1.10 },
969 root 1.26 setparent => sub {
970 root 1.24 @ARGV >= 1
971     or die "profile name is missing\n";
972    
973     $profile->{parent} = shift @ARGV;
974     ++$cfg->{dirty};
975     },
976     delparent => sub {
977     delete $profile->{parent};
978     ++$cfg->{dirty};
979     },
980 root 1.17 showprofile => sub {
981     @ARGV >= 1
982     or die "profile name is missing\n";
983     my $name = shift @ARGV;
984    
985     print JSON::XS->new->pretty->encode ($cfg->{profile}{$name} || {});
986     },
987     showconfig => sub {
988 root 1.53 my $name = @ARGV ? shift @ARGV : AnyEvent::MP::Kernel::nodename;
989 root 1.17
990 root 1.25 my $profile = AnyEvent::MP::Config::find_profile $name, @ARGV;
991     @ARGV = ();
992 root 1.24
993     # make it look nicer:
994     delete $profile->{profile};
995     delete $profile->{parent};
996    
997     print JSON::XS->new->pretty->encode ($profile);
998 root 1.17 },
999 root 1.25
1000     # undocumented
1001     _resolve => sub {
1002     print +(join ",", (AnyEvent::MP::Kernel::_resolve shift @ARGV)->recv), "\n";
1003     },
1004 root 1.1 );
1005    
1006 root 1.47 for my $attr (qw(
1007     monitor_timeout connect_interval framing_format auth_offer
1008 root 1.54 auth_accept autocork nodelay secure
1009 root 1.47 )) {
1010     $CMD{"set$attr"} = sub {
1011     @ARGV >= 1
1012     or die "$attr value is missing\n";
1013    
1014     $profile->{$attr} = shift @ARGV;
1015     ++$cfg->{dirty};
1016     };
1017     $CMD{"del$attr"} = sub {
1018     delete $profile->{$attr};
1019     ++$cfg->{dirty};
1020     };
1021     }
1022    
1023 root 1.27 for (keys %CMD) {
1024     $CMD{$1} = $CMD{$_} if /^set(.*)$/;
1025     }
1026    
1027 root 1.1 sub docmd {
1028     my $cmd = shift @ARGV;
1029    
1030     $CMD{$cmd}
1031 root 1.23 or die "$cmd: no such aemp command (try perldoc aemp, or man aemp)";
1032 root 1.1
1033     $CMD{$cmd}();
1034     }
1035    
1036     @ARGV
1037 root 1.23 or die "Usage: aemp subcommand ... (try perldoc aemp, or man aemp)\n";
1038 root 1.1
1039 root 1.19 docmd while @ARGV;
1040 root 1.1
1041