#!/usr/bin/perl ################################################################################# # # add2deploy.pl # Fri Nov 23 2001 # # This application allows the user to setup a new table to enter the local location # and remote location of the files that they wish to deploy to a specified # 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->{remote_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 add 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 = ); 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 = ); 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 = ); while ($ans eq "" ) { print qq(\n$ftpquestion\: ); chomp($ans = ); } $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 = ); 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 = ); while ($ans eq "" ) { print qq(\n$remotedirquestion\: ); chomp($ans = ); } $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 database 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 = ); while ($table_name eq "") { print qq(\n Enter the name of the table to add: ); chomp($table_name = ); } 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->{local_location}', '$file->{ftp_server}', '$file->{remote_dir}')"; die qq(SQL failed: ), $db->Error(), qq(\n) if ($db->Sql($add_entry)); } # 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 be deployed. The application recursively searches the specified directory (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 Access database and a DSN. See the documentation on Win32::ODBC for the details. 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 ftp 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 database 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 = ); 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 = ); while ($username eq "") { print qq(username: ); chomp($username = ); } print qq(password: ); chomp ($password = ); while ($password eq "") { print qq(password: ); chomp($password = ); } 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{$ftpsite}->{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($file->{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 $ftpsite\:$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 conjunction 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 should 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 enter the username and password for each ftp server that is referenced in the table 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 Access database and a DSN. See the documentation on Win32::ODBC for the details. 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.