fill_team: improve script reliability

This commit is contained in:
nemunaire 2016-11-19 16:14:35 +01:00
parent b41180c7b0
commit 5592b2e676

View File

@ -1,7 +1,48 @@
#!/bin/sh #!/bin/bash
BASEURL="http://localhost:8081" BASEURL="http://127.0.0.1:8081/admin"
PART_FILE="Challenge_Liste des participants.csv" GEN_CERTS=0
EXTRA_TEAMS=0
CSV_SPLITER=","
CSV_COL_LASTNAME=1
CSV_COL_FIRSTNAME=2
CSV_COL_NICKNAME=3
CSV_COL_COMPANY=7
CSV_COL_TEAM=7
usage() {
echo "$0 [options] csv_file"
echo " -B -baseurl BASEURL URL to administration endpoint (default: $BASEURL)"
echo " -S -csv-spliter SEP CSV separator (default: $CSV_SPLITER)"
echo " -e -extra-teams NBS Number of extra teams to generate (default: ${EXTRA_TEAMS})"
echo " -c -generate-certificate Should team certificates be generated? (default: no)"
}
# Parse options
while [ "${1:0:1}" = "-" ]
do
case "$1" in
-B|-baseurl)
BASEURL=$2
shift;;
-S|-csv-spliter)
CSV_SPLITER=$2
shift;;
-e|-extra-teams)
EXTRA_TEAMS=$2
shift;;
-c|-generate-certificates)
GEN_CERTS=1;;
*)
echo "Unknown option '$1'"
usage
exit 1;;
esac
shift
done
[ "$#" -lt 1 ] && { usage; exit 1; }
PART_FILE="$1"
new_team() { new_team() {
head -n "$1" team-names.txt | tail -1 | sed -E 's/^.*\|\[\[([^|]+\|)?([^|]+)\]\][^|]*\|([A-Fa-f0-9]{1,2})\|([A-Fa-f0-9]{1,2})\|([A-Fa-f0-9]{1,2})\|([0-9]{1,3})\|([0-9]{1,3})\|([0-9]{1,3})\|.*$/\6 \7 \8 \2/' | head -n "$1" team-names.txt | tail -1 | sed -E 's/^.*\|\[\[([^|]+\|)?([^|]+)\]\][^|]*\|([A-Fa-f0-9]{1,2})\|([A-Fa-f0-9]{1,2})\|([A-Fa-f0-9]{1,2})\|([0-9]{1,3})\|([0-9]{1,3})\|([0-9]{1,3})\|.*$/\6 \7 \8 \2/' |
@ -10,7 +51,11 @@ new_team() {
R=`echo $line | cut -d " " -f 1` R=`echo $line | cut -d " " -f 1`
G=`echo $line | cut -d " " -f 2` G=`echo $line | cut -d " " -f 2`
B=`echo $line | cut -d " " -f 3` B=`echo $line | cut -d " " -f 3`
if [ -z "$2" ]; then
N=`echo $line | cut -d " " -f 4` N=`echo $line | cut -d " " -f 4`
else
N=`echo -n $2 | tr -d '\r\n'`
fi
COLOR=$((($R*256 + $G) * 256 + $B)) COLOR=$((($R*256 + $G) * 256 + $B))
@ -20,7 +65,7 @@ new_team() {
TNUM=0 TNUM=0
for i in `seq 12` for i in $(seq $EXTRA_TEAMS)
do do
TNUM=$(($TNUM + 1)) TNUM=$(($TNUM + 1))
@ -28,31 +73,32 @@ do
TID=`new_team $TNUM` TID=`new_team $TNUM`
if ! curl -s -f "${BASEURL}/api/teams/${TID}/certificate" > /dev/null if [ "${GEN_CERTS}" -eq 1 ] && ! curl -s -f "${BASEURL}/api/teams/${TID}/certificate" > /dev/null
then then
curl -s -f "${BASEURL}/api/teams/${TID}/certificate/generate" curl -s -f "${BASEURL}/api/teams/${TID}/certificate/generate"
fi fi
echo echo
done done
TMAX=`sed "1d" "$PART_FILE" | cut -d \; -f 15 | sort | uniq | wc -l` TMAX=`cat "$PART_FILE" | cut -d "${CSV_SPLITER}" -f $CSV_COL_TEAM | sort | uniq | wc -l`
TMAX=$(($TMAX + $TNUM)) TMAX=$(($TMAX + $TNUM))
sed "1d" "$PART_FILE" | cut -d \; -f 15 | sort | uniq | while read TEAMID cat "$PART_FILE" | cut -d "${CSV_SPLITER}" -f $CSV_COL_TEAM | sort | uniq | while read TEAMID
do do
TNUM=$(($TNUM + 1)) TNUM=$(($TNUM + 1))
echo "Doing team $TNUM/$TMAX ("$(($TNUM*100/$TMAX))"%)..." echo "Doing team $TNUM/$TMAX ("$(($TNUM*100/$TMAX))"%)..."
TID=`new_team $TNUM` TID=`new_team "${TNUM}" "${TEAMID}"`
( if ! (
echo -n "[" echo -n "["
HAS_MEMBER=1 HAS_MEMBER=1
grep ";$TEAMID\$" "$PART_FILE" | while read MEMBER grep "${CSV_SPLITER}${TEAMID}\$" "$PART_FILE" | while read MEMBER
do do
LASTNAME=`echo $MEMBER | cut -d ";" -f 2` LASTNAME=`echo $MEMBER | cut -d "${CSV_SPLITER}" -f $CSV_COL_LASTNAME | tr -d "\r\n"`
FIRSTNAME=`echo $MEMBER | cut -d ";" -f 3` FIRSTNAME=`echo $MEMBER | cut -d "${CSV_SPLITER}" -f $CSV_COL_FIRSTNAME | tr -d "\r\n"`
COMPANY=`echo $MEMBER | cut -d ";" -f 4` NICKNAME=`echo $MEMBER | cut -d "${CSV_SPLITER}" -f $CSV_COL_NICKNAME | tr -d "\r\n"`
COMPANY=`echo $MEMBER | cut -d "${CSV_SPLITER}" -f $CSV_COL_COMPANY | tr -d "\r\n"`
if [ $HAS_MEMBER = 0 ] if [ $HAS_MEMBER = 0 ]
then then
@ -65,15 +111,16 @@ do
{ {
"firstname": "$FIRSTNAME", "firstname": "$FIRSTNAME",
"lastname": "$LASTNAME", "lastname": "$LASTNAME",
"nickname": "", "nickname": "$NICKNAME",
"company": "$COMPANY" "company": "$COMPANY"
} }
EOF EOF
done done
echo "]" echo "]"
) | curl -s -d @- "${BASEURL}/api/teams/${TID}" > /dev/null ) | curl -f -s -d @- "${BASEURL}/api/teams/${TID}"
then
if ! curl -s -f "${BASEURL}/api/teams/${TID}/certificate" > /dev/null echo "An error occured"
elif [ "${GEN_CERTS}" -eq 1 ] && ! curl -s -f "${BASEURL}/api/teams/${TID}/certificate" > /dev/null
then then
curl -s -f "${BASEURL}/api/teams/${TID}/certificate/generate" curl -s -f "${BASEURL}/api/teams/${TID}/certificate/generate"
fi fi