-
Notifications
You must be signed in to change notification settings - Fork 14
/
install.sh
executable file
·333 lines (268 loc) · 12 KB
/
install.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
################################################################################
# Variables you should adjust for your setup
################################################################################
APPHOST=12.34.56.78
SERVICENAME=meteor_app
################################################################################
# Internal variables
################################################################################
MAINUSER=$(whoami)
MAINGROUP=$(id -g -n $MAINUSER)
GITBAREREPO=/home/$MAINUSER/$SERVICENAME.git
EXPORTFOLDER=/tmp/$SERVICENAME
APPFOLDER=/home/$MAINUSER/$SERVICENAME
APPBINFOLDER=$APPFOLDER/bin
APPENV=$APPFOLDER/env.sh
APPCONFIGBASE=$APPFOLDER/config
################################################################################
# Utility functions
################################################################################
function replace {
sudo perl -0777 -pi -e "s{\Q$2\E}{$3}gm" "$1"
}
function replace_noescape {
sudo perl -0777 -pi -e "s{$2}{$3}gm" "$1"
}
function symlink {
if [ ! -f $2 ]
then
sudo ln -s "$1" "$2"
fi
}
function append {
echo -e "$2" | sudo tee -a "$1" > /dev/null
}
################################################################################
# Task functions
################################################################################
function apt_update_upgrade {
echo "--------------------------------------------------------------------------------"
echo "Update and upgrade all packages"
echo "--------------------------------------------------------------------------------"
sudo apt-get -y update
sudo apt-get -y upgrade
}
function install_fail2ban {
echo "--------------------------------------------------------------------------------"
echo "Install fail2ban"
echo "--------------------------------------------------------------------------------"
# Reference: http://plusbryan.com/my-first-5-minutes-on-a-server-or-essential-security-for-linux-servers
sudo apt-get -y install fail2ban
}
function configure_firewall {
echo "--------------------------------------------------------------------------------"
echo "Configure firewall"
echo "--------------------------------------------------------------------------------"
# Reference: http://plusbryan.com/my-first-5-minutes-on-a-server-or-essential-security-for-linux-servers
sudo ufw allow 22
sudo ufw allow 80
sudo ufw allow 443
}
function configure_automatic_security_updates {
echo "--------------------------------------------------------------------------------"
echo "Configure automatic security updates"
echo "--------------------------------------------------------------------------------"
# Reference: http://plusbryan.com/my-first-5-minutes-on-a-server-or-essential-security-for-linux-servers
# Note that you will still need to do your own restarts. Uncomment this line if you
# would like to have your server restarted automatically at 11:59pm on Sundays:
#append "/etc/crontab" "59 23\t* * 7\troot\t/sbin/shutdown -r now >> /dev/null 2>&1"
sudo apt-get -y install unattended-upgrades
replace "/etc/apt/apt.conf.d/10periodic" \
'APT::Periodic::Download-Upgradeable-Packages "0";
APT::Periodic::AutocleanInterval "0";' \
'APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::AutocleanInterval "7";
APT::Periodic::Unattended-Upgrade "1";'
}
function install_git {
echo "--------------------------------------------------------------------------------"
echo "Install Git"
echo "--------------------------------------------------------------------------------"
sudo apt-get -y install git-core
sudo git config --system user.email "$MAINUSER@$APPHOST"
sudo git config --system user.name "$MAINUSER"
}
function install_nodejs {
echo "--------------------------------------------------------------------------------"
echo "Install Node.js"
echo "--------------------------------------------------------------------------------"
sudo apt-get -y install python-software-properties
sudo add-apt-repository -y ppa:chris-lea/node.js
sudo apt-get -y update
sudo apt-get -y install nodejs
}
function install_mongodb {
echo "--------------------------------------------------------------------------------"
echo "Install MongoDB"
echo "--------------------------------------------------------------------------------"
sudo apt-get -y install mongodb
}
function install_meteor {
echo "--------------------------------------------------------------------------------"
echo "Install Meteor"
echo "--------------------------------------------------------------------------------"
curl https://install.meteor.com | /bin/sh
}
function install_meteorite {
echo "--------------------------------------------------------------------------------"
echo "Install Meteorite"
echo "--------------------------------------------------------------------------------"
sudo -H npm install meteorite -g
}
function install_phantomjs {
echo "--------------------------------------------------------------------------------"
echo "Install PhantomJS"
echo "--------------------------------------------------------------------------------"
sudo apt-get -y install fontconfig
sudo npm install phantomjs -g
}
function setup_app_skeleton {
echo "--------------------------------------------------------------------------------"
echo "Setup app skeleton"
echo "--------------------------------------------------------------------------------"
rm -rf $APPFOLDER
mkdir -p $APPBINFOLDER
touch $APPBINFOLDER/main.js
}
function setup_app_service {
echo "--------------------------------------------------------------------------------"
echo "Setup app service"
echo "--------------------------------------------------------------------------------"
local SERVICEFILE=/etc/init/$SERVICENAME.conf
local LOGFILE=/var/log/$SERVICENAME.log
sudo rm -f $SERVICEFILE
append $SERVICEFILE "description \"$SERVICENAME\""
append $SERVICEFILE "author \"Mathieu Bouchard <matb33@gmail.com>\""
append $SERVICEFILE "start on runlevel [2345]"
append $SERVICEFILE "stop on restart"
append $SERVICEFILE "respawn"
append $SERVICEFILE "pre-start script"
append $SERVICEFILE " echo \"[\$(/bin/date -u +%Y-%m-%dT%T.%3NZ)] (sys) Starting\" >> $LOGFILE"
append $SERVICEFILE "end script"
append $SERVICEFILE "pre-stop script"
append $SERVICEFILE " rm -f /var/run/$SERVICENAME.pid"
append $SERVICEFILE " echo \"[$(/bin/date -u +%Y-%m-%dT%T.%3NZ)] (sys) Stopping\" >> $LOGFILE"
append $SERVICEFILE "end script"
append $SERVICEFILE "script"
append $SERVICEFILE " sleep 5"
append $SERVICEFILE " echo \$\$ > /var/run/$SERVICENAME.pid"
append $SERVICEFILE " exec bash -c 'cd $APPFOLDER && source $APPENV && exec /usr/bin/node $APPBINFOLDER/main.js >> \"$LOGFILE\" 2>&1'"
append $SERVICEFILE "end script"
}
function setup_bare_repo {
echo "--------------------------------------------------------------------------------"
echo "Setup bare repo"
echo "--------------------------------------------------------------------------------"
rm -rf $GITBAREREPO
mkdir -p $GITBAREREPO
cd $GITBAREREPO
git init --bare
git update-server-info
}
function setup_post_update_hook {
echo "--------------------------------------------------------------------------------"
echo "Setup post update hook"
echo "--------------------------------------------------------------------------------"
local HOOK=$GITBAREREPO/hooks/post-receive
local RSYNCSOURCE=$EXPORTFOLDER/app_rsync
rm -f $HOOK
append $HOOK "#!/bin/bash"
append $HOOK "unset \$(git rev-parse --local-env-vars)"
append $HOOK "echo \"------------------------------------------------------------------------\""
append $HOOK "echo \"Exporting app from git repo\""
append $HOOK "echo \"------------------------------------------------------------------------\""
append $HOOK "sudo rm -rf $EXPORTFOLDER"
append $HOOK "mkdir -p $EXPORTFOLDER"
append $HOOK "while read oldrev newrev refname"
append $HOOK "do"
append $HOOK " BRANCH=\$(git rev-parse --symbolic --abbrev-ref \$refname)"
append $HOOK " git clone --recursive \"\$PWD\" -b \$BRANCH $EXPORTFOLDER"
append $HOOK " rm -rf $EXPORTFOLDER/.git*"
append $HOOK "done"
append $HOOK "echo \"------------------------------------------------------------------------\""
append $HOOK "echo \"Updating environment variable files\""
append $HOOK "echo \"------------------------------------------------------------------------\""
append $HOOK "mkdir -p $APPCONFIGBASE/\$BRANCH"
append $HOOK "cp -f $EXPORTFOLDER/config/\$BRANCH/env.sh $APPENV"
append $HOOK "cp -f $EXPORTFOLDER/config/\$BRANCH/settings.json $APPCONFIGBASE/\$BRANCH/settings.json"
append $HOOK "echo \"------------------------------------------------------------------------\""
append $HOOK "echo \"Bundling app as a standalone Node.js app\""
append $HOOK "echo \"------------------------------------------------------------------------\""
append $HOOK "mkdir -p $RSYNCSOURCE"
append $HOOK "cd $EXPORTFOLDER"
append $HOOK "if [ -f $EXPORTFOLDER/smart.json ]; then"
append $HOOK " mrt build --directory $RSYNCSOURCE"
append $HOOK "else"
append $HOOK " meteor build --directory $RSYNCSOURCE"
append $HOOK "fi"
append $HOOK "if [ -f $RSYNCSOURCE/bundle/main.js ]; then"
append $HOOK " echo \"------------------------------------------------------------------------\""
append $HOOK " echo \"Adjust bundle permissions\""
append $HOOK " echo \"------------------------------------------------------------------------\""
append $HOOK " sudo find \"$RSYNCSOURCE/bundle\" -type f -exec chmod 644 {} +;"
append $HOOK " sudo find \"$RSYNCSOURCE/bundle\" -type d -exec chmod 755 {} +;"
append $HOOK " echo \"------------------------------------------------------------------------\""
append $HOOK " echo \"Run npm install\""
append $HOOK " echo \"------------------------------------------------------------------------\""
append $HOOK " (cd $RSYNCSOURCE/bundle/programs/server && npm install)"
append $HOOK " echo \"------------------------------------------------------------------------\""
append $HOOK " echo \"Rsync standalone app to active app location\""
append $HOOK " echo \"------------------------------------------------------------------------\""
append $HOOK " rsync --checksum --recursive --update --delete --times $RSYNCSOURCE/bundle/ $APPBINFOLDER/"
append $HOOK " echo \"------------------------------------------------------------------------\""
append $HOOK " echo \"Restart app\""
append $HOOK " echo \"------------------------------------------------------------------------\""
append $HOOK " sudo service $SERVICENAME restart"
# Clean-up
append $HOOK " cd $APPBINFOLDER"
append $HOOK " sudo rm -rf $EXPORTFOLDER"
append $HOOK "else"
append $HOOK " echo \"Oops... couldn't find main.js\""
append $HOOK "fi"
append $HOOK "echo \"\n\n--- Done.\""
sudo chown $MAINUSER:$MAINGROUP $HOOK
chmod +x $HOOK
}
function show_conclusion {
echo -e "\n\n\n\n\n"
echo "########################################################################"
echo " On your local development server"
echo "########################################################################"
echo ""
echo "Add remote repository:"
echo "$ git remote add ec2 $MAINUSER@$APPHOST:$SERVICENAME.git"
echo ""
echo "Add to your ~/.ssh/config:"
echo -e "Host $APPHOST\n IdentityFile PRIVATE_KEY_YOU_GOT_FROM_AWS.pem"
echo ""
echo "To deploy:"
echo "$ git push ec2 master"
echo ""
echo "########################################################################"
echo " Manual commands to run to finish off installation"
echo "########################################################################"
echo ""
echo "Run the following command:"
echo "$ sudo ufw enable"
echo ""
echo "Reboot to complete the installation. Example:"
echo "$ sudo reboot"
echo ""
}
################################################################################
apt_update_upgrade
install_fail2ban
configure_firewall
configure_automatic_security_updates
install_git
install_nodejs
install_mongodb
install_meteor
install_meteorite
install_phantomjs
setup_app_skeleton
setup_app_service
setup_bare_repo
setup_post_update_hook
show_conclusion