-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprovision.sh
336 lines (289 loc) · 10.7 KB
/
provision.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
334
335
336
#!/usr/bin/env bash
## Develop with errors on.
#set -e
#
# Vagrant bootstrap file for building development environment.
#
PROJECT_ROOT="vagrant"
# Create new project directory in sites/
PROJECT_VHOST_DIR="develop.vagrant.dev"
if [[ ! -d /vagrant/sites/$PROJECT_VHOST_DIR ]]; then
mkdir -pv /vagrant/sites/$PROJECT_VHOST_DIR
cp /vagrant/sites/phpinfo.php /vagrant/sites/$PROJECT_VHOST_DIR/index.php
fi
# Create project variables
USER_USER="vagrant"
USER_GROUP=$USER_USER
DB_NAME="develop"
DB_USER="root"
DB_PASS=""
# Add vagrantshell bin to path
#chmod -R +x /$PROJECT_ROOT/bin
#export PATH="/$PROJECT_ROOT/bin:$PATH"
# Generate provision files to prevent rebuilding every time.
VAGRANT_PROVISION_FIRST="/$PROJECT_ROOT/tmp/vagrant-provision.first"
VAGRANT_PROVISION_DONE="/$PROJECT_ROOT/tmp/vagrant-provision.done"
# If this is a brand new VM, override provisioning.done
if ! [ -f $VAGRANT_PROVISION_FIRST ]; then
rm -f $VAGRANT_PROVISION_DONE
fi
if [ -f $VAGRANT_PROVISION_DONE ]; then
echo -e "Box is already provisioned. Delete the $VAGRANT_PROVISION_DONE file to rebuild on vagrant up."
# for some reason the nginx daemon is not starting on boot, though
# it's configured to, so just boot it here.
echo "Restarting services."
/etc/init.d/nginx restart
/etc/init.d/php-fpm restart
/etc/init.d/mysql restart
/etc/init.d/memcached restart
/etc/init.d/redis restart
exit 0;
fi
mkdir -pv /var/log/mysql
chown mysql:mysql /var/log/mysql
# Set timezone
mv /etc/localtime /etc/localtime.bak
ln -nsfv /usr/share/zoneinfo/EST5EDT /etc/localtime
# Update base box
echo "Updating current software."
yum -y update
# Install missing repos
echo "Installing repos for epel, IUS, Percona, nginx."
yum -y install epel-release
yum -y install https://centos6.iuscommunity.org/ius-release.rpm
yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
yum -y install http://yum.newrelic.com/pub/newrelic/el5/x86_64/newrelic-repo-5-3.noarch.rpm
yum -y install https://repo.varnish-cache.org/redhat/varnish-3.0.el6.rpm
yum -y install http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# Switch to mainline Nginx version in repo file.
#sed -i -e 's/packages\/centos/packages\/mainline\/centos/g' /etc/yum.repos.d/nginx.repo
# Install all software needed for machine
echo "Installing base software."
PHP_VERSION="php56u"
# Smaller footprint. 66M downloaded.
yum -y groupinstall "Development Tools"
# Install some essentials. 165MB downloaded.
yum -y install \
yum-utils yum-plugin-replace \
vim vim-common vim-enhanced vim-minimal htop mytop nmap at wget \
openssl openssl-devel curl libcurl libcurl-devel lsof tmux bash-completion \
gpg lynx memcached memcached-devel nginx npm pv parted ca-certificates \
setroubleshoot atop autofs bind-utils tuned cachefilesd symlinks \
$PHP_VERSION \
$PHP_VERSION-devel $PHP_VERSION-common $PHP_VERSION-gd $PHP_VERSION-imap \
$PHP_VERSION-mbstring $PHP_VERSION-mcrypt $PHP_VERSION-mhash \
$PHP_VERSION-mysql $PHP_VERSION-pear $PHP_VERSION-pecl-memcached \
$PHP_VERSION-pecl-memcached-debuginfo $PHP_VERSION-pecl-xdebug \
$PHP_VERSION-xml $PHP_VERSION-pdo $PHP_VERSION-fpm $PHP_VERSION-opcache \
$PHP_VERSION-cli $PHP_VERSION-pecl-jsonc $PHP_VERSION-devel \
$PHP_VERSION-pecl-geoip $PHP_VERSION-pecl-redis \
$PHP_VERSION-pecl-mongo mongodb mongodb-server \
$PHP_VERSION-ioncube-loader \
Percona-Server-client-56 Percona-Server-server-56 \
percona-toolkit percona-xtrabackup mysql-utilities mysqlreport mysqltuner \
varnish redis \
make patch wget mysql-devel pcre-devel \
gd-devel libxml2-devel expat-devel libicu-devel bzip2-devel oniguruma-devel \
openldap-devel readline-devel libc-client-devel libcap-devel binutils-devel \
pam-devel elfutils-libelf-devel ImageMagick-devel libxslt-devel libevent-devel \
libcurl-devel libmcrypt-devel tbb-devel libdwarf-devel
# Latest version of Node.js
curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
yum -y install nodejs
# This will be 1.2GB downloaded.
# Install groups of software. Some of the essentials below will already be
# included in these groups, but in case you ever want to shrink the size of the
# install, these can be removed. "Development Tools" should always be installed.
#yum -y --setopt=group_package_types=mandatory,default,optional groupinstall \
#"Base" "Development Tools" "Console internet tools" "Debugging Tools" \
#"Networking Tools" "Performance Tools"
# Essentials for compiling a number of projects, but mostly unnecessary for Web.
#yum -y install \
#zlib-devel cmake expect lua rpm-build rpm-devel autoconf automake gcc \
#svn cpp make libtool patch gcc-c++ boost-devel mysql-devel pcre-devel \
#gd-devel libxml2-devel expat-devel libicu-devel bzip2-devel oniguruma-devel \
#openldap-devel readline-devel libc-client-devel libcap-devel binutils-devel \
#pam-devel elfutils-libelf-devel ImageMagick-devel libxslt-devel libevent-devel \
#libcurl-devel libmcrypt-devel tbb-devel libdwarf-devel
# Clean yum
yum clean all
# Ensure nginx's terrible default configs are blown away.
rm -rf /etc/nginx/conf.d
# Map configs into core.
source /vagrant/bin/vshell map
# Set SELinux to permissive mode for Nginx
# This is done because for a virtual environment, we do not want SELINUX to be
# overriding permissions.
# TODO read this: http://nginx.com/blog/nginx-se-linux-changes-upgrading-rhel-6-6/
#echo -e "Setting SELinux enforcing of Nginx policy to permissive mode."
#semanage permissive -a httpd_t
echo -e "Disabling SELinux."
setenforce 0
sed -i -e 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
sed -i -e 's/SELINUX=permissive/SELINUX=disabled/g' /etc/sysconfig/selinux
# Installing PHP composer...
echo "Installing Composer."
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
# SSH
echo -e "Copying Vagrant SSH keys."
mkdir -pv ~/.ssh
mkdir -pv /home/$USER_USER/.ssh
cp -rf /$PROJECT_ROOT/ssh/* ~/.ssh/
cp -rf /$PROJECT_ROOT/ssh/* /home/$USER_USER/.ssh/
# Generating new one for those with zero knowledge of how this works. This can
# be automatically renamed to id_rsa in a post-provision script.
ssh-keygen -b 4096 -f /home/$USER_USER/.ssh/vagrantshell.id_rsa -C vagrantshell@4096_`date +%Y-%m-%d-%H%M%S` -N ""
# Permissions
echo -e "Setting permissions for $USER_USER:$USER_GROUP and root:root."
chown -R $USER_USER:$USER_GROUP /home/$USER_USER
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*
chmod 700 /home/$USER_USER/.ssh
chmod 600 /home/$USER_USER/.ssh/*
# Installing PECL Scrypt extension for PHP...
echo "Installing PECL Scrypt extension for PHP."
pecl install scrypt
# Installing PECL Http 1.7.6 extension for PHP...
echo "Installing PECL Http 1.7.6 extension for PHP."
pecl install http://pecl.php.net/get/pecl_http-1.7.6.tgz
# Tuning
tuned-adm profile latency-performance
cachefilesd -f /etc/cachefilesd.conf
modprobe cachefiles
service cachefilesd start
echo "Adding services to boot."
chkconfig nginx on
chkconfig mysql on
chkconfig php-fpm on
chkconfig memcached on
chkconfig redis on
chkconfig iptables off
chkconfig ip6tables off
chkconfig cachefilesd on
chkconfig mongod on
# Start services
echo "Starting/stopping services."
/etc/init.d/nginx restart
/etc/init.d/mysql restart
/etc/init.d/php-fpm restart
/etc/init.d/memcached restart
/etc/init.d/redis restart
/etc/init.d/iptables stop
/etc/init.d/ip6tables stop
/etc/init.d/mongod restart
echo "Waiting for Percona MySQL."
while ! service mysql status | grep -q running; do
sleep 1
done
# Set database user credentials
echo "Setting up DB, and granting all privileges to '$DB_USER'@'%'."
mysql -u $DB_USER --password="$DB_PASS" -e "GRANT ALL PRIVILEGES ON *.* TO '$DB_USER'@'%' WITH GRANT OPTION"
mysql -u $DB_USER --password="$DB_PASS" -e "DROP DATABASE IF EXISTS $DB_NAME; CREATE DATABASE $DB_NAME"
echo -e 'Updating Git.'
yum -y replace git --replace-with git2u
echo -e "Updating rsync."
yum -y replace rsync --replace-with rsync31u
# Symlink vshell utility into PATH for root and vagrant users.
echo -e "Add vshell utility to PATH."
if [[ ! -d "$HOME/bin" ]]; then
mkdir -pv "$HOME/bin"
fi
ln -s /vagrant/bin/vshell $HOME/bin
if [[ ! -d "/home/$USER_USER/bin" ]]; then
mkdir -pv "/home/$USER_USER/bin"
fi
ln -s /vagrant/bin/vshell /home/$USER_USER/bin
# Set permissions on regular user.
echo -e "Setting permissions for $USER_USER:$USER_GROUP on /home/$USER_USER"
chown -R $USER_USER:$USER_GROUP /home/$USER_USER
# Generate install files to prevent reinstalls.
echo -e "Cleaning install."
touch $VAGRANT_PROVISION_FIRST
touch $VAGRANT_PROVISION_DONE
yum -y clean all
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo -e "\n\nProvisioning complete!"
echo -e "--------------------------------------------------------------------------------"
echo "$PROJECT_ROOT provisioning complete."
echo -e "\nDB:"
echo " User: '$DB_USER'@'%'"
echo " Pass: $DB_PASS"
echo " DBName: $DB_NAME"
echo " Addr: 192.168.80.80"
echo " Port: guest 3306 -> host 3306"
echo -e "\nWeb:"
echo " guest :80 -> host :80"
echo " guest :443 -> host :443"
echo -e "\nSSH:"
echo " User: $USER_USER"
echo " Group: $USER_GROUP"
echo " root access: 'sudo su'"
echo " guest :22 -> host :4444"
echo -e "\nRemember to set /etc/hosts (or C:\Windows\System32\Drivers\etc\hosts):"
echo " 192.168.80.80 vagrant.dev develop.vagrant.dev"
echo -e "\nFor any questions: Dane MacMillan <[email protected]>"
echo -e "This vagrant box was provisioned using: https://github.com/danemacmillan/vagrantshell"
echo -e "--------------------------------------------------------------------------------"
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
# Post-provision
# --------------
# Import sql files
DB_DUMP=/$PROJECT_ROOT/post-provision/*.sql
shopt -s nullglob
for dbdump in $DB_DUMP
do
if [[ -f "$dbdump" ]]; then
echo -e "Importing sql file into DB $DB_NAME: $dbdump"
mysql -u $DB_USER --password="$DB_PASS" -f $DB_NAME < "$dbdump"
echo " "
fi
done
# Execute scripts
# Note: dotfiles are installed in post-provision
POST_PROVISION=/$PROJECT_ROOT/post-provision/*.sh
shopt -s nullglob
for pp in $POST_PROVISION
do
if [[ -f "$pp" ]]; then
echo -e "Running post-provision script: $pp"
source "$pp"
echo " "
fi
done
# Extra
# to change hostname
# vi /etc/sysconfig/network
# HOSTNAME=vagrant.dev
# hostname vagrant.dev
# vi /etc/hosts
# 192.168.80.80 develop.vagrant.dev
# /etc/init.d/network restart
#to change to httpd worker
#/etc/sysconfig/httpd
#uncomment the worker line.
# Append httpd.conf
#echo "Appending httpd.conf file"
#bash -c "echo 'Include /vagrant/httpd/*.httpd.conf' >> /etc/httpd/conf/httpd.conf"
# Give permissions to fcgid wapper.
#echo -e "Giving php.fcgi 777 permissions."
#chmod 777 /$PROJECT_ROOT/include/config/httpdconf/dev/fastcgi/php.fcgi