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

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Mon Aug 23 14:40:19 CDT 2010


Author: davidk
Date: 2010-08-23 14:40:19 -0500 (Mon, 23 Aug 2010)
New Revision: 3563

Modified:
   usertools/swift/swiftconfig/bin/swiftconfig
   usertools/swift/swiftconfig/lib/perl/SwiftConfig.pm
Log:
swiftconfig -template can add multiple hosts at once (useful with -defaults)
Can limit the profile settings you want to edit with -edit <site> -profile <setting1> <setting2>


Modified: usertools/swift/swiftconfig/bin/swiftconfig
===================================================================
--- usertools/swift/swiftconfig/bin/swiftconfig	2010-08-23 18:08:03 UTC (rev 3562)
+++ usertools/swift/swiftconfig/bin/swiftconfig	2010-08-23 19:40:19 UTC (rev 3563)
@@ -20,23 +20,25 @@
 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_template     = q{};            # Create config from template specified
+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
 
 # Assign variables from command line options
 GetOptions(
-    'remove=s'              => \$option_remove,
-    'default|defaults'      => \$option_default,
-    'full'                  => sub { $option_default = 0; },
-    'template=s'            => \$option_template,
-    'edit=s'                => \$option_edit,
-    'copy=s'                => \$option_copy,
-    'apps|applications'     => \$option_applications,
-    'import=s'              => \$option_import,
-    'list=s'                => \$option_list,
-    'help|man'              => sub { pod2usage(-verbose => 2); },
+    'remove=s'              	=> \$option_remove,
+    'default|defaults'      	=> \$option_default,
+    'full'                  	=> sub { $option_default = 0; },
+    'template|templates=s{,}'   => \@option_templates,
+    'edit=s'                	=> \$option_edit,
+    'copy=s'                	=> \$option_copy,
+    'apps|applications'     	=> \$option_applications,
+    'import=s'              	=> \$option_import,
+    'list=s'                	=> \$option_list,
+    'help|man'              	=> sub { pod2usage(-verbose => 2); },
+    'profile|profiles=s{,}' 	=> \@option_profiles,
 );
 
 # Prepare data
@@ -45,7 +47,7 @@
 
 # Check command line options
 if( !$option_remove 
-    && !$option_template 
+    && !@option_templates 
     && !$option_edit 
     && !$option_copy 
     && !$option_applications 
@@ -67,42 +69,45 @@
 my $template_directory = join( '/', @execution_path ) . '/etc/sites';
 
 # Add configuration from a template
-if ($option_template) {
+if (@option_templates) {
 
-    # Update XML info
-    my $from_directory   = "$template_directory/$option_template";
-    my $sites_input_file = "$from_directory/sites.xml";
-    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 $new_sitename     = $xml_ref->{handle};
-    my $output_directory = "$dotswift_directory/sites/$new_sitename";
-    if ( -d "$output_directory" ) {
-        die "Configuration for $new_sitename already exists\n";
-    }
+    foreach my $option_template(@option_templates) {
+    	# Update XML info
+    	my $from_directory   = "$template_directory/$option_template";
+    	my $sites_input_file = "$from_directory/sites.xml";
+    	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 $new_sitename     = $xml_ref->{handle};
+    	my $output_directory = "$dotswift_directory/sites/$new_sitename";
+    	if ( -d "$output_directory" ) {
+    	    print "Configuration for $new_sitename already exists\n";
+	    next;
+    	}
 
-    # Update and copy files to reflect input
-    my @files_to_copy = <$from_directory/*>;
-    create_directory("$output_directory");
-    foreach my $file_to_copy (@files_to_copy) {
-        copy_file($file_to_copy, $output_directory);
-    }
-    my $xml_out_ref = $xml->XMLout(
-        $xml_ref,
-        RootName      => 'pool',
-        SuppressEmpty => 1,
-    );
-    write_file( "$output_directory/sites.xml", $xml_out_ref );
+    	# Update and copy files to reflect input
+    	my @files_to_copy = <$from_directory/*>;
+    	create_directory("$output_directory");
+    	foreach my $file_to_copy (@files_to_copy) {
+    	    copy_file($file_to_copy, $output_directory);
+    	}
+    	my $xml_out_ref = $xml->XMLout(
+    	    $xml_ref,
+    	    RootName      => 'pool',
+    	    SuppressEmpty => 1,
+    	);
+    	write_file( "$output_directory/sites.xml", $xml_out_ref );
+	
+    	update_site_applications($new_sitename, $option_default);
 
-    update_site_applications($new_sitename, $option_default);
-
-    # Handle SSH
-    if ( $xml_ref->{execution}{provider} eq 'ssh' ) {
-        add_ssh( $new_sitename, $xml_ref->{execution}{url} );
+    	# Handle SSH
+    	if ( $xml_ref->{execution}{provider} eq 'ssh' ) {
+        	add_ssh( $new_sitename, $xml_ref->{execution}{url} );
+    	}
+    
+    	print "Successfully added $option_template\n";
     }
-    
-    print "Successfully added $option_template\n";
 }
 
 # Remove an entry
@@ -122,20 +127,25 @@
 if ($option_edit) {
     my $output_directory = "$dotswift_directory/sites/$option_edit";
 
-    my $sites_file = "$output_directory/sites.xml";
-    if ( !-e "$sites_file" ) {
+    my $xml_filename = "$output_directory/sites.xml";
+    if ( !-e "$xml_filename" ) {
         die "Unable to find configuration for $option_edit\n";
     }
 
-    create_directory($output_directory);    
-    my $xml_filename = "$output_directory/sites.xml";
     my $xml_ref = $xml->XMLin(
         $xml_filename,
         ForceArray => [qw(workdirectory profile)],
         KeyAttr    => [],
     );
-    $xml_ref = update_xml("$sites_file", 1, $option_default);
-    
+
+    if(@option_profiles) {
+	$xml_ref = edit_profile($xml_filename, @option_profiles);	
+    }
+    else {
+    	$xml_ref = update_xml("$xml_filename", 1, $option_default);
+	update_site_applications($option_edit, $option_default);
+    }
+
    # Save
     my $xml_out_ref = $xml->XMLout(
         $xml_ref,
@@ -143,7 +153,6 @@
         SuppressEmpty => 1,
     );
     write_file( "$output_directory/sites.xml", $xml_out_ref );
-    update_site_applications($option_edit, $option_default);
     
     # Add/modify/remove ssh entries as needed
     if ( $xml_ref->{execution}{provider} eq 'ssh' ) {

Modified: usertools/swift/swiftconfig/lib/perl/SwiftConfig.pm
===================================================================
--- usertools/swift/swiftconfig/lib/perl/SwiftConfig.pm	2010-08-23 18:08:03 UTC (rev 3562)
+++ usertools/swift/swiftconfig/lib/perl/SwiftConfig.pm	2010-08-23 19:40:19 UTC (rev 3563)
@@ -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
+import_xml edit_profile
 );
 our $VERSION = '0.01';
 
@@ -677,6 +677,32 @@
    print "Successfully imported application set named $set_name\n";
 }
 
+sub edit_profile {
+    my ($xml_filename, @profiles_to_edit) = @_;
+    if(! -e $xml_filename) {
+	print "Unable to edit profile on $xml_filename\n";
+	return;
+    }
+
+    my $xml_ref = $xml->XMLin(
+        $xml_filename,
+        ForceArray => [qw(workdirectory profile)],
+        KeyAttr    => [],      
+    );
+
+    
+    foreach my $profile ( @{ $xml_ref->{profile} } ) {
+	foreach my $profile_to_edit(@profiles_to_edit) {
+		if(lc($profile->{key}) eq lc($profile_to_edit)) {
+        		$profile->{content} =
+            			get_entry( $profile->{key}, $profile->{content}, 0);
+        	}
+    	}
+    }
+
+    return $xml_ref;
+}
+
 1;
 __END__
 




More information about the Swift-commit mailing list