App-Slaughter

 view release on metacpan or  search on metacpan

bin/slaughter  view on Meta::CPAN

            "lockfile=s",    \$CONFIG{ 'lockfile' },
            "role=s",        \$CONFIG{ 'role' },
            "fqdn=s",        \$CONFIG{ 'fqdn' },
            "hostname=s",    \$CONFIG{ 'hostname' },
            "outfile=s",     \$CONFIG{ 'outfile' },
            "transports",    \$CONFIG{ 'transports' },
            "no-delete",     \$CONFIG{ 'nodelete' },
            "no-execute",    \$CONFIG{ 'noexecute' },
            "verbose",       \$CONFIG{ 'verbose' },
            "skip-cfgfile",  \$CONFIG{ 'skipcfgfile' },
            "allow-nonroot", \$CONFIG{ 'allownonroot' },
        ) )
    {
        exit 1;
    }

    pod2usage(1) if $SHOW_HELP;
    pod2usage( -verbose => 2 ) if $SHOW_MANUAL;

    #
    #  Don't bother trying to execute the generated script if we're allowing non-root
    #
    if ( $CONFIG{ 'allownonroot' } )
    {
        $CONFIG{ 'noexecute' } ||= 1;
    }

    #
    # Don't delete the output file if it was specified on the command line
    #
    if ( $CONFIG{ 'outfile' } )
    {
        $CONFIG{ 'nodelete' } ||= 1;
    }

    #
    #  Showing the version number only?
    #
    if ($SHOW_VERSION)
    {
        print $VERSION . "\n";
        exit;
    }


    #
    #  Dump transport modules
    #
    if ( $CONFIG{ 'transports' } )
    {

        #
        #  Look beneath each directory on the perl include path.
        #
        foreach my $dir (@INC)
        {

            #
            #  Portable filename construction
            #
            my $guess = File::Spec->catfile( $dir, "Slaughter", "Transport" );

            #
            #  Found the module directory?
            #
            if ( -d $guess )
            {
                foreach my $pm ( sort( glob( $guess . "/*.pm" ) ) )
                {

                    # skip the base-class
                    next if ( $pm =~ /revisionControl\.pm/ );

                    # strip the directory name + suffix
                    my $name = basename($pm);
                    $name =~ s/\.pm$//gi;

                    print $name . "\n";
                }
            }
        }
        exit;
    }

}


=begin doc

Create a lockfile, which will be removed on process-termination.

=end doc

=cut

sub createLock
{

    #
    #  Get the lockfile - if we failed to define one then abort.
    #
    my $lockfile = $CONFIG{ 'lockfile' };
    return unless ($lockfile);


    #
    # Open the lock-file exclusively.
    #
    open( LOCK, ">>", $lockfile ) or
      die "Failed to open lockfile at $lockfile - $!";

    #
    # Lock the file.
    #
    flock( LOCK, LOCK_EX | LOCK_NB ) or
      die "$0 already running - Lock file $lockfile is locked";


    #
    # The file will be closed when slaughter terminates so although it
    # looks like we're leaking a handle here this is intentional.
    #

}

=begin doc

Create a temporary directory for holding files, some transports need this.

B<NOTE>:  This directory will be removed when this process terminates unless
slaughter was invoked with --no-delete.

=end doc

=cut

sub createTransportDir
{

    #
    #  Temporary directory for transports to use
    #
    $CONFIG{ 'transportDir' } = tempdir( CLEANUP => !$CONFIG{ 'nodelete' } );

    #  The temporary directory should not be world-readable
    chmod 0700, $CONFIG{ 'transportDir' };
}



=begin doc

Test the environment - which is a combination of the command line flags, and
configuration file settings, and the local user.

=end doc

=cut

sub testEnvironment
{
    if ( $UID != 0 and !$CONFIG{ 'allownonroot' } )
    {
        print <<EOF;
You must launch this command as root.
EOF
        exit 1;
    }

    #
    #  If we have no transport type, but we do have a prefix, we can attempt
    # to infer what to use.
    #
    if ( $CONFIG{ 'prefix' } && !$CONFIG{ 'transport' } )
    {

        # show what is going on
        $CONFIG{ 'verbose' } &&
          print "Attempting to guesss transport for $CONFIG{'prefix'}\n";

        # git://.... or   http://.../foo.git
        $CONFIG{ 'transport' } = "git"
          if ( $CONFIG{ 'prefix' } =~ /(^git|\.git$)/i );

        # http://.../foo.hg
        $CONFIG{ 'transport' } = "hg" if ( $CONFIG{ 'prefix' } =~ /\.hg$/i );

        # rsync://.../
        $CONFIG{ 'transport' } = "rsync"
          if ( $CONFIG{ 'prefix' } =~ /^rsync/i );

        # Local policies will start with /
        $CONFIG{ 'transport' } = "local"
          if ( $CONFIG{ 'prefix' } =~ /^\// );

        # fall-back to HTTP.
        $CONFIG{ 'transport' } = "http" if ( !$CONFIG{ 'transport' } );

        $CONFIG{ 'verbose' } &&
          print "Guessed transport: $CONFIG{'transport'}\n";
    }

    #
    # Abort if we don't have both transport & prefix set now.
    #
    if ( !$CONFIG{ 'transport' } || !$CONFIG{ 'prefix' } )
    {
        print
          "Unless you specify both --transport and --prefix slaughter will be unable to fetch policies\n";
        print
          "(You can use the configuration file /etc/slaughter/slaughter.conf if you prefer.)\n";
        exit 0;
    }


    #
    #  If an include file is specified it must exist.
    #
    if ( ( $CONFIG{ 'include' } ) && ( !-e $CONFIG{ 'include' } ) )
    {
        print "Ignoring the include file - doesn't exist: $CONFIG{'include'}\n";
        $CONFIG{ 'include' } = undef;
    }

}



=begin doc

Load and return the transport the user wants, taking care to cover the
case when either:

=over 8

=item  The transport named doesn't exist.

=item  The transport named isn't available.



( run in 1.924 second using v1.01-cache-2.11-cpan-39bf76dae61 )