Commit 7ac9d542 authored by HgO's avatar HgO

Resolve "Issues with the cleanup function when aborting"

parent 20549222
This diff is collapsed.
This diff is collapsed.
...@@ -27,16 +27,19 @@ ...@@ -27,16 +27,19 @@
declare -A LOG_LEVELS=([DEBUG]=0 [INFO]=1 [WARN]=2 [ERROR]=3) declare -A LOG_LEVELS=([DEBUG]=0 [INFO]=1 [WARN]=2 [ERROR]=3)
LOG_LEVEL=${LOG_LEVELS[INFO]} LOG_LEVEL=${LOG_LEVELS[INFO]}
CURRENT_SCRIPT_LOCATION="$(realpath $(dirname $0))"
export TEXTDOMAIN="neutrinet" export TEXTDOMAIN="neutrinet"
export TEXTDOMAINDIR="$(dirname $0)/locale" export TEXTDOMAINDIR="$CURRENT_SCRIPT_LOCATION/locale"
CURRENT_SCRIPT_DIR="$(dirname $0)"
YNH_IMG_LOCATION="https://build.yunohost.org" YNH_IMG_LOCATION="https://build.yunohost.org"
DEBIAN_CODENAME="stretch" DEBIAN_CODENAME="stretch"
INTERNETCUBE_PREFIX="internetcube" INTERNETCUBE_PREFIX="internetcube"
YUNOHOST_PREFIX="yunohost" YUNOHOST_PREFIX="yunohost"
CUBE_RESOURCES_LOCATION="cube_resources"
CUBE_RESOURCES_DIR="cube_resources"
CUBE_RESOURCES_LOCATION="${CURRENT_SCRIPT_LOCATION}/$CUBE_RESOURCES_DIR"
GPG_HOME_DIR=".gnupg" GPG_HOME_DIR=".gnupg"
GPG_YUNOHOST_KEY="0x360AAF3259A3E6FF" GPG_YUNOHOST_KEY="0x360AAF3259A3E6FF"
...@@ -56,6 +59,7 @@ INSTALL_SD_SCRIPT_NAME="install-sd.sh" ...@@ -56,6 +59,7 @@ INSTALL_SD_SCRIPT_NAME="install-sd.sh"
INSTALL_SD_CUSTOM_SCRIPT_ENABLED=true INSTALL_SD_CUSTOM_SCRIPT_ENABLED=true
HYPERCUBE_FILE="install.hypercube" HYPERCUBE_FILE="install.hypercube"
HYPERCUBE_LOCATION="${CURRENT_SCRIPT_LOCATION}/${HYPERCUBE_FILE}"
CUSTOM_SCRIPT_NAME="custom_neutrinet_specifics.sh" CUSTOM_SCRIPT_NAME="custom_neutrinet_specifics.sh"
CUSTOM_SCRIPT_PATH="${CUBE_RESOURCES_LOCATION}/$CUSTOM_SCRIPT_NAME" CUSTOM_SCRIPT_PATH="${CUBE_RESOURCES_LOCATION}/$CUSTOM_SCRIPT_NAME"
...@@ -348,14 +352,16 @@ function check_sudo() { ...@@ -348,14 +352,16 @@ function check_sudo() {
} }
function download_install_sd_script() { function download_install_sd_script() {
pushd $CUBE_RESOURCES_LOCATION > /dev/null
info $"Downloading $INSTALL_SD_SCRIPT_NAME script..." info $"Downloading $INSTALL_SD_SCRIPT_NAME script..."
cd $CUBE_RESOURCES_LOCATION
curl --show-error --fail --silent --output "$INSTALL_SD_SCRIPT_NAME" "$INSTALL_SD_SCRIPT_LOCATION/$INSTALL_SD_SCRIPT_NAME" curl --show-error --fail --silent --output "$INSTALL_SD_SCRIPT_NAME" "$INSTALL_SD_SCRIPT_LOCATION/$INSTALL_SD_SCRIPT_NAME"
check_integrity "$INSTALL_SD_SCRIPT_SHA256SUM" "$INSTALL_SD_SCRIPT_NAME" check_integrity "$INSTALL_SD_SCRIPT_SHA256SUM" "$INSTALL_SD_SCRIPT_NAME"
chmod u+x "$INSTALL_SD_SCRIPT_NAME" chmod u+x "$INSTALL_SD_SCRIPT_NAME"
cd ..
popd > /dev/null
} }
#================================================= #=================================================
...@@ -430,7 +436,7 @@ function check_custom_script() { ...@@ -430,7 +436,7 @@ function check_custom_script() {
function get_hypercube_setting() { function get_hypercube_setting() {
local key="$1" local key="$1"
local value=$(jq -r ".$key" "$HYPERCUBE_FILE") local value=$(jq -r ".$key" "$HYPERCUBE_LOCATION")
if [[ $key =~ pass(word|phrase) ]] if [[ $key =~ pass(word|phrase) ]]
then then
...@@ -513,7 +519,7 @@ function show_hypercube_settings() { ...@@ -513,7 +519,7 @@ function show_hypercube_settings() {
function create_hypercube_file() { function create_hypercube_file() {
debug $"Checking if $HYPERCUBE_FILE already exists..." debug $"Checking if $HYPERCUBE_FILE already exists..."
if [[ -f $HYPERCUBE_FILE ]] if [[ -f $HYPERCUBE_LOCATION ]]
then then
info $"Found $HYPERCUBE_FILE!" info $"Found $HYPERCUBE_FILE!"
show_hypercube_settings | column -t -s ':' show_hypercube_settings | column -t -s ':'
...@@ -703,7 +709,7 @@ EOF ...@@ -703,7 +709,7 @@ EOF
do do
json_args+=(--arg $json_arg "${!json_arg}") json_args+=(--arg $json_arg "${!json_arg}")
done done
jq "${json_args[@]}" "$hypercube_template" > "$HYPERCUBE_FILE" jq "${json_args[@]}" "$hypercube_template" > "$HYPERCUBE_LOCATION"
info $"$HYPERCUBE_FILE created!" info $"$HYPERCUBE_FILE created!"
} }
...@@ -724,13 +730,13 @@ function get_image() { ...@@ -724,13 +730,13 @@ function get_image() {
fi fi
done done
mkdir -p "$CUBE_RESOURCES_LOCATION"
cd "$CUBE_RESOURCES_LOCATION"
download_image download_image
cd ..
} }
function download_image() { function download_image() {
mkdir -p "$CUBE_RESOURCES_LOCATION"
pushd "$CUBE_RESOURCES_LOCATION" > /dev/null
debug $"Finding latest Yunohost $board image..." debug $"Finding latest Yunohost $board image..."
ynh_image_zip="$(curl --show-error --fail --silent $YNH_IMG_LOCATION \ ynh_image_zip="$(curl --show-error --fail --silent $YNH_IMG_LOCATION \
...@@ -780,13 +786,15 @@ function download_image() { ...@@ -780,13 +786,15 @@ function download_image() {
fi fi
debug $"Building internetcube image..." debug $"Building internetcube image..."
chmod u+x $CUBE_BUILD_SCRIPT_DIR
check_sudo check_sudo
sudo bash $CUBE_BUILD_SCRIPT_NAME "$ynh_image" sudo chmod u+x $CUBE_BUILD_SCRIPT_NAME
"./$CUBE_BUILD_SCRIPT_NAME" "$ynh_image"
rm -rf $CUBE_BUILD_SCRIPT_DIR rm -rf $CUBE_BUILD_SCRIPT_DIR
fi fi
info $"We've got our internetcube image!" info $"We've got our internetcube image!"
popd > /dev/null
} }
function flash_sd_card() { function flash_sd_card() {
...@@ -796,9 +804,7 @@ function flash_sd_card() { ...@@ -796,9 +804,7 @@ function flash_sd_card() {
download_install_sd_script download_install_sd_script
fi fi
create_custom_script local install_sd_options=(-y "$HYPERCUBE_FILE" -f "$CUBE_RESOURCES_LOCATION/$image")
local install_sd_options=(-y "$HYPERCUBE_FILE" -f $CUBE_RESOURCES_LOCATION/$image)
if $INSTALL_SD_CUSTOM_SCRIPT_ENABLED if $INSTALL_SD_CUSTOM_SCRIPT_ENABLED
then then
...@@ -814,21 +820,7 @@ function flash_sd_card() { ...@@ -814,21 +820,7 @@ function flash_sd_card() {
install_sd_options+=(-d) install_sd_options+=(-d)
fi fi
"./$CUBE_RESOURCES_LOCATION/$INSTALL_SD_SCRIPT_NAME" "${install_sd_options[@]}" "$CUBE_RESOURCES_LOCATION/$INSTALL_SD_SCRIPT_NAME" "${install_sd_options[@]}"
}
function create_custom_script() {
cat << "EOF" > $CUSTOM_SCRIPT_PATH
#!/bin/bash
# Adding neutrinet specifics
main_domain=$(yunohost tools maindomain --output=plain)
yunohost app fetchlist -n neutrinet -u https://neutrinet.be/apps.json --debug
yunohost app fetchlist --debug
yunohost app install neutrinet --force --debug --args "domain=${main_domain}&path=/neutrinet"
EOF
} }
function create_custom_script() { function create_custom_script() {
...@@ -880,7 +872,7 @@ function search_cube() { ...@@ -880,7 +872,7 @@ function search_cube() {
keep_searching=true keep_searching=true
while $keep_searching while $keep_searching
do do
"./$CUBE_RESOURCES_LOCATION/$INSTALL_SD_SCRIPT_NAME" -l || true "$CUBE_RESOURCES_LOCATION/$INSTALL_SD_SCRIPT_NAME" -l || true
info $"If we found it you should be able to follow the progress at the HyperCube Debug url" info $"If we found it you should be able to follow the progress at the HyperCube Debug url"
if ! confirm $"Do you want to continue searching?" "y" if ! confirm $"Do you want to continue searching?" "y"
...@@ -895,7 +887,7 @@ function search_cube() { ...@@ -895,7 +887,7 @@ function search_cube() {
function cleanup_hypercube_file() { function cleanup_hypercube_file() {
local default_answer=$1 local default_answer=$1
if [[ ! -e $HYPERCUBE_FILE ]] if [[ ! -e $HYPERCUBE_LOCATION ]]
then then
return return
fi fi
...@@ -908,7 +900,7 @@ function cleanup_hypercube_file() { ...@@ -908,7 +900,7 @@ function cleanup_hypercube_file() {
if confirm $"Do you want to remove the $HYPERCUBE_FILE file?" $default_answer if confirm $"Do you want to remove the $HYPERCUBE_FILE file?" $default_answer
then then
info $"Removing $HYPERCUBE_FILE..." info $"Removing $HYPERCUBE_FILE..."
rm -f "$HYPERCUBE_FILE" rm -f "$HYPERCUBE_LOCATION"
fi fi
} }
...@@ -921,10 +913,10 @@ function cleanup_cube_resources() { ...@@ -921,10 +913,10 @@ function cleanup_cube_resources() {
fi fi
echo echo
info $"We stored some resources (Yunohost images, scripts, etc.) in the $CUBE_RESOURCES_LOCATION folder" info $"We stored some resources (Yunohost images, scripts, etc.) in the $CUBE_RESOURCES_DIR folder"
info $"These resources could be reused for another cube install, so you wouldn't need to download everything again" info $"These resources could be reused for another cube install, so you wouldn't need to download everything again"
if confirm $"Do you want to remove the $CUBE_RESOURCES_LOCATION folder?" $default_answer if confirm $"Do you want to remove the $CUBE_RESOURCES_DIR folder?" $default_answer
then then
info $"Removing cube resources..." info $"Removing cube resources..."
rm -rf "$CUBE_RESOURCES_LOCATION" rm -rf "$CUBE_RESOURCES_LOCATION"
...@@ -943,6 +935,11 @@ function cleanup_resources_on_abort() { ...@@ -943,6 +935,11 @@ function cleanup_resources_on_abort() {
# In case we want to exit the trap function with CTRL+C # In case we want to exit the trap function with CTRL+C
trap - ERR INT trap - ERR INT
# Clear terminal state in order to display user inputs
# This is needed when user input was previously hidden (e.g. prompt for password, etc.)
# See https://unix.stackexchange.com/questions/499471/reset-terminal-after-interrupting-read-r-s
stty sane
echo echo
error $"Installation aborted" error $"Installation aborted"
...@@ -985,9 +982,7 @@ function show_help() { ...@@ -985,9 +982,7 @@ function show_help() {
} }
function prepare() { function prepare() {
info $"Setting up the $CUBE_RESOURCES_LOCATION folder..." info $"Setting up the $CUBE_RESOURCES_DIR folder..."
mkdir -p "$CUBE_RESOURCES_LOCATION"
cd $CUBE_RESOURCES_LOCATION
for board in lime lime2 for board in lime lime2
do do
...@@ -995,7 +990,6 @@ function prepare() { ...@@ -995,7 +990,6 @@ function prepare() {
download_image download_image
done done
cd ..
download_install_sd_script download_install_sd_script
} }
...@@ -1027,6 +1021,8 @@ done ...@@ -1027,6 +1021,8 @@ done
# SCRIPT # SCRIPT
#================================================= #=================================================
set -E
# Cleanup if any of the following command fails or if we do Ctrl+C # Cleanup if any of the following command fails or if we do Ctrl+C
trap "cleanup_resources_on_abort" ERR INT trap "cleanup_resources_on_abort" ERR INT
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment