Perl script to publish web content fr...

Perl script to publish web content from test to prod via ssh (linux with apache)
Requirements - public key auth for ssh between the 2 servers
create file /etc/fpub.allowed on test server with:
#source:destination paths


create file /usr/bin/fpub with:
#!/usr/bin/perl -w

use strict;

use Cwd qw(abs_path);

use Sys::Syslog qw(:standard :macros);
use File::Spec;

openlog('fpub', 'ndelay', LOG_USER);

# User to run as, system to scp to (should be put in a config file)

my $user = 'unixuser';
my $target = 'destinationservername';

my $relfile = $ARGV[0];

# Make sure that a file was specified

if (! $relfile) {
        print "No file specified for publishing.\n";
        print "Usage: fpub /file/to.publish\n";
        print "Also, use \"fpub list_allowed\" to see allowed publishing locations.\n";
        exit 1;

# List allowed publish locations if requested by the user

if ($relfile eq 'list_allowed') {
        print "Listing allowed publish locations:\n\n";
        open ALLOW, '/etc/fpub.allowed' or die "Could not open allowed locations file /etc/fpub.allowed: $!";

        while (<ALLOW>) {

                print $_;

        close ALLOW;

        print "\n";

        exit 0;


# Get the absolute path of the file

my $file = File::Spec->rel2abs($relfile);

print "Publishing file: $file\n";

my $username = getpwuid($>);

syslog(LOG_INFO, "fpub ($username) publishing file $file");

my $scpret = system("sudo -u $user fpub_scp $file $target");

if ($scpret == 100) {

        syslog(LOG_ERR, "fpub ($username) fpub.allowed error, could not publish file: $file");
} elsif ($scpret == 120) {
        syslog(LOG_WARNING, "fpub ($username) publish disallowed for file: $file");
} elsif ($scpret == 130) {
        syslog(LOG_ERR, "fpub ($username) fpub.allowed contains relative paths");
} elsif ($scpret != 0) {
        syslog(LOG_ERR, "fpub ($username) unspecified error.  File: $file");


Use examples:
fpub index.html #from within the directory where index.html exists
find . -type f -mtime -1 -exec fpub {} \;  #to publish all docs updated within last 24 hours

No comments: