-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLambda-Deployment-Script.sh
More file actions
171 lines (156 loc) · 8.21 KB
/
Copy pathLambda-Deployment-Script.sh
File metadata and controls
171 lines (156 loc) · 8.21 KB
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
#!/bin/bash
#Base Path
basePath='/opt/aws-lambda-adapter/'
logFilePath="$basePath"'deployments/sls-logs/'
mailTemplFile="$basePath"'deployments/mail-template.sh'
#Declaring the variable
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
DARKGRAY="\033[1;90m"
LIGHTBLUE="\033[1;94m"
LIGHTGRAY="\033[1;37m"
NOCOLOR="\033[0m"
#sls config
slsRegion='ap-southeast-1'
slsConfig='serverless.yml'
slsStage=$1
slsProfile=$2
#log file
gitCommitID=$(git log --format="%H" -n 1)
logFile="$logFilePath$gitCommitID"'.log'
#Decorator function
outputDecorator(){
if [ $1 == 0 ]
then
echo -e "${RED}Error: ${NOCOLOR} $2. ${NOCOLOR}" | tee -a $logFile
elif [ $1 == 1 ]
then
echo -e "${GREEN}Success: ${NOCOLOR} $2. ${NOCOLOR}" | tee -a $logFile
elif [ $1 == -1 ]
then
echo -e "$2 ${NOCOLOR}" | tee -a $logFile
fi
}
#Serverless delployment function
serverlessDeployment(){
outputDecorator -1 "${LIGHTBLUE}NPM:${NOCOLOR}${LIGHTGRAY} Installing the App's Dependency Packages...."
npm install
outputDecorator -1 "${LIGHTBLUE}NPM:${NOCOLOR}${LIGHTGRAY} Updating the App's Dependency Packages...."
npm update
outputDecorator -1 "${LIGHTBLUE}SLS:${NOCOLOR}${LIGHTGRAY} Serverless Deploying...."
outputDecorator -1 "${YELLOW} -------------------------------------------------------"
if [ -z "$slsProfile" ]; then
serverless deploy --stage "$slsStage" --region "$slsRegion" | tee -a $logFile
else
serverless deploy --stage "$slsStage" --region "$slsRegion" --profile "$slsProfile" | tee -a $logFile
fi
outputDecorator -1 "${YELLOW} -------------------------------------------------------"
if [ $(grep -o 'Service Information' "$logFile" | wc -l) -gt 0 ]; then
outputDecorator 1 "${GREEN} Serverless Deployment has successfully finished."
else
outputDecorator 1 "${RED} Serverless deploy failed."
fi
}
#Start default OUTPUT line
outputDecorator -1 "${DARKGRAY}############################## DEPLOYMENT LOG ##############################"
#END default line
#Check arguments supplied
#The $# variable will tell you the number of input arguments the script was passed.
if [ $# -eq 0 ]
then
outputDecorator 0 "${RED}Environment variable not defined.${NOCOCLOR}.\n${YELLOW}Hint: Pass the arguments to script as staging/prod"
exit 1
elif [ $slsStage == 'staging' ] || [ $slsStage == 'prod' ]
then
outputDecorator -1 "${LIGHTBLUE}ENV:${NOCOLOR}${LIGHTGRAY} Setting the Serverless environment as $slsStage"
else
outputDecorator 0 "Invalid Stage Param. Stage Parameter should be either ${YELLOW}Staging ${NOCOLOR}or ${GREEN}Prod"
exit 1
fi
#Check git command exist
if ! [ -x "$(command -v git)" ]; then
outputDecorator 0 "${RED}git ${NOCOLOR}is not installed"
exit 1
fi
#Git details
gitBaseName=$(basename `git rev-parse --show-toplevel`)
gitCommitID=$(git log --format="%H" -n 1)
gitNameEmailKey=0;
gitRevCommitIDs[$gitNameEmailKey]=$gitCommitID
gitUserName[$gitNameEmailKey]=$(git --no-pager show -s --format='%an <%ae>' $gitCommitID | grep -o -P '(?<=).*(?=<)')
gitUserEmail[$gitNameEmailKey]=$(git --no-pager show -s --format='%an <%ae>' $gitCommitID | grep -o -P '(?<=<).*(?=>)')
for parentCommitID in $(git log --pretty=%P -n 1 $gitCommitID)
do
((gitNameEmailKey++));
gitRevCommitIDs[$gitNameEmailKey]=$parentCommitID
gitUserName[$gitNameEmailKey]=$(git --no-pager show -s --format='%an <%ae>' $parentCommitID | grep -o -P '(?<=).*(?=<)')
gitUserEmail[$gitNameEmailKey]=$(git --no-pager show -s --format='%an <%ae>' $parentCommitID | grep -o -P '(?<=<).*(?=>)')
done;
#We can just overwrite our array with the unique elements
gitRevCommitIDs=( `for i in ${gitRevCommitIDs[@]}; do echo $i; done | sort -u` )
gitUserName=( `for i in ${gitUserName[@]}; do echo $i; done | sort -u` )
gitUserEmail=( `for i in ${gitUserEmail[@]}; do echo $i; done | sort -u` )
outputDecorator -1 "${LIGHTBLUE}GIT:${NOCOLOR}${LIGHTGRAY} Checking the Git Repository...."
#Check git repository exist
if [ -d '.git' ] || [ -d '../.git' ]; then
outputDecorator -1 "${LIGHTBLUE}GIT:${NOCOLOR}${LIGHTGRAY} Git Repository exists as $gitBaseName"
outputDecorator -1 "${LIGHTBLUE}GIT:${NOCOLOR}${LIGHTGRAY} Git resetting the $slsConfig file"
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${YELLOW} --------------- Git revision details ---------------"
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${LIGHTGRAY} Revision: $gitCommitID"
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${LIGHTGRAY} Message : $(git log --format=%B -n 1 $gitCommitID)"
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${LIGHTGRAY} User : $gitUserName"
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${LIGHTGRAY} Email : $gitUserEmail"
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${YELLOW} ----------------------------------------------------"
#If the commit was a merge, and it was TREESAME to parent, follow all parents.
if [[ "$(git cat-file -p $gitCommitID)" =~ .*Merging*. ]]; then
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${YELLOW} Revision has merge history and it was TREESAME to following parents"
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${YELLOW} --------------- Git revision's Parent commit details ---------------"
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${LIGHTGRAY} CommitId: ${gitRevCommitIDs[*]}"
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${LIGHTGRAY} Message : $(git log --format=%B -n 1 ${gitRevCommitIDs[*]})"
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${LIGHTGRAY} Users : ${gitUserName[*]}"
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${LIGHTGRAY} Emails : ${gitUserEmail[*]}"
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${YELLOW} ----------------------------------------------------"
fi
#git checkout "$slsConfig"
else
outputDecorator 0 "${RED} Git Repository ${NOCOLOR}Not exists"
exit 1
fi
#Check serverless config file and Replace the env variable
#if [ -f 'serverless.yml' ]; then
#if [ $(ls -1 *.yml 2>/dev/null | wc -l) -gt 0 ] || [ $(ls -1 *.yaml 2>/dev/null | wc -l) -gt 0 ]; then
if [ -f "$slsConfig" ]; then
#sed -i "s/.*stage:.*$/ stage: $slsStage/" "$slsConfig"
outputDecorator -1 "${LIGHTBLUE}ENV:${NOCOLOR}${LIGHTGRAY} Resetting the stage variable as $slsStage"
serverlessDeployment
else
for slsDir in * ; do
if [ -f "$basePath"'integration/'"$slsDir"'/'"$slsConfig" ]; then
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${LIGHTGRAY} ******************** START Serverless Log for $slsDir ********************"
outputDecorator -1 "${LIGHTBLUE}ENV:${NOCOLOR}${LIGHTGRAY} Resetting the stage variable as $slsStage in $slsDir"
echo $basePath'integration/'$slsDir
cd $basePath'integration/'$slsDir
serverlessDeployment
outputDecorator -1 "${LIGHTBLUE} ${NOCOLOR}${LIGHTGRAY} ******************** END Serverless Log fro $slsDir ********************"
fi
done
#echo 'Error: "$slsConfig" file is not exist.' >&2
#exit 1
fi
#Start default OUTPUT line
outputDecorator -1 "${DARKGRAY}############################## ENDED DEPLOYMENT LOG ##############################"
cp "$logFile" "$logFilePath$gitBaseName"'.log'
sed -ri "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" "$logFilePath$gitBaseName"'.log'
echo $'\n\n\n\n\n' >> "$logFile"
#Sending mail with the dump file
mailTempHeader='Deployment'
mailTempConcern='Hey there'
mailTempBodyTitle='Please find the <b>'"${gitBaseName^}"'</b> repository deployment details, '
mailTempBody=$(grep -Pzo '.*Service Information(.*\n)*' $logFilePath$gitBaseName'.log' | head -n -4 | tr '\r\n' '\t' | sed 's/\t/ <br \/> /g')
#mailTempBody=$(grep -Pzo '.*Service Information(.*\n)*' $logFilePath$gitBaseName'.log' | head -n -4 | sed ':a;N;$!ba;s/\r\n/<1br \/>/g')
mailTempFooter='Please find the attached file for further details. <p style="font-size: 14px;color: cornflowerblue;">IF YOU HAVE ANY QUESTIONS OR CONCERNS, PLEASE CONTACT <a href="http://bugzilla.aceturtle.net/" target="_blank"><b>DevOps</b></a></p>'
sh "$mailTemplFile" "$mailTempHeader" "$mailTempConcern" "$mailTempBodyTitle" "$mailTempBody" "$mailTempFooter" | mutt -e "set from=noreply@aceturtle.com" -e "set realname=Deployment" -e 'set content_type="text/html"' -s "${gitBaseName^} Deployment Logs: $(date +%d%b%Y-%T)" -a "$logFilePath$gitBaseName"'.log' -- $(echo ${gitUserEmail[@]} | tr ' ' ,)
rm -f "$logFilePath$gitBaseName"'.log'
rm -f "$logFile"
#END default line