George Baugh пре 1 година
родитељ
комит
11ef2febbd
7 измењених фајлова са 114 додато и 37 уклоњено
  1. 4 0
      .gitignore
  2. 26 0
      Readme.md
  3. 9 0
      bin/build_service
  4. 38 0
      bin/tpsgi-config
  5. 0 24
      bin/tpsgi-hostname
  6. 21 13
      service/tpsgi.sh
  7. 16 0
      service/tpsgi.tmpl

+ 4 - 0
.gitignore

@@ -1,3 +1,7 @@
 log/*
 www/*
 .tpsgi.ini
+perl5/
+.bashrc
+.bash_profile
+service/tpsgi.service*

+ 26 - 0
Readme.md

@@ -19,6 +19,14 @@ The best way to do that is to expect raw PSGI to be output.
 
 Obeys ~/.tpsgi.ini -- this is what will control all the custom aspects mentioned below:
 
+    domain:    what domain name this site is
+    user:      what user will this run under
+    http_user: user of the HTTP server (usually www_data)
+    routers:   Where routing modules live
+    indices:   What files are acceptable to execute as directory indexes
+
+You can run bin/tpsgi-config to get any config value needed by scripts.
+
 ## Custom routing
 
 All passed routing modules will be required, and combined to form a routing table so you have an analogue to rewrite rules.
@@ -112,3 +120,21 @@ The first default is to print ERROR and worse to the STDOUT of the PSGI server.
 The other is to emit INFO or better to logs/tpsgi.log
 
 Pass your own dispatch subclasses and watch it go whir.
+
+## Service configuration
+
+Proper operation of this as a production service requires some things:
+
+0. You have a user which has this reposity as their home directory.
+1. You run the shell script, tpsgi.sh in service/ to run this as a service.
+2. You set the username and the domain name of the service in .tpsgi.ini
+3. You set your PATH appropriately to pick up on things like custom perls in a .bashrc
+4. You set the group of your reverse proxy application in .tpsgi.ini (the http\_user variable)
+
+run service/tpsgi.sh as root and you should be set supposing the above is true.
+
+In general the operation is like so:
+
+1. nginx (or whatever) looks for a sock file owned $USER:$WEB\_GROUP, and reverse proxies to this
+2. tpsgi is actively listening on this sock file.
+

+ 9 - 0
bin/build_service

@@ -0,0 +1,9 @@
+#!/bin/sh
+
+DOMAIN=$1
+REPLACEME=$(dirname $0)/..
+REPLACEME=$(readlink -f $REPLACEME)
+
+sed -e "s|__REPLACEME__|$REPLACEME|g" $REPLACEME/service/tpsgi.tmpl > $REPLACEME/service/tpsgi.service.intermediate
+sed -e "s|__DOMAIN__|$DOMAIN|g" $REPLACEME/service/tpsgi.service.intermediate > $REPLACEME/service/tpsgi.service
+rm $REPLACEME/service/tpsgi.service.intermediate

+ 38 - 0
bin/tpsgi-config

@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+
+package TPSGI::ConfigTool;
+
+use strict;
+use warnings;
+
+use FindBin::libs;
+use Getopt::Long qw{GetOptionsFromArray};
+
+use TPSGI;
+
+sub main {
+    my @args = @_;
+
+    my %config = TPSGI::get_config();
+
+    my @params = keys(%config);
+    my (%def, %options);
+    @def{@params} = map { \$options{$_} } @params;
+
+    GetOptionsFromArray(\@args, %def);
+
+    foreach my $param (keys(%options)) {
+        next unless $options{$param};
+        my @to_print;
+        @to_print = ref $config{$param} eq 'ARRAY' ? @{$config{$param}}: $config{$param};
+        foreach my $line (@to_print) {
+            print "$line\n";
+        }
+        return 0;
+    }
+    die "Must pass at least one option";
+}
+
+exit main(@ARGV) unless caller;
+
+1;

+ 0 - 24
bin/tpsgi-hostname

@@ -1,24 +0,0 @@
-#!/usr/bin/env perl
-
-package TPSGI::Hostname;
-
-use strict;
-use warnings;
-
-use FindBin::libs;
-
-use TPSGI;
-
-sub main {
-    my @args = @_;
-
-    my %config = TPSGI::get_config();
-
-    die "Could not get hostname" unless $config{domain};
-    print "$config{domain}\n";
-    return 0;
-}
-
-exit main(@ARGV) unless caller;
-
-1;

+ 21 - 13
service/tpsgi.sh

@@ -1,24 +1,32 @@
 #!/bin/bash
-USERNAME=$(bin/tpsgi-hostname --user)
-echo "tCMS running as user $USERNAME"
-[[ -e run/tcms.pid ]] && sudo pkill -F run/tcms.pid
+HOMEDIR=$(git rev-parse --show-toplevel)
+[[ ! -z $HOMEDIR ]] && HOME=$HOMEDIR
+echo "Set HOME to $HOME"
+
+cd $HOME
+echo "Changed directory to $HOME"
+
+USERNAME=$(bin/tpsgi-config --user)
+[[ -z $USERNAME ]] && USERNAME=$USER
+echo "tPSGI running as user $USERNAME"
+
+[[ -e run/tpsgi.pid ]] && sudo pkill -F run/tpsgi.pid
 source perl5/perlbrew/etc/bashrc
-PERL=$(sudo -u $USERNAME perlbrew exec which perl)
-STARMAN=$(sudo -u $USERNAME perlbrew exec which starman)
-sudo $PERL $STARMAN www/server.psgi --listen run/tcms.sock --workers 20 --user "$USERNAME" --daemonize --pid run/tcms.pid --chroot $(pwd)
+
+# We should obey the PATH set by this user, whose homedir is right here, ideally. 
+PERL=$(sudo -i -u $USERNAME which perl)
+STARMAN=$(sudo -i -u $USERNAME which starman)
+sudo $PERL $STARMAN www/server.psgi --listen run/tpsgi.sock --workers 20 --user "$USERNAME" --daemonize --pid run/tpsgi.pid --chroot $(pwd)
 
 # Wait until the socket file is ready
-until [ -e run/tcms.sock ]
+until [ -e run/tpsgi.sock ]
 do
     echo "Waiting for sock to be ready..."
     sleep 1
 done
 
 # Fix ownership of socket so nginx can see it
-sudo chown $USERNAME:www-data run/tcms.sock
-sudo chmod 0770 run/tcms.sock
-
-echo "tCMS running as PID "`cat run/tcms.pid`
+sudo chown $USERNAME:www-data run/tpsgi.sock
+sudo chmod 0770 run/tpsgi.sock
 
-# TODO Run reloader process
-#$PERL bin/tcms_monitor
+echo "tPSGI running as PID "`cat run/tpsgi.pid`

+ 16 - 0
service/tpsgi.tmpl

@@ -0,0 +1,16 @@
+[Unit]
+Description=tPSGI for __DOMAIN__
+Wants=multi-user.target
+After=multi-user.target
+
+[Install]
+WantedBy=multi-user.target
+
+[Service]
+Type=forking
+PIDFile=__REPLACEME__/run/tpsgi.pid
+ExecStart=__REPLACEME__/service/tpsgi.sh
+WorkingDirectory=__REPLACEME__/
+Restart=always
+OOMPolicy=stop
+ExecReload=kill -HUP $MAINPID