]> mj.ucw.cz Git - moe.git/commitdiff
MO-P: mo-cms-users knows how to tie users to machines
authorroot <root@ceoi01.(none)>
Mon, 29 Jun 2015 17:46:39 +0000 (19:46 +0200)
committerroot <root@ceoi01.(none)>
Mon, 29 Jun 2015 17:46:39 +0000 (19:46 +0200)
mop/admin/mo-cms-users.pl
mop/admin/mo-get-users.sh

index 3dadea148c39f1db744dc99d9bd0b77581d9e8be..b6558b18380cd51e905def1309a626030df867b2 100644 (file)
@@ -1,16 +1,64 @@
 #!/usr/bin/perl
 use common::sense;
+use Getopt::Long;
 
-# FIXME: Tie to specified machines
+my $bind_to_ip;
+my $contest_id;
+
+Getopt::Long::Configure("bundling");
+GetOptions(
+       "i|bind-ip!" => \$bind_to_ip,
+       "c|contest=i" => \$contest_id,
+) && !$ARGV && $contest_id
+       or die "Usage: $0 -c <contest-id> [-i]\n";
 
-# Parses login.tex
-binmode STDIN, ':utf8';
 binmode STDOUT, ':utf8';
-while (<STDIN>) {
+
+# Parse /etc/hosts
+open my $hosts, '<', '/etc/hosts' or die;
+my %host_to_ip = ();
+while (<$hosts>) {
+       chomp;
+       /^$/ and next;
+       /^#/ and next;
+       my @a = split /\s+/;
+       my $ip = shift @a or next;
+       $host_to_ip{$_} = $ip for @a;
+}
+close $hosts;
+
+# use Data::Dumper;
+# print Dumper(\%host_to_ip);
+
+# Parse user list
+open my $userlist, '-|:utf8', 'bin/mo-get-users --raw' or die;
+my %fullname = ();
+my %machine = ();
+while (<$userlist>) {
+       chomp;
+       my ($user, $full, $mach) = split /\t+/;
+       $fullname{$user} = $full;
+       if ($mach) {
+               if ($host_to_ip{$mach}) {
+                       $machine{$user} = $host_to_ip{$mach};
+               } else {
+                       print STDERR "Warning: Unknown machine $mach\n";
+               }
+       }
+}
+close $userlist;
+
+# Parse login.tex
+open my $logins, '<:utf8', 'logins.tex' or die;
+while (<$logins>) {
        chomp;
        s[^\\user\{][] or next;
        s[}$][] or die;
-       my ($login, $passwd, $full) = split /}{/;
-       my ($fn, $ln) = split /\s+/, $full;
-       print "cmsAddUser -c 1 -p '$passwd' '$fn' '$ln' '$login'\n";
+       my ($login, $passwd) = split /}{/;
+       my ($fn, $ln) = split /\s+/, $fullname{$login};
+       my $ip = $machine{$login};
+       print "cmsAddUser -c $contest_id -p '$passwd'",
+               ($bind_to_ip && $ip ? " -i $ip" : ""),
+               " '$fn' '$ln' '$login'\n";
 }
+close $logins;
index 172e5223589acc642a28b3f320dbed24cb5fd861..1a3f54721ff227394a3b57397044d1be42583efe 100644 (file)
@@ -2,13 +2,15 @@
 # List all contestants according to $CT_USER_LIST
 
 if [ "$1" = --help ] ; then
-       echo "Usage: mo-get-users [--full]"
+       echo "Usage: mo-get-users [--full | --raw]"
 fi
 [ -f cf/mop ] || { echo "Missing config file, check cwd." ; exit 1 ; }
 set -e
 . cf/mop
 
-if [ "$1" = --full ] ; then
+if [ "$1" = --raw ] ; then
+       cat $CT_USER_LIST
+elif [ "$1" = --full ] ; then
        cut -d '        ' -f 1,2 <$CT_USER_LIST
 else
        cut -d '        ' -f 1 <$CT_USER_LIST