-
Notifications
You must be signed in to change notification settings - Fork 45
/
deploy.sh
194 lines (155 loc) · 4.62 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
processName="imgalign"
targetFolderPath="/var/www/latsic.com/html/imgalign/"
sshLogin="[email protected]"
doStartProcess="0"
doStopProcess="0"
doDeployProcess="0"
doPublishProcess="0"
doInstall="0"
isSpa="0"
isDotnet="1"
showUsageAndExit() {
echo "Unknown parameter: ${!i}"
echo "Usage: deploy [spa|dotnet=default][stop][publish][deploy][start]"
exit 1
}
initActions() {
if [[ "$doStartProcess" == "0" && "$doStopProcess" == "0" && "$doDeployProcess" == "0" && "$doPublishProcess" == "0" ]] ; then
doDeployProcess="1"
doPublishProcess="1"
doStopProcess="1"
doStartProcess="1"
fi
}
stopProcess() {
local processName=$1
pidOfProcess=`ps -ef | grep -v grep | grep $processName | awk '{print $2}'`
if [ -z "$pidOfProcess" ]; then
echo "stop porcess: process $processName not found"
else
echo "stop process: $processName, pid $pidOfProcess"
kill -15 "$pidOfProcess"
fi
}
createPathIfNotExisting() {
local path=$1
mkdir -p $path
}
publishAndCopy() {
local sshLogin=$1
local targetPath=$2
if [[ "$doPublishProcess" == "1" && "$isDotnet" == "1" ]] ; then
echo "publishing dotnet app"
dotnet publish --configuration Release
fi
if [[ "$doPublishProcess" == "1" && "$isSpa" == "1" ]] ; then
if [[ "$doInstall" == "1" ]] ; then
echo "installing dependencies for $processName"
npm install
fi
echo "building $processName app"
npm run build
fi
if [[ "$doDeployProcess" == "1" && "$isDotnet" == "1" ]] ; then
echo "$sshLogin: deploying app $processName to $targetPath"
scp -r ./bin/Release/netcoreapp2.1/publish/. $sshLogin:$targetPath.
fi
if [[ "$doDeployProcess" == "1" && "$isSpa" == "1" ]] ; then
echo "$sshLogin: deploying app $processName to $targetPath"
scp -r ./dist/. $sshLogin:$targetPath.
fi
}
startProcess() {
local processFolderPath=$1
local processName=$2
cd $processFolderPath
nohup dotnet $processFolderPath$processName > $processFolderPath$processName.log 2>&1 &
pidOfProcess=`ps -ef | grep -v grep | grep $processName | awk '{print $2}'`
if [ -z "$pidOfProcess" ]; then
echo "start process: error verifying process, no pid for $processName found"
else
echo "start process: $processName started, pid $pidOfProcess"
fi
}
echoRemote() {
while read IN; do
if [[ $IN = *[![:space:]]* ]]; then
# only if non whitespace characters are present.
echo "$sshLogin: $IN"
fi
done
}
callRemote() {
if (($# < 2))
then
echo "callRemote: need at least two function arguments, got only $#"
echo "usage: callRemote <user@host> <functionName> [<arguments>]"
else
local sshLogin=$1
local funcName=$2
local arguments=""
for ((i=3;i<=$#;i++));
do
if (("$i"==3)); then
arguments=${!i}
else
arguments="$arguments ${!i}"
fi
done
#echo "redirection function call $funcName to $sshLogin, arguments: $arguments"
{
ssh $sshLogin "`declare -f $funcName`; $funcName $arguments; exit"
} 2>&1 | echoRemote
fi
}
addFilePermissions() {
local processFolderPath=$1
cd $processFolderPath
echo "file permissions: adding read permission recursively for all files in $processFolderPath"
chmod -R o+r $processFolderPath
find $processFolderPath -type d -exec chmod o+x {} +
}
for ((i=1;i<=$#;i++))
do
if [[ "${!i}" == "stop" ]] ; then
doStopProcess="1"
elif [[ "${!i}" == "start" ]] ; then
doStartProcess="1"
elif [[ "${!i}" == "deploy" ]] ; then
doDeployProcess="1"
elif [[ "${!i}" == "publish" ]] ; then
doPublishProcess="1"
elif [[ "${!i}" == "install" ]] ; then
doInstall="1"
elif [[ "${!i}" == "dotnet" ]] ; then
isDotnet="1"
elif [[ "${!i}" == "spa" ]] ; then
isDotnet="0"
isSpa="1"
else
showUsageAndExit
fi
done
if [[ "$isSpa" == "1" && "$isDotnet" == "1" ]] ; then
showUsageAndExit
fi
initActions
if [[ "$doStopProcess" = "1" && "$isDotnet" == "1" ]] ; then
callRemote "$sshLogin" "stopProcess" "$processName"
fi
if [[ "$doDeployProcess" = "1" ]] ; then
callRemote "$sshLogin" "createPathIfNotExisting" "$targetFolderPath"
fi
publishAndCopy "$sshLogin" "$targetFolderPath"
if [[ "$isSpa" == "1" && "$doDeployProcess" == "1" ]] ; then
callRemote "$sshLogin" "addFilePermissions" "$targetFolderPath"
fi
if [[ "$doStartProcess" == "1" && "$isDotnet" == "1" ]] ; then
if [[ "$doStopProcess" == "0" ]] ; then
echo "Before starting a new process, stoping an existing $processName process (if running)"
callRemote "$sshLogin" "stopProcess" "$processName"
fi
callRemote "$sshLogin" "startProcess" "$targetFolderPath" "$processName"
fi
echo "DONE"