1 package MCollective::Action;
8 MCollective::Action - helper class for writing mcollective actions in perl
13 In your mcollective agent
16 validate :message, String
18 implemented by "/tmp/echo.perl"
25 use MCollective::Action;
27 my $mc = MCollective::Action->new;
28 $mc->reply->{message} = $mc->request->{message};
29 $mc->reply->{timestamp} = time;
30 $mc->info("some text to log on the server");
35 mcollective version 1.X introduced a mechanism for writing agent actions as
36 external commands. This module provides a convenient api for writing them in
37 perl which performs some of the boilerplate for you.
45 create a new MCollection::Action helper object
61 returns a hash reference containing the request
66 sub request { $_[0]->{request} }
71 returns a hash reference you should populate with your reply
75 sub reply { $_[0]->{reply} }
80 my $file = $ENV{MCOLLECTIVE_REQUEST_FILE};
82 or die "Can't open '$file': $!";
83 my $json = do { local $/; <$fh> };
84 $self->{request} = JSON->new->decode( $json );
85 delete $self->request->{data}{process_results};
95 my $file = $ENV{MCOLLECTIVE_REPLY_FILE};
97 or die "Can't open '$file': $!";
98 print $fh JSON->new->encode( $self->reply );
103 report a message into the server log
108 my ($self, $message) = @_;
109 print STDOUT $message, "\n";
112 =item error($message)
114 report an error into the server log
120 my ($self, $message) = @_;
121 print STDERR $message, "\n";
126 reports an error and exits immediately
131 my ($self, $message) = @_;
132 $self->error( $message );
144 Richard Clamp <richardc@unixbeard.net>
148 Copyright 2011, Richard Clamp. All Rights Reserved.
150 This program is free software; you can redistribute it
151 and/or modify it under the same terms as Perl itself.
155 http://docs.puppetlabs.com/mcollective/