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 @@
declare -A LOG_LEVELS=([DEBUG]=0 [INFO]=1 [WARN]=2 [ERROR]=3)
LOG_LEVEL=${LOG_LEVELS[INFO]}
CURRENT_SCRIPT_LOCATION="$(realpath $(dirname $0))"
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"
DEBIAN_CODENAME="stretch"
INTERNETCUBE_PREFIX="internetcube"
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_YUNOHOST_KEY="0x360AAF3259A3E6FF"
......@@ -56,6 +59,7 @@ INSTALL_SD_SCRIPT_NAME="install-sd.sh"
INSTALL_SD_CUSTOM_SCRIPT_ENABLED=true
HYPERCUBE_FILE="install.hypercube"
HYPERCUBE_LOCATION="${CURRENT_SCRIPT_LOCATION}/${HYPERCUBE_FILE}"
CUSTOM_SCRIPT_NAME="custom_neutrinet_specifics.sh"
CUSTOM_SCRIPT_PATH="${CUBE_RESOURCES_LOCATION}/$CUSTOM_SCRIPT_NAME"
......@@ -348,14 +352,16 @@ function check_sudo() {
}
function download_install_sd_script() {
pushd $CUBE_RESOURCES_LOCATION > /dev/null
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"
check_integrity "$INSTALL_SD_SCRIPT_SHA256SUM" "$INSTALL_SD_SCRIPT_NAME"
chmod u+x "$INSTALL_SD_SCRIPT_NAME"
cd ..
popd > /dev/null
}
#=================================================
......@@ -430,7 +436,7 @@ function check_custom_script() {
function get_hypercube_setting() {
local key="$1"
local value=$(jq -r ".$key" "$HYPERCUBE_FILE")
local value=$(jq -r ".$key" "$HYPERCUBE_LOCATION")
if [[ $key =~ pass(word|phrase) ]]
then
......@@ -513,7 +519,7 @@ function show_hypercube_settings() {
function create_hypercube_file() {
debug $"Checking if $HYPERCUBE_FILE already exists..."
if [[ -f $HYPERCUBE_FILE ]]
if [[ -f $HYPERCUBE_LOCATION ]]
then
info $"Found $HYPERCUBE_FILE!"
show_hypercube_settings | column -t -s ':'
......@@ -703,7 +709,7 @@ EOF
do
json_args+=(--arg $json_arg "${!json_arg}")
done
jq "${json_args[@]}" "$hypercube_template" > "$HYPERCUBE_FILE"
jq "${json_args[@]}" "$hypercube_template" > "$HYPERCUBE_LOCATION"
info $"$HYPERCUBE_FILE created!"
}
......@@ -724,13 +730,13 @@ function get_image() {
fi
done
mkdir -p "$CUBE_RESOURCES_LOCATION"
cd "$CUBE_RESOURCES_LOCATION"
download_image
cd ..
}
function download_image() {
mkdir -p "$CUBE_RESOURCES_LOCATION"
pushd "$CUBE_RESOURCES_LOCATION" > /dev/null
debug $"Finding latest Yunohost $board image..."
ynh_image_zip="$(curl --show-error --fail --silent $YNH_IMG_LOCATION \
......@@ -780,13 +786,15 @@ function download_image() {
fi
debug $"Building internetcube image..."
chmod u+x $CUBE_BUILD_SCRIPT_DIR
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
fi
info $"We've got our internetcube image!"
popd > /dev/null
}
function flash_sd_card() {
......@@ -796,9 +804,7 @@ function flash_sd_card() {
download_install_sd_script
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
then
......@@ -814,21 +820,7 @@ function flash_sd_card() {
install_sd_options+=(-d)
fi
"./$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
"$CUBE_RESOURCES_LOCATION/$INSTALL_SD_SCRIPT_NAME" "${install_sd_options[@]}"
}
function create_custom_script() {
......@@ -880,7 +872,7 @@ function search_cube() {
keep_searching=true
while $keep_searching
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"
if ! confirm $"Do you want to continue searching?" "y"
......@@ -895,7 +887,7 @@ function search_cube() {
function cleanup_hypercube_file() {
local default_answer=$1
if [[ ! -e $HYPERCUBE_FILE ]]
if [[ ! -e $HYPERCUBE_LOCATION ]]
then
return
fi
......@@ -908,7 +900,7 @@ function cleanup_hypercube_file() {
if confirm $"Do you want to remove the $HYPERCUBE_FILE file?" $default_answer
then
info $"Removing $HYPERCUBE_FILE..."
rm -f "$HYPERCUBE_FILE"
rm -f "$HYPERCUBE_LOCATION"
fi
}
......@@ -921,10 +913,10 @@ function cleanup_cube_resources() {
fi
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"
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
info $"Removing cube resources..."
rm -rf "$CUBE_RESOURCES_LOCATION"
......@@ -943,6 +935,11 @@ function cleanup_resources_on_abort() {
# In case we want to exit the trap function with CTRL+C
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
error $"Installation aborted"
......@@ -985,9 +982,7 @@ function show_help() {
}
function prepare() {
info $"Setting up the $CUBE_RESOURCES_LOCATION folder..."
mkdir -p "$CUBE_RESOURCES_LOCATION"
cd $CUBE_RESOURCES_LOCATION
info $"Setting up the $CUBE_RESOURCES_DIR folder..."
for board in lime lime2
do
......@@ -995,7 +990,6 @@ function prepare() {
download_image
done
cd ..
download_install_sd_script
}
......@@ -1027,6 +1021,8 @@ done
# SCRIPT
#=================================================
set -E
# Cleanup if any of the following command fails or if we do Ctrl+C
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