projects
/
angular-mojolicious.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
e0608bf
)
execute shell commands with trigger
author
Dobrica Pavlinusic
<dpavlin@rot13.org>
Mon, 22 Nov 2010 21:11:46 +0000
(22:11 +0100)
committer
Dobrica Pavlinusic
<dpavlin@rot13.org>
Mon, 22 Nov 2010 21:11:46 +0000
(22:11 +0100)
couchdb-trigger.pl
patch
|
blob
|
history
diff --git
a/couchdb-trigger.pl
b/couchdb-trigger.pl
index
5f02e0f
..
d39f007
100755
(executable)
--- a/
couchdb-trigger.pl
+++ b/
couchdb-trigger.pl
@@
-1,6
+1,14
@@
#!/usr/bin/perl
#!/usr/bin/perl
+# back-end trigger server for CouchDB monitoring changes feed:
+#
# http://wiki.apache.org/couchdb/HTTP_database_API#Changes
# http://wiki.apache.org/couchdb/HTTP_database_API#Changes
+#
+# implements state machine using document which you cen put with:
+#
+# curl -X PUT http://localhost:5984/monitor/df -d '{"trigger":{"command":"df -P","format":"table"}}'
+#
+# DEFAULT TRIGGER EXECUTE SHELL COMMANDS. IT IS NOT SECURE IF YOUR COUCHDB ISN'T SECURE!
use warnings;
use strict;
use warnings;
use strict;
@@
-9,8
+17,23
@@
use lib 'common/mojo/lib';
use Mojo::Client;
use Mojo::JSON;
use Mojo::Client;
use Mojo::JSON;
+use Time::HiRes qw(time);
my $url = 'http://localhost:5984/monitor';
my $url = 'http://localhost:5984/monitor';
+
+sub _trigger {
+ my $trigger = $_[0]->{trigger};
+ if ( my $command = $trigger->{command} ) {
+ # FIXME SECURITY HOLE
+ my $output = $trigger->{output} = `$command`;
+
+ $trigger->{output} =
+ [ map { [ split (/\s+/,$_) ] } split(/\n/,$output) ]
+ if $trigger->{format} =~ m/table/i;
+ }
+ return $trigger;
+}
+
my $seq = 0;
my $client = Mojo::Client->new;
my $seq = 0;
my $client = Mojo::Client->new;
@@
-72,11
+95,11
@@
while( ! $error ) {
} else {
my $res = $tx->res->json;
$change->{doc}->{_rev} = $res->{rev};
} else {
my $res = $tx->res->json;
$change->{doc}->{_rev} = $res->{rev};
- debug "TRIGGER execute ", $change->{doc};
- # FIXME trigger logic?
+ debug "TRIGGER execute ", $change->{doc};
+ _trigger( $change->{doc} );
- push @{ $trigger->{active} }, time()
; # timestamp ste
p
+ push @{ $trigger->{active} }, time()
, 0; # last timestam
p
$client->put( "$url/$id" => $json->encode( $change->{doc} ) => sub {
my ($client,$tx) = @_;
$client->put( "$url/$id" => $json->encode( $change->{doc} ) => sub {
my ($client,$tx) = @_;