This will be the first (of many??) posts to spill outside the topics one would think you’d find on a site with the name “Your Mac Guy”. You’ve been warned.
Back in January my primary work responsibilities shifted from Mac servers and desktops (and all that entailed) to Linux servers and desktops and the multitude of new things that entails (at least here where I work). One of the new tasks I’ve picked up is user administration of our Sun Grid Engine (SGE) 500-node cluster. New or existing users who want to submit jobs to the cluster need to be added to custom groups or, in SGE-speak, usersets. We create usersets for each lab, so if the user is part of a lab that doesn’t currently have access to submit jobs, I need to create a new userset and add that userset to each of 16 separate queues.
That last part, adding usersets to queues, is the most tedious part. So tedious, in fact, that it forced my hand into developing a scripted solution. I likely could have found an existing script to accomplish the task for me, but then I wouldn’t have had an excuse to brush up on my 3-years dormant perl skills.
With the help of a couple perl books, a dash of google, a short debugging session, and the successful execution of the product on our production cluster, I bring you the following code. I welcome any and all suggestions for improvement and general verbal lashings for poor form. Please note, if you intend to try it, just modify the $configpath variable and the @configs array to suit.
#!/usr/bin/perl -w use strict; # This script automates the process of adding usersets to the user_lists section # of the SGE cluster queue conf files. It takes the userset name as a command # line argument or it prompts for the userset name if no argument is given. It # currently supports adding one userset at a time. # Path to config files to be modified my $configpath="/sge/current/default/spool/qmaster/cqueues/"; # Filenames of queues to be modified my @configs = qw( list.q your.q queue.q names.q here.q ); my $input=$ARGV[0]; # If no arguments passed, print helpful info and prompt for userset name if (! defined($input)) { print "This command will add a new group to the user_lists line of the following\n"; print "cluster queues:\n"; print "@configs\n\n"; # Get input from STDIN. print "Enter the name of the new userset (or ^c to exit): "; chomp($input=<STDIN>); } foreach my $file (@configs) { # open the file and dump all lines into an @LINES array. chdir($configpath); open(CONF,$file) || die "Cannot open $file"; chomp(my(@LINES)=<CONF>); close(CONF); # create a new config file in /tmp open(NEWCONF,">/tmp/$file") || die "Cannot create $file"; foreach my $line (@LINES) { # if the current line is the user_lists line if ( $line =~ m/^user_lists/ ) { # add the new userset to the end of the user_lists line print NEWCONF "$line $input\n"; } else { # print the line unmodified print NEWCONF "$line\n"; } } close(NEWCONF); # read in new config in place of old one system("qconf -Mq /tmp/$file"); # delete the temp config file unlink("/tmp/$file"); }
Follow me on twitter