From c9768d2ec4b5dabbb573f6762cedebee83e9da42 Mon Sep 17 00:00:00 2001 From: theCalcaholic <3knoeppl@informatik.uni-hamburg.de> Date: Fri, 8 Feb 2019 18:23:38 +0100 Subject: [PATCH 01/22] library: Optionally execute ncp-app in tmux if set in app cfg --- etc/library.sh | 85 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 18 deletions(-) diff --git a/etc/library.sh b/etc/library.sh index 1b41fe2cc..e47bb6ceb 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -116,28 +116,77 @@ function run_app_unsafe() echo "Running $ncp_app" echo "[ $ncp_app ]" >> $log - # read script - unset configure - source "$script" - - # read cfg parameters - [[ -f "$cfg_file" ]] && { - local cfg="$( cat "$cfg_file" )" - local len="$(jq '.params | length' <<<"$cfg")" - for (( i = 0 ; i < len ; i++ )); do - local var="$(jq -r ".params[$i].id" <<<"$cfg")" - local val="$(jq -r ".params[$i].value" <<<"$cfg")" - eval "$var=$val" - done + # Check if app is already running in termux + tmux has-session -t="$ncp_app" && { + echo "Already running." >> $log + echo "Abort." >> $log + return 1 } + - # run - configure 2>&1 | tee -a $log - local ret="${PIPESTATUS[0]}" - + unset configure + ( + # read cfg parameters + [[ -f "$cfg_file" ]] && { + local cfg="$( cat "$cfg_file" )" + jq -e '.tmux' <<<"$cfg" > /dev/null 2>&1 + use_tmux="$?" + local len="$(jq '.params | length' <<<"$cfg")" + for (( i = 0 ; i < len ; i++ )); do + local var="$(jq -r ".params[$i].id" <<<"$cfg")" + local val="$(jq -r ".params[$i].value" <<<"$cfg")" + eval "export $var=$val" + done + } + + if [[ $use_tmux ]] + then + echo "Running $ncp_app in tmux." | tee $log + + tmux_log_file="$CFGDIR/../ncp-tmux/tmux.${ncp_app}.log" + export LIBDIR="$CFGDIR/../library.sh" + + echo "" > "$tmux_log_file" + #tail -f "$tmux_log_file" & + + tmux new-session -d -s "$ncp_app" "bash -c '( + trap \"echo \$?\" 0 1 2 3 4 6 9 11 15 19 29 + source \"$LIBDIR\" + source \"$script\" + configure 2>&1 | tee -a $log + )' 2>&1 | tee $tmux_log_file" + + (while tmux has-session -t="$ncp_app" 2>&1 > /dev/null + do + sleep 1 + done) & + tail --lines=+0 -f "$tmux_log_file" --pid="$!" + + return "$(tail -n 1 "$tmux_log_file")" + + else + # read script + source "$script" + # run + configure 2>&1 | tee -a $log + fi + local ret="${PIPESTATUS[0]}" + exit $ret + ) echo "" >> $log - return "$ret" + return "$?" +} + +function is_running_in_tmux() +{ + local ncp_app=$1 + if [[ -f "/usr/local/etc/ncp-tmux/${ncp_app}.session" ]] + then + local session_name="$(cat /usr/local/etc/ncp-tmux/${ncp_app}.session)" + return $? + fi + return 1 } function is_active_app() From 0dbc586b0b6467515e547ca385aa3d6348be3235 Mon Sep 17 00:00:00 2001 From: theCalcaholic <3knoeppl@informatik.uni-hamburg.de> Date: Fri, 8 Feb 2019 22:52:15 +0100 Subject: [PATCH 02/22] library: Fix return value not being retrieved from tmux'ed script --- etc/library.sh | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/etc/library.sh b/etc/library.sh index e47bb6ceb..dd4683f4e 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -150,7 +150,7 @@ function run_app_unsafe() #tail -f "$tmux_log_file" & tmux new-session -d -s "$ncp_app" "bash -c '( - trap \"echo \$?\" 0 1 2 3 4 6 9 11 15 19 29 + trap \"echo \$? >> $tmux_log_file\" 0 1 2 3 4 6 9 11 15 19 29 source \"$LIBDIR\" source \"$script\" configure 2>&1 | tee -a $log @@ -162,7 +162,13 @@ function run_app_unsafe() done) & tail --lines=+0 -f "$tmux_log_file" --pid="$!" - return "$(tail -n 1 "$tmux_log_file")" + ret="$(tail -n 1 "$tmux_log_file")" + + if [[ $ret =~ '^[0-9]+$' ]] + then + exit $ret + fi + exit 1 else # read script @@ -178,17 +184,6 @@ function run_app_unsafe() return "$?" } -function is_running_in_tmux() -{ - local ncp_app=$1 - if [[ -f "/usr/local/etc/ncp-tmux/${ncp_app}.session" ]] - then - local session_name="$(cat /usr/local/etc/ncp-tmux/${ncp_app}.session)" - return $? - fi - return 1 -} - function is_active_app() { local ncp_app=$1 From d91a5f5557191a87b099a5baf270eddc0b923344 Mon Sep 17 00:00:00 2001 From: theCalcaholic <3knoeppl@informatik.uni-hamburg.de> Date: Fri, 8 Feb 2019 23:01:11 +0100 Subject: [PATCH 03/22] update/install: Install tmux and dependencies on installation/update --- etc/library.sh | 6 +++--- ncp.sh | 4 ++-- update.sh | 3 ++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/etc/library.sh b/etc/library.sh index dd4683f4e..08e2d04bc 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -116,7 +116,7 @@ function run_app_unsafe() echo "Running $ncp_app" echo "[ $ncp_app ]" >> $log - # Check if app is already running in termux + # Check if app is already running in tmux tmux has-session -t="$ncp_app" && { echo "Already running." >> $log echo "Abort." >> $log @@ -143,8 +143,8 @@ function run_app_unsafe() then echo "Running $ncp_app in tmux." | tee $log - tmux_log_file="$CFGDIR/../ncp-tmux/tmux.${ncp_app}.log" - export LIBDIR="$CFGDIR/../library.sh" + tmux_log_file="$(dirname $CFGDIR)/ncp-tmux/tmux.${ncp_app}.log" + export LIBDIR="$(dirname $CFGDIR)/library.sh" echo "" > "$tmux_log_file" #tail -f "$tmux_log_file" & diff --git a/ncp.sh b/ncp.sh index 71872fa35..ba911c4c0 100644 --- a/ncp.sh +++ b/ncp.sh @@ -22,8 +22,8 @@ install() { # NCP-CONFIG apt-get update - $APTINSTALL git dialog whiptail jq - mkdir -p "$CONFDIR" "$BINDIR" + $APTINSTALL git dialog whiptail jq tmux locales-all + mkdir -p "$CONFDIR" "$BINDIR" "$(dirname $CONFDIR)/ncp-tmux" # include option in raspi-config (only Raspbian) test -f /usr/bin/raspi-config && { diff --git a/update.sh b/update.sh index 761c4c3ac..23985f5bc 100755 --- a/update.sh +++ b/update.sh @@ -46,7 +46,8 @@ pgrep apt &>/dev/null && { echo "apt is currently running. Try again later"; ex # install new dependencies type jq &>/dev/null || { apt-get update - apt-get install -y --no-install-recommends jq + apt-get install -y --no-install-recommends jq tmux locales-all + mkdir -p "$(dirname $CONFDIR)/ncp-tmux" } # migrate to the new cfg format From a3c2edfc8abe294d28276cbe00d44f0d0ac811f3 Mon Sep 17 00:00:00 2001 From: theCalcaholic <3knoeppl@informatik.uni-hamburg.de> Date: Fri, 8 Feb 2019 23:14:32 +0100 Subject: [PATCH 04/22] library: Fix formatting --- etc/library.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/library.sh b/etc/library.sh index 08e2d04bc..0aeeeaa85 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -156,7 +156,7 @@ function run_app_unsafe() configure 2>&1 | tee -a $log )' 2>&1 | tee $tmux_log_file" - (while tmux has-session -t="$ncp_app" 2>&1 > /dev/null + ( while tmux has-session -t="$ncp_app" 2>&1 > /dev/null do sleep 1 done) & From 88ed633a2937b171524cad0c757c880e9d0019f0 Mon Sep 17 00:00:00 2001 From: theCalcaholic <3knoeppl@informatik.uni-hamburg.de> Date: Fri, 8 Feb 2019 23:46:49 +0100 Subject: [PATCH 05/22] library: Fix valid return values from tmux not being accepted --- etc/library.sh | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/etc/library.sh b/etc/library.sh index 0aeeeaa85..79013235b 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -117,7 +117,7 @@ function run_app_unsafe() echo "[ $ncp_app ]" >> $log # Check if app is already running in tmux - tmux has-session -t="$ncp_app" && { + tmux has-session -t="$ncp_app" > /dev/null 2>&1 && { echo "Already running." >> $log echo "Abort." >> $log return 1 @@ -141,30 +141,32 @@ function run_app_unsafe() if [[ $use_tmux ]] then - echo "Running $ncp_app in tmux." | tee $log - + # Run app in tmux tmux_log_file="$(dirname $CFGDIR)/ncp-tmux/tmux.${ncp_app}.log" export LIBDIR="$(dirname $CFGDIR)/library.sh" echo "" > "$tmux_log_file" - #tail -f "$tmux_log_file" & tmux new-session -d -s "$ncp_app" "bash -c '( - trap \"echo \$? >> $tmux_log_file\" 0 1 2 3 4 6 9 11 15 19 29 + trap \"echo \$? >> $tmux_log_file\" 1 2 3 4 6 9 11 15 19 29 source \"$LIBDIR\" source \"$script\" configure 2>&1 | tee -a $log + echo "${PIPESTATUS[0]}" >> $tmux_log_file )' 2>&1 | tee $tmux_log_file" - ( while tmux has-session -t="$ncp_app" 2>&1 > /dev/null + ( while tmux has-session -t="$ncp_app" > /dev/null 2>&1 do sleep 1 done) & + + # Follow log file until tmux session has terminated tail --lines=+0 -f "$tmux_log_file" --pid="$!" ret="$(tail -n 1 "$tmux_log_file")" - - if [[ $ret =~ '^[0-9]+$' ]] + + # Read return value from tmux log file + if [[ $ret =~ ^[0-9]+$ ]] then exit $ret fi @@ -175,13 +177,14 @@ function run_app_unsafe() source "$script" # run configure 2>&1 | tee -a $log + local ret="${PIPESTATUS[0]}" + exit $ret fi - local ret="${PIPESTATUS[0]}" - exit $ret ) + ret="$?" echo "" >> $log - return "$?" + return "$ret" } function is_active_app() From 33f44f8fc55d5ff909651327798ebe87f02a36d6 Mon Sep 17 00:00:00 2001 From: theCalcaholic <3knoeppl@informatik.uni-hamburg.de> Date: Sun, 10 Feb 2019 16:53:48 +0100 Subject: [PATCH 06/22] Separate function for attaching to running ncp app's tmux session. - check if tmux is availabe before attempting to start app int tmux - Attach to tmux session if app is already running --- etc/library.sh | 61 +++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/etc/library.sh b/etc/library.sh index 79013235b..37405bfd2 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -117,10 +117,10 @@ function run_app_unsafe() echo "[ $ncp_app ]" >> $log # Check if app is already running in tmux - tmux has-session -t="$ncp_app" > /dev/null 2>&1 && { - echo "Already running." >> $log - echo "Abort." >> $log - return 1 + which tmux && tmux has-session -t="$ncp_app" > /dev/null 2>&1 && { + echo "Already running. Attaching to output..." | tee $log + attach_to_app $ncp_app + return $? } @@ -139,40 +139,29 @@ function run_app_unsafe() done } - if [[ $use_tmux ]] + if which tmux && [[ $use_tmux == 0 ]] then + echo "Running $ncp_app in tmux..." | tee $log # Run app in tmux - tmux_log_file="$(dirname $CFGDIR)/ncp-tmux/tmux.${ncp_app}.log" - export LIBDIR="$(dirname $CFGDIR)/library.sh" + local tmux_log_file="$(dirname "$CFGDIR")/ncp-tmux/tmux.${ncp_app}.log" + local tmux_status_file="$(dirname "$tmux_log_file")/tmux.${ncp_app}.status" + local LIBDIR="$(dirname $CFGDIR)/library.sh" echo "" > "$tmux_log_file" tmux new-session -d -s "$ncp_app" "bash -c '( - trap \"echo \$? >> $tmux_log_file\" 1 2 3 4 6 9 11 15 19 29 + trap \"echo \$? >> $tmux_status_file\" 1 2 3 4 6 9 11 15 19 29 source \"$LIBDIR\" source \"$script\" configure 2>&1 | tee -a $log - echo "${PIPESTATUS[0]}" >> $tmux_log_file + echo "\${PIPESTATUS[0]}" >> $tmux_status_file )' 2>&1 | tee $tmux_log_file" - ( while tmux has-session -t="$ncp_app" > /dev/null 2>&1 - do - sleep 1 - done) & - - # Follow log file until tmux session has terminated - tail --lines=+0 -f "$tmux_log_file" --pid="$!" - - ret="$(tail -n 1 "$tmux_log_file")" - - # Read return value from tmux log file - if [[ $ret =~ ^[0-9]+$ ]] - then - exit $ret - fi - exit 1 + attach_to_app "$ncp_app" + exit $? else + echo "Running $ncp_app without tmux..." | tee $log # read script source "$script" # run @@ -187,6 +176,28 @@ function run_app_unsafe() return "$ret" } +function attach_to_app() +{ + local tmux_log_file="$(dirname "$CFGDIR")/ncp-tmux/tmux.${ncp_app}.log" + local tmux_status_file="$(dirname "$tmux_log_file")/tmux.${ncp_app}.status" + + (while tmux has-session -t="$ncp_app" > /dev/null 2>&1 + do + sleep 1 + done) & + + # Follow log file until tmux session has terminated + tail --lines=+0 -f "$tmux_log_file" --pid="$!" + + # Read return value from tmux log file + ret="$(tail -n 1 "$tmux_status_file")" + rm "$tmux_log_file" + rm "$tmux_status_file" + + [[ $ret =~ ^[0-9]+$ ]] && return $ret + return 1 +} + function is_active_app() { local ncp_app=$1 From d707807de907074e15af157383c5cb28811d31a8 Mon Sep 17 00:00:00 2001 From: theCalcaholic <3knoeppl@informatik.uni-hamburg.de> Date: Sat, 16 Feb 2019 21:28:03 +0100 Subject: [PATCH 07/22] ncp-launcher.sh: Add parameter to check if an ncp-app is running --- ncp.sh | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ncp.sh b/ncp.sh index ba911c4c0..6da900e9f 100644 --- a/ncp.sh +++ b/ncp.sh @@ -131,8 +131,24 @@ EOF cat > /home/www/ncp-launcher.sh <<'EOF' #!/bin/bash + +check_running=false +for arg in $@ +do + if [[ $arg=="--check-running"]] + then + check_running=true + fi +done + source /usr/local/etc/library.sh -run_app $1 +if [[ $check_running=="true"]] +then + which tmux && tmux has-session -t="$ncp_app" > /dev/null 2>&1 + exit +else + run_app $1 +fi EOF chmod 700 /home/www/ncp-launcher.sh echo "www-data ALL = NOPASSWD: /home/www/ncp-launcher.sh , /sbin/halt, /sbin/reboot" >> /etc/sudoers From 8bf2d852df2d239045f86820e64778c5f896af24 Mon Sep 17 00:00:00 2001 From: theCalcaholic <3knoeppl@informatik.uni-hamburg.de> Date: Sat, 16 Feb 2019 21:28:48 +0100 Subject: [PATCH 08/22] ncp-web: Implement foundation for different server push message types --- ncp-web/index.php | 2 +- ncp-web/js/ncp.js | 124 ++++++++++++++++++++++++++++++++++----- ncp-web/ncp-launcher.php | 69 ++++++++++++++++++---- ncp-web/ncp-output.php | 10 +++- 4 files changed, 172 insertions(+), 33 deletions(-) diff --git a/ncp-web/index.php b/ncp-web/index.php index 509264801..869e1480b 100644 --- a/ncp-web/index.php +++ b/ncp-web/index.php @@ -211,7 +211,7 @@
-
+

__("Nextcloud configuration"); ?>

' ); - box.scrollTop = box.scrollHeight; + + var payload = JSON.parse(e.data); + if( !payload.hasOwnProperty('type') || !payload.hasOwnProperty('value') ) + return; + if( payload.messageType == "log" ) + { + var box_l = $('#' + selectedID + '-details-box'); + var box = box_l[0]; + box_l.ht( box.innerHTML + escapeHTML(payload.value) + '
' ); + box.scrollTop = box.scrollHeight; + } + else if( payload.messageType == "active_app" ) + { + activeId = payload.value; + } + }, false); set_sidebar_click_handlers(); @@ -268,21 +368,13 @@ $(function() var ret = $.parseJSON( result ); if ( ret.token ) $('#csrf-token').set( { value: ret.token } ); - if ( ret.ret ) // means that the process was launched + if ( ! ret.error ) // means that the process was launched { - if ( ret.ret == '0' ) - { - if( ret.ref && ret.ref == 'nc-update' ) - window.location.reload( true ); - reload_sidebar(); - $('.circle-retstatus').set('+icon-green-circle'); - } - else - $('.circle-retstatus').set('-icon-green-circle'); + wait_for_termination($ret.ref); } else // print error from server instead { - $('.details-box').fill(ret.output); + $('.details-box').fill(ret.error); $('.circle-retstatus').set('-icon-green-circle'); } $( 'input' , '#config-box-wrapper' ).set('@disabled', null); diff --git a/ncp-web/ncp-launcher.php b/ncp-web/ncp-launcher.php index 332450188..3f356fdc4 100644 --- a/ncp-web/ncp-launcher.php +++ b/ncp-web/ncp-launcher.php @@ -15,6 +15,12 @@ $l10nDir = "l10n"; ignore_user_abort(true); +function is_app_running($ncp_app) +{ + exec( 'bash -c "sudo /home/www/ncp-launcher.sh --check-running ' . $ncp_app . '"' , $output , $ret ); + return $ret; +} + // // language // @@ -28,7 +34,7 @@ // CSRF check $token = isset($_POST['csrf_token']) ? $_POST['csrf_token'] : ''; if ( empty($token) || !validateCSRFToken($token) ) - exit( '{ "output": "Unauthorized request. Try reloading the page" }' ); + exit( '{ "error": "Unauthorized request. Try reloading the page" }' ); // // launch @@ -36,12 +42,12 @@ if ( $_POST['action'] == "launch" && $_POST['config'] ) { // sanity checks - if ( !$_POST['ref'] ) exit( '{ "output": "Invalid request" }' ); + if ( !$_POST['ref'] ) exit( '{ "error": "Invalid request" }' ); $ncp_app = $_POST['ref']; preg_match( '/^[0-9A-Za-z_-]+$/' , $_POST['ref'] , $matches ) - or exit( '{ "output": "Invalid input" , "token": "' . getCSRFToken() . '" }' ); + or exit( '{ "error": "Invalid input" , "token": "' . getCSRFToken() . '" }' ); // save new config if ( $_POST['config'] != "{}" ) @@ -49,13 +55,13 @@ $cfg_file = $cfg_dir . $ncp_app . '.cfg'; $cfg_str = file_get_contents($cfg_file) - or exit('{ "output": "' . $ncp_app . ' read error" }'); + or exit('{ "error": "' . $ncp_app . ' read error" }'); $cfg = json_decode($cfg_str, true) - or exit('{ "output": "' . $ncp_app . ' read error" }'); + or exit('{ "error": "' . $ncp_app . ' read error" }'); $new_params = json_decode($_POST['config'], true) - or exit('{ "output": "Invalid request" }'); + or exit('{ "error": "Invalid request" }'); foreach ($cfg['params'] as $index => $param) { @@ -66,29 +72,66 @@ // sanitize $val = trim(escapeshellarg($new_params[$id]),"'"); preg_match( '/ /' , $val , $matches ) - and exit( '{ "output": "Invalid parameters" , "token": "' . getCSRFToken() . '" }' ); + and exit( '{ "error": "Invalid parameters" , "token": "' . getCSRFToken() . '" }' ); // save $cfg['params'][$index]['value'] = $val; } $cfg_str = json_encode($cfg) - or exit('{ "output": "' . $ncp_app . ' internal error" }'); + or exit('{ "error": "' . $ncp_app . ' internal error" }'); file_put_contents($cfg_file, $cfg_str) - or exit('{ "output": "' . $ncp_app . ' write error" }'); + or exit('{ "error": "' . $ncp_app . ' write error" }'); } + $is_running = is_app_running($ncp_app); + $tmux_log_file = dirname($cfg_dir) . "/ncp-tmux/tmux.$ncp_app.log"; + $app_output = ""; + if ( $is_running && file_exists($tmux_log_file) ) + $app_output = "App is already running...\n" . file_get_contents($tmux_log_file); + // launch echo '{ "token": "' . getCSRFToken() . '",'; // Get new token echo ' "ref": "' . $ncp_app . '",'; - echo ' "output": "" , '; - echo ' "ret": '; - exec( 'bash -c "sudo /home/www/ncp-launcher.sh ' . $ncp_app . '"' , $output , $ret ); - echo '"' . $ret . '" }'; + echo ' "output": "' . $app_output . '" }'; + + session_write_close(); + exec( 'bash -c "sudo /home/www/ncp-launcher.sh ' . $ncp_app . '" > /dev/null &' , $output , $ret ); } +// +// get_running_status +// +else if ( $_POST['action'] == "get_app_status" ) +{ + + // sanity checks + if ( !$_POST['ref'] ) exit( '{ "error": "Invalid request" }' ); + + $ncp_app = $_POST['ref']; + + preg_match( '/^[0-9A-Za-z_-]+$/' , $_POST['ref'] , $matches ) + or exit( '{ "error": "Invalid input" , "token": "' . getCSRFToken() . '" }' ); + + $tmux_log_file = dirname($cfg_dir) . "/ncp-tmux/tmux.$ncp_app.log"; + $tmux_status_file = dirname($cfg_dir) . "/ncp-tmux/tmux.$ncp_app.log"; + $past_output = ""; + if ( $ret == 0 && file_exists($tmux_log_file) ) + $past_output = file_get_contents(); + + $status = "-1"; + if ( $ret != 0 && file_exists($tmux_status_file) ) + $status = file_get_contents($tmux_status_file); + + // launch + echo '{ "token": "' . getCSRFToken() . '",'; // Get new token + echo ' "ref": "' . $ncp_app . '",'; + echo ' "output": "' . $past_output . '" , '; + echo ' "running": "' . $ret == 0 ? "true" : "false" . '"'; + echo ' "exit: "' . $status . '" }'; +} // // info // diff --git a/ncp-web/ncp-output.php b/ncp-web/ncp-output.php index 704b0a581..8804774a9 100644 --- a/ncp-web/ncp-output.php +++ b/ncp-web/ncp-output.php @@ -21,9 +21,13 @@ */ function sendMsg($id, $msg) { - echo "id: $id" . PHP_EOL; - echo "data: $msg" . PHP_EOL; - echo PHP_EOL; + echo + "{" + . "messageType: 'log'" + . "id: $id," + . "value: $msg" + . "}" + . PHP_EOL; ob_flush(); flush(); } From 8d6299143e3f9f3e9a1415db39a89b429c7ac547 Mon Sep 17 00:00:00 2001 From: theCalcaholic <3knoeppl@informatik.uni-hamburg.de> Date: Sun, 17 Feb 2019 15:30:06 +0100 Subject: [PATCH 09/22] library.sh: Fix tmux logs not being appended to ncp.log --- etc/library.sh | 8 ++++---- ncp.sh | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/etc/library.sh b/etc/library.sh index 37405bfd2..147a18c32 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -118,7 +118,7 @@ function run_app_unsafe() # Check if app is already running in tmux which tmux && tmux has-session -t="$ncp_app" > /dev/null 2>&1 && { - echo "Already running. Attaching to output..." | tee $log + echo "Already running. Attaching to output..." | tee -a $log attach_to_app $ncp_app return $? } @@ -141,7 +141,7 @@ function run_app_unsafe() if which tmux && [[ $use_tmux == 0 ]] then - echo "Running $ncp_app in tmux..." | tee $log + echo "Running $ncp_app in tmux..." | tee -a $log # Run app in tmux local tmux_log_file="$(dirname "$CFGDIR")/ncp-tmux/tmux.${ncp_app}.log" local tmux_status_file="$(dirname "$tmux_log_file")/tmux.${ncp_app}.status" @@ -155,13 +155,13 @@ function run_app_unsafe() source \"$script\" configure 2>&1 | tee -a $log echo "\${PIPESTATUS[0]}" >> $tmux_status_file - )' 2>&1 | tee $tmux_log_file" + )' 2>&1 | tee -a $tmux_log_file" attach_to_app "$ncp_app" exit $? else - echo "Running $ncp_app without tmux..." | tee $log + echo "Running $ncp_app without tmux..." | tee -a $log # read script source "$script" # run diff --git a/ncp.sh b/ncp.sh index 6da900e9f..379b473e0 100644 --- a/ncp.sh +++ b/ncp.sh @@ -135,17 +135,17 @@ EOF check_running=false for arg in $@ do - if [[ $arg=="--check-running"]] + if [[ $arg == "--check-running" ]] then check_running=true fi done source /usr/local/etc/library.sh -if [[ $check_running=="true"]] +if [[ $check_running == "true" ]] then which tmux && tmux has-session -t="$ncp_app" > /dev/null 2>&1 - exit + exit $? else run_app $1 fi From 5664109a717d1b67cada38904fcec4cd9132c251 Mon Sep 17 00:00:00 2001 From: theCalcaholic <3knoeppl@informatik.uni-hamburg.de> Date: Sun, 17 Feb 2019 15:42:17 +0100 Subject: [PATCH 10/22] Move ncp-web refactoring to separate branch This reverts commit 8bf2d852df2d239045f86820e64778c5f896af24. --- ncp-web/index.php | 2 +- ncp-web/js/ncp.js | 124 +++++---------------------------------- ncp-web/ncp-launcher.php | 69 ++++------------------ ncp-web/ncp-output.php | 10 +--- 4 files changed, 33 insertions(+), 172 deletions(-) diff --git a/ncp-web/index.php b/ncp-web/index.php index 869e1480b..509264801 100644 --- a/ncp-web/index.php +++ b/ncp-web/index.php @@ -211,7 +211,7 @@
-
+

__("Nextcloud configuration"); ?>

' ); - box.scrollTop = box.scrollHeight; - } - else if( payload.messageType == "active_app" ) - { - activeId = payload.value; - } - + var box_l = $('#' + selectedID + '-details-box'); + var box = box_l[0]; + box_l.ht( box.innerHTML + escapeHTML(e.data) + '
' ); + box.scrollTop = box.scrollHeight; }, false); set_sidebar_click_handlers(); @@ -368,13 +268,21 @@ $(function() var ret = $.parseJSON( result ); if ( ret.token ) $('#csrf-token').set( { value: ret.token } ); - if ( ! ret.error ) // means that the process was launched + if ( ret.ret ) // means that the process was launched { - wait_for_termination($ret.ref); + if ( ret.ret == '0' ) + { + if( ret.ref && ret.ref == 'nc-update' ) + window.location.reload( true ); + reload_sidebar(); + $('.circle-retstatus').set('+icon-green-circle'); + } + else + $('.circle-retstatus').set('-icon-green-circle'); } else // print error from server instead { - $('.details-box').fill(ret.error); + $('.details-box').fill(ret.output); $('.circle-retstatus').set('-icon-green-circle'); } $( 'input' , '#config-box-wrapper' ).set('@disabled', null); diff --git a/ncp-web/ncp-launcher.php b/ncp-web/ncp-launcher.php index 3f356fdc4..332450188 100644 --- a/ncp-web/ncp-launcher.php +++ b/ncp-web/ncp-launcher.php @@ -15,12 +15,6 @@ $l10nDir = "l10n"; ignore_user_abort(true); -function is_app_running($ncp_app) -{ - exec( 'bash -c "sudo /home/www/ncp-launcher.sh --check-running ' . $ncp_app . '"' , $output , $ret ); - return $ret; -} - // // language // @@ -34,7 +28,7 @@ function is_app_running($ncp_app) // CSRF check $token = isset($_POST['csrf_token']) ? $_POST['csrf_token'] : ''; if ( empty($token) || !validateCSRFToken($token) ) - exit( '{ "error": "Unauthorized request. Try reloading the page" }' ); + exit( '{ "output": "Unauthorized request. Try reloading the page" }' ); // // launch @@ -42,12 +36,12 @@ function is_app_running($ncp_app) if ( $_POST['action'] == "launch" && $_POST['config'] ) { // sanity checks - if ( !$_POST['ref'] ) exit( '{ "error": "Invalid request" }' ); + if ( !$_POST['ref'] ) exit( '{ "output": "Invalid request" }' ); $ncp_app = $_POST['ref']; preg_match( '/^[0-9A-Za-z_-]+$/' , $_POST['ref'] , $matches ) - or exit( '{ "error": "Invalid input" , "token": "' . getCSRFToken() . '" }' ); + or exit( '{ "output": "Invalid input" , "token": "' . getCSRFToken() . '" }' ); // save new config if ( $_POST['config'] != "{}" ) @@ -55,13 +49,13 @@ function is_app_running($ncp_app) $cfg_file = $cfg_dir . $ncp_app . '.cfg'; $cfg_str = file_get_contents($cfg_file) - or exit('{ "error": "' . $ncp_app . ' read error" }'); + or exit('{ "output": "' . $ncp_app . ' read error" }'); $cfg = json_decode($cfg_str, true) - or exit('{ "error": "' . $ncp_app . ' read error" }'); + or exit('{ "output": "' . $ncp_app . ' read error" }'); $new_params = json_decode($_POST['config'], true) - or exit('{ "error": "Invalid request" }'); + or exit('{ "output": "Invalid request" }'); foreach ($cfg['params'] as $index => $param) { @@ -72,66 +66,29 @@ function is_app_running($ncp_app) // sanitize $val = trim(escapeshellarg($new_params[$id]),"'"); preg_match( '/ /' , $val , $matches ) - and exit( '{ "error": "Invalid parameters" , "token": "' . getCSRFToken() . '" }' ); + and exit( '{ "output": "Invalid parameters" , "token": "' . getCSRFToken() . '" }' ); // save $cfg['params'][$index]['value'] = $val; } $cfg_str = json_encode($cfg) - or exit('{ "error": "' . $ncp_app . ' internal error" }'); + or exit('{ "output": "' . $ncp_app . ' internal error" }'); file_put_contents($cfg_file, $cfg_str) - or exit('{ "error": "' . $ncp_app . ' write error" }'); + or exit('{ "output": "' . $ncp_app . ' write error" }'); } - $is_running = is_app_running($ncp_app); - $tmux_log_file = dirname($cfg_dir) . "/ncp-tmux/tmux.$ncp_app.log"; - $app_output = ""; - if ( $is_running && file_exists($tmux_log_file) ) - $app_output = "App is already running...\n" . file_get_contents($tmux_log_file); - // launch echo '{ "token": "' . getCSRFToken() . '",'; // Get new token echo ' "ref": "' . $ncp_app . '",'; + echo ' "output": "" , '; + echo ' "ret": '; - echo ' "output": "' . $app_output . '" }'; - - session_write_close(); - exec( 'bash -c "sudo /home/www/ncp-launcher.sh ' . $ncp_app . '" > /dev/null &' , $output , $ret ); + exec( 'bash -c "sudo /home/www/ncp-launcher.sh ' . $ncp_app . '"' , $output , $ret ); + echo '"' . $ret . '" }'; } -// -// get_running_status -// -else if ( $_POST['action'] == "get_app_status" ) -{ - - // sanity checks - if ( !$_POST['ref'] ) exit( '{ "error": "Invalid request" }' ); - - $ncp_app = $_POST['ref']; - - preg_match( '/^[0-9A-Za-z_-]+$/' , $_POST['ref'] , $matches ) - or exit( '{ "error": "Invalid input" , "token": "' . getCSRFToken() . '" }' ); - - $tmux_log_file = dirname($cfg_dir) . "/ncp-tmux/tmux.$ncp_app.log"; - $tmux_status_file = dirname($cfg_dir) . "/ncp-tmux/tmux.$ncp_app.log"; - $past_output = ""; - if ( $ret == 0 && file_exists($tmux_log_file) ) - $past_output = file_get_contents(); - - $status = "-1"; - if ( $ret != 0 && file_exists($tmux_status_file) ) - $status = file_get_contents($tmux_status_file); - - // launch - echo '{ "token": "' . getCSRFToken() . '",'; // Get new token - echo ' "ref": "' . $ncp_app . '",'; - echo ' "output": "' . $past_output . '" , '; - echo ' "running": "' . $ret == 0 ? "true" : "false" . '"'; - echo ' "exit: "' . $status . '" }'; -} // // info // diff --git a/ncp-web/ncp-output.php b/ncp-web/ncp-output.php index 8804774a9..704b0a581 100644 --- a/ncp-web/ncp-output.php +++ b/ncp-web/ncp-output.php @@ -21,13 +21,9 @@ */ function sendMsg($id, $msg) { - echo - "{" - . "messageType: 'log'" - . "id: $id," - . "value: $msg" - . "}" - . PHP_EOL; + echo "id: $id" . PHP_EOL; + echo "data: $msg" . PHP_EOL; + echo PHP_EOL; ob_flush(); flush(); } From 4fd441d1dcd8b8ce6586abdda3b9dcd9ea653a46 Mon Sep 17 00:00:00 2001 From: theCalcaholic <3knoeppl@informatik.uni-hamburg.de> Date: Sun, 17 Feb 2019 22:02:43 +0100 Subject: [PATCH 11/22] ncp-web: Check if any app is running and attach if that is the case --- .gitignore | 1 + etc/library.sh | 153 ++++++++++++++++++++++++++------------- ncp-web/js/ncp.js | 28 +++++-- ncp-web/ncp-launcher.php | 32 ++++++-- ncp-web/ncp-output.php | 2 +- ncp.sh | 30 ++++---- 6 files changed, 169 insertions(+), 77 deletions(-) diff --git a/.gitignore b/.gitignore index da3509e3f..e5238fb7f 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ ncp-web/wizard.cfg ncp-web/ncp-web.cfg docker-armhf/qemu-arm-static .vagrant/ +.vscode/ diff --git a/etc/library.sh b/etc/library.sh index 147a18c32..e3ae37794 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -10,6 +10,7 @@ CFGDIR=/usr/local/etc/ncp-config.d BINDIR=/usr/local/bin/ncp +LOCK_FILE=/usr/local/etc/ncp.lock function configure_app() { @@ -22,14 +23,16 @@ function configure_app() type dialog &>/dev/null || { echo "please, install dialog for interactive configuration"; return 1; } [[ -f "$cfg_file" ]] || return 0; - local cfg="$( cat "$cfg_file" )" - local len="$(jq '.params | length' <<<"$cfg")" + local cfg len + cfg="$( cat "$cfg_file" )" + len="$(jq '.params | length' <<<"$cfg")" [[ $len -eq 0 ]] && return # read cfg parameters for (( i = 0 ; i < len ; i++ )); do - local var="$(jq -r ".params[$i].id" <<<"$cfg")" - local val="$(jq -r ".params[$i].value" <<<"$cfg")" + local var val + var="$(jq -r ".params[$i].id" <<<"$cfg")" + val="$(jq -r ".params[$i].value" <<<"$cfg")" local vars+=("$var") local vals+=("$val") local idx=$((i+1)) @@ -91,8 +94,9 @@ function configure_app() function run_app() { - local ncp_app=$1 - local script="$(find "$BINDIR" -name $ncp_app.sh)" + local script ncp_app + ncp_app=$1 + script="$(find "$BINDIR" -name "$ncp_app.sh")" [[ -f "$script" ]] || { echo "file $script not found"; return 1; } @@ -102,10 +106,11 @@ function run_app() # receives a script file, no security checks function run_app_unsafe() { - local script=$1 - local ncp_app="$(basename "$script" .sh)" + local script ncp_app + script=$1 + ncp_app="$(basename "$script" .sh)" local cfg_file="$CFGDIR/$ncp_app.cfg" - local log=/var/log/ncp.log + local log=/var/log/ncp/ncp.log [[ -f "$script" ]] || { echo "file $script not found"; return 1; } @@ -114,60 +119,93 @@ function run_app_unsafe() chown root:www-data $log echo "Running $ncp_app" - echo "[ $ncp_app ]" >> $log # Check if app is already running in tmux - which tmux && tmux has-session -t="$ncp_app" > /dev/null 2>&1 && { - echo "Already running. Attaching to output..." | tee -a $log - attach_to_app $ncp_app + running_app=$( [[ -f "$LOCK_FILE" ]] && cat "$LOCK_FILE" || echo "" ) + [[ ! -z $running_app ]] && which tmux > /dev/null && tmux has-session -t="$running_app" > /dev/null 2>&1 && { + #echo "Already running. Attaching to output..." | tee -a $log + + local choice + [[ $ATTACH_TO_RUNNING == "1" ]] && choice="y" + [[ $ATTACH_TO_RUNNING == "0" ]] && choice="n" + question="An app ($running_app) is already running. Do you want to attach to it's output? " + if [[ $choice == "y" ]] || [[ $choice == "n" ]] + then + echo "$question" + echo "Choice: " + else + read -rp "$question" choice + while [[ "$choice" != "y" ]] && [[ "$choice" != "n" ]] + do + echo "choice was '$choice'" + read -rp "Invalid choice. y or n expected." choice + done + fi + + if [[ "$choice" == "y" ]] + then + attach_to_app "$running_app" + fi return $? } - unset configure ( # read cfg parameters [[ -f "$cfg_file" ]] && { - local cfg="$( cat "$cfg_file" )" + local len cfg + cfg="$( cat "$cfg_file" )" jq -e '.tmux' <<<"$cfg" > /dev/null 2>&1 use_tmux="$?" - local len="$(jq '.params | length' <<<"$cfg")" + len="$(jq '.params | length' <<<"$cfg")" for (( i = 0 ; i < len ; i++ )); do - local var="$(jq -r ".params[$i].id" <<<"$cfg")" - local val="$(jq -r ".params[$i].value" <<<"$cfg")" + local var val + var="$(jq -r ".params[$i].id" <<<"$cfg")" + val="$(jq -r ".params[$i].value" <<<"$cfg")" eval "export $var=$val" done } - - if which tmux && [[ $use_tmux == 0 ]] + + echo "$ncp_app" > "$LOCK_FILE" + if which tmux > /dev/null && [[ $use_tmux == 0 ]] then echo "Running $ncp_app in tmux..." | tee -a $log # Run app in tmux - local tmux_log_file="$(dirname "$CFGDIR")/ncp-tmux/tmux.${ncp_app}.log" - local tmux_status_file="$(dirname "$tmux_log_file")/tmux.${ncp_app}.status" - local LIBDIR="$(dirname $CFGDIR)/library.sh" + local tmux_log_file tmux_status_file LIBPATH + tmux_log_file="/var/log/ncp/tmux.${ncp_app}.log" + tmux_status_file="/var/log/ncp/tmux.${ncp_app}.status" + LIBPATH="$(dirname $CFGDIR)/library.sh" - echo "" > "$tmux_log_file" + # Reset tmux output + echo "[ $ncp_app ]" | tee -a $log + echo "[ $ncp_app ]" > "$tmux_log_file" + echo "" > "$tmux_status_file" + chmod 640 "$tmux_log_file" "$tmux_status_file" + chown root:www-data "$tmux_log_file" "$tmux_status_file" tmux new-session -d -s "$ncp_app" "bash -c '( - trap \"echo \$? >> $tmux_status_file\" 1 2 3 4 6 9 11 15 19 29 - source \"$LIBDIR\" - source \"$script\" - configure 2>&1 | tee -a $log - echo "\${PIPESTATUS[0]}" >> $tmux_status_file + trap \"echo \\\$? > $tmux_status_file && rm $LOCK_FILE\" 1 2 3 4 6 9 11 15 19 29 + source \"$LIBPATH\" + source \"$script\" + configure 2>&1 | tee -a $log + echo \"\${PIPESTATUS[0]}\" > $tmux_status_file + rm $LOCK_FILE )' 2>&1 | tee -a $tmux_log_file" attach_to_app "$ncp_app" - exit $? + exit else - echo "Running $ncp_app without tmux..." | tee -a $log + trap "rm '$LOCK_FILE'" 0 1 2 3 4 6 11 15 19 29 + echo "[ $ncp_app ]" | tee -a $log + echo "Running $ncp_app directly..." | tee -a $log # read script + # shellcheck source=/dev/null source "$script" # run configure 2>&1 | tee -a $log local ret="${PIPESTATUS[0]}" - exit $ret + exit "$ret" fi ) ret="$?" @@ -178,21 +216,28 @@ function run_app_unsafe() function attach_to_app() { - local tmux_log_file="$(dirname "$CFGDIR")/ncp-tmux/tmux.${ncp_app}.log" - local tmux_status_file="$(dirname "$tmux_log_file")/tmux.${ncp_app}.status" - - (while tmux has-session -t="$ncp_app" > /dev/null 2>&1 - do - sleep 1 - done) & + local tmux_log_file tmux_status_file + tmux_log_file="/var/log/ncp/tmux.${ncp_app}.log" + tmux_status_file="/var/log/ncp/tmux.${ncp_app}.status" + + if [[ "$ATTACH_NO_FOLLOW" == "1" ]] + then + cat "$tmux_log_file" + return 0 + else + (while tmux has-session -t="$ncp_app" > /dev/null 2>&1 + do + sleep 1 + done) & - # Follow log file until tmux session has terminated - tail --lines=+0 -f "$tmux_log_file" --pid="$!" + # Follow log file until tmux session has terminated + tail --lines=+0 -f "$tmux_log_file" --pid="$!" + fi # Read return value from tmux log file ret="$(tail -n 1 "$tmux_status_file")" - rm "$tmux_log_file" - rm "$tmux_status_file" + #rm "$tmux_log_file" + #rm "$tmux_status_file" [[ $ret =~ ^[0-9]+$ ]] && return $ret return 1 @@ -205,18 +250,20 @@ function is_active_app() local script="$bin_dir/$ncp_app.sh" local cfg_file="$CFGDIR/$ncp_app.cfg" - [[ -f "$script" ]] || local script="$(find "$BINDIR" -name $ncp_app.sh)" + [[ -f "$script" ]] || script="$(find "$BINDIR" -name $ncp_app.sh)" [[ -f "$script" ]] || { echo "file $script not found"; return 1; } # function unset is_active + # shellcheck source=/dev/null source "$script" [[ $( type -t is_active ) == function ]] && { is_active; return $?; } # config [[ -f "$cfg_file" ]] || return 1 - local cfg="$( cat "$cfg_file" )" + local cfg + cfg="$( cat "$cfg_file" )" [[ "$(jq -r ".params[0].id" <<<"$cfg")" == "ACTIVE" ]] && \ [[ "$(jq -r ".params[0].value" <<<"$cfg")" == "yes" ]] && \ return 0 @@ -228,9 +275,10 @@ function info_app() local ncp_app=$1 local cfg_file="$CFGDIR/$ncp_app.cfg" - local cfg="$( cat "$cfg_file" 2>/dev/null )" - local info=$( jq -r .info <<<"$cfg" ) - local infotitle=$( jq -r .infotitle <<<"$cfg" ) + local cfg info infotitle + cfg="$( cat "$cfg_file" 2>/dev/null )" + info=$( jq -r .info <<<"$cfg" ) + infotitle=$( jq -r .infotitle <<<"$cfg" ) [[ "$info" == "" ]] || [[ "$info" == "null" ]] && return 0 [[ "$infotitle" == "" ]] || [[ "$infotitle" == "null" ]] && infotitle="Info" @@ -245,18 +293,20 @@ function info_app() function install_app() { + local script local ncp_app=$1 # $1 can be either an installed app name or an app script if [[ -f "$ncp_app" ]]; then - local script="$ncp_app" - local ncp_app="$(basename "$script" .sh)" + script="$ncp_app" + ncp_app="$(basename "$script" .sh)" else - local script="$(find "$BINDIR" -name $ncp_app.sh)" + script="$(find "$BINDIR" -name $ncp_app.sh)" fi # do it unset install + # shellcheck source=/dev/null source "$script" echo "Installing $ncp_app" (install) @@ -266,6 +316,7 @@ function cleanup_script() { local script=$1 unset cleanup + # shellcheck source=/dev/null source "$script" if [[ $( type -t cleanup ) == function ]]; then cleanup diff --git a/ncp-web/js/ncp.js b/ncp-web/js/ncp.js index 556bc90e6..0aeedcb6e 100644 --- a/ncp-web/js/ncp.js +++ b/ncp-web/js/ncp.js @@ -25,8 +25,9 @@ window.onpopstate = function(event) { click_app($('#' + selectedID)); }; -function errorMsg() -{ +function errorMsg(e) +{ + console.log(e); $('#config-box').fill( "Something went wrong. Try refreshing the page" ); } @@ -268,21 +269,36 @@ $(function() var ret = $.parseJSON( result ); if ( ret.token ) $('#csrf-token').set( { value: ret.token } ); - if ( ret.ret ) // means that the process was launched + if ( "ret" in ret ) // means that the process was launched { - if ( ret.ret == '0' ) + if ( ret.ret == 0 ) { if( ret.ref && ret.ref == 'nc-update' ) window.location.reload( true ); reload_sidebar(); $('.circle-retstatus').set('+icon-green-circle'); } - else + else if ( ret.ret == -1 ) + { + if( ret.output ) + { + var box_l = $('#' + selectedID + '-details-box'); + var box = box_l[0]; + var lines = ret.output.split("\n"); + lines.forEach(line => { + box_l.ht( box.innerHTML + escapeHTML(line) + '
' ); + }); + box.scrollTop = box.scrollHeight; + } $('.circle-retstatus').set('-icon-green-circle'); + } + else + { + $('.circle-retstatus').set('-icon-green-circle'); + } } else // print error from server instead { - $('.details-box').fill(ret.output); $('.circle-retstatus').set('-icon-green-circle'); } $( 'input' , '#config-box-wrapper' ).set('@disabled', null); diff --git a/ncp-web/ncp-launcher.php b/ncp-web/ncp-launcher.php index 332450188..0eac22157 100644 --- a/ncp-web/ncp-launcher.php +++ b/ncp-web/ncp-launcher.php @@ -80,13 +80,33 @@ } // launch - echo '{ "token": "' . getCSRFToken() . '",'; // Get new token - echo ' "ref": "' . $ncp_app . '",'; - echo ' "output": "" , '; - echo ' "ret": '; - exec( 'bash -c "sudo /home/www/ncp-launcher.sh ' . $ncp_app . '"' , $output , $ret ); - echo '"' . $ret . '" }'; + $ret = null; + $output = null; + if( file_exists("/usr/local/etc/ncp.lock") ) + { + $running = trim(file_get_contents("/usr/local/etc/ncp.lock")); + $output = ""; + $output .= "An app ($running) is already running...".PHP_EOL; + if ( file_exists("/var/log/ncp/tmux.$running.log") ) + { + $output .= "Attaching to its output:".PHP_EOL; + $output .= file_get_contents("/var/log/ncp/tmux.$running.log"); + } + $ret=-1; + } + else + { + exec( 'sudo /home/www/ncp-launcher.sh ' . $ncp_app , $output , $ret ); + $output = ""; + } + + echo json_encode(array( + "token" => getCSRFToken(), + "ref" => $ncp_app, + "output" => $output, + "ret" => $ret + )); } // diff --git a/ncp-web/ncp-output.php b/ncp-web/ncp-output.php index 704b0a581..f7083f529 100644 --- a/ncp-web/ncp-output.php +++ b/ncp-web/ncp-output.php @@ -79,7 +79,7 @@ function follow($file) session_write_close(); echo str_pad('',1024*1024*4); // make sure the browser buffer becomes full -$ncp_log = '/var/log/ncp.log'; +$ncp_log = '/var/log/ncp/ncp.log'; if (!file_exists($ncp_log)) touch($ncp_log); follow($ncp_log); diff --git a/ncp.sh b/ncp.sh index 379b473e0..46a34460b 100644 --- a/ncp.sh +++ b/ncp.sh @@ -14,6 +14,7 @@ BRANCH=master BINDIR=/usr/local/bin/ncp CONFDIR=/usr/local/etc/ncp-config.d/ +LOGDIR=/var/log/ncp APTINSTALL="apt-get install -y --no-install-recommends" export DEBIAN_FRONTEND=noninteractive @@ -23,7 +24,7 @@ install() # NCP-CONFIG apt-get update $APTINSTALL git dialog whiptail jq tmux locales-all - mkdir -p "$CONFDIR" "$BINDIR" "$(dirname $CONFDIR)/ncp-tmux" + mkdir -p "$CONFDIR" "$BINDIR" "$LOGDIR" # include option in raspi-config (only Raspbian) test -f /usr/bin/raspi-config && { @@ -132,23 +133,26 @@ EOF cat > /home/www/ncp-launcher.sh <<'EOF' #!/bin/bash -check_running=false +declare -a args for arg in $@ do - if [[ $arg == "--check-running" ]] + if [[ $arg == "--auto-attach" ]] then - check_running=true + export ATTACH_TO_RUNNING=1 + elif [[ $arg == "--no-attach" ]] + then + export ATTACH_TO_RUNNING=0 + elif [[ $arg == "--no-follow" ]] + then + export ATTACH_NO_FOLLOW=1 + else + args+="$arg" fi done source /usr/local/etc/library.sh -if [[ $check_running == "true" ]] -then - which tmux && tmux has-session -t="$ncp_app" > /dev/null 2>&1 - exit $? -else - run_app $1 -fi +run_app ${args[0]} + EOF chmod 700 /home/www/ncp-launcher.sh echo "www-data ALL = NOPASSWD: /home/www/ncp-launcher.sh , /sbin/halt, /sbin/reboot" >> /etc/sudoers @@ -204,7 +208,7 @@ EOF # LIMIT LOG SIZE grep -q maxsize /etc/logrotate.d/apache2 || sed -i /weekly/amaxsize2M /etc/logrotate.d/apache2 cat >> /etc/logrotate.d/ncp <<'EOF' -/var/log/ncp.log +/var/log/ncp/ncp.log { rotate 4 size 500K @@ -216,7 +220,7 @@ EOF # ONLY FOR IMAGE BUILDS if [[ -f /.ncp-image ]]; then - rm -rf /var/log/ncp.log + rm -rf /var/log/ncp/ncp.log ## NEXTCLOUDPI MOTD rm -rf /etc/update-motd.d From bff0bc1e0ed9e44e1ebbe9eb902b5464326a4c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= Date: Tue, 19 Feb 2019 22:15:34 +0100 Subject: [PATCH 12/22] Remove vscode config from .gitignore --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e5238fb7f..1ede2b5c7 100644 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,4 @@ raspbian_boot ncp-web/wizard.cfg ncp-web/ncp-web.cfg docker-armhf/qemu-arm-static -.vagrant/ -.vscode/ +.vagrant/ \ No newline at end of file From 2a4ee286d0ef58a8cbfe5a840f89a6714f48f41a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= Date: Tue, 19 Feb 2019 22:16:44 +0100 Subject: [PATCH 13/22] library.sh: Remove commented code --- etc/library.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/etc/library.sh b/etc/library.sh index e3ae37794..d39083e33 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -236,8 +236,6 @@ function attach_to_app() # Read return value from tmux log file ret="$(tail -n 1 "$tmux_status_file")" - #rm "$tmux_log_file" - #rm "$tmux_status_file" [[ $ret =~ ^[0-9]+$ ]] && return $ret return 1 From cd8451d318815945bae42ddda0a16b0a1ff1468a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= Date: Tue, 19 Feb 2019 22:23:01 +0100 Subject: [PATCH 14/22] ncp-web: Remove debug output and unnecessary variable declarations --- ncp-web/js/ncp.js | 3 +-- ncp-web/ncp-launcher.php | 8 ++------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/ncp-web/js/ncp.js b/ncp-web/js/ncp.js index 0aeedcb6e..33a372c37 100644 --- a/ncp-web/js/ncp.js +++ b/ncp-web/js/ncp.js @@ -27,7 +27,6 @@ window.onpopstate = function(event) { function errorMsg(e) { - console.log(e); $('#config-box').fill( "Something went wrong. Try refreshing the page" ); } @@ -278,7 +277,7 @@ $(function() reload_sidebar(); $('.circle-retstatus').set('+icon-green-circle'); } - else if ( ret.ret == -1 ) + else if ( ret.ret === -1 ) // means we attach to an already running app { if( ret.output ) { diff --git a/ncp-web/ncp-launcher.php b/ncp-web/ncp-launcher.php index 0eac22157..074758c9c 100644 --- a/ncp-web/ncp-launcher.php +++ b/ncp-web/ncp-launcher.php @@ -81,13 +81,10 @@ // launch - $ret = null; - $output = null; if( file_exists("/usr/local/etc/ncp.lock") ) { $running = trim(file_get_contents("/usr/local/etc/ncp.lock")); - $output = ""; - $output .= "An app ($running) is already running...".PHP_EOL; + $output = "An app ($running) is already running...".PHP_EOL; if ( file_exists("/var/log/ncp/tmux.$running.log") ) { $output .= "Attaching to its output:".PHP_EOL; @@ -97,8 +94,7 @@ } else { - exec( 'sudo /home/www/ncp-launcher.sh ' . $ncp_app , $output , $ret ); - $output = ""; + exec( 'sudo /home/www/ncp-launcher.sh ' . $ncp_app , $cmd_out , $ret ); } echo json_encode(array( From 6c8e95c4d79ac6ebae5983397fbe4887052a9986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= Date: Tue, 19 Feb 2019 22:47:21 +0100 Subject: [PATCH 15/22] library.sh: A number of fixes suggested in Github review --- etc/library.sh | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/etc/library.sh b/etc/library.sh index d39083e33..c93ef28a0 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -121,18 +121,18 @@ function run_app_unsafe() echo "Running $ncp_app" # Check if app is already running in tmux + local running_app running_app=$( [[ -f "$LOCK_FILE" ]] && cat "$LOCK_FILE" || echo "" ) - [[ ! -z $running_app ]] && which tmux > /dev/null && tmux has-session -t="$running_app" > /dev/null 2>&1 && { - #echo "Already running. Attaching to output..." | tee -a $log + [[ ! -z $running_app ]] && which tmux >/dev/null && tmux has-session -t="$running_app" &>/dev/null && { - local choice + local choice question [[ $ATTACH_TO_RUNNING == "1" ]] && choice="y" [[ $ATTACH_TO_RUNNING == "0" ]] && choice="n" question="An app ($running_app) is already running. Do you want to attach to it's output? " if [[ $choice == "y" ]] || [[ $choice == "n" ]] then echo "$question" - echo "Choice: " + echo "Choice: <$choice>" else read -rp "$question" choice while [[ "$choice" != "y" ]] && [[ "$choice" != "n" ]] @@ -155,8 +155,8 @@ function run_app_unsafe() [[ -f "$cfg_file" ]] && { local len cfg cfg="$( cat "$cfg_file" )" - jq -e '.tmux' <<<"$cfg" > /dev/null 2>&1 - use_tmux="$?" + jq -e '.tmux' <<<"$cfg" &>/dev/null + local use_tmux="$?" len="$(jq '.params | length' <<<"$cfg")" for (( i = 0 ; i < len ; i++ )); do local var val @@ -174,10 +174,10 @@ function run_app_unsafe() local tmux_log_file tmux_status_file LIBPATH tmux_log_file="/var/log/ncp/tmux.${ncp_app}.log" tmux_status_file="/var/log/ncp/tmux.${ncp_app}.status" - LIBPATH="$(dirname $CFGDIR)/library.sh" + LIBPATH="$(dirname "$CFGDIR")/library.sh" # Reset tmux output - echo "[ $ncp_app ]" | tee -a $log + echo "[ $ncp_app ]" | tee -a "$log" echo "[ $ncp_app ]" > "$tmux_log_file" echo "" > "$tmux_status_file" chmod 640 "$tmux_log_file" "$tmux_status_file" @@ -187,7 +187,7 @@ function run_app_unsafe() trap \"echo \\\$? > $tmux_status_file && rm $LOCK_FILE\" 1 2 3 4 6 9 11 15 19 29 source \"$LIBPATH\" source \"$script\" - configure 2>&1 | tee -a $log + configure 2>&1 | tee -a "$log" echo \"\${PIPESTATUS[0]}\" > $tmux_status_file rm $LOCK_FILE )' 2>&1 | tee -a $tmux_log_file" @@ -197,8 +197,8 @@ function run_app_unsafe() else trap "rm '$LOCK_FILE'" 0 1 2 3 4 6 11 15 19 29 - echo "[ $ncp_app ]" | tee -a $log - echo "Running $ncp_app directly..." | tee -a $log + echo "[ $ncp_app ]" | tee -a "$log" + echo "Running $ncp_app directly..." | tee -a "$log" # read script # shellcheck source=/dev/null source "$script" From 467dc0171ecdce0d97e48fb984461141eb6fa95b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= Date: Tue, 19 Feb 2019 22:49:29 +0100 Subject: [PATCH 16/22] Move lock file to /run/ncp.lock --- etc/library.sh | 2 +- ncp-web/ncp-launcher.php | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/etc/library.sh b/etc/library.sh index c93ef28a0..c3740ff85 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -10,7 +10,7 @@ CFGDIR=/usr/local/etc/ncp-config.d BINDIR=/usr/local/bin/ncp -LOCK_FILE=/usr/local/etc/ncp.lock +LOCK_FILE=/run/ncp.lock function configure_app() { diff --git a/ncp-web/ncp-launcher.php b/ncp-web/ncp-launcher.php index 074758c9c..5e068b4ac 100644 --- a/ncp-web/ncp-launcher.php +++ b/ncp-web/ncp-launcher.php @@ -81,16 +81,17 @@ // launch - if( file_exists("/usr/local/etc/ncp.lock") ) + $lock_file = "/run/ncp.lock"; + if( file_exists($lock_file) ) { - $running = trim(file_get_contents("/usr/local/etc/ncp.lock")); + $running = trim(file_get_contents($lock_file)); $output = "An app ($running) is already running...".PHP_EOL; if ( file_exists("/var/log/ncp/tmux.$running.log") ) { $output .= "Attaching to its output:".PHP_EOL; $output .= file_get_contents("/var/log/ncp/tmux.$running.log"); } - $ret=-1; + $ret = -1; } else { From 59fe7ce1007a3de07a9122c78c16b879b8b26f6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= Date: Tue, 19 Feb 2019 23:13:04 +0100 Subject: [PATCH 17/22] library.sh: Fix erroneously unescaped quotes --- etc/library.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/library.sh b/etc/library.sh index c3740ff85..68f35f158 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -187,7 +187,7 @@ function run_app_unsafe() trap \"echo \\\$? > $tmux_status_file && rm $LOCK_FILE\" 1 2 3 4 6 9 11 15 19 29 source \"$LIBPATH\" source \"$script\" - configure 2>&1 | tee -a "$log" + configure 2>&1 | tee -a \"$log\" echo \"\${PIPESTATUS[0]}\" > $tmux_status_file rm $LOCK_FILE )' 2>&1 | tee -a $tmux_log_file" From a357f2cdd45b97eb8685440b935ca5c00e30d4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= Date: Tue, 19 Feb 2019 23:24:28 +0100 Subject: [PATCH 18/22] update.sh: Create log dir and updated ncp-launcher.sh --- ncp.sh | 6 +++--- update.sh | 24 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/ncp.sh b/ncp.sh index 46a34460b..5d95eafbe 100644 --- a/ncp.sh +++ b/ncp.sh @@ -14,7 +14,7 @@ BRANCH=master BINDIR=/usr/local/bin/ncp CONFDIR=/usr/local/etc/ncp-config.d/ -LOGDIR=/var/log/ncp +LOGFILE=/var/log/ncp/ncp.log APTINSTALL="apt-get install -y --no-install-recommends" export DEBIAN_FRONTEND=noninteractive @@ -24,7 +24,7 @@ install() # NCP-CONFIG apt-get update $APTINSTALL git dialog whiptail jq tmux locales-all - mkdir -p "$CONFDIR" "$BINDIR" "$LOGDIR" + mkdir -p "$CONFDIR" "$BINDIR" "$(dirname LOGFILE)" # include option in raspi-config (only Raspbian) test -f /usr/bin/raspi-config && { @@ -220,7 +220,7 @@ EOF # ONLY FOR IMAGE BUILDS if [[ -f /.ncp-image ]]; then - rm -rf /var/log/ncp/ncp.log + rm -rf "$LOGFILE" ## NEXTCLOUDPI MOTD rm -rf /etc/update-motd.d diff --git a/update.sh b/update.sh index 23985f5bc..cb7e92e73 100755 --- a/update.sh +++ b/update.sh @@ -11,6 +11,7 @@ set -e CONFDIR=/usr/local/etc/ncp-config.d/ +LOGFILE=/var/log/ncp/ncp.log # don't make sense in a docker container EXCL_DOCKER=" @@ -47,7 +48,8 @@ pgrep apt &>/dev/null && { echo "apt is currently running. Try again later"; ex type jq &>/dev/null || { apt-get update apt-get install -y --no-install-recommends jq tmux locales-all - mkdir -p "$(dirname $CONFDIR)/ncp-tmux" + mkdir -p "$(dirname "$LOGFILE")" + [[ -f /var/log/ncp.log ]] && mv /var/log/ncp.log "$LOGFILE" } # migrate to the new cfg format @@ -99,8 +101,26 @@ type jq &>/dev/null || { cat > /home/www/ncp-launcher.sh <<'EOF' #!/bin/bash + +declare -a args +for arg in $@ +do + if [[ $arg == "--auto-attach" ]] + then + export ATTACH_TO_RUNNING=1 + elif [[ $arg == "--no-attach" ]] + then + export ATTACH_TO_RUNNING=0 + elif [[ $arg == "--no-follow" ]] + then + export ATTACH_NO_FOLLOW=1 + else + args+="$arg" + fi +done + source /usr/local/etc/library.sh -run_app $1 +run_app ${args[0]} EOF chmod 700 /home/www/ncp-launcher.sh } From a0b9b287f64170cf41f73ad9ded07a312f8d1426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= Date: Wed, 20 Feb 2019 00:11:54 +0100 Subject: [PATCH 19/22] library.sh: Restructure run_app_unsafe() --- etc/library.sh | 140 +++++++++++++++++++++++++++---------------------- 1 file changed, 77 insertions(+), 63 deletions(-) diff --git a/etc/library.sh b/etc/library.sh index 68f35f158..28c6b3c4f 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -11,6 +11,7 @@ CFGDIR=/usr/local/etc/ncp-config.d BINDIR=/usr/local/bin/ncp LOCK_FILE=/run/ncp.lock +LOG=/var/log/ncp/ncp.log function configure_app() { @@ -110,44 +111,16 @@ function run_app_unsafe() script=$1 ncp_app="$(basename "$script" .sh)" local cfg_file="$CFGDIR/$ncp_app.cfg" - local log=/var/log/ncp/ncp.log [[ -f "$script" ]] || { echo "file $script not found"; return 1; } - touch $log - chmod 640 $log - chown root:www-data $log + touch "$LOG" + chmod 640 "$LOG" + chown root:www-data "$LOG" echo "Running $ncp_app" - # Check if app is already running in tmux - local running_app - running_app=$( [[ -f "$LOCK_FILE" ]] && cat "$LOCK_FILE" || echo "" ) - [[ ! -z $running_app ]] && which tmux >/dev/null && tmux has-session -t="$running_app" &>/dev/null && { - - local choice question - [[ $ATTACH_TO_RUNNING == "1" ]] && choice="y" - [[ $ATTACH_TO_RUNNING == "0" ]] && choice="n" - question="An app ($running_app) is already running. Do you want to attach to it's output? " - if [[ $choice == "y" ]] || [[ $choice == "n" ]] - then - echo "$question" - echo "Choice: <$choice>" - else - read -rp "$question" choice - while [[ "$choice" != "y" ]] && [[ "$choice" != "n" ]] - do - echo "choice was '$choice'" - read -rp "Invalid choice. y or n expected." choice - done - fi - - if [[ "$choice" == "y" ]] - then - attach_to_app "$running_app" - fi - return $? - } + attach_and_exit_if_running unset configure ( @@ -169,51 +142,92 @@ function run_app_unsafe() echo "$ncp_app" > "$LOCK_FILE" if which tmux > /dev/null && [[ $use_tmux == 0 ]] then - echo "Running $ncp_app in tmux..." | tee -a $log - # Run app in tmux - local tmux_log_file tmux_status_file LIBPATH - tmux_log_file="/var/log/ncp/tmux.${ncp_app}.log" - tmux_status_file="/var/log/ncp/tmux.${ncp_app}.status" - LIBPATH="$(dirname "$CFGDIR")/library.sh" - - # Reset tmux output - echo "[ $ncp_app ]" | tee -a "$log" - echo "[ $ncp_app ]" > "$tmux_log_file" - echo "" > "$tmux_status_file" - chmod 640 "$tmux_log_file" "$tmux_status_file" - chown root:www-data "$tmux_log_file" "$tmux_status_file" - - tmux new-session -d -s "$ncp_app" "bash -c '( - trap \"echo \\\$? > $tmux_status_file && rm $LOCK_FILE\" 1 2 3 4 6 9 11 15 19 29 - source \"$LIBPATH\" - source \"$script\" - configure 2>&1 | tee -a \"$log\" - echo \"\${PIPESTATUS[0]}\" > $tmux_status_file - rm $LOCK_FILE - )' 2>&1 | tee -a $tmux_log_file" - - attach_to_app "$ncp_app" - exit - + run_app_in_tmux "$ncp_app" "$script" else - trap "rm '$LOCK_FILE'" 0 1 2 3 4 6 11 15 19 29 - echo "[ $ncp_app ]" | tee -a "$log" - echo "Running $ncp_app directly..." | tee -a "$log" + # shellcheck disable=SC2064 + trap "rm '$LOCK_FILE'" EXIT SIGHUP SIGINT SIGQUIT SIGILL SIGABRT SIGSEV SIGTERM SIGIO + echo "[ $ncp_app ]" | tee -a "$LOG" + echo "Running $ncp_app directly..." | tee -a "$LOG" # read script # shellcheck source=/dev/null source "$script" # run - configure 2>&1 | tee -a $log + configure 2>&1 | tee -a "$LOG" local ret="${PIPESTATUS[0]}" exit "$ret" fi ) ret="$?" - echo "" >> $log + echo "" >> "$LOG" return "$ret" } +function run_app_in_tmux() +{ + local ncp_app="$1" + local script="$2" + + echo "Running $ncp_app in tmux..." | tee -a "$LOG" + # Run app in tmux + local tmux_log_file tmux_status_file LIBPATH + tmux_log_file="/var/log/ncp/tmux.${ncp_app}.log" + tmux_status_file="/var/log/ncp/tmux.${ncp_app}.status" + LIBPATH="$(dirname "$CFGDIR")/library.sh" + + # Reset tmux output + echo "[ $ncp_app ]" >> "$LOG" + echo "[ $ncp_app ]" > "$tmux_log_file" + echo "" > "$tmux_status_file" + chmod 640 "$tmux_log_file" "$tmux_status_file" + chown root:www-data "$tmux_log_file" "$tmux_status_file" + + tmux new-session -d -s "$ncp_app" "bash -c '( + trap \"echo \\\$? > $tmux_status_file && rm $LOCK_FILE\" 1 2 3 4 6 9 11 15 19 29 + source \"$LIBPATH\" + source \"$script\" + configure 2>&1 | tee -a \"$LOG\" + echo \"\${PIPESTATUS[0]}\" > $tmux_status_file + rm $LOCK_FILE + )' 2>&1 | tee -a $tmux_log_file" + + attach_to_app "$ncp_app" + return $? +} + +function attach_and_exit_if_running() +{ + # Check if app is already running in tmux + local running_app + running_app=$( [[ -f "$LOCK_FILE" ]] && cat "$LOCK_FILE" || echo "" ) + [[ ! -z $running_app ]] && which tmux >/dev/null && tmux has-session -t="$running_app" &>/dev/null && { + + local choice question + [[ $ATTACH_TO_RUNNING == "1" ]] && choice="y" + [[ $ATTACH_TO_RUNNING == "0" ]] && choice="n" + question="An app ($running_app) is already running. Do you want to attach to it's output? " + if [[ $choice == "y" ]] || [[ $choice == "n" ]] + then + echo "$question" + echo "Choice: <$choice>" + else + read -rp "$question" choice + while [[ "$choice" != "y" ]] && [[ "$choice" != "n" ]] + do + echo "choice was '$choice'" + read -rp "Invalid choice. y or n expected." choice + done + fi + + if [[ "$choice" == "y" ]] + then + attach_to_app "$running_app" + fi + exit $? + } + return 0 +} + function attach_to_app() { local tmux_log_file tmux_status_file From aaa4202d2bb8d9360a66c2b22703630ada44868c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= Date: Fri, 22 Feb 2019 23:18:47 +0100 Subject: [PATCH 20/22] ncp-launcher.sh: Remove unused aguments nc-autoupdate-nc, nc-update-nc-apps-auto: Update log path --- bin/ncp/CONFIG/nc-autoupdate-nc.sh | 6 +++--- bin/ncp/CONFIG/nc-update-nc-apps-auto.sh | 6 +++--- ncp.sh | 20 +------------------- update.sh | 19 +------------------ 4 files changed, 8 insertions(+), 43 deletions(-) diff --git a/bin/ncp/CONFIG/nc-autoupdate-nc.sh b/bin/ncp/CONFIG/nc-autoupdate-nc.sh index 8cfa1375b..079740d24 100644 --- a/bin/ncp/CONFIG/nc-autoupdate-nc.sh +++ b/bin/ncp/CONFIG/nc-autoupdate-nc.sh @@ -23,8 +23,8 @@ configure() cat > /etc/cron.daily/ncp-autoupdate-nc <> /var/log/ncp.log -/usr/local/bin/ncp-update-nc "$VERSION" 2>&1 | tee -a /var/log/ncp.log +echo -e "[ncp-update-nc]" >> /var/log/ncp/ncp.log +/usr/local/bin/ncp-update-nc "$VERSION" 2>&1 | tee -a /var/log/ncp/ncp.log if [[ \${PIPESTATUS[0]} -eq 0 ]]; then @@ -33,7 +33,7 @@ if [[ \${PIPESTATUS[0]} -eq 0 ]]; then sudo -u www-data php /var/www/nextcloud/occ notification:generate \ "$NOTIFYUSER" "NextCloudPi" -l "Nextcloud was updated to \$VER" fi -echo "" >> /var/log/ncp.log +echo "" >> /var/log/ncp/ncp.log EOF chmod 755 /etc/cron.daily/ncp-autoupdate-nc echo "automatic Nextcloud updates enabled" diff --git a/bin/ncp/CONFIG/nc-update-nc-apps-auto.sh b/bin/ncp/CONFIG/nc-update-nc-apps-auto.sh index 95466161f..c08aa8cd2 100644 --- a/bin/ncp/CONFIG/nc-update-nc-apps-auto.sh +++ b/bin/ncp/CONFIG/nc-update-nc-apps-auto.sh @@ -20,9 +20,9 @@ configure() cat > "$cronfile" <<'EOF' #!/bin/bash -echo "[ nc-update-nc-apps-auto ]" >> /var/log/ncp.log -echo "checking for updates..." >> /var/log/ncp.log -ncc app:update --all -n >> /var/log/ncp.log +echo "[ nc-update-nc-apps-auto ]" >> /var/log/ncp/ncp.log +echo "checking for updates..." >> /var/log/ncp/ncp.log +ncc app:update --all -n >> /var/log/ncp/ncp.log EOF chmod 755 "$cronfile" echo "automatic app updates enabled" diff --git a/ncp.sh b/ncp.sh index 5d95eafbe..e30826263 100644 --- a/ncp.sh +++ b/ncp.sh @@ -133,26 +133,8 @@ EOF cat > /home/www/ncp-launcher.sh <<'EOF' #!/bin/bash -declare -a args -for arg in $@ -do - if [[ $arg == "--auto-attach" ]] - then - export ATTACH_TO_RUNNING=1 - elif [[ $arg == "--no-attach" ]] - then - export ATTACH_TO_RUNNING=0 - elif [[ $arg == "--no-follow" ]] - then - export ATTACH_NO_FOLLOW=1 - else - args+="$arg" - fi -done - source /usr/local/etc/library.sh -run_app ${args[0]} - +run_app "$1" EOF chmod 700 /home/www/ncp-launcher.sh echo "www-data ALL = NOPASSWD: /home/www/ncp-launcher.sh , /sbin/halt, /sbin/reboot" >> /etc/sudoers diff --git a/update.sh b/update.sh index cb7e92e73..fc6f97d69 100755 --- a/update.sh +++ b/update.sh @@ -102,25 +102,8 @@ type jq &>/dev/null || { cat > /home/www/ncp-launcher.sh <<'EOF' #!/bin/bash -declare -a args -for arg in $@ -do - if [[ $arg == "--auto-attach" ]] - then - export ATTACH_TO_RUNNING=1 - elif [[ $arg == "--no-attach" ]] - then - export ATTACH_TO_RUNNING=0 - elif [[ $arg == "--no-follow" ]] - then - export ATTACH_NO_FOLLOW=1 - else - args+="$arg" - fi -done - source /usr/local/etc/library.sh -run_app ${args[0]} +run_app "$1" EOF chmod 700 /home/www/ncp-launcher.sh } From e2edadf7b0c20d59b8b681ec5aa29b4462fb9e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= Date: Fri, 22 Feb 2019 23:44:20 +0100 Subject: [PATCH 21/22] ncp.sh,update.sh: Improve installation of tmux, locale and new log directory --- ncp.sh | 7 ++++++- update.sh | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ncp.sh b/ncp.sh index e30826263..533b960e7 100644 --- a/ncp.sh +++ b/ncp.sh @@ -23,7 +23,12 @@ install() { # NCP-CONFIG apt-get update - $APTINSTALL git dialog whiptail jq tmux locales-all + $APTINSTALL git dialog whiptail jq tmux locale + # Install UTF-8 locale required by tmux + grep -v '#' /etc/locale.gen | grep -ie "en_US.UTF-8[[:blank:]]*UTF-8" &> /dev/null || { + echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen + locale-gen + } mkdir -p "$CONFDIR" "$BINDIR" "$(dirname LOGFILE)" # include option in raspi-config (only Raspbian) diff --git a/update.sh b/update.sh index fc6f97d69..def75198e 100755 --- a/update.sh +++ b/update.sh @@ -47,11 +47,27 @@ pgrep apt &>/dev/null && { echo "apt is currently running. Try again later"; ex # install new dependencies type jq &>/dev/null || { apt-get update - apt-get install -y --no-install-recommends jq tmux locales-all + apt-get install -y --no-install-recommends jq + +} + +which tmux || { + apt install tmux locale +} + +# Install UTF-8 locale required by tmux +grep -v '#' /etc/locale.gen | grep -ie "en_US.UTF-8[[:blank:]]*UTF-8" &> /dev/null || { + echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen + locale-gen +} + +# Migrate to new log location +[[ -d /var/log/ncp ]] || { mkdir -p "$(dirname "$LOGFILE")" [[ -f /var/log/ncp.log ]] && mv /var/log/ncp.log "$LOGFILE" } + # migrate to the new cfg format [[ -f "$CONFDIR"/dnsmasq.sh ]] && { From 5da89381ab34481488d93d7e4d3b659cb54c9a05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= Date: Fri, 22 Feb 2019 23:52:12 +0100 Subject: [PATCH 22/22] library.sh: Give more relevant messages depending on whether an app is run in tmux or conventionally. --- etc/library.sh | 53 +++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/etc/library.sh b/etc/library.sh index 28c6b3c4f..d4c1abc6a 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -147,7 +147,7 @@ function run_app_unsafe() # shellcheck disable=SC2064 trap "rm '$LOCK_FILE'" EXIT SIGHUP SIGINT SIGQUIT SIGILL SIGABRT SIGSEV SIGTERM SIGIO echo "[ $ncp_app ]" | tee -a "$LOG" - echo "Running $ncp_app directly..." | tee -a "$LOG" + echo "Closing the browser/terminal session will interrupt $ncp_app!" | tee -a "$LOG" # read script # shellcheck source=/dev/null source "$script" @@ -168,31 +168,32 @@ function run_app_in_tmux() local ncp_app="$1" local script="$2" - echo "Running $ncp_app in tmux..." | tee -a "$LOG" - # Run app in tmux - local tmux_log_file tmux_status_file LIBPATH - tmux_log_file="/var/log/ncp/tmux.${ncp_app}.log" - tmux_status_file="/var/log/ncp/tmux.${ncp_app}.status" - LIBPATH="$(dirname "$CFGDIR")/library.sh" - - # Reset tmux output - echo "[ $ncp_app ]" >> "$LOG" - echo "[ $ncp_app ]" > "$tmux_log_file" - echo "" > "$tmux_status_file" - chmod 640 "$tmux_log_file" "$tmux_status_file" - chown root:www-data "$tmux_log_file" "$tmux_status_file" - - tmux new-session -d -s "$ncp_app" "bash -c '( - trap \"echo \\\$? > $tmux_status_file && rm $LOCK_FILE\" 1 2 3 4 6 9 11 15 19 29 - source \"$LIBPATH\" - source \"$script\" - configure 2>&1 | tee -a \"$LOG\" - echo \"\${PIPESTATUS[0]}\" > $tmux_status_file - rm $LOCK_FILE - )' 2>&1 | tee -a $tmux_log_file" - - attach_to_app "$ncp_app" - return $? + echo "You can safely exit. $ncp_app will keep running until it's done." | tee -a "$LOG" + echo "Reattach at any time by running the app again." | tee -a "$LOG" + # Run app in tmux + local tmux_log_file tmux_status_file LIBPATH + tmux_log_file="/var/log/ncp/tmux.${ncp_app}.log" + tmux_status_file="/var/log/ncp/tmux.${ncp_app}.status" + LIBPATH="$(dirname "$CFGDIR")/library.sh" + + # Reset tmux output + echo "[ $ncp_app ]" >> "$LOG" + echo "[ $ncp_app ]" > "$tmux_log_file" + echo "" > "$tmux_status_file" + chmod 640 "$tmux_log_file" "$tmux_status_file" + chown root:www-data "$tmux_log_file" "$tmux_status_file" + + tmux new-session -d -s "$ncp_app" "bash -c '( + trap \"echo \\\$? > $tmux_status_file && rm $LOCK_FILE\" 1 2 3 4 6 9 11 15 19 29 + source \"$LIBPATH\" + source \"$script\" + configure 2>&1 | tee -a \"$LOG\" + echo \"\${PIPESTATUS[0]}\" > $tmux_status_file + rm $LOCK_FILE + )' 2>&1 | tee -a $tmux_log_file" + + attach_to_app "$ncp_app" + return $? } function attach_and_exit_if_running()