[Swift-commit] r3536 - in usertools/swift/swiftconfig: bin lib/perl
noreply at svn.ci.uchicago.edu
noreply at svn.ci.uchicago.edu
Sun Aug 15 13:25:14 CDT 2010
Author: davidk
Date: 2010-08-15 13:25:13 -0500 (Sun, 15 Aug 2010)
New Revision: 3536
Added:
usertools/swift/swiftconfig/lib/perl/SwiftConfig.pm
Modified:
usertools/swift/swiftconfig/bin/swiftconfig
usertools/swift/swiftconfig/bin/swiftrun
Log:
Many updates to clarify code. Most shared functions now stored in SwiftConfig.pm module.
Modified: usertools/swift/swiftconfig/bin/swiftconfig
===================================================================
--- usertools/swift/swiftconfig/bin/swiftconfig 2010-08-14 22:45:00 UTC (rev 3535)
+++ usertools/swift/swiftconfig/bin/swiftconfig 2010-08-15 18:25:13 UTC (rev 3536)
@@ -1,42 +1,18 @@
#!/usr/bin/perl -w
use strict;
-use Cwd;
-use Cwd qw(abs_path);
+
use File::Copy;
use File::Path;
use Pod::Usage;
-
-my $swift_home = q{};
-if ( $ENV{'SWIFT_HOME'} && -e "$ENV{'SWIFT_HOME'}/bin/swift" ) {
- $swift_home = $ENV{'SWIFT_HOME'};
-}
-
-my @execution_path = split( '/', abs_path($0) );
- at execution_path = splice( @execution_path, 0, $#execution_path - 1 );
-my $parent_directory = join( '/', @execution_path );
-
-if ( !$swift_home ) {
- if ( -e "$parent_directory/bin/swift" ) {
- $swift_home = $parent_directory;
- }
-
- else {
- @execution_path = split( '/', which("swift") );
- @execution_path = splice( @execution_path, 0, $#execution_path - 1 );
- my $tmp = join( '/', @execution_path );
- if ( -e "$tmp/bin/swift" ) {
- $swift_home = $tmp;
- }
- }
-}
-
+use Cwd;
+use Cwd qw(abs_path);
use FindBin qw($Bin);
-use lib "$FindBin::Bin/../lib/perl"; # Use libraries in $swift_home/lib/perl
+use lib "$FindBin::Bin/../lib/perl"; # Use libs in swiftconfig_home/lib/perl
+use SwiftConfig;
use Getopt::Long;
-use File::Which qw(which where);
use XML::Simple;
-use Data::Dumper;
+use File::Which qw(which where);
# Variables used for sites.xml
my $option_templates = q{}; # List all templates
@@ -55,7 +31,6 @@
GetOptions(
'remove=s' => \$option_remove,
'templates' => \$option_templates,
- 'swift_home=s' => \$swift_home,
'sites' => \$option_sites,
'describe=s' => \$option_describe,
'default|defaults' => \$option_default,
@@ -63,77 +38,9 @@
'template=s' => \$option_template,
'edit=s' => \$option_edit,
'copy=s' => \$option_copy,
- 'help' => sub { pod2usage(-verbose => 2); },
+ 'help|man' => sub { pod2usage(-verbose => 2); },
);
-# Create a new directory if it does not exist
-sub create_directory {
- my $directory = $_[0];
- if ( !-d "$directory" ) {
- mkdir "$directory", 0700
- or die "Unable to create directory $directory\n";
- }
-}
-
-# Process keyboard input
-sub get_entry {
- my ( $entry_description, $entry_value, $use_default, @allowable_values, ) = @_;
-
- # If use_default is specified, automatically plug in the default value
- if($use_default) {
- return $entry_value;
- }
-
- if(!@allowable_values) {
- print "$entry_description [$entry_value]: ";
- }
- else {
- print "$entry_description [";
- my $counter=0;
- foreach my $allowable(@allowable_values) {
- if($counter != 0) {
- print " ";
- }
- if($allowable eq $entry_value) {
- print "*$allowable";
- }
- else {
- print "$allowable";
- }
- $counter++;
- }
- print "]: ";
- }
- my $new_value = <STDIN>;
- chomp($new_value);
- if ($new_value) {
- $entry_value = $new_value;
- }
-
-
- # Check if value entered is valid (if valid values were passed)
- my $is_valid = 0;
- if (@allowable_values) {
- foreach my $allowable (@allowable_values) {
- if ( $allowable eq $entry_value ) {
- $is_valid = 1;
- }
- }
- if ( !$is_valid ) {
- my $error_message = q{};
- foreach my $allowable (@allowable_values) {
- $error_message = $error_message . $allowable . ' ';
- }
- print 'Invalid value selected. Please select from: '
- . "$error_message\n";
- $entry_value = get_entry( $entry_description, $entry_value,
- @allowable_values, );
- }
- }
-
- return $entry_value;
-}
-
# Prepare data
my $xml = new XML::Simple();
@@ -144,279 +51,10 @@
create_directory("$dotswift_directory/sites");
# Determine the template directory
- at execution_path = split( '/', abs_path($0) );
+my @execution_path = split( '/', abs_path($0) );
@execution_path = splice( @execution_path, 0, $#execution_path - 1 );
my $template_directory = join( '/', @execution_path ) . '/etc/sites';
-# Add new entry to auths.default
-sub add_ssh {
- my ( $ssh_site, $ssh_url) = @_;
-
- # Open authfile
- my $auth_file = "$ENV{'HOME'}/.ssh/auth.defaults";
- if( -e "$auth_file") {
- open( AUTH_FILE, "<$auth_file")
- || die "Unable to open $auth_file for reading\n";
- }
- else {
- open( AUTH_FILE, "+>$auth_file" )
- || die "Unable to open $auth_file for read/write!\n";
- }
- my @auth_data = <AUTH_FILE>;
- close(AUTH_FILE);
-
- # Get existing values and modify if found
- my $ssh_username;
- my $ssh_password;
- my $found = 0;
- foreach (@auth_data) {
- if (/username/i && /^$ssh_url/i) {
- ( my $setting, $ssh_username, ) = split( '=', $_ );
- chomp($ssh_username);
- $ssh_username = get_entry( 'Username', $ssh_username, 0);
- $_ = "$ssh_url.username=$ssh_username\n";
- $found = 1;
- }
- if (/\.password/i && /^$ssh_url/i) {
- ( my $setting, $ssh_password, ) = split( '=', $_ );
- chomp($ssh_password);
- $ssh_password = get_entry( 'Password', $ssh_password, 0);
- $_ = "$ssh_url.password=$ssh_password\n";
- $found = 1;
- }
- }
-
- # Add new entry if needed
- if(!$found) {
- $ssh_username = getlogin();
- $ssh_username = get_entry( 'Username', $ssh_username, 0 );
- $ssh_password = get_entry( 'Password', '', 0);
-
- # Password based authentication
- push( @auth_data, "$ssh_url.type=password\n" );
- push( @auth_data, "$ssh_url.username=$ssh_username\n" );
- push( @auth_data, "$ssh_url.password=$ssh_password\n" );
- }
-
- # Write data
- write_file( $auth_file, @auth_data );
-}
-
-# Remove an entry from auth.defaults
-sub remove_ssh {
- my ( $ssh_url, ) = @_;
-
- # Open auth.defaults
- my $auth_file = "$ENV{'HOME'}/.ssh/auth.defaults";
- if( -e "$auth_file") {
- open( AUTH_FILE, "<$auth_file")
- || die "Unable to open $auth_file for reading\n";
- }
- else {
- return;
- }
- my @auth_data = <AUTH_FILE>;
- close(AUTH_FILE);
-
- # Remove
- foreach (@auth_data) {
- if (/^$ssh_url/i) {
- $_ = '';
- }
- }
-
- # Write data
- write_file( $auth_file, @auth_data );
-}
-
-# Update TC with correct hostname
-sub update_tc_hostname {
- my ( $tc_filename, $tc_host ) = @_;
-
- # Store TC data
- open( TC_FILESTREAM, "$tc_filename" )
- or die("Unable to open tc file $tc_filename!");
- my @tc_data = <TC_FILESTREAM>;
- close(TC_FILESTREAM);
-
- foreach my $line (@tc_data) {
-
- # Ignore comments
- my $first_character = substr( $line, 0, 1 );
- if ( $first_character eq '#' ) {
- next;
- }
-
- # Replace old entry with new entry
- my ($line_tc_host, $line_tc_name, $line_tc_path,
- $line_tc_status, $line_tc_platform, $line_tc_profile,
- ) = split( /\s+/, $line );
- $line =
- "$tc_host\t"
- . "$line_tc_name\t"
- . "$line_tc_path\t"
- . "$line_tc_status\t"
- . "$line_tc_platform\t"
- . "$line_tc_profile\n";
- }
-
- write_file( $tc_filename, @tc_data );
-}
-
-# Update XML hash with values from command line
-sub update_xml {
-
- # Set up data
- my ($xml_filename, $edit_mode) = @_;
- # Values for edit mode
- # 0 - Edit all
- # 1 - All but name
- # 2 - Customized.. to do
-
- if ( !-e $xml_filename ) {
- die "Unable to update xml file $xml_filename\n";
- }
-
- my $xml_ref = $xml->XMLin(
- $xml_filename,
- ForceArray => [qw(workdirectory profile)],
- KeyAttr => [],
- );
-
- # Handle
- if($edit_mode == 0) {
- $xml_ref->{handle} = get_entry( 'Site Entry Name', $xml_ref->{handle}, $option_default);
- }
-
- # Execution
- my $initial_exprovider = $xml_ref->{execution}{provider};
- $xml_ref->{execution}{provider} = get_entry(
- 'Execution Provider',
- $xml_ref->{execution}{provider},
- $option_default,
- @execution_providers,
- );
- my $current_exprovider = $xml_ref->{execution}{provider};
-
- # Handle changes in execution provider
- if( $initial_exprovider ne $current_exprovider ) {
- # Add SSH fields
- if( $current_exprovider eq 'ssh' ) {
- if( !$xml_ref->{execution}{url} ) {
- $xml_ref->{execution}{url} = 'unknown';
- }
- }
- # Add coaster fields
- if( $current_exprovider eq 'coaster' ) {
- if( !$xml_ref->{execution}{jobmanager} ) {
- $xml_ref->{execution}{jobmanager} = 'unknown';
- }
- if( !$xml_ref->{execution}{url} ){
- $xml_ref->{execution}{url} = 'unknown';
- }
- }
- }
-
- # Job manager
- if ( $xml_ref->{execution}{jobmanager}) {
- $xml_ref->{execution}{jobmanager} =
- get_entry( 'Execution Job Manager',
- $xml_ref->{execution}{jobmanager},
- $option_default
- );
- }
-
- # Execution URL
- if ( $xml_ref->{execution}{url} ) {
- $xml_ref->{execution}{url} =
- get_entry( 'Execution URL',
- $xml_ref->{execution}{url},
- $option_default
- );
- }
-
- # Grid FTP
- if ( $xml_ref->{gridftp} ) {
- $xml_ref->{gridftp}{url} =
- get_entry( 'GridFTP URL', $xml_ref->{gridftp}{url}, $option_default);
- }
-
- # Work directory
- if ( $xml_ref->{workdirectory} ) {
- $xml_ref->{workdirectory}[0] =~ s/\$HOME/$ENV{'HOME'}/;
- $xml_ref->{workdirectory} =
- [ get_entry( 'Work Directory', $xml_ref->{workdirectory}[0], $option_default) ];
- }
-
- # Job manager
- if ( $xml_ref->{jobmanager} ) {
- if ( $xml_ref->{jobmanager}{universe} ) {
- $xml_ref->{jobmanager}{universe} =
- get_entry( 'Job Universe', $xml_ref->{jobmanager}{universe}, $option_default);
- }
- if ( $xml_ref->{jobmanager}{url} ) {
- $xml_ref->{jobmanager}{url} =
- get_entry( 'Job Manager URL', $xml_ref->{jobmanager}{url}, $option_default);
- }
- if ( $xml_ref->{jobmanager}{major} ) {
- $xml_ref->{jobmanager}{major} =
- get_entry( 'Job Major Number',
- $xml_ref->{jobmanager}{major}, $option_default);
- }
- if ( $xml_ref->{jobmanager}{minor} ) {
- $xml_ref->{jobmanager}{minor} =
- get_entry( 'Job Minor Number',
- $xml_ref->{jobmanager}{minor}, $option_default);
- }
- }
-
- # Filesystem
- if ( $xml_ref->{filesystem} ) {
- if ( $xml_ref->{filesystem}{provider} ) {
- $xml_ref->{filesystem}{provider} = get_entry(
- 'Filesystem Provider',
- $xml_ref->{filesystem}{provider},
- $option_default,
- @execution_providers,
- );
- }
- if ( $xml_ref->{filesystem}{url} ) {
- $xml_ref->{filesystem}{url} =
- get_entry( 'Filesystem URL', $xml_ref->{filesystem}{url}, $option_default);
- }
- }
-
- # Profiles
- foreach my $profile ( @{ $xml_ref->{profile} } ) {
- $profile->{content} =
- get_entry( $profile->{key}, $profile->{content}, $option_default);
- }
-
- return $xml_ref;
-}
-
-# Write a file given variable and filename
-sub write_file {
- my ( $filename, @data ) = @_;
- open( TEMPFILESTREAM, ">$filename" )
- or die("Unable to open $filename!\n");
- print TEMPFILESTREAM @data;
- close(TEMPFILESTREAM);
-}
-
-# Print all files in a directory
-sub print_directory {
- my ($directory) = @_;
- chdir($directory)
- || die "Unable to change directories to $directory\n";
- my @files = <*>;
- foreach my $file (@files) {
- ( my $basename, my $ext ) = split( /\./, $file );
- my @path = split( '/', $basename );
- print "$basename\n";
- }
-}
-
# Add configuration from a template
if ($option_template) {
@@ -426,7 +64,7 @@
if ( !-e "$sites_input_file" ) {
die "Unable to find configuration for $sites_input_file\n";
}
- my $xml_ref = update_xml("$sites_input_file", 0);
+ my $xml_ref = update_xml("$sites_input_file", 0, $option_default, @execution_providers);
my $new_sitename = $xml_ref->{handle};
my $output_directory = "$dotswift_directory/sites/$new_sitename";
if ( -d "$output_directory" ) {
@@ -437,8 +75,7 @@
my @files_to_copy = <$from_directory/*>;
create_directory("$output_directory");
foreach my $file_to_copy (@files_to_copy) {
- copy( $file_to_copy, "$output_directory" )
- || die "Unable to copy to $file_to_copy to $output_directory\n";
+ copy_file($file_to_copy, $output_directory);
}
my $xml_out_ref = $xml->XMLout(
$xml_ref,
@@ -474,14 +111,13 @@
die "Unable to find configuration named $option_remove\n";
}
- my $xml_filename = "$dotswift_directory/sites/$option_remove/sites.xml";
- my $xml_ref = $xml->XMLin(
- $xml_filename,
- ForceArray => [qw(workdirectory profile)],
- KeyAttr => [],
- );
-
# Remove SSH entry if needed
+ #my $xml_filename = "$dotswift_directory/sites/$option_remove/sites.xml";
+ #my $xml_ref = $xml->XMLin(
+ # $xml_filename,
+ # ForceArray => [qw(workdirectory profile)],
+ # KeyAttr => [],
+ #);
#if($xml_ref->{execution}{provider} eq "ssh") {
# remove_ssh($xml_ref->{execution}{url});
#}
@@ -518,7 +154,7 @@
ForceArray => [qw(workdirectory profile)],
KeyAttr => [],
);
- $xml_ref = update_xml("$sites_file", 1);
+ $xml_ref = update_xml("$sites_file", 1, $option_default, @execution_providers);
# Save
my $xml_out_ref = $xml->XMLout(
@@ -551,8 +187,7 @@
my @files_to_copy = <$input_directory/*>;
foreach my $file_to_copy (@files_to_copy) {
- copy( $file_to_copy, "$output_directory" )
- || die "Unable to copy $file_to_copy to $output_directory\n";
+ copy_file($file_to_copy, $output_directory);
}
# Rename and save
@@ -572,7 +207,10 @@
# Edit sites
my $initial_exurl = $xml_ref->{execution}{url};
- $xml_ref = update_xml( "$output_directory/sites.xml", 1);
+ $xml_ref = update_xml( "$output_directory/sites.xml",
+ 1,
+ $option_default,
+ @execution_providers);
my $new_exurl = $xml_ref->{execution}{url};
$xml_out_ref = $xml->XMLout(
@@ -605,10 +243,9 @@
=head1 OVERVIEW
-The swiftconfig program allows users to configure Swift. It allows for the
-adding, removing, and modification of remote sites by utilizing a set of
-standard templates. It also provides a way to quickly add, remove and modify
-translation catalog entries without having to manually edit files.
+The swiftconfig program allows users to configure Swift. It enables users to
+add configurations based on templates, copy configurations, remove configurations,
+and edit configurations.
=head1 DESCRIPTION
Modified: usertools/swift/swiftconfig/bin/swiftrun
===================================================================
--- usertools/swift/swiftconfig/bin/swiftrun 2010-08-14 22:45:00 UTC (rev 3535)
+++ usertools/swift/swiftconfig/bin/swiftrun 2010-08-15 18:25:13 UTC (rev 3536)
@@ -11,70 +11,8 @@
use File::Which qw(which where);
use XML::Simple;
use Pod::Usage;
+use SwiftConfig;
-# Figure out the path to Swift
-my $swift_home = q{};
-if ( $ENV{'SWIFT_HOME'} && -e "$ENV{'SWIFT_HOME'}/bin/swift" ) {
- $swift_home = $ENV{'SWIFT_HOME'};
-}
-my @execution_path = split( '/', abs_path($0) );
- at execution_path = splice( @execution_path, 0, $#execution_path - 1 );
-my $parent_directory = join( '/', @execution_path );
-if ( !$swift_home ) {
- if ( -e "$parent_directory/bin/swift" ) {
- $swift_home = $parent_directory;
- }
- else {
- @execution_path = split( '/', which("swift") );
- @execution_path = splice( @execution_path, 0, $#execution_path - 1 );
- my $tmp = join( '/', @execution_path );
- if ( -e "$tmp/bin/swift" ) {
- $swift_home = $tmp;
- }
- }
-}
-
-# Create a new directory if it does not exist
-sub create_directory {
- my $directory = $_[0];
- if ( !-d "$directory" ) {
- mkdir "$directory", 0700
- || die "Unable to create directory $directory\n";
- }
-}
-
-# Append one file to another
-sub cat_file {
- my ( $src, $dst ) = @_;
- if ( -e $dst ) {
- open( DSTFILE, ">>$dst" ) || die "Unable to open $dst for append\n";
- }
- else {
- open( DSTFILE, ">$dst" )
- or die "Unable to open $dst for writing\n";
- }
- open( SRCFILE, $src ) || die "Unable to open $src for reading\n";
- foreach my $line (<SRCFILE>) {
- print DSTFILE $line;
- }
-}
-
-# Copy a file to a new location
-sub copy_file {
- my ( $src, $dst ) = @_;
- if ( !-e $src ) { die "Unable to access $src\n"; }
- if ( !-e $dst ) { die "Unable to access $dst\n"; }
- copy( $src, $dst ) or die "Unable to copy $src to $dst\n";
-}
-
-# Strip directory name out of script
-sub strip_directory {
- my ($fullpath) = @_;
- my @path = split( '/', $fullpath );
- my $filename = $path[$#path];
- return $filename;
-}
-
# Command line processing
my @option_sites; # Site name to execute upon
my $option_script; # Script name to run
@@ -84,10 +22,9 @@
'site|sites=s{,}' => \@option_sites,
'script=s' => \$option_script,
'input|inputs=s{,}' => \@option_inputs,
- 'help' => sub { pod2usage(-verbose => 2); },
+ 'help|man' => sub { pod2usage(-verbose => 2); },
);
-
# Create run directory
my $script_filename = strip_directory($option_script);
my $dotswift_directory = "$ENV{'HOME'}/.swift";
Added: usertools/swift/swiftconfig/lib/perl/SwiftConfig.pm
===================================================================
--- usertools/swift/swiftconfig/lib/perl/SwiftConfig.pm (rev 0)
+++ usertools/swift/swiftconfig/lib/perl/SwiftConfig.pm 2010-08-15 18:25:13 UTC (rev 3536)
@@ -0,0 +1,467 @@
+package SwiftConfig;
+
+use 5.010001;
+use strict;
+use warnings;
+
+use File::Copy;
+use File::Path;
+use Pod::Usage;
+use FindBin qw($Bin);
+use lib "$FindBin::Bin/../lib/perl"; # Use libraries in $swift_home/lib/perl
+use SwiftConfig;
+use Getopt::Long;
+use File::Which qw(which where);
+use XML::Simple;
+
+require Exporter;
+our @ISA = qw(Exporter);
+
+# Items to export into callers namespace by default. Note: do not export
+# names by default without a very good reason. Use EXPORT_OK instead.
+# Do not simply export all your public functions/methods/constants.
+
+# This allows declaration use SwiftConfig ':all';
+# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
+# will save memory.
+our %EXPORT_TAGS = ( 'all' => [ qw(
+) ] );
+our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
+our @EXPORT = qw(create_directory get_entry add_ssh remove_ssh
+update_tc_hostname update_xml write_file print_directory strip_directory
+copy_file cat_file
+);
+our $VERSION = '0.01';
+
+# Prepare data
+my $xml = new XML::Simple();
+
+# Create a new directory if it does not exist
+sub create_directory {
+ my $directory = $_[0];
+ if ( !-d "$directory" ) {
+ mkdir "$directory", 0700
+ or die "Unable to create directory $directory\n";
+ }
+}
+
+# Process keyboard input
+sub get_entry {
+ my ( $entry_description, $entry_value, $use_default, @allowable_values, ) = @_;
+
+ # If use_default is specified, automatically plug in the default value
+ if($use_default) {
+ return $entry_value;
+ }
+
+ if(!@allowable_values) {
+ print "$entry_description [$entry_value]: ";
+ }
+ else {
+ print "$entry_description [";
+ my $counter=0;
+ foreach my $allowable(@allowable_values) {
+ if($counter != 0) {
+ print " ";
+ }
+ if($allowable eq $entry_value) {
+ print "*$allowable";
+ }
+ else {
+ print "$allowable";
+ }
+ $counter++;
+ }
+ print "]: ";
+ }
+ my $new_value = <STDIN>;
+ chomp($new_value);
+ if ($new_value) {
+ $entry_value = $new_value;
+ }
+
+ # Check if value entered is valid (if valid values were passed)
+ my $is_valid = 0;
+ if (@allowable_values) {
+ foreach my $allowable (@allowable_values) {
+ if ( $allowable eq $entry_value ) {
+ $is_valid = 1;
+ }
+ }
+ if ( !$is_valid ) {
+ my $error_message = q{};
+ foreach my $allowable (@allowable_values) {
+ $error_message = $error_message . $allowable . ' ';
+ }
+ print 'Invalid value selected. Please select from: '
+ . "$error_message\n";
+ $entry_value = get_entry( $entry_description, $entry_value,
+ @allowable_values, );
+ }
+ }
+ return $entry_value;
+}
+
+# Add new entry to auths.default
+sub add_ssh {
+ my ( $ssh_site, $ssh_url) = @_;
+
+ # Open authfile
+ my $auth_file = "$ENV{'HOME'}/.ssh/auth.defaults";
+ if( -e "$auth_file") {
+ open( AUTH_FILE, "<$auth_file")
+ || die "Unable to open $auth_file for reading\n";
+ }
+ else {
+ open( AUTH_FILE, "+>$auth_file" )
+ || die "Unable to open $auth_file for read/write!\n";
+ }
+ my @auth_data = <AUTH_FILE>;
+ close(AUTH_FILE);
+
+ # Get existing values and modify if found
+ my $ssh_username;
+ my $ssh_password;
+ my $found = 0;
+ foreach (@auth_data) {
+ if (/username/i && /^$ssh_url/i) {
+ ( my $setting, $ssh_username, ) = split( '=', $_ );
+ chomp($ssh_username);
+ $ssh_username = get_entry( 'Username', $ssh_username, 0);
+ $_ = "$ssh_url.username=$ssh_username\n";
+ $found = 1;
+ }
+ if (/\.password/i && /^$ssh_url/i) {
+ ( my $setting, $ssh_password, ) = split( '=', $_ );
+ chomp($ssh_password);
+ $ssh_password = get_entry( 'Password', $ssh_password, 0);
+ $_ = "$ssh_url.password=$ssh_password\n";
+ $found = 1;
+ }
+ }
+
+ # Add new entry if needed
+ if(!$found) {
+ $ssh_username = getlogin();
+ $ssh_username = get_entry( 'Username', $ssh_username, 0 );
+ $ssh_password = get_entry( 'Password', '', 0);
+
+ # Password based authentication
+ push( @auth_data, "$ssh_url.type=password\n" );
+ push( @auth_data, "$ssh_url.username=$ssh_username\n" );
+ push( @auth_data, "$ssh_url.password=$ssh_password\n" );
+ }
+
+ # Write data
+ write_file( $auth_file, @auth_data );
+}
+
+# Remove an entry from auth.defaults
+sub remove_ssh {
+ my ( $ssh_url, ) = @_;
+
+ # Open auth.defaults
+ my $auth_file = "$ENV{'HOME'}/.ssh/auth.defaults";
+ if( -e "$auth_file") {
+ open( AUTH_FILE, "<$auth_file")
+ || die "Unable to open $auth_file for reading\n";
+ }
+ else {
+ return;
+ }
+ my @auth_data = <AUTH_FILE>;
+ close(AUTH_FILE);
+
+ # Remove
+ foreach (@auth_data) {
+ if (/^$ssh_url/i) {
+ $_ = '';
+ }
+ }
+
+ # Write data
+ write_file( $auth_file, @auth_data );
+}
+
+# Update TC with correct hostname
+sub update_tc_hostname {
+ my ( $tc_filename, $tc_host ) = @_;
+
+ # Store TC data
+ open( TC_FILESTREAM, "$tc_filename" )
+ or die("Unable to open tc file $tc_filename!");
+ my @tc_data = <TC_FILESTREAM>;
+ close(TC_FILESTREAM);
+
+ foreach my $line (@tc_data) {
+
+ # Ignore comments
+ my $first_character = substr( $line, 0, 1 );
+ if ( $first_character eq '#' ) {
+ next;
+ }
+
+ # Replace old entry with new entry
+ my ($line_tc_host, $line_tc_name, $line_tc_path,
+ $line_tc_status, $line_tc_platform, $line_tc_profile,
+ ) = split( /\s+/, $line );
+ $line =
+ "$tc_host\t"
+ . "$line_tc_name\t"
+ . "$line_tc_path\t"
+ . "$line_tc_status\t"
+ . "$line_tc_platform\t"
+ . "$line_tc_profile\n";
+ }
+
+ write_file( $tc_filename, @tc_data );
+}
+
+# Update XML hash with values from command line
+sub update_xml {
+
+ # Set up data
+ my ($xml_filename, $edit_mode, $option_default, @execution_providers) = @_;
+ # Values for edit mode
+ # 0 - Edit all
+ # 1 - All but name
+ # 2 - Customized.. to do
+
+ if ( !-e $xml_filename ) {
+ die "Unable to update xml file $xml_filename\n";
+ }
+
+ my $xml_ref = $xml->XMLin(
+ $xml_filename,
+ ForceArray => [qw(workdirectory profile)],
+ KeyAttr => [],
+ );
+
+ # Handle
+ if($edit_mode == 0) {
+ $xml_ref->{handle} = get_entry( 'Site Entry Name', $xml_ref->{handle}, $option_default);
+ }
+
+ # Execution
+ my $initial_exprovider = $xml_ref->{execution}{provider};
+ $xml_ref->{execution}{provider} = get_entry(
+ 'Execution Provider',
+ $xml_ref->{execution}{provider},
+ $option_default,
+ @execution_providers,
+ );
+ my $current_exprovider = $xml_ref->{execution}{provider};
+
+ # Handle changes in execution provider
+ if( $initial_exprovider ne $current_exprovider ) {
+ # Add SSH fields
+ if( $current_exprovider eq 'ssh' ) {
+ if( !$xml_ref->{execution}{url} ) {
+ $xml_ref->{execution}{url} = 'unknown';
+ }
+ }
+ # Add coaster fields
+ if( $current_exprovider eq 'coaster' ) {
+ if( !$xml_ref->{execution}{jobmanager} ) {
+ $xml_ref->{execution}{jobmanager} = 'unknown';
+ }
+ if( !$xml_ref->{execution}{url} ){
+ $xml_ref->{execution}{url} = 'unknown';
+ }
+ }
+ }
+
+ # Job manager
+ if ( $xml_ref->{execution}{jobmanager}) {
+ $xml_ref->{execution}{jobmanager} =
+ get_entry( 'Execution Job Manager',
+ $xml_ref->{execution}{jobmanager},
+ $option_default
+ );
+ }
+
+ # Execution URL
+ if ( $xml_ref->{execution}{url} ) {
+ $xml_ref->{execution}{url} =
+ get_entry( 'Execution URL',
+ $xml_ref->{execution}{url},
+ $option_default
+ );
+ }
+
+ # Grid FTP
+ if ( $xml_ref->{gridftp} ) {
+ $xml_ref->{gridftp}{url} =
+ get_entry( 'GridFTP URL', $xml_ref->{gridftp}{url}, $option_default);
+ }
+
+ # Work directory
+ if ( $xml_ref->{workdirectory} ) {
+ $xml_ref->{workdirectory}[0] =~ s/\$HOME/$ENV{'HOME'}/;
+ $xml_ref->{workdirectory} =
+ [ get_entry( 'Work Directory', $xml_ref->{workdirectory}[0], $option_default) ];
+ }
+
+ # Job manager
+ if ( $xml_ref->{jobmanager} ) {
+ if ( $xml_ref->{jobmanager}{universe} ) {
+ $xml_ref->{jobmanager}{universe} =
+ get_entry( 'Job Universe', $xml_ref->{jobmanager}{universe}, $option_default);
+ }
+ if ( $xml_ref->{jobmanager}{url} ) {
+ $xml_ref->{jobmanager}{url} =
+ get_entry( 'Job Manager URL', $xml_ref->{jobmanager}{url}, $option_default);
+ }
+ if ( $xml_ref->{jobmanager}{major} ) {
+ $xml_ref->{jobmanager}{major} =
+ get_entry( 'Job Major Number',
+ $xml_ref->{jobmanager}{major}, $option_default);
+ }
+ if ( $xml_ref->{jobmanager}{minor} ) {
+ $xml_ref->{jobmanager}{minor} =
+ get_entry( 'Job Minor Number',
+ $xml_ref->{jobmanager}{minor}, $option_default);
+ }
+ }
+
+ # Filesystem
+ if ( $xml_ref->{filesystem} ) {
+ if ( $xml_ref->{filesystem}{provider} ) {
+ $xml_ref->{filesystem}{provider} = get_entry(
+ 'Filesystem Provider',
+ $xml_ref->{filesystem}{provider},
+ $option_default,
+ @execution_providers,
+ );
+ }
+ if ( $xml_ref->{filesystem}{url} ) {
+ $xml_ref->{filesystem}{url} =
+ get_entry( 'Filesystem URL', $xml_ref->{filesystem}{url}, $option_default);
+ }
+ }
+
+ # Profiles
+ foreach my $profile ( @{ $xml_ref->{profile} } ) {
+ $profile->{content} =
+ get_entry( $profile->{key}, $profile->{content}, $option_default);
+ }
+
+ return $xml_ref;
+}
+
+# Write a file given variable and filename
+sub write_file {
+ my ( $filename, @data ) = @_;
+ open( TEMPFILESTREAM, ">$filename" )
+ or die("Unable to open $filename!\n");
+ print TEMPFILESTREAM @data;
+ close(TEMPFILESTREAM);
+}
+
+# Print all files in a directory
+sub print_directory {
+ my ($directory) = @_;
+ chdir($directory)
+ || die "Unable to change directories to $directory\n";
+ my @files = <*>;
+ foreach my $file (@files) {
+ ( my $basename, my $ext ) = split( /\./, $file );
+ my @path = split( '/', $basename );
+ print "$basename\n";
+ }
+}
+
+# Append one file to another
+sub cat_file {
+ my ( $src, $dst ) = @_;
+ if ( -e $dst ) {
+ open( DSTFILE, ">>$dst" ) || die "Unable to open $dst for append\n";
+ }
+ else {
+ open( DSTFILE, ">$dst" )
+ or die "Unable to open $dst for writing\n";
+ }
+ open( SRCFILE, $src ) || die "Unable to open $src for reading\n";
+ foreach my $line (<SRCFILE>) {
+ print DSTFILE $line;
+ }
+}
+
+# Copy a file to a new location
+sub copy_file {
+ my ( $src, $dst ) = @_;
+ if ( !-e $src ) { die "Unable to access $src\n"; }
+ if ( !-e $dst ) { die "Unable to access $dst\n"; }
+ copy( $src, $dst ) or die "Unable to copy $src to $dst\n";
+}
+
+# Strip directory name out of a string
+sub strip_directory {
+ my ($fullpath) = @_;
+ my @path = split( '/', $fullpath );
+ my $filename = $path[$#path];
+ return $filename;
+}
+
+1;
+__END__
+
+=head1 SwiftConfig
+
+SwiftConfig - Perl module for swiftconfig and swiftrun applications
+
+=head1 SYNOPSIS
+
+ use SwiftConfig;
+
+=head1 DESCRIPTION
+
+The SwiftConfig module provides a set of standard routines needed to handle
+Swift Configuration.
+
+create_directory(directory_name)
+ Creates a directory, handles errors
+
+get_entry($entry_description, $entry_value, $use_default, @allowable_values)
+ get_entry is used to get input from the user
+ $entry_description is used to describe the data to the user
+ $entry_value is the default value. If the user hits enter, it will be used
+ $use_default, if true, will set input to $entry_value without prompting
+ @allowable_values is a list of all available options for that input
+
+add_ssh($ssh_site, $ssh_url)
+ Add an entry to ~/.ssh/auth.defaults
+ $ssh_site is the swift configuration name
+ $ssh_url is the network name/address to connect to
+
+remove_ssh($ssh_url)
+ Remove an entry from ~/.ssh/auth.defaults
+ $ssh_url is the network name/address to remove
+
+update_tc_hostname($filename, $tc_hostname)
+ Change the site name in a tc.data file
+ $filename is the filename of the tc.data
+ $tc_hostname is the value of the new hostname
+
+update_xml($xml_filename, $edit_mode, $option_default, @execution_providers)
+ Edit and update a sites.xml file
+ $xml_filename filename to edit
+ There are currently two edit modes: 0=edit everything, 1=everything but name
+ $option_default, if true, will automatically use defaults
+ @execution_providers is the list of all valid execution providers
+
+write_file($filename, @data)
+ Write to $filename with @data. Handle errors
+
+print_directory($directory)
+ List all files within a directory without paths
+
+cat_file($src, $dst)
+ Concatenate $src to $dst, handle errors
+
+copy_file($src, $dst)
+ Copy file $src to $dst, handle errors
+
+strip_directory($fullpath)
+ Given a string like "/foo/blah/file.txt", return "file.txt"
+=cut
More information about the Swift-commit
mailing list