llng-fastcgi-server 6.45 KB
Newer Older
Yadd's avatar
Yadd committed
1 2 3 4 5 6
#!/usr/bin/env perl

use Plack::Runner;
use strict;
use warnings;
use POSIX;
Yadd's avatar
Yadd committed
7
use Getopt::Long;
Yadd's avatar
Yadd committed
8
use Lemonldap::NG::Handler::Main::Reload;
Yadd's avatar
Yadd committed
9

Yadd's avatar
Yadd committed
10
our $VERSION = '2.0.0';
Yadd's avatar
Yadd committed
11

Yadd's avatar
Yadd committed
12 13 14 15 16 17
our (
    $foreground, $engine,              $nproc,
    $pidFile,    $socket,              $user,
    $group,      $customFunctionsFile, %plackOptions
);
my %_apps;
Yadd's avatar
Yadd committed
18

Yadd's avatar
Yadd committed
19 20
$SIG{'PIPE'} = 'IGNORE';

Yadd's avatar
Yadd committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
$foreground = 0;
$engine  ||= $ENV{ENGINE} || 'FCGI';
$nproc   ||= $ENV{NPROC}  || 7;
$pidFile ||= $ENV{PID}    || '__FASTCGISOCKDIR__/llng-fastcgi.pid';
$socket  ||= $ENV{SOCKET} || '__FASTCGISOCKDIR__/llng-fastcgi.sock';
$user    ||= $ENV{USER};
$group   ||= $ENV{GROUP};
$customFunctionsFile ||= $ENV{CUSTOM_FUNCTIONS_FILE};

#Getopt::Long::Configure ("bundling_values");
GetOptions(
    'foreground'              => \$foreground,
    'engine|e=s'              => \$engine,
    'proc|n=s'                => \$nproc,
    'pid|p=s'                 => \$pidFile,
    'socket|s=s'              => \$socket,
    'user|u=s'                => \$user,
    'group|g=s'               => \$group,
    'customFunctionsFile|f=s' => \$customFunctionsFile,
    'plackOptions=s'          => \%plackOptions,
);
Yadd's avatar
Yadd committed
42

Yadd's avatar
Yadd committed
43 44
if ($group) {
    my $grp = getgrnam($group) or warn "Can't change uid to $group";
Yadd's avatar
Yadd committed
45
    POSIX::setgid($grp);
Yadd's avatar
Yadd committed
46 47
}

Yadd's avatar
Yadd committed
48 49
if ($user) {
    my $uid = getpwnam($user) or warn "Can't change uid to $user";
Yadd's avatar
Yadd committed
50 51 52
    POSIX::setuid($uid);
}

Yadd's avatar
Yadd committed
53 54
if ($customFunctionsFile) {
    eval { require $customFunctionsFile };
Yadd's avatar
Yadd committed
55
    die $@ if ($@);
56 57
}

Yadd's avatar
Yadd committed
58 59
my %builder = (
    handler => sub {
Yadd's avatar
Yadd committed
60 61
        require Lemonldap::NG::Handler::Server::Nginx;
        return Lemonldap::NG::Handler::Server::Nginx->run( {} );
Yadd's avatar
Yadd committed
62
    },
Yadd's avatar
Yadd committed
63
    reload => sub {
Yadd's avatar
Yadd committed
64 65
        require Lemonldap::NG::Handler::Server::Nginx;
        return Lemonldap::NG::Handler::Server::Nginx->reload();
Yadd's avatar
Yadd committed
66
    },
Yadd's avatar
Yadd committed
67
    status => sub {
Yadd's avatar
Yadd committed
68 69
        require Lemonldap::NG::Handler::Server::Nginx;
        return Lemonldap::NG::Handler::Server::Nginx->status();
Yadd's avatar
Yadd committed
70
    },
Yadd's avatar
Yadd committed
71 72 73 74
    manager => sub {
        require Lemonldap::NG::Manager;
        return Lemonldap::NG::Manager->run( {} );
    },
75
    cgi => sub {
Yadd's avatar
Yadd committed
76 77
        require CGI::Emulate::PSGI;
        require CGI::Compile;
78
        return sub {
79
            my $script = $_[0]->{SCRIPT_FILENAME};
80 81 82
            return $_apps{$script}->(@_) if ( $_apps{$script} );
            $_apps{$script} =
              CGI::Emulate::PSGI->handler( CGI::Compile->compile($script) );
83
            return $_apps{$script}->(@_);
84
        };
Yadd's avatar
Yadd committed
85
    },
Yadd's avatar
Yadd committed
86 87 88 89 90 91 92 93
    psgi => sub {
        return sub {
            my $script = $_[0]->{SCRIPT_FILENAME};
            return $_apps{$script}->(@_) if ( $_apps{$script} );
            $_apps{$script} = do $script;
            return $_apps{$script}->(@_);
          }
    },
Yadd's avatar
Yadd committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
);

unless ($>) {
    die "Refuse to run as root. Aborting";
}

my $app = sub {
    my $type = $_[0]->{LLTYPE} || 'handler';
    return $_apps{$type}->(@_) if ( defined $_apps{$type} );
    if ( defined $builder{$type} ) {
        $_apps{$type} = $builder{$type}->();
        return $_apps{$type}->(@_);
    }
    die "Unknown PSGI type $type";
};

110
# Hook for customFunctions initialization
Yadd's avatar
Yadd committed
111
Lemonldap::NG::Handler::Main->onReload(
Yadd's avatar
Yadd committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    bless( {}, 'Lemonldap::NG::Handler::FastCGI::Loader' ),
    'loadCustomLogins' );

my $server = Plack::Runner->new();
$server->parse_options(
    '-s'           => $engine,
    '-E'           => 'deployment',
    '--pid'        => $pidFile,
    '--nproc'      => $nproc,
    '--socket'     => $socket,
    '--proc-title' => 'llng-fastcgi-server',
    ( $foreground ? () : '--daemonize' ),
    '--no-default-middleware',
    %plackOptions,
);

$server->run($app);

package Lemonldap::NG::Handler::FastCGI::Loader;
Yadd's avatar
Yadd committed
131 132

# Load configuration and look if custom handlers have been defined
Yadd's avatar
Yadd committed
133 134
sub loadCustomLogins {
    my ( $obj, $conf ) = @_;
Yadd's avatar
Yadd committed
135 136
    foreach my $lltype ( keys %{ $conf->{nginxCustomHandlers} // {} } ) {
        my $v = $conf->{nginxCustomHandlers}->{$lltype};
Yadd's avatar
Yadd committed
137
        if ( $v =~ m#[/\\\.]# ) {
Yadd's avatar
Yadd committed
138
            eval { require $v; };
Yadd's avatar
Yadd committed
139 140 141 142 143 144 145
        }
        else {
            eval "use $v";
        }
        if ($@) {
            print STDERR "Unable to load $v, skipping: $@\n";
            next;
Yadd's avatar
Yadd committed
146 147 148 149 150 151 152
        }
        $builder{$lltype} = sub {
            require $v;
            return $v->run( {} );
        };
    }
}
Yadd's avatar
Yadd committed
153
__END__
Yadd's avatar
Yadd committed
154

Yadd's avatar
Yadd committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
=head1 NAME

=encoding utf8

llng-fastcgi-server - FastCGI server used to provide Lemonldap::NG services to
Nginx

=head1 SYNOPSIS

  # Start server listening to /run/llng.sock with 10 process
  llng-fastcgi-server -u nobody -g nobody -s /run/llng.sock -n 10

=head1 DESCRIPTION

llng-fastcgi-server has been designed provides Lemonldap::NG services to Nginx.
Portal, manager and handler will be compiled only is used. So this FastCGI
server can be used on every Lemonldap::NG server even if it needs only some
parts (isolated handlers, portal,...).

Yadd's avatar
Yadd committed
174 175 176 177 178 179
=head1 PARAMETERS

Each parameter can be set by an option or a environment variable.

=over

Yadd's avatar
Yadd committed
180 181 182
=item --pid -p ($ENV{PID}):       pid file

=item --user -u ($ENV{USER}):     user
Yadd's avatar
Yadd committed
183

Yadd's avatar
Yadd committed
184
=item --group -g ($ENV{GROUP}):   group
Yadd's avatar
Yadd committed
185

Yadd's avatar
Yadd committed
186
=item --proc -n ($ENV{NPROC}):    Number of processus for FCGI
Yadd's avatar
Yadd committed
187

Yadd's avatar
Yadd committed
188
=item --engine -e ($ENV{ENGINE}): Plack::Handler engine, default to FCGI
Yadd's avatar
Yadd committed
189

Yadd's avatar
Yadd committed
190
=item --socket -s ($ENV{SOCKET}): Unix socket
Yadd's avatar
Yadd committed
191

Yadd's avatar
Yadd committed
192 193
=item --customFunctionsFile -f ($ENV{CUSTOM_FUNCTIONS_FILE}): file to load for
custom functions
Yadd's avatar
Yadd committed
194

Yadd's avatar
Yadd committed
195 196
=item --plackOptions: other options to pass to Plack. This multi-valued
parameter must have "key=value" values.
Yadd's avatar
Yadd committed
197 198 199

=back

Yadd's avatar
Yadd committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
=head1 SEE ALSO

L<http://lemonldap-ng.org/>

=head1 AUTHORS

=over

=item Clement Oudot, E<lt>clem.oudot@gmail.comE<gt>

=item Xavier Guimard, E<lt>x.guimard@free.frE<gt>

=back

=head1 BUG REPORT

Use OW2 system to report bug or ask for features:
L<http://jira.ow2.org>

=head1 DOWNLOAD

Lemonldap::NG is available at
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>

=head1 COPYRIGHT AND LICENSE

=over

=item Copyright (C) 2008-2016 by Xavier Guimard, E<lt>x.guimard@free.frE<gt>

=item Copyright (C) 2008-2016 by Clément Oudot, E<lt>clem.oudot@gmail.comE<gt>

=back

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see L<http://www.gnu.org/licenses/>.

=cut