@ -14,17 +14,23 @@
# ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.#
# shellcheck disable=SC2148
# shellcheck disable=SC2155
#=======================================
# CONFIG
#=======================================
REQUIRED_NODE_VERSION = 18.0.0
LOGTO = /dev/null
LOGTO = ./log.txt
ENV_FILE_ADMIN = /srv/nightscout/config/admin.env
ENV_FILE_NS = /srv/nightscout/config/nightscout.env
ENV_FILE_DEP = /srv/nightscout/config/deployment.env
DOCKER_COMPOSE_FILE = /srv/nightscout/config/docker-compose.yml
MONGO_DB_DIR = /srv/nightscout/data/mongodb
TOOL_FILE = /srv/nightscout/tools/nightscout-tool
TOOL_LINK = /usr/bin/nightscout-tool
UPDATES_DIR = /srv/nightscout/updates
#=======================================
# SETUP
@ -101,7 +107,7 @@ else
tty_escape( ) { :; }
fi
tty_mkbold( ) { tty_escape " 1; $1 " ; }
tty_underline = " $( tty_escape "4;39" ) "
# tty_underline="$(tty_escape "4;39") "
tty_blue = " $( tty_mkbold 34) "
tty_red = " $( tty_mkbold 31) "
tty_bold = " $( tty_mkbold 39) "
@ -111,7 +117,6 @@ tty_reset="$(tty_escape 0)"
# EMOJIS
#=======================================
emoji_unicorn = "\U1F984"
emoji_check = "\U2705"
emoji_ok = "\U1F197"
@ -153,10 +158,12 @@ ohai() {
}
msgok( ) {
# shellcheck disable=SC2059
printf " $emoji_ok $1 \n "
}
msgcheck( ) {
# shellcheck disable=SC2059
printf " $emoji_check $1 \n "
}
@ -187,7 +194,7 @@ version_lt() {
[ [ " ${ 1 %.* } " -lt " ${ 2 %.* } " ] ] || [ [ " ${ 1 %.* } " -eq " ${ 2 %.* } " && " ${ 1 #*. } " -lt " ${ 2 #*. } " ] ]
}
if: IsSet( ) {
ifIsSet( ) {
[ [ ${ !1-x } = = x ] ] && return 1 || return 0
}
@ -209,34 +216,34 @@ echo_progress() {
local firstPhaseSecs = $5 # how long first, ticked part, last
if [ " $realProg " -eq "0" ] ; then
local progrsec = $(( ( $ countr * $ realStart) / ( 3 * $ firstPhaseSecs) ))
if [ $progrsec -lt $realStart ] ; then
local progrsec = $(( ( countr * realStart) / ( 3 * firstPhaseSecs)) )
if [ $progrsec -lt " $realStart " ] ; then
echo $progrsec
else
echo $realStart
echo " $realStart "
fi
else
echo $(( ( $realProg * ( 100 - $realStart ) / $realMax ) + $realStart ))
echo $(( ( realProg * ( 100 - realStart) / realMax) + realStart))
fi
}
process_gauge( ) {
local process_to_measure = $1
local message = $3
local lenmsg = $( echo " $4 " | wc -l)
eval $process_to_measure &
local lenmsg
lenmsg = $( echo " $4 " | wc -l)
eval " $process_to_measure " &
local thepid = $!
local num = 1
while true; do
echo 0
while kill -0 " $thepid " >/dev/null 2>& 1; do
eval $2 $num
eval " $2 " $num
num = $(( num + 1 ))
sleep 0.3
done
echo 100
break
done | whiptail --title " $3 " --gauge " \n $4 \n " $(( $ lenmsg + 6 )) 70 0
done | whiptail --title " $3 " --gauge " \n $4 \n " $(( lenmsg + 6 )) 70 0
}
download_if_not_exists( ) {
@ -244,7 +251,7 @@ download_if_not_exists() {
msgok " Found $1 "
else
ohai " Downloading $1 ... "
curl -fsSL -o $2 $3
curl -fsSL -o " $2 " " $3 "
msgcheck " Downloaded $1 "
fi
}
@ -255,8 +262,9 @@ download_if_not_exists() {
packages = ( )
aptGetWasUpdated = 0
serverName = $( hostname)
apiKey = ""
MIKRUS_APIKEY = ''
MIKRUS_HOST = ''
#=======================================
# ACTIONS AND STEPS
@ -266,7 +274,7 @@ setup_update_repo() {
if [ " $aptGetWasUpdated " -eq "0" ] ; then
aptGetWasUpdated = 1
ohai "Updating package repository"
apt-get -yq update >/dev/null 2>& 1
apt-get -yq update >>$LOGTO 2>& 1
fi
}
@ -293,15 +301,10 @@ add_if_not_ok_cmd() {
msgcheck " $1 installed "
else
ohai " Installing $1 ... "
eval $2 >/dev/null 2>& 1 && msgcheck " Installing $1 successfull "
eval " $2 " >>$LOGTO 2>& 1 && msgcheck " Installing $1 successfull "
fi
}
check_tig( ) {
tig -v >/dev/null 2>& 1
add_if_not_ok "Tig" "tig"
}
check_git( ) {
git --version >/dev/null 2>& 1
add_if_not_ok "GIT" "git"
@ -328,7 +331,9 @@ check_dotenv() {
}
setup_packages( ) {
if :IsSet packages && setup_update_repo && ohai " Installing packages: ${ packages [@] } " && apt-get -yq install ${ packages [@] } >/dev/null 2>& 1 && msgcheck "Install successfull" || msgok "All required packages already installed"
# shellcheck disable=SC2145
# shellcheck disable=SC2068
( ifIsSet packages && setup_update_repo && ohai " Installing packages: ${ packages [@] } " && apt-get -yq install ${ packages [@] } >>$LOGTO 2>& 1 && msgcheck "Install successfull" ) || msgok "All required packages already installed"
}
setup_node( ) {
@ -341,7 +346,7 @@ setup_node() {
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - >/dev/null 2>& 1
ohai "Installing Node.js"
apt-get install -y nodejs >/dev/null 2>& 1
apt-get install -y nodejs >>$LOGTO 2>& 1
fi
}
@ -360,49 +365,49 @@ setup_dir_structure() {
ohai "Configuring folder structure"
mkdir -p $MONGO_DB_DIR
mkdir -p /srv/nightscout/config
mkdir -p /srv/nightscout/tools
mkdir -p $UPDATES_DIR
chown -R mongodb:root $MONGO_DB_DIR
}
get_docker_status( ) {
local ID = $( docker ps -a --no-trunc --filter name = " ^ $1 " --format '{{ .ID }}' )
if [ [ " $ID " = ~ [ 0-9a-fA-F] { 12,} ] ] ; then
echo $( docker inspect $ID | jq -r ".[0].State.Status" )
docker inspect " $ID " | jq -r ".[0].State.Status"
else
echo 'missing'
fi
}
install_containers( ) {
docker-compose --env-file /srv/nightscout/config/deployment.env -f /srv/nightscout/config/docker-compose.yml up -d >/dev/null 2>& 1
docker-compose --env-file /srv/nightscout/config/deployment.env -f /srv/nightscout/config/docker-compose.yml up -d >>$LOGTO 2>& 1
}
install_containers_progress( ) {
local created = $( docker container ls -f 'status=created' -f name = ns-server -f name = ns-database | wc -l)
local current = $( docker container ls -f 'status=running' -f name = ns-server -f name = ns-database | wc -l)
local progr = $(( ( $current - 1 ) * 2 + ( $created - 1 ) ) )
echo_progress $progr 6 50 $1 60
local progr = $(( ( current - 1 ) * 2 + ( created - 1 )) )
echo_progress $progr 6 50 " $1 " 60
}
uninstall_containers( ) {
docker-compose --env-file /srv/nightscout/config/deployment.env -f /srv/nightscout/config/docker-compose.yml down >/dev/null 2>& 1
docker-compose --env-file /srv/nightscout/config/deployment.env -f /srv/nightscout/config/docker-compose.yml down >>$LOGTO 2>& 1
}
uninstall_containers_progress( ) {
local running = $( docker container ls -f 'status=running' -f name = ns-server -f name = ns-database -f name = ns-backup | wc -l)
local current = $( docker container ls -f 'status=exited' -f name = ns-server -f name = ns-database -f name = ns-backup | wc -l)
local progr = $(( $current - 1 ))
if [ " $(( ( $running - 1 ) + ( $current - 1 ) ) )" -eq "0" ] ; then
echo_progress 3 3 50 $1 15
local progr = $(( current - 1 ))
if [ " $(( ( running - 1 ) + ( current - 1 )) )" -eq "0" ] ; then
echo_progress 3 3 50 " $1 " 15
else
echo_progress $progr 3 50 $1 15
echo_progress $progr 3 50 " $1 " 15
fi
}
MIKRUS_APIKEY = ''
MIKRUS_HOST = ''
source_admin( ) {
if [ [ -f $ENV_FILE_ADMIN ] ] ; then
# shellcheck disable=SC1090
source $ENV_FILE_ADMIN
msgok "Imported admin config"
fi
@ -414,6 +419,22 @@ download_conf() {
download_if_not_exists "docker compose file" $DOCKER_COMPOSE_FILE https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/docker-compose.yml
}
download_tools( ) {
if ! [ [ -f $TOOL_FILE ] ] ; then
download_if_not_exists "nightscout-tool file" $TOOL_FILE https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/install.sh
local timestamp = $( date +%s)
echo " $timestamp " >" $UPDATES_DIR /timestamp "
fi
if ! [ [ -f $TOOL_LINK ] ] ; then
ohai "Linking nightscout-tool"
ln -s " $TOOL_FILE " " $TOOL_LINK "
fi
chmod +x $TOOL_FILE
chmod +x $TOOL_LINK
}
prompt_welcome( ) {
whiptail --title "Witamy" --yesno "Ten skrypt zainstaluje Nightscout na bieżącym serwerze mikr.us\n\nJeśli na tym serwerze jest już Nightscout \n- ten skrypt umożliwia jego aktualizację oraz diagnostykę." --yes-button " $uni_start " --no-button " $uni_exit " 12 70
exit_on_no_cancel
@ -421,16 +442,16 @@ prompt_welcome() {
prompt_mikrus_host( ) {
if ! [ [ " $MIKRUS_HOST " = ~ [ a-z] [ 0-9] { 3} ] ] ; then
MIKRUS_HOST = ` hostname`
MIKRUS_HOST = $( hostname)
while :; do
if [ [ " $MIKRUS_HOST " = ~ [ a-z] [ 0-9] { 3} ] ] ; then
break;
break
else
MIKRUS_NEW_HOST = $( whiptail --title "Podaj identyfikator serwera" --inputbox "\nNie udało się wykryć identyfikatora serwera,\npodaj go poniżej ręcznie.\n\nIdentyfikator składa się z jednej litery i trzech cyfr\n" --cancel-button "Anuluj" 13 65 3>& 1 1>& 2 2>& 3)
exit_on_no_cancel
if [ [ " $MIKRUS_NEW_HOST " = ~ [ a-z] [ 0-9] { 3} ] ] ; then
MIKRUS_HOST = $MIKRUS_NEW_HOST
break;
break
else
whiptail --title " $uni_excl Nieprawidłowy identyfikator serwera $uni_excl " --yesno "Podany identyfikator serwera ma nieprawidłowy format.\n\nChcesz podać go ponownie?" --yes-button " $uni_reenter " --no-button " $uni_exit " 12 70
exit_on_no_cancel
@ -493,13 +514,12 @@ prompt_api_secret() {
while :; do
API_SECRET = $( whiptail --title "Podaj API SECRET" --inputbox " \nWpisz API SECRET do serwera Nightscout:\n ${ uni_bullet } Upewnij się że masz go zapisanego np.: w managerze haseł\n ${ uni_bullet } Użyj conajmniej 12 znaków: małych i dużych liter i cyfr\n\n " --cancel-button "Anuluj" 12 75 3>& 1 1>& 2 2>& 3)
if [ $? -eq 1 ] ; then
break;
break
fi
if [ [ " $API_SECRET " = ~ [ a-zA-Z0-9%+= ./:= @_] { 12,} ] ] ; then
break;
break
else
whiptail --title " $uni_excl Nieprawidłowy API SECRET $uni_excl " --yesno "Podany API SECRET ma nieprawidłowy format.\nChcesz podać go ponownie?" --yes-button " $uni_reenter " --no-button " $uni_noenter " 10 73
if [ $? -eq 1 ] ; then
@ -556,7 +576,7 @@ admin_panel_promo() {
get_container_status( ) {
local ID = $( docker ps -a --no-trunc --filter name = " ^ $1 $" --format '{{ .ID }}' )
if [ [ " $ID " = ~ [ 0-9a-fA-F] { 12,} ] ] ; then
local status = $( docker inspect $ID | jq -r ".[0].State.Status" )
local status = $( docker inspect " $ID " | jq -r ".[0].State.Status" )
case " $status " in
"running" )
printf "\U1F7E2 działa"
@ -585,8 +605,8 @@ get_container_status() {
}
show_logs( ) {
local col = $(( $ COLUMNS - 10 ))
local rws = $(( $ LINES - 3 ))
local col = $(( COLUMNS - 10 ))
local rws = $(( LINES - 3 ))
if [ $col -gt 120 ] ; then
col = 160
fi
@ -598,11 +618,11 @@ show_logs() {
fi
local ID = $( docker ps -a --no-trunc --filter name = " ^ $1 $" --format '{{ .ID }}' )
if ! [ -z $ID ] ; then
if [ -n " $ID " ] ; then
local tmpfile = $( mktemp)
local logs = $( docker logs $ID 2>& 1 | tail $(( $ rws * - 6 )) | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' > $tmpfile )
whiptail --title " Logi $2 " --scrolltext --textbox $tmpfile $rws $col
rm $tmpfile
docker logs " $ID " 2>& 1 | tail $(( rws * - 6 )) | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' >" $tmpfile "
whiptail --title " Logi $2 " --scrolltext --textbox " $tmpfile " $rws $col
rm " $tmpfile "
fi
}
@ -660,7 +680,7 @@ uninstall_menu() {
if ! [ $? -eq 1 ] ; then
docker_compose_down
dialog --title " Czyszczenie bazy danych " --infobox "\n Usuwanie plików bazy\n ... Proszę czekać ..." 6 32
rm -r $ MONGO_DB_DIR/*
rm -r " ${ MONGO_DB_DIR: ? } /*"
docker_compose_up
fi
; ;
@ -698,10 +718,10 @@ main_menu() {
"3)" )
ohai "Updating package list"
dialog --title " Aktualizacja systemu " --infobox "\n Pobieranie listy pakietów\n ..... Proszę czekać ....." 6 33
apt-get -yq update >/dev/null 2>& 1
apt-get -yq update >>$LOGTO 2>& 1
ohai "Upgrading system"
dialog --title " Aktualizacja systemu " --infobox "\n Instalowanie pakietów\n ... Proszę czekać ..." 6 33
apt-get -yq upgrade >/dev/null 2>& 1
apt-get -yq upgrade >>$LOGTO 2>& 1
; ;
"4)" )
docker_compose_down
@ -727,7 +747,6 @@ setup_done() {
}
#=======================================
# MAIN SCRIPT
#=======================================
@ -742,6 +761,7 @@ check_dotenv
setup_users
setup_dir_structure
download_conf
download_tools
source_admin
@ -761,4 +781,3 @@ else
msgok "Wykryto uruchomiony Nightscout"
main_menu
fi