diff --git a/Dockerfile b/Dockerfile index 5fb8d51..31f9249 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,7 @@ RUN apt-get update && apt-get install -y \ nano \ netcat \ nginx \ + pandoc \ ssh \ vim \ && rm -rf /var/lib/apt/lists/* @@ -19,10 +20,13 @@ ENV TZ=Europe/Paris # SSH server RUN mkdir /run/sshd -CMD ["netcat", "-lp", "5000", "-e", "/root/http/http_bash.sh"] +CMD ["sh", "-c", "while true ; do netcat -lp 5000 -e /root/http_bash.sh ; done"] # Nginx conf -COPY ./default /etc/nginx/sites-enabled/default +COPY ./nginx-server.conf /etc/nginx/sites-enabled/default + +# Bash HTTP website +COPY ./http_bash.sh ./http_bash.sh # Entrypoint COPY ./entrypoint.sh ./entrypoint.sh diff --git a/docker-compose.yml b/docker-compose.yml index ed9292b..eeb273b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,6 @@ services: - ./config:/root/config - ./home_eleves:/home - ./exercices:/root/exercices - - ./http:/root/http + - ./exercices/html:/var/www/html network_mode: "host" restart: "unless-stopped" diff --git a/exercices/gen_enonces.sh b/exercices/gen_enonces.sh index 2bc1ac6..03e6bcc 100755 --- a/exercices/gen_enonces.sh +++ b/exercices/gen_enonces.sh @@ -209,4 +209,4 @@ for i in {1..8} ; do done -pandoc --from=markdown --to=html --output=$html/$eleve.html --table-of-contents --columns=80 $enonces/ex*.txt +pandoc --standalone --from=markdown --to=html --output=$html/$eleve.html --table-of-contents --columns=80 $enonces/ex*.txt diff --git a/exercices/gen_seed.sh b/exercices/gen_seed.sh index a1d80ee..1070f5c 100755 --- a/exercices/gen_seed.sh +++ b/exercices/gen_seed.sh @@ -34,15 +34,25 @@ if echo $DIR | grep -v /$ > /dev/null ; then DIR=$DIR/ ; fi mkdir -p $DIR$USER ; -find $root -type d | shuf | head -n $folders > $DIR$USER/folders.seed +if [[ ! -s $DIR$USER/folders.seed ]] ; then + find $root -type d | shuf | head -n $folders > $DIR$USER/folders.seed + echo folders +fi -find $root -type f | shuf | head -n $files > $DIR$USER/files.seed +if [[ ! -s $DIR$USER/files.seed ]] ; then + find $root -type f | shuf | head -n $files > $DIR$USER/files.seed + echo files +fi -cat haddock.list | shuf | head -n $filenames > $DIR$USER/filenames.seed +if [[ ! -s $DIR$USER/filenames.seed ]] ; then + cat haddock.list | shuf | head -n $filenames > $DIR$USER/filenames.seed + echo names +fi -if [[ -s $DIR$USER/ints.seed ]] ; then rm $DIR$USER/ints.seed ; fi - -for i in $(seq 1 $ints) ; do - echo $((3 + $RANDOM % 13)) >> $DIR$USER/ints.seed -done +if [[ ! -s $DIR$USER/ints.seed ]] ; then + for i in $(seq 1 $ints) ; do + echo $((3 + $RANDOM % 13)) >> $DIR$USER/ints.seed + done + echo ints +fi diff --git a/exercices/init.sh b/exercices/init.sh index 317070f..ecd7376 100755 --- a/exercices/init.sh +++ b/exercices/init.sh @@ -1,14 +1,23 @@ #!/bin/bash -if [ "$#" -ne 1 ] ; then - echo -e "usage : $0 \n\tGenerate the exercice for a class.\nFILE must be a list containing identifiers (names) and passwords for each student, separated by a '='.\n\tEx:\n\t\tJean-claude-dus=mot_de_passe\n\t\tJean-dus-claude=passe_de_mot\n\t\tClaude-jean-dus=azerty\n\nThis file must be executed from the same folder as the files gen_arbo.sh, gen_seed.sh and gen_enonces.sh.\nFolders will be created in this directory, containing the exercices, their correction and the grades of the students." +if [ "$#" -ne 1 && "$#" -ne 2 ] ; then + echo -e "usage : $0 [USER]\n Generate the exercice for a class.\nFILE must be a list containing identifiers (names) and passwords for each student, separated by a '='.\n Ex:\n Jean-claude-dus=mot_de_passe\n Jean-dus-claude=passe_de_mot\n Claude-jean-dus=azerty\nIf a USER is provided, init will only be run for that user (useful to add a student mid-class, reset a session when something went horribly wrong, or if a student finished early and wants to start over.\n\nThis file must be executed from the same folder as the files gen_arbo.sh, gen_seed.sh and gen_enonces.sh.\nFolders will be created in this directory, containing the exercices, their correction and the grades of the students." exit 1; fi -cat $1 - home=/home + +#Little debug option for one of the devs. If we forgot to remove it before deployment, feel free to erase this line. +if [ $USER = karsaell ] ; then home=root ; fi + names=$(cut -d '=' -f 1 $1) +if [ "$#" -eq 2 ] ; then + names=$(grep $2 $1 | cut -d '=' -f 1) + if [ -z $names ] ; then + echo "Error : no user named $2 found in $1" + exit 1 + fi +fi for name in $names ; do ./gen_arbo.sh $home/$name $name diff --git a/http/http_bash.sh b/http_bash.sh similarity index 78% rename from http/http_bash.sh rename to http_bash.sh index bdb4b85..e8974d2 100755 --- a/http/http_bash.sh +++ b/http_bash.sh @@ -58,9 +58,23 @@ serve_static_string() { send_response_ok_exit <<< "$1" } +where () { + h="$(dirname "$0")" + # If $h isnt absolute, prepend working dir + if [[ ! "${h:0:1}" = "/" ]] ; then + h="$(pwd)/$(dirname "$0")" + fi + h="$(realpath "$h")" + echo "$h" +} + main () { + # Request uri must be a relative path from the exercices/corriges directory [ -x "$REQUEST_URI" ] || [ ! -d "$REQUEST_URI" ] || fail_with 404 - serve_static_string "$(basename "$REQUEST_URI" | cut -d '.' -f 1) : $(sh "$REQUEST_URI" 2>&1 && echo Ok || echo 'Non validé')" + here="$(where)" + path="$(realpath "$here/exercices/corriges/$REQUEST_URI")" + [[ "$path" = "$here"* ]] || fail_with 404 + serve_static_string "$(basename "$path" | cut -d '.' -f 1) : $(bash "$path" 2>&1 && echo Ok || echo 'Non validé')" } # Request-Line HTTP RFC 2616 $5.1 diff --git a/default b/nginx-server.conf similarity index 78% rename from default rename to nginx-server.conf index 80b5bdf..84732c5 100644 --- a/default +++ b/nginx-server.conf @@ -2,7 +2,7 @@ server { listen 80 default_server; listen [::]:80 default_server; - root /root/exercices/html; + root /var/www/html; index index.html; @@ -12,10 +12,11 @@ server { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; + add_header Content-Type 'text/html;'; autoindex on; } - location ~ /.sh$ { + location ~ \.sh$ { proxy_pass http://127.0.0.1:5000; } diff --git a/readme.md b/readme.md index 9c898b3..0c383fe 100644 --- a/readme.md +++ b/readme.md @@ -18,7 +18,7 @@ Chaque élève a un dossier perso dans `/home` dans lequel il peut se connecter ## Génération de l’activité ``` cd exercices -./init.sh +./init.sh ../config/passwords.txt ``` ## Énoncés des exercices @@ -29,7 +29,7 @@ Si un·e élève a saccagé ses fichiers, il est possible de les réparer en lan $username est son nom d’utilisateurice. ``` cd exercices -./init.sh $username +./init.sh ../config/passwords.txt $username ``` ## Ajouter des élèves en cours de route