Permalink
Browse files

another bulk update

  • Loading branch information...
1 parent cb9fef1 commit bdc7edbdd986ea76b9de18cd3e874a575e6f6cc0 @ruz ruz committed Nov 21, 2011
Showing with 314 additions and 59 deletions.
  1. +1 −0 META.yml
  2. +2 −0 Makefile.PL
  3. +10 −0 README
  4. +1 −1 etc/nginx.conf
  5. +11 −4 etc/rt.server.conf
  6. +110 −2 lib/RT/Extension/Nginx.pm
  7. +29 −52 sbin/rt-generate-nginx-conf
  8. +150 −0 sbin/rt-nginx-control
View
1 META.yml
@@ -17,6 +17,7 @@ name: RT-Extension-Nginx
no_index:
directory:
- etc
+ - html
- inc
requires:
Text::Template: 1.2
View
2 Makefile.PL
@@ -13,8 +13,10 @@ substitute( {
RT_LIB_PATH => join( ' ', $local_lib_path, $lib_path ),
},
'sbin/rt-generate-nginx-conf',
+ 'sbin/rt-nginx-control',
);
requires 'autodie';
requires 'Text::Template' => 1.20;
+WriteAll();
View
10 README
@@ -0,0 +1,10 @@
+NAME
+ RT::Extension::Nginx - build a
+
+DESCRIPION
+AUTHOR
+ Ruslan Zakirov <ruz@bestpractical.com>
+
+LICENSE
+ Under the same terms as perl itself.
+
View
2 etc/nginx.conf
@@ -1,4 +1,4 @@
-user <% $stah{user} %> <% $stah{group} %>;
+user <% $stash{web_user} %> <% $stash{web_group} %>;
pid <% $stash{nginx_root} %>/nginx.pid;
error_log <% $stash{nginx_root} %>/nginx.error.log debug;
View
15 etc/rt.server.conf
@@ -18,31 +18,38 @@ server {
location <% RT->Config->Get('WebPath') %>/NoAuth/images/ {
root <% $RT::BasePath %>;
try_files
- local/html\$uri
- share/html\$uri
+ local/html$uri
+ <% join ' ', map "$_\$uri", map File::Spec->abs2rel($_, $RT::BasePath), RT->PluginDirs('html') %>
+ share/html$uri
@main
;
}
location <% RT->Config->Get('WebPath') %>/NoAuth/css/ {
root <% $stash{fcgi_storage} %>;
gzip_comp_level 9;
- error_page 404 = /fetch\$uri;
+ error_page 404 = @fetch_and_store;
}
location <% RT->Config->Get('WebPath') %>/NoAuth/js/ {
root <% $stash{fcgi_storage} %>;
gzip_comp_level 9;
error_page 404 = @fetch_and_store;
}
+ location <% RT->Config->Get('WebPath') %>/NoAuth/RichText/ {
+ root <% $stash{fcgi_storage} %>;
+ gzip_comp_level 9;
+ error_page 404 = @fetch_and_store;
+ }
location <% RT->Config->Get('WebPath') %>/ {
include <% $stash{nginx_root} %>/fcgi.include.conf;
}
- location \@main {
+ location @main {
include <% $stash{nginx_root} %>/fcgi.include.conf;
}
location @fetch_and_store {
include <% $stash{nginx_root} %>/fcgi.include.conf;
+ root <% $stash{fcgi_storage} %>;
fastcgi_store on;
fastcgi_store_access user:rw group:rw all:r;
fastcgi_temp_path <% $stash{fcgi_temp} %>;
View
112 lib/RT/Extension/Nginx.pm
@@ -1,10 +1,15 @@
-package RT::Extension::Nginx;
-
+use 5.008003;
use strict;
use warnings;
+package RT::Extension::Nginx;
+
our $VERSION = '0.01';
+use File::Spec;
+use File::Path qw(make_path);
+use autodie;
+
=head1 NAME
RT::Extension::Nginx - build a
@@ -13,6 +18,109 @@ RT::Extension::Nginx - build a
=cut
+sub RootPath { return (shift)->CreateVarDir }
+sub FcgiTempPath { return (shift)->CreateVarDir('fcgi.temp') }
+sub FcgiStoragePath { return (shift)->CreateVarDir('fcgi.storage') }
+
+sub NginxConfigPath {
+ return File::Spec->catfile( (shift)->RootPath, 'nginx.conf' );
+}
+
+sub CreateVarDir {
+ my $self = shift;
+ my $path = File::Spec->catdir( $RT::VarPath, 'nginx', @_ );
+ make_path( $path );
+ return $path;
+}
+
+sub SetupRights {
+ my $self = shift;
+
+ my ($wuid, $wgid) = ( ($self->GetWebUser)[0], ($self->GetWebGroup)[0] );
+ my ($rtuid, $rtgid) = (stat $RT::EtcPath)[4, 5];
+
+ my $root = $self->RootPath;
+
+ chmod 0400, map File::Spec->catfile($root, $_), $self->Templates;
+ chown $rtuid, $rtgid, map File::Spec->catfile($root, $_), $self->Templates;
+
+ chmod 0770, $self->FcgiTempPath, $self->FcgiStoragePath;
+ chown $wuid, $wgid, $self->FcgiTempPath, $self->FcgiStoragePath;
+
+ chmod 0755, $self->RootPath;
+ chown $rtuid, $rtgid, $self->RootPath;
+}
+
+sub Templates {
+ return qw(nginx.conf rt.server.conf fcgi.include.conf mime.types);
+}
+
+sub FindExecutable {
+ my $self = shift;
+ my $name = shift;
+
+ foreach my $dir ( File::Spec->path ) {
+ my $file = File::Spec->catfile( $dir, $name );
+ return $file if -e $file && -x _;
+ }
+ return undef;
+}
+
+sub GetWebUser {
+ my $self = shift;
+ my $id = (stat $RT::MasonDataDir)[4];
+ return ($id, getpwuid $id);
+}
+
+sub GetWebGroup {
+ my $self = shift;
+ my $id = (stat $RT::MasonDataDir)[5];
+ return ($id, getgrgid $id);
+}
+
+sub GetSystemUser {
+ my $self = shift;
+ return ($>, getpwuid $>);
+}
+
+sub GenerateFile {
+ my $self = shift;
+ my $name = shift;
+ my $stash = shift;
+
+ require RT::Plugin;
+ my $from = RT::Plugin->new( name => 'RT::Extension::Nginx' )->Path('etc');
+
+ return $self->ParseTemplate(
+ From => [$from, $name],
+ To => [$stash->{'nginx_root'}, $name],
+ Stash => $stash,
+ );
+}
+
+sub ParseTemplate {
+ my $self = shift;
+ my %args = @_;
+
+ $_ = File::Spec->catfile(@$_) foreach grep ref $_, $args{'From'}, $args{'To'};
+
+ use Text::Template;
+ my $template = Text::Template->new(
+ TYPE => 'FILE',
+ SOURCE => $args{'From'},
+ DELIMITERS => [qw(<% %>)],
+ PREPEND => 'use warnings;',
+ );
+ my $res = $template->fill_in( HASH => { stash => $args{'Stash'} } );
+ return $res unless $args{'To'};
+
+ open my $fh, '>', $args{'To'};
+ print $fh $res;
+ close $fh;
+
+ return $res;
+}
+
=head1 AUTHOR
View
81 sbin/rt-generate-nginx-conf
@@ -1,7 +1,10 @@
#!/usr/bin/env perl
### after: use lib qw(@RT_LIB_PATH@);
-use lib qw(/opt/rt4/local/lib /opt/rt4/lib);
+use lib qw(/opt/rt4ngx/local/lib /opt/rt4ngx//lib);
+
+use strict;
+use warnings;
use File::Spec;
use File::Path qw(make_path);
@@ -10,62 +13,36 @@ use autodie;
use RT;
RT::LoadConfig();
-die "Only empty WebPath is supported at the moment,"
- ." eg serving RT from http://rt.$stash{domain}/,"
- ." not http://$stash{domain}$stash{web_path}/"
- if RT->Config->Get('WebPath');
-
-my %stash;
-
-$stash{'nginx_root'} = File::Spec->catdir($RT::VarPath, 'nginx');
-make_path( $stash{'nginx_root'} );
-
-$stash{'nginx_conf'} = File::Spec->catfile($stash{'nginx_root'}, 'nginx.conf'),
-
-$stash{'fcgi_storage'} = File::Spec->catfile($stash{'nginx_root'}, 'fcgi.storage'),
-make_path( $stash{'fcgi_storage'} );
-
-$stash{'fcgi_temp'} = File::Spec->catfile($stash{'nginx_root'}, 'fcgi.temp'),
-make_path( $stash{'fcgi_temp'} );
-
-foreach my $file (qw(nginx.conf rt.server.conf fcgi.include.conf)) {
- main->GenerateFile( $file, \%stash );
-}
-
-sub GenerateFile {
- my $self = shift;
- my $name = shift;
- my $stash = shift;
-
- require RT::Plugin;
- my $from = RT::Plugin->new( name => 'RT::Extension::Nginx' )->Path('etc');
-
- return $self->ParseTemplate(
- From => [$from, $name],
- To => [$stash->{'nginx_root'}, $name],
- Stash => $stash,
- );
+if ( RT->Config->Get('WebPath') ) {
+ die sprintf
+ "Only empty WebPath is supported at the moment,"
+ ." eg serving RT from http://rt.%s/,"
+ ." not http://%s%s/",
+ RT->Config->Get('WebDomain'),
+ RT->Config->Get('WebDomain'),
+ RT->Config->Get('WebPath'),
+ ;
}
-sub ParseTemplate {
- my $self = shift;
- my %args = @_;
+my @plugins = RT->Config->Get('Plugins');
+push @plugins, 'RT::Extension::Nginx';
+RT->Config->Set('Plugins' => @plugins);
+RT::InitPluginPaths();
+require RT::Extension::Nginx;
- $_ = File::Spec->catfile(@$_) foreach grep ref $_, $args{'From'}, $args{'To'};
+my %stash;
- use Text::Template;
- my $template = Text::Template->new(
- TYPE => 'FILE',
- SOURCE => $args{'From'},
- DELIMITERS => [qw(<% %>)],
- );
- my $res = $template->fill_in( HASH => { stash => $args{'Stash'} } );
- return $res unless $args{'To'};
+@stash{'web_uid', 'web_user'} = RT::Extension::Nginx->GetWebUser;
+@stash{'web_gid', 'web_group'} = RT::Extension::Nginx->GetWebGroup;
- open my $fh, '>', $args{'To'};
- print $fh $res;
- close $fh;
+$stash{'nginx_root'} = RT::Extension::Nginx->RootPath;
+$stash{'nginx_conf'} = RT::Extension::Nginx->NginxConfigPath;
+$stash{'fcgi_storage'} = RT::Extension::Nginx->FcgiStoragePath;
+$stash{'fcgi_temp'} = RT::Extension::Nginx->FcgiTempPath;
- return $res;
+foreach my $file ( RT::Extension::Nginx->Templates ) {
+ RT::Extension::Nginx->GenerateFile( $file, \%stash );
+ print "Generated $file config\n";
}
+RT::Extension::Nginx->SetupRights;
View
150 sbin/rt-nginx-control
@@ -0,0 +1,150 @@
+#!/usr/bin/env perl
+
+### after: use lib qw(@RT_LIB_PATH@);
+use lib qw(/opt/rt4ngx/local/lib /opt/rt4ngx//lib);
+
+use strict;
+use warnings;
+
+use File::Spec;
+use autodie;
+
+use RT;
+RT::LoadConfig();
+
+my %targets = map {$_ => 1} qw(nginx fcgi);
+my %commands = map {$_ => 1} qw(start stop restart);
+
+my $command = pop @ARGV;
+my $target = pop @ARGV;
+if ( !$command || !$commands{ $command } || ($target && !$target) ) {
+ exit usage();
+}
+
+my @plugins = RT->Config->Get('Plugins');
+push @plugins, 'RT::Extension::Nginx';
+RT->Config->Set('Plugins' => @plugins);
+RT::InitPluginPaths();
+require RT::Extension::Nginx;
+
+if ( $command eq 'start' ) {
+ main->start_fcgi() if !$target || $target eq 'fcgi';
+ main->start_nginx() if !$target || $target eq 'nginx';
+}
+elsif ( $command eq 'stop' ) {
+ main->stop_fcgi() if !$target || $target eq 'fcgi';
+ main->stop_nginx() if !$target || $target eq 'nginx';
+}
+elsif ( $command eq 'restart' ) {
+ main->restart_fcgi() if !$target || $target eq 'fcgi';
+ main->restart_nginx() if !$target || $target eq 'nginx';
+}
+
+sub start_nginx {
+ my $self = shift;
+
+ $self->run_command_as_web_user(
+ 'nginx',
+ '-c', RT::Extension::Nginx->NginxConfigPath,
+ );
+}
+sub stop_nginx {
+ my $self = shift;
+
+ $self->run_command_as_web_user(
+ 'nginx',
+ '-c', RT::Extension::Nginx->NginxConfigPath,
+ '-s', 'quit',
+ );
+}
+sub restart_nginx {
+ my $self = shift;
+
+ $self->run_command_as_web_user(
+ 'nginx',
+ '-c', RT::Extension::Nginx->NginxConfigPath,
+ '-s', 'reload',
+ );
+}
+sub nginx_pidfile {
+ my $self = shift;
+ return File::Spec->catfile( RT::Extension::Nginx->RootPath, 'nginx.pid' );
+}
+
+sub start_fcgi {
+ my $self = shift;
+ my $root = RT::Extension::Nginx->RootPath;
+
+ $self->run_command_as_web_user(
+ 'plackup',
+ '--server', 'FCGI',
+ '--listen', File::Spec->catfile( $root, 'fcgi.sock' ),
+ '--pid', $self->fcgi_pidfile,
+ '--daemonize', '--nproc', 10,
+ '--keep-stderr', 1,
+ File::Spec->catfile( $RT::SbinPath, 'rt-server' ),
+ );
+}
+
+sub stop_fcgi {
+ my $self = shift;
+
+ my $pid = do {
+ open my $fh, '<', $self->fcgi_pidfile;
+ local $/; my $res = <$fh>; chomp $res if $res;
+ $res;
+ };
+ unless ($pid) {
+ print "No FCGI pid\n";
+ return;
+ }
+ kill 'SIGTERM', $pid or die "couldn't kill process #$pid: $!";
+}
+
+sub restart_fcgi {
+ my $self = shift;
+
+ my $pidfile = $self->fcgi_pidfile;
+ if (-e $pidfile ) {
+ $self->stop_fcgi;
+ sleep 1 while -e $pidfile;
+ }
+ $self->start_fcgi;
+}
+
+sub fcgi_pidfile {
+ my $self = shift;
+ return File::Spec->catfile( RT::Extension::Nginx->RootPath, 'fcgi.pid' );
+}
+
+sub run_command_as_web_user {
+ my $self = shift;
+ my $name = shift;
+ my @cmd = @_;
+
+ my $executable = RT::Extension::Nginx->FindExecutable( $name );
+ die "Couldn't find $name executable under PATH" unless $executable;
+ unshift @cmd, $executable;
+
+ if ( (RT::Extension::Nginx->GetSystemUser)[0] != (RT::Extension::Nginx->GetWebUser)[0] ) {
+ my $exit_code = system @cmd;
+ die "Couldn't run `". join(' ', @cmd) ."`: $!" if $exit_code;
+ return;
+ }
+
+ my ($wuid, $wgid) = (
+ (RT::Extension::Nginx->GetWebUser)[0],
+ (RT::Extension::Nginx->GetWebGroup)[0],
+ );
+
+ if (my $pid = fork) { wait }
+ elsif ( !defined $pid ) { die "Couldn't fork: $!" }
+ else {
+ require POSIX;
+ POSIX::setgid( $wgid ) or die "couldn't set gid: $!";
+ POSIX::setuid( $wuid ) or die "couldn't set uid: $!";
+ exec @cmd; die "couldn't exec foo: $!";
+ }
+ return;
+}
+

0 comments on commit bdc7edb

Please sign in to comment.