-
Notifications
You must be signed in to change notification settings - Fork 4
/
deploy.sh
executable file
·222 lines (188 loc) · 5.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env bash
#
# HipstaDeploy
# ~~~~~~~~~~~~
#
# A Bash script for deploying stuff on Amazon CloudFront.
#
# Copyright: (c) 2014 by Gianluca Bargelli <[email protected]>
# License: MIT
#
COLOR_GREEN="\e[0;32m"
COLOR_RESET="\e[0m"
CHECKMARK_SYMBOL="✔"
XMARK_SYMBOL="✘"
COLOR_RED="\e[0;33m"
VERSION="v0.2.0"
# Version
function usage {
echo "HipstaDeploy $VERSION. Static site deployment on Amazon Cloudfront."
echo ""
printf "Usage: $0 [-u url] [-p path] [-r remote URL]\n"
echo ""
echo "Options:"
printf " -h\tShows this screen.\n"
printf " -u\tURL of your website [default: http://localhost:2368].\n"
printf " -o\tOutput folder of the generated static pages [default: _site].\n"
printf " -r\tURL of remote site (used for RSS feed find/replace) [no default].\n"
}
function show_banner {
printf '
%b______ ______ _____ ________ ______
%b___ / / /__(_)________________ /______ ___ __ \_______________ /__________ __
%b__ /_/ /__ /___ __ \_ ___/ __/ __ `/_ / / / _ \__ __ \_ /_ __ \_ / / /
%b_ __ / _ / __ /_/ /(__ )/ /_ / /_/ /_ /_/ // __/_ /_/ / / / /_/ / /_/ /
%b/_/ /_/ /_/ _ .___//____/ \__/ \__,_/ /_____/ \___/_ .___//_/ \____/_\__, /
/_/ /_/ /____/' $(tput setaf 164) $(tput setaf 163) $(tput setaf 162) $(tput setaf 161) $(tput setaf 160)
printf $COLOR_RESET
echo ""
echo " The Amazing Static Site Deploy Script™! "
echo "==================================================================================
"
}
function display_green_check {
printf $COLOR_GREEN
echo "$CHECKMARK_SYMBOL $1"
printf $COLOR_RESET
}
function display_red_check {
printf $COLOR_RED
echo "$XMARK_SYMBOL $1"
printf $COLOR_RESET
}
function generate_static_site {
SECTION_NAME="Generate static files"
CMD="wget --recursive \
--convert-links \
--page-requisites \
--no-parent \
--directory-prefix $OUTPUT_FOLDER \
--no-host-directories $SITE_URL"
if [[ $(eval $CMD "2>&1 > /dev/null") != 0 && $? == 8 ]]; then
display_red_check "Got some 404 while generating static files."
echo ""
printf "Do you want to continue? %b[Y/n]%b: " $(tput setaf 12) ${COLOR_RESET}
read CHOICE
if [[ "$CHOICE" = "N" || "$CHOICE" = "n" ]]; then
echo ""
display_red_check "$SECTION_NAME"
show_footer
exit 0
fi
elif [[ $? == 0 ]]; then
display_red_check "$SECTION_NAME"
exit 1
fi
display_green_check "$SECTION_NAME"
}
function remove_assets_version {
SECTION_NAME="Rename Assets Version"
for f in $(find . -type f | grep "?v=" | uniq)
do
mv "$f" $(echo "$f" | sed "s/?v=.*//g")
done
display_green_check "$SECTION_NAME"
}
function rename_links {
grep -l -R "" * | xargs sed -i".bak" 's/index\.html//'
find . -type f | grep "bak$" | xargs rm -rf
display_green_check "Rename links"
}
function fix_rss {
if [ -n "$PUBLISH_URL" ]; then
ruby -e "files = Dir.glob('$OUTPUT_FOLDER' + 'rss/*') << Dir.glob('$OUTPUT_FOLDER' + 'rss*') << Dir.glob('$OUTPUT_FOLDER' + 'feed/*') << Dir.glob('$OUTPUT_FOLDER' + 'feed*'); files.each { |file_name| if file_name.kind_of?(String) then text = File.read(file_name); replace = text.gsub!('$SITE_URL', '$PUBLISH_URL'); File.open(file_name, 'w') { |file| file.puts replace } end }"
display_green_check "Fixing RSS feed"
else
display_red_check "Fixing RSS feed (no publish url; skipping)"
fi
}
function deploy {
SECTION_NAME="Deploy to Amazon CloudFront"
CMD="s3_website push --site=$OUTPUT_FOLDER"
echo ""
printf "Do you want to deploy your static site on Amazon CloudFront? %b[Y/n]%b: " $(tput setaf 12) ${COLOR_RESET}
read DEPLOY
if [[ "$DEPLOY" = "N" || "$DEPLOY" = "n" ]]; then
echo ""
display_red_check "$SECTION_NAME"
return
fi
while IFS= read -r line; do
echo $line
if [[ $line == ERROR* ]]
then
echo -e "\n\n"
echo "Exiting on s3_website push error."
display_red_check "$SECTION_NAME"
return
exit 0
fi
done < <($CMD 2>&1)
display_green_check "$SECTION_NAME"
}
function show_footer {
END=$(date +%s)
echo ""
echo "=================================================================================="
echo ""
TOTAL_TIME=$(echo $((END-START)) | awk '{printf "%d:%02d:%02d", $1/3600, ($1/60)%60, $1%60}')
printf $COLOR_RED
printf "Done!$COLOR_RESET Total time was $TOTAL_TIME\n"
}
function jsonval {
prop=$1
temp1=`echo $CONFIG | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | \
awk '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | \
sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -iw $prop`
temp2=`IFS=':' read -r value string <<< "$temp1"; echo $string`
echo "${temp2##*|}"
}
function read_config {
CONFIG=$(<Hipstafile)
SITE_URL=$(jsonval 'SITE_URL')
OUTPUT_FOLDER=$(jsonval 'OUTPUT_FOLDER')
PUBLISH_URL=$(jsonval 'PUBLISH_URL')
echo $SITE_URL
echo $OUTPUT_FOLDER
echo $PUBLISH_URL
}
function main {
START=$(date +%s)
read_config
show_banner
generate_static_site
remove_assets_version
rename_links
fix_rss
deploy
show_footer
}
while getopts ":u:o:r:h" OPT; do
case $OPT in
h)
usage
exit 0
;;
u)
SITE_URL="$OPTARG"
;;
o)
OUTPUT_FOLDER="$OPTARG"
;;
r)
PUBLISH_URL="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
exit 1
;;
esac
done
main
exit 0