-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·78 lines (63 loc) · 1.6 KB
/
deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
#
# Remote tomcat deploy script
#
# 1. Scps a web archive to a the remote server.
# 2. Stop the remote tomcat.
# 3. Copies to war file to tomcat webapps.
# 4. Starts the remote tomcat.
#
EXPECTED_ARGS=3
NOW=$(date +"%Y%m%d%H%M%S")
if [ $# -lt $EXPECTED_ARGS ]
then
echo "Usage: `basename $0` <server> <file> <context>"
exit $E_BADARGS
fi
CONTEXT=$3
FILE=$2
SERVER=$1
REMOTE_FILE=/tmp/$CONTEXT-$NOW.war
REMOTE_WEBAPPS=/opt/tomcat/current/webapps
REMOTE_WAR="$REMOTE_WEBAPPS/$CONTEXT.war"
REMOTE_CONTEXT="$REMOTE_WEBAPPS/$CONTEXT"
START_TOMCAT="sudo /etc/init.d/tomcat start"
STOP_TOMCAT="sudo /etc/init.d/tomcat stop"
# Copy local war to remote machine
copy_file() {
log "Copying $FILE to $SERVER:$REMOTE_FILE"
scp $FILE $SERVER:$REMOTE_FILE
}
# Stops tomcat on remote machine
stop_server() {
log "Stopping remote tomcat"
ssh $SERVER $STOP_TOMCAT
sleep 10
ssh $SERVER "kill -9 `ps ux | awk '/java/ && /tomcat/ && !/awk/ {print $2}'`"
}
# Start tomcat on remote machine
start_server() {
log "Starting remote tomcat"
ssh $SERVER $START_TOMCAT
}
# Copy remote temporary file to deployment path
deploy_file() {
log "Deleting old version of $CONTEXT"
ssh $SERVER sudo rm -rf $REMOTE_WAR
ssh $SERVER sudo rm -rf $REMOTE_CONTEXT
log "Copying temporary file to $REMOTE_WAR"
ssh $SERVER sudo mv $REMOTE_FILE $REMOTE_WAR
}
log_deployment() {
ssh $SERVER 'echo "Deployment was started at `date`" >> ~/deploy_log.txt'
}
log() {
echo "$(date +"%Y-%m-%d %H:%M:%S"): $1"
}
log "Deploying $FILE to $SERVER/$CONTEXT"
log_deployment
copy_file
stop_server
deploy_file
start_server
log "Done deploying."