llng-fastcgi-server 6.79 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
our (
    $foreground, $engine,              $nproc,
    $pidFile,    $socket,              $user,
15 16
    $listen,     $group,               $customFunctionsFile,
    %plackOptions
Yadd's avatar
Yadd committed
17 18
);
my %_apps;
Yadd's avatar
Yadd committed
19

Yadd's avatar
Yadd committed
20
$SIG{'PIPE'} = 'IGNORE';
21
$ENV{LLNG_DEFAULTLOGGER} ||= 'Lemonldap::NG::Common::Logger::Syslog';
Yadd's avatar
Yadd committed
22

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

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

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

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

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

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

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

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

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

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

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

=over

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

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

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

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

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

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

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

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

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

=back

Yadd's avatar
Yadd committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
=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
225
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
Yadd's avatar
Yadd committed
226 227 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

=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