[3b403dc] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
[6b7441a] | 3 | # This script was largely written by Jessica Hamrick (jhamrick), with |
---|
| 4 | # help from Kyle Brogle (broglek) |
---|
[3b403dc] | 5 | |
---|
| 6 | use strict; |
---|
| 7 | use warnings; |
---|
| 8 | use Getopt::Long; |
---|
| 9 | |
---|
[6b7441a] | 10 | # the usage for the program |
---|
[814d4f8] | 11 | my $usage = <<USAGE; |
---|
| 12 | Usage: gutenbach-client-config [-l|--list|-a|--add|-d|--delete] [QUEUE] [--host=HOST] |
---|
| 13 | |
---|
| 14 | -l, --list List available queues |
---|
| 15 | -a, --add QUEUE Add a queue (must be used with -h) |
---|
| 16 | -d, --delete QUEUE Delete a queue |
---|
| 17 | -s, --set-default QUEUE Set the default queue |
---|
| 18 | -h, --host HOST Hostname for the queue |
---|
| 19 | USAGE |
---|
[3b403dc] | 20 | |
---|
[6b7441a] | 21 | # initialize the variables that will hold the arguments |
---|
[3b403dc] | 22 | my $list = 0; |
---|
| 23 | my $add = ""; |
---|
| 24 | my $delete = ""; |
---|
| 25 | my $host = ""; |
---|
[7cdd65d] | 26 | my $default = ""; |
---|
[3b403dc] | 27 | |
---|
[6b7441a] | 28 | # get the options from the command line |
---|
[3b403dc] | 29 | GetOptions ('l|list' => \$list, |
---|
[7cdd65d] | 30 | 's|set-default=s' => \$default, |
---|
[3b403dc] | 31 | 'a|add=s' => \$add, |
---|
| 32 | 'd|delete=s' => \$delete, |
---|
| 33 | 'h|host=s' => \$host); |
---|
| 34 | |
---|
[6b7441a] | 35 | # set the path where the configuration files live |
---|
[3b403dc] | 36 | my $configpath = "$ENV{'HOME'}/.gutenbach"; |
---|
| 37 | |
---|
[6b7441a] | 38 | # if the configuration path doens't exist, then make it |
---|
[85a1ac1] | 39 | if (! -e $configpath) { |
---|
| 40 | mkdir "$configpath"; |
---|
| 41 | } |
---|
[6b7441a] | 42 | |
---|
| 43 | # if the 'default' option was specified, then set given queue to |
---|
| 44 | # default |
---|
[7cdd65d] | 45 | if($default and !$add and !$delete and !$list) { |
---|
[6b7441a] | 46 | # if the specified queue doesn't exist, then throw an error |
---|
[7cdd65d] | 47 | unless(-e "$configpath/$default") { |
---|
| 48 | print "Error: queue '$default' doesn't exist yet...you should add it first.\n"; |
---|
| 49 | exit 1; |
---|
| 50 | } |
---|
[6b7441a] | 51 | |
---|
| 52 | # if there already exists a default, then remove it so we can |
---|
| 53 | # replace it with the new default |
---|
[7cdd65d] | 54 | if( -e "$configpath/DEFAULT"){ |
---|
| 55 | unlink("$configpath/DEFAULT") or die "Couldn't remove config file '$configpath/DEFAULT'"; |
---|
| 56 | } |
---|
[6b7441a] | 57 | |
---|
| 58 | # check to make sure we can create symlinks |
---|
[7cdd65d] | 59 | my $symlink_exists = eval { symlink("",""); 1 }; |
---|
[6b7441a] | 60 | |
---|
| 61 | # if so, then create the symlink and report it |
---|
[7cdd65d] | 62 | if ($symlink_exists){ |
---|
| 63 | symlink("$configpath/$default","$configpath/DEFAULT"); |
---|
| 64 | print "Changed default queue to $default.\n"; |
---|
| 65 | } |
---|
[6b7441a] | 66 | |
---|
| 67 | # otherwise, throw an error |
---|
[7cdd65d] | 68 | else |
---|
| 69 | { |
---|
| 70 | print "Error creating symlink to $configpath/$default"; |
---|
| 71 | exit 1; |
---|
| 72 | } |
---|
| 73 | } |
---|
[85a1ac1] | 74 | |
---|
[6b7441a] | 75 | # if the 'list' option was specified, then list the existing queues |
---|
[7cdd65d] | 76 | elsif ($list and !$add and !$delete and !$default) { |
---|
[6b7441a] | 77 | # get the config files in the configuration path -- these are the queues |
---|
[3b403dc] | 78 | my @queues = glob("$configpath/*") or die "Couldn't find configuration files at '$configpath'"; |
---|
| 79 | |
---|
[6b7441a] | 80 | # for each of the queues, load the configuration file and print |
---|
| 81 | # the queue name and the host it's on |
---|
[3b403dc] | 82 | print "Queue\t\tHost\n"; |
---|
| 83 | foreach my $q (@queues) { |
---|
| 84 | my ($host, $queue); |
---|
| 85 | |
---|
| 86 | if (-r $q) { |
---|
| 87 | local $/; |
---|
| 88 | my $fh; |
---|
| 89 | open $fh, $q; |
---|
| 90 | eval <$fh>; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | print "$queue\t\t$host\n"; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
[6b7441a] | 97 | # if the 'add' option was specified, then add a new queue |
---|
[3b403dc] | 98 | elsif (!$list and $add and !$delete) { |
---|
[6b7441a] | 99 | |
---|
| 100 | # make sure there was a host specified as well (otherwise, we |
---|
| 101 | # won't know where to print to) |
---|
[3b403dc] | 102 | if (!$host) { |
---|
| 103 | print $usage; |
---|
| 104 | exit 1; |
---|
| 105 | } |
---|
| 106 | |
---|
[6b7441a] | 107 | # if the queue already exists, then print a warning |
---|
[3b403dc] | 108 | if (-e "$configpath/$add") { |
---|
| 109 | print "Warning: queue '$add' already exists\n"; |
---|
| 110 | } |
---|
| 111 | |
---|
[6b7441a] | 112 | # create the configuration file |
---|
[3b403dc] | 113 | open CONFIG, "> $configpath/$add" or die "Couldn't open config file '$configpath/$add'"; |
---|
| 114 | print CONFIG "\$host = \"$host\";\n"; |
---|
| 115 | print CONFIG "\$queue = \"$add\";\n"; |
---|
| 116 | close CONFIG; |
---|
| 117 | |
---|
| 118 | print "Added queue '$add' on host '$host'\n" |
---|
| 119 | } |
---|
| 120 | |
---|
[6b7441a] | 121 | # if the 'delete' option was specified, then delete an existing queue |
---|
[3b403dc] | 122 | elsif (!$list and !$add and $delete) { |
---|
[6b7441a] | 123 | |
---|
| 124 | # if the queue doesn't exist, then print an error and exit |
---|
[3b403dc] | 125 | if (! -e "$configpath/$delete") { |
---|
[6b7441a] | 126 | print "Error: queue '$delete' does not exist\n"; |
---|
[3b403dc] | 127 | exit 1; |
---|
| 128 | } |
---|
| 129 | |
---|
[6b7441a] | 130 | # otherwise, remove the configuration file |
---|
[3b403dc] | 131 | unlink("$configpath/$delete") or die "Couldn't remove config file '$configpath/$delete'"; |
---|
| 132 | } |
---|
| 133 | |
---|
[6b7441a] | 134 | # otherwise, it's an unrecognized option, so print the usage and exit |
---|
[3b403dc] | 135 | else { |
---|
| 136 | print $usage; |
---|
| 137 | exit 1; |
---|
| 138 | } |
---|