Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4 enhancement allow custom known error #5

Merged
merged 5 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 64 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,72 @@ E: Unable to locate package fdsfqfqfsqdf
======================================================================
```

Auto fix for this common errors :
# NEW

`apt --fix-broken install`
Implement your own error handler : add your handler anywhere after the *include header* and before the `post` cmd :

```
add_fix_handler "string to grep in errors" "message to show if the string is found" "command to fix the error"
```

or

```
myFixFunc() {
echo "this is my fix"
}
add_fix_handler "string to grep in errors" "message to show if the string is found" myFixFunc
```

or

```
myTestFunct() {
echo "this is my test"
return 0; # 0 will trigger the fix, other return value don't trigger the fix
}
add_fix_handler myTestFunct "message to show if the string is found" "command to fix the error"
```

or

```
add_fix_handler "string to grep in errors" "*short message to show in default message" "command to fix the error"
```

or

```
add_fix_handler "string to grep in errors" "" "command to fix the error" #empty message uses the default message with "string to grep in errors"
```

Real life examples :

```
test_npm_ver() {
npm -v | grep "8.11.0" &>/dev/null
return $?
}
fix_npm_ver() {
sudo npm install -g npm@8.12.2
}
add_fix_handler test_npm_ver "*NPM 8.11.0" fix_npm_ver
```

or

```
add_fix_handler "EINTEGRITY" "" "sudo npm cache clean --force"
```

Auto fix alreday included for this common errors :

apt `apt --fix-broken install`

`sudo dpkg --configure -a`
dkpg `sudo dpkg --configure -a`

`EINTEGRITY`
npm `EINTEGRITY`

`npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/'`
npm `npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/'`

`changed its 'Suite' value from 'testing' to 'oldstable'`
apt `changed its 'Suite' value from 'testing' to 'oldstable'`
159 changes: 71 additions & 88 deletions dependance.lib
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ fi
PROGRESS_FILE=${TMPFOLDER}/${PROGRESS_FILENAME}
HR=$(printf '=%.0s' {1..70})
TAB="echo -n "________""
declare -A fix_handlers
h=0 # no fix_handlers

if [ -z $LANG_DEP ]; then
LANG_DEP=fr
fi

add_fix_handler() {
fix_handlers[$h,grepOrFunct]=$1
fix_handlers[$h,msgIfFound]=$2
fix_handlers[$h,fixStringOrFunct]=$3
((h++))
}

echo_success() {
echo -en "[ OK ]\r"
return 0
Expand Down Expand Up @@ -144,6 +153,43 @@ next() {
return $STEP_OK
}

fix () {
for (( f=0;f<h;f++ )); do # for each fix handler
if [ "$(type -t ${fix_handlers[$f,grepOrFunct]})" = "function" ]; then
${fix_handlers[$f,grepOrFunct]}
SEARCH_RESULT=$?
else
grep "${fix_handlers[$f,grepOrFunct]}" $TMPFOLDER/errorLog.$$ &>/dev/null
SEARCH_RESULT=$?
fi
if [ "$SEARCH_RESULT" -eq 0 ];then
if [ "${fix_handlers[$f,msgIfFound]}" = "" ]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "== TROUVÉ '${fix_handlers[$f,grepOrFunct]}' dans l'erreur, je lance une commande pour corriger, attendez 45sec et relancez les dépendances pour réessayer"
else
echo "== FOUND '${fix_handlers[$f,grepOrFunct]}' in error, launching countermeasures, wait 45sec and relaunch dependencies to retry"
fi
else
if [ "${fix_handlers[$f,msgIfFound]:0:1}" = "*" ]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "== TROUVÉ '${fix_handlers[$f,msgIfFound]:1}' dans l'erreur, je lance une commande pour corriger, attendez 45sec et relancez les dépendances pour réessayer"
else
echo "== FOUND '${fix_handlers[$f,msgIfFound]:1}' in error, launching countermeasures, wait 45sec and relaunch dependencies to retry"
fi
else
echo ${fix_handlers[$f,msgIfFound]}
fi
fi
if [ "$(type -t ${fix_handlers[$f,fixStringOrFunct]})" = "function" ]; then
${fix_handlers[$f,fixStringOrFunct]}
else
eval ${fix_handlers[$f,fixStringOrFunct]}
fi
HAS_FIX=1
fi
done
}

post() {
if [[ $STEP_IN_PROG -eq 1 ]]; then
STEP=99
Expand Down Expand Up @@ -176,95 +222,10 @@ post() {
else
echo "== ANALYSING ERRORS..."
fi

#try to fix if possible
grep "apt --fix-broken install" $TMPFOLDER/errorLog.$$ &>/dev/null
FOUND_FIX_BROKEN=$?
if [[ $FOUND_FIX_BROKEN -eq 0 ]]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "== TROUVÉ 'sudo apt --fix-broken install' dans l'erreur, je lance une commande pour corriger, attendez 45sec et relancez les dépendances pour réessayer"
else
echo "== FOUND 'sudo apt --fix-broken install' in error, launching countermeasures, wait 45sec and relaunch dependencies to retry"
fi
yes | sudo apt --fix-broken install
HAS_FIX=1
fi
grep "sudo dpkg --configure -a" $TMPFOLDER/errorLog.$$ &>/dev/null
FOUND_DPKG_CONFIGURE_A=$?
if [[ $FOUND_DPKG_CONFIGURE_A -eq 0 ]]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "== TROUVÉ 'dpkg --configure -a' dans l'erreur, je lance une commande pour corriger, attendez 45sec et relancez les dépendances pour réessayer"
else
echo "== FOUND 'dpkg --configure -a' in error, launching countermeasures, wait 45sec and relaunch dependencies to retry"
fi
sudo dpkg --configure -a --force-confdef
HAS_FIX=1
fi
grep "EINTEGRITY" $TMPFOLDER/errorLog.$$ &>/dev/null
FOUND_EINTEGRITY=$?
if [[ $FOUND_EINTEGRITY -eq 0 ]]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "== TROUVÉ 'EINTEGRITY' dans l'erreur, je lance une commande pour corriger, attendez 45sec et relancez les dépendances pour réessayer"
else
echo "== FOUND 'EINTEGRITY' in error, launching countermeasures, wait 45sec and relaunch dependencies to retry"
fi
sudo npm cache clean --force
HAS_FIX=1
fi
grep "npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/" $TMPFOLDER/errorLog.$$ &>/dev/null
FOUND_E128=$?
if [[ $FOUND_E128 -eq 0 ]]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "== TROUVÉ 'code 128' dans l'erreur, je lance une commande pour corriger, attendez 45sec et relancez les dépendances pour réessayer"
else
echo "== FOUND 'code 128' in error, launching countermeasures, wait 45sec and relaunch dependencies to retry"
fi
sudo chown -R root:root /root/.npm
HAS_FIX=1
fi
grep "changed its 'Suite' value from" $TMPFOLDER/errorLog.$$ &>/dev/null
FOUND_SUITECHANGED=$?
if [[ $FOUND_SUITECHANGED -eq 0 ]]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "== TROUVÉ -changed its 'Suite' value from- dans l'erreur, je lance une commande pour corriger, attendez 45sec et relancez les dépendances pour réessayer"
else
echo "== FOUND -changed its 'Suite' value from- in error, launching countermeasures, wait 45sec and relaunch dependencies to retry"
fi
sudo apt-get --allow-releaseinfo-change update
HAS_FIX=1
fi
grep "npm ERR! dest /usr/local/lib/node_modules/.homebridge-config-ui-x-" $TMPFOLDER/errorLog.$$ &>/dev/null
FOUND_ENOTEMPTY=$?
if [[ $FOUND_ENOTEMPTY -eq 0 ]]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "== TROUVÉ 'ENOTEMPTY' dans l'erreur, je lance une commande pour corriger, attendez 45sec et relancez les dépendances pour réessayer"
else
echo "== FOUND 'ENOTEMPTY' in error, launching countermeasures, wait 45sec and relaunch dependencies to retry"
fi
sudo rm -fR /usr/local/lib/node_modules/.homebridge-config-ui-x-*
HAS_FIX=1
fi
grep "npm ERR! dest /usr/lib/node_modules/.homebridge-config-ui-x-" $TMPFOLDER/errorLog.$$ &>/dev/null
FOUND_ENOTEMPTY=$?
if [[ $FOUND_ENOTEMPTY -eq 0 ]]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "== TROUVÉ 'ENOTEMPTY' dans l'erreur, je lance une commande pour corriger, attendez 45sec et relancez les dépendances pour réessayer"
else
echo "== FOUND 'ENOTEMPTY' in error, launching countermeasures, wait 45sec and relaunch dependencies to retry"
fi
sudo rm -fR /usr/lib/node_modules/.homebridge-config-ui-x-*
HAS_FIX=1
fi
npm -v | grep "8.11.0" &>/dev/null
NPM_8110=$?
if [[ $NPM_8110 -eq 0 ]]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "== TROUVÉ 'NPM 8.11.0', je lance une commande pour corriger, attendez 45sec et relancez les dépendances pour réessayer"
else
echo "== FOUND 'NPM 8.11.0', launching countermeasures, wait 45sec and relaunch dependencies to retry"
fi
sudo npm install -g npm@8.12.2
HAS_FIX=1
fi
fix

if [[ $HAS_FIX -eq 0 ]]; then
#show the error
if [ "$LANG_DEP" = "fr" ]; then
Expand All @@ -291,3 +252,25 @@ post() {
fi
rm -f ${PROGRESS_FILE}
}

# fix apt broken install
add_fix_handler "apt --fix-broken install" "" "yes | sudo apt --fix-broken install"

# fix dkpg error (probably stopped during apt install)
add_fix_handler "sudo dpkg --configure -a" "*dpkg --configure -a" "sudo dpkg --configure -a --force-confdef"

# fix npm cache integrity issue
add_fix_handler "EINTEGRITY" "" "sudo npm cache clean --force"

# fix npm cache permissions
add_fix_handler "npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/" "*code 128" "sudo chown -R root:root /root/.npm"

# if debian have changed the Suite value, allow releaseinfo change in apt-get
add_fix_handler "changed its 'Suite' value from" "*changed its Suite value from" "sudo apt-get --allow-releaseinfo-change update"

# check for ENOTEMPTY error in both /usr and /usr/local
add_fix_handler "npm ERR! dest /usr/local/lib/node_modules/.homebridge-config-ui-x-" "*ENOTEMPTY" "sudo rm -fR /usr/local/lib/node_modules/.homebridge-config-ui-x-*"
add_fix_handler "npm ERR! dest /usr/lib/node_modules/.homebridge-config-ui-x-" "*ENOTEMPTY" "sudo rm -fR /usr/lib/node_modules/.homebridge-config-ui-x-*"

add_fix_handler "npm ERR! dest /usr/local/lib/node_modules/.homebridge-alexa-" "*ENOTEMPTY" "sudo rm -fR /usr/local/lib/node_modules/.homebridge-alexa-*"
add_fix_handler "npm ERR! dest /usr/lib/node_modules/.homebridge-alexa-" "*ENOTEMPTY" "sudo rm -fR /usr/lib/node_modules/.homebridge-alexa-*"