[Swift-commit] r3539 - in usertools/swift/swiftconfig: bin lib/perl

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Mon Aug 16 00:35:45 CDT 2010


Author: davidk
Date: 2010-08-16 00:35:45 -0500 (Mon, 16 Aug 2010)
New Revision: 3539

Modified:
   usertools/swift/swiftconfig/bin/swiftconfig
   usertools/swift/swiftconfig/bin/swiftrun
   usertools/swift/swiftconfig/lib/perl/SwiftConfig.pm
Log:
Preliminary version of TC editing


Modified: usertools/swift/swiftconfig/bin/swiftconfig
===================================================================
--- usertools/swift/swiftconfig/bin/swiftconfig	2010-08-15 22:20:15 UTC (rev 3538)
+++ usertools/swift/swiftconfig/bin/swiftconfig	2010-08-16 05:35:45 UTC (rev 3539)
@@ -15,18 +15,16 @@
 use File::Which qw(which where);
 
 # Variables used for sites.xml
-my $option_templates = q{};            # List all templates
-my $option_remove    = q{};            # Remove entry from sites.xml
-my $option_edit      = q{};            # Edit mode
-my $option_sites     = q{};            # List all usable sites
-my $option_describe  = q{};            # Print raw XML configuration
-my $option_default   = q{};            # When adding a site, use all defaults
-my $option_template  = q{};            # Create config from template specified
-my $option_copy      = q{};            # Copy an existing configuration
+my $option_templates    = q{};            # List all templates
+my $option_remove       = q{};            # Remove entry from sites.xml
+my $option_edit         = q{};            # Edit mode
+my $option_sites        = q{};            # List all usable sites
+my $option_describe     = q{};            # Print raw XML configuration
+my $option_default      = q{};            # When adding a site, use all defaults
+my $option_template     = q{};            # Create config from template specified
+my $option_copy         = q{};            # Copy an existing configuration
+my $option_applications = q{};            # Edit applications
 
-my @execution_providers =
-    ( "gt4", "local", "pbs", "condor", "ssh", "coaster", );
-
 # Assign variables from command line options
 GetOptions(
     'remove=s'              => \$option_remove,
@@ -38,11 +36,13 @@
     'template=s'            => \$option_template,
     'edit=s'                => \$option_edit,
     'copy=s'                => \$option_copy,
+    'apps|applications'     => \$option_applications,
     'help|man'                  => sub { pod2usage(-verbose => 2); },
 );
 
 # Prepare data
 my $xml = new XML::Simple();
+initialize_swiftconfig();
 
 # Create all required directories
 my $dotswift_directory = "$ENV{'HOME'}/.swift";
@@ -64,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, $option_default, @execution_providers);
+    my $xml_ref          = update_xml("$sites_input_file", 0, $option_default);
     my $new_sitename     = $xml_ref->{handle};
     my $output_directory = "$dotswift_directory/sites/$new_sitename";
     if ( -d "$output_directory" ) {
@@ -154,7 +154,7 @@
         ForceArray => [qw(workdirectory profile)],
         KeyAttr    => [],
     );
-    $xml_ref = update_xml("$sites_file", 1, $option_default, @execution_providers);
+    $xml_ref = update_xml("$sites_file", 1, $option_default);
     
    # Save
     my $xml_out_ref = $xml->XMLout(
@@ -207,10 +207,7 @@
 
     # Edit sites
     my $initial_exurl = $xml_ref->{execution}{url}; 
-    $xml_ref = update_xml( "$output_directory/sites.xml", 
-                           1, 
-                           $option_default,
-                           @execution_providers);
+    $xml_ref = update_xml( "$output_directory/sites.xml", 1, $option_default,);
     my $new_exurl = $xml_ref->{execution}{url};
     
     $xml_out_ref = $xml->XMLout(
@@ -231,6 +228,55 @@
     }
 }
 
+# Add/Edit applications
+if($option_applications) {
+       my @applications = glob("$dotswift_directory/apps/*");
+       foreach(@applications) {
+           $_ = strip_directory($_);
+           my ($base, $ext) = split('\.');
+           $_ = $base;
+       }      
+       push(@applications, 'new');
+       
+       my $application_set = get_entry('Select application set', 'linux', 0, 
+                                       @applications);
+       if($application_set eq 'new') {
+           $application_set = get_entry('Name of new application set', '', 0);
+           my $now = time();
+           if(-e "$dotswift_directory/apps/$application_set.apps") {
+               print "Set named $application_set already exists\n";
+               return;
+           }
+           open(APPFILE, ">$dotswift_directory/apps/$application_set.apps")
+               || die "Unable to create file $dotswift_directory/apps/$application_set.apps";
+           print "$application_set set created\n\n";
+           close(APPFILE);
+       }
+       
+       my $quit = 0;
+       while($quit == 0) {
+           print "\n(E)dit, (a)dd, (r)emove, (l)ist, or (q)uit: ";
+           my $edit_or_add = <STDIN>;
+           chomp($edit_or_add);
+           
+           if(lc($edit_or_add) eq 'a') {
+               add_application("$dotswift_directory/apps/$application_set.apps");
+           }
+           elsif(lc($edit_or_add) eq 'q') {
+               $quit = 1;
+           }
+           elsif(lc($edit_or_add) eq 'r') {
+                remove_application("$dotswift_directory/apps/$application_set.apps");
+           }
+           elsif(lc($edit_or_add) eq 'l') {
+                list_applications("$dotswift_directory/apps/$application_set.apps");
+           }
+           elsif(lc($edit_or_add) eq 'e') {
+                edit_application("$dotswift_directory/apps/$application_set.apps");
+           }
+       }
+}
+
 __END__
 
 =head1 NAME

Modified: usertools/swift/swiftconfig/bin/swiftrun
===================================================================
--- usertools/swift/swiftconfig/bin/swiftrun	2010-08-15 22:20:15 UTC (rev 3538)
+++ usertools/swift/swiftconfig/bin/swiftrun	2010-08-16 05:35:45 UTC (rev 3539)
@@ -26,6 +26,7 @@
 );
 
 # Create run directory
+initialize_swiftconfig();
 my $script_filename = strip_directory($option_script);
 my $dotswift_directory = "$ENV{'HOME'}/.swift";
 my @run_directories = <run.????>;
@@ -35,46 +36,72 @@
 my ($run_number) = $run_directories[-1] =~ m{(\d+)};
 $run_number++;
 my $run_directory = getcwd() . "/" . sprintf "run.%04s", $run_number;
+my @sites_xml;
 create_directory($run_directory);
 copy_file( "$option_script", "$run_directory" );
 
-# Create configuration files for run
-foreach my $site (@option_sites) {
-    # Handle 'all' to use all available configurations at once
-    if( lc($site) eq 'all' ) {
-        foreach my $all_site(<$dotswift_directory/sites/*>) {
-            if ( !-d "$all_site" ) {
-                die "Unable to find directory for $all_site\n";
+# Sites.xml for 'all'
+if( grep /all/, @option_sites ) {
+    foreach my $all_site(<$dotswift_directory/sites/*>) {
+        if ( !-d "$all_site" ) {
+            die "Unable to find directory for $all_site\n";
+        }
+        if ( !-e "$all_site/sites.xml" ) {
+            die "Unable to find sites.xml for $all_site\n";
+        }
+        open(CURRENT_SITE, "$all_site/sites.xml")
+            || die "Unable to open $all_site/sites.xml";
+        my @current_site_data = <CURRENT_SITE>;
+        push(@sites_xml, @current_site_data);
+        close(CURRENT_SITE);
+        # Add applications
+        my @all_apps = glob("$dotswift_directory/apps/*.apps");
+        foreach my $current_app_file(@all_apps) {
+            open(CURRENT_APP, $current_app_file) || die "Unable to open app file $current_app_file\n";
+            my @current_app_data = <CURRENT_APP>;
+            close(CURRENT_APP);
+            $all_site = strip_directory($all_site);
+            foreach(@current_app_data) {
+                s/\$HOST/$all_site/ig;
             }
-            if ( !-e "$all_site/sites.xml" ) {
-                die "Unable to find sites.xml for $all_site\n";
-            }
-            if ( !-e "$all_site/tc.data" ) {
-                die "Unable to find tc.data for $all_site\n";
-            }
-            cat_file( "$all_site/sites.xml", "$run_directory/sites.xml" );
-            cat_file( "$all_site/tc.data", "$run_directory/tc.data" );
+            open(RUNTC, ">>$run_directory/tc.data") || die "Unable to write to $run_directory/tc.data\n";
+            print RUNTC @current_app_data;
+            close(RUNTC);
         }
     }
-    
-    # Normal, non-'all' site
-    else {    
+}
+  
+# Sites.xml for non-'all'
+else {
+    foreach my $site (@option_sites) {
         if ( !-d "$dotswift_directory/sites/$site" ) {
-            die "Unable to find entry for $site\n";
+            die "Unable to find configuration for $site\n";
         }
         if ( !-e "$dotswift_directory/sites/$site/sites.xml" ) {
             die "Unable to find sites.xml for $site\n";
+        }              
+        open(CURRENT_SITE, "$dotswift_directory/sites/$site/sites.xml")
+            || die "Unable to open $dotswift_directory/sites/$site/sites.xml";
+        my @current_site_data = <CURRENT_SITE>;
+        close(CURRENT_SITE);
+        push(@sites_xml,  @current_site_data);
+        # Add applications
+        my @all_apps = glob("$dotswift_directory/apps/*.apps");
+        foreach my $current_app_file(@all_apps) {
+            open(CURRENT_APP, $current_app_file) || die "Unable to open app file $current_app_file\n";
+            my @current_app_data = <CURRENT_APP>;
+            close(CURRENT_APP);
+            foreach(@current_app_data) {
+                s/\$HOST/$site/ig;
+            }
+            open(RUNTC, ">>$run_directory/tc.data") || die "Unable to write to $run_directory/tc.data\n";
+            print RUNTC @current_app_data;
+            close(RUNTC);
         }
-        if ( !-e "$dotswift_directory/sites/$site/tc.data" ) {
-            die "Unable to find tc.data for $site\n";
-        }
-        cat_file( "$dotswift_directory/sites/$site/sites.xml",
-                  "$run_directory/sites.xml" );
-        cat_file( "$dotswift_directory/sites/$site/tc.data",
-                  "$run_directory/tc.data" );
-   }
+    }
 }
 
+
 # Link input data
 foreach my $input_file (@option_inputs) {
     # Check for relative path
@@ -91,14 +118,9 @@
         || die "Unable to create symlink to $new_path\n";
 }
 
-# Complete XML tags
-open( SITEFILE, "$run_directory/sites.xml" ) 
-    || die "Unable to open $run_directory/sites.xml\n";
-my @sitedata = <SITEFILE>;
-close(SITEFILE);
 open( SITEFILE, ">$run_directory/sites.xml" ) 
     || die "Unable to open $run_directory/sites.xml for writing\n";
-print SITEFILE "<config>\n at sitedata</config>\n";
+print SITEFILE "<config>\n at sites_xml</config>\n";
 close(SITEFILE);
 
 # Run swift

Modified: usertools/swift/swiftconfig/lib/perl/SwiftConfig.pm
===================================================================
--- usertools/swift/swiftconfig/lib/perl/SwiftConfig.pm	2010-08-15 22:20:15 UTC (rev 3538)
+++ usertools/swift/swiftconfig/lib/perl/SwiftConfig.pm	2010-08-16 05:35:45 UTC (rev 3539)
@@ -16,25 +16,19 @@
 
 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_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	
+copy_file cat_file add_application edit_application remove_application
+list_applications initialize_swiftconfig
 );
 our $VERSION = '0.01';
 
 # Prepare data
 my $xml = new XML::Simple();
+my @execution_providers =
+    ( "gt4", "local", "pbs", "condor", "ssh", "coaster", );
 
 # Create a new directory if it does not exist
 sub create_directory {
@@ -221,7 +215,7 @@
 sub update_xml {
 
     # Set up data
-    my ($xml_filename, $edit_mode, $option_default, @execution_providers) = @_;
+    my ($xml_filename, $edit_mode, $option_default) = @_;
     # Values for edit mode
     # 0 - Edit all
     # 1 - All but name
@@ -248,7 +242,7 @@
         'Execution Provider',
         $xml_ref->{execution}{provider},
         $option_default,
-        @execution_providers, 
+        @execution_providers,
     );
     my $current_exprovider = $xml_ref->{execution}{provider};
     
@@ -331,7 +325,6 @@
                 'Filesystem Provider',
                 $xml_ref->{filesystem}{provider},
                 $option_default,
-                @execution_providers,
             );
         }
         if ( $xml_ref->{filesystem}{url} ) {
@@ -345,7 +338,6 @@
         $profile->{content} =
             get_entry( $profile->{key}, $profile->{content}, $option_default);
     }
-
     return $xml_ref;
 }
 
@@ -403,6 +395,155 @@
     return $filename;
 }
 
+# Add an application
+sub add_application {
+    my ($app_filename) = @_;
+    open(APPFILE, "+<$app_filename") || die "Unable to open application file $app_filename for writing\n";
+
+    print "Enter name of new application: ";
+    my $app_name = <STDIN>;
+    chomp($app_name);
+    foreach(<APPFILE>){ 
+        my ($site, $name, $path, $status, $platform, $profile) = split(/\s+/);
+        if($name eq $app_name) {
+            print "$name already exists\n";
+            return;
+        }
+    }
+    my $app_path_guess = which("$app_name");
+    if(!$app_path_guess) {
+        $app_path_guess = "/path/to/$app_name";
+    }
+    my $app_path = get_entry("Path", $app_path_guess, 0);
+    my $app_time = get_entry("Walltime (hh:mm:ss)", "00:20:00", 0);
+    while($app_time !~ m/(\d+:\d+:\d+)/ ) {
+        print "Error: incorrect format\n";
+        $app_time = get_entry("Walltime", $1, 0);
+    }
+    print APPFILE "\$HOST\t$app_name\t$app_path\tINSTALLED\tINTEL32::LINUX\tGLOBUS::maxwalltime=\"$app_time\"\n";
+    close(APPFILE);
+    print "Added $app_name\n";
+}
+            
+# Remove an application
+sub remove_application {
+    my ($app_filename) = @_;
+    open(APPFILE, "$app_filename") || die "Unable to open application file $app_filename\n";
+    my $app_to_remove = get_entry("Name of application to remove", '', 0);
+    my @app_data;
+    my $found=0;
+    foreach my $line (<APPFILE>){
+        my ($site, $name, $path, $status, $platform, $profile) = split(/\s+/, $line);
+        if($name eq $app_to_remove) {
+            $found=1;
+        }
+        else {
+            push(@app_data, $line);
+        }    
+    }
+    close(APPFILE);
+    if(!$found) {
+        print "Unable to find application named $app_to_remove\n";
+        return;
+    }
+    else {
+        write_file($app_filename, @app_data);
+        print "Removed $app_to_remove\n";
+    }
+}
+
+# List applications
+sub list_applications {
+    my ($app_filename) = @_;
+    open(APPFILE, $app_filename) || die "Unable to open application file $app_filename\n";
+    my @app_data = <APPFILE>;
+    close(APPFILE);
+    my ($site, $name, $path, $status, $platform, $profile);
+    my $counter=0;
+    foreach(@app_data) {
+        ($site, $name, $path, $status, $platform, $profile) = split(/\s+/);
+        my $nicely_formatted = sprintf("%-15s %-30s %-35s", $name, $path, $profile);
+        print "$nicely_formatted\n";
+    }
+
+}
+
+# Edit application
+sub edit_application {
+    my ($app_filename) = @_;
+    open(APPFILE, $app_filename) || die "Unable to open application file $app_filename\n";
+    my @app_data = <APPFILE>;
+    close(APPFILE);
+    print "Application to edit: ";
+    my $app_to_edit = <STDIN>;
+    chomp($app_to_edit);
+
+    my $found=0;
+    foreach(@app_data) {
+        my ($site, $name, $path, $status, $platform, $profile) = split(/\s+/);
+        if( $name eq $app_to_edit) {
+                $found=1;
+                my $previous_name = $name;
+                # App name
+		        $name = get_entry("New application name", $name, 0);
+                if($name ne $previous_name) {
+                    $path = which("$name");
+                }
+                # Path
+                if(!$path) {
+                    $path = "/path/app";
+                }
+                $path = get_entry("New path", $path, 0);
+                # Time
+                my $time = '';
+                my $wallfound=0;
+                my $counter=0;
+                my @profiles = split(/;/, $profile);
+                foreach (@profiles) {
+                    if( m/maxwalltime=\"(\d+:\d+:\d+)\"/ig ) {
+                        $wallfound=1;
+                        $time = get_entry("Walltime", $1, 0);
+                        while($time !~ m/(\d+:\d+:\d+)/ ) {
+                            print "Error: incorrect format\n";
+                            $time = get_entry("Walltime", $1, 0);
+                        }
+                        s/(\d+:\d+:\d+)/$time/g;
+                    }
+                }
+                if( !$wallfound ) {
+                    $time = get_entry("Time", "00:20:00", 0);
+                    push(@profiles, "GLOBUS::maxwalltime=\"$time\"");
+                }
+                $profile = join(';', @profiles);            
+                $_ = "$site\t$name\t$path\t$status\t$platform\t$profile\n";
+       }
+   }
+
+    if(!$found) {
+        print "Unable to find application named $app_to_edit\n";
+        return;
+    }
+    else {
+        print "$app_to_edit updated\n";
+    }
+    open(APPFILE, ">$app_filename") || die "Unable to open application file $app_filename for writing\n";
+    print APPFILE @app_data;
+    close(APPFILE);
+}
+
+# Initialize swiftconfig. Prepare things it needs for first run
+sub initialize_swiftconfig {
+    create_directory("$ENV{'HOME'}/.swift");
+    create_directory("$ENV{'HOME'}/.swift/sites");
+    if(!-e "$ENV{'HOME'}/.swift/apps") {
+        create_directory("$ENV{'HOME'}/.swift/apps");
+        my @app_files = glob("$FindBin::Bin/../etc/apps/*.apps");
+        foreach my $app_file(@app_files) {
+            copy_file($app_file, "$ENV{'HOME'}/.swift/apps");
+        }
+    }
+}
+
 1;
 __END__
 
@@ -443,12 +584,11 @@
     $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)
+update_xml($xml_filename, $edit_mode, $option_default)
     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




More information about the Swift-commit mailing list