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

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
$SIG{'PIPE'} = 'IGNORE';
20
$ENV{LLNG_DEFAULTLOGGER} ||= 'Lemonldap::NG::Common::Logger::Syslog';
Yadd's avatar
Yadd committed
21

Yadd's avatar
Yadd committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
$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
43

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

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

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

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

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";
};

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

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
132 133

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

Yadd's avatar
Yadd committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
=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
176 177 178 179 180 181
=head1 PARAMETERS

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

=over

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

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

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

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

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

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

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

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

=back

Yadd's avatar
Yadd committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
=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:
Yadd's avatar
Yadd committed
219
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
Yadd's avatar
Yadd committed
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 248 249

=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