Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Web Deployment Schemes

by Rich36 (Chaplain)
on Nov 25, 2001 at 01:13 UTC ( [id://127309]=sourcecode: print w/replies, xml ) Need Help??
Category: HTML Utility
Author/Contact Info Rich Reuter
Rich36
redearth@donet.com
Description:

These applications allow the user to store deployment schemes in a database and use that information to FTP the files to a remote location.
The idea behind this code is to have a method to easily send a set of files for a web page out to an ftp server and to be able to resend the set of files when necessary.

add2deploy.pl - store the deployment schemes in a database
deploy.pl - ftp the files to the specified remote location.

See the POD for more information. These applications have only been tested on the Windows platform

UPDATE: Added checking for binary/acsii file in deploy.pl (19:35 11/24/01).

#!/usr/bin/perl


######################################################################
+###########
#
# add2deploy.pl
# Fri Nov 23 2001
#
# This application allows the user to setup a new table to enter the l
+ocal location
# and remote location of the files that they wish to deploy to a speci
+fied
# remote location.
#
######################################################################
+###########

#######################################################
# Setup variables and packages
#######################################################
use strict;
use warnings;
use Win32::ODBC;
use File::Find;
use Cwd;

my ($dir, @files, @deployfiles);

#######################################################
# MAIN
#######################################################

# Set up the directory to be searched for the files
if (($ARGV[0]) && (-d $ARGV[0])) {
    $dir = $ARGV[0];
} else {
    $dir = cwd();
}

# Get a list of all the files in the specified location.
find(\&getFiles, $dir);

# For each file, get information about the remote ftp server
# And the remote location.
&getDeployInformation(@files);

# Create the table in the database
&makeTable();

# Output which files have been added
print qq(Added the following entries to the table:\n);
foreach my $file(@deployfiles) {
    print qq($file->{local_location}\t$file->{ftp_server}\t$file->{rem
+ote_dir}\n);
}

#######################################################
# Subroutines
#######################################################

######################################################################
+###########
# getFiles()
# Get a list of all the files in the specified directory
#
######################################################################
+###########

sub getFiles() {

    my $element = {
        local_location => $File::Find::name,
        ftp_server => "",
        remote_dir => ""
    };

    if (-f $element->{local_location}) {
        push(@files, $element);
    }
}


######################################################################
+###########
# getDeployInformation()
# For each file, get the following information if the user wants to ad
+d the
# file to the list of files to be deployed
# 1. Remote ftp server
# 2. Remote location
#
######################################################################
+###########

sub getDeployInformation() {
    my @files = @_;
    my($default_ftpserver, $default_remotedir);

    # Print menu header
    print qq(=-=-=-= Configure Deployment =-=-=-=\n);
    print qq(For each file, specify the ftp server and the remote dir 
+location\n);
    print qq(To accept the default for any question, press "Enter"\n);

    foreach my $file(@files) {
        print qq(----------------------------------------------\n);
        print qq(FILE: $file->{local_location}\n);

        # Prompt the user to add the file to the list of files being
        # added to the configuration file
        print qq(Do you want to add this file to the list?[y/n] );
        chomp(my $ans = <STDIN>);
        if ($ans =~ m/n/gi) {next;}

        # FTP SERVER
        my $ftpquestion = "Specify the ftp server ";
        # Get name of the remote ftp server
        if ($default_ftpserver) {
            print qq($ftpquestion [default=$default_ftpserver]: );
            chomp(my $ans = <STDIN>);
            if ($ans eq "") {
                $file->{ftp_server} = $default_ftpserver;
            } else {
                $file->{ftp_server} = $ans;
                # Reset the default
                $default_ftpserver = $ans;
            }

        } else {
            print qq($ftpquestion\: );
            chomp(my $ans = <STDIN>);
            while ($ans eq "" ) {
                print qq(\n$ftpquestion\: );
                chomp($ans = <STDIN>);
            }
            $file->{ftp_server} = $ans;
            # Set the default
            $default_ftpserver = $ans;

        } # END FTP

        # REMOTE DIRECTORY
        my $remotedirquestion = "Specify the remote directoy ";
        # Get the remote dir name
        if ($default_remotedir) {
            print qq($remotedirquestion [default=$default_remotedir]: 
+);
            chomp(my $ans = <STDIN>);
            if ($ans eq "") {
                $file->{remote_dir} = $default_remotedir;
            } else {
                $file->{remote_dir} = $ans;
                # Reset the default
                $default_remotedir = $ans;
            }

        } else {
            print qq($remotedirquestion\: );
            chomp(my $ans = <STDIN>);
            while ($ans eq "" ) {
                print qq(\n$remotedirquestion\: );
                chomp($ans = <STDIN>);
            }
            $file->{remote_dir} = $ans;
            # Set the default
            $default_remotedir = $ans;

        } # END REMOTE DIRECTORY

        # Push the element onto a new array.
        push(@deployfiles, $file);
    } #  END foreach(@files)...
}


######################################################################
+###########
# makeTable()
# Create the table in the specified database
# Prompt for table name
# Fields:
# 1. local_location
# 2. ftp_server
# 3. remote_dir
######################################################################
+###########

sub makeTable() {
    my $dsn = "deploy"; # You must create the DSN to connect to your d
+atabase
    my $db;

    if (!($db = new Win32::ODBC($dsn))){
        print qq(error connecting to $dsn\n);
        print "error: " . Win32::ODBC::DumpError() . "\n";
        exit;
    } else {
        print qq(Succesfully connected to $dsn\n);
    }

    print qq(Enter the name of the table to add: );
    chomp(my $table_name = <STDIN>);
    while ($table_name eq "") {
        print qq(\n    Enter the name of the table to add: );
        chomp($table_name = <STDIN>);
    }

    my $addtable = "CREATE TABLE $table_name (local_location Text(150)
+,ftp_server Text(150),remote_dir Text(100))";

    die qq(SQL failed: ), $db->Error(), qq(\n) if ($db->Sql($addtable)
+);

    foreach my $file(@deployfiles) {
        my $add_entry = "INSERT INTO $table_name VALUES ('$file->{loca
+l_location}', '$file->{ftp_server}', '$file->{remote_dir}')";
        die qq(SQL failed: ), $db->Error(), qq(\n) if ($db->Sql($add_e
+ntry));
    }

    # Close the connection
    $db->Close;
}
__END__

=head1 NAME

add2deploy.pl

=head1 SYNOPSIS

add2deploy.pl creates schemes for deploying files to a remote server. 
+ This application is
used in conjunction with deploy.pl, which FTPs the files to the remote
+ locations

=head1 DESCRIPTION

add2deploy.pl populates a database with a series of deployment schemes
designed to specify the web server and dir where a local file should b
+e
deployed.  The application recursively searches the specified director
+y
(or the current directory if none is specified).

For each file found, the user is prompted:

=over 4

=item 1

Whether or not to add the file to the scheme (y/n)

=item 2

What ftp site to send the file to (i.e. ftp.domain.com)

=item 3

What directory on the remote server should the file reside on? (i.e. /
+www)

=back

The user is then prompted to enter a table name.  The application then
+ stores
the data in the specified table.

=head1 NOTES

In order to use add2deploy.pl and deploy.pl, you must set up a MS Acce
+ss
database and a DSN.  See the documentation on Win32::ODBC for the deta
+ils.
This application could be modified to use other database sources.

This application has only been tested on the Windows platform.


=head1 AUTHOR

Rich Reuter [redearth@donet.com]

=head1 COPYRIGHT

Copyright 2001, Rich Reuter. All Rights Reserved.
This program is free software.  You may copy or
redistribute it under the same terms as Perl itself.

------------------------------------------------

#!/usr/bin/perl

######################################################################
+###########
#
# deploy.pl
# Fri Nov 23 2001
#
# Open a table created using add2deploy.pl and ftp the files to the ft
+p server/
# remote directory.
#
######################################################################
+###########

#######################################################
# Setup variables and packages
#######################################################
use strict;
use warnings;
use Net::FTP;
use Win32::ODBC;
use File::Basename;

my ($table, @rows, %ftpsites);

#######################################################
# MAIN
#######################################################

# Connect to the database
my $dsn = "deploy"; # You must create the DSN to connect to your datab
+ase
my $db;

if (!($db = new Win32::ODBC($dsn))){
       print qq(error connecting to $dsn\n);
       print "error: " . Win32::ODBC::DumpError() . "\n";
       exit;
} else {
    print qq(Succesfully connected to database using DSN\: $dsn\n);
}

# Get the names of the tables currently in the db
# And print out a menu for the user to choice which
# scheme to upload
my @tables = $db->TableList;
if (!@tables) {die "No tables could be found in the specified database
+.\n";}

my $id = 1;
my %menu = ();
foreach(@tables) {
        print qq($id\t$_\n);
        $menu{$id} = $_;
        $id++
}

print qq(Select a scheme to upload: );
chomp(my $ans = <STDIN>);
if (!$menu{$ans}) {
    die "That is not a valid menu choice.\n";
} else {
    $table = $menu{$ans};
}

# Open up the table and get the rows
&getInfo();

# Close the database
$db->Close;

# Get the name and password for each ftp server.
&getUserPassword();

# FTP the files to their new location
&ftpFiles();

#######################################################
# Subroutines
#######################################################

######################################################################
+###########
# getInfo()
# Open up the table and get the information in the table
######################################################################
+###########

sub getInfo() {
    die qq(SQL failed: ), $db->Error(), qq(\n) if ($db->Sql("SELECT * 
+FROM $table"));

    while ($db->FetchRow()) {
        my %data = $db->DataHash();
        push @rows, {%data};
    }
}



######################################################################
+###########
# getUserPassword()
# Get the username and password for each FTP site
######################################################################
+###########

sub getUserPassword() {
    my ($username, $password);
    # Get the unique names of the ftp sites listed in the database
    foreach my $row(@rows) {
            $ftpsites{$row->{ftp_server}} = "";
    }

    foreach(keys(%ftpsites)) {
        print qq(FTP_SITE = $_\n);
        print qq(username: );
        chomp ($username = <STDIN>);
        while ($username eq "") {
            print qq(username: );
            chomp($username = <STDIN>);
        }

        print qq(password: );
        chomp ($password = <STDIN>);
        while ($password eq "") {
            print qq(password: );
            chomp($password = <STDIN>);
        }

        my $login = {
            username => $username,
            password => $password
        };
        # Assign the vars to a hash
        $ftpsites{$_} = $login;

    }

}


######################################################################
+###########
# ftpFiles()
# FTP the files to the remote location
######################################################################
+###########

sub ftpFiles() {
    my ($put, $remotefile);
    foreach my $ftpsite(keys(%ftpsites)) {
        # Open the Net::FTP connection
        # And login using the specified username, password sets.
        my $ftp = Net::FTP->new("$ftpsite", Debug => 0);
        $ftp->login($ftpsites{$ftpsite}->{username}, $ftpsites{$ftpsit
+e}->{password}) ||
        die "Couldn't login into $ftpsite\n";

        # Foreach file listed, ftp the file to the remote server
        foreach my $file(@rows) {
            if ($file->{ftp_server} eq $ftpsite) {
                # Make sure that the remote_dir doesn't already have a
+ trailing slash
                # remote file = /remote_dir/file_name
                $file->{remote_dir} =~ s|\/$||;
                $remotefile = $file->{remote_dir} . "/" . basename($fi
+le->{local_location});

                # Set the type of transfer (binary/acsii)
                if (-T $file->{local_location}) {
                    $ftp->type("A");
                } else {
                    $ftp->type("I");
                }

                # Send the file to the webserver
                $put = $ftp->put($file->{local_location}, $remotefile)
+;

                if ($put) {
                    print qq(Transferred $file->{local_location} to $f
+tpsite\:$remotefile\n);
                } else {
                    print qq(Could not transfer $file->{local_location
+} to $ftpsite\:$remotefile\n);
                }
            }
        }
        # Close the FTP connection
        $ftp->quit;
    }
}

__END__

=head1 NAME

deploy.pl

=head1 SYNOPSIS

Deploys file sets to remote servers.  This application is used in conj
+unction with
add2deploy.pl

=head1 DESCRIPTION

add2deploy.pl populates a database with a series of deployment schemes
designed to specify the web server and directory where a local file sh
+ould be
deployed.  deploy.pl allows the user to select one of the schemes and
ftp the files to the remote location.

The user is prompted to select a table name from the database, then en
+ter the
username and password for each ftp server that is referenced in the ta
+ble entries.
The application will then ftp each file in the scheme to its specified
+ remote
location.

=head1 NOTES

In order to use add2deploy.pl and deploy.pl, you must set up a MS Acce
+ss
database and a DSN.  See the documentation on Win32::ODBC for the deta
+ils.
This application could be modified to use other database sources.

This application has only been tested on the Windows platform.

=head1 AUTHOR

Rich Reuter [redearth@donet.com]

=head1 COPYRIGHT

Copyright 2001, Rich Reuter. All Rights Reserved.
This program is free software.  You may copy or
redistribute it under the same terms as Perl itself.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://127309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-03-28 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found