Skip to content

Instantly share code, notes, and snippets.

@pmbuko
Last active December 18, 2015 19:29
Show Gist options
  • Save pmbuko/5833625 to your computer and use it in GitHub Desktop.
Save pmbuko/5833625 to your computer and use it in GitHub Desktop.
This was too ridiculous not to share. (I wrote it before I learned python.) This script outputs a puppet manifest file that is used to manage local scratch directories for each compute cluster user on all our computer cluster nodes. I realize it is an abomination and welcome slaps to the face and derision as long as they're done lovingly.
#!/bin/bash
##########################################
# MKCLUSTERSCRATCH -last modified 10/10/14
##########################################
function help {
cat <<EOF
MKCLUSTERSCRATCH TOOL
This tool generates a complete clusterscratch.pp puppet manifest file from a
list of cluster users generated by running 'qconf -suserl'. This generated
file defines the scratch directories that should exist on all cluster nodes.
`basename $0`: `basename $0` [-p] [-h]
You can optionally specify one of the following:
-p -- Write to /etc/puppetlabs/puppet/manifests/classes/clusterscratch.pp
-h -- Display this help screen.
With no options given, the clusterscratch.pp file will be created in the
current directory.
EOF
exit 0
}
function preload {
# load the necessary environment
source /sge/current/default/common/settings.sh
# get the user list and export it so it's available to perl
CLUSERS=$(qconf -suserl)
export CLUSERS
}
function main {
#cat <<EOF > /root/bin/scratch.pl
perl -w <<EOF
##########################################################
# SETTINGS - You might need to tweak items in this section
##########################################################
# list of users who need group write on their scratch dirs
my @gw_array = qw(
username1
username2
username3
);
# name (and path, if you want) of the output file
my \$outfile = \$ENV{"OUTFILE"};
#############################################
# THE CODE - Don't change anything below here
#############################################
# put the userlist into an array
my \$clusers = \$ENV{"CLUSERS"};
my @allusers = split(/^/,\$clusers);
# take the @gw_array and turn each item into a hash key,
# because it's easy to test for existence of hash keys.
my %gw_hash = ();
foreach my \$item (@gw_array) {
\$gw_hash{\$item} = "";
}
# open a new clusterscratch.pp file reference, overwriting any existing file
open(OUTFILE, ">\$outfile") || die "Cannot create \$outfile";
# write the top of the puppet manifest
print OUTFILE <<"HEADER";
# File generated by /root/bin/mkclustersratch script. If you are adding group
# write permissions to this file, please add the corresponding username to the
# \@gw_array definition in the script.
#
class cluster::scratchroot {
file { '/scratch':
ensure => directory,
owner => 'root',
group => 'root',
mode => '0755',
}
}
class cluster::scratch {
define cluster::scratchdir( \\\$u, \\\$g, \\\$m ) {
include cluster::scratchroot
file { "/scratch/\\\$name":
ensure => directory,
owner => \\\$u,
group => \\\$g,
mode => \\\$m,
require => File['/scratch'],
}
}
HEADER
# iterate through @allusers and create one scratchdir resource per user
print "Processing users";
foreach my \$user (@allusers) {
print ".";
chomp(\$user);
# get uid and gid
my @userinfo=split( /:/, \`getent passwd \$user\`);
next if (!@userinfo); # skip this user if they're not in ldap
my \$uid = \$userinfo[2];
my \$gid = \$userinfo[3];
# get directory mode
my \$mode;
if (exists \$gw_hash{\$user}) {
\$mode = "0775";
} else {
\$mode = "0755";
}
# write scratchdir resource
print OUTFILE <<"END";
cluster::scratchdir { "\$user":
u => "\$uid",
g => "\$gid",
m => "\$mode",
}
END
}
print "\nDone.\n";
# write closing bracket for clusterscratch class and close the file
print OUTFILE "}";
close(OUTFILE);
EOF
}
if [[ $# < 1 ]]; then
OUTFILE="clusterscratch.pp"
export OUTFILE
preload
main
echo "Created `pwd`/$OUTFILE. Use '-p' to create in /etc/puppetlabs/puppet/manifests/classes/."
elif [[ $# = 1 ]]; then
if [ $1 = "-p" ]; then
OUTFILE="/etc/puppetlabs/puppet/manifests/classes/clusterscratch.pp"
export OUTFILE
preload
main
echo "Created $OUTFILE."
exit 0
elif [ $1 = "-h" ]; then
help
exit 0
else
echo "Usage: `basename $0` [-p] [-h]"
echo "For additional help, run '`basename $0` -h'"
exit 0
fi
else
echo "Are you stupid or something? Use one or no options, please."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment