From b7610cc24a1eafffd2e76835289e22e16c4daa2a Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 9 Oct 2022 05:31:57 +0200 Subject: [PATCH 01/25] First implementation --- README.MD | 4 - demo_lxc_build_init.sh | 188 ----------------------------------------- 2 files changed, 192 deletions(-) delete mode 100755 demo_lxc_build_init.sh diff --git a/README.MD b/README.MD index 8a78446..7509b5e 100644 --- a/README.MD +++ b/README.MD @@ -4,10 +4,6 @@ * `apt install sudo git nginx -y` * register a dns name to your demo server and make you demo server available from internet for TCP/80 and TCP/443 -## demo_lxc_build_init -Ce script prépare le serveur hôte à recevoir les conteneurs LXC de demo. -Il doit être exécuté une seule fois et en premier. - ## demo_lxc_build Ce script construit les conteneurs de demo et les paramètres. Il met également en places les crons et démarre le 1er conteneur. diff --git a/demo_lxc_build_init.sh b/demo_lxc_build_init.sh deleted file mode 100755 index 9de69ab..0000000 --- a/demo_lxc_build_init.sh +++ /dev/null @@ -1,188 +0,0 @@ -#!/bin/bash - -# Installe LXC et les paramètres réseaux avant de procéder au build. - -# Récupère le dossier du script -if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi - -LOG=$(cat "$script_dir/demo_lxc_build.sh" | grep LOG= | cut -d '=' -f2) -LOG_BUILD_LXC="$script_dir/$LOG" -LXC_NAME1=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME1= | cut -d '=' -f2) -LXC_NAME2=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME2= | cut -d '=' -f2) -PLAGE_IP=$(cat "$script_dir/demo_lxc_build.sh" | grep PLAGE_IP= | cut -d '=' -f2) -IP_LXC1=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC1= | cut -d '=' -f2) -IP_LXC2=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC2= | cut -d '=' -f2) -MAIL_ADDR=$(cat "$script_dir/demo_lxc_build.sh" | grep MAIL_ADDR= | cut -d '=' -f2) - -# Check user -echo $(whoami) > "$script_dir/setup_user" - -read -p "Indiquer le nom de domaine du serveur de demo: " DOMAIN -echo "$DOMAIN" > "$script_dir/domain.ini" - -# Créer le dossier de log -sudo mkdir -p $(dirname $LOG_BUILD_LXC) - -echo -e "\e[1m> Update et install lxc, lxctl et mailutils\e[0m" | tee "$LOG_BUILD_LXC" -sudo apt-get update >> "$LOG_BUILD_LXC" 2>&1 -sudo apt-get install -y lxc lxctl mailutils certbot >> "$LOG_BUILD_LXC" 2>&1 - -echo -e "\e[1m> Autoriser l'ip forwarding, pour router vers la machine virtuelle.\e[0m" | tee -a "$LOG_BUILD_LXC" -echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/lxc_demo.conf >> "$LOG_BUILD_LXC" 2>&1 -sudo sysctl -p /etc/sysctl.d/lxc_demo.conf >> "$LOG_BUILD_LXC" 2>&1 - -echo -e "\e[1m> Ajoute un brige réseau pour la machine virtualisée\e[0m" | tee -a "$LOG_BUILD_LXC" -echo | sudo tee /etc/network/interfaces.d/lxc_demo <> "$LOG_BUILD_LXC" 2>&1 -auto lxc_demo -iface lxc_demo inet static - address $PLAGE_IP.1/24 - bridge_ports none - bridge_fd 0 - bridge_maxwait 0 -EOF - -echo -e "\e[1m> Active le bridge réseau\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo ifup lxc_demo --interfaces=/etc/network/interfaces.d/lxc_demo >> "$LOG_BUILD_LXC" 2>&1 - -echo -e "\e[1m> Mise en place de la connexion ssh vers l'invité.\e[0m" | tee -a "$LOG_BUILD_LXC" -if [ -e $HOME/.ssh/$LXC_NAME1 ]; then - rm -f $HOME/.ssh/$LXC_NAME1 $HOME/.ssh/$LXC_NAME1.pub - ssh-keygen -f $HOME/.ssh/known_hosts -R $IP_LXC1 - ssh-keygen -f $HOME/.ssh/known_hosts -R $IP_LXC2 -fi -ssh-keygen -t rsa -f $HOME/.ssh/$LXC_NAME1 -P '' >> "$LOG_BUILD_LXC" 2>&1 - -echo | tee -a $HOME/.ssh/config <> "$LOG_BUILD_LXC" 2>&1 -# ssh $LXC_NAME1 -Host $LXC_NAME1 -Hostname $IP_LXC1 -User ssh_demo -IdentityFile $HOME/.ssh/$LXC_NAME1 -Host $LXC_NAME2 -Hostname $IP_LXC2 -User ssh_demo -IdentityFile $HOME/.ssh/$LXC_NAME1 -# End ssh $LXC_NAME1 -EOF - -echo -e "\e[1m> Mise en place du reverse proxy et du load balancing\e[0m" | tee -a "$LOG_BUILD_LXC" -echo | sudo tee /etc/nginx/conf.d/$DOMAIN.conf <> "$LOG_BUILD_LXC" 2>&1 -#upstream $DOMAIN { -# server $IP_LXC1:443 ; -# server $IP_LXC2:443 ; -#} - -server { - listen 80; - listen [::]:80; - server_name $DOMAIN; - - location '/.well-known/acme-challenge' { - default_type "text/plain"; - root /tmp/letsencrypt-auto; - } - - access_log /var/log/nginx/$DOMAIN-access.log; - error_log /var/log/nginx/$DOMAIN-error.log; -} -EOF - -sudo service nginx reload - -echo -e "\e[1m> Création du certificat SSL.\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo mkdir -p /etc/letsencrypt - -# Créer le fichier de config -echo | sudo tee /etc/letsencrypt/conf.ini <> "$LOG_BUILD_LXC" 2>&1 -################################# -# Let's encrypt configuration # -################################# - -# Use a 4096 bit RSA key instead of 2048 -rsa-key-size = 4096 - -# Uncomment and update to register with the specified e-mail address -email = $MAIL_ADDR - -# Uncomment to use the webroot authenticator. Replace webroot-path with the -# path to the public_html / webroot folder being served by your web server. -# avec le contenu dans /tmp/letsencrypt-auto -authenticator = webroot -webroot-path = /tmp/letsencrypt-auto - -# Utiliser l'interface texte -text = True -# Uncomment to automatically agree to the terms of service of the ACME server -agree-tos = true - -# (Serveur de test uniquement : si vous l'utilisez, -# votre certificat ne sera pas vraiment valide) -# server = https://acme-staging-v02.api.letsencrypt.org/directory -EOF - -mkdir -p /tmp/letsencrypt-auto -# Créer le certificat -sudo certbot certonly --config /etc/letsencrypt/conf.ini -d $DOMAIN --no-eff-email - -# Route l'upstream sur le port 443. Le port 80 servait uniquement à let's encrypt -# sudo sed -i "s/server $IP_LXC1:80 ;/server $IP_LXC1:443 ;/" /etc/nginx/conf.d/$DOMAIN.conf -# Décommente les lignes du certificat -# sudo sed -i "s/#\tssl_certificate/\tssl_certificate/g" /etc/nginx/conf.d/$DOMAIN.conf -# Supprime les commentaires dans la conf nginx - -echo | sudo tee /etc/nginx/conf.d/$DOMAIN.conf <> "$LOG_BUILD_LXC" 2>&1 -#upstream $DOMAIN { -# server $IP_LXC1:443 ; -# server $IP_LXC2:443 ; -#} - -server { - listen 80; - listen [::]:80; - server_name $DOMAIN; - - location '/.well-known/acme-challenge' { - default_type "text/plain"; - root /tmp/letsencrypt-auto; - } - - access_log /var/log/nginx/$DOMAIN-access.log; - error_log /var/log/nginx/$DOMAIN-error.log; -} - -server { - listen 443 ssl; - listen [::]:443 ssl; - server_name $DOMAIN; - - ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem; - ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem; - ssl_session_timeout 5m; - ssl_session_cache shared:SSL:50m; - ssl_prefer_server_ciphers on; - ssl_protocols TLSv1 TLSv1.1 TLSv1.2; - ssl_ciphers ALL:!aNULL:!eNULL:!LOW:!EXP:!RC4:!3DES:+HIGH:+MEDIUM; - add_header Strict-Transport-Security "max-age=31536000;"; - - location / { - proxy_pass https://$DOMAIN; - proxy_redirect off; - proxy_set_header Host \$host; - proxy_set_header X-Real-IP \$remote_addr; - proxy_set_header X-Forwarded-Proto \$scheme; - proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Host \$server_name; - } - - access_log /var/log/nginx/$DOMAIN-access.log; - error_log /var/log/nginx/$DOMAIN-error.log; -} -EOF - -sudo service nginx reload - -echo -e "\e[1mLe serveur est prêt à déployer les conteneurs de demo.\e[0m" -echo -e "\e[1mExécutez le script demo_lxc_build.sh pour créer les conteneurs et mettre en place la demo.\e[0m" - -# Déploie les conteneurs de demo -# "$script_dir/demo_lxc_build.sh" From c944e13aabf5263cc1548f23aaeeca70c899679f Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 9 Oct 2022 17:28:08 +0200 Subject: [PATCH 02/25] typo --- demo_switch.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo_switch.sh b/demo_switch.sh index 3670609..f1a220c 100755 --- a/demo_switch.sh +++ b/demo_switch.sh @@ -57,5 +57,5 @@ else # Restaure le snapshot de la machine A avant sa prochaine exécution sudo lxc-snapshot -r snap0 -n $LXC_A sudo rm /var/lib/lxc/$LXC_A.lock_fileS # Libère le lock - echo "Finish restauring $LXC_A" + echo "Finish restoring $LXC_A" fi From 4d85576823f248ba26d5ad54cdee77a0113f9c59 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 9 Oct 2022 17:28:40 +0200 Subject: [PATCH 03/25] Revert "typo" This reverts commit c944e13aabf5263cc1548f23aaeeca70c899679f. --- demo_switch.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo_switch.sh b/demo_switch.sh index f1a220c..3670609 100755 --- a/demo_switch.sh +++ b/demo_switch.sh @@ -57,5 +57,5 @@ else # Restaure le snapshot de la machine A avant sa prochaine exécution sudo lxc-snapshot -r snap0 -n $LXC_A sudo rm /var/lib/lxc/$LXC_A.lock_fileS # Libère le lock - echo "Finish restoring $LXC_A" + echo "Finish restauring $LXC_A" fi From ba196567ff07a090c210715aea1046f1669e0be4 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Wed, 12 Oct 2022 00:00:00 +0200 Subject: [PATCH 04/25] Revert "First implementation" This reverts commit b7610cc24a1eafffd2e76835289e22e16c4daa2a. --- README.MD | 4 + demo_lxc_build_init.sh | 188 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100755 demo_lxc_build_init.sh diff --git a/README.MD b/README.MD index 7509b5e..8a78446 100644 --- a/README.MD +++ b/README.MD @@ -4,6 +4,10 @@ * `apt install sudo git nginx -y` * register a dns name to your demo server and make you demo server available from internet for TCP/80 and TCP/443 +## demo_lxc_build_init +Ce script prépare le serveur hôte à recevoir les conteneurs LXC de demo. +Il doit être exécuté une seule fois et en premier. + ## demo_lxc_build Ce script construit les conteneurs de demo et les paramètres. Il met également en places les crons et démarre le 1er conteneur. diff --git a/demo_lxc_build_init.sh b/demo_lxc_build_init.sh new file mode 100755 index 0000000..9de69ab --- /dev/null +++ b/demo_lxc_build_init.sh @@ -0,0 +1,188 @@ +#!/bin/bash + +# Installe LXC et les paramètres réseaux avant de procéder au build. + +# Récupère le dossier du script +if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi + +LOG=$(cat "$script_dir/demo_lxc_build.sh" | grep LOG= | cut -d '=' -f2) +LOG_BUILD_LXC="$script_dir/$LOG" +LXC_NAME1=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME1= | cut -d '=' -f2) +LXC_NAME2=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME2= | cut -d '=' -f2) +PLAGE_IP=$(cat "$script_dir/demo_lxc_build.sh" | grep PLAGE_IP= | cut -d '=' -f2) +IP_LXC1=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC1= | cut -d '=' -f2) +IP_LXC2=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC2= | cut -d '=' -f2) +MAIL_ADDR=$(cat "$script_dir/demo_lxc_build.sh" | grep MAIL_ADDR= | cut -d '=' -f2) + +# Check user +echo $(whoami) > "$script_dir/setup_user" + +read -p "Indiquer le nom de domaine du serveur de demo: " DOMAIN +echo "$DOMAIN" > "$script_dir/domain.ini" + +# Créer le dossier de log +sudo mkdir -p $(dirname $LOG_BUILD_LXC) + +echo -e "\e[1m> Update et install lxc, lxctl et mailutils\e[0m" | tee "$LOG_BUILD_LXC" +sudo apt-get update >> "$LOG_BUILD_LXC" 2>&1 +sudo apt-get install -y lxc lxctl mailutils certbot >> "$LOG_BUILD_LXC" 2>&1 + +echo -e "\e[1m> Autoriser l'ip forwarding, pour router vers la machine virtuelle.\e[0m" | tee -a "$LOG_BUILD_LXC" +echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/lxc_demo.conf >> "$LOG_BUILD_LXC" 2>&1 +sudo sysctl -p /etc/sysctl.d/lxc_demo.conf >> "$LOG_BUILD_LXC" 2>&1 + +echo -e "\e[1m> Ajoute un brige réseau pour la machine virtualisée\e[0m" | tee -a "$LOG_BUILD_LXC" +echo | sudo tee /etc/network/interfaces.d/lxc_demo <> "$LOG_BUILD_LXC" 2>&1 +auto lxc_demo +iface lxc_demo inet static + address $PLAGE_IP.1/24 + bridge_ports none + bridge_fd 0 + bridge_maxwait 0 +EOF + +echo -e "\e[1m> Active le bridge réseau\e[0m" | tee -a "$LOG_BUILD_LXC" +sudo ifup lxc_demo --interfaces=/etc/network/interfaces.d/lxc_demo >> "$LOG_BUILD_LXC" 2>&1 + +echo -e "\e[1m> Mise en place de la connexion ssh vers l'invité.\e[0m" | tee -a "$LOG_BUILD_LXC" +if [ -e $HOME/.ssh/$LXC_NAME1 ]; then + rm -f $HOME/.ssh/$LXC_NAME1 $HOME/.ssh/$LXC_NAME1.pub + ssh-keygen -f $HOME/.ssh/known_hosts -R $IP_LXC1 + ssh-keygen -f $HOME/.ssh/known_hosts -R $IP_LXC2 +fi +ssh-keygen -t rsa -f $HOME/.ssh/$LXC_NAME1 -P '' >> "$LOG_BUILD_LXC" 2>&1 + +echo | tee -a $HOME/.ssh/config <> "$LOG_BUILD_LXC" 2>&1 +# ssh $LXC_NAME1 +Host $LXC_NAME1 +Hostname $IP_LXC1 +User ssh_demo +IdentityFile $HOME/.ssh/$LXC_NAME1 +Host $LXC_NAME2 +Hostname $IP_LXC2 +User ssh_demo +IdentityFile $HOME/.ssh/$LXC_NAME1 +# End ssh $LXC_NAME1 +EOF + +echo -e "\e[1m> Mise en place du reverse proxy et du load balancing\e[0m" | tee -a "$LOG_BUILD_LXC" +echo | sudo tee /etc/nginx/conf.d/$DOMAIN.conf <> "$LOG_BUILD_LXC" 2>&1 +#upstream $DOMAIN { +# server $IP_LXC1:443 ; +# server $IP_LXC2:443 ; +#} + +server { + listen 80; + listen [::]:80; + server_name $DOMAIN; + + location '/.well-known/acme-challenge' { + default_type "text/plain"; + root /tmp/letsencrypt-auto; + } + + access_log /var/log/nginx/$DOMAIN-access.log; + error_log /var/log/nginx/$DOMAIN-error.log; +} +EOF + +sudo service nginx reload + +echo -e "\e[1m> Création du certificat SSL.\e[0m" | tee -a "$LOG_BUILD_LXC" +sudo mkdir -p /etc/letsencrypt + +# Créer le fichier de config +echo | sudo tee /etc/letsencrypt/conf.ini <> "$LOG_BUILD_LXC" 2>&1 +################################# +# Let's encrypt configuration # +################################# + +# Use a 4096 bit RSA key instead of 2048 +rsa-key-size = 4096 + +# Uncomment and update to register with the specified e-mail address +email = $MAIL_ADDR + +# Uncomment to use the webroot authenticator. Replace webroot-path with the +# path to the public_html / webroot folder being served by your web server. +# avec le contenu dans /tmp/letsencrypt-auto +authenticator = webroot +webroot-path = /tmp/letsencrypt-auto + +# Utiliser l'interface texte +text = True +# Uncomment to automatically agree to the terms of service of the ACME server +agree-tos = true + +# (Serveur de test uniquement : si vous l'utilisez, +# votre certificat ne sera pas vraiment valide) +# server = https://acme-staging-v02.api.letsencrypt.org/directory +EOF + +mkdir -p /tmp/letsencrypt-auto +# Créer le certificat +sudo certbot certonly --config /etc/letsencrypt/conf.ini -d $DOMAIN --no-eff-email + +# Route l'upstream sur le port 443. Le port 80 servait uniquement à let's encrypt +# sudo sed -i "s/server $IP_LXC1:80 ;/server $IP_LXC1:443 ;/" /etc/nginx/conf.d/$DOMAIN.conf +# Décommente les lignes du certificat +# sudo sed -i "s/#\tssl_certificate/\tssl_certificate/g" /etc/nginx/conf.d/$DOMAIN.conf +# Supprime les commentaires dans la conf nginx + +echo | sudo tee /etc/nginx/conf.d/$DOMAIN.conf <> "$LOG_BUILD_LXC" 2>&1 +#upstream $DOMAIN { +# server $IP_LXC1:443 ; +# server $IP_LXC2:443 ; +#} + +server { + listen 80; + listen [::]:80; + server_name $DOMAIN; + + location '/.well-known/acme-challenge' { + default_type "text/plain"; + root /tmp/letsencrypt-auto; + } + + access_log /var/log/nginx/$DOMAIN-access.log; + error_log /var/log/nginx/$DOMAIN-error.log; +} + +server { + listen 443 ssl; + listen [::]:443 ssl; + server_name $DOMAIN; + + ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem; + ssl_session_timeout 5m; + ssl_session_cache shared:SSL:50m; + ssl_prefer_server_ciphers on; + ssl_protocols TLSv1 TLSv1.1 TLSv1.2; + ssl_ciphers ALL:!aNULL:!eNULL:!LOW:!EXP:!RC4:!3DES:+HIGH:+MEDIUM; + add_header Strict-Transport-Security "max-age=31536000;"; + + location / { + proxy_pass https://$DOMAIN; + proxy_redirect off; + proxy_set_header Host \$host; + proxy_set_header X-Real-IP \$remote_addr; + proxy_set_header X-Forwarded-Proto \$scheme; + proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host \$server_name; + } + + access_log /var/log/nginx/$DOMAIN-access.log; + error_log /var/log/nginx/$DOMAIN-error.log; +} +EOF + +sudo service nginx reload + +echo -e "\e[1mLe serveur est prêt à déployer les conteneurs de demo.\e[0m" +echo -e "\e[1mExécutez le script demo_lxc_build.sh pour créer les conteneurs et mettre en place la demo.\e[0m" + +# Déploie les conteneurs de demo +# "$script_dir/demo_lxc_build.sh" From 98f1bbdabd8d806f7ef18d08dca0425b353c7b4c Mon Sep 17 00:00:00 2001 From: yalh76 Date: Thu, 13 Oct 2022 05:46:46 +0200 Subject: [PATCH 05/25] Switching to LXD --- conf/cron_demo_switch | 2 + conf/cron_demo_upgrade | 2 + conf/systemd.service | 13 + demo_lxc_build.sh | 394 ++++++------------ demo_lxc_build_init.sh | 82 ++-- demo_lxc_destroy.sh | 60 ++- demo_lxc_remove.sh | 32 +- demo_restore_crash.sh | 171 ++------ demo_start.sh | 41 +- demo_stop.sh | 61 ++- demo_switch.sh | 69 +-- demo_upgrade.sh | 170 +------- upgrade.d/Constant_upgrade/Default_login | 23 +- upgrade.d/Constant_upgrade/Disable_add_domain | 4 +- .../Constant_upgrade/Disable_custom_apps | 4 +- ynh_lxd_demo | 177 ++++++++ 16 files changed, 554 insertions(+), 751 deletions(-) create mode 100644 conf/cron_demo_switch create mode 100644 conf/cron_demo_upgrade create mode 100644 conf/systemd.service create mode 100644 ynh_lxd_demo diff --git a/conf/cron_demo_switch b/conf/cron_demo_switch new file mode 100644 index 0000000..e06eb84 --- /dev/null +++ b/conf/cron_demo_switch @@ -0,0 +1,2 @@ +# Switch des conteneurs toutes les __TIME_TO_SWITCH__ minutes +*/__TIME_TO_SWITCH__ * * * * root /bin/bash __FINAL_PATH__/demo_switch.sh >> "__FINAL_PATH__/demo_switch.log" 2>&1 diff --git a/conf/cron_demo_upgrade b/conf/cron_demo_upgrade new file mode 100644 index 0000000..74be870 --- /dev/null +++ b/conf/cron_demo_upgrade @@ -0,0 +1,2 @@ +# Vérifie les mises à jour des conteneurs de demo, lorsqu'ils ne sont pas utilisés, à partir de 3h2minutes chaque nuit. Attention à rester sur un multiple du temps de switch. +2 3 * * * root /bin/bash __FINAL_PATH__/demo_upgrade.sh >> "__FINAL_PATH__/demo_upgrade.log" 2>&1 \ No newline at end of file diff --git a/conf/systemd.service b/conf/systemd.service new file mode 100644 index 0000000..9a8fce8 --- /dev/null +++ b/conf/systemd.service @@ -0,0 +1,13 @@ +[Unit] +Description=Start and stop script for lxc demo container +Requires=network.target +After=network.target +[Service] +Type=forking +ExecStart=/bin/bash __FINAL_PATH__/demo_start.sh +RemainAfterExit=true +ExecStop=/bin/bash __FINAL_PATH__/demo_stop.sh +ExecReload=/bin/bash __FINAL_PATH__/demo_start.sh + +[Install] +WantedBy=multi-user.target diff --git a/demo_lxc_build.sh b/demo_lxc_build.sh index 3b76403..d8a49d8 100755 --- a/demo_lxc_build.sh +++ b/demo_lxc_build.sh @@ -5,313 +5,173 @@ # Récupère le dossier du script if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi +source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo +source /usr/share/yunohost/helpers + +app=${__APP__:-yunohost_demo} +final_path=$(ynh_app_setting_get --app=$app --key=final_path) +domain=$(ynh_app_setting_get --app=$app --key=domain) +path_url=$(ynh_app_setting_get --app=$app --key=path) +lxdbr_demo_name=$(ynh_app_setting_get --app=$app --key=lxdbr_demo_name) +lxdbr_demo_network=$(ynh_app_setting_get --app=$app --key=lxdbr_demo_network) +lxc_ip1=$(ynh_app_setting_get --app=$app --key=lxc_ip1) +lxc_ip2=$(ynh_app_setting_get --app=$app --key=lxc_ip2) +demo_user=$(ynh_app_setting_get --app=$app --key=demo_user) +demo_password=$(ynh_app_setting_get --app=$app --key=demo_password) +demo_package=$(ynh_app_setting_get --app=$app --key=demo_package) +yunohost_password="$demo_password" +lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) +lxc_name2=$(ynh_app_setting_get --app=$app --key=lxc_name2) +time_to_switch=$(ynh_app_setting_get --app=$app --key=time_to_switch) +DIST=$(ynh_app_setting_get --app=$app --key=DIST) +ARCH=$(ynh_app_setting_get --app=$app --key=ARCH) +YNH_BRANCH=$(ynh_app_setting_get --app=$app --key=YNH_BRANCH) +lxc_base="ynh-dev-$DIST-$ARCH-$YNH_BRANCH-base" + LOG=Build_lxc.log -LOG_BUILD_LXC="$script_dir/$LOG" -PLAGE_IP=10.1.5 -IP_LXC1=10.1.5.3 -IP_LXC2=10.1.5.4 -ARG_SSH=-t -DOMAIN=$(cat "$script_dir/domain.ini") -YUNO_PWD=demo -LXC_NAME1=yunohost_demo1 -LXC_NAME2=yunohost_demo2 -TIME_TO_SWITCH=30 -DIST="bullseye" - # En minutes -MAIL_ADDR=demo@yunohost.org -dnsforce=0 -main_iface= -dns= +LOG_BUILD_LXC="$final_path/$LOG" -lxc_current_version=$(lxc-info --version) - -if $(dpkg --compare-versions "$lxc_current_version" "gt" "3.0.0"); then new_lxc=1; else new_lxc=0; fi - -USER_DEMO=demo -PASSWORD_DEMO=demo - -# Tente de définir l'interface réseau principale -if [ -z $main_iface ] # Si main_iface est vide, tente de le trouver. -then -# main_iface=$(sudo route | grep default.*0.0.0.0 -m1 | awk '{print $8;}') # Prend l'interface réseau défini par default - main_iface=$(sudo ip route | grep default | awk '{print $5;}') # Prend l'interface réseau défini par default - if [ -z $main_iface ]; then - echo -e "\e[91mImpossible de déterminer le nom de l'interface réseau de l'hôte.\e[0m" - exit 1 - fi -fi - -if [ -z $dns ] # Si l'adresse du dns est vide, tente de le déterminer à partir de la passerelle par défaut. -then -# dns=$(sudo route -n | grep ^0.0.0.0.*$main_iface | awk '{print $2;}') - dns=$(sudo ip route | grep default | awk '{print $3;}') - if [ -z $dns ]; then - echo -e "\e[91mImpossible de déterminer l'adresse de la passerelle.\e[0m" - exit 1 - fi -fi - -# Check user -if [ "$USER" != "$(cat "$script_dir/setup_user")" ] && test -e "$script_dir/setup_user"; then - echo -e "\e[91mCe script doit être exécuté avec l'utilisateur $(cat "$script_dir/setup_user")" - echo -en "\e[0m" - exit 0 -fi - -sudo mkdir -p /var/lib/lxcsnaps # Créer le dossier lxcsnaps, pour s'assurer que lxc utilisera ce dossier, même avec lxc 2. - -if sudo lxc-info -n $LXC_NAME1 > /dev/null 2>&1 +if $(ynh_lxc_exists --name="$name") then # Si le conteneur existe déjà - echo -e "\e[1m> Suppression du conteneur existant.\e[0m" | tee -a "$LOG_BUILD_LXC" - "$script_dir/demo_lxc_destroy.sh" quiet | tee -a "$LOG_BUILD_LXC" + ynh_print_info --message="\e[1m> Suppression du conteneur existant.\e[0m" | tee -a "$LOG_BUILD_LXC" + /bin/bash "$final_path/demo_lxc_destroy.sh" quiet | tee -a "$LOG_BUILD_LXC" fi -echo -e "\e[1m> Création d'une machine debian $DIST minimaliste\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo lxc-create -n $LXC_NAME1 -t debian -- -r $DIST >> "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="\e[1m> Création d'une machine debian $DIST minimaliste\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_create --image="$lxc_base" --name="$lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 -echo -e "\e[1m> Active le bridge réseau\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo ifup lxc_demo --interfaces=/etc/network/interfaces.d/lxc_demo >> "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message= "\e[1m> Active le bridge réseau\e[0m" | tee -a "$LOG_BUILD_LXC" +lxc network attach $lxdbr_demo_name $lxc_name1 eth1 eth1 | tee -a "$LOG_BUILD_LXC" 2>&1 -echo -e "\e[1m> Configuration réseau du conteneur\e[0m" | tee -a "$LOG_BUILD_LXC" -if [ $new_lxc -eq 1 ]; then - sudo sed -i "s/^lxc.net.0.link = lxcbr0$/lxc.net.0.link = lxc_demo\nlxc.net.0.name = eth0\nlxc.net.0.veth.pair = $LXC_NAME1/" /var/lib/lxc/$LXC_NAME1/config >> "$LOG_BUILD_LXC" 2>&1 -else - sudo sed -i "s/^lxc.net.0.type = empty$/lxc.net.0.type = veth\nlxc.net.0.flags = up\nlxc.net.0.link = lxc_demo\nlxc.net.0.name = eth0\nlxc.net.0.veth.pair = $LXC_NAME1\nlxc.net.0.hwaddr = 00:FF:AA:00:00:03/" /var/lib/lxc/$LXC_NAME1/config >> "$LOG_BUILD_LXC" 2>&1 -fi +ynh_print_info --message="\e[1m> Configuration réseau de la machine virtualisée\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="cp /etc/systemd/network/eth0.network /etc/systemd/network/eth1.network" +ynh_lxc_run_inside --name="$lxc_name1" --command="sed -i s/eth0/eth1/g /etc/systemd/network/eth1.network" -echo -e "\e[1m> Configuration réseau de la machine virtualisée\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo sed -i "s@iface eth0 inet dhcp@iface eth0 inet static\n\taddress $IP_LXC1/24\n\tgateway $PLAGE_IP.1@" /var/lib/lxc/$LXC_NAME1/rootfs/etc/network/interfaces >> "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="\e[1m> Update de la machine virtualisée\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y update" +ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y full-upgrade" +ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y autoremove" +ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y clean" -echo -e "\e[1m> Configure le parefeu\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo iptables -A FORWARD -i lxc_demo -o eth0 -j ACCEPT >> "$LOG_BUILD_LXC" 2>&1 -sudo iptables -A FORWARD -i eth0 -o lxc_demo -j ACCEPT >> "$LOG_BUILD_LXC" 2>&1 -sudo iptables -t nat -A POSTROUTING -s $PLAGE_IP.0/24 -j MASQUERADE >> "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="\e[1m> Post install Yunohost\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost tools postinstall --domain $domain --password $yunohost_password --force-password" | tee -a "$LOG_BUILD_LXC" 2>&1 -echo -e "\e[1m> Vérification du contenu du resolv.conf\e[0m" | tee -a "$LOG_BUILD_LXC" -if ! sudo cat /var/lib/lxc/$LXC_NAME1/rootfs/etc/resolv.conf | grep -q nameserver; then - dnsforce=1 # Le resolv.conf est vide, on force l'ajout d'un dns. -fi -if [ $dnsforce -eq 1 ]; then # Force la réécriture du resolv.conf - echo "nameserver $dns" | sudo tee /var/lib/lxc/$LXC_NAME1/rootfs/etc/resolv.conf -fi +ynh_print_info --message="\e[1m> Disable password strength\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost settings set security.password.user.strength -v -1" | tee -a "$LOG_BUILD_LXC" -# Fix an issue with apparmor when the container start. -if [ $new_lxc -eq 1 ]; then - sudo sed -i "s/^lxc.apparmor.profile = generated$/lxc.apparmor.profile = unconfined/" /var/lib/lxc/$LXC_NAME1/config >> "$LOG_BUILD_LXC" 2>&1 -else - echo -e "\n# Fix apparmor issues\nlxc.aa_profile = unconfined" | sudo tee -a /var/lib/lxc/$LXC_NAME1/config >> "$LOG_BUILD_LXC" 2>&1 -fi +ynh_print_info --message="\e[1m> Ajout de l'utilisateur de demo\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost user create $demo_user --firstname $demo_user --lastname $demo_user --domain $domain --password $demo_password" | tee -a "$LOG_BUILD_LXC" -echo -e "\e[1m> Démarrage de la machine\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo lxc-start -n $LXC_NAME1 -d --logfile "$script_dir/lxc_boot.log" >> "$LOG_BUILD_LXC" 2>&1 -sleep 3 -sudo lxc-ls -f >> "$LOG_BUILD_LXC" 2>&1 - -echo -e "\e[1m> Update et install aptitude sudo aptitude sudo ssh openssh-server curl\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo lxc-attach -n $LXC_NAME1 -- apt-get update -sudo lxc-attach -n $LXC_NAME1 -- apt-get install -y aptitude sudo ssh openssh-server curl -echo -e "\e[1m> Installation des paquets standard et ssh-server\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo lxc-attach -n $LXC_NAME1 -- aptitude install -y ~pstandard ~prequired ~pimportant - -echo -e "\e[1m> Renseigne /etc/hosts sur l'invité\e[0m" | tee -a "$LOG_BUILD_LXC" -echo "127.0.0.1 $LXC_NAME1" | sudo tee -a /var/lib/lxc/$LXC_NAME1/rootfs/etc/hosts >> "$LOG_BUILD_LXC" 2>&1 - -echo -e "\e[1m> Ajoute l'user ssh_demo (avec un mot de passe à revoir...)\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo lxc-attach -n $LXC_NAME1 -- useradd -m -p ssh_demo ssh_demo >> "$LOG_BUILD_LXC" 2>&1 - -echo -e "\e[1m> Autorise ssh_demo à utiliser sudo sans mot de passe\e[0m" | tee -a "$LOG_BUILD_LXC" -echo "ssh_demo ALL=(ALL:ALL) NOPASSWD: ALL" | sudo tee -a /var/lib/lxc/$LXC_NAME1/rootfs/etc/sudoers >> "$LOG_BUILD_LXC" 2>&1 - -echo -e "\e[1m> Mise en place de la connexion ssh vers l'invité.\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo mkdir /var/lib/lxc/$LXC_NAME1/rootfs/home/ssh_demo/.ssh >> "$LOG_BUILD_LXC" 2>&1 -sudo cp $HOME/.ssh/$LXC_NAME1.pub /var/lib/lxc/$LXC_NAME1/rootfs/home/ssh_demo/.ssh/authorized_keys >> "$LOG_BUILD_LXC" 2>&1 -sudo lxc-attach -n $LXC_NAME1 -- chown ssh_demo -R /home/ssh_demo/.ssh >> "$LOG_BUILD_LXC" 2>&1 - -ssh -o StrictHostKeyChecking=no $ARG_SSH $LXC_NAME1 "exit 0" # Initie une première connexion SSH pour valider la clé. -if [ "$?" -ne 0 ]; then # Si l'utilisateur tarde trop, la connexion sera refusée... ??? - ssh $ARG_SSH $LXC_NAME1 "exit 0" # Initie une premier connexion SSH pour valider la clé. -fi - -# Fix ssh common issues with stretch "No supported key exchange algorithms" -sudo lxc-attach -n $LXC_NAME -- dpkg-reconfigure openssh-server >> "$LOG_BUILD_LXC" 2>&1 - -# Fix locales issue -sudo lxc-attach -n $LXC_NAME -- locale-gen en_US.UTF-8 >> "$LOG_BUILD_LXC" 2>&1 -sudo lxc-attach -n $LXC_NAME -- localedef -i en_US -f UTF-8 en_US.UTF-8 >> "$LOG_BUILD_LXC" 2>&1 - -echo -e "\e[1m> Installation de Yunohost...\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo /bin/bash -c \"curl https://install.yunohost.org/$DIST | bash -s -- -a -d stable\"" | tee -a "$LOG_BUILD_LXC" 2>&1 -echo -e "\e[1m> Post install Yunohost\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo systemctl start dbus.service" | tee -a "$LOG_BUILD_LXC" 2>&1 -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost tools postinstall --domain $DOMAIN --password $YUNO_PWD --force-password" | tee -a "$LOG_BUILD_LXC" 2>&1 - -echo -e "\e[1m> Fix SSH access\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo lxc-attach -n $LXC_NAME1 -- sed -i "s/AllowGroups ssh.main sftp.main ssh.app sftp.app admins root/AllowGroups ssh.main sftp.main ssh.app sftp.app admins root ssh_demo/" /etc/ssh/sshd_config >> "$LOG_BUILD_LXC" 2>&1 -sudo lxc-attach -n $LXC_NAME1 -- service sshd restart >> "$LOG_BUILD_LXC" 2>&1 - -echo -e "\e[1m> Disable password strength\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost settings set security.password.user.strength -v -1" | tee -a "$LOG_BUILD_LXC" - -USER_DEMO_CLEAN=${USER_DEMO//"_"/""} -echo -e "\e[1m> Ajout de l'utilisateur de demo\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost user create \"$USER_DEMO\" --firstname \"$USER_DEMO_CLEAN\" --lastname \"$USER_DEMO_CLEAN\" --domain \"$DOMAIN\" --password \"$PASSWORD_DEMO\"" - -echo -e "\e[1m\n> Vérification de l'état de Yunohost\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost -v" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="\e[1m\n> Vérification de l'état de Yunohost\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost -v" | tee -a "$LOG_BUILD_LXC" 2>&1 # ******** -echo -e "\e[1m>> Modification de Yunohost pour la demo\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[1m>> Modification de Yunohost pour la demo\e[0m" | tee -a "$LOG_BUILD_LXC" +if [ ! -z "$PACKAGE_CHECK_EXEC" ] +then # App officielles -echo -e "\e[1m> Installation des applications officielles\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[1m> Installation des applications officielles\e[0m" | tee -a "$LOG_BUILD_LXC" # Ampache -echo -e "\e[36mInstallation de Ampache\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install ampache --force --args \"domain=$DOMAIN&path=/ampache&admin=$USER_DEMO&is_public=1\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de Ampache\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install ampache --force --args \"domain=$domain&path=/ampache&admin=$demo_user&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Baikal -echo -e "\e[36mInstallation de baikal\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install baikal --force --args \"domain=$DOMAIN&path=/baikal&password=$PASSWORD_DEMO\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de baikal\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install baikal --force --args \"domain=$domain&path=/baikal&password=$demo_password&\"" | tee -a "$LOG_BUILD_LXC" # Agendav -echo -e "\e[36mInstallation d'agendav\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install agendav --force --args \"domain=$DOMAIN&path=/agendav&language=en\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation d'agendav\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install agendav --force --args \"domain=$domain&path=/agendav&language=en&\"" | tee -a "$LOG_BUILD_LXC" # Dokuwiki -echo -e "\e[36mInstallation de dokuwiki\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install dokuwiki --force --args \"domain=$DOMAIN&path=/dokuwiki&admin=$USER_DEMO&is_public=1&language=en\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de dokuwiki\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install dokuwiki --force --args \"domain=$domain&path=/dokuwiki&admin=$demo_user&is_public=1&language=en&\"" | tee -a "$LOG_BUILD_LXC" # Etherpad -echo -e "\e[36mInstallation de etherpad\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install etherpad_mypads --force --args \"domain=$DOMAIN&path=/etherpad&admin=$USER_DEMO&password=administration&language=en&is_public=1&export=none&mypads=1&useldap=0\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de etherpad\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install etherpad_mypads --force --args \"domain=$domain&path=/etherpad&admin=$demo_user&password=administration&language=en&is_public=1&export=none&mypads=1&useldap=0&\"" | tee -a "$LOG_BUILD_LXC" # Hextris -echo -e "\e[36mInstallation de hextris\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install hextris --force --args \"domain=$DOMAIN&path=/hextris&is_public=1\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de hextris\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install hextris --force --args \"domain=$domain&path=/hextris&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Jirafeau -echo -e "\e[36mInstallation de jirafeau\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install jirafeau --force --args \"domain=$DOMAIN&path=/jirafeau&admin_user=$USER_DEMO&upload_password=$PASSWORD_DEMO&is_public=1\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de jirafeau\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install jirafeau --force --args \"domain=$domain&path=/jirafeau&admin_user=$demo_user&upload_password=$demo_password&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Kanboard -echo -e "\e[36mInstallation de kanboard\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install kanboard --force --args \"domain=$DOMAIN&path=/kanboard&admin=$USER_DEMO&is_public=1\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de kanboard\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install kanboard --force --args \"domain=$domain&path=/kanboard&admin=$demo_user&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Nextcloud -echo -e "\e[36mInstallation de nextcloud\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install nextcloud --force --args \"domain=$DOMAIN&path=/nextcloud&admin=$USER_DEMO&user_home=0&is_public=1\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de nextcloud\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install nextcloud --force --args \"domain=$domain&path=/nextcloud&admin=$demo_user&user_home=0&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Opensondage -echo -e "\e[36mInstallation de opensondage\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install opensondage --force --args \"domain=$DOMAIN&path=/date&admin=$USER_DEMO&language=en&is_public=1\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de opensondage\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install opensondage --force --args \"domain=$domain&path=/date&admin=$demo_user&language=en&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Phpmyadmin -echo -e "\e[36mInstallation de phpmyadmin\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install phpmyadmin --force --args \"domain=$DOMAIN&path=/phpmyadmin&admin=$USER_DEMO\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de phpmyadmin\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install phpmyadmin --force --args \"domain=$domain&path=/phpmyadmin&admin=$demo_user&\"" | tee -a "$LOG_BUILD_LXC" # Piwigo -echo -e "\e[36mInstallation de piwigo\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install piwigo --force --args \"domain=$DOMAIN&path=/piwigo&admin=$USER_DEMO&is_public=1&language=en\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de piwigo\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install piwigo --force --args \"domain=$domain&path=/piwigo&admin=$demo_user&is_public=1&language=en&\"" | tee -a "$LOG_BUILD_LXC" # Rainloop -echo -e "\e[36mInstallation de rainloop\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install rainloop --force --args \"domain=$DOMAIN&path=/rainloop&is_public=No&password=$PASSWORD_DEMO&ldap=Yes&language=en\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de rainloop\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install rainloop --force --args \"domain=$domain&path=/rainloop&is_public=No&password=$demo_password&ldap=Yes&language=en&\"" | tee -a "$LOG_BUILD_LXC" # Roundcube -echo -e "\e[36mInstallation de roundcube\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install roundcube --force --args \"domain=$DOMAIN&path=/webmail&with_carddav=0&with_enigma=0&language=en_GB\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de roundcube\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install roundcube --force --args \"domain=$domain&path=/webmail&with_carddav=0&with_enigma=0&language=en_GB&\"" | tee -a "$LOG_BUILD_LXC" # Searx -echo -e "\e[36mInstallation de searx\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install searx --force --args \"domain=$DOMAIN&path=/searx&is_public=1\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="\e[36mInstallation de searx\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install searx --force --args \"domain=$domain&path=/searx&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Shellinabox -echo -e "\e[36mInstallation de shellinabox\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install shellinabox --force --args \"domain=$DOMAIN&path=/ssh\"" | tee -a "$LOG_BUILD_LXC" -# Strut -echo -e "\e[36mInstallation de strut\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install strut --force --args \"domain=$DOMAIN&path=/strut&is_public=1\"" | tee -a "$LOG_BUILD_LXC" -# Transmission -echo -e "\e[36mInstallation de transmission\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install transmission --force --args \"domain=$DOMAIN&path=/torrent\"" | tee -a "$LOG_BUILD_LXC" -# Ttrss -echo -e "\e[36mInstallation de ttrss\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install ttrss --force --args \"domain=$DOMAIN&path=/ttrss&is_public=1\"" | tee -a "$LOG_BUILD_LXC" -# Wallabag -echo -e "\e[36mInstallation de wallabag\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install wallabag2 --force --args \"domain=$DOMAIN&path=/wallabag&admin=$USER_DEMO\"" | tee -a "$LOG_BUILD_LXC" -# Wordpress -echo -e "\e[36mInstallation de wordpress\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install wordpress --force --args \"domain=$DOMAIN&path=/blog&admin=$USER_DEMO&language=en_US&multisite=0&is_public=1\"" | tee -a "$LOG_BUILD_LXC" -# Zerobin -echo -e "\e[36mInstallation de zerobin\e[0m" | tee -a "$LOG_BUILD_LXC" -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app install zerobin --force --args \"domain=$DOMAIN&path=/zerobin&is_public=1\"" | tee -a "$LOG_BUILD_LXC" - +ynh_print_info --message="\e[36mInstallation de shellinabox\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install shellinabox --force --args \"domain=$domain&path=/ssh&\"" | tee -a "$LOG_BUILD_LXC" # Désactive l'accès à shellinabox -sudo rm "/var/lib/lxc/$LXC_NAME1/rootfs/etc/nginx/conf.d/$DOMAIN.d/shellinabox.conf" # Supprime le fichier de conf nginx de shellinabox pour empêcher d'y accéder. -ssh $ARG_SSH $LXC_NAME1 "sudo yunohost app setting shellinabox path -d && sudo yunohost app setting shellinabox domain -d && sudo yunohost app ssowatconf" | tee -a "$LOG_BUILD_LXC" - -# Indique le couple login/mot de passe demo/demo -# Et ajoute demo/demo par défaut dans les champs d'identification -sed -i "3i\
Login: $USER_DEMO / Password: $PASSWORD_DEMO
" /var/lib/lxc/yunohost_demo1/rootfs/usr/share/ssowat/portal/login.html # Sur le login du portail -sed -i "s/id=\"user\" type=\"text\" name=\"user\"/id=\"user\" type=\"text\" name=\"user\" value=\"$USER_DEMO\"/" /var/lib/lxc/yunohost_demo1/rootfs/usr/share/ssowat/portal/login.html -sed -i "s/id=\"password\" type=\"password\" name=\"password\"/id=\"password\" type=\"password\" name=\"password\" value=\"$PASSWORD_DEMO\"/" /var/lib/lxc/yunohost_demo1/rootfs/usr/share/ssowat/portal/login.html - -#sed -i "17i\   Password: $YUNO_PWD" /var/lib/lxc/yunohost_demo1/rootfs/usr/share/yunohost/admin/views/login.ms # Et sur le login admin -#sed -i "s/type=\"password\" id=\"password\" name=\"password\"/type=\"password\" id=\"password\" name=\"password\" value=\"$YUNO_PWD\"/" /var/lib/lxc/yunohost_demo1/rootfs/usr/share/yunohost/admin/views/login.ms - -# Désactive l'installation d'app custom -sed -i "s@{{t 'install'}}@{{t 'install'}}@g" /var/lib/lxc/yunohost_demo1/rootfs/usr/share/yunohost/admin/views/app/app_catalog_category.ms - -# Désactive l'ajout de domaine, pour éviter surtout les nohost -sed -i "s@@@g" /var/lib/lxc/yunohost_demo1/rootfs/usr/share/yunohost/admin/views/domain/domain_add.ms - +ynh_lxc_run_inside --name="$lxc_name1" --command="rm /etc/nginx/conf.d/$domain.d/shellinabox.conf" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app setting shellinabox path -d" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app setting shellinabox domain -d" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app ssowatconf" +# Strut +ynh_print_info --message="\e[36mInstallation de strut\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install strut --force --args \"domain=$domain&path=/strut&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +# Transmission +ynh_print_info --message="\e[36mInstallation de transmission\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install transmission --force --args \"domain=$domain&path=/torrent&\"" | tee -a "$LOG_BUILD_LXC" +# Ttrss +ynh_print_info --message="\e[36mInstallation de ttrss\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install ttrss --force --args \"domain=$domain&path=/ttrss&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +# Wallabag +ynh_print_info --message="\e[36mInstallation de wallabag\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wallabag2 --force --args \"domain=$domain&path=/wallabag&admin=$demo_user&\"" | tee -a "$LOG_BUILD_LXC" +# Wordpress +ynh_print_info --message="\e[36mInstallation de wordpress\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wordpress --force --args \"domain=$domain&path=/blog&admin=$demo_user&language=en_US&multisite=0&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +# Zerobin +ynh_print_info --message="\e[36mInstallation de zerobin\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install zerobin --force --args \"domain=$domain&path=/zerobin&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +fi # ******** -echo -e "\e[1m> Arrêt de la machine virtualisée\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo lxc-stop -n $LXC_NAME1 >> "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="\e[1m> Création d'un snapshot\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_create_snapshot --name="$lxc_name1" --snapname="snap0" -echo -e "\e[1m> Suppression des règles de parefeu\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo iptables -D FORWARD -i lxc_demo -o eth0 -j ACCEPT >> "$LOG_BUILD_LXC" 2>&1 -sudo iptables -D FORWARD -i eth0 -o lxc_demo -j ACCEPT >> "$LOG_BUILD_LXC" 2>&1 -sudo iptables -t nat -D POSTROUTING -s $PLAGE_IP.0/24 -j MASQUERADE >> "$LOG_BUILD_LXC" 2>&1 -sudo ifdown --force lxc_demo >> "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="\e[1m> Mise à jour de la machine virtualisée\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_stop --name="$lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_upgrade_demo --name=$lxc_name1 --time_to_switch=$time_to_switch -echo -e "\e[1m> Création d'un snapshot\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo lxc-snapshot -n $LXC_NAME1 >> "$LOG_BUILD_LXC" 2>&1 -# Il sera nommé snap0 et stocké dans /var/lib/lxcsnaps/$LXC_NAME1/snap0/ +ynh_print_info --message="\e[1m> Clone la machine\e[0m" | tee -a "$LOG_BUILD_LXC" +lxc copy "$lxc_name1" "$lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 -echo -e "\e[1m> Clone la machine\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo lxc-copy --name=$LXC_NAME1 --newname=$LXC_NAME2 >> "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="\e[1m> Création d'un snapshot\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_lxc_create_snapshot --name="$lxc_name2" --snapname="snap0" -echo -e "\e[1m> Modification de l'ip du clone\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo sed -i "s@address $IP_LXC1@address $IP_LXC2@" /var/lib/lxc/$LXC_NAME2/rootfs/etc/network/interfaces >> "$LOG_BUILD_LXC" 2>&1 -echo -e "\e[1m> Et le nom du veth\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo sed -i "s@^lxc.net.0.veth.pair = $LXC_NAME1@lxc.net.0.veth.pair = $LXC_NAME2@" /var/lib/lxc/$LXC_NAME2/config >> "$LOG_BUILD_LXC" 2>&1 -echo -e "\e[1m> Et enfin renseigne /etc/hosts sur le clone\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo sed -i "s@^127.0.0.1 $LXC_NAME1@127.0.0.1 $LXC_NAME2@" /var/lib/lxc/$LXC_NAME2/rootfs/etc/hosts >> "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="\e[1m> Mise en place du cron de switch\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_add_config --template="$final_path/conf/cron_demo_switch" --destination="/etc/cron.d/demo_switch" -echo -e "\e[1m> Mise en place du cron de switch\e[0m" -echo | sudo tee /etc/cron.d/demo_switch < /dev/null -# Switch des conteneurs toutes les $TIME_TO_SWITCH minutes -*/$TIME_TO_SWITCH * * * * root $script_dir/demo_switch.sh >> "$script_dir/demo_switch.log" 2>&1 -EOF -echo -e "\e[1m> Et du cron d'upgrade\e[0m" -echo | sudo tee /etc/cron.d/demo_upgrade < /dev/null -# Vérifie les mises à jour des conteneurs de demo, lorsqu'ils ne sont pas utilisés, à partir de 3h2minutes chaque nuit. Attention à rester sur un multiple du temps de switch. -2 3 * * * root $script_dir/demo_upgrade.sh >> "$script_dir/demo_upgrade.log" 2>&1 -EOF +ynh_print_info --message="\e[1m> Et du cron d'upgrade\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_add_config --template="$final_path/conf/cron_demo_upgrade" --destination="/etc/cron.d/demo_upgrade" -echo -e "\e[1m> Démarrage de la démo\e[0m" -"$script_dir/demo_start.sh" +ynh_print_info --message="e[1m> Mise en place du service" | tee -a "$LOG_BUILD_LXC" +#ynh_add_systemd_config --template="$final_path/conf/systemd.service" +ynh_add_systemd_config -# echo "> Mise en place du service" -echo | sudo tee /etc/systemd/system/lxc_demo.service < /dev/null -[Unit] -Description=Start and stop script for lxc demo container -Requires=network.target -After=network.target +ynh_print_info --message="e[1m> Integrating service in YunoHost..." | tee -a "$LOG_BUILD_LXC" +yunohost service add $app --log="/var/log/$app/$app.log" -[Service] -Type=forking -ExecStart=$script_dir/demo_start.sh -RemainAfterExit=true -ExecStop=$script_dir/demo_stop.sh -ExecReload=$script_dir/demo_start.sh - -[Install] -WantedBy=multi-user.target -EOF - -# Démarrage automatique du service -sudo systemctl enable lxc_demo.service -sudo service lxc_demo start - -# Après le démarrage du premier conteneur, fait un snapshot du deuxième. -echo -e "\e[1m> Création d'un snapshot pour le 2e conteneur\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo lxc-snapshot -n $LXC_NAME2 >> "$LOG_BUILD_LXC" 2>&1 -# Il sera nommé snap0 et stocké dans /var/lib/lxcsnaps/$LXC_NAME2/snap0/ +ynh_print_info --message="e[1m> Starting a systemd service..." | tee -a "$LOG_BUILD_LXC" +ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" diff --git a/demo_lxc_build_init.sh b/demo_lxc_build_init.sh index 9de69ab..7e178b0 100755 --- a/demo_lxc_build_init.sh +++ b/demo_lxc_build_init.sh @@ -7,11 +7,11 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$( LOG=$(cat "$script_dir/demo_lxc_build.sh" | grep LOG= | cut -d '=' -f2) LOG_BUILD_LXC="$script_dir/$LOG" -LXC_NAME1=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME1= | cut -d '=' -f2) -LXC_NAME2=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME2= | cut -d '=' -f2) -PLAGE_IP=$(cat "$script_dir/demo_lxc_build.sh" | grep PLAGE_IP= | cut -d '=' -f2) -IP_LXC1=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC1= | cut -d '=' -f2) -IP_LXC2=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC2= | cut -d '=' -f2) +lxc_name1=$(cat "$script_dir/demo_lxc_build.sh" | grep lxc_name1= | cut -d '=' -f2) +lxc_name2=$(cat "$script_dir/demo_lxc_build.sh" | grep lxc_name2= | cut -d '=' -f2) +lxdbr_demo_network=$(cat "$script_dir/demo_lxc_build.sh" | grep lxdbr_demo_network= | cut -d '=' -f2) +lxc_ip1=$(cat "$script_dir/demo_lxc_build.sh" | grep lxc_ip1= | cut -d '=' -f2) +lxc_ip2=$(cat "$script_dir/demo_lxc_build.sh" | grep lxc_ip2= | cut -d '=' -f2) MAIL_ADDR=$(cat "$script_dir/demo_lxc_build.sh" | grep MAIL_ADDR= | cut -d '=' -f2) # Check user @@ -24,52 +24,52 @@ echo "$DOMAIN" > "$script_dir/domain.ini" sudo mkdir -p $(dirname $LOG_BUILD_LXC) echo -e "\e[1m> Update et install lxc, lxctl et mailutils\e[0m" | tee "$LOG_BUILD_LXC" -sudo apt-get update >> "$LOG_BUILD_LXC" 2>&1 -sudo apt-get install -y lxc lxctl mailutils certbot >> "$LOG_BUILD_LXC" 2>&1 +sudo apt-get update | tee -a "$LOG_BUILD_LXC" 2>&1 +sudo apt-get install -y lxc lxctl mailutils certbot | tee -a "$LOG_BUILD_LXC" 2>&1 echo -e "\e[1m> Autoriser l'ip forwarding, pour router vers la machine virtuelle.\e[0m" | tee -a "$LOG_BUILD_LXC" -echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/lxc_demo.conf >> "$LOG_BUILD_LXC" 2>&1 -sudo sysctl -p /etc/sysctl.d/lxc_demo.conf >> "$LOG_BUILD_LXC" 2>&1 +echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/lxc_demo.conf | tee -a "$LOG_BUILD_LXC" 2>&1 +sudo sysctl -p /etc/sysctl.d/lxc_demo.conf | tee -a "$LOG_BUILD_LXC" 2>&1 echo -e "\e[1m> Ajoute un brige réseau pour la machine virtualisée\e[0m" | tee -a "$LOG_BUILD_LXC" -echo | sudo tee /etc/network/interfaces.d/lxc_demo <> "$LOG_BUILD_LXC" 2>&1 +echo | sudo tee /etc/network/interfaces.d/lxc_demo <&1 auto lxc_demo iface lxc_demo inet static - address $PLAGE_IP.1/24 - bridge_ports none - bridge_fd 0 - bridge_maxwait 0 + address $lxdbr_demo_network.1/24 + bridge_ports none + bridge_fd 0 + bridge_maxwait 0 EOF echo -e "\e[1m> Active le bridge réseau\e[0m" | tee -a "$LOG_BUILD_LXC" -sudo ifup lxc_demo --interfaces=/etc/network/interfaces.d/lxc_demo >> "$LOG_BUILD_LXC" 2>&1 +sudo ifup lxc_demo --interfaces=/etc/network/interfaces.d/lxc_demo | tee -a "$LOG_BUILD_LXC" 2>&1 echo -e "\e[1m> Mise en place de la connexion ssh vers l'invité.\e[0m" | tee -a "$LOG_BUILD_LXC" -if [ -e $HOME/.ssh/$LXC_NAME1 ]; then - rm -f $HOME/.ssh/$LXC_NAME1 $HOME/.ssh/$LXC_NAME1.pub - ssh-keygen -f $HOME/.ssh/known_hosts -R $IP_LXC1 - ssh-keygen -f $HOME/.ssh/known_hosts -R $IP_LXC2 +if [ -e $HOME/.ssh/$lxc_name1 ]; then + rm -f $HOME/.ssh/$lxc_name1 $HOME/.ssh/$lxc_name1.pub + ssh-keygen -f $HOME/.ssh/known_hosts -R $lxdbr_demo_network$lxc_ip1 + ssh-keygen -f $HOME/.ssh/known_hosts -R $lxdbr_demo_network$lxc_ip2 fi -ssh-keygen -t rsa -f $HOME/.ssh/$LXC_NAME1 -P '' >> "$LOG_BUILD_LXC" 2>&1 +ssh-keygen -t rsa -f $HOME/.ssh/$lxc_name1 -P '' | tee -a "$LOG_BUILD_LXC" 2>&1 -echo | tee -a $HOME/.ssh/config <> "$LOG_BUILD_LXC" 2>&1 -# ssh $LXC_NAME1 -Host $LXC_NAME1 -Hostname $IP_LXC1 +echo | tee -a $HOME/.ssh/config <&1 +# ssh $lxc_name1 +Host $lxc_name1 +Hostname $lxdbr_demo_network$lxc_ip1 User ssh_demo -IdentityFile $HOME/.ssh/$LXC_NAME1 -Host $LXC_NAME2 -Hostname $IP_LXC2 +IdentityFile $HOME/.ssh/$lxc_name1 +Host $lxc_name2 +Hostname $lxdbr_demo_network$lxc_ip2 User ssh_demo -IdentityFile $HOME/.ssh/$LXC_NAME1 -# End ssh $LXC_NAME1 +IdentityFile $HOME/.ssh/$lxc_name1 +# End ssh $lxc_name1 EOF echo -e "\e[1m> Mise en place du reverse proxy et du load balancing\e[0m" | tee -a "$LOG_BUILD_LXC" -echo | sudo tee /etc/nginx/conf.d/$DOMAIN.conf <> "$LOG_BUILD_LXC" 2>&1 +echo | sudo tee /etc/nginx/conf.d/$DOMAIN.conf <&1 #upstream $DOMAIN { -# server $IP_LXC1:443 ; -# server $IP_LXC2:443 ; +# server $lxdbr_demo_network$lxc_ip1:443 ; +# server $lxdbr_demo_network$lxc_ip2:443 ; #} server { @@ -79,7 +79,7 @@ server { location '/.well-known/acme-challenge' { default_type "text/plain"; - root /tmp/letsencrypt-auto; + root /tmp/letsencrypt-auto; } access_log /var/log/nginx/$DOMAIN-access.log; @@ -93,7 +93,7 @@ echo -e "\e[1m> Création du certificat SSL.\e[0m" | tee -a "$LOG_BUILD_LXC" sudo mkdir -p /etc/letsencrypt # Créer le fichier de config -echo | sudo tee /etc/letsencrypt/conf.ini <> "$LOG_BUILD_LXC" 2>&1 +echo | sudo tee /etc/letsencrypt/conf.ini <&1 ################################# # Let's encrypt configuration # ################################# @@ -125,15 +125,15 @@ mkdir -p /tmp/letsencrypt-auto sudo certbot certonly --config /etc/letsencrypt/conf.ini -d $DOMAIN --no-eff-email # Route l'upstream sur le port 443. Le port 80 servait uniquement à let's encrypt -# sudo sed -i "s/server $IP_LXC1:80 ;/server $IP_LXC1:443 ;/" /etc/nginx/conf.d/$DOMAIN.conf +# sudo sed -i "s/server $lxdbr_demo_network$lxc_ip1:80 ;/server $lxdbr_demo_network$lxc_ip1:443 ;/" /etc/nginx/conf.d/$DOMAIN.conf # Décommente les lignes du certificat # sudo sed -i "s/#\tssl_certificate/\tssl_certificate/g" /etc/nginx/conf.d/$DOMAIN.conf # Supprime les commentaires dans la conf nginx -echo | sudo tee /etc/nginx/conf.d/$DOMAIN.conf <> "$LOG_BUILD_LXC" 2>&1 +echo | sudo tee /etc/nginx/conf.d/$DOMAIN.conf <&1 #upstream $DOMAIN { -# server $IP_LXC1:443 ; -# server $IP_LXC2:443 ; +# server $lxdbr_demo_network$lxc_ip1:443 ; +# server $lxdbr_demo_network$lxc_ip2:443 ; #} server { @@ -143,7 +143,7 @@ server { location '/.well-known/acme-challenge' { default_type "text/plain"; - root /tmp/letsencrypt-auto; + root /tmp/letsencrypt-auto; } access_log /var/log/nginx/$DOMAIN-access.log; @@ -165,8 +165,8 @@ server { add_header Strict-Transport-Security "max-age=31536000;"; location / { - proxy_pass https://$DOMAIN; - proxy_redirect off; + proxy_pass https://$DOMAIN; + proxy_redirect off; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-Proto \$scheme; diff --git a/demo_lxc_destroy.sh b/demo_lxc_destroy.sh index b8266a8..1fbb555 100755 --- a/demo_lxc_destroy.sh +++ b/demo_lxc_destroy.sh @@ -6,36 +6,34 @@ # Récupère le dossier du script if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi -LXC_NAME1=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME1= | cut -d '=' -f2) -LXC_NAME2=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME2= | cut -d '=' -f2) -IP_LXC1=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC1= | cut -d '=' -f2) -IP_LXC2=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC2= | cut -d '=' -f2) +source $script_dir/ynh_lxd +source /usr/share/yunohost/helpers -# Check user -if [ "$USER" != "$(cat "$script_dir/setup_user")" ]; then - echo -e "\e[91mCe script doit être exécuté avec l'utilisateur $(cat "$script_dir/setup_user")" - echo -en "\e[0m" - exit 0 +app=${__APP__:-yunohost_demo} +final_path=$(ynh_app_setting_get --app=$app --key=final_path) +lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) +lxc_name2=$(ynh_app_setting_get --app=$app --key=lxc_name2) +lxdbr_demo_network=$(ynh_app_setting_get --app=$app --key=lxdbr_demo_network) +lxc_ip1=$(ynh_app_setting_get --app=$app --key=lxc_ip1) +lxc_ip2=$(ynh_app_setting_get --app=$app --key=lxc_ip2) + +/bin/bash "$final_path/demo_stop.sh" -f + +ynh_print_info --message="\e[1m> Suppression des conteneurs et de leur snapshots\e[0m" +ynh_secure_remove --file="/var/lib/lxd/snapshots/$lxc_name1/snap0.tar.gz" +ynh_lxc_delete --name=$lxc_name1 +ynh_secure_remove --file="/var/lib/lxd/snapshots/$lxc_name2/snap0.tar.gz" +ynh_lxc_delete --name=$lxc_name2 + +ynh_print_info --message="\e[1m> Suppression des crons\e[0m" +ynh_secure_remove --file=/etc/cron.d/demo_switch +ynh_secure_remove --file=/etc/cron.d/demo_upgrade + +ynh_print_info --message="\e[1m> Suppression du service\e[0m" +if ynh_exec_warn_less yunohost service status $app >/dev/null +then + ynh_print_info --message="Removing $app service integration..." + yunohost service remove $app fi - -"$script_dir/demo_stop.sh" - -echo -e "\e[1m> Suppression des conteneurs et de leur snapshots\e[0m" -sudo lxc-snapshot -n $LXC_NAME1 -d snap0 -sudo rm -f /var/lib/lxcsnaps/$LXC_NAME1/snap0.tar.gz -sudo lxc-destroy -n $LXC_NAME1 -f -sudo lxc-snapshot -n $LXC_NAME2 -d snap0 -sudo rm -f /var/lib/lxcsnaps/$LXC_NAME2/snap0.tar.gz -sudo lxc-destroy -n $LXC_NAME2 -f - -echo -e "\e[1m> Suppression des crons\e[0m" -sudo rm /etc/cron.d/demo_switch -sudo rm /etc/cron.d/demo_upgrade - -echo -e "\e[1m> Suppression des clés ECDSA dans known_hosts\e[0m" -ssh-keygen -f "$HOME/.ssh/known_hosts" -R $IP_LXC1 -ssh-keygen -f "$HOME/.ssh/known_hosts" -R $IP_LXC2 - -echo -e "\e[1m> Suppression du service\e[0m" -sudo systemctl disable lxc_demo.service -sudo rm -f /etc/systemd/system/lxc_demo.service +ynh_print_info --message="Stopping and removing the systemd service..." +ynh_remove_systemd_config diff --git a/demo_lxc_remove.sh b/demo_lxc_remove.sh index 84e2e7d..a711611 100755 --- a/demo_lxc_remove.sh +++ b/demo_lxc_remove.sh @@ -6,33 +6,15 @@ # Récupère le dossier du script if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi -LXC_NAME1=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME1= | cut -d '=' -f2) -DOMAIN=$(cat "$script_dir/domain.ini") +source $script_dir/ynh_lxd +source /usr/share/yunohost/helpers -# Check user -if [ "$USER" != "$(cat "$script_dir/setup_user")" ]; then - echo -e "\e[91mCe script doit être exécuté avec l'utilisateur $(cat "$script_dir/setup_user")" - echo -en "\e[0m" - exit 0 -fi +app=${__APP__:-yunohost_demo} +final_path=$(ynh_app_setting_get --app=$app --key=final_path) +lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) +path_url=$(ynh_app_setting_get --app=$app --key=path) -"$script_dir/demo_lxc_destroy.sh" - -echo -e "\e[1m> Retire l'ip forwarding.\e[0m" -sudo rm /etc/sysctl.d/lxc_demo.conf -sudo sysctl -p - -echo -e "\e[1m> Supprime le brige réseau\e[0m" -sudo rm /etc/network/interfaces.d/lxc_demo - -echo -e "\e[1m> Remove lxc lxctl\e[0m" -sudo apt-get remove lxc lxctl - -echo -e "\e[1m> Suppression de la clé SSH\e[0m" -rm -f $HOME/.ssh/$LXC_NAME1 $HOME/.ssh/$LXC_NAME1.pub -echo -e "\e[1m> Et de sa config spécifique dans $HOME/.ssh/config\e[0m" -BEGIN_LINE=$(cat $HOME/.ssh/config | grep -n "^# ssh $LXC_NAME1" | cut -d':' -f 1) -sed -i "$BEGIN_LINE,/^# End ssh $LXC_NAME1/d" $HOME/.ssh/config +/bin/bash "$final_path/demo_lxc_destroy.sh" # Suppression du reverse proxy echo -e "\e[1m> Suppression de la config nginx\e[0m" diff --git a/demo_restore_crash.sh b/demo_restore_crash.sh index cf64b5c..fd5a52b 100755 --- a/demo_restore_crash.sh +++ b/demo_restore_crash.sh @@ -5,185 +5,90 @@ # Récupère le dossier du script if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi -PLAGE_IP=$(cat "$script_dir/demo_lxc_build.sh" | grep PLAGE_IP= | cut -d '=' -f2) -IP_LXC1=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC1= | cut -d '=' -f2) -IP_LXC2=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC2= | cut -d '=' -f2) -LXC_NAME1=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME1= | cut -d '=' -f2) -LXC_NAME2=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME2= | cut -d '=' -f2) +source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo +source /usr/share/yunohost/helpers -STOP_CONTAINER () { - MACHINE=$1 - if [ $(sudo lxc-info --name $MACHINE | grep -c "STOPPED") -eq 0 ]; then - echo "Arrêt du conteneur $MACHINE" - sudo lxc-stop -n $MACHINE - fi -} +app=${__APP__:-yunohost_demo} +final_path=$(ynh_app_setting_get --app=$app --key=final_path) +lxdbr_demo_network=$(ynh_app_setting_get --app=$app --key=lxdbr_demo_network) +lxc_ip1=$(ynh_app_setting_get --app=$app --key=lxc_ip1) +lxc_ip2=$(ynh_app_setting_get --app=$app --key=lxc_ip2) +lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) +lxc_name2=$(ynh_app_setting_get --app=$app --key=lxc_name2) -CHECK_CONTAINER () { - MACHINE=$1 - echo "Test du conteneur $MACHINE" - sudo lxc-start -n $MACHINE -d > /dev/null 2>&1 # Démarre le conteneur - sudo lxc-wait -n $MACHINE -s 'RUNNING' -t 20 # Attend pendant 20s maximum que le conteneur démarre -# sudo lxc-ls -f - if [ $(sudo lxc-info --name $MACHINE | grep -c "RUNNING") -ne 1 ]; then - return 1 # Renvoi 1 si le démarrage du conteneur a échoué - else - STOP_CONTAINER $MACHINE - return 0 # Renvoi 0 si le démarrage du conteneur a réussi - fi -} +ynh_print_info --message="Désactive le cron switch." +sed -i "s/.*demo_switch.sh/#&/" /etc/cron.d/demo_switch # Le cron est commenté durant l'opération de maintenance. -RESTORE_SNAPSHOT () { - MACHINE=$1 - echo -e "\e[1m> Restauration du snapshot du conteneur $MACHINE\e[0m" - sudo lxc-snapshot -r snap0 -n $MACHINE - CHECK_CONTAINER $MACHINE - STATUS=$? - if [ $STATUS -eq 1 ]; then - echo -e "\e[91m> Conteneur $MACHINE en défaut.\e[0m" - return 1 - else - echo -e "\e[92m> Conteneur $MACHINE en état de marche.\e[0m" - return 0 - fi -} +ynh_print_info --message="Suppression des lock et arrêt forcé des conteneurs." +ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileS" +ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileS" +ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileU" +ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileU" -RESTORE_ARCHIVE_SNAPSHOT () { - MACHINE=$1 - if ! test -e "/var/lib/lxcsnaps/$MACHINE/snap1.tar.gz"; then - echo "Aucune archive de snapshot pour le conteneur $MACHINE" - return 1 - fi - echo -e "\e[1m> Restauration du snapshot archivé pour le conteneur $MACHINE\e[0m" - echo "Suppression du snapshot" - sudo lxc-snapshot -n $MACHINE -d snap0 - echo "Décompression de l'archive" - sudo tar -x --acls --xattrs -f /var/lib/lxcsnaps/$MACHINE/snap0.tar.gz -C / - RESTORE_SNAPSHOT $MACHINE - return $? -} - -CLONE_CONTAINER () { - MACHINE_SOURCE=$1 - MACHINE_CIBLE=$2 - IP_SOURCE=$3 - IP_CIBLE=$4 - echo "Suppression du conteneur $MACHINE_CIBLE" - sudo lxc-snapshot -n $MACHINE_CIBLE -d snap0 - sudo rm -f /var/lib/lxcsnaps/$MACHINE_CIBLE/snap0.tar.gz - sudo lxc-destroy -n $MACHINE_CIBLE -f - - echo -e "\e[1m> Clone le conteneur $MACHINE_SOURCE sur $MACHINE_CIBLE\e[0m" - sudo lxc-copy --name=$MACHINE_SOURCE --newname=$MACHINE_CIBLE - - echo "Modification de l'ip du clone," - sudo sed -i "s@address $IP_SOURCE@address $IP_CIBLE@" /var/lib/lxc/$MACHINE_CIBLE/rootfs/etc/network/interfaces - echo "du nom du veth" - sudo sed -i "s@$MACHINE_SOURCE@$MACHINE_CIBLE@g" /var/lib/lxc/$MACHINE_CIBLE/config - echo "Et enfin renseigne /etc/hosts sur le clone" - sudo sed -i "s@^127.0.0.1 $MACHINE_SOURCE@127.0.0.1 $MACHINE_CIBLE@" /var/lib/lxc/$MACHINE_CIBLE/rootfs/etc/hosts - - CHECK_CONTAINER $MACHINE_CIBLE - STATUS=$? - if [ $STATUS -eq 1 ]; then - echo -e "\e[91m> Conteneur $MACHINE_CIBLE en défaut.\e[0m" - else - echo -e "\e[92m> Conteneur $MACHINE_CIBLE en état de marche.\e[0m" - echo "Création d'un nouveau snapshot pour le conteneur $MACHINE_CIBLE" - sudo lxc-snapshot -n $MACHINE_CIBLE - fi - return $STATUS -} - -echo "Désactive le cron switch." -sudo sed -i "s/.*demo_switch.sh/#&/" /etc/cron.d/demo_switch # Le cron est commenté durant l'opération de maintenance. - -echo "Suppression des lock et arrêt forcé des conteneurs." -sudo rm -f /var/lib/lxc/$LXC_NAME1.lock_fileS -sudo rm -f /var/lib/lxc/$LXC_NAME2.lock_fileS -sudo rm -f /var/lib/lxc/$LXC_NAME1.lock_fileU -sudo rm -f /var/lib/lxc/$LXC_NAME2.lock_fileU - -STOP_CONTAINER $LXC_NAME1 -STOP_CONTAINER $LXC_NAME2 - -echo "Initialisation du réseau pour le conteneur." -if ! sudo ifquery lxc_demo --state > /dev/null; then - sudo ifup lxc_demo --interfaces=/etc/network/interfaces.d/lxc_demo -fi - -# Activation des règles iptables -echo "Configure le parefeu" -if ! sudo iptables -D FORWARD -i lxc_demo -o eth0 -j ACCEPT 2> /dev/null; then - sudo iptables -A FORWARD -i lxc_demo -o eth0 -j ACCEPT -fi -if ! sudo iptables -C FORWARD -i eth0 -o lxc_demo -j ACCEPT 2> /dev/null; then - sudo iptables -A FORWARD -i eth0 -o lxc_demo -j ACCEPT -fi -if ! sudo iptables -t nat -C POSTROUTING -s $PLAGE_IP.0/24 -j MASQUERADE 2> /dev/null; then - sudo iptables -t nat -A POSTROUTING -s $PLAGE_IP.0/24 -j MASQUERADE -fi +ynh_lxc_stop_as_demo --name="$lxc_name1" +ynh_lxc_stop_as_demo --name="$lxc_name2" # Vérifie l'état des conteneurs. -CHECK_CONTAINER $LXC_NAME1 +ynh_lxc_check_container_start --name=$lxc_name1 LXC1_STATUS=$? -CHECK_CONTAINER $LXC_NAME2 +ynh_lxc_check_container_start --name=$lxc_name2 LXC2_STATUS=$? if [ $LXC1_STATUS -eq 1 ]; then - echo -e "\e[91m> Conteneur $LXC_NAME1 en défaut.\e[0m" + ynh_print_info --message="\e[91m> Conteneur $lxc_name1 en défaut.\e[0m" else - echo -e "\e[92m> Conteneur $LXC_NAME1 en état de marche.\e[0m" + ynh_print_info --message="\e[92m> Conteneur $lxc_name1 en état de marche.\e[0m" fi if [ $LXC2_STATUS -eq 1 ]; then - echo -e "\e[91m> Conteneur $LXC_NAME2 en défaut.\e[0m" + ynh_print_info --message="\e[91m> Conteneur $lxc_name2 en défaut.\e[0m" else - echo -e "\e[92m> Conteneur $LXC_NAME2 en état de marche.\e[0m" + ynh_print_info --message="\e[92m> Conteneur $lxc_name2 en état de marche.\e[0m" fi # Restauration des snapshots if [ $LXC1_STATUS -eq 1 ]; then - RESTORE_SNAPSHOT $LXC_NAME1 + ynh_lxc_restore_from_snapshot --name=$lxc_name1 LXC1_STATUS=$? fi if [ $LXC2_STATUS -eq 1 ]; then - RESTORE_SNAPSHOT $LXC_NAME2 + ynh_lxc_restore_from_snapshot --name=$lxc_name2 LXC2_STATUS=$? fi # Restauration des archives des snapshots if [ $LXC1_STATUS -eq 1 ]; then - RESTORE_ARCHIVE_SNAPSHOT $LXC_NAME1 + ynh_lxc_restore_from_archive --name=$lxc_name1 LXC1_STATUS=$? fi if [ $LXC2_STATUS -eq 1 ]; then - RESTORE_ARCHIVE_SNAPSHOT $LXC_NAME2 + ynh_lxc_restore_from_archive --name=$lxc_name2 LXC2_STATUS=$? fi # Si des erreurs persistent, tente de cloner depuis un conteneur sain if [ $LXC1_STATUS -eq 1 ] && [ $LXC2_STATUS -eq 0 ] ; then - CLONE_CONTAINER $LXC_NAME2 $LXC_NAME1 $IP_LXC2 $IP_LXC1 + ynh_lxc_clone --source=$lxc_name2 --destination=$lxc_name1 LXC1_STATUS=$? fi if [ $LXC2_STATUS -eq 1 ] && [ $LXC1_STATUS -eq 0 ]; then - CLONE_CONTAINER $LXC_NAME1 $LXC_NAME2 $IP_LXC1 $IP_LXC2 + ynh_lxc_clone --source=$lxc_name1 --destination=$lxc_name2 LXC2_STATUS=$? fi # Résultats finaux if [ $LXC1_STATUS -eq 1 ] || [ $LXC2_STATUS -eq 1 ]; then if [ $LXC1_STATUS -eq 1 ]; then - echo -e "\e[91m\n> Le conteneur $LXC_NAME1 n'a pas pu être réparé...\e[0m" + ynh_print_info --message="\e[91m\n> Le conteneur $lxc_name1 n'a pas pu être réparé...\e[0m" fi if [ $LXC2_STATUS -eq 1 ]; then - echo -e "\e[91m\n> Le conteneur $LXC_NAME2 n'a pas pu être réparé...\e[0m" + ynh_print_info --message="\e[91m\n> Le conteneur $lxc_name2 n'a pas pu être réparé...\e[0m" fi else - echo -e "\e[92m\n> Les 2 conteneurs sont sains et fonctionnels.\e[0m" + ynh_print_info --message="\e[92m\n> Les 2 conteneurs sont sains et fonctionnels.\e[0m" fi -echo "Réactive le cron switch." -sudo sed -i "s/#*\*/\*/" /etc/cron.d/demo_switch # Le cron est décommenté -echo "Restart la demo." -$script_dir/demo_start.sh +ynh_print_info --message="Réactive le cron switch." +sed -i "s/#*\*/\*/" /etc/cron.d/demo_switch # Le cron est décommenté +ynh_print_info --message="Restart la demo." +$final_path/demo_start.sh diff --git a/demo_start.sh b/demo_start.sh index 79fc30b..a2be13a 100755 --- a/demo_start.sh +++ b/demo_start.sh @@ -1,38 +1,25 @@ #!/bin/bash -# Démarre le premier conteneur de demo et active la config réseau dédiée. +# Démarre le premier conteneur de demo # Récupère le dossier du script if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi -PLAGE_IP=$(cat "$script_dir/demo_lxc_build.sh" | grep PLAGE_IP= | cut -d '=' -f2) -LXC_NAME=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME1= | cut -d '=' -f2) +source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo +source /usr/share/yunohost/helpers -"$script_dir/demo_stop.sh" > /dev/null 2>&1 +app=${__APP__:-yunohost_demo} +final_path=$(ynh_app_setting_get --app=$app --key=final_path) +lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) +lxdbr_demo_network=$(ynh_app_setting_get --app=$app --key=lxdbr_demo_network) +lxc_ip1=$(ynh_app_setting_get --app=$app --key=lxc_ip1) -echo "Initialisation du réseau pour le conteneur." -if ! sudo ifquery lxc_demo --state > /dev/null; then - sudo ifup lxc_demo --interfaces=/etc/network/interfaces.d/lxc_demo -fi - -# Activation des règles iptables -echo "> Configure le parefeu" -if ! sudo iptables -D FORWARD -i lxc_demo -o eth0 -j ACCEPT 2> /dev/null; then - sudo iptables -A FORWARD -i lxc_demo -o eth0 -j ACCEPT -fi -if ! sudo iptables -C FORWARD -i eth0 -o lxc_demo -j ACCEPT 2> /dev/null; then - sudo iptables -A FORWARD -i eth0 -o lxc_demo -j ACCEPT -fi -if ! sudo iptables -t nat -C POSTROUTING -s $PLAGE_IP.0/24 -j MASQUERADE 2> /dev/null; then - sudo iptables -t nat -A POSTROUTING -s $PLAGE_IP.0/24 -j MASQUERADE -fi +/bin/bash "$final_path/demo_stop.sh" > /dev/null 2>&1 # Démarrage de la machine -echo "> Démarrage de la machine" -date >> "$script_dir/demo_boot.log" -echo "Starting $LXC_NAME" >> "$script_dir/demo_boot.log" -sudo lxc-start -n $LXC_NAME -o "$script_dir/demo_boot.log" -d +ynh_print_info --message="> Démarrage de la machine" | tee -a "$final_path/demo_boot.log" +date | tee -a "$final_path/demo_boot.log" +ynh_print_info --message="Starting $lxc_name1" | tee -a "$final_path/demo_boot.log" +ynh_lxc_start_as_demo --name=$lxc_name1 --ip="$lxdbr_demo_network$lxc_ip1" | tee -a "$final_path/demo_boot.log" sleep 3 - -# Vérifie que la machine a démarré -sudo lxc-ls -f diff --git a/demo_stop.sh b/demo_stop.sh index 6408ad1..227cc15 100755 --- a/demo_stop.sh +++ b/demo_stop.sh @@ -3,50 +3,39 @@ # Stoppe les conteneurs de demo et arrête la config réseau dédiée. # Récupère le dossier du script -if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi +if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(ynh_print_info --message=$PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi -PLAGE_IP=$(cat "$script_dir/demo_lxc_build.sh" | grep PLAGE_IP= | cut -d '=' -f2) -LXC_NAME1=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME1= | cut -d '=' -f2) -LXC_NAME2=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME2= | cut -d '=' -f2) +source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo +source /usr/share/yunohost/helpers + +app=${__APP__:-yunohost_demo} +final_path=$(ynh_app_setting_get --app=$app --key=final_path) +lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) +lxc_name2=$(ynh_app_setting_get --app=$app --key=lxc_name2) if [ "$#" -eq 1 ] && [ "$1" == "-f" ] then - echo "> Suppression des lock et arrêt forcé des conteneurs." - sudo rm -f /var/lib/lxc/$LXC_NAME1.lock_fileS - sudo rm -f /var/lib/lxc/$LXC_NAME2.lock_fileS - sudo rm -f /var/lib/lxc/$LXC_NAME1.lock_fileU - sudo rm -f /var/lib/lxc/$LXC_NAME2.lock_fileU + ynh_print_info --message="> Suppression des lock et arrêt forcé des conteneurs." + ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileS" + ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileS" + ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileU" + ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileU" else - echo "> Attend la libération des lock sur les conteneurs." - while test -e /var/lib/lxc/$LXC_NAME1.lock_file* || test -e /var/lib/lxc/$LXC_NAME2.lock_file*; do + ynh_print_info --message="> Attend la libération des lock sur les conteneurs." + while test -e /var/lib/lxd/$lxc_name1.lock_file* || test -e /var/lib/lxd/$lxc_name2.lock_file*; do sleep 5 # Attend que les conteneur soit libérés par les script upgrade ou switch, le cas échéant. done fi -echo "> Arrêt des conteneurs" -if [ $(sudo lxc-info --name $LXC_NAME1 | grep -c "STOPPED") -eq 0 ]; then - echo "Arrêt du conteneur $LXC_NAME1" - sudo lxc-stop -n $LXC_NAME1 +ynh_print_info --message="> Arrêt des conteneurs" +if ! ynh_lxc_is_stopped --name=$lxc_name1 +then + ynh_print_info --message="Arrêt du conteneur $lxc_name1" + ynh_lxc_stop_as_demo --name=$lxc_name1 fi -if [ $(sudo lxc-info --name $LXC_NAME2 | grep -c "STOPPED") -eq 0 ]; then - echo "Arrêt du conteneur $LXC_NAME2" - sudo lxc-stop -n $LXC_NAME2 +if ! ynh_lxc_is_stopped --name=$lxc_name2 +then + ynh_print_info --message="Arrêt du conteneur $lxc_name2" + ynh_lxc_stop_as_demo --name=$lxc_name2 fi - -echo "> Suppression des règles de parefeu" -if sudo iptables -C FORWARD -i lxc_demo -o eth0 -j ACCEPT 2> /dev/null; then - sudo iptables -D FORWARD -i lxc_demo -o eth0 -j ACCEPT -fi -if sudo iptables -C FORWARD -i eth0 -o lxc_demo -j ACCEPT 2> /dev/null; then - sudo iptables -D FORWARD -i eth0 -o lxc_demo -j ACCEPT -fi -if sudo iptables -t nat -C POSTROUTING -s $PLAGE_IP.0/24 -j MASQUERADE 2> /dev/null; then - sudo iptables -t nat -D POSTROUTING -s $PLAGE_IP.0/24 -j MASQUERADE -fi - -echo "Arrêt de l'interface réseau pour le conteneur." -if sudo ifquery lxc_demo --state > /dev/null; then - sudo ifdown --force lxc_demo -fi - -sudo lxc-ls -f diff --git a/demo_switch.sh b/demo_switch.sh index f1a220c..9ad9f3c 100755 --- a/demo_switch.sh +++ b/demo_switch.sh @@ -6,56 +6,69 @@ # Récupère le dossier du script if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi -PLAGE_IP=$(cat "$script_dir/demo_lxc_build.sh" | grep PLAGE_IP= | cut -d '=' -f2) -LXC_NAME1=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME1= | cut -d '=' -f2) -LXC_NAME2=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME2= | cut -d '=' -f2) -MAIL_ADDR=$(cat "$script_dir/demo_lxc_build.sh" | grep MAIL_ADDR= | cut -d '=' -f2) -DOMAIN=$(cat "$script_dir/domain.ini") +source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo +source /usr/share/yunohost/helpers -log_line=$(wc -l "$script_dir/demo_switch.log" | cut -d ' ' -f 1) # Repère la fin du log actuel. Pour récupérer les lignes ajoutées sur cette exécution. +app=${__APP__:-yunohost_demo} +final_path=$(ynh_app_setting_get --app=$app --key=final_path) +lxdbr_demo_network=$(ynh_app_setting_get --app=$app --key=lxdbr_demo_network) +lxc_ip1=$(ynh_app_setting_get --app=$app --key=lxc_ip1) +lxc_ip2=$(ynh_app_setting_get --app=$app --key=lxc_ip2) +lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) +lxc_name2=$(ynh_app_setting_get --app=$app --key=lxc_name2) +domain=${$(ynh_app_setting_get --app=$app --key=domain):-$(cat "$final_path/domain.ini")} +path_url=$(ynh_app_setting_get --app=$app --key=path) + +log_line=$(wc -l "$final_path/demo_switch.log" | cut -d ' ' -f 1) # Repère la fin du log actuel. Pour récupérer les lignes ajoutées sur cette exécution. log_line=$(( $log_line + 1 )) # Ignore la première ligne, reprise de l'ancien log. -date >> "$script_dir/demo_switch.log" +date >> "$final_path/demo_switch.log" -while test -e /var/lib/lxc/$LXC_NAME1.lock_file* || test -e /var/lib/lxc/$LXC_NAME2.lock_file*; do +while test -e /var/lib/lxd/$lxc_name1.lock_file* || test -e /var/lib/lxd/$lxc_name2.lock_file*; do sleep 5 # Attend que le conteneur soit libéré par les script upgrade ou switch, le cas échéant. done # Vérifie l'état des machines. -if [ "$(sudo lxc-info --name $LXC_NAME1 | grep -c "RUNNING")" -eq "1" ] +if ynh_lxc_is_started --name=$lxc_name1 then # Si la machine 1 est démarrée. - LXC_A=$LXC_NAME1 - LXC_B=$LXC_NAME2 + LXC_A=$lxc_name1 + IP_A="$lxdbr_demo_network$lxc_ip1" + LXC_B=$lxc_name2 + IP_B="$lxdbr_demo_network$lxc_ip2" else # Sinon, on suppose que c'est la machine 2 qui est en cours. - LXC_A=$LXC_NAME2 - LXC_B=$LXC_NAME1 + LXC_A=$lxc_name2 + IP_A="$lxdbr_demo_network$lxc_ip2" + LXC_B=$lxc_name1 + IP_B="$lxdbr_demo_network$lxc_ip1" # Si aucune machine ne tourne, la première démarrera. fi # Supprime les éventuels swap présents. -/sbin/swapoff /var/lib/lxc/$LXC_A/rootfs/swap_* +/sbin/swapoff /var/lib/lxd/$LXC_A/rootfs/swap_* -echo "Starting $LXC_B" +ynh_print_info --message="Starting $LXC_B" # Démarre le conteneur B et arrête le conteneur A. -sudo lxc-start -n $LXC_B -o "$script_dir/demo_switch.log" -d > /dev/null # Démarre l'autre machine -sleep 10 # Attend 10 seconde pour s'assurer du démarrage de la machine. -if [ "$(sudo lxc-info --name $LXC_B | grep -c "STOPPED")" -eq "1" ] +ynh_lxc_start_as_demo --name=$LXC_B --ip=$IP_B +sleep 5 # Attend 10 seconde pour s'assurer du démarrage de la machine. +if ! ynh_lxc_is_started --name=$LXC_B then # Le conteneur n'a pas réussi à démarrer. On averti un responsable par mail... - echo -e "Échec du démarrage du conteneur $LXC_B sur le serveur de demo $DOMAIN! \n\nExtrait du log:\n$(tail -n +$log_line "$script_dir/demo_switch.log")\n\nLe script 'demo_restore_crash.sh' va être exécuté pour tenter de fixer l'erreur." | mail -a "Content-Type: text/plain; charset=UTF-8" -s "Demo Yunohost" $MAIL_ADDR - $script_dir/demo_restore_crash.sh & + echo -e "Échec du démarrage du conteneur $LXC_B sur le serveur de demo $DOMAIN! \n\nExtrait du log:\n$(tail -n +$log_line "$final_path/demo_switch.log")\n\nLe script 'demo_restore_crash.sh' va être exécuté pour tenter de fixer l'erreur." | mail -a "Content-Type: text/plain; charset=UTF-8" -s "Demo Yunohost" $MAIL_ADDR + /bin/bash $final_path/demo_restore_crash.sh & exit 1 else - echo "Stopping $LXC_A" + ynh_print_info --message="Stopping $LXC_A" # Bascule sur le conteneur B avec le load balancing de nginx... # Automatique par nginx lorsque la machine A sera éteinte. # Arrêt du conteneur A. Il est remplacé par le B - sudo touch /var/lib/lxc/$LXC_A.lock_fileS # Met en place un fichier pour indiquer que la machine n'est pas encore dispo. - sudo lxc-stop -n $LXC_A + touch /var/lib/lxd/$LXC_A.lock_fileS # Met en place un fichier pour indiquer que la machine n'est pas encore dispo. + ynh_lxc_stop_as_demo --name=$LXC_A # Supprime les éventuels swap présents. - /sbin/swapoff /var/lib/lxc/$LXC_A/rootfs/swap_* - echo "Restauring $LXC_A from snapshot" + /sbin/swapoff /var/lib/lxd/$LXC_A/rootfs/swap_* + ynh_print_info --message="Restauring $LXC_A from snapshot" # Restaure le snapshot de la machine A avant sa prochaine exécution - sudo lxc-snapshot -r snap0 -n $LXC_A - sudo rm /var/lib/lxc/$LXC_A.lock_fileS # Libère le lock - echo "Finish restoring $LXC_A" + ynh_lxc_load_snapshot --name=$LXC_A --snapname=snap0 + ynh_lxc_stop --name=$LXC_A + ynh_secure_remove --file="/var/lib/lxd/$LXC_A.lock_fileS" # Libère le lock + ynh_print_info --message="Finish restoring $LXC_A" fi diff --git a/demo_upgrade.sh b/demo_upgrade.sh index ba2a952..87b3e4b 100755 --- a/demo_upgrade.sh +++ b/demo_upgrade.sh @@ -6,163 +6,27 @@ # Récupère le dossier du script if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi -LXC_NAME1=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME1= | cut -d '=' -f2) -LXC_NAME2=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME2= | cut -d '=' -f2) -IP_LXC1=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC1= | cut -d '=' -f2) -IP_LXC2=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC2= | cut -d '=' -f2) -PLAGE_IP=$(cat "$script_dir/demo_lxc_build.sh" | grep PLAGE_IP= | cut -d '=' -f2) -TIME_TO_SWITCH=$(cat "$script_dir/demo_lxc_build.sh" | grep TIME_TO_SWITCH= | cut -d '=' -f2) -MAIL_ADDR=$(cat "$script_dir/demo_lxc_build.sh" | grep MAIL_ADDR= | cut -d '=' -f2) -DOMAIN=$(cat "$script_dir/domain.ini") +source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo +source /usr/share/yunohost/helpers -IP_UPGRADE=$PLAGE_IP.150 +app=${__APP__:-yunohost_demo} +final_path=$(ynh_app_setting_get --app=$app --key=final_path) +domain=$(ynh_app_setting_get --app=$app --key=domain) +lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) +lxc_name2=$(ynh_app_setting_get --app=$app --key=lxc_name2) +time_to_switch=$(ynh_app_setting_get --app=$app --key=time_to_switch) + +IP_UPGRADE=$lxdbr_demo_network.150 LOOP=0 -log_line=$(wc -l "$script_dir/demo_upgrade.log" | cut -d ' ' -f 1) # Repère la fin du log actuel. Pour récupérer les lignes ajoutées sur cette exécution. +log_line=$(wc -l "$final_path/demo_upgrade.log" | cut -d ' ' -f 1) # Repère la fin du log actuel. Pour récupérer les lignes ajoutées sur cette exécution. log_line=$(( $log_line + 1 )) # Ignore la première ligne, reprise de l'ancien log. -date >> "$script_dir/demo_upgrade.log" +date >> "$final_path/demo_upgrade.log" -UPGRADE_DEMO_CONTAINER () { # Démarrage, upgrade et snapshot - MACHINE=$1 - IP_MACHINE=$2 - echo "Upgrading $MACHINE" - # Attend que la machine soit éteinte. - # Timeout à $TIME_TO_SWITCH +5 minutes, en seconde - TIME_OUT=$(($TIME_TO_SWITCH * 60 + 300)) - sudo lxc-wait -n $MACHINE -s 'STOPPED' -t $TIME_OUT - while test -e /var/lib/lxc/$MACHINE.lock_fileS; do - sleep 5 # Attend que le conteneur soit libéré par le script switch. - done - - sudo touch /var/lib/lxc/$MACHINE.lock_fileU # Met en place un fichier pour indiquer que la machine est indisponible pendant l'upgrade - - # Supprime les éventuels swap présents. - /sbin/swapoff /var/lib/lxc/$MACHINE/rootfs/swap_* - - # Restaure le snapshot - sudo lxc-snapshot -r snap0 -n $MACHINE - - # Change l'ip du conteneur le temps de l'upgrade. Pour empêcher HAProxy de basculer sur le conteneur. - sudo sed -i "s@address $IP_MACHINE@address $IP_UPGRADE@" /var/lib/lxc/$MACHINE/rootfs/etc/network/interfaces - - # Active le bridge réseau - if ! sudo ifquery lxc_demo --state > /dev/null - then - sudo ifup lxc_demo --interfaces=/etc/network/interfaces.d/lxc_demo - fi - - # Configure le parefeu - if ! sudo iptables -D FORWARD -i lxc_demo -o eth0 -j ACCEPT 2> /dev/null - then - sudo iptables -A FORWARD -i lxc_demo -o eth0 -j ACCEPT - fi - if ! sudo iptables -C FORWARD -i eth0 -o lxc_demo -j ACCEPT 2> /dev/null - then - sudo iptables -A FORWARD -i eth0 -o lxc_demo -j ACCEPT - fi - if ! sudo iptables -t nat -C POSTROUTING -s $PLAGE_IP.0/24 -j MASQUERADE 2> /dev/null - then - sudo iptables -t nat -A POSTROUTING -s $PLAGE_IP.0/24 -j MASQUERADE - fi - - # Démarre le conteneur - date >> "$script_dir/demo_boot.log" - sudo lxc-start -n $MACHINE -o "$script_dir/demo_boot.log" -d > /dev/null - sleep 10 - - # Update - update_apt=0 - sudo lxc-attach -n $MACHINE -- apt-get update - sudo lxc-attach -n $MACHINE -- apt-get dist-upgrade --dry-run | grep -q "^Inst " > /dev/null # Vérifie si il y aura des mises à jour. - if [ "$?" -eq 0 ]; then - date - update_apt=1 - # Upgrade - sudo lxc-attach -n $MACHINE -- apt-get dist-upgrade --option Dpkg::Options::=--force-confold -yy - # Clean - sudo lxc-attach -n $MACHINE -- apt-get autoremove -y - sudo lxc-attach -n $MACHINE -- apt-get autoclean - fi - sudo lxc-attach -n $MACHINE -- yunohost tools update - sudo lxc-attach -n $MACHINE -- yunohost tools upgrade system - - # Exécution des scripts de upgrade.d - LOOP=$((LOOP + 1)) - while read LIGNE - do - if [ ! "$LIGNE" == "exemple" ] && [ ! "$LIGNE" == "old_scripts" ] && [ ! "$LIGNE" == "Constant_upgrade" ] && ! echo "$LIGNE" | grep -q ".fail$" # Le fichier exemple, le dossier old_scripts et les scripts fail sont ignorés - then - date - # Exécute chaque script trouvé dans upgrade.d - echo "Exécution du script $LIGNE sur le conteneur $MACHINE" - /bin/bash "$script_dir/upgrade.d/$LIGNE" $MACHINE - if [ "$?" -ne 0 ]; then # Si le script a échoué, le snapshot est annulé. - echo "Échec du script $LIGNE" - mv -f "$script_dir/upgrade.d/$LIGNE" "$script_dir/upgrade.d/$LIGNE.fail" - echo -e "Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $MACHINE sur le serveur de demo $DOMAIN!\nLe script a été renommé en .fail, il ne sera plus exécuté tant que le préfixe ne sera pas retiré.\n\nExtrait du log:\n$(tail -n +$log_line "$script_dir/demo_upgrade.log")" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "Demo Yunohost" $MAIL_ADDR - update_apt=0 - else - echo "Le script $LIGNE a été exécuté sans erreur" - update_apt=1 - fi - fi - done <<< "$(ls -1 "$script_dir/upgrade.d")" - - # Exécution des scripts de upgrade.d/Constant_upgrade - while read LIGNE - do - if [ "$update_apt" -eq "1" ] - then - date - # Exécute chaque script trouvé dans upgrade.d/Constant_upgrade - echo "Exécution du script $LIGNE sur le conteneur $MACHINE" - /bin/bash "$script_dir/upgrade.d/Constant_upgrade/$LIGNE" $MACHINE - if [ "$?" -ne 0 ]; then - echo "Échec du script $LIGNE" - echo -e "Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $MACHINE sur le serveur de demo $DOMAIN!\n" - else - echo "Le script $LIGNE a été exécuté sans erreur" - fi - fi - done <<< "$(ls -1 "$script_dir/upgrade.d/Constant_upgrade")" - - # Upgrade des apps - sudo lxc-attach -n $MACHINE -- yunohost tools update - sudo lxc-attach -n $MACHINE -- systemctl restart nginx - sudo lxc-attach -n $MACHINE -- yunohost tools upgrade apps - sudo lxc-attach -n $MACHINE -- systemctl restart nginx - - # Arrêt de la machine virtualisée - sudo lxc-stop -n $MACHINE - - # Restaure l'ip d'origine du conteneur. - sudo sed -i "s@address $IP_UPGRADE@address $IP_MACHINE@" /var/lib/lxc/$MACHINE/rootfs/etc/network/interfaces - - if [ "$update_apt" -eq "1" ] - then - # Archivage du snapshot - sudo tar -cz --acls --xattrs -f /var/lib/lxcsnaps/$MACHINE/snap0.tar.gz /var/lib/lxcsnaps/$MACHINE/snap0 - # Remplacement du snapshot - sudo lxc-snapshot -n $MACHINE -d snap0 - sudo lxc-snapshot -n $MACHINE - - if [ "$LOOP" -eq 2 ] - then # Après l'upgrade du 2e conteneur, déplace les scripts dans le dossier des anciens scripts si ils ont été exécutés avec succès. - ls -1 "$script_dir/upgrade.d" | while read LIGNE - do - if [ ! "$LIGNE" == "exemple" ] && [ ! "$LIGNE" == "old_scripts" ] && [ ! "$LIGNE" == "Constant_upgrade" ] && ! echo "$LIGNE" | grep -q ".fail$" # Le fichier exemple, le dossier old_scripts et les scripts fail sont ignorés - then - mv -f "$script_dir/upgrade.d/$LIGNE" "$script_dir/upgrade.d/old_scripts/$LIGNE" - fi - done - fi - fi - sudo rm /var/lib/lxc/$MACHINE.lock_fileU # Libère le lock, la machine est à nouveau disponible - echo "Finished upgrading $MACHINE" -} - -echo "" +ynh_print_info --message="Starting upgrade..." date -UPGRADE_DEMO_CONTAINER $LXC_NAME1 $IP_LXC1 -UPGRADE_DEMO_CONTAINER $LXC_NAME2 $IP_LXC2 +ynh_lxc_upgrade_demo --name=$lxc_name1 --time_to_switch=$time_to_switch +ynh_lxc_upgrade_demo --name=$lxc_name2 --time_to_switch=$time_to_switch +ynh_print_info --message="Upgrade finished..." diff --git a/upgrade.d/Constant_upgrade/Default_login b/upgrade.d/Constant_upgrade/Default_login index e9000f0..61e6285 100644 --- a/upgrade.d/Constant_upgrade/Default_login +++ b/upgrade.d/Constant_upgrade/Default_login @@ -3,12 +3,23 @@ # Récupère le dossier du script if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$PWD/$(dirname "$0" | cut -d '.' -f2)"; fi -MACHINE=$1 # Nom du conteneur +source $script_dir/../../ynh_lxd +source /usr/share/yunohost/helpers +app=${__APP__:-yunohost_demo} +final_path=$(ynh_app_setting_get --app=$app --key=final_path) +demo_user=$(ynh_app_setting_get --app=$app --key=demo_user) +demo_password=$(ynh_app_setting_get --app=$app --key=demo_password) + +MACHINE=$1 # Nom du conteneur + +# Active le mode démo de la webadmin / TO BE IMPLEMENTED +#ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost settings set demo 1" # Indique le couple login/mot de passe demo/demo # Et ajoute demo/demo par défaut dans les champs d'identification -sed -i "s/id=\"user\" type=\"text\" name=\"user\"/id=\"user\" type=\"text\" name=\"user\" value=\"demo\"/" /var/lib/lxc/$MACHINE/rootfs/usr/share/ssowat/portal/login.html -sed -i "s/id=\"password\" type=\"password\" name=\"password\"/id=\"password\" type=\"password\" name=\"password\" value=\"demo\"/" /var/lib/lxc/$MACHINE/rootfs/usr/share/ssowat/portal/login.html - -#sed -i "17i\   Password: $YUNO_PWD" /var/lib/lxc/$MACHINE/rootfs/usr/share/yunohost/admin/views/login.ms # Et sur le login admin -#sed -i "s/type=\"password\" id=\"password\" name=\"password\"/type=\"password\" id=\"password\" name=\"password\" value=\"$YUNO_PWD\"/" /var/lib/lxc/$MACHINE/rootfs/usr/share/yunohost/admin/views/login.ms +ynh_lxc_run_inside --name="$MACHINE" --command="sed -i \"3i\
Login: $demo_user / Password: $demo_password
\" /usr/share/ssowat/portal/login.html" # Sur le login du portail +ynh_lxc_run_inside --name="$MACHINE" --command="sed -i \"s/id=\"user\" type=\"text\" name=\"user\"/id=\"user\" type=\"text\" name=\"user\" value=\"$demo_user\"/\" /usr/share/ssowat/portal/login.html" +ynh_lxc_run_inside --name="$MACHINE" --command="sed -i \"s/id=\"password\" type=\"password\" name=\"password\"/id=\"password\" type=\"password\" name=\"password\" value=\"$demo_password\"/\" /usr/share/ssowat/portal/login.html" +# ainsi que dans la webadmin +#sed -i "17i\   Password: $YUNO_PWD" /var/lib/lxd/yunohost_demo1/rootfs/usr/share/yunohost/admin/views/login.ms # Et sur le login admin +#sed -i "s/type=\"password\" id=\"password\" name=\"password\"/type=\"password\" id=\"password\" name=\"password\" value=\"$YUNO_PWD\"/" /var/lib/lxd/yunohost_demo1/rootfs/usr/share/yunohost/admin/views/login.ms diff --git a/upgrade.d/Constant_upgrade/Disable_add_domain b/upgrade.d/Constant_upgrade/Disable_add_domain index 50a5b0a..658d11e 100644 --- a/upgrade.d/Constant_upgrade/Disable_add_domain +++ b/upgrade.d/Constant_upgrade/Disable_add_domain @@ -3,7 +3,7 @@ # Récupère le dossier du script if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$PWD/$(dirname "$0" | cut -d '.' -f2)"; fi -MACHINE=$1 # Nom du conteneur +MACHINE=$1 # Nom du conteneur # Désactive l'ajout de domaine, pour éviter surtout les nohost -#sed -i "s@@@g" /var/lib/lxc/$MACHINE/rootfs/usr/share/yunohost/admin/views/domain/domain_add.ms +#sed -i "s@@@g" /var/lib/lxd/yunohost_demo1/rootfs/usr/share/yunohost/admin/views/domain/domain_add.ms diff --git a/upgrade.d/Constant_upgrade/Disable_custom_apps b/upgrade.d/Constant_upgrade/Disable_custom_apps index fc64630..9efc84c 100644 --- a/upgrade.d/Constant_upgrade/Disable_custom_apps +++ b/upgrade.d/Constant_upgrade/Disable_custom_apps @@ -3,7 +3,7 @@ # Récupère le dossier du script if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$PWD/$(dirname "$0" | cut -d '.' -f2)"; fi -MACHINE=$1 # Nom du conteneur +MACHINE=$1 # Nom du conteneur # Désactive l'installation d'app custom -#sed -i "s@{{t 'install'}}@{{t 'install'}}@g" /var/lib/lxc/$MACHINE/rootfs/usr/share/yunohost/admin/views/app/app_catalog_category.ms +#sed -i "s@{{t 'install'}}@{{t 'install'}}@g" /var/lib/lxd/yunohost_demo1/rootfs/usr/share/yunohost/admin/views/app/app_catalog_category.ms diff --git a/ynh_lxd_demo b/ynh_lxd_demo new file mode 100644 index 0000000..a7fc044 --- /dev/null +++ b/ynh_lxd_demo @@ -0,0 +1,177 @@ +#!/bin/bash + +#================================================= +# Demo helpers +#================================================= + +# Start an LXC container in demo mode +# +# usage: ynh_lxc_start_as_demo --name=name --ip=ip +# | arg: -n, --name= - name of the LXC +# | arg: -n, --ip= - demo ip of the lxc +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_start_as_demo() { + # Declare an array to define the options of this helper. + local legacy_args=ni + local -A args_array=([n]=name= [i]=ip=) + local name + local ip + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_lxc_stop --name="$name" + lxc config device set $name eth1 ipv4.address $ip + _ynh_lxc_start_and_wait --name="$name" +} + +# Stop an LXC container in demo mode +# +# usage: ynh_lxc_stop_as_demo --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_stop_as_demo() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_lxc_stop --name="$name" + lxc config device unset $name eth1 ipv4.address +} + +# Upgrading demo container +# +# usage: ynh_lxc_upgrade_demo --name=name --time_to_switch=time_to_switch +# | arg: -n, --name= - name of the LXC +# | arg: -t, --time_to_switch= - time to switch +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_upgrade_demo() { + # Declare an array to define the options of this helper. + local legacy_args=nt + local -A args_array=([n]=name= [t]=time_to_switch=) + local name + local time_to_switch + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_print_info --message="Upgrading $name" + # Attend que la machine soit éteinte. + # Timeout à $time_to_switch +5 minutes, en seconde + TIME_OUT=$(($time_to_switch * 60 + 300)) + wait_period=0 + while ! ynh_lxc_is_stopped --name=$name + do + wait_period=$(($wait_period+10)) + if [ $wait_period -gt $TIME_OUT ];then + break + else + sleep 5 + fi + done + + while test -e /var/lib/lxd/$name.lock_fileS; do + sleep 5 # Attend que le conteneur soit libéré par le script switch. + done + + touch /var/lib/lxd/$name.lock_fileU # Met en place un fichier pour indiquer que la machine est indisponible pendant l'upgrade + + # Supprime les éventuels swap présents. + /sbin/swapoff /var/lib/lxd/$name/rootfs/swap_* + + # Restaure le snapshot + ynh_lxc_load_snapshot --name=$name --snapname=snap0 + + # Démarre le conteneur + date >> "$final_path/demo_boot.log" + + # Update + update_apt=0 + ynh_lxc_run_inside --name="$name" --command="apt-get update" + ynh_lxc_run_inside --name="$name" --command="apt-get dist-upgrade --dry-run | grep -q "^Inst " > /dev/null" # Vérifie si il y aura des mises à jour. + if [ "$?" -eq 0 ]; then + date + update_apt=1 + # Upgrade + ynh_lxc_run_inside --name="$name" --command="apt-get dist-upgrade --option Dpkg::Options::=--force-confold -yy" + # Clean + ynh_lxc_run_inside --name="$name" --command="apt-get autoremove -y" + ynh_lxc_run_inside --name="$name" --command="apt-get autoclean" + fi + ynh_lxc_run_inside --name="$name" --command="yunohost tools update" + ynh_lxc_run_inside --name="$name" --command="yunohost tools upgrade system" + + # Exécution des scripts de upgrade.d + LOOP=$((LOOP + 1)) + while read LIGNE + do + if [ ! "$LIGNE" == "exemple" ] && [ ! "$LIGNE" == "old_scripts" ] && [ ! "$LIGNE" == "Constant_upgrade" ] && ! echo "$LIGNE" | grep -q ".fail$" # Le fichier exemple, le dossier old_scripts et les scripts fail sont ignorés + then + date + # Exécute chaque script trouvé dans upgrade.d + ynh_print_info --message="Exécution du script $LIGNE sur le conteneur $name" + /bin/bash "$final_path/upgrade.d/$LIGNE" $name + if [ "$?" -ne 0 ]; then # Si le script a échoué, le snapshot est annulé. + ynh_print_info --message="Échec du script $LIGNE" + mv -f "$final_path/upgrade.d/$LIGNE" "$final_path/upgrade.d/$LIGNE.fail" + echo -e "Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\nLe script a été renommé en .fail, il ne sera plus exécuté tant que le préfixe ne sera pas retiré.\n\nExtrait du log:\n$(tail -n +$log_line "$script_dir/demo_upgrade.log")" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "Demo Yunohost" $MAIL_ADDR + update_apt=0 + else + ynh_print_info --message="Le script $LIGNE a été exécuté sans erreur" + update_apt=1 + fi + fi + done <<< "$(ls -1 "$final_path/upgrade.d")" + + # Exécution des scripts de upgrade.d/Constant_upgrade + while read LIGNE + do + if [ "$update_apt" -eq "1" ] + then + date + # Exécute chaque script trouvé dans upgrade.d/Constant_upgrade + ynh_print_info --message="Exécution du script $LIGNE sur le conteneur $name" + /bin/bash "$final_path/upgrade.d/Constant_upgrade/$LIGNE" $name + if [ "$?" -ne 0 ]; then + ynh_print_info --message="Échec du script $LIGNE" + echo -e "Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\n" + else + ynh_print_info --message="Le script $LIGNE a été exécuté sans erreur" + fi + fi + done <<< "$(ls -1 "$final_path/upgrade.d/Constant_upgrade")" + + # Upgrade des apps + ynh_lxc_run_inside --name="$name" --command="yunohost tools update" + ynh_lxc_run_inside --name="$name" --command="systemctl restart nginx" + ynh_lxc_run_inside --name="$name" --command="yunohost tools upgrade apps" + ynh_lxc_run_inside --name="$name" --command="systemctl restart nginx" + + # Arrêt de la machine virtualisée + ynh_lxc_stop --name=$name + + if [ "$update_apt" -eq "1" ] + then + # Archivage du snapshot + tar -cz --acls --xattrs -f /var/lib/lxd/snapshots/$name/snap0.tar.gz /var/lib/lxd/snapshots/$name/snap0 + # Remplacement du snapshot + ynh_lxc_create_snapshot --name=$name --snapname=snap0 + + if [ "$LOOP" -eq 2 ] + then # Après l'upgrade du 2e conteneur, déplace les scripts dans le dossier des anciens scripts si ils ont été exécutés avec succès. + ls -1 "$final_path/upgrade.d" | while read LIGNE + do + if [ ! "$LIGNE" == "exemple" ] && [ ! "$LIGNE" == "old_scripts" ] && [ ! "$LIGNE" == "Constant_upgrade" ] && ! echo "$LIGNE" | grep -q ".fail$" # Le fichier exemple, le dossier old_scripts et les scripts fail sont ignorés + then + mv -f "$final_path/upgrade.d/$LIGNE" "$final_path/upgrade.d/old_scripts/$LIGNE" + fi + done + fi + fi + ynh_secure_remove --file="/var/lib/lxd/$name.lock_fileU" # Libère le lock, la machine est à nouveau disponible + ynh_print_info --message="Finished upgrading $name" +} From 7524c0f4b29ae2a70587ccba88773e74866994f0 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Thu, 13 Oct 2022 06:17:08 +0200 Subject: [PATCH 06/25] Allow to install apps --- demo_lxc_build.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/demo_lxc_build.sh b/demo_lxc_build.sh index d8a49d8..8b8aa23 100755 --- a/demo_lxc_build.sh +++ b/demo_lxc_build.sh @@ -69,8 +69,6 @@ ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost -v" | tee -a "$LOG_BU # ******** ynh_print_info --message="\e[1m>> Modification de Yunohost pour la demo\e[0m" | tee -a "$LOG_BUILD_LXC" -if [ ! -z "$PACKAGE_CHECK_EXEC" ] -then # App officielles ynh_print_info --message="\e[1m> Installation des applications officielles\e[0m" | tee -a "$LOG_BUILD_LXC" # Ampache @@ -144,7 +142,7 @@ ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wordpress # Zerobin ynh_print_info --message="\e[36mInstallation de zerobin\e[0m" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install zerobin --force --args \"domain=$domain&path=/zerobin&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" -fi + # ******** ynh_print_info --message="\e[1m> Création d'un snapshot\e[0m" | tee -a "$LOG_BUILD_LXC" From b94c117d2bd4659cc969f65fe7eab68ef1abdb15 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 14 Oct 2022 05:02:08 +0200 Subject: [PATCH 07/25] Cleanup --- demo_lxc_build.sh | 84 +++++++++++++++++++++--------------------- demo_lxc_build_init.sh | 18 ++++----- demo_lxc_destroy.sh | 6 +-- demo_lxc_remove.sh | 4 +- demo_restore_crash.sh | 14 +++---- 5 files changed, 63 insertions(+), 63 deletions(-) diff --git a/demo_lxc_build.sh b/demo_lxc_build.sh index 8b8aa23..ce5a3f9 100755 --- a/demo_lxc_build.sh +++ b/demo_lxc_build.sh @@ -34,90 +34,90 @@ LOG_BUILD_LXC="$final_path/$LOG" if $(ynh_lxc_exists --name="$name") then # Si le conteneur existe déjà - ynh_print_info --message="\e[1m> Suppression du conteneur existant.\e[0m" | tee -a "$LOG_BUILD_LXC" + ynh_print_info --message="> Suppression du conteneur existant." | tee -a "$LOG_BUILD_LXC" /bin/bash "$final_path/demo_lxc_destroy.sh" quiet | tee -a "$LOG_BUILD_LXC" fi -ynh_print_info --message="\e[1m> Création d'une machine debian $DIST minimaliste\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Création d'une machine debian $DIST minimaliste" | tee -a "$LOG_BUILD_LXC" ynh_lxc_create --image="$lxc_base" --name="$lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message= "\e[1m> Active le bridge réseau\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message= "> Active le bridge réseau" | tee -a "$LOG_BUILD_LXC" lxc network attach $lxdbr_demo_name $lxc_name1 eth1 eth1 | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message="\e[1m> Configuration réseau de la machine virtualisée\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Configuration réseau de la machine virtualisée" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="cp /etc/systemd/network/eth0.network /etc/systemd/network/eth1.network" ynh_lxc_run_inside --name="$lxc_name1" --command="sed -i s/eth0/eth1/g /etc/systemd/network/eth1.network" -ynh_print_info --message="\e[1m> Update de la machine virtualisée\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Update de la machine virtualisée" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y update" ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y full-upgrade" ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y autoremove" ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y clean" -ynh_print_info --message="\e[1m> Post install Yunohost\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Post install Yunohost" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost tools postinstall --domain $domain --password $yunohost_password --force-password" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message="\e[1m> Disable password strength\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Disable password strength" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost settings set security.password.user.strength -v -1" | tee -a "$LOG_BUILD_LXC" -ynh_print_info --message="\e[1m> Ajout de l'utilisateur de demo\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Ajout de l'utilisateur de demo" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost user create $demo_user --firstname $demo_user --lastname $demo_user --domain $domain --password $demo_password" | tee -a "$LOG_BUILD_LXC" -ynh_print_info --message="\e[1m\n> Vérification de l'état de Yunohost\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Vérification de l'état de Yunohost" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost -v" | tee -a "$LOG_BUILD_LXC" 2>&1 # ******** -ynh_print_info --message="\e[1m>> Modification de Yunohost pour la demo\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Modification de Yunohost pour la demo" | tee -a "$LOG_BUILD_LXC" # App officielles -ynh_print_info --message="\e[1m> Installation des applications officielles\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Installation des applications officielles" | tee -a "$LOG_BUILD_LXC" # Ampache -ynh_print_info --message="\e[36mInstallation de Ampache\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de Ampache" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install ampache --force --args \"domain=$domain&path=/ampache&admin=$demo_user&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Baikal -ynh_print_info --message="\e[36mInstallation de baikal\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de baikal" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install baikal --force --args \"domain=$domain&path=/baikal&password=$demo_password&\"" | tee -a "$LOG_BUILD_LXC" # Agendav -ynh_print_info --message="\e[36mInstallation d'agendav\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation d'agendav" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install agendav --force --args \"domain=$domain&path=/agendav&language=en&\"" | tee -a "$LOG_BUILD_LXC" # Dokuwiki -ynh_print_info --message="\e[36mInstallation de dokuwiki\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de dokuwiki" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install dokuwiki --force --args \"domain=$domain&path=/dokuwiki&admin=$demo_user&is_public=1&language=en&\"" | tee -a "$LOG_BUILD_LXC" # Etherpad -ynh_print_info --message="\e[36mInstallation de etherpad\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de etherpad" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install etherpad_mypads --force --args \"domain=$domain&path=/etherpad&admin=$demo_user&password=administration&language=en&is_public=1&export=none&mypads=1&useldap=0&\"" | tee -a "$LOG_BUILD_LXC" # Hextris -ynh_print_info --message="\e[36mInstallation de hextris\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de hextris" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install hextris --force --args \"domain=$domain&path=/hextris&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Jirafeau -ynh_print_info --message="\e[36mInstallation de jirafeau\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de jirafeau" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install jirafeau --force --args \"domain=$domain&path=/jirafeau&admin_user=$demo_user&upload_password=$demo_password&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Kanboard -ynh_print_info --message="\e[36mInstallation de kanboard\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de kanboard" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install kanboard --force --args \"domain=$domain&path=/kanboard&admin=$demo_user&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Nextcloud -ynh_print_info --message="\e[36mInstallation de nextcloud\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de nextcloud" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install nextcloud --force --args \"domain=$domain&path=/nextcloud&admin=$demo_user&user_home=0&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Opensondage -ynh_print_info --message="\e[36mInstallation de opensondage\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de opensondage" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install opensondage --force --args \"domain=$domain&path=/date&admin=$demo_user&language=en&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Phpmyadmin -ynh_print_info --message="\e[36mInstallation de phpmyadmin\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de phpmyadmin" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install phpmyadmin --force --args \"domain=$domain&path=/phpmyadmin&admin=$demo_user&\"" | tee -a "$LOG_BUILD_LXC" # Piwigo -ynh_print_info --message="\e[36mInstallation de piwigo\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de piwigo" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install piwigo --force --args \"domain=$domain&path=/piwigo&admin=$demo_user&is_public=1&language=en&\"" | tee -a "$LOG_BUILD_LXC" # Rainloop -ynh_print_info --message="\e[36mInstallation de rainloop\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de rainloop" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install rainloop --force --args \"domain=$domain&path=/rainloop&is_public=No&password=$demo_password&ldap=Yes&language=en&\"" | tee -a "$LOG_BUILD_LXC" # Roundcube -ynh_print_info --message="\e[36mInstallation de roundcube\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de roundcube" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install roundcube --force --args \"domain=$domain&path=/webmail&with_carddav=0&with_enigma=0&language=en_GB&\"" | tee -a "$LOG_BUILD_LXC" # Searx -ynh_print_info --message="\e[36mInstallation de searx\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de searx" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install searx --force --args \"domain=$domain&path=/searx&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Shellinabox -ynh_print_info --message="\e[36mInstallation de shellinabox\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de shellinabox" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install shellinabox --force --args \"domain=$domain&path=/ssh&\"" | tee -a "$LOG_BUILD_LXC" # Désactive l'accès à shellinabox ynh_lxc_run_inside --name="$lxc_name1" --command="rm /etc/nginx/conf.d/$domain.d/shellinabox.conf" @@ -125,51 +125,51 @@ ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app setting shellinab ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app setting shellinabox domain -d" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app ssowatconf" # Strut -ynh_print_info --message="\e[36mInstallation de strut\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de strut" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install strut --force --args \"domain=$domain&path=/strut&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Transmission -ynh_print_info --message="\e[36mInstallation de transmission\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de transmission" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install transmission --force --args \"domain=$domain&path=/torrent&\"" | tee -a "$LOG_BUILD_LXC" # Ttrss -ynh_print_info --message="\e[36mInstallation de ttrss\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de ttrss" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install ttrss --force --args \"domain=$domain&path=/ttrss&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Wallabag -ynh_print_info --message="\e[36mInstallation de wallabag\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de wallabag" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wallabag2 --force --args \"domain=$domain&path=/wallabag&admin=$demo_user&\"" | tee -a "$LOG_BUILD_LXC" # Wordpress -ynh_print_info --message="\e[36mInstallation de wordpress\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de wordpress" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wordpress --force --args \"domain=$domain&path=/blog&admin=$demo_user&language=en_US&multisite=0&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # Zerobin -ynh_print_info --message="\e[36mInstallation de zerobin\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de zerobin" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install zerobin --force --args \"domain=$domain&path=/zerobin&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" # ******** -ynh_print_info --message="\e[1m> Création d'un snapshot\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Création d'un snapshot" | tee -a "$LOG_BUILD_LXC" ynh_lxc_create_snapshot --name="$lxc_name1" --snapname="snap0" -ynh_print_info --message="\e[1m> Mise à jour de la machine virtualisée\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Mise à jour de la machine virtualisée" | tee -a "$LOG_BUILD_LXC" ynh_lxc_stop --name="$lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_upgrade_demo --name=$lxc_name1 --time_to_switch=$time_to_switch -ynh_print_info --message="\e[1m> Clone la machine\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Clone la machine" | tee -a "$LOG_BUILD_LXC" lxc copy "$lxc_name1" "$lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message="\e[1m> Création d'un snapshot\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Création d'un snapshot" | tee -a "$LOG_BUILD_LXC" ynh_lxc_create_snapshot --name="$lxc_name2" --snapname="snap0" -ynh_print_info --message="\e[1m> Mise en place du cron de switch\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Mise en place du cron de switch" | tee -a "$LOG_BUILD_LXC" ynh_add_config --template="$final_path/conf/cron_demo_switch" --destination="/etc/cron.d/demo_switch" -ynh_print_info --message="\e[1m> Et du cron d'upgrade\e[0m" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Et du cron d'upgrade" | tee -a "$LOG_BUILD_LXC" ynh_add_config --template="$final_path/conf/cron_demo_upgrade" --destination="/etc/cron.d/demo_upgrade" -ynh_print_info --message="e[1m> Mise en place du service" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Mise en place du service" | tee -a "$LOG_BUILD_LXC" #ynh_add_systemd_config --template="$final_path/conf/systemd.service" ynh_add_systemd_config -ynh_print_info --message="e[1m> Integrating service in YunoHost..." | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Integrating service in YunoHost..." | tee -a "$LOG_BUILD_LXC" yunohost service add $app --log="/var/log/$app/$app.log" -ynh_print_info --message="e[1m> Starting a systemd service..." | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Starting a systemd service..." | tee -a "$LOG_BUILD_LXC" ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" diff --git a/demo_lxc_build_init.sh b/demo_lxc_build_init.sh index 7e178b0..3909895 100755 --- a/demo_lxc_build_init.sh +++ b/demo_lxc_build_init.sh @@ -23,15 +23,15 @@ echo "$DOMAIN" > "$script_dir/domain.ini" # Créer le dossier de log sudo mkdir -p $(dirname $LOG_BUILD_LXC) -echo -e "\e[1m> Update et install lxc, lxctl et mailutils\e[0m" | tee "$LOG_BUILD_LXC" +echo -e "> Update et install lxc, lxctl et mailutils" | tee "$LOG_BUILD_LXC" sudo apt-get update | tee -a "$LOG_BUILD_LXC" 2>&1 sudo apt-get install -y lxc lxctl mailutils certbot | tee -a "$LOG_BUILD_LXC" 2>&1 -echo -e "\e[1m> Autoriser l'ip forwarding, pour router vers la machine virtuelle.\e[0m" | tee -a "$LOG_BUILD_LXC" +echo -e "> Autoriser l'ip forwarding, pour router vers la machine virtuelle." | tee -a "$LOG_BUILD_LXC" echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/lxc_demo.conf | tee -a "$LOG_BUILD_LXC" 2>&1 sudo sysctl -p /etc/sysctl.d/lxc_demo.conf | tee -a "$LOG_BUILD_LXC" 2>&1 -echo -e "\e[1m> Ajoute un brige réseau pour la machine virtualisée\e[0m" | tee -a "$LOG_BUILD_LXC" +echo -e "> Ajoute un brige réseau pour la machine virtualisée" | tee -a "$LOG_BUILD_LXC" echo | sudo tee /etc/network/interfaces.d/lxc_demo <&1 auto lxc_demo iface lxc_demo inet static @@ -41,10 +41,10 @@ iface lxc_demo inet static bridge_maxwait 0 EOF -echo -e "\e[1m> Active le bridge réseau\e[0m" | tee -a "$LOG_BUILD_LXC" +echo -e "> Active le bridge réseau" | tee -a "$LOG_BUILD_LXC" sudo ifup lxc_demo --interfaces=/etc/network/interfaces.d/lxc_demo | tee -a "$LOG_BUILD_LXC" 2>&1 -echo -e "\e[1m> Mise en place de la connexion ssh vers l'invité.\e[0m" | tee -a "$LOG_BUILD_LXC" +echo -e "> Mise en place de la connexion ssh vers l'invité." | tee -a "$LOG_BUILD_LXC" if [ -e $HOME/.ssh/$lxc_name1 ]; then rm -f $HOME/.ssh/$lxc_name1 $HOME/.ssh/$lxc_name1.pub ssh-keygen -f $HOME/.ssh/known_hosts -R $lxdbr_demo_network$lxc_ip1 @@ -65,7 +65,7 @@ IdentityFile $HOME/.ssh/$lxc_name1 # End ssh $lxc_name1 EOF -echo -e "\e[1m> Mise en place du reverse proxy et du load balancing\e[0m" | tee -a "$LOG_BUILD_LXC" +echo -e "> Mise en place du reverse proxy et du load balancing" | tee -a "$LOG_BUILD_LXC" echo | sudo tee /etc/nginx/conf.d/$DOMAIN.conf <&1 #upstream $DOMAIN { # server $lxdbr_demo_network$lxc_ip1:443 ; @@ -89,7 +89,7 @@ EOF sudo service nginx reload -echo -e "\e[1m> Création du certificat SSL.\e[0m" | tee -a "$LOG_BUILD_LXC" +echo -e "> Création du certificat SSL." | tee -a "$LOG_BUILD_LXC" sudo mkdir -p /etc/letsencrypt # Créer le fichier de config @@ -181,8 +181,8 @@ EOF sudo service nginx reload -echo -e "\e[1mLe serveur est prêt à déployer les conteneurs de demo.\e[0m" -echo -e "\e[1mExécutez le script demo_lxc_build.sh pour créer les conteneurs et mettre en place la demo.\e[0m" +echo -e "Le serveur est prêt à déployer les conteneurs de demo." +echo -e "Exécutez le script demo_lxc_build.sh pour créer les conteneurs et mettre en place la demo." # Déploie les conteneurs de demo # "$script_dir/demo_lxc_build.sh" diff --git a/demo_lxc_destroy.sh b/demo_lxc_destroy.sh index 1fbb555..575b34b 100755 --- a/demo_lxc_destroy.sh +++ b/demo_lxc_destroy.sh @@ -19,17 +19,17 @@ lxc_ip2=$(ynh_app_setting_get --app=$app --key=lxc_ip2) /bin/bash "$final_path/demo_stop.sh" -f -ynh_print_info --message="\e[1m> Suppression des conteneurs et de leur snapshots\e[0m" +ynh_print_info --message="> Suppression des conteneurs et de leur snapshots" ynh_secure_remove --file="/var/lib/lxd/snapshots/$lxc_name1/snap0.tar.gz" ynh_lxc_delete --name=$lxc_name1 ynh_secure_remove --file="/var/lib/lxd/snapshots/$lxc_name2/snap0.tar.gz" ynh_lxc_delete --name=$lxc_name2 -ynh_print_info --message="\e[1m> Suppression des crons\e[0m" +ynh_print_info --message="> Suppression des crons" ynh_secure_remove --file=/etc/cron.d/demo_switch ynh_secure_remove --file=/etc/cron.d/demo_upgrade -ynh_print_info --message="\e[1m> Suppression du service\e[0m" +ynh_print_info --message="> Suppression du service" if ynh_exec_warn_less yunohost service status $app >/dev/null then ynh_print_info --message="Removing $app service integration..." diff --git a/demo_lxc_remove.sh b/demo_lxc_remove.sh index a711611..a3e60e0 100755 --- a/demo_lxc_remove.sh +++ b/demo_lxc_remove.sh @@ -17,12 +17,12 @@ path_url=$(ynh_app_setting_get --app=$app --key=path) /bin/bash "$final_path/demo_lxc_destroy.sh" # Suppression du reverse proxy -echo -e "\e[1m> Suppression de la config nginx\e[0m" +echo -e "> Suppression de la config nginx" sudo rm /etc/nginx/conf.d/$DOMAIN.conf sudo service nginx reload # Suppression du certificat Let's encrypt -echo -e "\e[1m> Suppression de Let's encrypt\e[0m" +echo -e "> Suppression de Let's encrypt" sudo rm -r /etc/letsencrypt sudo rm -r ~/.local/share/letsencrypt sudo rm -r ~/letsencrypt diff --git a/demo_restore_crash.sh b/demo_restore_crash.sh index fd5a52b..76d8478 100755 --- a/demo_restore_crash.sh +++ b/demo_restore_crash.sh @@ -36,14 +36,14 @@ ynh_lxc_check_container_start --name=$lxc_name2 LXC2_STATUS=$? if [ $LXC1_STATUS -eq 1 ]; then - ynh_print_info --message="\e[91m> Conteneur $lxc_name1 en défaut.\e[0m" + ynh_print_info --message="> Conteneur $lxc_name1 en défaut." else - ynh_print_info --message="\e[92m> Conteneur $lxc_name1 en état de marche.\e[0m" + ynh_print_info --message="> Conteneur $lxc_name1 en état de marche." fi if [ $LXC2_STATUS -eq 1 ]; then - ynh_print_info --message="\e[91m> Conteneur $lxc_name2 en défaut.\e[0m" + ynh_print_info --message="> Conteneur $lxc_name2 en défaut." else - ynh_print_info --message="\e[92m> Conteneur $lxc_name2 en état de marche.\e[0m" + ynh_print_info --message="> Conteneur $lxc_name2 en état de marche." fi # Restauration des snapshots @@ -79,13 +79,13 @@ fi # Résultats finaux if [ $LXC1_STATUS -eq 1 ] || [ $LXC2_STATUS -eq 1 ]; then if [ $LXC1_STATUS -eq 1 ]; then - ynh_print_info --message="\e[91m\n> Le conteneur $lxc_name1 n'a pas pu être réparé...\e[0m" + ynh_print_info --message="> Le conteneur $lxc_name1 n'a pas pu être réparé..." fi if [ $LXC2_STATUS -eq 1 ]; then - ynh_print_info --message="\e[91m\n> Le conteneur $lxc_name2 n'a pas pu être réparé...\e[0m" + ynh_print_info --message="> Le conteneur $lxc_name2 n'a pas pu être réparé..." fi else - ynh_print_info --message="\e[92m\n> Les 2 conteneurs sont sains et fonctionnels.\e[0m" + ynh_print_info --message="> Les 2 conteneurs sont sains et fonctionnels." fi ynh_print_info --message="Réactive le cron switch." From 2516dc36ea2a54bfd5d82b4475daa54d0c7b04d5 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 15 Oct 2022 01:40:26 +0200 Subject: [PATCH 08/25] Update systemd.service --- conf/systemd.service | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/conf/systemd.service b/conf/systemd.service index 9a8fce8..5161bb7 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -1,9 +1,12 @@ [Unit] -Description=Start and stop script for lxc demo container +Description=Start and stop script for LXC demo container Requires=network.target After=network.target + [Service] Type=forking +User=__APP__ +Group=__APP__ ExecStart=/bin/bash __FINAL_PATH__/demo_start.sh RemainAfterExit=true ExecStop=/bin/bash __FINAL_PATH__/demo_stop.sh From a8bcfd0f3ecce20a5edcac26aa9e4e55ca6eafbc Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 15 Oct 2022 01:41:22 +0200 Subject: [PATCH 09/25] Disable apps installation --- demo_lxc_build.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/demo_lxc_build.sh b/demo_lxc_build.sh index ce5a3f9..ac9ceae 100755 --- a/demo_lxc_build.sh +++ b/demo_lxc_build.sh @@ -69,6 +69,8 @@ ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost -v" | tee -a "$LOG_BU # ******** ynh_print_info --message="> Modification de Yunohost pour la demo" | tee -a "$LOG_BUILD_LXC" +if [ ! -z "$PACKAGE_CHECK_EXEC" ] +then # App officielles ynh_print_info --message="> Installation des applications officielles" | tee -a "$LOG_BUILD_LXC" # Ampache @@ -142,7 +144,7 @@ ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wordpress # Zerobin ynh_print_info --message="Installation de zerobin" | tee -a "$LOG_BUILD_LXC" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install zerobin --force --args \"domain=$domain&path=/zerobin&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" - +fi # ******** ynh_print_info --message="> Création d'un snapshot" | tee -a "$LOG_BUILD_LXC" From 6d7d377a9afe003c18a8ee62670ee31703771566 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 15 Oct 2022 02:20:32 +0200 Subject: [PATCH 10/25] Update systemd.service --- conf/systemd.service | 2 -- 1 file changed, 2 deletions(-) diff --git a/conf/systemd.service b/conf/systemd.service index 5161bb7..9fec607 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -5,8 +5,6 @@ After=network.target [Service] Type=forking -User=__APP__ -Group=__APP__ ExecStart=/bin/bash __FINAL_PATH__/demo_start.sh RemainAfterExit=true ExecStop=/bin/bash __FINAL_PATH__/demo_stop.sh From 7dfe57e6da153662dd013979aae7e8eb093ea2a0 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 15 Oct 2022 02:43:00 +0200 Subject: [PATCH 11/25] Update demo_stop.sh --- demo_stop.sh | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/demo_stop.sh b/demo_stop.sh index 227cc15..d4dd1e5 100755 --- a/demo_stop.sh +++ b/demo_stop.sh @@ -29,13 +29,19 @@ else fi ynh_print_info --message="> Arrêt des conteneurs" -if ! ynh_lxc_is_stopped --name=$lxc_name1 +if ynh_lxc_exists --name=$lxc_name1 then - ynh_print_info --message="Arrêt du conteneur $lxc_name1" - ynh_lxc_stop_as_demo --name=$lxc_name1 + if ! ynh_lxc_is_stopped --name=$lxc_name1 + then + ynh_print_info --message="Arrêt du conteneur $lxc_name1" + ynh_lxc_stop_as_demo --name=$lxc_name1 + fi fi -if ! ynh_lxc_is_stopped --name=$lxc_name2 +if ynh_lxc_exists --name=$lxc_name2 then - ynh_print_info --message="Arrêt du conteneur $lxc_name2" - ynh_lxc_stop_as_demo --name=$lxc_name2 + if ! ynh_lxc_is_stopped --name=$lxc_name2 + then + ynh_print_info --message="Arrêt du conteneur $lxc_name2" + ynh_lxc_stop_as_demo --name=$lxc_name2 + fi fi From 22edb9b5977ec247cb39dbd3128a9f8273e43fb9 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 15 Oct 2022 02:45:34 +0200 Subject: [PATCH 12/25] Update demo_lxc_build.sh --- demo_lxc_build.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/demo_lxc_build.sh b/demo_lxc_build.sh index ac9ceae..5b5fd79 100755 --- a/demo_lxc_build.sh +++ b/demo_lxc_build.sh @@ -69,8 +69,7 @@ ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost -v" | tee -a "$LOG_BU # ******** ynh_print_info --message="> Modification de Yunohost pour la demo" | tee -a "$LOG_BUILD_LXC" -if [ ! -z "$PACKAGE_CHECK_EXEC" ] -then +if [ ${FOR_PACKAGE_CHECK:-0} -eq 1 ]; then # App officielles ynh_print_info --message="> Installation des applications officielles" | tee -a "$LOG_BUILD_LXC" # Ampache From 2e4cc68fed853a7021fcc8e843ea754402536e2d Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 15 Oct 2022 20:19:52 +0200 Subject: [PATCH 13/25] Update demo_lxc_build.sh --- demo_lxc_build.sh | 134 +++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 67 deletions(-) diff --git a/demo_lxc_build.sh b/demo_lxc_build.sh index 5b5fd79..d392ce9 100755 --- a/demo_lxc_build.sh +++ b/demo_lxc_build.sh @@ -34,143 +34,143 @@ LOG_BUILD_LXC="$final_path/$LOG" if $(ynh_lxc_exists --name="$name") then # Si le conteneur existe déjà - ynh_print_info --message="> Suppression du conteneur existant." | tee -a "$LOG_BUILD_LXC" - /bin/bash "$final_path/demo_lxc_destroy.sh" quiet | tee -a "$LOG_BUILD_LXC" + ynh_print_info --message="> Suppression du conteneur existant." | tee -a "$LOG_BUILD_LXC" 2>&1 + /bin/bash "$final_path/demo_lxc_destroy.sh" quiet | tee -a "$LOG_BUILD_LXC" 2>&1 fi -ynh_print_info --message="> Création d'une machine debian $DIST minimaliste" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Création d'une machine debian $DIST minimaliste" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_create --image="$lxc_base" --name="$lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message= "> Active le bridge réseau" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message= "> Active le bridge réseau" | tee -a "$LOG_BUILD_LXC" 2>&1 lxc network attach $lxdbr_demo_name $lxc_name1 eth1 eth1 | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message="> Configuration réseau de la machine virtualisée" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Configuration réseau de la machine virtualisée" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="cp /etc/systemd/network/eth0.network /etc/systemd/network/eth1.network" ynh_lxc_run_inside --name="$lxc_name1" --command="sed -i s/eth0/eth1/g /etc/systemd/network/eth1.network" -ynh_print_info --message="> Update de la machine virtualisée" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Update de la machine virtualisée" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y update" ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y full-upgrade" ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y autoremove" ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y clean" -ynh_print_info --message="> Post install Yunohost" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Post install Yunohost" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost tools postinstall --domain $domain --password $yunohost_password --force-password" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message="> Disable password strength" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost settings set security.password.user.strength -v -1" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Disable password strength" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost settings set security.password.user.strength -v -1" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message="> Ajout de l'utilisateur de demo" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost user create $demo_user --firstname $demo_user --lastname $demo_user --domain $domain --password $demo_password" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Ajout de l'utilisateur de demo" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost user create $demo_user --firstname $demo_user --lastname $demo_user --domain $domain --password $demo_password" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message="> Vérification de l'état de Yunohost" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Vérification de l'état de Yunohost" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost -v" | tee -a "$LOG_BUILD_LXC" 2>&1 # ******** -ynh_print_info --message="> Modification de Yunohost pour la demo" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Modification de Yunohost pour la demo" | tee -a "$LOG_BUILD_LXC" 2>&1 if [ ${FOR_PACKAGE_CHECK:-0} -eq 1 ]; then # App officielles -ynh_print_info --message="> Installation des applications officielles" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Installation des applications officielles" | tee -a "$LOG_BUILD_LXC" 2>&1 # Ampache -ynh_print_info --message="Installation de Ampache" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install ampache --force --args \"domain=$domain&path=/ampache&admin=$demo_user&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de Ampache" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install ampache --force --args \"domain=$domain&path=/ampache&admin=$demo_user&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Baikal -ynh_print_info --message="Installation de baikal" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install baikal --force --args \"domain=$domain&path=/baikal&password=$demo_password&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de baikal" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install baikal --force --args \"domain=$domain&path=/baikal&password=$demo_password&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Agendav -ynh_print_info --message="Installation d'agendav" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install agendav --force --args \"domain=$domain&path=/agendav&language=en&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation d'agendav" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install agendav --force --args \"domain=$domain&path=/agendav&language=en&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Dokuwiki -ynh_print_info --message="Installation de dokuwiki" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install dokuwiki --force --args \"domain=$domain&path=/dokuwiki&admin=$demo_user&is_public=1&language=en&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de dokuwiki" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install dokuwiki --force --args \"domain=$domain&path=/dokuwiki&admin=$demo_user&is_public=1&language=en&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Etherpad -ynh_print_info --message="Installation de etherpad" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install etherpad_mypads --force --args \"domain=$domain&path=/etherpad&admin=$demo_user&password=administration&language=en&is_public=1&export=none&mypads=1&useldap=0&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de etherpad" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install etherpad_mypads --force --args \"domain=$domain&path=/etherpad&admin=$demo_user&password=administration&language=en&is_public=1&export=none&mypads=1&useldap=0&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Hextris -ynh_print_info --message="Installation de hextris" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install hextris --force --args \"domain=$domain&path=/hextris&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de hextris" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install hextris --force --args \"domain=$domain&path=/hextris&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Jirafeau -ynh_print_info --message="Installation de jirafeau" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install jirafeau --force --args \"domain=$domain&path=/jirafeau&admin_user=$demo_user&upload_password=$demo_password&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de jirafeau" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install jirafeau --force --args \"domain=$domain&path=/jirafeau&admin_user=$demo_user&upload_password=$demo_password&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Kanboard -ynh_print_info --message="Installation de kanboard" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install kanboard --force --args \"domain=$domain&path=/kanboard&admin=$demo_user&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de kanboard" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install kanboard --force --args \"domain=$domain&path=/kanboard&admin=$demo_user&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Nextcloud -ynh_print_info --message="Installation de nextcloud" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install nextcloud --force --args \"domain=$domain&path=/nextcloud&admin=$demo_user&user_home=0&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de nextcloud" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install nextcloud --force --args \"domain=$domain&path=/nextcloud&admin=$demo_user&user_home=0&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Opensondage -ynh_print_info --message="Installation de opensondage" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install opensondage --force --args \"domain=$domain&path=/date&admin=$demo_user&language=en&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de opensondage" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install opensondage --force --args \"domain=$domain&path=/date&admin=$demo_user&language=en&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Phpmyadmin -ynh_print_info --message="Installation de phpmyadmin" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install phpmyadmin --force --args \"domain=$domain&path=/phpmyadmin&admin=$demo_user&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de phpmyadmin" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install phpmyadmin --force --args \"domain=$domain&path=/phpmyadmin&admin=$demo_user&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Piwigo -ynh_print_info --message="Installation de piwigo" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install piwigo --force --args \"domain=$domain&path=/piwigo&admin=$demo_user&is_public=1&language=en&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de piwigo" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install piwigo --force --args \"domain=$domain&path=/piwigo&admin=$demo_user&is_public=1&language=en&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Rainloop -ynh_print_info --message="Installation de rainloop" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install rainloop --force --args \"domain=$domain&path=/rainloop&is_public=No&password=$demo_password&ldap=Yes&language=en&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de rainloop" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install rainloop --force --args \"domain=$domain&path=/rainloop&is_public=No&password=$demo_password&ldap=Yes&language=en&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Roundcube -ynh_print_info --message="Installation de roundcube" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install roundcube --force --args \"domain=$domain&path=/webmail&with_carddav=0&with_enigma=0&language=en_GB&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de roundcube" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install roundcube --force --args \"domain=$domain&path=/webmail&with_carddav=0&with_enigma=0&language=en_GB&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Searx -ynh_print_info --message="Installation de searx" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install searx --force --args \"domain=$domain&path=/searx&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de searx" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install searx --force --args \"domain=$domain&path=/searx&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Shellinabox -ynh_print_info --message="Installation de shellinabox" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install shellinabox --force --args \"domain=$domain&path=/ssh&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de shellinabox" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install shellinabox --force --args \"domain=$domain&path=/ssh&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Désactive l'accès à shellinabox ynh_lxc_run_inside --name="$lxc_name1" --command="rm /etc/nginx/conf.d/$domain.d/shellinabox.conf" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app setting shellinabox path -d" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app setting shellinabox domain -d" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app ssowatconf" # Strut -ynh_print_info --message="Installation de strut" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install strut --force --args \"domain=$domain&path=/strut&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de strut" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install strut --force --args \"domain=$domain&path=/strut&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Transmission -ynh_print_info --message="Installation de transmission" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install transmission --force --args \"domain=$domain&path=/torrent&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de transmission" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install transmission --force --args \"domain=$domain&path=/torrent&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Ttrss -ynh_print_info --message="Installation de ttrss" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install ttrss --force --args \"domain=$domain&path=/ttrss&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de ttrss" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install ttrss --force --args \"domain=$domain&path=/ttrss&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Wallabag -ynh_print_info --message="Installation de wallabag" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wallabag2 --force --args \"domain=$domain&path=/wallabag&admin=$demo_user&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de wallabag" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wallabag2 --force --args \"domain=$domain&path=/wallabag&admin=$demo_user&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Wordpress -ynh_print_info --message="Installation de wordpress" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wordpress --force --args \"domain=$domain&path=/blog&admin=$demo_user&language=en_US&multisite=0&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de wordpress" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wordpress --force --args \"domain=$domain&path=/blog&admin=$demo_user&language=en_US&multisite=0&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Zerobin -ynh_print_info --message="Installation de zerobin" | tee -a "$LOG_BUILD_LXC" -ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install zerobin --force --args \"domain=$domain&path=/zerobin&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="Installation de zerobin" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install zerobin --force --args \"domain=$domain&path=/zerobin&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 fi # ******** -ynh_print_info --message="> Création d'un snapshot" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Création d'un snapshot" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_create_snapshot --name="$lxc_name1" --snapname="snap0" -ynh_print_info --message="> Mise à jour de la machine virtualisée" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Mise à jour de la machine virtualisée" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_stop --name="$lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_upgrade_demo --name=$lxc_name1 --time_to_switch=$time_to_switch -ynh_print_info --message="> Clone la machine" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Clone la machine" | tee -a "$LOG_BUILD_LXC" 2>&1 lxc copy "$lxc_name1" "$lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message="> Création d'un snapshot" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Création d'un snapshot" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_create_snapshot --name="$lxc_name2" --snapname="snap0" -ynh_print_info --message="> Mise en place du cron de switch" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Mise en place du cron de switch" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_add_config --template="$final_path/conf/cron_demo_switch" --destination="/etc/cron.d/demo_switch" -ynh_print_info --message="> Et du cron d'upgrade" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Et du cron d'upgrade" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_add_config --template="$final_path/conf/cron_demo_upgrade" --destination="/etc/cron.d/demo_upgrade" -ynh_print_info --message="> Mise en place du service" | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Mise en place du service" | tee -a "$LOG_BUILD_LXC" 2>&1 #ynh_add_systemd_config --template="$final_path/conf/systemd.service" ynh_add_systemd_config -ynh_print_info --message="> Integrating service in YunoHost..." | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Integrating service in YunoHost..." | tee -a "$LOG_BUILD_LXC" 2>&1 yunohost service add $app --log="/var/log/$app/$app.log" -ynh_print_info --message="> Starting a systemd service..." | tee -a "$LOG_BUILD_LXC" +ynh_print_info --message="> Starting a systemd service..." | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" From e04cca93b1949a49d853cde933524f8db0515724 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 15 Oct 2022 21:13:40 +0200 Subject: [PATCH 14/25] Improving --- demo_lxc_build.sh | 2 +- ynh_lxd | 981 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 982 insertions(+), 1 deletion(-) create mode 100644 ynh_lxd diff --git a/demo_lxc_build.sh b/demo_lxc_build.sh index d392ce9..84a2985 100755 --- a/demo_lxc_build.sh +++ b/demo_lxc_build.sh @@ -69,7 +69,7 @@ ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost -v" | tee -a "$LOG_BU # ******** ynh_print_info --message="> Modification de Yunohost pour la demo" | tee -a "$LOG_BUILD_LXC" 2>&1 -if [ ${FOR_PACKAGE_CHECK:-0} -eq 1 ]; then +if [ ${DONT_INSTALL_FOR_NOW:-0} -eq 1 ]; then # App officielles ynh_print_info --message="> Installation des applications officielles" | tee -a "$LOG_BUILD_LXC" 2>&1 # Ampache diff --git a/ynh_lxd b/ynh_lxd new file mode 100644 index 0000000..5e6abe7 --- /dev/null +++ b/ynh_lxd @@ -0,0 +1,981 @@ +#!/bin/bash + +#================================================= +# LXC helpers +#================================================= + +# Check if a LXC container exists +# +# usage: ynh_lxc_exists --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_exists() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ! lxc list --format json | jq -e --arg name $name '.[] | select(.name==$name) | .name' >/dev/null + then + return 1 + else + return 0 + fi +} + +# Return LXC container status +# +# usage: ynh_lxc_status --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_status() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ynh_lxc_exists --name=$name + then + lxc list --format json | jq -r --arg name $name '.[] | select(.name==$name) | .state | .status' + fi +} + +# Check if an LXC container is running +# +# usage: ynh_lxc_is_started --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_is_started() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if [ "$(ynh_lxc_status --name=$name)" == Running ] + then + return 0 + else + return 1 + fi +} + +# Check if an LXC container is stopped +# +# usage: ynh_lxc_is_stopped --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_is_stopped() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if [ "$(ynh_lxc_status --name=$name)" == Stopped ] + then + return 0 + else + return 1 + fi +} + +# Start an LXC container +# +# usage: ynh_lxc_start --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_start() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # If the container exists + if ynh_lxc_exists --name=$name + then + if ! ynh_lxc_is_started --name=$name + then + lxc start $name + fi + fi +} + +# Stopping an LXC container +# +# usage: ynh_lxc_stop --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_stop() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # If the container exists + if ynh_lxc_exists --name=$name + then + ynh_print_info --message="Stopping LXC $name" + wait_period=0 + while ! ynh_lxc_is_stopped --name=$name + do + lxc stop $name + wait_period=$(($wait_period+10)) + if [ $wait_period -gt 30 ];then + break + else + sleep 1 + fi + done + + # If the command times out, then add the option --force + wait_period=0 + while ! ynh_lxc_is_stopped --name=$name + do + lxc stop $name --force + wait_period=$(($wait_period+10)) + if [ $wait_period -gt 30 ];then + break + else + sleep 5 + fi + done + fi +} + +# Run a command inside an LXC container +# +# usage: ynh_lxc_run_inside --name=name --command=command +# | arg: -n, --name= - name of the LXC +# | arg: -c, --command= - command to execute +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_run_inside() { + # Declare an array to define the options of this helper. + local legacy_args=nc + local -A args_array=([n]=name= [c]=command=) + local name + local command + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + lxc exec $name -- /bin/bash -c "$command" +} + +# Check an LXC container can start +# +# usage: ynh_lxc_check_container_start --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_check_container_start() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_print_info --message="Test du conteneur $name" + ynh_lxc_start --name=$name # Démarre le conteneur + + wait_period=0 + while ! ynh_lxc_is_started --name=$name + do + wait_period=$(($wait_period+10)) + if [ $wait_period -gt 20 ];then + break + else + sleep 5 + fi + done + + ynh_lxc_is_started --name=$name +} + +# Restart a container +# +# usage: _ynh_restart_container --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +_ynh_restart_container() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_lxc_stop --name="$name" + ynh_lxc_start --name="$name" +} + +# Keep sure the LXC is started +# +# usage: _ynh_lxc_start_and_wait --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +_ynh_lxc_start_and_wait() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # Try to start the container 3 times. + ynh_lxc_start --name=$name + local max_try=3 + local i=0 + while [ $i -lt $max_try ] + do + i=$(( i +1 )) + local failstart=0 + + # Wait for container to start, we are using systemd to check this, + # for the sake of brevity. + for j in $(seq 1 10); do + if ynh_lxc_run_inside --name=$name --command="systemctl isolate multi-user.target >/dev/null 2>/dev/null"; then + break + fi + + if [ "$j" == "10" ]; then + log_debug 'Failed to start the container ... restarting ...' + failstart=1 + + _ynh_restart_container --name="$name" + fi + + sleep 1s + done + + # Wait for container to access the internet + for j in $(seq 1 10); do + if ynh_lxc_run_inside --name=$name --command="curl -s http://wikipedia.org > /dev/null 2>/dev/null"; then + break + fi + + if [ "$j" == "10" ]; then + log_debug 'Failed to access the internet ... restarting' + failstart=1 + + _ynh_restart_container --name="$name" + fi + + sleep 1s + done + + # Has started and has access to the internet + if [ $failstart -eq 0 ] + then + break + fi + + # Fail if the container failed to start + if [ $i -eq $max_try ] && [ $failstart -eq 1 ] + then + log_error "The container miserably failed to start or to connect to the internet" + lxc info --show-log $name + return 1 + fi + done + + LXC_IP=$(ynh_lxc_run_inside --name=$name --command="hostname -I | cut -d' ' -f1 | grep -E -o \"\<[0-9.]{8,}\>\"") +} + +# Create a new LXC from an image +# +# usage: ynh_lxc_create --image=image --name=name +# | arg: -i, --image= - image to create from +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_create(){ + # Declare an array to define the options of this helper. + local legacy_args=in + local -A args_array=([i]=image= [n]=name=) + local image + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + log_info "Launching new LXC $name ..." + # Check if we can launch container from YunoHost remote image + if ynh_exec_as $app lxc remote list | grep -q "yunohost" && ynh_exec_as $app lxc image list yunohost:$image | grep -q -w $image; then + ynh_exec_as $app lxc launch yunohost:$image $name \ + -c security.nesting=true \ + -c security.privileged=true \ + -c limits.memory=80% \ + -c limits.cpu.allowance=80% | tee -a /proc/self/fd/3 + # Check if we can launch container from a local image + elif ynh_exec_as $app lxc image list $image | grep -q -w $image; then + ynh_exec_as $app lxc launch $image $name \ + -c security.nesting=true \ + -c security.privileged=true \ + -c limits.memory=80% \ + -c limits.cpu.allowance=80% | tee -a /proc/self/fd/3 + else + log_critical "Can't find base image $image" + fi + + if [ ! -z "$PACKAGE_CHECK_EXEC" ] + then + pipestatus="${PIPESTATUS[0]}" + location=$(ynh_exec_as $app lxc list --format json | jq -e --arg name $name '.[] | select(.name==$name) | .location' | tr -d '"') + [[ "$location" != "none" ]] && log_info "... on $location" + + [[ "$pipestatus" -eq 0 ]] || exit 1 + fi + + _ynh_lxc_start_and_wait --name="$name" + if [ ! -z "$PACKAGE_CHECK_EXEC" ] + then + ynh_lxc_set_witness --name="$name" + fi + ynh_lxc_create_snapshot --name="$name" --snapname="snap0" +} + +# Delete a lxc container +# +# usage: ynh_lxc_delete --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_delete() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ynh_lxc_exists --name=$name + then + lxc delete $name --force + else + return 1 + fi +} + +# Clean the swapfiles of an LXC container +# +# usage: ynh_lxc_clean_swapfiles --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_clean_swapfiles() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + _ynh_lxc_start_and_wait --name="$name" + + ynh_lxc_run_inside --name=$name --command='for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' + ynh_lxc_run_inside --name=$name --command='for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $swapfile; done' +} + +# Check if a snapshot exist for an LXC container +# +# usage: ynh_lxc_snapshot_exists --name=name --snapname=snapname +# | arg: -n, --name= - name of the LXC +# | arg: -s, --snapname= - name of the snapshot +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_snapshot_exists() { + # Declare an array to define the options of this helper. + local legacy_args=ns + local -A args_array=([n]=name= [s]=snapname=) + local name + local snapname + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # If the container exists + if ynh_lxc_exists --name=$name + then + if lxc list --format json | jq -e --arg name $name --arg snapname $snapname '.[] | select(.name==$name) | .snapshots' >/dev/null + then + if lxc list --format json | jq -e --arg name $name --arg snapname $snapname '.[] | select(.name==$name) | .snapshots[] | select(.name==$snapname)' >/dev/null + then + return 0 + else + return 1 + fi + else + return 1 + fi + fi +} + +# Create a snapshot of an LXC container +# +# usage: ynh_lxc_create_snapshot --name=name --snapname=snapname +# | arg: -n, --name= - name of the LXC +# | arg: -s, --snapname= - name of the snapshot +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_create_snapshot() { + # Declare an array to define the options of this helper. + local legacy_args=ns + local -A args_array=([n]=name= [s]=snapname=) + local name + local snapname + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_lxc_start_timer + + if [ ! -z "$PACKAGE_CHECK_EXEC" ] + then + # Check all the witness files, to verify if them still here + ynh_lxc_check_witness >&2 + fi + + # Remove swap files to avoid killing the CI with huge snapshots. + ynh_lxc_clean_swapfiles --name="$name" + + ynh_lxc_stop --name="$name" + + # Check if the snapshot already exist + if ! ynh_lxc_snapshot_exists --name="$name" --snapname="$snapname" + then + log_info "(Creating snapshot $snapname ...)" + lxc snapshot $name $snapname + else + log_info "(Recreating snapshot $snapname ...)" + lxc snapshot $name $snapname --reuse + fi + + _ynh_lxc_start_and_wait --name="$name" + + ynh_lxc_stop_timer 1 +} + +# Delete a snapshot of an LXC container +# +# usage: ynh_lxc_delete_snapshot --name=name --snapname=snapname +# | arg: -n, --name= - name of the LXC +# | arg: -s, --snapname= - name of the snapshot +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_delete_snapshot() { + # Declare an array to define the options of this helper. + local legacy_args=ns + local -A args_array=([n]=name= [s]=snapname=) + local name + local snapname + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ynh_lxc_snapshot_exists --name="$name" --snapname="$snapname" + then + lxc delete $name/$snapname + return 0 + else + return 1 + fi +} + +# Load a snapshot of an LXC container +# +# usage: ynh_lxc_load_snapshot --name=name --snapname=snapname +# | arg: -n, --name= - name of the LXC +# | arg: -s, --snapname= - name of the snapshot +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_load_snapshot() { + # Declare an array to define the options of this helper. + local legacy_args=ns + local -A args_array=([n]=name= [s]=snapname=) + local name + local snapname + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ynh_lxc_snapshot_exists --name="$name" --snapname="$snapname" + then + log_debug "Loading snapshot $snapname ..." + + # Remove swap files before restoring the snapshot. + ynh_lxc_clean_swapfiles --name="$name" + + ynh_lxc_stop --name="$name" + + lxc restore $name $snapname + _ynh_lxc_start_and_wait --name="$name" + else + return 1 + fi +} + +# Restore an LXC container from snap0 snapshot +# +# usage: ynh_lxc_restore_from_snapshot --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_restore_from_snapshot () { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_print_info --message="\e[1m> Restauration du snapshot du conteneur $name\e[0m" + ynh_lxc_load_snapshot --name=$name --snapname=snap0 + if ynh_lxc_check_container_start --name=$name + then + ynh_print_info --message="\e[92m> Conteneur $name en état de marche.\e[0m" + return 0 + else + ynh_print_info --message="\e[91m> Conteneur $name en défaut.\e[0m" + return 1 + fi +} + +# Restore an LXC container from an archive +# +# usage: ynh_lxc_restore_from_archive --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_restore_from_archive () { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ! test -e "/var/lib/lxd/snapshots/$name/snap1.tar.gz"; then + ynh_print_info --message="Aucune archive de snapshot pour le conteneur $name" + return 1 + fi + ynh_print_info --message="\e[1m> Restauration du snapshot archivé pour le conteneur $name\e[0m" + ynh_print_info --message="Suppression du snapshot" + ynh_lxc_delete_snapshot --name=$name --snapname=snap0 + ynh_print_info --message="Décompression de l'archive" + tar -x --acls --xattrs -f /var/lib/lxd/snapshots/$name/snap0.tar.gz -C / + ynh_lxc_restore_from_snapshot --name=$name + return $? +} + +# Clone an LXC container +# +# usage: ynh_lxc_clone --source=source --destination=destination +# | arg: -s, --source= - source LXC +# | arg: -d, --destination= - destination LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_clone() { + # Declare an array to define the options of this helper. + local legacy_args=sd + local -A args_array=([s]=source= [d]=destination=) + local source + local destination + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_print_info --message="Suppression du conteneur $destination" + ynh_secure_remove --file="/var/lib/lxd/snapshots/$destination/snap0.tar.gz" + ynh_lxc_reset --name=$destination + + ynh_print_info --message="\e[1m> Clone le conteneur $source sur $destination\e[0m" + lxc copy "$source" "$destination" + + ynh_lxc_check_container_start --name=$destination + STATUS=$? + if [ $STATUS -eq 1 ]; then + ynh_print_info --message="\e[91m> Conteneur $destination en défaut.\e[0m" + else + ynh_print_info --message="\e[92m> Conteneur $destination en état de marche.\e[0m" + ynh_print_info --message= "Création d'un nouveau snapshot pour le conteneur $destination" + ynh_lxc_create_snapshot --name="$destination" --snapname="snap0" + fi + return $STATUS +} + +# usage: ynh_lxc_test --name=name - +ynh_lxc_test(){ + # Declare an array to define the options of this helper. + local legacy_args=ns + local -A args_array=([n]=name= [s]=snapname=) + local name + local snapname + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ynh_lxc_exists --name=$name + then + ynh_print_info --message="$name exist" + else + ynh_print_info --message="$name does not exist" + fi +} + +# Reset an LXC container +# +# usage: ynh_lxc_reset --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_reset() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # If the container exists + if ynh_lxc_exists --name=$name + then + # Remove swap files before deletting the continer + ynh_lxc_clean_swapfiles --name="$name" + ynh_lxc_stop --name="$name" + local current_storage=$(lxc list $name --format json --columns b | jq '.[].expanded_devices.root.pool') + swapoff "$(lxc storage get $current_storage source)/containers/$name/rootfs/swap" 2>/dev/null + ynh_lxc_delete --name=$name + fi +} + +#================================================= +# Logging helpers +#================================================= + +readonly NORMAL=$(printf '\033[0m') +readonly BOLD=$(printf '\033[1m') +readonly faint=$(printf '\033[2m') +readonly UNDERLINE=$(printf '\033[4m') +readonly NEGATIVE=$(printf '\033[7m') +readonly RED=$(printf '\033[31m') +readonly GREEN=$(printf '\033[32m') +readonly ORANGE=$(printf '\033[33m') +readonly BLUE=$(printf '\033[34m') +readonly YELLOW=$(printf '\033[93m') +readonly WHITE=$(printf '\033[39m') + +function log_title() +{ + cat << EOF | tee -a /proc/self/fd/3 +${BOLD} + ============================================ + $1 + ============================================ +${NORMAL} +EOF +} + +function log_small_title() +{ + echo -e "\n${BOLD} > ${1}${NORMAL}\n" | tee -a /proc/self/fd/3 +} + + +function log_debug() +{ + echo "$1" | tee -a /proc/self/fd/3 +} + +function log_info() +{ + echo "${1}" | tee -a /proc/self/fd/3 +} + +function log_success() +{ + echo "${BOLD}${GREEN}Success: ${1}${NORMAL}" | tee -a /proc/self/fd/3 +} + +function log_warning() +{ + echo "${BOLD}${ORANGE}Warning: ${1}${NORMAL}" | tee -a /proc/self/fd/3 +} + +function log_error() +{ + echo "${BOLD}${RED}Error: ${1}${NORMAL}" | tee -a /proc/self/fd/3 +} + +function log_critical() +{ + echo "${BOLD}${RED}Critical: ${1}${NORMAL}" | tee -a /proc/self/fd/3 + exit 1 +} + +function log_report_test_success () { + echo -e "\n${BOLD}${GREEN}--- SUCCESS ---${NORMAL}\n" | tee -a /proc/self/fd/3 +} + +function log_report_test_warning () { + echo -e "\n${BOLD}${ORANGE}--- WARNING ---${NORMAL}\n" | tee -a /proc/self/fd/3 +} + +function log_report_test_failed () { + echo -e "\n${BOLD}${RED}--- FAIL ---${NORMAL}\n" | tee -a /proc/self/fd/3 +} + +#================================================= +# Timing helpers +#================================================= + +# Start a timer +# +# usage: ynh_lxc_start_timer +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_start_timer() { + # Set the beginning of the timer + starttime=$(date +%s) +} + +# Stop a timer +# +# usage: ynh_lxc_stop_timer +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_stop_timer() { + # Ending the timer + # $1 = Type of querying + + local finishtime=$(date +%s) + # Calculate the gap between the starting and the ending of the timer + local elapsedtime=$(echo $(( $finishtime - $starttime ))) + # Extract the number of hour + local hours=$(echo $(( $elapsedtime / 3600 ))) + local elapsedtime=$(echo $(( $elapsedtime - ( 3600 * $hours) ))) + # Minutes + local minutes=$(echo $(( $elapsedtime / 60 ))) + # And seconds + local seconds=$(echo $(( $elapsedtime - ( 60 * $minutes) ))) + + local phours="" + local pminutes="" + local pseconds="" + + # Avoid null values + [ $hours -eq 0 ] || phours="$hours hour" + [ $minutes -eq 0 ] || pminutes="$minutes minute" + [ $seconds -eq 0 ] || pseconds="$seconds second" + + # Add a 's' for plural values + [ $hours -eq 1 ] && phours="${phours}, " || test -z "$phours" || phours="${phours}s, " + [ $minutes -eq 1 ] && pminutes="${pminutes}, " || test -z "$pminutes" || pminutes="${pminutes}s, " + [ $seconds -gt 1 ] && pseconds="${pseconds}s" || pseconds="0s" + + local time="${phours}${pminutes}${pseconds} ($(date '+%T'))" + if [ $1 -eq 2 ]; then + log_info "Working time for this test: $time" + elif [ $1 -eq 3 ]; then + log_info "Global working time for all tests: $time" + else + log_debug "Working time: $time" + fi +} + +#================================================= +# PACKAGE_CHECK helpers +#================================================= + +# Start an LXC and execute a command in it, to be used for PACKAGE_CHECK_EXEC +# +# usage: ynh_lxc_exec --name=name --command=command +# | arg: -n, --name= - name of the LXC +# | arg: -c, --command= - command to execute +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_exec() { + # Declare an array to define the options of this helper. + local legacy_args=nc + local -A args_array=([n]=name= [c]=command=) + local name + local command + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + _ynh_lxc_start_and_wait --name="$name" + + ynh_lxc_start_timer + + # Execute the command given in argument in the container and log its results. + lxc exec $name --env PACKAGE_CHECK_EXEC=1 -t -- /bin/bash -c "$command" + + # Store the return code of the command + local returncode=${PIPESTATUS[0]} + + log_debug "Return code: $returncode" + + ynh_lxc_stop_timer 1 + # Return the exit code of the ssh command + return $returncode +} + +# Create a witness in an LXC container +# +# usage: ynh_lxc_create_witness --name=name --witness=witness --type=type +# | arg: -n, --name= - name of the LXC +# | arg: -w, --witness= - witness to create +# | arg: -t, --type= - type of witness, can be file or directory +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_create_witness () { + # Declare an array to define the options of this helper. + local legacy_args=nwt + local -A args_array=([n]=name= [w]=witness= [t]=type=) + local name + local witness + local type + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + [ "$type" = "file" ] && local action="touch" || local action="mkdir -p" + ynh_lxc_run_inside --name=$name --command="$action $witness" +} + +# Set witness in an LXC container +# +# usage: ynh_lxc_set_witness --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_set_witness() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # Create files to check if the remove script does not remove them accidentally + log_debug "Create witness files..." + + # Nginx conf + ynh_lxc_create_witness -name=$name --witness="/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" --type=file + ynh_lxc_create_witness -name=$name --witness="/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" --type=file + + # /etc + ynh_lxc_create_witness -name=$name --witness="/etc/witnessfile" --type=file + + # /opt directory + ynh_lxc_create_witness -name=$name --witness="/opt/witnessdir" --type=directory + + # /var/www directory + ynh_lxc_create_witness -name=$name --witness="/var/www/witnessdir" --type=directory + + # /home/yunohost.app/ + ynh_lxc_create_witness -name=$name --witness="/home/yunohost.app/witnessdir" --type=directory + + # /var/log + ynh_lxc_create_witness -name=$name --witness="/var/log/witnessfile" --type=file + + # Config fpm + ynh_lxc_create_witness -name=$name --witness="/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" --type=file + + # Config logrotate + ynh_lxc_create_witness -name=$name --witness="/etc/logrotate.d/witnessfile" --type=file + + # Config systemd + ynh_lxc_create_witness -name=$name --witness="/etc/systemd/system/witnessfile.service" --type=file + + # Database + ynh_lxc_run_inside --name=$name --command="mysqladmin --wait status > /dev/null 2>&1" + ynh_lxc_run_inside --name=$name --command="echo \"CREATE DATABASE witnessdb\" | mysql --wait > /dev/null 2>&1" +} + +# Check if a witness exists in an LXC container +# +# usage: ynh_lxc_create_witness --name=name --witness=witness +# | arg: -n, --name= - name of the LXC +# | arg: -w, --witness= - witness to create +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_check_witness_exist () { + # Declare an array to define the options of this helper. + local legacy_args=nw + local -A args_array=([n]=name= [w]=witness=) + local name + local witness + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ynh_lxc_run_inside --name=$name --command="test ! -e \"$witness\"" + then + log_error "The file $witness is missing ! Something gone wrong !" + SET_RESULT "failure" witness + fi +} + +# Check witness in an LXC container +# +# usage: ynh_lxc_check_witness --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_check_witness() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # Check all the witness files, to verify if them still here + + # Nginx conf + ynh_lxc_check_witness_exist --name=$name --witness="/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" + ynh_lxc_check_witness_exist --name=$name --witness="/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" + + # /etc + ynh_lxc_check_witness_exist --name=$name --witness="/etc/witnessfile" + + # /opt directory + ynh_lxc_check_witness_exist --name=$name --witness="/opt/witnessdir" + + # /var/www directory + ynh_lxc_check_witness_exist --name=$name --witness="/var/www/witnessdir" + + # /home/yunohost.app/ + ynh_lxc_check_witness_exist --name=$name --witness="/home/yunohost.app/witnessdir" + + # /var/log + ynh_lxc_check_witness_exist --name=$name --witness="/var/log/witnessfile" + + # Config fpm + ynh_lxc_check_witness_exist --name=$name --witness="/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" + + # Config logrotate + ynh_lxc_check_witness_exist --name=$name --witness="/etc/logrotate.d/witnessfile" + + # Config systemd + ynh_lxc_check_witness_exist --name=$name --witness="/etc/systemd/system/witnessfile.service" + + # Database + if ! ynh_lxc_run_inside --name=$name --command="mysqlshow witnessdb > /dev/null 2>&1" + then + log_error "The database witnessdb is missing ! Something gone wrong !" + SET_RESULT "failure" witness + return 1 + fi +} From 02985e0342e64f9a56793c1221c907c0fe1ae12d Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 15 Oct 2022 21:25:09 +0200 Subject: [PATCH 15/25] Update ynh_lxd --- ynh_lxd | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ynh_lxd b/ynh_lxd index 5e6abe7..7c1b65a 100644 --- a/ynh_lxd +++ b/ynh_lxd @@ -336,7 +336,7 @@ ynh_lxc_create(){ log_critical "Can't find base image $image" fi - if [ ! -z "$PACKAGE_CHECK_EXEC" ] + if [ ! -z "$FOR_PACKAGE_CHECK" ] then pipestatus="${PIPESTATUS[0]}" location=$(ynh_exec_as $app lxc list --format json | jq -e --arg name $name '.[] | select(.name==$name) | .location' | tr -d '"') @@ -346,7 +346,7 @@ ynh_lxc_create(){ fi _ynh_lxc_start_and_wait --name="$name" - if [ ! -z "$PACKAGE_CHECK_EXEC" ] + if [ ! -z "$FOR_PACKAGE_CHECK" ] then ynh_lxc_set_witness --name="$name" fi @@ -446,7 +446,7 @@ ynh_lxc_create_snapshot() { ynh_lxc_start_timer - if [ ! -z "$PACKAGE_CHECK_EXEC" ] + if [ ! -z "$FOR_PACKAGE_CHECK" ] then # Check all the witness files, to verify if them still here ynh_lxc_check_witness >&2 @@ -799,6 +799,8 @@ ynh_lxc_stop_timer() { # PACKAGE_CHECK helpers #================================================= +# ynh_lxd commands have to be launch with FOR_PACKAGE_CHECK=1 + # Start an LXC and execute a command in it, to be used for PACKAGE_CHECK_EXEC # # usage: ynh_lxc_exec --name=name --command=command From 2737911d076388fc78fe4cb731828c294dd2bf8d Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 15 Oct 2022 22:25:49 +0200 Subject: [PATCH 16/25] More translations --- demo_lxc_build.sh | 88 +++++++++++++++++++++++-------------------- demo_lxc_destroy.sh | 10 +++-- demo_restore_crash.sh | 26 +++++++------ demo_start.sh | 14 +++++-- demo_stop.sh | 16 +++++--- demo_switch.sh | 7 +++- demo_upgrade.sh | 10 ++--- ynh_lxd | 8 ++-- 8 files changed, 105 insertions(+), 74 deletions(-) diff --git a/demo_lxc_build.sh b/demo_lxc_build.sh index 84a2985..7811351 100755 --- a/demo_lxc_build.sh +++ b/demo_lxc_build.sh @@ -32,23 +32,26 @@ lxc_base="ynh-dev-$DIST-$ARCH-$YNH_BRANCH-base" LOG=Build_lxc.log LOG_BUILD_LXC="$final_path/$LOG" -if $(ynh_lxc_exists --name="$name") -then # Si le conteneur existe déjà - ynh_print_info --message="> Suppression du conteneur existant." | tee -a "$LOG_BUILD_LXC" 2>&1 +date | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message=">> Starting demo build." | tee -a "$LOG_BUILD_LXC" 2>&1 + +if ynh_lxc_exists --name="$name" +then + ynh_print_info --message="> Deleting existing LXC containers." | tee -a "$LOG_BUILD_LXC" 2>&1 /bin/bash "$final_path/demo_lxc_destroy.sh" quiet | tee -a "$LOG_BUILD_LXC" 2>&1 fi -ynh_print_info --message="> Création d'une machine debian $DIST minimaliste" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> Creating a YunoHost $DIST $ARCH $YNH_BRANCH" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_create --image="$lxc_base" --name="$lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message= "> Active le bridge réseau" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message= "> Creating the $lxdbr_demo_name bridge" | tee -a "$LOG_BUILD_LXC" 2>&1 lxc network attach $lxdbr_demo_name $lxc_name1 eth1 eth1 | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message="> Configuration réseau de la machine virtualisée" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> Configuring network of the LXC container" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="cp /etc/systemd/network/eth0.network /etc/systemd/network/eth1.network" ynh_lxc_run_inside --name="$lxc_name1" --command="sed -i s/eth0/eth1/g /etc/systemd/network/eth1.network" -ynh_print_info --message="> Update de la machine virtualisée" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> Update of the LXC container" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y update" ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y full-upgrade" ynh_lxc_run_inside --name="$lxc_name1" --command="apt-get -y autoremove" @@ -60,65 +63,64 @@ ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost tools postinstall --d ynh_print_info --message="> Disable password strength" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost settings set security.password.user.strength -v -1" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message="> Ajout de l'utilisateur de demo" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> Add demo user" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost user create $demo_user --firstname $demo_user --lastname $demo_user --domain $domain --password $demo_password" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message="> Vérification de l'état de Yunohost" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> Check YunoHost state" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost -v" | tee -a "$LOG_BUILD_LXC" 2>&1 # ******** -ynh_print_info --message="> Modification de Yunohost pour la demo" | tee -a "$LOG_BUILD_LXC" 2>&1 + +ynh_print_info --message="> Installing demo apps" | tee -a "$LOG_BUILD_LXC" 2>&1 if [ ${DONT_INSTALL_FOR_NOW:-0} -eq 1 ]; then -# App officielles -ynh_print_info --message="> Installation des applications officielles" | tee -a "$LOG_BUILD_LXC" 2>&1 # Ampache -ynh_print_info --message="Installation de Ampache" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing Ampache" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install ampache --force --args \"domain=$domain&path=/ampache&admin=$demo_user&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Baikal -ynh_print_info --message="Installation de baikal" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing baikal" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install baikal --force --args \"domain=$domain&path=/baikal&password=$demo_password&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Agendav ynh_print_info --message="Installation d'agendav" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install agendav --force --args \"domain=$domain&path=/agendav&language=en&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Dokuwiki -ynh_print_info --message="Installation de dokuwiki" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing dokuwiki" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install dokuwiki --force --args \"domain=$domain&path=/dokuwiki&admin=$demo_user&is_public=1&language=en&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Etherpad -ynh_print_info --message="Installation de etherpad" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing etherpad" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install etherpad_mypads --force --args \"domain=$domain&path=/etherpad&admin=$demo_user&password=administration&language=en&is_public=1&export=none&mypads=1&useldap=0&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Hextris -ynh_print_info --message="Installation de hextris" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing hextris" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install hextris --force --args \"domain=$domain&path=/hextris&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Jirafeau -ynh_print_info --message="Installation de jirafeau" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing jirafeau" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install jirafeau --force --args \"domain=$domain&path=/jirafeau&admin_user=$demo_user&upload_password=$demo_password&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Kanboard -ynh_print_info --message="Installation de kanboard" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing kanboard" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install kanboard --force --args \"domain=$domain&path=/kanboard&admin=$demo_user&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Nextcloud -ynh_print_info --message="Installation de nextcloud" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing nextcloud" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install nextcloud --force --args \"domain=$domain&path=/nextcloud&admin=$demo_user&user_home=0&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Opensondage -ynh_print_info --message="Installation de opensondage" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing opensondage" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install opensondage --force --args \"domain=$domain&path=/date&admin=$demo_user&language=en&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Phpmyadmin -ynh_print_info --message="Installation de phpmyadmin" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing phpmyadmin" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install phpmyadmin --force --args \"domain=$domain&path=/phpmyadmin&admin=$demo_user&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Piwigo -ynh_print_info --message="Installation de piwigo" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing piwigo" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install piwigo --force --args \"domain=$domain&path=/piwigo&admin=$demo_user&is_public=1&language=en&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Rainloop -ynh_print_info --message="Installation de rainloop" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing rainloop" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install rainloop --force --args \"domain=$domain&path=/rainloop&is_public=No&password=$demo_password&ldap=Yes&language=en&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Roundcube -ynh_print_info --message="Installation de roundcube" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing roundcube" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install roundcube --force --args \"domain=$domain&path=/webmail&with_carddav=0&with_enigma=0&language=en_GB&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Searx -ynh_print_info --message="Installation de searx" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing searx" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install searx --force --args \"domain=$domain&path=/searx&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Shellinabox -ynh_print_info --message="Installation de shellinabox" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing shellinabox" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install shellinabox --force --args \"domain=$domain&path=/ssh&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Désactive l'accès à shellinabox ynh_lxc_run_inside --name="$lxc_name1" --command="rm /etc/nginx/conf.d/$domain.d/shellinabox.conf" @@ -126,46 +128,47 @@ ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app setting shellinab ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app setting shellinabox domain -d" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app ssowatconf" # Strut -ynh_print_info --message="Installation de strut" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing strut" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install strut --force --args \"domain=$domain&path=/strut&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Transmission -ynh_print_info --message="Installation de transmission" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing transmission" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install transmission --force --args \"domain=$domain&path=/torrent&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Ttrss -ynh_print_info --message="Installation de ttrss" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing ttrss" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install ttrss --force --args \"domain=$domain&path=/ttrss&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Wallabag -ynh_print_info --message="Installation de wallabag" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing wallabag" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wallabag2 --force --args \"domain=$domain&path=/wallabag&admin=$demo_user&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Wordpress -ynh_print_info --message="Installation de wordpress" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing wordpress" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wordpress --force --args \"domain=$domain&path=/blog&admin=$demo_user&language=en_US&multisite=0&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Zerobin -ynh_print_info --message="Installation de zerobin" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="installing zerobin" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install zerobin --force --args \"domain=$domain&path=/zerobin&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 fi + # ******** -ynh_print_info --message="> Création d'un snapshot" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> Creating a snapshot for $lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_create_snapshot --name="$lxc_name1" --snapname="snap0" -ynh_print_info --message="> Mise à jour de la machine virtualisée" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> Upgrading the $lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_stop --name="$lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_upgrade_demo --name=$lxc_name1 --time_to_switch=$time_to_switch -ynh_print_info --message="> Clone la machine" | tee -a "$LOG_BUILD_LXC" 2>&1 -lxc copy "$lxc_name1" "$lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> Cloning $lxc_name1 to $lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_clone --source="$lxc_name1" --destination="$lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_print_info --message="> Création d'un snapshot" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> Creating a snapshot for $lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_create_snapshot --name="$lxc_name2" --snapname="snap0" -ynh_print_info --message="> Mise en place du cron de switch" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> Setuping the switch cron" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_add_config --template="$final_path/conf/cron_demo_switch" --destination="/etc/cron.d/demo_switch" -ynh_print_info --message="> Et du cron d'upgrade" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> and the upgrade cron" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_add_config --template="$final_path/conf/cron_demo_upgrade" --destination="/etc/cron.d/demo_upgrade" -ynh_print_info --message="> Mise en place du service" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> Setuping the service" | tee -a "$LOG_BUILD_LXC" 2>&1 #ynh_add_systemd_config --template="$final_path/conf/systemd.service" ynh_add_systemd_config @@ -174,3 +177,6 @@ yunohost service add $app --log="/var/log/$app/$app.log" ynh_print_info --message="> Starting a systemd service..." | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" + +date | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message=">> Demo build finished." | tee -a "$LOG_BUILD_LXC" 2>&1 diff --git a/demo_lxc_destroy.sh b/demo_lxc_destroy.sh index 575b34b..d78e83e 100755 --- a/demo_lxc_destroy.sh +++ b/demo_lxc_destroy.sh @@ -17,19 +17,21 @@ lxdbr_demo_network=$(ynh_app_setting_get --app=$app --key=lxdbr_demo_network) lxc_ip1=$(ynh_app_setting_get --app=$app --key=lxc_ip1) lxc_ip2=$(ynh_app_setting_get --app=$app --key=lxc_ip2) +ynh_print_info --message=">> Starting demo destroy." + /bin/bash "$final_path/demo_stop.sh" -f -ynh_print_info --message="> Suppression des conteneurs et de leur snapshots" +ynh_print_info --message="> Deleting containers and snapshots" ynh_secure_remove --file="/var/lib/lxd/snapshots/$lxc_name1/snap0.tar.gz" ynh_lxc_delete --name=$lxc_name1 ynh_secure_remove --file="/var/lib/lxd/snapshots/$lxc_name2/snap0.tar.gz" ynh_lxc_delete --name=$lxc_name2 -ynh_print_info --message="> Suppression des crons" +ynh_print_info --message="> Deleting crons" ynh_secure_remove --file=/etc/cron.d/demo_switch ynh_secure_remove --file=/etc/cron.d/demo_upgrade -ynh_print_info --message="> Suppression du service" +ynh_print_info --message="> Deleting service" if ynh_exec_warn_less yunohost service status $app >/dev/null then ynh_print_info --message="Removing $app service integration..." @@ -37,3 +39,5 @@ then fi ynh_print_info --message="Stopping and removing the systemd service..." ynh_remove_systemd_config + +ynh_print_info --message=">> Finished demo destroy." diff --git a/demo_restore_crash.sh b/demo_restore_crash.sh index 76d8478..a9236fe 100755 --- a/demo_restore_crash.sh +++ b/demo_restore_crash.sh @@ -17,10 +17,12 @@ lxc_ip2=$(ynh_app_setting_get --app=$app --key=lxc_ip2) lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) lxc_name2=$(ynh_app_setting_get --app=$app --key=lxc_name2) -ynh_print_info --message="Désactive le cron switch." +ynh_print_info --message=">> Starting demo restore from crash." + +ynh_print_info --message="> Disabling switch cron." sed -i "s/.*demo_switch.sh/#&/" /etc/cron.d/demo_switch # Le cron est commenté durant l'opération de maintenance. -ynh_print_info --message="Suppression des lock et arrêt forcé des conteneurs." +ynh_print_info --message="> Deleting locks and stoping LXC containers." ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileS" ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileS" ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileU" @@ -36,14 +38,14 @@ ynh_lxc_check_container_start --name=$lxc_name2 LXC2_STATUS=$? if [ $LXC1_STATUS -eq 1 ]; then - ynh_print_info --message="> Conteneur $lxc_name1 en défaut." + ynh_print_info --message="> LXC container $lxc_name1 is broken." else - ynh_print_info --message="> Conteneur $lxc_name1 en état de marche." + ynh_print_info --message="> LXC container $lxc_name1 is working." fi if [ $LXC2_STATUS -eq 1 ]; then - ynh_print_info --message="> Conteneur $lxc_name2 en défaut." + ynh_print_info --message="> LXC container $lxc_name2 is broken." else - ynh_print_info --message="> Conteneur $lxc_name2 en état de marche." + ynh_print_info --message="> LXC container $lxc_name2 is working." fi # Restauration des snapshots @@ -79,16 +81,18 @@ fi # Résultats finaux if [ $LXC1_STATUS -eq 1 ] || [ $LXC2_STATUS -eq 1 ]; then if [ $LXC1_STATUS -eq 1 ]; then - ynh_print_info --message="> Le conteneur $lxc_name1 n'a pas pu être réparé..." + ynh_print_info --message="> $lxc_name1 LXC container can't be repaired..." fi if [ $LXC2_STATUS -eq 1 ]; then - ynh_print_info --message="> Le conteneur $lxc_name2 n'a pas pu être réparé..." + ynh_print_info --message="> $lxc_name2 LXC container can't be repaired..." fi else - ynh_print_info --message="> Les 2 conteneurs sont sains et fonctionnels." + ynh_print_info --message="> The 2 LXC containers are working." fi -ynh_print_info --message="Réactive le cron switch." +ynh_print_info --message="> Enabling switch cron." sed -i "s/#*\*/\*/" /etc/cron.d/demo_switch # Le cron est décommenté -ynh_print_info --message="Restart la demo." +ynh_print_info --message="> Restart the demo." $final_path/demo_start.sh + +ynh_print_info --message=">> Finished demo restore from crash." diff --git a/demo_start.sh b/demo_start.sh index a2be13a..6aff977 100755 --- a/demo_start.sh +++ b/demo_start.sh @@ -15,11 +15,17 @@ lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) lxdbr_demo_network=$(ynh_app_setting_get --app=$app --key=lxdbr_demo_network) lxc_ip1=$(ynh_app_setting_get --app=$app --key=lxc_ip1) +date | tee -a "$final_path/demo_boot.log" 2>&1 +ynh_print_info --message=">> Starting demo." | tee -a "$final_path/demo_boot.log" 2>&1 + /bin/bash "$final_path/demo_stop.sh" > /dev/null 2>&1 # Démarrage de la machine -ynh_print_info --message="> Démarrage de la machine" | tee -a "$final_path/demo_boot.log" -date | tee -a "$final_path/demo_boot.log" -ynh_print_info --message="Starting $lxc_name1" | tee -a "$final_path/demo_boot.log" -ynh_lxc_start_as_demo --name=$lxc_name1 --ip="$lxdbr_demo_network$lxc_ip1" | tee -a "$final_path/demo_boot.log" +ynh_print_info --message="> Démarrage de la machine" | tee -a "$final_path/demo_boot.log" 2>&1 +date | tee -a "$final_path/demo_boot.log" 2>&1 +ynh_print_info --message="Starting $lxc_name1" | tee -a "$final_path/demo_boot.log" 2>&1 +ynh_lxc_start_as_demo --name=$lxc_name1 --ip="$lxdbr_demo_network$lxc_ip1" | tee -a "$final_path/demo_boot.log" 2>&1 sleep 3 + +date | tee -a "$final_path/demo_boot.log" 2>&1 +ynh_print_info --message=">> Finished starting demo." | tee -a "$final_path/demo_boot.log" 2>&1 diff --git a/demo_stop.sh b/demo_stop.sh index d4dd1e5..8e3cd63 100755 --- a/demo_stop.sh +++ b/demo_stop.sh @@ -14,26 +14,29 @@ final_path=$(ynh_app_setting_get --app=$app --key=final_path) lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) lxc_name2=$(ynh_app_setting_get --app=$app --key=lxc_name2) +date | tee -a "$final_path/demo_boot.log" 2>&1 +ynh_print_info --message=">> Stopping demo." | tee -a "$final_path/demo_boot.log" 2>&1 + if [ "$#" -eq 1 ] && [ "$1" == "-f" ] then - ynh_print_info --message="> Suppression des lock et arrêt forcé des conteneurs." + ynh_print_info --message="> Deleting locks and force stopping LXC containers." | tee -a "$final_path/demo_boot.log" 2>&1 ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileS" ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileS" ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileU" ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileU" else - ynh_print_info --message="> Attend la libération des lock sur les conteneurs." + ynh_print_info --message="> Waiting locks." | tee -a "$final_path/demo_boot.log" 2>&1 while test -e /var/lib/lxd/$lxc_name1.lock_file* || test -e /var/lib/lxd/$lxc_name2.lock_file*; do sleep 5 # Attend que les conteneur soit libérés par les script upgrade ou switch, le cas échéant. done fi -ynh_print_info --message="> Arrêt des conteneurs" +ynh_print_info --message="> Stopping LXC containers" | tee -a "$final_path/demo_boot.log" 2>&1 if ynh_lxc_exists --name=$lxc_name1 then if ! ynh_lxc_is_stopped --name=$lxc_name1 then - ynh_print_info --message="Arrêt du conteneur $lxc_name1" + ynh_print_info --message="Stopping $lxc_name1 LXC container" | tee -a "$final_path/demo_boot.log" 2>&1 ynh_lxc_stop_as_demo --name=$lxc_name1 fi fi @@ -41,7 +44,10 @@ if ynh_lxc_exists --name=$lxc_name2 then if ! ynh_lxc_is_stopped --name=$lxc_name2 then - ynh_print_info --message="Arrêt du conteneur $lxc_name2" + ynh_print_info --message="Stopping $lxc_name2 LXC container" ynh_lxc_stop_as_demo --name=$lxc_name2 fi fi + +date | tee -a "$final_path/demo_boot.log" 2>&1 +ynh_print_info --message=">> Finished stopping demo." | tee -a "$final_path/demo_boot.log" 2>&1 diff --git a/demo_switch.sh b/demo_switch.sh index 9ad9f3c..2786269 100755 --- a/demo_switch.sh +++ b/demo_switch.sh @@ -22,7 +22,9 @@ path_url=$(ynh_app_setting_get --app=$app --key=path) log_line=$(wc -l "$final_path/demo_switch.log" | cut -d ' ' -f 1) # Repère la fin du log actuel. Pour récupérer les lignes ajoutées sur cette exécution. log_line=$(( $log_line + 1 )) # Ignore la première ligne, reprise de l'ancien log. -date >> "$final_path/demo_switch.log" + +date | tee -a "$final_path/demo_switch.log" 2>&1 +ynh_print_info --message=">> Switching demo." | tee -a "$final_path/demo_switch.log" 2>&1 while test -e /var/lib/lxd/$lxc_name1.lock_file* || test -e /var/lib/lxd/$lxc_name2.lock_file*; do sleep 5 # Attend que le conteneur soit libéré par les script upgrade ou switch, le cas échéant. @@ -72,3 +74,6 @@ else ynh_secure_remove --file="/var/lib/lxd/$LXC_A.lock_fileS" # Libère le lock ynh_print_info --message="Finish restoring $LXC_A" fi + +date | tee -a "$final_path/demo_switch.log" 2>&1 +ynh_print_info --message=">> Finished switching demo." | tee -a "$final_path/demo_switch.log" 2>&1 diff --git a/demo_upgrade.sh b/demo_upgrade.sh index 87b3e4b..7150ca5 100755 --- a/demo_upgrade.sh +++ b/demo_upgrade.sh @@ -17,16 +17,16 @@ lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) lxc_name2=$(ynh_app_setting_get --app=$app --key=lxc_name2) time_to_switch=$(ynh_app_setting_get --app=$app --key=time_to_switch) -IP_UPGRADE=$lxdbr_demo_network.150 LOOP=0 log_line=$(wc -l "$final_path/demo_upgrade.log" | cut -d ' ' -f 1) # Repère la fin du log actuel. Pour récupérer les lignes ajoutées sur cette exécution. log_line=$(( $log_line + 1 )) # Ignore la première ligne, reprise de l'ancien log. -date >> "$final_path/demo_upgrade.log" +date | tee -a "$final_path/demo_upgrade.log" 2>&1 +ynh_print_info --message=">> Upgrading demo." | tee -a "$final_path/demo_upgrade.log" 2>&1 -ynh_print_info --message="Starting upgrade..." -date ynh_lxc_upgrade_demo --name=$lxc_name1 --time_to_switch=$time_to_switch ynh_lxc_upgrade_demo --name=$lxc_name2 --time_to_switch=$time_to_switch -ynh_print_info --message="Upgrade finished..." + +date | tee -a "$final_path/demo_upgrade.log" 2>&1 +ynh_print_info --message=">> Finished upgrading demo." | tee -a "$final_path/demo_upgrade.log" 2>&1 diff --git a/ynh_lxd b/ynh_lxd index 7c1b65a..846e6cc 100644 --- a/ynh_lxd +++ b/ynh_lxd @@ -547,10 +547,10 @@ ynh_lxc_restore_from_snapshot () { ynh_lxc_load_snapshot --name=$name --snapname=snap0 if ynh_lxc_check_container_start --name=$name then - ynh_print_info --message="\e[92m> Conteneur $name en état de marche.\e[0m" + ynh_print_info --message="\e[92m> LXC container $name is working.\e[0m" return 0 else - ynh_print_info --message="\e[91m> Conteneur $name en défaut.\e[0m" + ynh_print_info --message="\e[91m> LXC container $name is broken.\e[0m" return 1 fi } @@ -608,9 +608,9 @@ ynh_lxc_clone() { ynh_lxc_check_container_start --name=$destination STATUS=$? if [ $STATUS -eq 1 ]; then - ynh_print_info --message="\e[91m> Conteneur $destination en défaut.\e[0m" + ynh_print_info --message="\e[91m> LXC container $destination is broken.\e[0m" else - ynh_print_info --message="\e[92m> Conteneur $destination en état de marche.\e[0m" + ynh_print_info --message="\e[92m> LXC container $destination is working.\e[0m" ynh_print_info --message= "Création d'un nouveau snapshot pour le conteneur $destination" ynh_lxc_create_snapshot --name="$destination" --snapname="snap0" fi From af5d9e4f3702e7a4d5da4cb560b37a93da6e1a0e Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 15 Oct 2022 22:47:40 +0200 Subject: [PATCH 17/25] more print --- demo_lxc_destroy.sh | 4 ++-- demo_start.sh | 2 +- demo_stop.sh | 4 ++-- demo_switch.sh | 10 +++++----- ynh_lxd_demo | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/demo_lxc_destroy.sh b/demo_lxc_destroy.sh index d78e83e..889792e 100755 --- a/demo_lxc_destroy.sh +++ b/demo_lxc_destroy.sh @@ -34,10 +34,10 @@ ynh_secure_remove --file=/etc/cron.d/demo_upgrade ynh_print_info --message="> Deleting service" if ynh_exec_warn_less yunohost service status $app >/dev/null then - ynh_print_info --message="Removing $app service integration..." + ynh_print_info --message="> Removing $app service integration..." yunohost service remove $app fi -ynh_print_info --message="Stopping and removing the systemd service..." +ynh_print_info --message="> Stopping and removing the systemd service..." ynh_remove_systemd_config ynh_print_info --message=">> Finished demo destroy." diff --git a/demo_start.sh b/demo_start.sh index 6aff977..acb145c 100755 --- a/demo_start.sh +++ b/demo_start.sh @@ -23,7 +23,7 @@ ynh_print_info --message=">> Starting demo." | tee -a "$final_path/demo_boot.log # Démarrage de la machine ynh_print_info --message="> Démarrage de la machine" | tee -a "$final_path/demo_boot.log" 2>&1 date | tee -a "$final_path/demo_boot.log" 2>&1 -ynh_print_info --message="Starting $lxc_name1" | tee -a "$final_path/demo_boot.log" 2>&1 +ynh_print_info --message="> Starting $lxc_name1" | tee -a "$final_path/demo_boot.log" 2>&1 ynh_lxc_start_as_demo --name=$lxc_name1 --ip="$lxdbr_demo_network$lxc_ip1" | tee -a "$final_path/demo_boot.log" 2>&1 sleep 3 diff --git a/demo_stop.sh b/demo_stop.sh index 8e3cd63..f5b2024 100755 --- a/demo_stop.sh +++ b/demo_stop.sh @@ -36,7 +36,7 @@ if ynh_lxc_exists --name=$lxc_name1 then if ! ynh_lxc_is_stopped --name=$lxc_name1 then - ynh_print_info --message="Stopping $lxc_name1 LXC container" | tee -a "$final_path/demo_boot.log" 2>&1 + ynh_print_info --message="> Stopping $lxc_name1 LXC container" | tee -a "$final_path/demo_boot.log" 2>&1 ynh_lxc_stop_as_demo --name=$lxc_name1 fi fi @@ -44,7 +44,7 @@ if ynh_lxc_exists --name=$lxc_name2 then if ! ynh_lxc_is_stopped --name=$lxc_name2 then - ynh_print_info --message="Stopping $lxc_name2 LXC container" + ynh_print_info --message="> Stopping $lxc_name2 LXC container" ynh_lxc_stop_as_demo --name=$lxc_name2 fi fi diff --git a/demo_switch.sh b/demo_switch.sh index 2786269..524a98b 100755 --- a/demo_switch.sh +++ b/demo_switch.sh @@ -48,18 +48,18 @@ fi # Supprime les éventuels swap présents. /sbin/swapoff /var/lib/lxd/$LXC_A/rootfs/swap_* -ynh_print_info --message="Starting $LXC_B" +ynh_print_info --message="> Starting $LXC_B" # Démarre le conteneur B et arrête le conteneur A. ynh_lxc_start_as_demo --name=$LXC_B --ip=$IP_B sleep 5 # Attend 10 seconde pour s'assurer du démarrage de la machine. if ! ynh_lxc_is_started --name=$LXC_B then # Le conteneur n'a pas réussi à démarrer. On averti un responsable par mail... - echo -e "Échec du démarrage du conteneur $LXC_B sur le serveur de demo $DOMAIN! \n\nExtrait du log:\n$(tail -n +$log_line "$final_path/demo_switch.log")\n\nLe script 'demo_restore_crash.sh' va être exécuté pour tenter de fixer l'erreur." | mail -a "Content-Type: text/plain; charset=UTF-8" -s "Demo Yunohost" $MAIL_ADDR + ynh_print_info --message="> Échec du démarrage du conteneur $LXC_B sur le serveur de demo $DOMAIN! \n\nExtrait du log:\n$(tail -n +$log_line "$final_path/demo_switch.log")\n\nLe script 'demo_restore_crash.sh' va être exécuté pour tenter de fixer l'erreur." | mail -a "Content-Type: text/plain; charset=UTF-8" -s "Demo Yunohost" $MAIL_ADDR /bin/bash $final_path/demo_restore_crash.sh & exit 1 else - ynh_print_info --message="Stopping $LXC_A" + ynh_print_info --message="> Stopping $LXC_A" # Bascule sur le conteneur B avec le load balancing de nginx... # Automatique par nginx lorsque la machine A sera éteinte. # Arrêt du conteneur A. Il est remplacé par le B @@ -67,12 +67,12 @@ else ynh_lxc_stop_as_demo --name=$LXC_A # Supprime les éventuels swap présents. /sbin/swapoff /var/lib/lxd/$LXC_A/rootfs/swap_* - ynh_print_info --message="Restauring $LXC_A from snapshot" + ynh_print_info --message="> Restauring $LXC_A from snapshot" # Restaure le snapshot de la machine A avant sa prochaine exécution ynh_lxc_load_snapshot --name=$LXC_A --snapname=snap0 ynh_lxc_stop --name=$LXC_A ynh_secure_remove --file="/var/lib/lxd/$LXC_A.lock_fileS" # Libère le lock - ynh_print_info --message="Finish restoring $LXC_A" + ynh_print_info --message="> Finish restoring $LXC_A" fi date | tee -a "$final_path/demo_switch.log" 2>&1 diff --git a/ynh_lxd_demo b/ynh_lxd_demo index a7fc044..e16b3c3 100644 --- a/ynh_lxd_demo +++ b/ynh_lxd_demo @@ -118,7 +118,7 @@ ynh_lxc_upgrade_demo() { if [ "$?" -ne 0 ]; then # Si le script a échoué, le snapshot est annulé. ynh_print_info --message="Échec du script $LIGNE" mv -f "$final_path/upgrade.d/$LIGNE" "$final_path/upgrade.d/$LIGNE.fail" - echo -e "Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\nLe script a été renommé en .fail, il ne sera plus exécuté tant que le préfixe ne sera pas retiré.\n\nExtrait du log:\n$(tail -n +$log_line "$script_dir/demo_upgrade.log")" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "Demo Yunohost" $MAIL_ADDR + ynh_print_info --message="Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\nLe script a été renommé en .fail, il ne sera plus exécuté tant que le préfixe ne sera pas retiré.\n\nExtrait du log:\n$(tail -n +$log_line "$script_dir/demo_upgrade.log")" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "Demo Yunohost" $MAIL_ADDR update_apt=0 else ynh_print_info --message="Le script $LIGNE a été exécuté sans erreur" @@ -138,7 +138,7 @@ ynh_lxc_upgrade_demo() { /bin/bash "$final_path/upgrade.d/Constant_upgrade/$LIGNE" $name if [ "$?" -ne 0 ]; then ynh_print_info --message="Échec du script $LIGNE" - echo -e "Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\n" + ynh_print_info --message="Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\n" else ynh_print_info --message="Le script $LIGNE a été exécuté sans erreur" fi From c3dee09570832ceffa8e92221ed0b87fb29b8500 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 15 Oct 2022 23:36:15 +0200 Subject: [PATCH 18/25] less warnings --- demo_lxc_build.sh | 10 +++++----- demo_lxc_destroy.sh | 4 ++-- demo_stop.sh | 8 ++++---- ynh_lxd_demo | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/demo_lxc_build.sh b/demo_lxc_build.sh index 7811351..02b215c 100755 --- a/demo_lxc_build.sh +++ b/demo_lxc_build.sh @@ -152,21 +152,21 @@ fi ynh_print_info --message="> Creating a snapshot for $lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_create_snapshot --name="$lxc_name1" --snapname="snap0" -ynh_print_info --message="> Upgrading the $lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="> Upgrading the $lxc_name1 LXC container" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_stop --name="$lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_lxc_upgrade_demo --name=$lxc_name1 --time_to_switch=$time_to_switch +ynh_lxc_upgrade_demo --name=$lxc_name1 --time_to_switch=$time_to_switch | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_print_info --message="> Cloning $lxc_name1 to $lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_clone --source="$lxc_name1" --destination="$lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_print_info --message="> Creating a snapshot for $lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_lxc_create_snapshot --name="$lxc_name2" --snapname="snap0" +ynh_lxc_create_snapshot --name="$lxc_name2" --snapname="snap0" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_print_info --message="> Setuping the switch cron" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_add_config --template="$final_path/conf/cron_demo_switch" --destination="/etc/cron.d/demo_switch" +ynh_add_config --template="$final_path/conf/cron_demo_switch" --destination="/etc/cron.d/demo_switch" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_print_info --message="> and the upgrade cron" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_add_config --template="$final_path/conf/cron_demo_upgrade" --destination="/etc/cron.d/demo_upgrade" +ynh_add_config --template="$final_path/conf/cron_demo_upgrade" --destination="/etc/cron.d/demo_upgrade" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_print_info --message="> Setuping the service" | tee -a "$LOG_BUILD_LXC" 2>&1 #ynh_add_systemd_config --template="$final_path/conf/systemd.service" diff --git a/demo_lxc_destroy.sh b/demo_lxc_destroy.sh index 889792e..9e265d8 100755 --- a/demo_lxc_destroy.sh +++ b/demo_lxc_destroy.sh @@ -22,9 +22,9 @@ ynh_print_info --message=">> Starting demo destroy." /bin/bash "$final_path/demo_stop.sh" -f ynh_print_info --message="> Deleting containers and snapshots" -ynh_secure_remove --file="/var/lib/lxd/snapshots/$lxc_name1/snap0.tar.gz" +ynh_exec_warn_less ynh_secure_remove --file="/var/lib/lxd/snapshots/$lxc_name1/snap0.tar.gz" ynh_lxc_delete --name=$lxc_name1 -ynh_secure_remove --file="/var/lib/lxd/snapshots/$lxc_name2/snap0.tar.gz" +ynh_exec_warn_less ynh_secure_remove --file="/var/lib/lxd/snapshots/$lxc_name2/snap0.tar.gz" ynh_lxc_delete --name=$lxc_name2 ynh_print_info --message="> Deleting crons" diff --git a/demo_stop.sh b/demo_stop.sh index f5b2024..c9d9b20 100755 --- a/demo_stop.sh +++ b/demo_stop.sh @@ -20,10 +20,10 @@ ynh_print_info --message=">> Stopping demo." | tee -a "$final_path/demo_boot.log if [ "$#" -eq 1 ] && [ "$1" == "-f" ] then ynh_print_info --message="> Deleting locks and force stopping LXC containers." | tee -a "$final_path/demo_boot.log" 2>&1 - ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileS" - ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileS" - ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileU" - ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileU" + ynh_exec_warn_less ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileS" + ynh_exec_warn_less ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileS" + ynh_exec_warn_less ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileU" + ynh_exec_warn_less ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileU" else ynh_print_info --message="> Waiting locks." | tee -a "$final_path/demo_boot.log" 2>&1 while test -e /var/lib/lxd/$lxc_name1.lock_file* || test -e /var/lib/lxd/$lxc_name2.lock_file*; do diff --git a/ynh_lxd_demo b/ynh_lxd_demo index e16b3c3..51a99db 100644 --- a/ynh_lxd_demo +++ b/ynh_lxd_demo @@ -157,7 +157,7 @@ ynh_lxc_upgrade_demo() { if [ "$update_apt" -eq "1" ] then # Archivage du snapshot - tar -cz --acls --xattrs -f /var/lib/lxd/snapshots/$name/snap0.tar.gz /var/lib/lxd/snapshots/$name/snap0 + ynh_exec_warn_less tar -cz --acls --xattrs -f /var/lib/lxd/snapshots/$name/snap0.tar.gz /var/lib/lxd/snapshots/$name/snap0 # Remplacement du snapshot ynh_lxc_create_snapshot --name=$name --snapname=snap0 From ed5976b72219683de681fa0f532d09367003dfdc Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 16 Oct 2022 00:07:42 +0200 Subject: [PATCH 19/25] More translations --- ynh_lxd | 51 ++++++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/ynh_lxd b/ynh_lxd index 846e6cc..45478be 100644 --- a/ynh_lxd +++ b/ynh_lxd @@ -543,14 +543,14 @@ ynh_lxc_restore_from_snapshot () { # Manage arguments with getopts ynh_handle_getopts_args "$@" - ynh_print_info --message="\e[1m> Restauration du snapshot du conteneur $name\e[0m" + ynh_print_info --message="Restoring LXC container $name from snapshot" ynh_lxc_load_snapshot --name=$name --snapname=snap0 if ynh_lxc_check_container_start --name=$name then - ynh_print_info --message="\e[92m> LXC container $name is working.\e[0m" + ynh_print_info --message="LXC container $name is working." return 0 else - ynh_print_info --message="\e[91m> LXC container $name is broken.\e[0m" + ynh_print_info --message="LXC container $name is broken." return 1 fi } @@ -570,13 +570,13 @@ ynh_lxc_restore_from_archive () { ynh_handle_getopts_args "$@" if ! test -e "/var/lib/lxd/snapshots/$name/snap1.tar.gz"; then - ynh_print_info --message="Aucune archive de snapshot pour le conteneur $name" + ynh_print_info --message="No snapshot archive for LXC container $name" return 1 fi - ynh_print_info --message="\e[1m> Restauration du snapshot archivé pour le conteneur $name\e[0m" - ynh_print_info --message="Suppression du snapshot" + ynh_print_info --message="Restoring snapshot archive for LXC container $name" + ynh_print_info --message="Deleting snapshot" ynh_lxc_delete_snapshot --name=$name --snapname=snap0 - ynh_print_info --message="Décompression de l'archive" + ynh_print_info --message="Untar archive" tar -x --acls --xattrs -f /var/lib/lxd/snapshots/$name/snap0.tar.gz -C / ynh_lxc_restore_from_snapshot --name=$name return $? @@ -596,45 +596,30 @@ ynh_lxc_clone() { local source local destination # Manage arguments with getopts - ynh_handle_getopts_args "$@" + ynh_handle_getopts_args "$@" - ynh_print_info --message="Suppression du conteneur $destination" - ynh_secure_remove --file="/var/lib/lxd/snapshots/$destination/snap0.tar.gz" - ynh_lxc_reset --name=$destination + if ynh_lxc_exists --name=$destination + then + ynh_print_info --message="Deleting LXC container $destination" + ynh_secure_remove --file="/var/lib/lxd/snapshots/$destination/snap0.tar.gz" + ynh_lxc_reset --name=$destination + fi - ynh_print_info --message="\e[1m> Clone le conteneur $source sur $destination\e[0m" + ynh_print_info --message="Cloning LXC container from $source to $destination" lxc copy "$source" "$destination" ynh_lxc_check_container_start --name=$destination STATUS=$? if [ $STATUS -eq 1 ]; then - ynh_print_info --message="\e[91m> LXC container $destination is broken.\e[0m" + ynh_print_info --message="LXC container $destination is broken." else - ynh_print_info --message="\e[92m> LXC container $destination is working.\e[0m" - ynh_print_info --message= "Création d'un nouveau snapshot pour le conteneur $destination" + ynh_print_info --message=" LXC container $destination is working." + ynh_print_info --message= "Creating snapshot of LXC container $destination" ynh_lxc_create_snapshot --name="$destination" --snapname="snap0" fi return $STATUS } -# usage: ynh_lxc_test --name=name - -ynh_lxc_test(){ - # Declare an array to define the options of this helper. - local legacy_args=ns - local -A args_array=([n]=name= [s]=snapname=) - local name - local snapname - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - if ynh_lxc_exists --name=$name - then - ynh_print_info --message="$name exist" - else - ynh_print_info --message="$name does not exist" - fi -} - # Reset an LXC container # # usage: ynh_lxc_reset --name=name From 1a04270c9db41375ef7c579a5c105b97363f0cfa Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 16 Oct 2022 01:38:07 +0200 Subject: [PATCH 20/25] Update demo_switch.sh --- demo_switch.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demo_switch.sh b/demo_switch.sh index 524a98b..771c8ce 100755 --- a/demo_switch.sh +++ b/demo_switch.sh @@ -17,14 +17,14 @@ lxc_ip1=$(ynh_app_setting_get --app=$app --key=lxc_ip1) lxc_ip2=$(ynh_app_setting_get --app=$app --key=lxc_ip2) lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) lxc_name2=$(ynh_app_setting_get --app=$app --key=lxc_name2) -domain=${$(ynh_app_setting_get --app=$app --key=domain):-$(cat "$final_path/domain.ini")} +domain=$(ynh_app_setting_get --app=$app --key=domain) path_url=$(ynh_app_setting_get --app=$app --key=path) log_line=$(wc -l "$final_path/demo_switch.log" | cut -d ' ' -f 1) # Repère la fin du log actuel. Pour récupérer les lignes ajoutées sur cette exécution. log_line=$(( $log_line + 1 )) # Ignore la première ligne, reprise de l'ancien log. date | tee -a "$final_path/demo_switch.log" 2>&1 -ynh_print_info --message=">> Switching demo." | tee -a "$final_path/demo_switch.log" 2>&1 +ynh_print_info --message=">> Start switching demo." | tee -a "$final_path/demo_switch.log" 2>&1 while test -e /var/lib/lxd/$lxc_name1.lock_file* || test -e /var/lib/lxd/$lxc_name2.lock_file*; do sleep 5 # Attend que le conteneur soit libéré par les script upgrade ou switch, le cas échéant. From 9a1ea864fab190011df5259b89c02d8efe327fa3 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 21 Oct 2022 00:52:40 +0200 Subject: [PATCH 21/25] Merging helpers --- demo_restore_crash.sh | 1 - demo_start.sh | 1 - demo_stop.sh | 1 - demo_switch.sh | 1 - demo_upgrade.sh | 1 - ynh_lxd | 184 +++++++++++++++++++++++++++++++++++++++++- ynh_lxd_demo | 177 ---------------------------------------- 7 files changed, 180 insertions(+), 186 deletions(-) delete mode 100644 ynh_lxd_demo diff --git a/demo_restore_crash.sh b/demo_restore_crash.sh index a9236fe..3d5fb13 100755 --- a/demo_restore_crash.sh +++ b/demo_restore_crash.sh @@ -6,7 +6,6 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd -source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} diff --git a/demo_start.sh b/demo_start.sh index acb145c..a08675b 100755 --- a/demo_start.sh +++ b/demo_start.sh @@ -6,7 +6,6 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd -source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} diff --git a/demo_stop.sh b/demo_stop.sh index c9d9b20..bb9ba51 100755 --- a/demo_stop.sh +++ b/demo_stop.sh @@ -6,7 +6,6 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(ynh_print_info --message=$PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd -source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} diff --git a/demo_switch.sh b/demo_switch.sh index 771c8ce..05c2b56 100755 --- a/demo_switch.sh +++ b/demo_switch.sh @@ -7,7 +7,6 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd -source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} diff --git a/demo_upgrade.sh b/demo_upgrade.sh index 7150ca5..cde8987 100755 --- a/demo_upgrade.sh +++ b/demo_upgrade.sh @@ -7,7 +7,6 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd -source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} diff --git a/ynh_lxd b/ynh_lxd index 45478be..f6e35e2 100644 --- a/ynh_lxd +++ b/ynh_lxd @@ -1,7 +1,7 @@ #!/bin/bash #================================================= -# LXC helpers +# LXD HELPERS #================================================= # Check if a LXC container exists @@ -647,7 +647,7 @@ ynh_lxc_reset() { } #================================================= -# Logging helpers +# LOGGING HELPERS #================================================= readonly NORMAL=$(printf '\033[0m') @@ -723,7 +723,7 @@ function log_report_test_failed () { } #================================================= -# Timing helpers +# TIMING HELPERS #================================================= # Start a timer @@ -781,7 +781,7 @@ ynh_lxc_stop_timer() { } #================================================= -# PACKAGE_CHECK helpers +# PACKAGE_CHECK HELPERS #================================================= # ynh_lxd commands have to be launch with FOR_PACKAGE_CHECK=1 @@ -966,3 +966,179 @@ ynh_lxc_check_witness() { return 1 fi } + +#================================================= +# DEMO HELPERS +#================================================= + +# Start an LXC container in demo mode +# +# usage: ynh_lxc_start_as_demo --name=name --ip=ip +# | arg: -n, --name= - name of the LXC +# | arg: -n, --ip= - demo ip of the lxc +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_start_as_demo() { + # Declare an array to define the options of this helper. + local legacy_args=ni + local -A args_array=([n]=name= [i]=ip=) + local name + local ip + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_lxc_stop --name="$name" + lxc config device set $name eth1 ipv4.address $ip + _ynh_lxc_start_and_wait --name="$name" +} + +# Stop an LXC container in demo mode +# +# usage: ynh_lxc_stop_as_demo --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_stop_as_demo() { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_lxc_stop --name="$name" + lxc config device unset $name eth1 ipv4.address +} + +# Upgrading demo container +# +# usage: ynh_lxc_upgrade_demo --name=name --time_to_switch=time_to_switch +# | arg: -n, --name= - name of the LXC +# | arg: -t, --time_to_switch= - time to switch +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_upgrade_demo() { + # Declare an array to define the options of this helper. + local legacy_args=nt + local -A args_array=([n]=name= [t]=time_to_switch=) + local name + local time_to_switch + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_print_info --message="Upgrading $name" + # Attend que la machine soit éteinte. + # Timeout à $time_to_switch +5 minutes, en seconde + TIME_OUT=$(($time_to_switch * 60 + 300)) + wait_period=0 + while ! ynh_lxc_is_stopped --name=$name + do + wait_period=$(($wait_period+10)) + if [ $wait_period -gt $TIME_OUT ];then + break + else + sleep 5 + fi + done + + while test -e /var/lib/lxd/$name.lock_fileS; do + sleep 5 # Attend que le conteneur soit libéré par le script switch. + done + + touch /var/lib/lxd/$name.lock_fileU # Met en place un fichier pour indiquer que la machine est indisponible pendant l'upgrade + + # Supprime les éventuels swap présents. + /sbin/swapoff /var/lib/lxd/$name/rootfs/swap_* + + # Restaure le snapshot + ynh_lxc_load_snapshot --name=$name --snapname=snap0 + + # Démarre le conteneur + date >> "$final_path/demo_boot.log" + + # Update + update_apt=0 + ynh_lxc_run_inside --name="$name" --command="apt-get update" + ynh_lxc_run_inside --name="$name" --command="apt-get dist-upgrade --dry-run | grep -q "^Inst " > /dev/null" # Vérifie si il y aura des mises à jour. + if [ "$?" -eq 0 ]; then + date + update_apt=1 + # Upgrade + ynh_lxc_run_inside --name="$name" --command="apt-get dist-upgrade --option Dpkg::Options::=--force-confold -yy" + # Clean + ynh_lxc_run_inside --name="$name" --command="apt-get autoremove -y" + ynh_lxc_run_inside --name="$name" --command="apt-get autoclean" + fi + ynh_lxc_run_inside --name="$name" --command="yunohost tools update" + ynh_lxc_run_inside --name="$name" --command="yunohost tools upgrade system" + + # Exécution des scripts de upgrade.d + LOOP=$((LOOP + 1)) + while read LIGNE + do + if [ ! "$LIGNE" == "exemple" ] && [ ! "$LIGNE" == "old_scripts" ] && [ ! "$LIGNE" == "Constant_upgrade" ] && ! echo "$LIGNE" | grep -q ".fail$" # Le fichier exemple, le dossier old_scripts et les scripts fail sont ignorés + then + date + # Exécute chaque script trouvé dans upgrade.d + ynh_print_info --message="Exécution du script $LIGNE sur le conteneur $name" + /bin/bash "$final_path/upgrade.d/$LIGNE" $name + if [ "$?" -ne 0 ]; then # Si le script a échoué, le snapshot est annulé. + ynh_print_info --message="Échec du script $LIGNE" + mv -f "$final_path/upgrade.d/$LIGNE" "$final_path/upgrade.d/$LIGNE.fail" + ynh_print_info --message="Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\nLe script a été renommé en .fail, il ne sera plus exécuté tant que le préfixe ne sera pas retiré.\n\nExtrait du log:\n$(tail -n +$log_line "$script_dir/demo_upgrade.log")" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "Demo Yunohost" $MAIL_ADDR + update_apt=0 + else + ynh_print_info --message="Le script $LIGNE a été exécuté sans erreur" + update_apt=1 + fi + fi + done <<< "$(ls -1 "$final_path/upgrade.d")" + + # Exécution des scripts de upgrade.d/Constant_upgrade + while read LIGNE + do + if [ "$update_apt" -eq "1" ] + then + date + # Exécute chaque script trouvé dans upgrade.d/Constant_upgrade + ynh_print_info --message="Exécution du script $LIGNE sur le conteneur $name" + /bin/bash "$final_path/upgrade.d/Constant_upgrade/$LIGNE" $name + if [ "$?" -ne 0 ]; then + ynh_print_info --message="Échec du script $LIGNE" + ynh_print_info --message="Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\n" + else + ynh_print_info --message="Le script $LIGNE a été exécuté sans erreur" + fi + fi + done <<< "$(ls -1 "$final_path/upgrade.d/Constant_upgrade")" + + # Upgrade des apps + ynh_lxc_run_inside --name="$name" --command="yunohost tools update" + ynh_lxc_run_inside --name="$name" --command="systemctl restart nginx" + ynh_lxc_run_inside --name="$name" --command="yunohost tools upgrade apps" + ynh_lxc_run_inside --name="$name" --command="systemctl restart nginx" + + # Arrêt de la machine virtualisée + ynh_lxc_stop --name=$name + + if [ "$update_apt" -eq "1" ] + then + # Archivage du snapshot + ynh_exec_warn_less tar -cz --acls --xattrs -f /var/lib/lxd/snapshots/$name/snap0.tar.gz /var/lib/lxd/snapshots/$name/snap0 + # Remplacement du snapshot + ynh_lxc_create_snapshot --name=$name --snapname=snap0 + + if [ "$LOOP" -eq 2 ] + then # Après l'upgrade du 2e conteneur, déplace les scripts dans le dossier des anciens scripts si ils ont été exécutés avec succès. + ls -1 "$final_path/upgrade.d" | while read LIGNE + do + if [ ! "$LIGNE" == "exemple" ] && [ ! "$LIGNE" == "old_scripts" ] && [ ! "$LIGNE" == "Constant_upgrade" ] && ! echo "$LIGNE" | grep -q ".fail$" # Le fichier exemple, le dossier old_scripts et les scripts fail sont ignorés + then + mv -f "$final_path/upgrade.d/$LIGNE" "$final_path/upgrade.d/old_scripts/$LIGNE" + fi + done + fi + fi + ynh_secure_remove --file="/var/lib/lxd/$name.lock_fileU" # Libère le lock, la machine est à nouveau disponible + ynh_print_info --message="Finished upgrading $name" +} diff --git a/ynh_lxd_demo b/ynh_lxd_demo deleted file mode 100644 index 51a99db..0000000 --- a/ynh_lxd_demo +++ /dev/null @@ -1,177 +0,0 @@ -#!/bin/bash - -#================================================= -# Demo helpers -#================================================= - -# Start an LXC container in demo mode -# -# usage: ynh_lxc_start_as_demo --name=name --ip=ip -# | arg: -n, --name= - name of the LXC -# | arg: -n, --ip= - demo ip of the lxc -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_start_as_demo() { - # Declare an array to define the options of this helper. - local legacy_args=ni - local -A args_array=([n]=name= [i]=ip=) - local name - local ip - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - ynh_lxc_stop --name="$name" - lxc config device set $name eth1 ipv4.address $ip - _ynh_lxc_start_and_wait --name="$name" -} - -# Stop an LXC container in demo mode -# -# usage: ynh_lxc_stop_as_demo --name=name -# | arg: -n, --name= - name of the LXC -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_stop_as_demo() { - # Declare an array to define the options of this helper. - local legacy_args=n - local -A args_array=([n]=name=) - local name - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - ynh_lxc_stop --name="$name" - lxc config device unset $name eth1 ipv4.address -} - -# Upgrading demo container -# -# usage: ynh_lxc_upgrade_demo --name=name --time_to_switch=time_to_switch -# | arg: -n, --name= - name of the LXC -# | arg: -t, --time_to_switch= - time to switch -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_upgrade_demo() { - # Declare an array to define the options of this helper. - local legacy_args=nt - local -A args_array=([n]=name= [t]=time_to_switch=) - local name - local time_to_switch - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - ynh_print_info --message="Upgrading $name" - # Attend que la machine soit éteinte. - # Timeout à $time_to_switch +5 minutes, en seconde - TIME_OUT=$(($time_to_switch * 60 + 300)) - wait_period=0 - while ! ynh_lxc_is_stopped --name=$name - do - wait_period=$(($wait_period+10)) - if [ $wait_period -gt $TIME_OUT ];then - break - else - sleep 5 - fi - done - - while test -e /var/lib/lxd/$name.lock_fileS; do - sleep 5 # Attend que le conteneur soit libéré par le script switch. - done - - touch /var/lib/lxd/$name.lock_fileU # Met en place un fichier pour indiquer que la machine est indisponible pendant l'upgrade - - # Supprime les éventuels swap présents. - /sbin/swapoff /var/lib/lxd/$name/rootfs/swap_* - - # Restaure le snapshot - ynh_lxc_load_snapshot --name=$name --snapname=snap0 - - # Démarre le conteneur - date >> "$final_path/demo_boot.log" - - # Update - update_apt=0 - ynh_lxc_run_inside --name="$name" --command="apt-get update" - ynh_lxc_run_inside --name="$name" --command="apt-get dist-upgrade --dry-run | grep -q "^Inst " > /dev/null" # Vérifie si il y aura des mises à jour. - if [ "$?" -eq 0 ]; then - date - update_apt=1 - # Upgrade - ynh_lxc_run_inside --name="$name" --command="apt-get dist-upgrade --option Dpkg::Options::=--force-confold -yy" - # Clean - ynh_lxc_run_inside --name="$name" --command="apt-get autoremove -y" - ynh_lxc_run_inside --name="$name" --command="apt-get autoclean" - fi - ynh_lxc_run_inside --name="$name" --command="yunohost tools update" - ynh_lxc_run_inside --name="$name" --command="yunohost tools upgrade system" - - # Exécution des scripts de upgrade.d - LOOP=$((LOOP + 1)) - while read LIGNE - do - if [ ! "$LIGNE" == "exemple" ] && [ ! "$LIGNE" == "old_scripts" ] && [ ! "$LIGNE" == "Constant_upgrade" ] && ! echo "$LIGNE" | grep -q ".fail$" # Le fichier exemple, le dossier old_scripts et les scripts fail sont ignorés - then - date - # Exécute chaque script trouvé dans upgrade.d - ynh_print_info --message="Exécution du script $LIGNE sur le conteneur $name" - /bin/bash "$final_path/upgrade.d/$LIGNE" $name - if [ "$?" -ne 0 ]; then # Si le script a échoué, le snapshot est annulé. - ynh_print_info --message="Échec du script $LIGNE" - mv -f "$final_path/upgrade.d/$LIGNE" "$final_path/upgrade.d/$LIGNE.fail" - ynh_print_info --message="Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\nLe script a été renommé en .fail, il ne sera plus exécuté tant que le préfixe ne sera pas retiré.\n\nExtrait du log:\n$(tail -n +$log_line "$script_dir/demo_upgrade.log")" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "Demo Yunohost" $MAIL_ADDR - update_apt=0 - else - ynh_print_info --message="Le script $LIGNE a été exécuté sans erreur" - update_apt=1 - fi - fi - done <<< "$(ls -1 "$final_path/upgrade.d")" - - # Exécution des scripts de upgrade.d/Constant_upgrade - while read LIGNE - do - if [ "$update_apt" -eq "1" ] - then - date - # Exécute chaque script trouvé dans upgrade.d/Constant_upgrade - ynh_print_info --message="Exécution du script $LIGNE sur le conteneur $name" - /bin/bash "$final_path/upgrade.d/Constant_upgrade/$LIGNE" $name - if [ "$?" -ne 0 ]; then - ynh_print_info --message="Échec du script $LIGNE" - ynh_print_info --message="Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\n" - else - ynh_print_info --message="Le script $LIGNE a été exécuté sans erreur" - fi - fi - done <<< "$(ls -1 "$final_path/upgrade.d/Constant_upgrade")" - - # Upgrade des apps - ynh_lxc_run_inside --name="$name" --command="yunohost tools update" - ynh_lxc_run_inside --name="$name" --command="systemctl restart nginx" - ynh_lxc_run_inside --name="$name" --command="yunohost tools upgrade apps" - ynh_lxc_run_inside --name="$name" --command="systemctl restart nginx" - - # Arrêt de la machine virtualisée - ynh_lxc_stop --name=$name - - if [ "$update_apt" -eq "1" ] - then - # Archivage du snapshot - ynh_exec_warn_less tar -cz --acls --xattrs -f /var/lib/lxd/snapshots/$name/snap0.tar.gz /var/lib/lxd/snapshots/$name/snap0 - # Remplacement du snapshot - ynh_lxc_create_snapshot --name=$name --snapname=snap0 - - if [ "$LOOP" -eq 2 ] - then # Après l'upgrade du 2e conteneur, déplace les scripts dans le dossier des anciens scripts si ils ont été exécutés avec succès. - ls -1 "$final_path/upgrade.d" | while read LIGNE - do - if [ ! "$LIGNE" == "exemple" ] && [ ! "$LIGNE" == "old_scripts" ] && [ ! "$LIGNE" == "Constant_upgrade" ] && ! echo "$LIGNE" | grep -q ".fail$" # Le fichier exemple, le dossier old_scripts et les scripts fail sont ignorés - then - mv -f "$final_path/upgrade.d/$LIGNE" "$final_path/upgrade.d/old_scripts/$LIGNE" - fi - done - fi - fi - ynh_secure_remove --file="/var/lib/lxd/$name.lock_fileU" # Libère le lock, la machine est à nouveau disponible - ynh_print_info --message="Finished upgrading $name" -} From f223c64eba2056b8a1f85eb2d0a89475410891c7 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 21 Oct 2022 20:00:03 +0200 Subject: [PATCH 22/25] Update demo_lxc_build.sh --- demo_lxc_build.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/demo_lxc_build.sh b/demo_lxc_build.sh index 02b215c..0e1ef88 100755 --- a/demo_lxc_build.sh +++ b/demo_lxc_build.sh @@ -6,7 +6,6 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd -source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} From 19cffd872bc6d1cc47e4f0e2a584f7b20c1caae2 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 5 Nov 2022 20:15:50 +0100 Subject: [PATCH 23/25] Improving --- demo_lxc_build.sh | 9 +- demo_lxc_destroy.sh | 1 + demo_lxc_remove.sh | 1 + demo_restore_crash.sh | 13 +- demo_start.sh | 3 +- demo_stop.sh | 5 +- demo_switch.sh | 7 +- demo_upgrade.sh | 5 +- ynh_lxd | 676 +++--------------------------------------- ynh_lxd_demo | 230 ++++++++++++++ 10 files changed, 305 insertions(+), 645 deletions(-) create mode 100644 ynh_lxd_demo diff --git a/demo_lxc_build.sh b/demo_lxc_build.sh index 0e1ef88..13397e3 100755 --- a/demo_lxc_build.sh +++ b/demo_lxc_build.sh @@ -6,6 +6,7 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} @@ -41,7 +42,7 @@ then fi ynh_print_info --message="> Creating a YunoHost $DIST $ARCH $YNH_BRANCH" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_lxc_create --image="$lxc_base" --name="$lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_launch --image="$lxc_base" --name="$lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_print_info --message= "> Creating the $lxdbr_demo_name bridge" | tee -a "$LOG_BUILD_LXC" 2>&1 lxc network attach $lxdbr_demo_name $lxc_name1 eth1 eth1 | tee -a "$LOG_BUILD_LXC" 2>&1 @@ -149,17 +150,17 @@ fi # ******** ynh_print_info --message="> Creating a snapshot for $lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_lxc_create_snapshot --name="$lxc_name1" --snapname="snap0" +ynh_lxc_snapshot_create --name="$lxc_name1" --snapname="snap0" ynh_print_info --message="> Upgrading the $lxc_name1 LXC container" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_stop --name="$lxc_name1" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_lxc_upgrade_demo --name=$lxc_name1 --time_to_switch=$time_to_switch | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_demo_upgrade --name=$lxc_name1 --time_to_switch=$time_to_switch | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_print_info --message="> Cloning $lxc_name1 to $lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_clone --source="$lxc_name1" --destination="$lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_print_info --message="> Creating a snapshot for $lxc_name2" | tee -a "$LOG_BUILD_LXC" 2>&1 -ynh_lxc_create_snapshot --name="$lxc_name2" --snapname="snap0" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_lxc_snapshot_create --name="$lxc_name2" --snapname="snap0" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_print_info --message="> Setuping the switch cron" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_add_config --template="$final_path/conf/cron_demo_switch" --destination="/etc/cron.d/demo_switch" | tee -a "$LOG_BUILD_LXC" 2>&1 diff --git a/demo_lxc_destroy.sh b/demo_lxc_destroy.sh index 9e265d8..369f02c 100755 --- a/demo_lxc_destroy.sh +++ b/demo_lxc_destroy.sh @@ -7,6 +7,7 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} diff --git a/demo_lxc_remove.sh b/demo_lxc_remove.sh index a3e60e0..08bfaf6 100755 --- a/demo_lxc_remove.sh +++ b/demo_lxc_remove.sh @@ -7,6 +7,7 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} diff --git a/demo_restore_crash.sh b/demo_restore_crash.sh index 3d5fb13..6cbf59e 100755 --- a/demo_restore_crash.sh +++ b/demo_restore_crash.sh @@ -6,6 +6,7 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} @@ -27,8 +28,8 @@ ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileS" ynh_secure_remove --file="/var/lib/lxd/$lxc_name1.lock_fileU" ynh_secure_remove --file="/var/lib/lxd/$lxc_name2.lock_fileU" -ynh_lxc_stop_as_demo --name="$lxc_name1" -ynh_lxc_stop_as_demo --name="$lxc_name2" +ynh_lxc_demo_stop --name="$lxc_name1" +ynh_lxc_demo_stop --name="$lxc_name2" # Vérifie l'état des conteneurs. ynh_lxc_check_container_start --name=$lxc_name1 @@ -49,21 +50,21 @@ fi # Restauration des snapshots if [ $LXC1_STATUS -eq 1 ]; then - ynh_lxc_restore_from_snapshot --name=$lxc_name1 + ynh_lxc_demo_restore_from_snapshot --name=$lxc_name1 LXC1_STATUS=$? fi if [ $LXC2_STATUS -eq 1 ]; then - ynh_lxc_restore_from_snapshot --name=$lxc_name2 + ynh_lxc_demo_restore_from_snapshot --name=$lxc_name2 LXC2_STATUS=$? fi # Restauration des archives des snapshots if [ $LXC1_STATUS -eq 1 ]; then - ynh_lxc_restore_from_archive --name=$lxc_name1 + ynh_lxc_demo_restore_from_archive --name=$lxc_name1 LXC1_STATUS=$? fi if [ $LXC2_STATUS -eq 1 ]; then - ynh_lxc_restore_from_archive --name=$lxc_name2 + ynh_lxc_demo_restore_from_archive --name=$lxc_name2 LXC2_STATUS=$? fi diff --git a/demo_start.sh b/demo_start.sh index a08675b..31d1240 100755 --- a/demo_start.sh +++ b/demo_start.sh @@ -6,6 +6,7 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} @@ -23,7 +24,7 @@ ynh_print_info --message=">> Starting demo." | tee -a "$final_path/demo_boot.log ynh_print_info --message="> Démarrage de la machine" | tee -a "$final_path/demo_boot.log" 2>&1 date | tee -a "$final_path/demo_boot.log" 2>&1 ynh_print_info --message="> Starting $lxc_name1" | tee -a "$final_path/demo_boot.log" 2>&1 -ynh_lxc_start_as_demo --name=$lxc_name1 --ip="$lxdbr_demo_network$lxc_ip1" | tee -a "$final_path/demo_boot.log" 2>&1 +ynh_lxc_demo_start --name=$lxc_name1 --ip="$lxdbr_demo_network$lxc_ip1" | tee -a "$final_path/demo_boot.log" 2>&1 sleep 3 date | tee -a "$final_path/demo_boot.log" 2>&1 diff --git a/demo_stop.sh b/demo_stop.sh index bb9ba51..80f3a14 100755 --- a/demo_stop.sh +++ b/demo_stop.sh @@ -6,6 +6,7 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(ynh_print_info --message=$PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} @@ -36,7 +37,7 @@ then if ! ynh_lxc_is_stopped --name=$lxc_name1 then ynh_print_info --message="> Stopping $lxc_name1 LXC container" | tee -a "$final_path/demo_boot.log" 2>&1 - ynh_lxc_stop_as_demo --name=$lxc_name1 + ynh_lxc_demo_stop --name=$lxc_name1 fi fi if ynh_lxc_exists --name=$lxc_name2 @@ -44,7 +45,7 @@ then if ! ynh_lxc_is_stopped --name=$lxc_name2 then ynh_print_info --message="> Stopping $lxc_name2 LXC container" - ynh_lxc_stop_as_demo --name=$lxc_name2 + ynh_lxc_demo_stop --name=$lxc_name2 fi fi diff --git a/demo_switch.sh b/demo_switch.sh index 05c2b56..c192328 100755 --- a/demo_switch.sh +++ b/demo_switch.sh @@ -7,6 +7,7 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} @@ -49,7 +50,7 @@ fi ynh_print_info --message="> Starting $LXC_B" # Démarre le conteneur B et arrête le conteneur A. -ynh_lxc_start_as_demo --name=$LXC_B --ip=$IP_B +ynh_lxc_demo_start --name=$LXC_B --ip=$IP_B sleep 5 # Attend 10 seconde pour s'assurer du démarrage de la machine. if ! ynh_lxc_is_started --name=$LXC_B then @@ -63,12 +64,12 @@ else # Automatique par nginx lorsque la machine A sera éteinte. # Arrêt du conteneur A. Il est remplacé par le B touch /var/lib/lxd/$LXC_A.lock_fileS # Met en place un fichier pour indiquer que la machine n'est pas encore dispo. - ynh_lxc_stop_as_demo --name=$LXC_A + ynh_lxc_demo_stop --name=$LXC_A # Supprime les éventuels swap présents. /sbin/swapoff /var/lib/lxd/$LXC_A/rootfs/swap_* ynh_print_info --message="> Restauring $LXC_A from snapshot" # Restaure le snapshot de la machine A avant sa prochaine exécution - ynh_lxc_load_snapshot --name=$LXC_A --snapname=snap0 + ynh_lxc_snapshot_load --name=$LXC_A --snapname=snap0 ynh_lxc_stop --name=$LXC_A ynh_secure_remove --file="/var/lib/lxd/$LXC_A.lock_fileS" # Libère le lock ynh_print_info --message="> Finish restoring $LXC_A" diff --git a/demo_upgrade.sh b/demo_upgrade.sh index cde8987..0cd9320 100755 --- a/demo_upgrade.sh +++ b/demo_upgrade.sh @@ -7,6 +7,7 @@ if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi source $script_dir/ynh_lxd +source $script_dir/ynh_lxd_demo source /usr/share/yunohost/helpers app=${__APP__:-yunohost_demo} @@ -24,8 +25,8 @@ log_line=$(( $log_line + 1 )) # Ignore la première ligne, reprise de l'ancien l date | tee -a "$final_path/demo_upgrade.log" 2>&1 ynh_print_info --message=">> Upgrading demo." | tee -a "$final_path/demo_upgrade.log" 2>&1 -ynh_lxc_upgrade_demo --name=$lxc_name1 --time_to_switch=$time_to_switch -ynh_lxc_upgrade_demo --name=$lxc_name2 --time_to_switch=$time_to_switch +ynh_lxc_demo_upgrade --name=$lxc_name1 --time_to_switch=$time_to_switch +ynh_lxc_demo_upgrade --name=$lxc_name2 --time_to_switch=$time_to_switch date | tee -a "$final_path/demo_upgrade.log" 2>&1 ynh_print_info --message=">> Finished upgrading demo." | tee -a "$final_path/demo_upgrade.log" 2>&1 diff --git a/ynh_lxd b/ynh_lxd index f6e35e2..d8e0bac 100644 --- a/ynh_lxd +++ b/ynh_lxd @@ -10,7 +10,7 @@ # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_exists() { +ynh_lxc_exists () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) @@ -32,7 +32,7 @@ ynh_lxc_exists() { # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_status() { +ynh_lxc_status () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) @@ -52,7 +52,7 @@ ynh_lxc_status() { # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_is_started() { +ynh_lxc_is_started () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) @@ -74,7 +74,7 @@ ynh_lxc_is_started() { # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_is_stopped() { +ynh_lxc_is_stopped () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) @@ -96,7 +96,7 @@ ynh_lxc_is_stopped() { # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_start() { +ynh_lxc_start () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) @@ -120,7 +120,7 @@ ynh_lxc_start() { # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_stop() { +ynh_lxc_stop () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) @@ -166,7 +166,7 @@ ynh_lxc_stop() { # | arg: -c, --command= - command to execute # # Requires YunoHost version *.*.* or higher. -ynh_lxc_run_inside() { +ynh_lxc_run_inside () { # Declare an array to define the options of this helper. local legacy_args=nc local -A args_array=([n]=name= [c]=command=) @@ -184,7 +184,7 @@ ynh_lxc_run_inside() { # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_check_container_start() { +ynh_lxc_check_container_start () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) @@ -211,11 +211,11 @@ ynh_lxc_check_container_start() { # Restart a container # -# usage: _ynh_restart_container --name=name +# usage: _ynh_lxc_restart_container --name=name # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -_ynh_restart_container() { +_ynh_lxc_restart_container () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) @@ -223,8 +223,8 @@ _ynh_restart_container() { # Manage arguments with getopts ynh_handle_getopts_args "$@" - ynh_lxc_stop --name="$name" - ynh_lxc_start --name="$name" + ynh_lxc_stop --name=$name + ynh_lxc_start --name=$name } # Keep sure the LXC is started @@ -233,7 +233,7 @@ _ynh_restart_container() { # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -_ynh_lxc_start_and_wait() { +_ynh_lxc_start_and_wait () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) @@ -253,7 +253,8 @@ _ynh_lxc_start_and_wait() { # Wait for container to start, we are using systemd to check this, # for the sake of brevity. for j in $(seq 1 10); do - if ynh_lxc_run_inside --name=$name --command="systemctl isolate multi-user.target >/dev/null 2>/dev/null"; then + if ynh_lxc_run_inside --name=$name --command="systemctl isolate multi-user.target >/dev/null 2>/dev/null" + then break fi @@ -261,7 +262,7 @@ _ynh_lxc_start_and_wait() { log_debug 'Failed to start the container ... restarting ...' failstart=1 - _ynh_restart_container --name="$name" + _ynh_lxc_restart_container --name="$name" fi sleep 1s @@ -269,7 +270,8 @@ _ynh_lxc_start_and_wait() { # Wait for container to access the internet for j in $(seq 1 10); do - if ynh_lxc_run_inside --name=$name --command="curl -s http://wikipedia.org > /dev/null 2>/dev/null"; then + if ynh_lxc_run_inside --name=$name --command="curl -s http://wikipedia.org > /dev/null 2>/dev/null" + then break fi @@ -277,7 +279,7 @@ _ynh_lxc_start_and_wait() { log_debug 'Failed to access the internet ... restarting' failstart=1 - _ynh_restart_container --name="$name" + _ynh_lxc_restart_container --name="$name" fi sleep 1s @@ -301,14 +303,14 @@ _ynh_lxc_start_and_wait() { LXC_IP=$(ynh_lxc_run_inside --name=$name --command="hostname -I | cut -d' ' -f1 | grep -E -o \"\<[0-9.]{8,}\>\"") } -# Create a new LXC from an image +# Launch a new LXC from an image # -# usage: ynh_lxc_create --image=image --name=name +# usage: ynh_lxc_launch --image=image --name=name # | arg: -i, --image= - image to create from # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_create(){ +ynh_lxc_launch (){ # Declare an array to define the options of this helper. local legacy_args=in local -A args_array=([i]=image= [n]=name=) @@ -317,17 +319,15 @@ ynh_lxc_create(){ # Manage arguments with getopts ynh_handle_getopts_args "$@" - log_info "Launching new LXC $name ..." - # Check if we can launch container from YunoHost remote image - if ynh_exec_as $app lxc remote list | grep -q "yunohost" && ynh_exec_as $app lxc image list yunohost:$image | grep -q -w $image; then - ynh_exec_as $app lxc launch yunohost:$image $name \ + if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$image | grep -q -w $image; then + lxc launch yunohost:$image $name \ -c security.nesting=true \ -c security.privileged=true \ -c limits.memory=80% \ -c limits.cpu.allowance=80% | tee -a /proc/self/fd/3 # Check if we can launch container from a local image - elif ynh_exec_as $app lxc image list $image | grep -q -w $image; then - ynh_exec_as $app lxc launch $image $name \ + elif lxc image list $image | grep -q -w $image; then + lxc launch $image $name \ -c security.nesting=true \ -c security.privileged=true \ -c limits.memory=80% \ @@ -335,22 +335,6 @@ ynh_lxc_create(){ else log_critical "Can't find base image $image" fi - - if [ ! -z "$FOR_PACKAGE_CHECK" ] - then - pipestatus="${PIPESTATUS[0]}" - location=$(ynh_exec_as $app lxc list --format json | jq -e --arg name $name '.[] | select(.name==$name) | .location' | tr -d '"') - [[ "$location" != "none" ]] && log_info "... on $location" - - [[ "$pipestatus" -eq 0 ]] || exit 1 - fi - - _ynh_lxc_start_and_wait --name="$name" - if [ ! -z "$FOR_PACKAGE_CHECK" ] - then - ynh_lxc_set_witness --name="$name" - fi - ynh_lxc_create_snapshot --name="$name" --snapname="snap0" } # Delete a lxc container @@ -359,7 +343,7 @@ ynh_lxc_create(){ # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_delete() { +ynh_lxc_delete () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) @@ -377,11 +361,11 @@ ynh_lxc_delete() { # Clean the swapfiles of an LXC container # -# usage: ynh_lxc_clean_swapfiles --name=name +# usage: ynh_lxc_swapfiles_clean --name=name # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_clean_swapfiles() { +ynh_lxc_swapfiles_clean () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) @@ -389,7 +373,7 @@ ynh_lxc_clean_swapfiles() { # Manage arguments with getopts ynh_handle_getopts_args "$@" - _ynh_lxc_start_and_wait --name="$name" + _ynh_lxc_start_and_wait --name=$name ynh_lxc_run_inside --name=$name --command='for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' ynh_lxc_run_inside --name=$name --command='for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $swapfile; done' @@ -402,7 +386,7 @@ ynh_lxc_clean_swapfiles() { # | arg: -s, --snapname= - name of the snapshot # # Requires YunoHost version *.*.* or higher. -ynh_lxc_snapshot_exists() { +ynh_lxc_snapshot_exists () { # Declare an array to define the options of this helper. local legacy_args=ns local -A args_array=([n]=name= [s]=snapname=) @@ -430,12 +414,12 @@ ynh_lxc_snapshot_exists() { # Create a snapshot of an LXC container # -# usage: ynh_lxc_create_snapshot --name=name --snapname=snapname +# usage: ynh_lxc_snapshot_create --name=name --snapname=snapname # | arg: -n, --name= - name of the LXC # | arg: -s, --snapname= - name of the snapshot # # Requires YunoHost version *.*.* or higher. -ynh_lxc_create_snapshot() { +ynh_lxc_snapshot_create () { # Declare an array to define the options of this helper. local legacy_args=ns local -A args_array=([n]=name= [s]=snapname=) @@ -444,21 +428,13 @@ ynh_lxc_create_snapshot() { # Manage arguments with getopts ynh_handle_getopts_args "$@" - ynh_lxc_start_timer - - if [ ! -z "$FOR_PACKAGE_CHECK" ] - then - # Check all the witness files, to verify if them still here - ynh_lxc_check_witness >&2 - fi - # Remove swap files to avoid killing the CI with huge snapshots. - ynh_lxc_clean_swapfiles --name="$name" + ynh_lxc_swapfiles_clean --name=$name - ynh_lxc_stop --name="$name" + ynh_lxc_stop --name=$name # Check if the snapshot already exist - if ! ynh_lxc_snapshot_exists --name="$name" --snapname="$snapname" + if ! ynh_lxc_snapshot_exists --name=$name --snapname="$snapname" then log_info "(Creating snapshot $snapname ...)" lxc snapshot $name $snapname @@ -466,20 +442,16 @@ ynh_lxc_create_snapshot() { log_info "(Recreating snapshot $snapname ...)" lxc snapshot $name $snapname --reuse fi - - _ynh_lxc_start_and_wait --name="$name" - - ynh_lxc_stop_timer 1 } # Delete a snapshot of an LXC container # -# usage: ynh_lxc_delete_snapshot --name=name --snapname=snapname +# usage: ynh_lxc_snapshot_delete --name=name --snapname=snapname # | arg: -n, --name= - name of the LXC # | arg: -s, --snapname= - name of the snapshot # # Requires YunoHost version *.*.* or higher. -ynh_lxc_delete_snapshot() { +ynh_lxc_snapshot_delete () { # Declare an array to define the options of this helper. local legacy_args=ns local -A args_array=([n]=name= [s]=snapname=) @@ -499,12 +471,12 @@ ynh_lxc_delete_snapshot() { # Load a snapshot of an LXC container # -# usage: ynh_lxc_load_snapshot --name=name --snapname=snapname +# usage: ynh_lxc_snapshot_load --name=name --snapname=snapname # | arg: -n, --name= - name of the LXC # | arg: -s, --snapname= - name of the snapshot # # Requires YunoHost version *.*.* or higher. -ynh_lxc_load_snapshot() { +ynh_lxc_snapshot_load () { # Declare an array to define the options of this helper. local legacy_args=ns local -A args_array=([n]=name= [s]=snapname=) @@ -518,70 +490,17 @@ ynh_lxc_load_snapshot() { log_debug "Loading snapshot $snapname ..." # Remove swap files before restoring the snapshot. - ynh_lxc_clean_swapfiles --name="$name" + ynh_lxc_swapfiles_clean --name=$name - ynh_lxc_stop --name="$name" + ynh_lxc_stop --name=$name lxc restore $name $snapname - _ynh_lxc_start_and_wait --name="$name" + _ynh_lxc_start_and_wait --name=$name else return 1 fi } -# Restore an LXC container from snap0 snapshot -# -# usage: ynh_lxc_restore_from_snapshot --name=name -# | arg: -n, --name= - name of the LXC -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_restore_from_snapshot () { - # Declare an array to define the options of this helper. - local legacy_args=n - local -A args_array=([n]=name=) - local name - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - ynh_print_info --message="Restoring LXC container $name from snapshot" - ynh_lxc_load_snapshot --name=$name --snapname=snap0 - if ynh_lxc_check_container_start --name=$name - then - ynh_print_info --message="LXC container $name is working." - return 0 - else - ynh_print_info --message="LXC container $name is broken." - return 1 - fi -} - -# Restore an LXC container from an archive -# -# usage: ynh_lxc_restore_from_archive --name=name -# | arg: -n, --name= - name of the LXC -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_restore_from_archive () { - # Declare an array to define the options of this helper. - local legacy_args=n - local -A args_array=([n]=name=) - local name - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - if ! test -e "/var/lib/lxd/snapshots/$name/snap1.tar.gz"; then - ynh_print_info --message="No snapshot archive for LXC container $name" - return 1 - fi - ynh_print_info --message="Restoring snapshot archive for LXC container $name" - ynh_print_info --message="Deleting snapshot" - ynh_lxc_delete_snapshot --name=$name --snapname=snap0 - ynh_print_info --message="Untar archive" - tar -x --acls --xattrs -f /var/lib/lxd/snapshots/$name/snap0.tar.gz -C / - ynh_lxc_restore_from_snapshot --name=$name - return $? -} - # Clone an LXC container # # usage: ynh_lxc_clone --source=source --destination=destination @@ -589,7 +508,7 @@ ynh_lxc_restore_from_archive () { # | arg: -d, --destination= - destination LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_clone() { +ynh_lxc_clone () { # Declare an array to define the options of this helper. local legacy_args=sd local -A args_array=([s]=source= [d]=destination=) @@ -615,7 +534,7 @@ ynh_lxc_clone() { else ynh_print_info --message=" LXC container $destination is working." ynh_print_info --message= "Creating snapshot of LXC container $destination" - ynh_lxc_create_snapshot --name="$destination" --snapname="snap0" + ynh_lxc_snapshot_create --name="$destination" --snapname="snap0" fi return $STATUS } @@ -626,7 +545,7 @@ ynh_lxc_clone() { # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_reset() { +ynh_lxc_reset () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) @@ -637,508 +556,11 @@ ynh_lxc_reset() { # If the container exists if ynh_lxc_exists --name=$name then - # Remove swap files before deletting the continer - ynh_lxc_clean_swapfiles --name="$name" - ynh_lxc_stop --name="$name" + # Remove swap files before deleting the container + ynh_lxc_swapfiles_clean --name=$name + ynh_lxc_stop --name=$name local current_storage=$(lxc list $name --format json --columns b | jq '.[].expanded_devices.root.pool') swapoff "$(lxc storage get $current_storage source)/containers/$name/rootfs/swap" 2>/dev/null ynh_lxc_delete --name=$name fi } - -#================================================= -# LOGGING HELPERS -#================================================= - -readonly NORMAL=$(printf '\033[0m') -readonly BOLD=$(printf '\033[1m') -readonly faint=$(printf '\033[2m') -readonly UNDERLINE=$(printf '\033[4m') -readonly NEGATIVE=$(printf '\033[7m') -readonly RED=$(printf '\033[31m') -readonly GREEN=$(printf '\033[32m') -readonly ORANGE=$(printf '\033[33m') -readonly BLUE=$(printf '\033[34m') -readonly YELLOW=$(printf '\033[93m') -readonly WHITE=$(printf '\033[39m') - -function log_title() -{ - cat << EOF | tee -a /proc/self/fd/3 -${BOLD} - ============================================ - $1 - ============================================ -${NORMAL} -EOF -} - -function log_small_title() -{ - echo -e "\n${BOLD} > ${1}${NORMAL}\n" | tee -a /proc/self/fd/3 -} - - -function log_debug() -{ - echo "$1" | tee -a /proc/self/fd/3 -} - -function log_info() -{ - echo "${1}" | tee -a /proc/self/fd/3 -} - -function log_success() -{ - echo "${BOLD}${GREEN}Success: ${1}${NORMAL}" | tee -a /proc/self/fd/3 -} - -function log_warning() -{ - echo "${BOLD}${ORANGE}Warning: ${1}${NORMAL}" | tee -a /proc/self/fd/3 -} - -function log_error() -{ - echo "${BOLD}${RED}Error: ${1}${NORMAL}" | tee -a /proc/self/fd/3 -} - -function log_critical() -{ - echo "${BOLD}${RED}Critical: ${1}${NORMAL}" | tee -a /proc/self/fd/3 - exit 1 -} - -function log_report_test_success () { - echo -e "\n${BOLD}${GREEN}--- SUCCESS ---${NORMAL}\n" | tee -a /proc/self/fd/3 -} - -function log_report_test_warning () { - echo -e "\n${BOLD}${ORANGE}--- WARNING ---${NORMAL}\n" | tee -a /proc/self/fd/3 -} - -function log_report_test_failed () { - echo -e "\n${BOLD}${RED}--- FAIL ---${NORMAL}\n" | tee -a /proc/self/fd/3 -} - -#================================================= -# TIMING HELPERS -#================================================= - -# Start a timer -# -# usage: ynh_lxc_start_timer -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_start_timer() { - # Set the beginning of the timer - starttime=$(date +%s) -} - -# Stop a timer -# -# usage: ynh_lxc_stop_timer -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_stop_timer() { - # Ending the timer - # $1 = Type of querying - - local finishtime=$(date +%s) - # Calculate the gap between the starting and the ending of the timer - local elapsedtime=$(echo $(( $finishtime - $starttime ))) - # Extract the number of hour - local hours=$(echo $(( $elapsedtime / 3600 ))) - local elapsedtime=$(echo $(( $elapsedtime - ( 3600 * $hours) ))) - # Minutes - local minutes=$(echo $(( $elapsedtime / 60 ))) - # And seconds - local seconds=$(echo $(( $elapsedtime - ( 60 * $minutes) ))) - - local phours="" - local pminutes="" - local pseconds="" - - # Avoid null values - [ $hours -eq 0 ] || phours="$hours hour" - [ $minutes -eq 0 ] || pminutes="$minutes minute" - [ $seconds -eq 0 ] || pseconds="$seconds second" - - # Add a 's' for plural values - [ $hours -eq 1 ] && phours="${phours}, " || test -z "$phours" || phours="${phours}s, " - [ $minutes -eq 1 ] && pminutes="${pminutes}, " || test -z "$pminutes" || pminutes="${pminutes}s, " - [ $seconds -gt 1 ] && pseconds="${pseconds}s" || pseconds="0s" - - local time="${phours}${pminutes}${pseconds} ($(date '+%T'))" - if [ $1 -eq 2 ]; then - log_info "Working time for this test: $time" - elif [ $1 -eq 3 ]; then - log_info "Global working time for all tests: $time" - else - log_debug "Working time: $time" - fi -} - -#================================================= -# PACKAGE_CHECK HELPERS -#================================================= - -# ynh_lxd commands have to be launch with FOR_PACKAGE_CHECK=1 - -# Start an LXC and execute a command in it, to be used for PACKAGE_CHECK_EXEC -# -# usage: ynh_lxc_exec --name=name --command=command -# | arg: -n, --name= - name of the LXC -# | arg: -c, --command= - command to execute -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_exec() { - # Declare an array to define the options of this helper. - local legacy_args=nc - local -A args_array=([n]=name= [c]=command=) - local name - local command - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - _ynh_lxc_start_and_wait --name="$name" - - ynh_lxc_start_timer - - # Execute the command given in argument in the container and log its results. - lxc exec $name --env PACKAGE_CHECK_EXEC=1 -t -- /bin/bash -c "$command" - - # Store the return code of the command - local returncode=${PIPESTATUS[0]} - - log_debug "Return code: $returncode" - - ynh_lxc_stop_timer 1 - # Return the exit code of the ssh command - return $returncode -} - -# Create a witness in an LXC container -# -# usage: ynh_lxc_create_witness --name=name --witness=witness --type=type -# | arg: -n, --name= - name of the LXC -# | arg: -w, --witness= - witness to create -# | arg: -t, --type= - type of witness, can be file or directory -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_create_witness () { - # Declare an array to define the options of this helper. - local legacy_args=nwt - local -A args_array=([n]=name= [w]=witness= [t]=type=) - local name - local witness - local type - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - [ "$type" = "file" ] && local action="touch" || local action="mkdir -p" - ynh_lxc_run_inside --name=$name --command="$action $witness" -} - -# Set witness in an LXC container -# -# usage: ynh_lxc_set_witness --name=name -# | arg: -n, --name= - name of the LXC -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_set_witness() { - # Declare an array to define the options of this helper. - local legacy_args=n - local -A args_array=([n]=name=) - local name - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - # Create files to check if the remove script does not remove them accidentally - log_debug "Create witness files..." - - # Nginx conf - ynh_lxc_create_witness -name=$name --witness="/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" --type=file - ynh_lxc_create_witness -name=$name --witness="/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" --type=file - - # /etc - ynh_lxc_create_witness -name=$name --witness="/etc/witnessfile" --type=file - - # /opt directory - ynh_lxc_create_witness -name=$name --witness="/opt/witnessdir" --type=directory - - # /var/www directory - ynh_lxc_create_witness -name=$name --witness="/var/www/witnessdir" --type=directory - - # /home/yunohost.app/ - ynh_lxc_create_witness -name=$name --witness="/home/yunohost.app/witnessdir" --type=directory - - # /var/log - ynh_lxc_create_witness -name=$name --witness="/var/log/witnessfile" --type=file - - # Config fpm - ynh_lxc_create_witness -name=$name --witness="/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" --type=file - - # Config logrotate - ynh_lxc_create_witness -name=$name --witness="/etc/logrotate.d/witnessfile" --type=file - - # Config systemd - ynh_lxc_create_witness -name=$name --witness="/etc/systemd/system/witnessfile.service" --type=file - - # Database - ynh_lxc_run_inside --name=$name --command="mysqladmin --wait status > /dev/null 2>&1" - ynh_lxc_run_inside --name=$name --command="echo \"CREATE DATABASE witnessdb\" | mysql --wait > /dev/null 2>&1" -} - -# Check if a witness exists in an LXC container -# -# usage: ynh_lxc_create_witness --name=name --witness=witness -# | arg: -n, --name= - name of the LXC -# | arg: -w, --witness= - witness to create -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_check_witness_exist () { - # Declare an array to define the options of this helper. - local legacy_args=nw - local -A args_array=([n]=name= [w]=witness=) - local name - local witness - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - if ynh_lxc_run_inside --name=$name --command="test ! -e \"$witness\"" - then - log_error "The file $witness is missing ! Something gone wrong !" - SET_RESULT "failure" witness - fi -} - -# Check witness in an LXC container -# -# usage: ynh_lxc_check_witness --name=name -# | arg: -n, --name= - name of the LXC -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_check_witness() { - # Declare an array to define the options of this helper. - local legacy_args=n - local -A args_array=([n]=name=) - local name - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - # Check all the witness files, to verify if them still here - - # Nginx conf - ynh_lxc_check_witness_exist --name=$name --witness="/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" - ynh_lxc_check_witness_exist --name=$name --witness="/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" - - # /etc - ynh_lxc_check_witness_exist --name=$name --witness="/etc/witnessfile" - - # /opt directory - ynh_lxc_check_witness_exist --name=$name --witness="/opt/witnessdir" - - # /var/www directory - ynh_lxc_check_witness_exist --name=$name --witness="/var/www/witnessdir" - - # /home/yunohost.app/ - ynh_lxc_check_witness_exist --name=$name --witness="/home/yunohost.app/witnessdir" - - # /var/log - ynh_lxc_check_witness_exist --name=$name --witness="/var/log/witnessfile" - - # Config fpm - ynh_lxc_check_witness_exist --name=$name --witness="/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" - - # Config logrotate - ynh_lxc_check_witness_exist --name=$name --witness="/etc/logrotate.d/witnessfile" - - # Config systemd - ynh_lxc_check_witness_exist --name=$name --witness="/etc/systemd/system/witnessfile.service" - - # Database - if ! ynh_lxc_run_inside --name=$name --command="mysqlshow witnessdb > /dev/null 2>&1" - then - log_error "The database witnessdb is missing ! Something gone wrong !" - SET_RESULT "failure" witness - return 1 - fi -} - -#================================================= -# DEMO HELPERS -#================================================= - -# Start an LXC container in demo mode -# -# usage: ynh_lxc_start_as_demo --name=name --ip=ip -# | arg: -n, --name= - name of the LXC -# | arg: -n, --ip= - demo ip of the lxc -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_start_as_demo() { - # Declare an array to define the options of this helper. - local legacy_args=ni - local -A args_array=([n]=name= [i]=ip=) - local name - local ip - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - ynh_lxc_stop --name="$name" - lxc config device set $name eth1 ipv4.address $ip - _ynh_lxc_start_and_wait --name="$name" -} - -# Stop an LXC container in demo mode -# -# usage: ynh_lxc_stop_as_demo --name=name -# | arg: -n, --name= - name of the LXC -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_stop_as_demo() { - # Declare an array to define the options of this helper. - local legacy_args=n - local -A args_array=([n]=name=) - local name - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - ynh_lxc_stop --name="$name" - lxc config device unset $name eth1 ipv4.address -} - -# Upgrading demo container -# -# usage: ynh_lxc_upgrade_demo --name=name --time_to_switch=time_to_switch -# | arg: -n, --name= - name of the LXC -# | arg: -t, --time_to_switch= - time to switch -# -# Requires YunoHost version *.*.* or higher. -ynh_lxc_upgrade_demo() { - # Declare an array to define the options of this helper. - local legacy_args=nt - local -A args_array=([n]=name= [t]=time_to_switch=) - local name - local time_to_switch - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - ynh_print_info --message="Upgrading $name" - # Attend que la machine soit éteinte. - # Timeout à $time_to_switch +5 minutes, en seconde - TIME_OUT=$(($time_to_switch * 60 + 300)) - wait_period=0 - while ! ynh_lxc_is_stopped --name=$name - do - wait_period=$(($wait_period+10)) - if [ $wait_period -gt $TIME_OUT ];then - break - else - sleep 5 - fi - done - - while test -e /var/lib/lxd/$name.lock_fileS; do - sleep 5 # Attend que le conteneur soit libéré par le script switch. - done - - touch /var/lib/lxd/$name.lock_fileU # Met en place un fichier pour indiquer que la machine est indisponible pendant l'upgrade - - # Supprime les éventuels swap présents. - /sbin/swapoff /var/lib/lxd/$name/rootfs/swap_* - - # Restaure le snapshot - ynh_lxc_load_snapshot --name=$name --snapname=snap0 - - # Démarre le conteneur - date >> "$final_path/demo_boot.log" - - # Update - update_apt=0 - ynh_lxc_run_inside --name="$name" --command="apt-get update" - ynh_lxc_run_inside --name="$name" --command="apt-get dist-upgrade --dry-run | grep -q "^Inst " > /dev/null" # Vérifie si il y aura des mises à jour. - if [ "$?" -eq 0 ]; then - date - update_apt=1 - # Upgrade - ynh_lxc_run_inside --name="$name" --command="apt-get dist-upgrade --option Dpkg::Options::=--force-confold -yy" - # Clean - ynh_lxc_run_inside --name="$name" --command="apt-get autoremove -y" - ynh_lxc_run_inside --name="$name" --command="apt-get autoclean" - fi - ynh_lxc_run_inside --name="$name" --command="yunohost tools update" - ynh_lxc_run_inside --name="$name" --command="yunohost tools upgrade system" - - # Exécution des scripts de upgrade.d - LOOP=$((LOOP + 1)) - while read LIGNE - do - if [ ! "$LIGNE" == "exemple" ] && [ ! "$LIGNE" == "old_scripts" ] && [ ! "$LIGNE" == "Constant_upgrade" ] && ! echo "$LIGNE" | grep -q ".fail$" # Le fichier exemple, le dossier old_scripts et les scripts fail sont ignorés - then - date - # Exécute chaque script trouvé dans upgrade.d - ynh_print_info --message="Exécution du script $LIGNE sur le conteneur $name" - /bin/bash "$final_path/upgrade.d/$LIGNE" $name - if [ "$?" -ne 0 ]; then # Si le script a échoué, le snapshot est annulé. - ynh_print_info --message="Échec du script $LIGNE" - mv -f "$final_path/upgrade.d/$LIGNE" "$final_path/upgrade.d/$LIGNE.fail" - ynh_print_info --message="Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\nLe script a été renommé en .fail, il ne sera plus exécuté tant que le préfixe ne sera pas retiré.\n\nExtrait du log:\n$(tail -n +$log_line "$script_dir/demo_upgrade.log")" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "Demo Yunohost" $MAIL_ADDR - update_apt=0 - else - ynh_print_info --message="Le script $LIGNE a été exécuté sans erreur" - update_apt=1 - fi - fi - done <<< "$(ls -1 "$final_path/upgrade.d")" - - # Exécution des scripts de upgrade.d/Constant_upgrade - while read LIGNE - do - if [ "$update_apt" -eq "1" ] - then - date - # Exécute chaque script trouvé dans upgrade.d/Constant_upgrade - ynh_print_info --message="Exécution du script $LIGNE sur le conteneur $name" - /bin/bash "$final_path/upgrade.d/Constant_upgrade/$LIGNE" $name - if [ "$?" -ne 0 ]; then - ynh_print_info --message="Échec du script $LIGNE" - ynh_print_info --message="Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\n" - else - ynh_print_info --message="Le script $LIGNE a été exécuté sans erreur" - fi - fi - done <<< "$(ls -1 "$final_path/upgrade.d/Constant_upgrade")" - - # Upgrade des apps - ynh_lxc_run_inside --name="$name" --command="yunohost tools update" - ynh_lxc_run_inside --name="$name" --command="systemctl restart nginx" - ynh_lxc_run_inside --name="$name" --command="yunohost tools upgrade apps" - ynh_lxc_run_inside --name="$name" --command="systemctl restart nginx" - - # Arrêt de la machine virtualisée - ynh_lxc_stop --name=$name - - if [ "$update_apt" -eq "1" ] - then - # Archivage du snapshot - ynh_exec_warn_less tar -cz --acls --xattrs -f /var/lib/lxd/snapshots/$name/snap0.tar.gz /var/lib/lxd/snapshots/$name/snap0 - # Remplacement du snapshot - ynh_lxc_create_snapshot --name=$name --snapname=snap0 - - if [ "$LOOP" -eq 2 ] - then # Après l'upgrade du 2e conteneur, déplace les scripts dans le dossier des anciens scripts si ils ont été exécutés avec succès. - ls -1 "$final_path/upgrade.d" | while read LIGNE - do - if [ ! "$LIGNE" == "exemple" ] && [ ! "$LIGNE" == "old_scripts" ] && [ ! "$LIGNE" == "Constant_upgrade" ] && ! echo "$LIGNE" | grep -q ".fail$" # Le fichier exemple, le dossier old_scripts et les scripts fail sont ignorés - then - mv -f "$final_path/upgrade.d/$LIGNE" "$final_path/upgrade.d/old_scripts/$LIGNE" - fi - done - fi - fi - ynh_secure_remove --file="/var/lib/lxd/$name.lock_fileU" # Libère le lock, la machine est à nouveau disponible - ynh_print_info --message="Finished upgrading $name" -} diff --git a/ynh_lxd_demo b/ynh_lxd_demo new file mode 100644 index 0000000..b3a7b37 --- /dev/null +++ b/ynh_lxd_demo @@ -0,0 +1,230 @@ +#!/bin/bash + +#================================================= +# DEMO HELPERS +#================================================= + +# Start an LXC container in demo mode +# +# usage: ynh_lxc_demo_start --name=name --ip=ip +# | arg: -n, --name= - name of the LXC +# | arg: -n, --ip= - demo ip of the lxc +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_demo_start () { + # Declare an array to define the options of this helper. + local legacy_args=ni + local -A args_array=([n]=name= [i]=ip=) + local name + local ip + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_lxc_stop --name="$name" + lxc config device set $name eth1 ipv4.address $ip + _ynh_lxc_start_and_wait --name="$name" +} + +# Stop an LXC container in demo mode +# +# usage: ynh_lxc_demo_stop --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_demo_stop () { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_lxc_stop --name="$name" + lxc config device unset $name eth1 ipv4.address +} + +# Upgrading demo container +# +# usage: ynh_lxc_demo_upgrade --name=name --time_to_switch=time_to_switch +# | arg: -n, --name= - name of the LXC +# | arg: -t, --time_to_switch= - time to switch +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_demo_upgrade () { + # Declare an array to define the options of this helper. + local legacy_args=nt + local -A args_array=([n]=name= [t]=time_to_switch=) + local name + local time_to_switch + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_print_info --message="Upgrading $name" + # Attend que la machine soit éteinte. + # Timeout à $time_to_switch +5 minutes, en seconde + TIME_OUT=$(($time_to_switch * 60 + 300)) + wait_period=0 + while ! ynh_lxc_is_stopped --name=$name + do + wait_period=$(($wait_period+10)) + if [ $wait_period -gt $TIME_OUT ];then + break + else + sleep 5 + fi + done + + while test -e /var/lib/lxd/$name.lock_fileS; do + sleep 5 # Attend que le conteneur soit libéré par le script switch. + done + + touch /var/lib/lxd/$name.lock_fileU # Met en place un fichier pour indiquer que la machine est indisponible pendant l'upgrade + + # Supprime les éventuels swap présents. + /sbin/swapoff /var/lib/lxd/$name/rootfs/swap_* + + # Restaure le snapshot + ynh_lxc_snapshot_load --name=$name --snapname=snap0 + + # Démarre le conteneur + date >> "$final_path/demo_boot.log" + + # Update + update_apt=0 + ynh_lxc_run_inside --name="$name" --command="apt-get update" + ynh_lxc_run_inside --name="$name" --command="apt-get dist-upgrade --dry-run | grep -q "^Inst " > /dev/null" # Vérifie si il y aura des mises à jour. + if [ "$?" -eq 0 ]; then + date + update_apt=1 + # Upgrade + ynh_lxc_run_inside --name="$name" --command="apt-get dist-upgrade --option Dpkg::Options::=--force-confold -yy" + # Clean + ynh_lxc_run_inside --name="$name" --command="apt-get autoremove -y" + ynh_lxc_run_inside --name="$name" --command="apt-get autoclean" + fi + ynh_lxc_run_inside --name="$name" --command="yunohost tools update" + ynh_lxc_run_inside --name="$name" --command="yunohost tools upgrade system" + + # Exécution des scripts de upgrade.d + LOOP=$((LOOP + 1)) + while read LIGNE + do + if [ ! "$LIGNE" == "exemple" ] && [ ! "$LIGNE" == "old_scripts" ] && [ ! "$LIGNE" == "Constant_upgrade" ] && ! echo "$LIGNE" | grep -q ".fail$" # Le fichier exemple, le dossier old_scripts et les scripts fail sont ignorés + then + date + # Exécute chaque script trouvé dans upgrade.d + ynh_print_info --message="Exécution du script $LIGNE sur le conteneur $name" + /bin/bash "$final_path/upgrade.d/$LIGNE" $name + if [ "$?" -ne 0 ]; then # Si le script a échoué, le snapshot est annulé. + ynh_print_info --message="Échec du script $LIGNE" + mv -f "$final_path/upgrade.d/$LIGNE" "$final_path/upgrade.d/$LIGNE.fail" + ynh_print_info --message="Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\nLe script a été renommé en .fail, il ne sera plus exécuté tant que le préfixe ne sera pas retiré.\n\nExtrait du log:\n$(tail -n +$log_line "$script_dir/demo_upgrade.log")" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "Demo Yunohost" $MAIL_ADDR + update_apt=0 + else + ynh_print_info --message="Le script $LIGNE a été exécuté sans erreur" + update_apt=1 + fi + fi + done <<< "$(ls -1 "$final_path/upgrade.d")" + + # Exécution des scripts de upgrade.d/Constant_upgrade + while read LIGNE + do + if [ "$update_apt" -eq "1" ] + then + date + # Exécute chaque script trouvé dans upgrade.d/Constant_upgrade + ynh_print_info --message="Exécution du script $LIGNE sur le conteneur $name" + /bin/bash "$final_path/upgrade.d/Constant_upgrade/$LIGNE" $name + if [ "$?" -ne 0 ]; then + ynh_print_info --message="Échec du script $LIGNE" + ynh_print_info --message="Échec d'exécution du script d'upgrade $LIGNE sur le conteneur $name sur le serveur de demo $DOMAIN!\n" + else + ynh_print_info --message="Le script $LIGNE a été exécuté sans erreur" + fi + fi + done <<< "$(ls -1 "$final_path/upgrade.d/Constant_upgrade")" + + # Upgrade des apps + ynh_lxc_run_inside --name="$name" --command="yunohost tools update" + ynh_lxc_run_inside --name="$name" --command="systemctl restart nginx" + ynh_lxc_run_inside --name="$name" --command="yunohost tools upgrade apps" + ynh_lxc_run_inside --name="$name" --command="systemctl restart nginx" + + # Arrêt de la machine virtualisée + ynh_lxc_stop --name=$name + + if [ "$update_apt" -eq "1" ] + then + # Archivage du snapshot + ynh_exec_warn_less tar -cz --acls --xattrs -f /var/lib/lxd/snapshots/$name/snap0.tar.gz /var/lib/lxd/snapshots/$name/snap0 + # Remplacement du snapshot + ynh_lxc_snapshot_create --name=$name --snapname=snap0 + + if [ "$LOOP" -eq 2 ] + then # Après l'upgrade du 2e conteneur, déplace les scripts dans le dossier des anciens scripts si ils ont été exécutés avec succès. + ls -1 "$final_path/upgrade.d" | while read LIGNE + do + if [ ! "$LIGNE" == "exemple" ] && [ ! "$LIGNE" == "old_scripts" ] && [ ! "$LIGNE" == "Constant_upgrade" ] && ! echo "$LIGNE" | grep -q ".fail$" # Le fichier exemple, le dossier old_scripts et les scripts fail sont ignorés + then + mv -f "$final_path/upgrade.d/$LIGNE" "$final_path/upgrade.d/old_scripts/$LIGNE" + fi + done + fi + fi + ynh_secure_remove --file="/var/lib/lxd/$name.lock_fileU" # Libère le lock, la machine est à nouveau disponible + ynh_print_info --message="Finished upgrading $name" +} + +# Restore an LXC container from snap0 snapshot +# +# usage: ynh_lxc_demo_restore_from_snapshot --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_demo_restore_from_snapshot () { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_print_info --message="Restoring LXC container $name from snapshot" + ynh_lxc_snapshot_load --name=$name --snapname=snap0 + if ynh_lxc_check_container_start --name=$name + then + ynh_print_info --message="LXC container $name is working." + return 0 + else + ynh_print_info --message="LXC container $name is broken." + return 1 + fi +} + +# Restore an LXC container from an archive +# +# usage: ynh_lxc_demo_restore_from_archive --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_demo_restore_from_archive () { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ! test -e "/var/lib/lxd/snapshots/$name/snap1.tar.gz"; then + ynh_print_info --message="No snapshot archive for LXC container $name" + return 1 + fi + ynh_print_info --message="Restoring snapshot archive for LXC container $name" + ynh_print_info --message="Deleting snapshot" + ynh_lxc_snapshot_delete --name=$name --snapname=snap0 + ynh_print_info --message="Untar archive" + tar -x --acls --xattrs -f /var/lib/lxd/snapshots/$name/snap0.tar.gz -C / + ynh_lxc_demo_restore_from_snapshot --name=$name + return $? +} From 2d7e03f28d9f573beb9346275ced1a91757cc6a0 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 6 Nov 2022 18:03:24 +0100 Subject: [PATCH 24/25] Update demo_lxc_build.sh --- demo_lxc_build.sh | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/demo_lxc_build.sh b/demo_lxc_build.sh index 13397e3..b139fc0 100755 --- a/demo_lxc_build.sh +++ b/demo_lxc_build.sh @@ -73,54 +73,53 @@ ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost -v" | tee -a "$LOG_BU ynh_print_info --message="> Installing demo apps" | tee -a "$LOG_BUILD_LXC" 2>&1 -if [ ${DONT_INSTALL_FOR_NOW:-0} -eq 1 ]; then # Ampache -ynh_print_info --message="installing Ampache" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing ampache" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install ampache --force --args \"domain=$domain&path=/ampache&admin=$demo_user&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Baikal -ynh_print_info --message="installing baikal" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing baikal" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install baikal --force --args \"domain=$domain&path=/baikal&password=$demo_password&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Agendav ynh_print_info --message="Installation d'agendav" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install agendav --force --args \"domain=$domain&path=/agendav&language=en&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Dokuwiki -ynh_print_info --message="installing dokuwiki" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing dokuwiki" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install dokuwiki --force --args \"domain=$domain&path=/dokuwiki&admin=$demo_user&is_public=1&language=en&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Etherpad -ynh_print_info --message="installing etherpad" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing etherpad" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install etherpad_mypads --force --args \"domain=$domain&path=/etherpad&admin=$demo_user&password=administration&language=en&is_public=1&export=none&mypads=1&useldap=0&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Hextris -ynh_print_info --message="installing hextris" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing hextris" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install hextris --force --args \"domain=$domain&path=/hextris&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Jirafeau -ynh_print_info --message="installing jirafeau" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing jirafeau" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install jirafeau --force --args \"domain=$domain&path=/jirafeau&admin_user=$demo_user&upload_password=$demo_password&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Kanboard -ynh_print_info --message="installing kanboard" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing kanboard" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install kanboard --force --args \"domain=$domain&path=/kanboard&admin=$demo_user&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Nextcloud -ynh_print_info --message="installing nextcloud" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing nextcloud" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install nextcloud --force --args \"domain=$domain&path=/nextcloud&admin=$demo_user&user_home=0&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Opensondage -ynh_print_info --message="installing opensondage" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing opensondage" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install opensondage --force --args \"domain=$domain&path=/date&admin=$demo_user&language=en&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Phpmyadmin -ynh_print_info --message="installing phpmyadmin" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing phpmyadmin" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install phpmyadmin --force --args \"domain=$domain&path=/phpmyadmin&admin=$demo_user&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Piwigo -ynh_print_info --message="installing piwigo" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing piwigo" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install piwigo --force --args \"domain=$domain&path=/piwigo&admin=$demo_user&is_public=1&language=en&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Rainloop -ynh_print_info --message="installing rainloop" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing rainloop" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install rainloop --force --args \"domain=$domain&path=/rainloop&is_public=No&password=$demo_password&ldap=Yes&language=en&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Roundcube -ynh_print_info --message="installing roundcube" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing roundcube" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install roundcube --force --args \"domain=$domain&path=/webmail&with_carddav=0&with_enigma=0&language=en_GB&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Searx -ynh_print_info --message="installing searx" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing searx" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install searx --force --args \"domain=$domain&path=/searx&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Shellinabox -ynh_print_info --message="installing shellinabox" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing shellinabox" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install shellinabox --force --args \"domain=$domain&path=/ssh&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Désactive l'accès à shellinabox ynh_lxc_run_inside --name="$lxc_name1" --command="rm /etc/nginx/conf.d/$domain.d/shellinabox.conf" @@ -128,24 +127,23 @@ ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app setting shellinab ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app setting shellinabox domain -d" ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app ssowatconf" # Strut -ynh_print_info --message="installing strut" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing strut" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install strut --force --args \"domain=$domain&path=/strut&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Transmission -ynh_print_info --message="installing transmission" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing transmission" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install transmission --force --args \"domain=$domain&path=/torrent&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Ttrss -ynh_print_info --message="installing ttrss" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing ttrss" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install ttrss --force --args \"domain=$domain&path=/ttrss&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Wallabag -ynh_print_info --message="installing wallabag" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing wallabag" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wallabag2 --force --args \"domain=$domain&path=/wallabag&admin=$demo_user&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Wordpress -ynh_print_info --message="installing wordpress" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing wordpress" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install wordpress --force --args \"domain=$domain&path=/blog&admin=$demo_user&language=en_US&multisite=0&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 # Zerobin -ynh_print_info --message="installing zerobin" | tee -a "$LOG_BUILD_LXC" 2>&1 +ynh_print_info --message="Installing zerobin" | tee -a "$LOG_BUILD_LXC" 2>&1 ynh_lxc_run_inside --name="$lxc_name1" --command="yunohost app install zerobin --force --args \"domain=$domain&path=/zerobin&is_public=1&\"" | tee -a "$LOG_BUILD_LXC" 2>&1 -fi # ******** From 5ca012c2267f37b21d5287f19678857e2c5c5b2a Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 6 Nov 2022 19:02:46 +0100 Subject: [PATCH 25/25] Improving readme --- README.MD | 41 ++++---- demo_lxc_build_init.sh | 229 +++++++++++------------------------------ demo_lxc_remove.sh | 33 +++--- 3 files changed, 98 insertions(+), 205 deletions(-) diff --git a/README.MD b/README.MD index 8a78446..768124e 100644 --- a/README.MD +++ b/README.MD @@ -1,42 +1,39 @@ -# Scripts de gestion et mise en place des serveurs de demo Yunohost sous LXC +# Installation and management scripts for YunoHost demo servers using LXD -## Prérequis -* `apt install sudo git nginx -y` -* register a dns name to your demo server and make you demo server available from internet for TCP/80 and TCP/443 +## Requirements +* A debian server +* Register a dns name to your demo server and make you demo server available from internet for TCP/80 and TCP/443 ## demo_lxc_build_init -Ce script prépare le serveur hôte à recevoir les conteneurs LXC de demo. -Il doit être exécuté une seule fois et en premier. +This script will install YunoHost and the app yunohost_demo_ynh ## demo_lxc_build -Ce script construit les conteneurs de demo et les paramètres. -Il met également en places les crons et démarre le 1er conteneur. -Après l'exécution de ce script, la demo est en place. +This script will build the demo containers. +It also install crons and start the first container. +After executing this script, YunoHost demo is working. ## demo_lxc_destroy -Ce script arrête les conteneurs et les détruits. -Il sert en général à une reconstruction des conteneurs avec de nouveaux paramètres. Et devrait donc être suivi du script demo_lxc_build. +This script stop the containers and destroy them. +It is usually used for rebuilding containers with new parameters. And should therefore be followed by the demo_lxc_build script. ## demo_lxc_remove -Ce script, après avoir appelé demo_lxc_destroy va désinstaller LXC et remettre le serveur à son état d'origine. Retirant les paramétrages mis en place pour la demo. -Il ne devrait être utilisé qu'en cas de changement de serveur hôte. +This script will remove yunohost_demo_ynh. +It should only be used when the host server is changed. ## demo_start -Permet simplement un démarrage propre du 1er conteneur. Avec la mise en place du bridge réseau et des règles de parefeu. +Simply allows a clean start of the first container. With network bridge and firewall rules in place. ## demo_stop -Arrête proprement les conteneurs et désactive la config réseau spécifique. +Cleanly shuts down containers and disables the specific network config. ## demo_switch -Ce script est placé en cron par demo_lxc_build. -Il va alterner toutes les 30 minutes (par défaut) les 2 conteneurs et rétablir le snapshot du conteneurs après son extinction. +It will alternate every 30 minutes (by default) the 2 containers and restore the snapshot of the container after it is shut down. ## demo_upgrade -Ce script est placé en cron par demo_lxc_build. -demo_upgrade vérifie chaque nuit les mises à jour sur les conteneurs, et les applique le cas échéant. Si nécessaire, il se charge également de refaire le snapshot. +It checks every night for updates on the containers, and applies them if necessary. If necessary, it also takes care of redoing the snapshot. ## demo_restore_crash -Tente de réparer les conteneurs qui ne démarre pas à partir des snapshots ou des archives de snapshots précédents. +Attempts to repair containers that do not boot from previous snapshots or snapshot archives. -Pour effectuer des modifications pérennes sur les conteneurs, il est possible d'ajouter des scripts dans le dossier upgrade.d. Ces scripts seront exécutés après l'upgrade des packages dans chaque conteneur. -En cas d'échec d'exécution, le script sera mis de côté en attendant sa correction. Sinon, il sera archivé. +To make permanent changes to the containers, it is possible to add scripts to the upgrade.d folder. These scripts will be executed after the upgrade of the packages in each container. +In case of execution failure, the script will be put aside until it is corrected. Otherwise, it will be archived. diff --git a/demo_lxc_build_init.sh b/demo_lxc_build_init.sh index 3909895..750906d 100755 --- a/demo_lxc_build_init.sh +++ b/demo_lxc_build_init.sh @@ -1,188 +1,77 @@ #!/bin/bash -# Installe LXC et les paramètres réseaux avant de procéder au build. +cd "$(dirname $(realpath $0))" -# Récupère le dossier du script -if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$(echo $PWD/$(dirname "$0" | cut -d '.' -f2) | sed 's@/$@@')"; fi +if (( $# < 3 )) +then + cat << EOF +Usage: ./demo_lxc_build_init.sh some.domain.tld SecretAdminPasswurzd! Demo_User Demo_Password -LOG=$(cat "$script_dir/demo_lxc_build.sh" | grep LOG= | cut -d '=' -f2) -LOG_BUILD_LXC="$script_dir/$LOG" -lxc_name1=$(cat "$script_dir/demo_lxc_build.sh" | grep lxc_name1= | cut -d '=' -f2) -lxc_name2=$(cat "$script_dir/demo_lxc_build.sh" | grep lxc_name2= | cut -d '=' -f2) -lxdbr_demo_network=$(cat "$script_dir/demo_lxc_build.sh" | grep lxdbr_demo_network= | cut -d '=' -f2) -lxc_ip1=$(cat "$script_dir/demo_lxc_build.sh" | grep lxc_ip1= | cut -d '=' -f2) -lxc_ip2=$(cat "$script_dir/demo_lxc_build.sh" | grep lxc_ip2= | cut -d '=' -f2) -MAIL_ADDR=$(cat "$script_dir/demo_lxc_build.sh" | grep MAIL_ADDR= | cut -d '=' -f2) +1st and 2nd arguments are for yunohost postinstall + - domain + - admin password -# Check user -echo $(whoami) > "$script_dir/setup_user" +3rd and 4th argument are used for the demo + - demo_user + - demo_password -read -p "Indiquer le nom de domaine du serveur de demo: " DOMAIN -echo "$DOMAIN" > "$script_dir/domain.ini" - -# Créer le dossier de log -sudo mkdir -p $(dirname $LOG_BUILD_LXC) - -echo -e "> Update et install lxc, lxctl et mailutils" | tee "$LOG_BUILD_LXC" -sudo apt-get update | tee -a "$LOG_BUILD_LXC" 2>&1 -sudo apt-get install -y lxc lxctl mailutils certbot | tee -a "$LOG_BUILD_LXC" 2>&1 - -echo -e "> Autoriser l'ip forwarding, pour router vers la machine virtuelle." | tee -a "$LOG_BUILD_LXC" -echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/lxc_demo.conf | tee -a "$LOG_BUILD_LXC" 2>&1 -sudo sysctl -p /etc/sysctl.d/lxc_demo.conf | tee -a "$LOG_BUILD_LXC" 2>&1 - -echo -e "> Ajoute un brige réseau pour la machine virtualisée" | tee -a "$LOG_BUILD_LXC" -echo | sudo tee /etc/network/interfaces.d/lxc_demo <&1 -auto lxc_demo -iface lxc_demo inet static - address $lxdbr_demo_network.1/24 - bridge_ports none - bridge_fd 0 - bridge_maxwait 0 EOF - -echo -e "> Active le bridge réseau" | tee -a "$LOG_BUILD_LXC" -sudo ifup lxc_demo --interfaces=/etc/network/interfaces.d/lxc_demo | tee -a "$LOG_BUILD_LXC" 2>&1 - -echo -e "> Mise en place de la connexion ssh vers l'invité." | tee -a "$LOG_BUILD_LXC" -if [ -e $HOME/.ssh/$lxc_name1 ]; then - rm -f $HOME/.ssh/$lxc_name1 $HOME/.ssh/$lxc_name1.pub - ssh-keygen -f $HOME/.ssh/known_hosts -R $lxdbr_demo_network$lxc_ip1 - ssh-keygen -f $HOME/.ssh/known_hosts -R $lxdbr_demo_network$lxc_ip2 + exit 1 fi -ssh-keygen -t rsa -f $HOME/.ssh/$lxc_name1 -P '' | tee -a "$LOG_BUILD_LXC" 2>&1 -echo | tee -a $HOME/.ssh/config <&1 -# ssh $lxc_name1 -Host $lxc_name1 -Hostname $lxdbr_demo_network$lxc_ip1 -User ssh_demo -IdentityFile $HOME/.ssh/$lxc_name1 -Host $lxc_name2 -Hostname $lxdbr_demo_network$lxc_ip2 -User ssh_demo -IdentityFile $HOME/.ssh/$lxc_name1 -# End ssh $lxc_name1 -EOF +domain=$1 +yuno_pwd=$2 +demo_user=$3 +demo_password=$4 -echo -e "> Mise en place du reverse proxy et du load balancing" | tee -a "$LOG_BUILD_LXC" -echo | sudo tee /etc/nginx/conf.d/$DOMAIN.conf <&1 -#upstream $DOMAIN { -# server $lxdbr_demo_network$lxc_ip1:443 ; -# server $lxdbr_demo_network$lxc_ip2:443 ; -#} - -server { - listen 80; - listen [::]:80; - server_name $DOMAIN; - - location '/.well-known/acme-challenge' { - default_type "text/plain"; - root /tmp/letsencrypt-auto; - } - - access_log /var/log/nginx/$DOMAIN-access.log; - error_log /var/log/nginx/$DOMAIN-error.log; -} -EOF - -sudo service nginx reload - -echo -e "> Création du certificat SSL." | tee -a "$LOG_BUILD_LXC" -sudo mkdir -p /etc/letsencrypt - -# Créer le fichier de config -echo | sudo tee /etc/letsencrypt/conf.ini <&1 -################################# -# Let's encrypt configuration # -################################# - -# Use a 4096 bit RSA key instead of 2048 -rsa-key-size = 4096 - -# Uncomment and update to register with the specified e-mail address -email = $MAIL_ADDR - -# Uncomment to use the webroot authenticator. Replace webroot-path with the -# path to the public_html / webroot folder being served by your web server. -# avec le contenu dans /tmp/letsencrypt-auto -authenticator = webroot -webroot-path = /tmp/letsencrypt-auto - -# Utiliser l'interface texte -text = True -# Uncomment to automatically agree to the terms of service of the ACME server -agree-tos = true - -# (Serveur de test uniquement : si vous l'utilisez, -# votre certificat ne sera pas vraiment valide) -# server = https://acme-staging-v02.api.letsencrypt.org/directory -EOF - -mkdir -p /tmp/letsencrypt-auto -# Créer le certificat -sudo certbot certonly --config /etc/letsencrypt/conf.ini -d $DOMAIN --no-eff-email - -# Route l'upstream sur le port 443. Le port 80 servait uniquement à let's encrypt -# sudo sed -i "s/server $lxdbr_demo_network$lxc_ip1:80 ;/server $lxdbr_demo_network$lxc_ip1:443 ;/" /etc/nginx/conf.d/$DOMAIN.conf -# Décommente les lignes du certificat -# sudo sed -i "s/#\tssl_certificate/\tssl_certificate/g" /etc/nginx/conf.d/$DOMAIN.conf -# Supprime les commentaires dans la conf nginx - -echo | sudo tee /etc/nginx/conf.d/$DOMAIN.conf <&1 -#upstream $DOMAIN { -# server $lxdbr_demo_network$lxc_ip1:443 ; -# server $lxdbr_demo_network$lxc_ip2:443 ; -#} - -server { - listen 80; - listen [::]:80; - server_name $DOMAIN; - - location '/.well-known/acme-challenge' { - default_type "text/plain"; - root /tmp/letsencrypt-auto; - } - - access_log /var/log/nginx/$DOMAIN-access.log; - error_log /var/log/nginx/$DOMAIN-error.log; +echo_bold () { + echo -e "\e[1m$1\e[0m" } -server { - listen 443 ssl; - listen [::]:443 ssl; - server_name $DOMAIN; +# ----------------------------------------------------------------- - ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem; - ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem; - ssl_session_timeout 5m; - ssl_session_cache shared:SSL:50m; - ssl_prefer_server_ciphers on; - ssl_protocols TLSv1 TLSv1.1 TLSv1.2; - ssl_ciphers ALL:!aNULL:!eNULL:!LOW:!EXP:!RC4:!3DES:+HIGH:+MEDIUM; - add_header Strict-Transport-Security "max-age=31536000;"; +function install_dependencies() { - location / { - proxy_pass https://$DOMAIN; - proxy_redirect off; - proxy_set_header Host \$host; - proxy_set_header X-Real-IP \$remote_addr; - proxy_set_header X-Forwarded-Proto \$scheme; - proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Host \$server_name; - } - - access_log /var/log/nginx/$DOMAIN-access.log; - error_log /var/log/nginx/$DOMAIN-error.log; + echo_bold "> Installing dependencies..." + apt-get update + apt-get install -y curl wget git python3-pip } -EOF -sudo service nginx reload +function setup_yunohost() { + + echo_bold "> Setting up Yunohost..." + local DIST="bullseye" + local INSTALL_SCRIPT="https://install.yunohost.org/$DIST" + curl $INSTALL_SCRIPT | bash -s -- -a + + echo_bold "> Running yunohost postinstall" + yunohost tools postinstall --domain $domain --password $yuno_pwd -echo -e "Le serveur est prêt à déployer les conteneurs de demo." -echo -e "Exécutez le script demo_lxc_build.sh pour créer les conteneurs et mettre en place la demo." + echo_bold "> Disabling unecessary services to save up RAM" + for SERVICE in mysql php7.3-fpm metronome rspamd dovecot postfix redis-server postsrsd yunohost-api avahi-daemon + do + systemctl stop $SERVICE + systemctl disable $SERVICE --quiet + done +} -# Déploie les conteneurs de demo -# "$script_dir/demo_lxc_build.sh" +function setup_yunohost_demo() { + echo_bold "> Installation of yunohost_demo..." + if ! yunohost app list --output-as json --quiet | jq -e '.apps[] | select(.id == "yunohost_demo")' >/dev/null + then + yunohost app install --force https://github.com/YunoHost-Apps/yunohost_demo_ynh -a "domain=$domain&demo_user=$demo_user&demo_password=$demo_password" + fi +} + +# ========================= +# Main stuff +# ========================= + +install_dependencies + +[ -e /usr/bin/yunohost ] || setup_yunohost + +setup_yunohost_demo + +echo "Done!" +echo " " diff --git a/demo_lxc_remove.sh b/demo_lxc_remove.sh index 08bfaf6..4dfe3dd 100755 --- a/demo_lxc_remove.sh +++ b/demo_lxc_remove.sh @@ -15,18 +15,25 @@ final_path=$(ynh_app_setting_get --app=$app --key=final_path) lxc_name1=$(ynh_app_setting_get --app=$app --key=lxc_name1) path_url=$(ynh_app_setting_get --app=$app --key=path) -/bin/bash "$final_path/demo_lxc_destroy.sh" +echo_bold () { + echo -e "\e[1m$1\e[0m" +} -# Suppression du reverse proxy -echo -e "> Suppression de la config nginx" -sudo rm /etc/nginx/conf.d/$DOMAIN.conf -sudo service nginx reload +# ----------------------------------------------------------------- -# Suppression du certificat Let's encrypt -echo -e "> Suppression de Let's encrypt" -sudo rm -r /etc/letsencrypt -sudo rm -r ~/.local/share/letsencrypt -sudo rm -r ~/letsencrypt -sudo rm -r /var/lib/letsencrypt -# Supprime la tache cron -sudo rm /etc/cron.weekly/Certificate_Renewer +function remove_yunohost_demo() { + echo_bold "> Installation of yunohost_demo..." + if yunohost app list --output-as json --quiet | jq -e '.apps[] | select(.id == "yunohost_demo")' >/dev/null + then + yunohost app remove yunohost_demo --purge + fi +} + +# ========================= +# Main stuff +# ========================= + +remove_yunohost_demo + +echo "Done!" +echo " "