Skip to content

Instantly share code, notes, and snippets.

@nihilismus
Created October 18, 2015 17:12
Show Gist options
  • Save nihilismus/33cfc88afdd4be7c4c9d to your computer and use it in GitHub Desktop.
Save nihilismus/33cfc88afdd4be7c4c9d to your computer and use it in GitHub Desktop.
deploy-jee: Submits a WAR file and it's md5 file to an SFTP server.
#!/bin/sh
# deploy-jee: Submits a WAR file and it's md5 file to an SFTP server.
#
# Copyright © 2015 Antonio Hernández Blas <[email protected]>
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://www.wtfpl.net/ for more details.
host="SERVER'S NAME TO CONNECT"
port="SERVER'S TCP PORT TO CONNECT"
user="USER FOR LOGIN"
password="USER'S PASSWORD FOR LOGIN"
directory="REMOTE DIRECTORY (NOT PRECEDED BY /) TO SUBMIT WAR AND MD5 FILES"
function list_remote_files() {
echo "=> Files at '${host}:${port}/${directory}'"
lftp <<EOF
open sftp://${host}:${port}
user ${user} ${password}
cd ${directory}
recls -a -1 -h --date --size
bye
EOF
echo "=> Done"
}
function submit_war_and_md5_files() {
war_file="${1}"
md5_file="${1}.md5"
echo "=> Generating ${md5_file}"
md5sum "$war_file" > "$md5_file"
echo "=> Submitting '${war_file}' and '${md5_file}' to '${host}:${port}/${directory}'"
lftp <<EOF
open sftp://${host}:${port}
user ${user} ${password}
cd ${directory}
rm -f $md5_file
put $md5_file
rm -f $war_file
put $war_file
bye
EOF
sleep 1
list_remote_files
}
function remove_remote_war_file() {
war_file="$(basename ${1})"
md5_file="$(basename ${1}.md5)"
echo "=> Removing '${war_file}' and '${md5_file}' from '${host}:${port}/${directory}'"
lftp <<EOF
open sftp://${host}:${port}
user ${user} ${password}
cd ${directory}
rm -f $md5_file
rm -f $war_file
bye
EOF
echo "=> Done"
sleep 1
list_remote_files
}
function print_usage() {
echo
echo "Usage: deploy-jee [-h|-l] [-d war file] [-r war file]"
echo
echo "Examples:"
echo
echo " To deploy app99.war into the remote server:"
echo " \$ deploy-jee -d /home/alumno/wars/app99.war"
echo
echo " To list files/wars in the remote server:"
echo " \$ deploy-jee -l"
echo
echo " To remove a war file in the remote server:"
echo " \$ deploy-jee -r app99.war"
echo
}
# main
if [ $# -eq 0 ]; then
print_usage
exit 1
fi
case "$1" in
"-h")
print_usage
exit
;;
"-l")
list_remote_files
exit $?
;;
"-d")
if ! $(echo "${2}" | grep -Eq '.*\.war$'); then
echo "Error: you must give a local war file to deploy to the remote server."
exit 1
fi
if [ ! -f "${2}" ]; then
echo "Error: the war file '${1}' does not locally exist."
exit 1
fi
submit_war_and_md5_files "${2}"
exit $?
;;
"-r")
if ! $(echo "${2}" | grep -Eq '.*\.war$'); then
echo "Error, you must give a war file to remove from the remote server."
exit 1
fi
remove_remote_war_file "${2}"
exit $?
;;
*)
print_usage
exit 1
;;
esac
#EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment