__("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()