[Swift-commit] r3595 - in trunk: bin lib/perl

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Fri Sep 3 14:25:50 CDT 2010


Author: davidk
Date: 2010-09-03 14:25:50 -0500 (Fri, 03 Sep 2010)
New Revision: 3595

Modified:
   trunk/bin/swiftconfig
   trunk/bin/swiftrun
   trunk/lib/perl/SwiftConfig.pm
Log:
Initial implementation of site groupings


Modified: trunk/bin/swiftconfig
===================================================================
--- trunk/bin/swiftconfig	2010-09-01 01:25:26 UTC (rev 3594)
+++ trunk/bin/swiftconfig	2010-09-03 19:25:50 UTC (rev 3595)
@@ -15,16 +15,17 @@
 use File::Which qw(which where);
 use Data::Dumper;
 
-# Variables used for sites.xml
-my $option_list         = q{};            # List information about config
-my $option_remove       = q{};            # Remove entry from sites.xml
-my $option_edit         = q{};            # Edit mode
-my $option_default      = q{};            # When adding a site, use all defaults
-my @option_templates    = ();            # Create config from template specified
-my $option_copy         = q{};            # Copy an existing configuration
-my $option_applications = q{};            # Edit applications
-my $option_import       = q{};            # Import existing configuration
-my @option_profiles 	= ();		  # Specify one or more profiles to edit
+# Variables for command line options
+my $option_list         = q{};      # List information about config
+my $option_remove       = q{};      # Remove entry from sites.xml
+my $option_edit         = q{};      # Edit mode
+my $option_default      = q{};      # When adding a site, use all defaults
+my @option_templates    = ();       # Create config from template specified
+my $option_copy         = q{};      # Copy an existing configuration
+my $option_applications = q{};      # Edit applications
+my $option_import       = q{};      # Import existing configuration
+my @option_profiles 	= ();       # Specify one or more profiles to edit
+my $option_groups       = q{};      # Edit site groupings
 
 # Assign variables from command line options
 GetOptions(
@@ -37,8 +38,9 @@
     'apps|applications'     	=> \$option_applications,
     'import=s'              	=> \$option_import,
     'list=s'                	=> \$option_list,
-    'help|man'              	=> sub { pod2usage(-verbose => 2); },
+    'help|man|h'              	=> sub { pod2usage(-verbose => 2); },
     'profile|profiles=s{,}' 	=> \@option_profiles,
+    'group|groups'              => \$option_groups,
 );
 
 # Prepare data
@@ -46,13 +48,14 @@
 initialize_swiftconfig();
 
 # Check command line options
-if( !$option_remove 
+if(    !$option_remove 
     && !@option_templates 
     && !$option_edit 
     && !$option_copy 
     && !$option_applications 
     && !$option_import 
-    && !$option_list) {
+    && !$option_list
+    && !$option_groups) {
     print "Incorrect usage. See swiftconfig -help for more information\n";  
     exit;
 }
@@ -78,7 +81,7 @@
     	if ( !-e "$sites_input_file" ) {
     	    die "Unable to find template for $option_template\n";
     	}
-	my $xml_ref          = update_xml("$sites_input_file", 0, $option_default);
+	    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" ) {
@@ -98,7 +101,6 @@
     	    SuppressEmpty => 1,
     	);
     	write_file( "$output_directory/sites.xml", $xml_out_ref );
-	
     	update_site_applications($new_sitename, $option_default);
 
     	# Handle SSH
@@ -143,7 +145,7 @@
     }
     else {
     	$xml_ref = update_xml("$xml_filename", 1, $option_default);
-	update_site_applications($option_edit, $option_default);
+	    update_site_applications($option_edit, $option_default);
     }
 
    # Save
@@ -214,6 +216,54 @@
     }
 }
 
+# Edit site groupings
+if($option_groups) {
+    my @groups = glob("$dotswift_directory/groups/*");
+    foreach(@groups) {
+        $_ = strip_directory($_);
+        my ($base, $ext) = split('\.');
+        $_ = $base;
+    }
+    push(@groups, 'new');
+    my $group_selection = get_entry('Select group', 'new', 0, @groups);
+    if($group_selection eq 'new') {
+        $group_selection = get_entry('Name of new group', '', 0);
+        if(-e "$dotswift_directory/groups/$group_selection.group") {
+            print "Group named $group_selection already exists!\n";
+            exit;
+        }
+        open(GROUPFILE, ">$dotswift_directory/groups/$group_selection.group")
+            || die "Unable to create group file $dotswift_directory/groups/$group_selection.group";
+        print "Group $group_selection created\n";
+        close(GROUPFILE);
+    }
+    
+    my $quit = 0;
+    my $group_filename = "$dotswift_directory/groups/$group_selection.group";
+    
+    while($quit == 0) {
+        print "\n(A)dd, (r)emove, (l)ist, or (q)uit: ";
+           my $command = <STDIN>;
+           chomp($command);
+           
+           if(lc($command) eq 'a') {
+               add_to_group($group_filename);
+           }
+           elsif(lc($command) eq 'q') {
+               $quit = 1;
+           }
+           elsif(lc($command) eq 'r') {
+                remove_from_group($group_filename);
+           }
+           elsif(lc($command) eq 'l') {
+                list_group($group_filename);
+           }
+           else {
+               print "Unknown option: $command\n";
+           }
+     }
+}
+
 # Add/Edit applications
 if($option_applications) {
        my @applications = glob("$dotswift_directory/apps/*");
@@ -311,6 +361,8 @@
         }
     }
 }
+
+
 __END__
 
 =head1 NAME
@@ -339,6 +391,7 @@
    -import               Import a sites.xml file into swiftconfig
    -list                 Options: templates, configs, or a config
    -apps                 Edit or create application sets
+   -groups               Edit site groupings
    
 =head1 EXAMPLES
 

Modified: trunk/bin/swiftrun
===================================================================
--- trunk/bin/swiftrun	2010-09-01 01:25:26 UTC (rev 3594)
+++ trunk/bin/swiftrun	2010-09-03 19:25:50 UTC (rev 3595)
@@ -17,17 +17,19 @@
 my @option_sites;     # Site name to execute upon
 my $option_script;    # Script name to run
 my @option_inputs;    # Name of data to include with execution
+my @option_groups;    # Name of group/groups to run script on
 
 GetOptions(
     'site|sites=s{,}'   => \@option_sites,
     'script=s'          => \$option_script,
     'input|inputs=s{,}' => \@option_inputs,
-    'help|man'              => sub { pod2usage(-verbose => 2); },
+    'help|man|h'        => sub { pod2usage(-verbose => 2); },
+    'group|groups=s{,}' => \@option_groups,
 );
 
 # Check arguments
 initialize_swiftconfig();
-if(!$option_script || !@option_sites) {
+if(!$option_script || (!@option_sites && !@option_groups)) {
     print "Incorrect usage. Try swiftrun -help\n";
     exit;
 }
@@ -43,109 +45,98 @@
 $run_number++;
 my $run_directory = getcwd() . "/" . sprintf "run.%04s", $run_number;
 create_directory($run_directory);
-my @sites_xml;
 
 # Copy over needed files
 copy_file( "$option_script", "$run_directory" );
 
-# 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";
-        }
-        my $properties_file = "$dotswift_directory/sites/$all_site/swift.properties";
-        if( -e "$properties_file") {
-            copy_file( $properties_file, "$run_directory/swift.properties");
-        }
+# Determine list of sites. Store each swiftconfig directory we need in @sites
+my @sites = ();
 
-        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;
-        if(-e "$all_site/apps") {
-            open(APPFILE, "$all_site/apps")
-                || die "Unable to open $all_site/apps";
-            @all_apps = <APPFILE>;
-            close(APPFILE);
-            foreach(@all_apps) {
-                chomp($_);
-                $_ = "$dotswift_directory/apps/$_.apps\n";
-            }
-        }
-        else {   
-            @all_apps = glob("$dotswift_directory/apps/*.apps");
-        }
-        foreach my $current_app_file(@all_apps) {
-            chomp($current_app_file);
-            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/SWIFTCONFIGURATION/$all_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);
-        }
+# Check for -sites all
+foreach my $option_site (@option_sites) {
+    if(lc($option_site) eq 'all') {
+        @sites = glob("$dotswift_directory/sites/*");
     }
 }
-  
-# Sites.xml for non-'all'
-else {
-    foreach my $site (@option_sites) {
-        if ( !-d "$dotswift_directory/sites/$site" ) {
-            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);
-        my $properties_file = "$dotswift_directory/sites/$site/swift.properties";
-        if( -e "$properties_file") {
-            copy_file( $properties_file, "$run_directory");
-        }
-        # Add applications
-        my @all_apps;
-        if(-e "$dotswift_directory/sites/$site/apps") {
-            open(APPFILE, "$dotswift_directory/sites/$site/apps")
-                || die "Unable to open $site/apps";
-            @all_apps = <APPFILE>;
-            close(APPFILE);
-            foreach(@all_apps) {
-                chomp($_);
-                $_ = "$dotswift_directory/apps/$_.apps";
+
+# Everything else specified by -sites
+foreach my $option_site (@option_sites) {
+    if(lc($option_site) eq 'all') {
+        next;
+    }
+    if(-d "$dotswift_directory/sites/$option_site") {
+        push(@sites, "$dotswift_directory/sites/$option_site");
+    }
+    else {
+        die "Unable to find configuration for $option_site\n";
+    }
+}
+
+# Groups
+foreach my $group(@option_groups) {
+    open(GROUPFILE, "$dotswift_directory/groups/$group.group")
+        || die "Unable to open file for $group\n";
+    while(<GROUPFILE>) {
+        chomp($_);
+        push(@sites, "$dotswift_directory/sites/$_");
+    }
+    close(GROUPFILE);
+}
+
+# Create configuration files for run         
+my @sites_xml;
+my @apps_data;
+foreach my $site (@sites) {
+    if ( !-d "$site" ) {
+        die "Unable to find configuration for $site\n";
+    }
+    
+    # Sites.xml
+    if ( !-e "$site/sites.xml" ) {
+        die "Unable to find sites.xml for $site\n";
+    }              
+    open(CURRENT_SITE, "$site/sites.xml")
+            || die "Unable to open $site/sites.xml";
+    push(@sites_xml, <CURRENT_SITE>);
+    close(CURRENT_SITE);
+    
+    # Properties
+    if( -e "$site/swift.properties") {
+        copy_file( "$site/swift.properties", "$run_directory");
+    }
+
+    # Applications
+    if(-e "$site/apps") {
+        open(APPFILE, "$site/apps") || die "Unable to open $site/apps";
+        while(<APPFILE>) {
+            chomp($_);
+            if(-e "$dotswift_directory/apps/$_.apps") {
+                open(APPDATA, "$dotswift_directory/apps/$_.apps")
+                    || die "Unable to open $dotswift_directory/apps/$_.apps";
+                my @temp_app_data = <APPDATA>;
+                close(APPDATA);
+                foreach(@temp_app_data) {
+                    my $simple_site_name = strip_directory($site);
+                    s/SWIFTCONFIGURATION/$simple_site_name/ig;
+                }
+                push(@apps_data, @temp_app_data);
             }
         }
-        else {   
-            @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/SWIFTCONFIGURATION/$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);
-        }
     }
 }
 
+# Save configuration information
+open(TC, ">>$run_directory/tc.data") 
+    || die "Unable to write to $run_directory/tc.data\n";
+print TC @apps_data;
+close(TC);
 
+open( SITEFILE, ">$run_directory/sites.xml" ) 
+    || die "Unable to open $run_directory/sites.xml for writing\n";
+print SITEFILE "<config>\n at sites_xml</config>\n";
+close(SITEFILE);
+exit;
+
 # Link input data
 foreach my $input_file (@option_inputs) {
     # Check for relative path
@@ -162,11 +153,6 @@
         || die "Unable to create symlink to $new_path\n";
 }
 
-open( SITEFILE, ">$run_directory/sites.xml" ) 
-    || die "Unable to open $run_directory/sites.xml for writing\n";
-print SITEFILE "<config>\n at sites_xml</config>\n";
-close(SITEFILE);
-
 # Run swift
 chdir("$run_directory") || die "Unable to chdir to $run_directory\n";
 print "Executing script $option_script\n";
@@ -213,10 +199,10 @@
 =head1 DESCRIPTION
 
 General operations:
-   -site <site1 site2>	Execute swift on given sitename(s)
-   -script file.swift	Specify the swift script to run
-   -input <file1 file2>  Link to data required for execution
-   
+   -site <site1 site2>	  Execute swift on given sitename(s)
+   -script file.swift	  Specify the swift script to run
+   -input <file1 file2>    Link to data required for execution
+   -groups <group1 group2> Run script on a site grouping
 =head1 EXAMPLES
 
 Run a script on a single site

Modified: trunk/lib/perl/SwiftConfig.pm
===================================================================
--- trunk/lib/perl/SwiftConfig.pm	2010-09-01 01:25:26 UTC (rev 3594)
+++ trunk/lib/perl/SwiftConfig.pm	2010-09-03 19:25:50 UTC (rev 3595)
@@ -21,7 +21,7 @@
 update_tc_hostname update_xml write_file print_directory strip_directory
 copy_file cat_file add_application edit_application remove_application
 list_applications initialize_swiftconfig update_site_applications import_tc
-import_xml edit_profile
+import_xml edit_profile add_to_group list_group remove_from_group
 );
 our $VERSION = '0.01';
 
@@ -542,6 +542,7 @@
 sub initialize_swiftconfig {
     create_directory("$ENV{'HOME'}/.swift");
     create_directory("$ENV{'HOME'}/.swift/sites");
+    create_directory("$ENV{'HOME'}/.swift/groups");
     if(!-e "$ENV{'HOME'}/.swift/apps") {
         create_directory("$ENV{'HOME'}/.swift/apps");
         my @app_files = glob("$FindBin::Bin/../etc/apps/*.apps");
@@ -703,6 +704,59 @@
     return $xml_ref;
 }
 
+sub add_to_group {
+    my ($group_filename) = @_;
+    if(!-e $group_filename) {
+        print "Unable to find file $group_filename for editing\n";
+        return;
+    }
+    open(GROUPFILE, ">>$group_filename") || die "Error opening $group_filename\n";
+    my @all_sites = glob("$ENV{'HOME'}/.swift/sites/*");
+    foreach(@all_sites) {
+        $_ = strip_directory($_);
+    }
+    my $new_site = get_entry("Enter name of site to add", '', 0, @all_sites);
+    
+    print GROUPFILE "$new_site\n";
+    close(GROUPFILE);    
+}
+
+sub list_group {
+    my ($group_filename) = @_;
+    if(!-e $group_filename) {
+        print "Unable to find $group_filename for editing\n";
+        return;
+    }
+    system("cat $group_filename");
+}
+
+sub remove_from_group {
+    my ($group_filename) = @_;
+    if(!-e $group_filename) {
+        print "Unable to find $group_filename for editing\n";
+        return;
+    } 
+    
+    open(GROUPFILE, $group_filename) || die "Error opening $group_filename\n";
+    my @group_data = <GROUPFILE>;
+    close(GROUPFILE);
+    
+    my $remove_group = get_entry("Enter name of site to remove", '', 0);
+    $remove_group .= "\n";
+    
+    my $found=0;
+    foreach(@group_data) {
+        if($_ eq $remove_group)
+        {
+            $found=1;
+            $_ = '';
+        }
+    }
+    if(!$found) {
+        print "Unable to find site $remove_group. Use 'l' to get a list\n";
+    }
+    write_file($group_filename, @group_data);
+}
 1;
 __END__
 




More information about the Swift-commit mailing list