|
#!/bin/bash |
|
|
|
#-------------------------- |
|
# Default Variable |
|
#-------------------------- |
|
: ${LXCOM_LX_SCRIPT_DIR:= "./remote_tmux"} |
|
: ${LXCOM_LOCAL_SCRIPT_DIR:="./bin"} |
|
: ${LXCOM_LOCAL_SETUP_FILE_NAME:="$LXCOM_LX_SCRIPT_DIR/setup_local"} |
|
|
|
#-------------------------- |
|
# HELP |
|
#-------------------------- |
|
function remote_commander_function_help { |
|
cat<<EOFF |
|
|
|
USAGE: $0 [-boh] <command> |
|
|
|
* OPTIONS : |
|
w/o option : run (default) command on new tmux window and get in |
|
-b : run command on new tmux in batch mode |
|
-o : open last tmux window or new window |
|
-r : run without default command |
|
-w : generate example file for setup |
|
-ws : generate run_on_tmux script ( this may be slightly faster than default ) |
|
-h : display this help |
|
|
|
* Setup Script |
|
$LXCOM_LX_SCRIPT_DIR/setup_local/setup_local_$USER |
|
$LXCOM_LX_SCRIPT_DIR/setup_local/setup_loca |
|
|
|
* Other Command : These command doesn't have option. |
|
batch_tmux_remote : same as $0 -b |
|
bcomm_tmux_remote : same as $0 -br |
|
comm_tmux_remote : same as $0 -r |
|
open_tmux_remote : same as $0 -o |
|
|
|
* SHELL VARIABLE |
|
EOFF |
|
remote_comander_function_var_example |
|
echo |
|
} |
|
|
|
#-------------------------- |
|
# Main Process |
|
#-------------------------- |
|
main() { |
|
#remote_commander_function_check_variable $* |
|
#remote_commander_function_check_option $* |
|
case "$1" in |
|
-h) |
|
remote_commander_function_help |
|
exit 0 |
|
;; |
|
-w) |
|
mkdir -p $LXCOM_LX_SCRIPT_DIR |
|
remote_comander_function_var_example > ${LXCOM_LOCAL_SETUP_FILE_NAME}_$USER.sample |
|
echo |
|
echo "* Example: Remove \".sample\" to use this" |
|
echo "${LXCOM_LOCAL_SETUP_FILE_NAME}_$USER.sample" |
|
echo |
|
exit 0 |
|
;; |
|
-ws) |
|
SCRIPT_remote > $LXCOM_LX_SCRIPT_DIR/run_on_tmux.sh |
|
echo |
|
echo "* \"$LXCOM_LX_SCRIPT_DIR/run_on_remote.sh\" has been written" |
|
echo "* To use this script, add next to your setup script" |
|
echo "LXCOM_TMUX_SCRIPT=$LXCOM_LX_SCRIPT_DIR/run_on_tmux.sh" |
|
echo |
|
exit 0 |
|
;; |
|
|
|
-*) |
|
option=$1 |
|
shift |
|
;; |
|
*) |
|
case $(basename $0) in |
|
open_tmux_remote*) |
|
exec $0 -o $* |
|
;; |
|
batch_tmux_remote*) |
|
exec $0 -b $* |
|
;; |
|
comm_tmux_remote*) |
|
exec $0 -r $* |
|
;; |
|
bcomm_tmux_remote*) |
|
exec $0 -rb $* |
|
;; |
|
esac |
|
esac |
|
remote_commander_function_loadSetupScript $* |
|
remote_commander_function_check_variable $* |
|
remote_commander_function_update_files |
|
remote_commander_function_copy_tmux_script |
|
remote_commander_function_run_on_remote_tmux $* |
|
} |
|
|
|
|
|
|
|
|
|
|
|
#-------------------------- |
|
# Script at remote |
|
#-------------------------- |
|
function SCRIPT_remote { |
|
echo '#!/bin/bash' |
|
cat<<'EOFF' |
|
[[ -n "$LXCOM_TARGET_DIR" && -e "$LXCOM_TARGET_DIR" ]] && cd $LXCOM_TARGET_DIR |
|
tname=mon; |
|
twin=monw; |
|
case "$1" in |
|
-*) |
|
mode=$1; |
|
shift |
|
;; |
|
esac |
|
tmux has-session -t $tname; |
|
if [ $? != 0 ]; |
|
then |
|
tmux new-session -s $tname -n $twin -d ; |
|
else |
|
[[ "$mode" =~ "o" ]] || tmux new-window ; |
|
fi |
|
if [ -n "$LXCOM_REMOTE_PATH" ];then |
|
tmux send-keys "export PATH=\$LXCOM_REMOTE_PATH:\$PATH" Enter; |
|
fi |
|
if [[ $mode =~ "r" ]];then |
|
[[ "$mode" =~ "o" ]] || tmux send-keys "$*" Enter; |
|
else |
|
[[ "$mode" =~ "o" ]] || tmux send-keys "$LXCOM_DEFAULT_COMMAND $*" Enter; |
|
fi |
|
|
|
[[ "$mode" =~ "b" ]] || tmux attach -t $tname; |
|
EOFF |
|
} |
|
|
|
|
|
#-------------------------------------------------- |
|
# Update Files to remote |
|
#-------------------------------------------------- |
|
remote_commander_function_update_files(){ |
|
if [[ "$LXCOM_COPY_LIST" != "off" ]];then |
|
rsync -avP --delete $LXCOM_COPY_LIST \ |
|
$LXCOM_SSH_NAME:$LXCOM_TARGET_DIR/ |
|
fi |
|
} |
|
|
|
#-------------------------------------------------- |
|
# Copy tmux script to remote if it doesn't exits |
|
#-------------------------------------------------- |
|
remote_commander_function_copy_tmux_script(){ |
|
if [[ -z "$LXCOM_TMUX_SCRIPT" || ! -e "$LXCOM_TMUX_SCRIPT" ]];then |
|
echo "### Copy tmux script to server" |
|
LXCOM_TMUX_SCRIPT=$(SCRIPT_remote | \ |
|
ssh $LXCOM_SSH_NAME \ |
|
'x=$(mktemp);cat - > $x;chmod +x $x;echo $x;') |
|
fi |
|
} |
|
|
|
#-------------------------------------------------- |
|
# RUN Command on remote |
|
#-------------------------------------------------- |
|
remote_commander_function_run_on_remote_tmux() { |
|
add_export LXCOM_TARGET_DIR |
|
add_export LXCOM_REMOTE_PATH |
|
if [[ ! "$option" =~ "r" ]];then |
|
add_export LXCOM_DEFAULT_COMMAND |
|
fi |
|
ssh $LXCOM_SSH_NAME -t \ |
|
"cd $LXCOM_TARGET_DIR;$LXCOM_EXPORT$LXCOM_TMUX_SCRIPT $option $*" |
|
} |
|
|
|
add_export(){ |
|
eval "local tmpvar=\${$1}" |
|
if [[ -n $tmpvar ]];then |
|
LXCOM_EXPORT=$LXCOM_EXPORT"export $1=$tmpvar;" |
|
fi |
|
} |
|
#-------------------------- |
|
# Load Set Up script |
|
#-------------------------- |
|
remote_commander_function_loadSetupScript() { |
|
local _LXCOM_LOCAL_SETUP_FILE_NAME=$(eval echo ${LXCOM_LOCAL_SETUP_FILE_NAME}{_$USER,}) |
|
for x in ${_LXCOM_LOCAL_SETUP_FILE_NAME};do |
|
if [ -e "$x" ];then |
|
. $x && local __Setup_Loaded='true' |
|
break |
|
fi |
|
done |
|
[ -z "$__Setup_Loaded" ] && \ |
|
echo "ERROR : Any of ${_LXCOM_LOCAL_SETUP_FILE_NAME} doesn't exits" && exit 1 |
|
} |
|
|
|
|
|
#-------------------------- |
|
# Check Variables |
|
#-------------------------- |
|
remote_commander_function_check_variable() { |
|
for x in ${*:-USER_NAME SERVER_NAME TARGET_DIR COPY_LIST} |
|
do |
|
if [ -z "$(eval echo \${LXCOM_$x})" ];then |
|
echo "\$$x is empty" |
|
local __SUCCESS="false" |
|
fi |
|
done |
|
[ "$__SUCCESS" == "false" ] && exit 1 |
|
LXCOM_SSH_NAME=$LXCOM_USER_NAME@$LXCOM_SERVER_NAME |
|
} |
|
|
|
#-------------------------------------------------- |
|
# EXAMPLE SETUP |
|
#-------------------------------------------------- |
|
function remote_comander_function_var_example { |
|
cat <<EOFF |
|
LXCOM_LX_SCRIPT_DIR=./remote_tmux |
|
LXCOM_LOCAL_SCRIPT_DIR=./bin |
|
LXCOM_USER_NAME=\$USER |
|
LXCOM_SERVER_NAME=lx |
|
LXCOM_TARGET_DIR="~/work/\$(basename \$PWD)" |
|
LXCOM_COPY_LIST="*.h *.cxx *.sh \$LXCOM_LOCAL_SCRIPT_DIR \$LXCOM_LX_SCRIPT_DIR" # can be "off" |
|
#LXCOM_DEFAULT_COMMAND="\$LXCOM_LX_SCRIPT_DIR/run.sh" |
|
#LXCOM_REMOTE_PATH="\$LXCOM_LX_SCRIPT_DIR" # Optional |
|
#LXCOM_DEFAULT_COMMAND="\$LXCOM_LX_SCRIPT_DIR/run.sh" # Optional |
|
#LXCOM_TMUX_SCRIPT="\$LXCOM_LX_SCRIPT_DIR/run_on_tmux.sh" # Optional |
|
EOFF |
|
} |
|
|
|
#-------------------------------------------------- |
|
# call main function in the end. |
|
# This should be at last line !!!!! |
|
#-------------------------------------------------- |
|
main $* |