llng-fastcgi-server 6.89 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
our (
Yadd's avatar
Yadd committed
13 14 15
    $foreground,          $engine, $nproc,  $pidFile,
    $socket,              $user,   $listen, $group,
    $customFunctionsFile, %plackOptions
Yadd's avatar
Yadd committed
16 17
);
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
$foreground = 0;
$engine  ||= $ENV{ENGINE} || 'FCGI';
$nproc   ||= $ENV{NPROC}  || 7;
$pidFile ||= $ENV{PID}    || '__FASTCGISOCKDIR__/llng-fastcgi.pid';
$socket  ||= $ENV{SOCKET} || '__FASTCGISOCKDIR__/llng-fastcgi.sock';
27
$listen  ||= $ENV{LISTEN} || undef;
Yadd's avatar
Yadd committed
28 29 30 31 32 33 34 35 36 37 38
$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,
39
    'listen|l=s'              => \$listen,
Yadd's avatar
Yadd committed
40 41 42 43 44
    'user|u=s'                => \$user,
    'group|g=s'               => \$group,
    'customFunctionsFile|f=s' => \$customFunctionsFile,
    'plackOptions=s'          => \%plackOptions,
);
Yadd's avatar
Yadd committed
45

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

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

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

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

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

116
# Hook for customFunctions initialization
Yadd's avatar
Yadd committed
117
Lemonldap::NG::Handler::Main->onReload(
Yadd's avatar
Yadd committed
118
    bless( {}, 'Lemonldap::NG::Handler::FastCGI::Loader' ),
Yadd's avatar
Yadd committed
119
    'loadCustomHandlers' );
Yadd's avatar
Yadd committed
120 121 122

my $server = Plack::Runner->new();
$server->parse_options(
Yadd's avatar
Yadd committed
123 124 125 126 127 128
    '-s'       => $engine,
    '-E'       => 'deployment',
    '--pid'    => $pidFile,
    '--nproc'  => $nproc,
    '--socket' => $socket,
    ( $listen ? ( '--listen', $listen ) : () ),
Yadd's avatar
Yadd committed
129 130 131 132 133 134 135 136 137
    '--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
138 139

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

Yadd's avatar
Yadd committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
=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
182 183 184 185 186 187
=head1 PARAMETERS

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

=over

Yadd's avatar
Yadd committed
188 189 190
=item --pid -p ($ENV{PID}):       pid file

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

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

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

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

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

200 201
=item --listen -l ($ENV{LISTEN}): Listening address (HOST:PORT, :PORT, or PATH)

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

Yadd's avatar
Yadd committed
205 206
=item --plackOptions: other options to pass to Plack. This multi-valued
parameter must have "key=value" values.
Yadd's avatar
Yadd committed
207 208 209

=back

Yadd's avatar
Yadd committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
=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
227
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
Yadd's avatar
Yadd committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

=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