freedomboneeee/src/freedombone-utils-selector

437 lines
12 KiB
Plaintext
Raw Normal View History

2016-07-06 14:21:43 +02:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# Functions for selecting which apps to install or remove
#
# License
# =======
#
# Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Array containing names of available apps
APPS_AVAILABLE=()
# Array containing 1 or 0 indicating installed apps
APPS_INSTALLED=()
# Apps selected with checklist
APPS_CHOSEN=()
2016-07-09 19:02:33 +02:00
# A list of the names of installed apps
APPS_INSTALLED_NAMES=()
# file containing a list of removed apps
REMOVED_APPS_FILE=/root/removed
2016-10-05 23:33:41 +02:00
# Loads variables defined at the beginning of an app script
function app_load_variables {
app_name=$1
var_name=${app_name}_variables
2016-10-09 12:02:11 +02:00
if [ ! ${!var_name} ]; then
2016-10-05 23:33:41 +02:00
return
fi
2016-10-09 12:02:11 +02:00
varname=${!var_name}
for v in "${var_name[@]}"
2016-10-05 23:33:41 +02:00
do
read_config_param $v
done
}
# Saves variables for a given app script
function app_save_variables {
app_name=$1
var_name=${app_name}_variables
2016-10-09 12:02:11 +02:00
if [ ! ${!var_name} ]; then
2016-10-05 23:33:41 +02:00
return
fi
2016-10-09 12:02:11 +02:00
varname=${!var_name}
for v in "${varname[@]}"
2016-10-05 23:33:41 +02:00
do
2016-10-09 12:02:11 +02:00
save_config_param $v "${!v}"
2016-10-05 23:33:41 +02:00
done
}
2016-10-01 10:42:20 +02:00
# gets the variants list from an app script
function app_variants {
filename=$1
2016-10-08 23:00:53 +02:00
variants_line=$(cat ${filename} | grep 'VARIANTS=')
if [[ "$variants_line" == *"'"* ]]; then
variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F "'" '{print $2}')
else
variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
fi
echo "$variants_list"
}
2016-10-01 10:42:20 +02:00
# whether a given item is in an array
2016-07-06 14:21:43 +02:00
function item_in_array {
2016-09-28 22:26:36 +02:00
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
2016-07-06 14:21:43 +02:00
}
2016-10-08 19:21:43 +02:00
# returns a list of available system variants
# based upon the variants string in each app script
function available_system_variants {
FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
2016-10-09 23:17:33 +02:00
new_available_variants_list=()
2016-10-08 19:21:43 +02:00
for filename in $FILES
do
2016-10-08 23:00:53 +02:00
system_variants_list=$(app_variants $filename)
variants_array=($system_variants_list)
for variant_str in "${variants_array[@]}"
2016-10-08 19:21:43 +02:00
do
2016-10-09 23:17:33 +02:00
item_in_array "${variant_str}" "${new_available_variants_list[@]}"
2016-10-08 23:00:53 +02:00
if [[ $? != 0 ]]; then
2016-10-09 23:17:33 +02:00
new_available_variants_list+=("$variant_str")
2016-10-08 19:21:43 +02:00
fi
done
done
2016-10-09 23:17:33 +02:00
available_variants_list=($(sort <<<"${new_available_variants_list[*]}"))
2016-10-08 19:21:43 +02:00
}
2016-10-08 20:32:04 +02:00
function is_valid_variant {
sys_type="$1"
available_variants_list=()
available_system_variants
for variant_str in "${available_variants_list[@]}"
do
if [[ "$sys_type" == "$variant_str" ]]; then
return "1"
fi
done
return "0"
}
2016-10-08 20:55:21 +02:00
function show_available_variants {
available_variants_list=()
available_system_variants
for variant_str in "${available_variants_list[@]}"
do
echo " $variant_str"
done
}
2016-10-01 10:42:20 +02:00
# mark a given app as having been removed so that it doesn't get reinstalled on updates
function remove_app {
app_name=$1
if [ ! -f $REMOVED_APPS_FILE ]; then
touch $REMOVED_APPS_FILE
fi
2016-10-01 11:08:36 +02:00
if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
echo "_${app_name}_" >> $REMOVED_APPS_FILE
fi
}
2016-10-01 10:42:20 +02:00
# returns 1 if an app has been marked as removed
function app_is_removed {
app_name="$1"
if [ ! -f $REMOVED_APPS_FILE ]; then
echo "0"
return
fi
2016-10-01 11:08:36 +02:00
if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
echo "0"
else
echo "1"
fi
}
2016-10-01 10:42:20 +02:00
# Allows an app to be reinstalled even if it was previously marked as being removed
function reinstall_app {
app_name=$1
if [ ! -f $REMOVED_APPS_FILE ]; then
return
fi
if [[ $(app_is_removed $app_name) == "1" ]]; then
2016-10-01 11:08:36 +02:00
sed -i "/_${app_name}_/d" $REMOVED_APPS_FILE
fi
}
2016-10-01 10:42:20 +02:00
# returns 1 if an app is installed
2016-07-06 14:21:43 +02:00
function app_is_installed {
2016-09-28 22:26:36 +02:00
app_name="$1"
2016-09-30 18:49:24 +02:00
# Why does this secondary file exist, apart from COMPLETION_FILE ?
# It's so that it is visible to unprivileged users from the user control panel
2016-09-30 18:39:02 +02:00
INSTALLED_APPS_LIST=/usr/share/${PROJECT_NAME}/installed.txt
if [ -f $INSTALLED_APPS_LIST ]; then
if ! grep -Fxq "install_${app_name}" $INSTALLED_APPS_LIST; then
2016-09-30 18:30:32 +02:00
echo "0"
else
echo "1"
fi
return
fi
2016-10-01 10:42:20 +02:00
# check the completion file to see if it was installed
2016-09-30 18:39:02 +02:00
if [ ! -f $COMPLETION_FILE ]; then
echo "0"
return
fi
2016-09-28 22:26:36 +02:00
if ! grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
echo "0"
else
echo "1"
fi
2016-07-06 14:21:43 +02:00
}
2016-10-01 10:42:20 +02:00
# called at the end of the install section of an app script
2016-09-30 17:00:45 +02:00
function install_completed {
if [ ! ${1} ]; then
exit 673935
fi
echo "install_${1}" >> $COMPLETION_FILE
}
2016-10-01 10:42:20 +02:00
# populates an array of "0" or "1" for whether apps are installed
2016-07-06 14:21:43 +02:00
function get_apps_installed {
2016-09-28 22:26:36 +02:00
for a in "${APPS_AVAILABLE[@]}"
do
APPS_INSTALLED+=("$(app_is_installed $a)")
done
2016-07-06 14:21:43 +02:00
}
2016-10-01 10:42:20 +02:00
# populates an array of installed app names
2016-07-09 19:02:33 +02:00
function get_apps_installed_names {
2016-09-28 22:26:36 +02:00
APPS_INSTALLED_NAMES=()
for a in "${APPS_AVAILABLE[@]}"
do
if [[ $(app_is_installed $a) == "1" ]]; then
APPS_INSTALLED_NAMES+=("$a")
fi
done
2016-07-09 19:02:33 +02:00
}
2016-10-01 10:42:20 +02:00
# detects what apps are available
2016-07-06 14:21:43 +02:00
function detect_apps {
2016-09-28 22:26:36 +02:00
FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
APPS_AVAILABLE=()
APPS_CHOSEN=()
# for all the app scripts
for filename in $FILES
do
app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
2016-10-08 23:00:53 +02:00
item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
if [[ $? != 0 ]]; then
2016-09-28 22:26:36 +02:00
APPS_AVAILABLE+=("${app_name}")
APPS_CHOSEN+=("0")
fi
done
function_check get_apps_installed
get_apps_installed
get_apps_installed_names
2016-07-06 14:21:43 +02:00
}
2016-10-01 10:42:20 +02:00
# detects what apps are available and can be installed
# If the variants list within an app script is an empty string then
# it is considered to be too experimental to be installable
function detect_installable_apps {
FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
APPS_AVAILABLE=()
APPS_CHOSEN=()
APPS_INSTALLED=()
APPS_INSTALLED_NAMES=()
function_check app_is_installed
# for all the app scripts
for filename in $FILES
do
app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
2016-10-08 23:00:53 +02:00
item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
if [[ $? != 0 ]]; then
variants_list=$(app_variants $filename)
2016-10-01 10:42:20 +02:00
# check for empty string
if [ ${#variants_list} -gt 0 ]; then
APPS_AVAILABLE+=("${app_name}")
APPS_CHOSEN+=("0")
APPS_INSTALLED+=("$(app_is_installed $app_name)")
if [[ $(app_is_installed $app_name) == "1" ]]; then
APPS_INSTALLED_NAMES+=("$app_name")
fi
fi
fi
done
}
2016-07-06 17:47:55 +02:00
# creates the APPS_AVAILABLE and APPS_CHOSEN arrays based on
# the given variant name
function choose_apps_for_variant {
2016-09-28 22:26:36 +02:00
variant_name="$1"
FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
APPS_AVAILABLE=()
APPS_CHOSEN=()
# for all the app scripts
for filename in $FILES
do
app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
2016-10-08 23:00:53 +02:00
item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
if [[ $? != 0 ]]; then
2016-09-28 22:26:36 +02:00
APPS_AVAILABLE+=("${app_name}")
if grep -q "VARIANTS=" ${filename}; then
variants_list=$(app_variants $filename)
2016-09-28 22:56:22 +02:00
if [[ "${variants_list}" == 'all'* || \
2016-09-28 22:26:36 +02:00
"${variants_list}" == "$variant_name "* || \
"${variants_list}" == *" $variant_name "* || \
"${variants_list}" == *" $variant_name" ]]; then
2016-10-01 00:47:08 +02:00
if [[ $(app_is_removed ${a}) == "0" ]]; then
APPS_CHOSEN+=("1")
else
APPS_CHOSEN+=("0")
fi
2016-09-28 22:26:36 +02:00
else
APPS_CHOSEN+=("0")
fi
else
APPS_CHOSEN+=("0")
fi
fi
done
function_check get_apps_installed
get_apps_installed
2016-07-06 17:47:55 +02:00
}
2016-10-01 10:42:20 +02:00
# show a list of apps which have been chosen
2016-07-06 17:47:55 +02:00
function list_chosen_apps {
2016-09-28 22:26:36 +02:00
app_index=0
for a in "${APPS_AVAILABLE[@]}"
do
if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
echo $"${a}"
fi
app_index=$[app_index+1]
done
2016-07-06 17:47:55 +02:00
}
2016-07-06 14:21:43 +02:00
function remove_apps {
2016-09-28 22:26:36 +02:00
app_index=0
for a in "${APPS_AVAILABLE[@]}"
do
if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
echo $"Removing application: ${a}"
2016-10-05 23:33:41 +02:00
app_load_variables ${a}
remove_app ${a}
2016-09-28 22:26:36 +02:00
remove_${a}
echo $"${a} was removed"
fi
fi
app_index=$[app_index+1]
done
2016-09-30 18:30:32 +02:00
update_installed_apps_list
2016-07-06 14:21:43 +02:00
}
function install_apps {
is_interactive=$1
2016-10-01 10:48:57 +02:00
# interactive install configuration for each app
if [ ${is_interactive} ]; then
app_index=0
for a in "${APPS_AVAILABLE[@]}"
do
2016-10-01 10:42:20 +02:00
if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
2016-10-01 10:48:57 +02:00
# interactively obtain settings for this app
if [[ $(function_exists install_interactive_${a}) == "1" ]]; then
install_interactive_${a}
fi
fi
fi
2016-10-01 10:48:57 +02:00
app_index=$[app_index+1]
done
fi
2016-10-01 10:48:57 +02:00
# now install the apps
app_index=0
for a in "${APPS_AVAILABLE[@]}"
do
if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
if [ ${is_interactive} ]; then
2016-10-01 10:48:57 +02:00
# clears any removal indicator
reinstall_app ${a}
2016-10-01 10:48:57 +02:00
2016-10-05 23:33:41 +02:00
app_load_variables ${a}
2016-10-01 18:38:07 +02:00
if [[ $(app_is_installed ${a}) == "1" ]]; then
echo $"Upgrading application from interactive: ${a}"
upgrade_${a}
echo $"${a} was upgraded from interactive"
else
echo $"Installing application from interactive: ${a}"
install_${a}
2016-10-05 23:33:41 +02:00
app_save_variables ${a}
2016-10-01 18:38:07 +02:00
install_completed ${a}
echo $"${a} was installed from interactive"
fi
else
2016-10-01 10:48:57 +02:00
# check if the app was removed
if [[ $(app_is_removed ${a}) == "0" ]]; then
2016-10-05 23:33:41 +02:00
app_load_variables ${a}
2016-10-01 18:38:07 +02:00
if [[ $(app_is_installed ${a}) == "1" ]]; then
echo $"Upgrading application: ${a}"
upgrade_${a}
echo $"${a} was upgraded"
else
echo $"Installing application: ${a}"
install_${a}
install_completed ${a}
echo $"${a} was installed"
fi
else
echo $"${a} has been removed and so will not be reinstalled"
fi
fi
2016-09-28 22:26:36 +02:00
fi
fi
app_index=$[app_index+1]
done
2016-09-30 18:30:32 +02:00
update_installed_apps_list
2016-07-06 14:21:43 +02:00
}
2016-07-06 15:43:20 +02:00
# NOTE: deliberately no exit 0