App-Foca
view release on metacpan or search on metacpan
lib/App/Foca/Server.pm view on Meta::CPAN
=head1 EXAMPLE
my $server = App::Foca::Server->new(
port => $port,
commands_file => $commands,
commands_timeout => $timeout,
debug => $debug);
$server->run_server();
=head1 EXAMPLE COMMANDS FILE
commands_dirs:
- /some/path/over/there/bin
commands:
df_path:
cmd: '/bin/df {%foca_args%} | tail -n1'
uptime:
cmd: '/usr/bin/uptime'
'true':
cmd: '/bin/true'
The way the example commands file work is: First it will look if there is a
I<commands_dir> key, this key should have a list of directories (that means
it should be an array reference), Foca will look for all executables inside
the given directories and add them into memory. Second, it will look for the
I<commands> key, this one should be a hash where each key is the name of the
command and it should have B<at least> a I<cmd> key which value should be
the I<real> command to execute.
Please note that when you use the I<commands_dir>, Foca will use the basename
of each executable as the name of the command so if you have /usr/local/foo,
the foca command will be I<foo> while the command it will execute will be
I</usr/local/foo>.
Also, you can override commands found in I<commands_dir> via I<commands>, so
going back to our /usr/local/foo example, you can have this executable
in your /usr/local directory but also have a I<foo> command defined in
I<commands>, the one that is defined in I<commands> will be the one that
will be used by Foca.
Command parameters are accepted but they should be find or declared in
the I<Foca-Cmd-Params> HTTP header. L<App::Foca::Client> takes care of
preparing the header.
Commands can have place-holders, this means that you can define your command
in the YAML file and the I<real> command can be a combination of pipes. If your
command needs some parameters then you can use I<{%foca_args%}> and it will
be replaced with whatever parameters are found in the HTTP header
I<Foca-Cmd-Params>.
There are two ways to update the list of commands once the server started: One
is by obviously restarting it and the other one is via localhost send a
HTTP request to localhost:yourport/reload.
=cut
use strict;
use warnings;
use Cache::FastMmap;
use Data::Dumper;
use Fcntl;
use File::Basename;
use FindBin;
use HTTP::Status qw(:constants status_message);
use IPC::Cmd qw(run_forked);
use Linux::Proc::Net::TCP;
use Moose;
use Time::HiRes qw(time);
use YAML::Syck qw(LoadFile);
# Foca libs/modules
use App::Foca::Server::HTTP;
use App::Foca::Tools::Logger;
=head1 VERSION
Version 0.05
=cut
our $VERSION = '0.05';
# Some constants
use constant {
FOCA_RUN_RC_OK => 100,
FOCA_RUN_RC_FAILED_CMD => 200,
FOCA_RUN_RC_MISSING_CMD => 300,
FOCA_RUN_RC_TIMEOUT_CMD => 400};
=head1 Attributes
=over 4
=item B<commands_file>
YAML file with the supported commands.
=cut
has 'commands_file' => (
is => 'rw',
isa => 'Str',
required => 1);
=item B<commands>
Hash reference with a list of supported commands. Basically the content of
C<commands_file>.
=cut
has 'commands' => (
is => 'ro',
isa => 'HashRef');
=item B<port>
Where to listen for requests?
=cut
has 'port' => (
is => 'rw',
isa => 'Int',
( run in 0.646 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )