From b10879398daef21e02a107c0faf1e2c25d76c9fd Mon Sep 17 00:00:00 2001 From: hmalti Date: Sat, 20 Apr 2019 13:29:08 +0200 Subject: [PATCH] Ajout commandes --- baleine.sh | 13 +++++++------ container.sh | 6 ++++++ create_bridge.sh | 14 +++++++------- create_container.sh | 11 ++++++++++- create_image.sh | 38 ++++++++++++++++++++------------------ list_bridges.sh | 2 +- list_container.sh | 2 +- list_images.sh | 2 +- remove_bridge.sh | 8 ++++---- remove_container.sh | 13 +++++++++++++ remove_image.sh | 7 +++---- restart_container.sh | 10 ++++++++++ stop_container.sh | 5 +++++ test.sh | 4 ++++ 14 files changed, 92 insertions(+), 43 deletions(-) create mode 100644 remove_container.sh create mode 100644 restart_container.sh create mode 100644 stop_container.sh create mode 100644 test.sh diff --git a/baleine.sh b/baleine.sh index ae7c639..3ac5f62 100755 --- a/baleine.sh +++ b/baleine.sh @@ -7,8 +7,9 @@ #script qui va traiter ces cas, on garde les fichiers courts mais simples -export PATH_BALEINE=/var/lib/baleine -export PATH_MANIFEST=$PATH_BALEINE/manifest +export PATH_BALEINE="/var/lib/baleine" +export PATH_MANIFEST="$PATH_BALEINE/manifest" + if [ $# -lt 1 ]; then printf "Baleine est un outil simple de gestion de conteneur.\n" @@ -16,7 +17,7 @@ if [ $# -lt 1 ]; then printf "\t \t baleine [arguments]\n" printf "Les commandes sont :\n \n" printf "\t \t %-10s %-10s \n" "container" "Lance ou stoppe un contenneur." - printf "\t \t %-10s %-10s \n" "network" "Créée ou supprime des interfaces réseau." + printf "\t \t %-10s %-10s \n" "bridge" "Créée ou supprime des commutateurs linux virtuels." printf "\t \t %-10s %-10s \n \n" "image" "Créer ou supprime des images de contenneurs." printf "Utilisez baleine help pour plus d'informations à propos d'une commande.\n" fi @@ -28,9 +29,6 @@ case "$1" in #autrement dit, ./baleine.sh container create apellera le script container.sh avec les arguments $1=container et $2=create bash container.sh "$@" ;; - "network") - bash network.sh "$@" - ;; "image") bash image.sh "$@" ;; @@ -40,4 +38,7 @@ case "$1" in "help") bash help.sh "$@" ;; + "test") + bash test.sh "$@" + ;; esac \ No newline at end of file diff --git a/container.sh b/container.sh index 2857eca..d402bc0 100644 --- a/container.sh +++ b/container.sh @@ -13,5 +13,11 @@ case $2 in "stop") bash stop_container.sh "$@" ;; + "remove") + bash remove_container.sh "$@" + ;; + "restart") + bash restart_container.sh "$@" + ;; esac \ No newline at end of file diff --git a/create_bridge.sh b/create_bridge.sh index 048ad5d..dfa29d8 100644 --- a/create_bridge.sh +++ b/create_bridge.sh @@ -1,7 +1,7 @@ #!/bin/bash NOM_BRIDGE=$3; ADDR_IPV4=$4; -REPERTOIRE=${PWD} + #condition sur le nbre d'arguments @@ -14,12 +14,12 @@ ip link add $NOM_BRIDGE type bridge ip a add dev $NOM_BRIDGE $ADDR_IPV4 ip link set $NOM_BRIDGE down ip link set $NOM_BRIDGE up - -touch $NOM_BRIDGE.manifest # On crée un fichier contenant les infos du bridge -echo "$NOM_BRIDGE" >> $NOM_BRIDGE.manifest #nom du bridge +FILE= $NOM_BRIDGE.manifest +touch FILE # On crée un fichier contenant les infos du bridge +echo "$NOM_BRIDGE" >> FILE #nom du bridge #condition de bordure pour le dossier Bridges -if [[ ! -d "$REPERTOIRE/baleine/Bridges" ]]; then - mkdir -p $REPERTOIRE/baleine/Bridges && mv $NOM_BRIDGE.manifest $REPERTOIRE/baleine/Bridges +if [[ ! -d "$PATH_MANIFEST/bridges" ]]; then + mkdir -p $PATH_MANIFEST/bridges #&& mv FILE $PATH_MANIFEST/bridges fi -mv $NOM_BRIDGE.manifest $REPERTOIRE/baleine/Bridges \ No newline at end of file +mv FILE $PATH_MANIFEST/bridges \ No newline at end of file diff --git a/create_container.sh b/create_container.sh index 44b7dd6..4ef9e79 100644 --- a/create_container.sh +++ b/create_container.sh @@ -13,7 +13,7 @@ PROGRAM=$7; #On veut récupérer le chemin de l'image -PATH_IMAGE= grep chemin $PATH_MANIFEST/$NOM_IMAGE.manifest | cut -d':' -f2 +PATH_IMAGE= grep chemin $PATH_MANIFEST/images/$NOM_IMAGE.manifest | cut -d':' -f2 #on fait une copie dans /var/BaleineImages pour mount depuis le conteneur if [[ ! -d "$PATH_BALEINE/containers/$NOM_CONTAINER" ]]; then @@ -48,6 +48,15 @@ echo "nom_image:$NOM_IMAGE" >> $FILE #nom de son image echo "pid:$PID" >> $FILE #Son PID echo "nom_bridge:$NOM_BRIDGE" >> $FILE #SON BRIDGE +#temps d'exécution du container ? +# start_time=`date +%s` +# +# end_time=`date +%s` +# echo execution time was `expr $end_time - $start_time` s. +# ou +#start_time=`date +%s` +# && echo run time is $(expr `date +%s` - $start_time) s + if [[ ! -d "$PATH_MANIFEST/containers" ]]; then mkdir -p $PATH_MANIFEST/containers fi diff --git a/create_image.sh b/create_image.sh index 31fe492..93b6fdf 100644 --- a/create_image.sh +++ b/create_image.sh @@ -1,5 +1,4 @@ #!/bin/bash -#TOUT MARCHE BIEN ICI NOM_IMAGE=$3; # L'utilisateur devra spécifier le nom de l'image SIZE=$4; #L'utilisateur devra spécifier la taille du disque qu'il souhaite allouer @@ -15,12 +14,12 @@ if [ "$SIZE" -lt 0 ] || [ "$SIZE" -gt 10240 ] || [ -z $SIZE ]; then #vérifie qu $SIZE=10240 fi -mkdir -p /usr/lib/baleine/images +mkdir -p $PATH_BALEINE/images #condition pour le répertoire if [[ -z $REPERTOIRE ]]; then #si l'utilisateur oublie de donner le path on utilise celui-ci par défaut echo "Il n'y pas eu d'argements donnés pour répertoire, je fais moi même le chemin" - REPERTOIRE=/var/lib/baleine/images + REPERTOIRE=$PATH_BALEINE/images fi #vérification que l'utilisateur donne bien un nom à l'image @@ -28,29 +27,32 @@ if [[ -z "$NOM_IMAGE" ]]; then echo "Il faut donner le nom de l'image ! Relancez la commande avec les bons arguments." exit fi -#génération de nombres aléatoires afin de créer à chaque fois un fichier avec un nombre unique (éviter qu'un soit écraser) -NUMBER=$(cat /dev/urandom | tr -dc '0-9' | fold -w 256 | head -n 1 | sed -e 's/^0*//' | head --bytes 3) -if [ "$NUMBER" == "" ]; then - NUMBER=0 -fi -echo $REPERTOIRE +# #génération de nombres aléatoires afin de créer à chaque fois un fichier avec un nombre unique (éviter qu'un soit écraser) +# NUMBER=$(cat /dev/urandom | tr -dc '0-9' | fold -w 256 | head -n 1 | sed -e 's/^0*//' | head --bytes 3) +# if [ "$NUMBER" == "" ]; then +# NUMBER=0 +# fi +echo "Le repertoire est $REPERTOIRE" #Crée image +FILE=$NOM_IMAGE.manifest echo "Je vais commencer à allouer !" dd if=/dev/zero of=$REPERTOIRE/$NOM_IMAGE bs=1024k count=$SIZE -touch $NOM_IMAGE.$NUMBER.manifest -echo "$NOM_IMAGE" >> $NOM_IMAGE.$NUMBER.manifest #nom de l'image -echo "$SIZE">> $NOM_IMAGE.$NUMBER.manifest #taille de l'image -echo "$REPERTOIRE/$NOM_IMAGE">> $NOM_IMAGE.$NUMBER.manifest #son chemin +touch FILE +echo "nom image:$NOM_IMAGE" >> FILE #nom de l'image +echo "taille:$SIZE">> FILE #taille de l'image +echo "chemin:$REPERTOIRE">> FILE #son chemin + + -if [[ ! -d "$REPERTOIRE/baleine/Images" ]]; then - mkdir -p $REPERTOIRE/baleine/Images && mv $NOM_IMAGE.$NUMBER.manifest $REPERTOIRE/baleine/Images +if [[ ! -d "$PATH_MANIFEST/images" ]]; then + mkdir -p $PATH_MANIFEST/images #&& mv $NOM_IMAGE.$NUMBER.manifest $REPERTOIRE/images fi -mv $NOM_IMAGE.$NUMBER.manifest $REPERTOIRE/baleine/Images +mv $NOM_IMAGE.manifest $PATH_MANIFEST/images #création du système de fichiers au format "ext4" -mkfs.ext4 $REPERTOIRE/$NOM_IMAGE +mkfs.ext4 $REPERTOIRE/images/$NOM_IMAGE @@ -59,7 +61,7 @@ export http_proxy=http://proxy.polytech-lille.fr:3128 #Création de l'arborescence Debian avec debootstrap echo "Je vais faire le debootstrap" -debootstrap --include=apache2,vim,nano stable /mnt/$NOM_IMAGE +debootstrap --include=apache2,vim,nano stable /mnt/baleine/$NOM_IMAGE #On renseigne le fichier fstab du conteneur echo "proc /proc proc defaults 0 0" >> /mnt/$NOM_IMAGE/etc/fstab \ No newline at end of file diff --git a/list_bridges.sh b/list_bridges.sh index 7dc5db8..c4a2ca4 100644 --- a/list_bridges.sh +++ b/list_bridges.sh @@ -1,5 +1,5 @@ #!/bin/bash -cd $PWD/baleine/Bridges +cd $PATH_MANIFEST/bridges for eachfile in ./*.manifest do diff --git a/list_container.sh b/list_container.sh index ebb43f0..a41c15a 100644 --- a/list_container.sh +++ b/list_container.sh @@ -1,6 +1,6 @@ #!/bin/bash -cd $PWD/baleine/Containers +cd $PATH_MANIFEST/containers for eachfile in ./*.manifest do diff --git a/list_images.sh b/list_images.sh index 132e45b..a5c51cc 100644 --- a/list_images.sh +++ b/list_images.sh @@ -1,6 +1,6 @@ #!/bin/bash -cd $PWD/baleine/Images +cd $PATH_MANIFEST/images for eachfile in ./*.manifest do diff --git a/remove_bridge.sh b/remove_bridge.sh index 460fda4..6dc3aac 100644 --- a/remove_bridge.sh +++ b/remove_bridge.sh @@ -1,13 +1,13 @@ #!/bin/bash NOM_BRIDGE_TO_REMOVE=$3; -REPERTOIRE=${PWD} + #vérification que l'utilisateur donne bien un nom à l'image if [[ -z "$NOM_BRIDGE_TO_REMOVE" ]]; then echo "Il faut donner le nom du bridge à supprimer ! Relancez la commande avec les bons arguments." exit fi -chmod -R 755 $REPERTOIRE/baleine -rm $REPERTOIRE/baleine/Bridges/$NOM_BRIDGE_TO_REMOVE.* +chmod -R 755 $PATH_MANIFEST +rm $PATH_MANIFEST/bridges/$NOM_BRIDGE_TO_REMOVE.* ip link delete $NOM_BRIDGE_TO_REMOVE type bridge -delbr +#delbr diff --git a/remove_container.sh b/remove_container.sh new file mode 100644 index 0000000..17afe88 --- /dev/null +++ b/remove_container.sh @@ -0,0 +1,13 @@ +#!/bin/bash +NOM_CONTAINER_TO_REMOVE=$3; + +#vérification que l'utilisateur donne bien un nom à l'CONTAINER +if [[ -z "$NOM_CONTAINER_TO_REMOVE" ]]; then + echo "Il faut donner le nom du conteneur! Relancez la commande avec les bons arguments." + exit +fi +chmod -R 755 $PATH_MANIFEST +rm $PATH_MANIFEST/containers/$NOM_CONTAINER_TO_REMOVE.manifest +rm $PATH_BALEINE/containers/$NOM_CONTAINER_TO_REMOVE + +#il doit remove aussi l'image, les manifessts les bridges supprime la copie? diff --git a/remove_image.sh b/remove_image.sh index eaad778..af2b1e3 100644 --- a/remove_image.sh +++ b/remove_image.sh @@ -1,12 +1,11 @@ #!/bin/bash NOM_IMAGE_TO_REMOVE=$3; -REPERTOIRE=${PWD} #vérification que l'utilisateur donne bien un nom à l'image if [[ -z "$NOM_IMAGE_TO_REMOVE" ]]; then echo "Il faut donner le nom de l'image ! Relancez la commande avec les bons arguments." exit fi -chmod -R 755 $REPERTOIRE/baleine -rm $REPERTOIRE/baleine/Images/$NOM_IMAGE_TO_REMOVE.manifest -rm $REPERTOIRE/$NOM_IMAGE_TO_REMOVE \ No newline at end of file +chmod -R 755 $PATH_MANIFEST +rm $PATH_MANIFEST/images/$NOM_IMAGE_TO_REMOVE.manifest +rm $PATH_BALEINE/images/$NOM_IMAGE_TO_REMOVE \ No newline at end of file diff --git a/restart_container.sh b/restart_container.sh new file mode 100644 index 0000000..e3b8260 --- /dev/null +++ b/restart_container.sh @@ -0,0 +1,10 @@ +#!/bin/bash +NOM_CONTAINER=$3; +NOM_IMAGE=$4; + +mount -t ext4 -o loop $PATH_BALEINE/containers/$NOM_CONTAINER/$NOM_IMAGE /mnt/baleine/$NOM_CONTAINER + +nohup unshare -p -f -m -n -u chroot /mnt/baleine/$NOM_CONTAINER $PROGRAM -c "mount /proc" & +PID=$! + +ps axo ppid,pid | grep "^ *$PID" | sed -e 's/.* //' diff --git a/stop_container.sh b/stop_container.sh new file mode 100644 index 0000000..5786865 --- /dev/null +++ b/stop_container.sh @@ -0,0 +1,5 @@ +#!/bin/bash +NOM_CONTAINER=$3; + +kill $PID +umount /mnt/baleine/$NOM_CONTAINER \ No newline at end of file diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..5fa8488 --- /dev/null +++ b/test.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +source baleine.sh +echo "$PATH_MANIFEST" \ No newline at end of file -- libgit2 0.21.2