Last active
June 13, 2016 08:53
-
-
Save didyhu/540c8b37e0767f2145f07474faf3b232 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bash | |
targethost='root@targethost' | |
# eg. 'root@dev.csdra.net' | |
targetpath='/var/www' | |
# eg. '/var/www' | |
targetname=TARGETNAME | |
# eg. 'SomeProject' | |
targetport=TARGETPORT | |
# eg. 8080 | |
targetparams="" | |
# other params | |
echo "Welcome" | |
reset(){ | |
echo "## Remove local workdir" | |
rm .git/.deliver -Rf | |
echo "## Move remote workdir" | |
ssh $targethost "mv ${targetpath}/$targetname ${targetpath}/$targetname.bak.`date +'%s'`" | |
echo "## Remove remote repository" | |
ssh $targethost "rm ${targetpath}/$targetname.git -Rf" | |
} | |
start(){ | |
echo "## Try to start" | |
ssh $targethost "cd ${targetpath}/$targetname;(java -cp target org.springframework.boot.loader.JarLauncher --server.port=${targetport} ${targetparams} &)"& | |
} | |
stop(){ | |
echo "## Try to stop" | |
ssh $targethost "cid=\$(lsof -i:$targetport | grep java | awk '{print \$2}' | head -1); echo \"killing:\$cid\"; [ \$cid ] && kill \$cid" | |
} | |
build(){ | |
echo "## Start packaging" | |
./mvnw clean package -Dmaven.test.skip=true | |
[ $? -eq 0 ] || exit $? | |
rm .git/.deliver/target -Rf | |
unzip -o target/*.jar -d .git/.deliver/target | |
} | |
transfer(){ | |
echo "## Start transferring" | |
cd .git/.deliver | |
echo "## Commit local workdir" | |
git add . | |
git commit -m "`date +"%Y-%m-%dT%H-%M-%S %z"`" | |
echo "## Push to remote repository" | |
git push target master | |
echo "## Pull to remote workdir" | |
ssh $targethost "cd ${targetpath};if [ ! -d $targetname ]; then git clone $targetpath/$targetname.git; else cd $targetname; git pull; fi;" | |
cd ../.. | |
} | |
init(){ | |
echo "## Prepare remote repository" | |
ssh $targethost "if [ ! -d $targetpath/$targetname.git ]; then echo 'Creating git repository'; git init $targetpath/$targetname.git --bare; else echo 'Found repository' ; fi" | |
echo "## Prepare local workdir" | |
mkdir -p .git/.deliver | |
cd .git/.deliver | |
if [ ! -d .git ] | |
then git init;git remote add target "ssh://${targethost}${targetpath}/${targetname}.git"; | |
fi | |
git pull target master; | |
cd ../.. | |
} | |
restart(){ | |
stop | |
start | |
} | |
status(){ | |
ssh $targethost "lsof -i:${targetport}" | |
} | |
sshagent(){ | |
if [ ! "$SSH_AGENT_PID" ] | |
then | |
eval `ssh-agent` | |
ssh-add | |
fi | |
} | |
case "$1" in | |
sshagent) sshagent ;; | |
start) sshagent; start ;; | |
stop) sshagent; stop ;; | |
reset) sshagent; reset ;; | |
init) sshagent; init ;; | |
build) build ;; | |
upload) sshagent; init; build; transfer ;; | |
restart) sshagent; stop; start ;; | |
status) sshagent; status ;; | |
*) sshagent; init; build; transfer; restart ;; | |
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
usage() | |
{ | |
echo "Usage: `basename $0` -c COMMAND -h HOST -p PORT [-c start|stop|restart|upload|status]" | |
exit 1 | |
} | |
[ $# -eq 0 ] && usage | |
NAME=`basename "$(pwd)"` | |
REMOTEDIR="/var/www/$NAME" | |
REMOTEREPO="/var/git/.deliver/$NAME.git" | |
LOCALREPO="$(pwd)/.git/.deliver" | |
while getopts "h:p:c:" OPTION | |
do | |
case $OPTION in | |
h) | |
HOST=$OPTARG | |
;; | |
p) | |
PORT=$OPTARG | |
;; | |
c) | |
COMMAND=$OPTARG | |
;; | |
esac | |
done | |
echo "Welcome" | |
start(){ | |
echo "## Try to start" | |
ssh $HOST "cd $REMOTEDIR;(java -cp target org.springframework.boot.loader.JarLauncher --server.port=$PORT &)"& | |
} | |
stop(){ | |
echo "## Try to stop" | |
ssh $HOST "cid=\$(lsof -i:$PORT | grep java | awk '{print \$2}' | head -1); if [ \$cid ] ; then echo \"killing:\$cid\" ; kill \$cid; fi;" | |
} | |
build(){ | |
echo "## Start packaging" | |
sh mvnw clean package -Dmaven.test.skip=true | |
[ $? -eq 0 ] || exit $? | |
rm $LOCALREPO/target -Rf | |
unzip -o target/*.jar -d $LOCALREPO/target | |
} | |
transfer(){ | |
echo "## Start transferring" | |
cd $LOCALREPO | |
echo "## Commit local workdir" | |
git add . | |
git commit -m "`date +"%Y-%m-%dT%H-%M-%S %z"`" | |
echo "## Push to remote repository" | |
git push target master | |
echo "## Pull to remote workdir" | |
ssh $HOST "if [ ! -d $REMOTEDIR ]; then git clone $REMOTEREPO $REMOTEDIR; else cd $REMOTEDIR; git pull; fi;" | |
cd ../.. | |
} | |
init(){ | |
echo "## Prepare remote repository" | |
ssh $HOST "if [ ! -d $REMOTEREPO ]; then echo 'Creating git repository'; git init $REMOTEREPO --bare; else echo 'Found repository' ; fi" | |
echo "## Prepare local workdir" | |
mkdir -p $LOCALREPO | |
cd $LOCALREPO | |
if [ ! -d .git ] | |
then git init;git remote add target "ssh://$HOST$REMOTEREPO"; | |
fi | |
git pull target master; | |
cd ../.. | |
} | |
restart(){ | |
stop | |
start | |
} | |
status(){ | |
ssh $HOST "lsof -i:${PORT}" | |
} | |
sshagent(){ | |
if [ ! "$SSH_AGENT_PID" ] | |
then | |
eval `ssh-agent` | |
ssh-add | |
fi | |
} | |
case $COMMAND in | |
sshagent) sshagent ;; | |
start) sshagent; start ;; | |
stop) sshagent; stop ;; | |
init) sshagent; init ;; | |
build) build ;; | |
upload) sshagent; init; build; transfer ;; | |
restart) sshagent; stop; start ;; | |
status) sshagent; status ;; | |
*) sshagent; init; build; transfer; restart ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment