Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Mar 14, 2024
1 parent 7cc6ca6 commit e313fed
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 52 deletions.
9 changes: 1 addition & 8 deletions lib/rakpios-cli-network-manage
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,4 @@ do_network_main_menu()
esac
fi

}

# if [ $(id -u) -ne 0 ]; then
# printf "Script must be run as root. Try 'sudo ./whiptail_network_config'\n"
# exit 1
# fi

# do_main_menu
}
12 changes: 2 additions & 10 deletions lib/rakpios-cli-service-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ do_choose_service_env() {
service_env_array=()

IFS=$'\n' keys=($(sort -n <<<"${!service_env[@]}"))
unset IFS
for k in "${keys[@]}"
do
value=${service_env[$k]}
Expand Down Expand Up @@ -398,13 +399,4 @@ do_print_post_up_info() {

do_container() {
do_select_service
}

# if [ $(id -u) -ne 0 ]; then
# printf "Script must be run as root. Try 'sudo ./portainer-json-parser'\n"
# exit 1
# fi

#do_container


}
5 changes: 1 addition & 4 deletions lib/rakpios-cli-service-list
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ do_command() {
docker stop "$CONTAINER_NAME" > /dev/null
else
docker compose -p "$CONTAINER_NAME" stop

fi
if [ $? -eq 0 ]; then
# __msg_debug "docker compose stop ran successfully"
Expand Down Expand Up @@ -138,6 +137,4 @@ do_service_menu() {


fi
}

# do_service_menu
}
64 changes: 34 additions & 30 deletions rakpios-cli
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ main() {
SHORT=i,u,d,h,v,s
LONG=install,upgrade,debug,help,version,silent
OPTS=$(getopt --options $SHORT --longoptions $LONG -- "$@")

if [ $? -ne 0 ]; then
usage
exit 0
Expand Down Expand Up @@ -90,32 +89,41 @@ main() {
esac
done

# Check dependencies
do_check_dependencies

# Configure tmux
echo -e "set-option -g mouse on\nbind-key -n C-C confirm-before kill-session" >~/.tmux.conf

if [ "$INSTALL" == "1" ] || [ "$UPGRADE" == "1" ]; then
do_install
exit 0
fi

do_check_dependencies

# start the rakpios-cli if no options is passed to the script
if [[ $OPTIND -eq 1 ]]; then
# shellcheck source=/dev/null
source "${DIR_LIB}/rakpios-cli-network-manage"
# shellcheck source=/dev/null
source "${DIR_LIB}/rakpios-cli-service-deploy"
# shellcheck source=/dev/null
source "${DIR_LIB}/rakpios-cli-service-list"
do_check_for_update
do_rakpios_cli_menu
fi
}

do_check_dependencies() {

# check for necessary tools
# docker will be checked separately
need_cmd curl
need_cmd jq
need_cmd unzip
need_cmd whiptail
need_cmd network-manager
need_cmd tmux
# docker will be checkd separately

}

do_install() {
Expand Down Expand Up @@ -147,7 +155,7 @@ do_install() {
}

do_print_install_location(){
__msg_debug "The rakpios-cli will be installed in the ${DIR_BIN}, ${DIR_LIB} and ${DIR_CNF}"
__msg_debug "The rakpios-cli will be installed in the ${DIR_BIN}, ${DIR_LIB} and ${DIR_CNF} folders"
}

do_print_version(){
Expand Down Expand Up @@ -181,11 +189,11 @@ do_check_directory() {
}

do_fresh_install() {

do_check_dependencies
# print install location
do_clean_up
do_print_install_location
do_check_directory "${DIR_TMP}" "${DIR_LIB}" "${DIR_CNF}" "${DIR_BIN}"
do_check_directory "${DIR_TMP}" "${DIR_CNF}" "${DIR_BIN}" # "${DIR_LIB}" # DIR_LIB is created below

# clone the rakpios-cli repo and move the files
if [[ $UPGRADE -eq 1 ]]; then
Expand All @@ -196,7 +204,7 @@ do_fresh_install() {

if git clone --quiet https://github.com/${org}/${repo}.git "${DIR_TMP}/"; then
__msg_debug "Download completed"
cp -r "${DIR_TMP}/lib/*" "${DIR_LIB}"
cp -r "${DIR_TMP}/lib" "${DIR_LIB}" # overwrites folder (that's why we are not creating it before)
cp "${DIR_TMP}/rakpios-cli" "${DIR_BIN}/rakpios-cli"
git -C "${DIR_TMP}" tag -l --sort=v:refname | tail -1 > "${DIR_CNF}/rakpios-cli.version"
__msg_debug "Tool copy completed"
Expand Down Expand Up @@ -274,13 +282,17 @@ do_rakpios_cli_menu(){
# function to install command if not installed before
need_cmd() {

if ! check_cmd "$1"; then
echo "need '$1' (command not found)"
apt install $1 -y
fi
if [[ $1 == "tmux" ]]; then
echo -e "set-option -g mouse on\nbind-key -n C-C confirm-before kill-session" >~/.tmux.conf
local COMMAND="$1"

if ! check_cmd "$COMMAND"; then
read -rp "Missing dependency: '$COMMAND'. Do you want to install it now? [y/N]: " RESPONSE
if [[ "$RESPONSE" != "y" ]]; then
echo "Quitting..."
exit 0
fi
sudo apt install "$COMMAND" -y
fi

}

# function to check whether command is installed or not
Expand All @@ -294,15 +306,6 @@ check_cmd() {

}

# slient pushd and popd
pushd() {
command pushd "$@" > /dev/null
}

popd() {
command popd "$@" > /dev/null
}

do_get_version_information() {

REMOTE_VERSION_URL_portainer_json="https://raw.githubusercontent.com/RAKWireless/portainer-templates/master/portainer_app_template.json"
Expand All @@ -320,7 +323,7 @@ do_get_version_information() {
local_version_rakpios_cli=$(cat "${DIR_CNF}/rakpios-cli.version")

else
__msg_debug "Can not found Portainer and rakpios-cli configuration files."
__msg_debug "Can not found rakpios-cli or stack configuration files."
CONFIG_FILE=1
fi

Expand All @@ -330,26 +333,27 @@ do_get_version_information() {
do_check_for_update(){

do_get_version_information
VERSION_PATTERN=^v[0-99].[0-99]

if [ $CONFIG_FILE == 1 ] ; then
whiptail --title "Error" --msgbox "Unable to find Portainer and rakpios-cli configuration files. Try reinstall the tool to fix it." 10 78
whiptail --title "Error" --msgbox "Unable to find rakpios-cli or stack configuration files. Try reinstall the tool to fix it." 10 78
return
fi

# Check versions format
VERSION_PATTERN="^v[0-99].[0-99]"
if [[ ! $remote_version_portainer_json =~ $VERSION_PATTERN ]] || [[ ! $local_version_portainer_json =~ $VERSION_PATTERN ]] || [[ ! $remote_version_rakpios_cli =~ $VERSION_PATTERN ]] || [[ ! $local_version_rakpios_cli =~ $VERSION_PATTERN ]]; then
whiptail --title "Error" --msgbox "Updates check failed, please check your network connection, and try again later." 10 78
return
fi

# check updates for rakpios-cli and portainer template version
# Check updates for rakpios-cli and portainer template version
if [[ $remote_version_portainer_json > $local_version_portainer_json ]] || [[ $remote_version_rakpios_cli > $local_version_rakpios_cli ]]; then
whiptail --title "New Update is available" --msgbox "An update for rakpios-cli is available. Run 'rakpios-cli --upgrade' to upgrade." 8 78
whiptail --title "New Update is available" --msgbox "An update for rakpios-cli is available.\nRun 'rakpios-cli --upgrade' to upgrade." 8 78
__msg_debug "An update is available."
fi

}


###################################################################################################


main "$@" || exit 1

0 comments on commit e313fed

Please sign in to comment.