Skip to content

Commit

Permalink
Allow execution of web script
Browse files Browse the repository at this point in the history
Fixes: #2
  • Loading branch information
ssbarnea committed Dec 30, 2018
1 parent cc12961 commit 4d9d49d
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 6 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ dist
build
eggs
parts
bin
var
sdist
develop-eggs
Expand Down Expand Up @@ -59,4 +58,4 @@ ChangeLog

# Files created by releasenotes build
releasenotes/build
docs/movie.app
docs/movie.app
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ exit

# run df on two remote hosts, syncronized windows
HOSTS="node1 node2" rmux df

# run a script downloaded from the web (installs pip)
rmux -w https://bootstrap.pypa.io/get-pip.py

```

If you fail to define the ``HOSTS`` variable the tool will default to localhost
Expand Down
124 changes: 124 additions & 0 deletions bin/rmux
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/env bash
# Shortcut for creating templated new shell scripts
set -euo pipefail
: ${HOSTS:=localhost}
: ${XPANES_OPT:=-ss}
red=`tput setaf 1`
green=`tput setaf 2`
blue=`tput setaf 4`
bold=`tput bold; tput setaf 7`
reset=`tput sgr0`

# -- functions ---

get_abs_path() {
local PARENT_DIR
local ABS_PATH
PARENT_DIR=$(dirname "$1")
cd "$PARENT_DIR"
ABS_PATH="$(pwd)"/"$(basename "$1")"
cd - >/dev/null
echo "$ABS_PATH"
}

# --- cli processing ---
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"

case $key in
-w)
WEB_SCRIPT="$2"
shift # past argument
shift # past value
break # anything after url will be passed to remote command
;;
-h|--help)
shift # past argument
cat <<EOF
Usage:
rmux [command] [options]
rmux -w <url> [options]
General Options:
-h, --help Show help.
--version Displays current version and where it runs from
-w <url> Download script from url and execute it
The list of hosts is loaded from ${bold}HOSTS${reset} environment variable \
and when this is not defined it will fallback to using localhost, still even \
in this case it will use ssh.
EOF
exit 0
;;
--version)
shift
echo "rmux ${green}$(python -c 'import rmux; " \
"print(rmux.__version__)')${reset} from ${blue}" \
"$(get_abs_path `which $0`)${reset}"
exit 0
;;
*) # unknown option
POSITIONAL+=("$*") # save it in an array for later
# shift
break
;;
esac
done
# set -- "${POSITIONAL[@]:-}" # restore positional parameters

if [ "${HOSTS}" == "localhost" ]; then
&>2 echo "Warning: using localhost fallback, define HOSTS as " \
"a space separated list"
fi

which xpanes >/dev/null || {
>&2 echo "FATAL: xpanes not found, please install it. " \
"https://github.com/greymd/tmux-xpanes#installation"
exit 2
}

# detect current project based on .git presence
PROJECT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || true)
PROJECT_NAME=$(basename "$PROJECT_ROOT")
REMOTE_DIR=.cache/.rmux/$PROJECT_NAME
if [[ "$PROJECT_ROOT" != "" ]]; then
echo "INFO: Found ${green}$PROJECT_NAME${reset} project on " \
"${green}$PROJECT_ROOT${reset}"
# generate smart exclude list, based on .gitignore when possible.
# See https://stackoverflow.com/a/15373763/99834
(
cd $PROJECT_ROOT
git ls-files --exclude-standard -oi --directory \
>$PROJECT_ROOT/.git/ignores.tmp
)
for HOST in $HOSTS; do
set -x
ssh $HOST mkdir -p $REMOTE_DIR
rsync -ah --no-o --no-g --include .git \
--exclude-from=$PROJECT_ROOT/.git/ignores.tmp \
$PROJECT_ROOT/ $HOST:$REMOTE_DIR/
done
fi


# >&2 echo "DEBUG: arguments $# : $*"
XPANES_OPT='-ss'

if [[ "${WEB_SCRIPT:-}" != "" ]]; then
SCRIPT_FILENAME="${WEB_SCRIPT##*/}"
INJECT="curl -s $WEB_SCRIPT > $SCRIPT_FILENAME && "\
"chmod +x $SCRIPT_FILENAME && ./$SCRIPT_FILENAME"
fi


if [ "$#" -eq 0 ]; then
>&2 echo "INFO: No command specified, just starting ssh sessions"
CMD="\${SHELL:-sh} -i <<< 'cd $REMOTE_DIR;${INJECT:-}; exec </dev/tty'"
else
XPANES_OPT='-ss'
CMD="\${SHELL:-sh} -i <<< 'cd $REMOTE_DIR;${INJECT:-} $*; exec </dev/tty'"
fi

xpanes $XPANES_OPT -t \
-c "ssh -t -o StrictHostKeyChecking=no -o ForwardX11=no {} \"$CMD\"" $HOSTS
8 changes: 4 additions & 4 deletions docs/movie.applescript
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
tell application "Finder"
set parentpath to POSIX path of (parent of (path to me) as string)
set filename to name of (path to me)

-- display dialog parentpath
-- display dialog filename
end tell
Expand All @@ -11,7 +11,7 @@ end tell
tell application "iTerm"
activate
create window with default profile

tell first session of current tab of current window
set name to "rmux usage example"
set columns to 80
Expand All @@ -30,7 +30,7 @@ tell application "iTerm"
my info_string("so we are done, time to close our session by typing exit or pressing Ctrl-D")
delay 4
end tell

tell application "System Events" to tell process "iTerm"
keystroke "d" using control down -- close tmux
delay 1
Expand Down Expand Up @@ -78,4 +78,4 @@ on replace_chars(this_text, search_string, replacement_string)
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
end replace_chars

0 comments on commit 4d9d49d

Please sign in to comment.