packaging doc: Remove useless page about bash trap ... + properly separate helper/resource pages because only one is displayed

This commit is contained in:
Alexandre Aubin 2023-02-01 23:56:21 +01:00
parent ba549a464b
commit d5e18cbd03
5 changed files with 0 additions and 240 deletions

View file

@ -1,120 +0,0 @@
---
title: Trap
template: docs
taxonomy:
category: docs
routes:
default: '/packaging_apps_trap'
---
Trap est une commande interne du shell permettant de capturer les signaux de sorties des commandes exécutées dans le shell courant et ses sous-shell.
Toute commande exécutée dans le shell renvoi un signal de sortie à la fin de son exécution. Soit 0 pour indiquer la fin de l'exécution de la commande, soit une valeur non nulle indiquant une interruption de celle-ci.
Dans le cas des scripts d'installation, trap va nous permettre de détecter une commande interrompue au milieu de son exécution en raison d'une erreur.
La détection de cette erreur permettra de mettre fin à l'installation et de renvoyer vers le script remove en vue d'un nettoyage des résidus.
Trap s'utilise de la manière suivante :
```bash
trap 'commande' liste_de_signaux
```
Pour simplifier, nous utiliserons le pseudo signal `ERR` pour rassembler tout les signaux d'erreur.
On pourrait ajouter simplement cette ligne en début de script :
```bash
trap "echo Erreur d'installation" ERR
```
Après cette ligne, toute commande provoquant une erreur déclenchera l'affichage du message indiqué par trap.
L'ensemble du shell courant et du sous-shell sera pris en charge par trap.
Pour arrêter la capture des signaux par trap, on peut simplement désactiver trap.
```bash
trap ERR
```
Ou ignorer complètement les signaux de sorties concernés.
```bash
trap "" ERR
```
Dans ce dernier cas, le signal d'interruption n'aura aucun effet sur le shell. Cela peux être utile pour une commande dont la sortie en erreur ne doit pas impacter le déroulement du script d'installation.
### Stopper le script d'installation et nettoyer avant de quitter.
En cas d'erreur du script d'installation, trap doit nous permettre de stopper l'installation, puis de nettoyer les fichiers résiduels partiellement installés avant de quitter le script.
Pour cela, nous allons prévoir une fonction dédiée à l'échec de l'installation.
```bash
# Delete files and db if exit with an error
EXIT_PROPERLY () {
trap ERR # Disable trap
echo -e "\e[91m \e[1m" # Shell in light red bold
echo -e "!!\n $app install's script has encountered an error. Installation was cancelled.\n!!"
echo -e "\e[22m" # Remove bold
# Clean hosts
sudo sed -i '/#leed/d' /etc/hosts
if [ $ynh_version = "2.2" ]; then
/bin/bash ./remove # Call the script remove. In 2.2, this behavior is not automatic.
fi
exit 1
}
```
La fonction EXIT_PROPERLY doit indiquer à l'utilisateur l'échec de l'installation et nettoyer les résidus qui ne seront pas pris en charge par le script remove. Ce dernier sera automatiquement appelé à la suite de l'exit 1 avec YunoHost 2.4
Après cette fonction, on peut mettre en place la capture des signaux par trap.
```bash
trap EXIT_PROPERLY ERR
```
Si une commande échoue durant l'installation, la fonction EXIT_PROPERLY sera appelée, mettant fin à l'installation.
Pour simplifier la capture des signaux et les ignorer pour des commandes ponctuelles. Il est possible de placer les appels à trap dans des fonctions.
```bash
TRAP_ON () { # Activate signal capture
trap EXIT_PROPERLY ERR # Capturing exit signals on error
}
TRAP_OFF () { # Ignoring signal capture until TRAP_ON
trap '' ERR # Ignoring exit signals
}
```
> Ma fonction TRAP_OFF ne fonctionne pas. Pour une raison qui m'échappe. Utiliser `trap '' ERR` directement fonctionne très bien en revanche.
Pour gérer les éventuelles erreur d'installations, on peut donc simplement ajouter ce morceau de code après la récupération des arguments :
```bash
# Delete files and db if exit with an error
EXIT_PROPERLY () {
trap ERR # Disable trap
echo -e "\e[91m \e[1m" # Shell in light red bold
echo -e "!!\n $app install's script has encountered an error. Installation was cancelled.\n!!"
echo -e "\e[22m" # Remove bold
# Clean hosts
sudo sed -i '/#leed/d' /etc/hosts
if [ $ynh_version = "2.2" ]; then
/bin/bash ./remove # Call the script remove. In 2.2, this behavior is not automatic.
fi
exit 1
}
TRAP_ON () { # Activate signal capture
trap EXIT_PROPERLY ERR # Capturing exit signals on error
}
TRAP_OFF () { # Ignoring signal capture until TRAP_ON
trap '' ERR # Ignoring exit signals
}
TRAP_ON
```

View file

@ -1,120 +0,0 @@
---
title: Trap
template: docs
taxonomy:
category: docs
routes:
default: '/packaging_apps_trap'
---
Trap is an internal shell command used to capture the output signals of commands executed in the current shell and its subshells.
Any command executed in the shell returns an exit signal at the end of its execution. Either 0 to indicate the end of the execution of the command, or a non-zero value indicating an interruption thereof.
In the case of installation scripts, trap will allow us to detect a command interrupted in the middle of its execution due to an error.
Detection of this error will allow the installation to be terminated and returned to the remove script for cleaning up residues.
Trap is used as follows:
```bash
trap 'commande' liste_de_signaux
```
To simplify, we will use the pseudo signal `ERR` to gather all the error signals.
We could simply add this line at the beginning of the script:
```bash
trap "echo Erreur d'installation" ERR
```
After this line, any command causing an error will trigger the display of the message indicated by trap.
All of the current shell and the subshell will be supported by trap.
To stop capturing signals with trap, you can simply deactivate trap.
```bash
trap ERR
```
Or completely ignore the affected output signals.
```bash
trap "" ERR
```
In the latter case, the interrupt signal will have no effect on the shell. This can be useful for a command whose error output should not impact the progress of the installation script.
### Stop the installation script and clean up before exiting.
In the event of an error in the installation script, trap must allow to stop the installation, then clean up the partially installed residual files before leaving the script.
For this, we will provide a function dedicated to the installation failure.
```bash
# Delete files and db if exit with an error
EXIT_PROPERLY () {
trap ERR # Disable trap
echo -e "\e[91m \e[1m" # Shell in light red bold
echo -e "!!\n $app install's script has encountered an error. Installation was cancelled.\n!!"
echo -e "\e[22m" # Remove bold
# Clean hosts
sudo sed -i '/#leed/d' /etc/hosts
if [ $ynh_version = "2.2" ]; then
/bin/bash ./remove # Call the script remove. In 2.2, this behavior is not automatic.
fi
exit 1
}
```
The `EXIT_PROPERLY` function must indicate to the user that the installation has failed and clean up any residue that will not be taken care of by the remove script. The latter will be automatically called after exit `1` with YunoHost 2.4
After this function, we can set up signal capture by trap.
```bash
trap EXIT_PROPERLY ERR
```
If a command fails during installation, the `EXIT_PROPERLY` function will be called, ending the installation.
To simplify the capture of signals and ignore them for specific commands. It is possible to place trap calls in functions.
```bash
TRAP_ON () { # Activate signal capture
trap EXIT_PROPERLY ERR # Capturing exit signals on error
}
TRAP_OFF () { # Ignoring signal capture until TRAP_ON
trap '' ERR # Ignoring exit signals
}
```
> The `TRAP_OFF` function does not work. For some reason. Using `trap '' ERR` directly works fine however.
To manage possible installation errors, we can therefore simply add this code after retrieving the arguments:
```bash
# Delete files and db if exit with an error
EXIT_PROPERLY () {
trap ERR # Disable trap
echo -e "\e[91m \e[1m" # Shell in light red bold
echo -e "!!\n $app install's script has encountered an error. Installation was cancelled.\n!!"
echo -e "\e[22m" # Remove bold
# Clean hosts
sudo sed -i '/#leed/d' /etc/hosts
if [ $ynh_version = "2.2" ]; then
/bin/bash ./remove # Call the script remove. In 2.2, this behavior is not automatic.
fi
exit 1
}
TRAP_ON () { # Activate signal capture
trap EXIT_PROPERLY ERR # Capturing exit signals on error
}
TRAP_OFF () { # Ignoring signal capture until TRAP_ON
trap '' ERR # Ignoring exit signals
}
TRAP_ON
```