From 1f5455b88946008c8227f73db6a02077f0007792 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 29 Jun 2015 19:46:39 +0200 Subject: [PATCH] MO-P: mo-cms-users knows how to tie users to machines --- mop/admin/mo-cms-users.pl | 62 ++++++++++++++++++++++++++++++++++----- mop/admin/mo-get-users.sh | 6 ++-- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/mop/admin/mo-cms-users.pl b/mop/admin/mo-cms-users.pl index 3dadea1..b6558b1 100644 --- a/mop/admin/mo-cms-users.pl +++ b/mop/admin/mo-cms-users.pl @@ -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 [-i]\n"; -# Parses login.tex -binmode STDIN, ':utf8'; binmode STDOUT, ':utf8'; -while () { + +# 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; diff --git a/mop/admin/mo-get-users.sh b/mop/admin/mo-get-users.sh index 172e522..1a3f547 100644 --- a/mop/admin/mo-get-users.sh +++ b/mop/admin/mo-get-users.sh @@ -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 -- 2.39.2