-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.php
344 lines (289 loc) · 10.5 KB
/
deploy.php
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
337
338
339
340
341
342
343
344
<?php
namespace Deployer;
use Deployer\Task\Context;
require 'recipe/laravel.php';
require './.deployer/config.php';
task('info', function (){
writeln('<info>Host: ' . run('whoami') . '@' . run('hostname') . '</info>');
$serverIpAddress = run('hostname -I');
writeln('<info>IP: ' . $serverIpAddress . '</info>');
set('server_ip', explode(' ', $serverIpAddress)[0]);
writeln('<info>Deploy path: {{deploy_path}}</info>');
});
task('check:provision', function (){
if ( ! test('[ -e /home/deployer/.provisioned ]')) {
writeln('<comment>Server is not provisioned. Starting provision...</comment>');
invoke('server:provision');
}
invoke('server:user');
});
task('check:domain', function (){
$domainIpAddress = gethostbyname(get('domain'));
if ( ! get('server_ip', '')) {
invoke('info');
}
if (get('server_ip') !== $domainIpAddress) {
writeln('<error>Server IP and Domain IP doesn\'t match. It will create issues while generating ssl certificate. Hence exiting...</error>');
exit;
}
});
task('npm:install', function (){
if (has('previous_release')) {
if (test('[ -d {{previous_release}}/node_modules ]')) {
run('cp -R {{previous_release}}/node_modules {{release_path}}');
}
}
run('cd {{release_path}} && npm install');
});
task('build', function (){
run('cd {{release_path}} && npm run production');
});
/**
* Upload .env.production file as .env
*/
task('env', function (){
$env = file_exists('.deployer/.env.' . get('stage') . '.env') ? file_get_contents('.deployer/.env.' . get('stage') . '.env') : '';
if(! $env){
$env = file_exists('.deployer/.env') ? file_get_contents('.deployer/.env') : '';
}
if ($env) {
file_put_contents('.env.' . get('stage') . '_compiled.env', parse($env));
upload('.env.' . get('stage') . '_compiled.env', '{{release_path}}/.env');
unlink('.env.' . get('stage') . '_compiled.env');
}else {
writeln('<error>Env not found</error>');
}
});
task('artisan:queue:restart', function (){
invoke('supervisor:restart');
$output = run('php {{deploy_path}}/current/artisan queue:restart');
writeln('<info>' . $output . '</info>');
});
task('supervisor:restart', function (){
$output = run('sudo service supervisor restart');
writeln('<info>' . $output . '</info>');
});
task('supervisor', function (){
invoke('server:user');
$data = file_exists('.deployer/supervisor.conf') ? file_get_contents('.deployer/supervisor.conf') : '';
if ($data) {
file_put_contents('supervisor_compiled.conf', parse($data));
invoke('server:root_user');
upload('supervisor_compiled.conf', '/etc/supervisor/conf.d/{{application}}.conf');
invoke('server:user');
unlink('supervisor_compiled.conf');
}else {
writeln('<error>Data not found</error>');
}
});
task('supervisor:restart', function (){
run('sudo service supervisor restart');
});
task('vhost', function (){
$data = file_exists('.deployer/host.conf') ? file_get_contents('.deployer/host.conf') : '';
if ($data) {
file_put_contents('host_compiled.conf', parse($data));
invoke('server:root_user');
upload('.deployer/letsencrypt.conf', '/etc/nginx/snippets/letsencrypt.conf');
upload('.deployer/ssl-params.conf', '/etc/nginx/snippets/ssl-params.conf');
upload('host_compiled.conf', '/etc/nginx/sites-available/{{domain}}.conf');
unlink('host_compiled.conf');
run('sudo test -e /etc/nginx/sites-enabled/{{domain}}.conf || sudo ln -s /etc/nginx/sites-available/{{domain}}.conf /etc/nginx/sites-enabled/{{domain}}.conf');
invoke('server:user');
run('sudo nginx -t');
run('sudo service nginx reload');
}else {
writeln('<error>Data not found</error>');
}
});
task('database', function (){
run('mysql -uroot -p' . get('mysql_password') . ' -e "CREATE DATABASE IF NOT EXISTS ' . get('mysql_database') . '"');
});
/**
* Server related scripts
*/
task('server:info', function (){
writeln('<info>Host: ' . run('whoami') . '@' . run('hostname') . '</info>');
writeln('<info>IP: ' . run('hostname -I') . '</info>');
});
task('server:init', function (){
if (test('[ -e /home/deployer/.provisioned ]')) {
run('sudo rm /home/deployer/.provisioned');
}
});
task('server:update', function (){
run('sudo apt-get update');
})->desc('Update server');
task('server:common', function (){
run('sudo apt-get install -y openssl acl software-properties-common');
run('export LC_ALL="en_US.UTF-8"');
run('export LC_CTYPE="en_US.UTF-8"');
});
task('server:nginx', function(){
run('sudo apt-get install -y nginx');
});
task('server:curl', function(){
run('sudo apt-get install -y zip unzip');
});
task('server:zip', function(){
run('sudo apt-get install -y curl');
});
task('server:git', function (){
run('sudo apt-get install -y git');
within('~', function (){
if (get('git_username')) {
run('git config --global user.name "' . get('git_username') . '"');
}
if (get('git_email')) {
run('git config --global user.email "' . get('git_email') . '"');
}
});
});
task('server:node_js', function (){
run('curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - && sudo apt-get install -y nodejs');
writeln('The node version is: ' . run('node -v'));
writeln('The NPM version is: ' . run('npm -v'));
});
task('server:redis', function (){
run('sudo apt-get install -y redis-server && update-rc.d redis-server enable && update-rc.d redis-server defaults');
});
task('server:supervisor', function (){
run('sudo apt-get install -y supervisor');
});
task('server:php', function (){
run('sudo add-apt-repository -y ppa:ondrej/php');
invoke('server:update');
run('sudo apt-get install -y php7.2 php7.2-common php7.2-cli php7.2-fpm php7.2-curl php7.2-json php7.2-tidy php7.2-mysql php7.2-gd php7.2-xml php7.2-zip php7.2-mbstring php7.2-dom php7.2-mysql php7.2-imap php7.2-bcmath php-apcu php7.2-intl');
run("sudo sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/7.2/fpm/php.ini");
run("sudo sed -i 's/memory_limit = 128M/memory_limit = 256M/g' /etc/php/7.2/fpm/php.ini");
run("sudo sed -i 's/max_execution_time = 30/max_execution_time = 120/g' /etc/php/7.2/fpm/php.ini");
});
task('server:mysql', function (){
run('echo "mysql-server-5.7 mysql-server/root_password password ' . get('mysql_password', 'root') . '" | sudo debconf-set-selections');
run('echo "mysql-server-5.7 mysql-server/root_password_again password ' . get('mysql_password', 'root') . '" | sudo debconf-set-selections');
run('sudo apt-get -y install mysql-server-5.7');
if (get('mysql_database')) {
run('mysql -uroot -p' . get('mysql_password') . ' -e "CREATE DATABASE IF NOT EXISTS ' . get('mysql_database') . '"');
}
});
task('server:composer', function (){
run('curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer');
// SWAP memory for composer
if (get('swap_memory_size')) {
run('sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count={{swap_memory_size}}');
run('sudo /sbin/mkswap /var/swap.1');
run('sudo /sbin/swapon /var/swap.1');
}
});
task('server:certbot', function (){
run('sudo add-apt-repository ppa:certbot/certbot');
invoke('server:update');
run('sudo apt-get install -y certbot python-certbot-nginx');
run('sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048');
});
task('server:deployer', function (){
try{
run('grep -c \'^deployer:\' /etc/passwd');
}catch (\Exception $e){
run('sudo adduser deployer');
run('sudo usermod -aG www-data deployer');
run('sudo chfn -o umask=022 deployer');
run('sudo chfn -o umask=022 deployer');
run('sudo mkdir -p /var/www/html');
run('sudo chown deployer:www-data /var/www/html');
run('sudo chmod g+s /var/www/html');
run('echo "deployer ALL=(ALL:ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/deployer');
run('mkdir -p /home/deployer/.ssh');
upload('./.deployer/.ssh/id_rsa', '/home/deployer/.ssh/id_rsa');
upload('./.deployer/.ssh/id_rsa.pub', '/home/deployer/.ssh/id_rsa.pub');
run('sudo chown -R deployer /home/deployer/.ssh');
run('chmod 400 /home/deployer/.ssh/id_rsa');
run('chmod 400 /home/deployer/.ssh/id_rsa.pub');
}
try{
run('sudo -Hu deployer ssh -T git@github.com -o StrictHostKeyChecking=no');
}catch (\Exception $e){
}
});
task('server:ssl', function (){
run('sudo certbot certonly -n --nginx -d {{domain}} --agree-tos -m {{email}}');
run('sudo service nginx reload');
});
task('server:cleanup', function (){
run('sudo apt -y autoremove');
run('sudo apt-get clean');
});
task('server:done', function (){
run('touch /home/deployer/.provisioned');
});
task('server:user', function ($user = 'deployer'){
if (run('whoami') !== $user) {
$host = Context::get()->getHost();
writeln('<comment>Changing user to deployer</comment>');
$host->become($user);
}
});
task('server:root_user', function ($user = 'root'){
if (run('whoami') !== $user) {
$host = Context::get()->getHost();
writeln('<comment>Changing user to root</comment>');
$host->become($user);
}
});
task('server:provision', [
'server:info',
'check:domain',
'server:init',
'server:update',
'server:deployer',
'server:common',
'server:nginx',
'server:curl',
'server:zip',
'server:git',
'server:node_js',
'server:redis',
'server:supervisor',
'server:php',
'server:mysql',
'server:composer',
'server:certbot',
'server:ssl',
'server:cleanup',
'server:done',
]);
/**
* Deploy script
*/
task('deploy', [
'info',
'deploy:info',
'check:provision',
'deploy:prepare',
'deploy:lock',
'deploy:release',
'deploy:update_code',
'deploy:shared',
'env',
'database',
'deploy:vendors',
'deploy:writable',
'artisan:storage:link',
'npm:install',
'artisan:cache:clear',
'artisan:view:clear',
// 'artisan:route:cache',
'artisan:config:cache',
'artisan:migrate',
'artisan:db:seed',
'build',
'deploy:symlink',
'deploy:unlock',
'cleanup',
'supervisor',
'vhost',
'supervisor:restart',
'artisan:queue:restart',
]);
after('deploy', 'success');
after('deploy:failed', 'deploy:unlock');