-
Notifications
You must be signed in to change notification settings - Fork 5
/
Envoy.blade.php
350 lines (305 loc) · 12.1 KB
/
Envoy.blade.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
345
346
347
348
349
350
<?php
use Symfony\Component\Process\Process;
/**
* Application name
*/
$appname = ''; // domain
/**
* Remote server connection string
*/
$server_map = [
'dev' => '', // dev.domain.com
'production' => '', // domain.com
];
/**
* Repository source
*/
$source_repo = ''; // git@github.com:username/repo.git
/**
* Deployment base path
*/
$deploy_basepath = ''; // /var/www/html/
/**
* Remote server service user(group) that the application runs as
*/
$serviceowner = 'apache';
/**
* shared sub-directories name , eg: storage
*/
$shared_subdirs = [
'storage',
];
/**
* addon exclude pattens , eg: /node_modules/
*/
$exclude_addon_pattens = [
'node_modules',
];
function getReleaseHash() {
$process = new Process(['git', 'rev-parse', 'HEAD']);
$process->run();
return trim($process->getOutput());
}
function isReleaseCurrent($dir, $hash) {
return file_exists($dir . '/release_' . $hash . '.tgz');
}
?>
@setup
if ( ! isset($appname) ) { throw new Exception('App Name is not set'); }
if ( ! isset($server_map['dev']) && $server_map['dev'] != '' ) { throw new Exception('SSH development login username/host is not set'); }
if ( ! isset($server_map['production']) && $server_map['production'] != '' ) { throw new Exception('SSH production login username/host is not set'); }
if ( ! isset($source_repo) ) { throw new Exception('Git repository is not set'); }
if ( ! isset($serviceowner) ) { throw new Exception('Service Owner is not set'); }
if ( ! isset($deploy_basepath) ) { throw new Exception('Path is not set'); }
if ( substr($deploy_basepath, 0, 1) !== '/' ) { throw new Exception('Careful - your path does not begin with /'); }
$envoy_servers = array_merge(['local'=>'localhost',],$server_map);
$now = new DateTime();
$dateDisplay = $now->format('Y-m-d H:i:s');
$date = $now->format('YmdHis');
$remote_server = (isset($on) && $on == 'production') ? 'production' : 'dev';
if(!isset($branch)){
$branch = (isset($remote_server) && $remote_server == 'production' ? "master" : "develop");
}else{
$remote_server = 'dev';
}
$source_name = 'source';
$app_base = $deploy_basepath.$appname;
$source_dir = $app_base.'/'.$source_name;
$release_dir = $app_base.'/releases';
$app_dir = $app_base.'/current';
$prev_dir = $app_base.'/prevrelease';
$last_dir = $app_base.'/lastrelease';
$shared_dir = $app_base.'/shared';
$release = 'release_' . $date;
$tmp_dir = $app_base.'/tmp';
$local_dir = getcwd();
$localdeploy_dirname = '.envoydeploy';
$localdeploy_base = $local_dir.'/'.$localdeploy_dirname;
$localdeploy_source_dir = $localdeploy_base.'/'.$source_name;
$localdeploy_tmp_dir = $localdeploy_base.'/tmp';
$release_hash = getReleaseHash();
$release_current = isReleaseCurrent($localdeploy_tmp_dir, $release_hash);
@endsetup
@servers($envoy_servers)
@macro('deploy')
show_env_local
show_env_remote
init_basedir_local
init_basedir_remote
updaterepo_localsrc
depsinstall_localsrc
packrelease_localsrc
rcpreleasepack_to_remote
extractreleasepack_on_remote
syncshareddata_remotesrc
baseenvlink_remoterelease
prepare_remoterelease
link_newrelease_on_remote
cache_remote_release
cleanup_oldreleases_on_remote
clean_localsrc
update_conf_for_refresh
@endmacro
@macro('rollback')
link_rollback_on_remote
@endmacro
@task('show_env_local', ['on' => 'local'])
echo '----';
echo 'Current Release Name: {{$release}}';
echo 'Current branch is {{$branch}}';
echo 'Deployment Start at {{$dateDisplay}}';
echo 'App base {{ $localdeploy_base }}';
echo 'Deploy hash {{ $release_hash }}';
echo 'Is this a redeploy? {{ ($release_current)?'Yes':'No' }}';
echo '----';
@endtask
@task('show_env_remote', ['on' => $remote_server])
echo '----';
echo 'Current Release Name: {{$release}}';
echo 'Current branch is {{$branch}}';
echo 'Deployment Start at {{$dateDisplay}}';
echo 'Releases dir {{ $release_dir }}';
echo 'Shared dir {{ $shared_dir }}';
echo 'App base {{ $app_base }}';
echo '----';
@endtask
@task('init_basedir_local', ['on' => 'local'])
[ -d {{ $localdeploy_base }} ] || mkdir -p {{ $localdeploy_base }};
[ -d {{ $localdeploy_source_dir }} ] || mkdir -p {{ $localdeploy_source_dir }};
[ -d {{ $localdeploy_tmp_dir }} ] || mkdir -p {{ $localdeploy_tmp_dir }};
@endtask
@task('init_basedir_remote', ['on' => $remote_server])
if [ ! -d {{ $release_dir }} ]; then
mkdir -p -m 02770 {{ $release_dir }};
chgrp marketing {{ $release_dir }};
chmod g+s {{ $release_dir }};
fi
if [ ! -d {{ $shared_dir }}/storage ]; then
mkdir -p -m 02770 {{ $shared_dir }}/storage;
chgrp marketing {{ $shared_dir }}/storage;
chmod g+s {{ $shared_dir }}/storage;
fi
[ -d {{ $shared_dir }}/storage/debugbar ] || mkdir -p -m 02770 {{ $shared_dir }}/storage/debugbar;
[ -d {{ $shared_dir }}/storage/logs ] || mkdir -p -m 02770 {{ $shared_dir }}/storage/logs;
[ -d {{ $shared_dir }}/storage/app ] || mkdir -p -m 02770 {{ $shared_dir }}/storage/app;
[ -d {{ $shared_dir }}/storage/app/public ] || mkdir -p -m 02770 {{ $shared_dir }}/storage/app/public;
[ -d {{ $shared_dir }}/storage/framework ] || mkdir -p -m 02770 {{ $shared_dir }}/storage/framework;
[ -d {{ $shared_dir }}/storage/framework/cache ] || mkdir -p -m 02770 {{ $shared_dir }}/storage/framework/cache;
[ -d {{ $shared_dir }}/storage/framework/views ] || mkdir -p -m 02770 {{ $shared_dir }}/storage/framework/views;
[ -d {{ $app_base }}/tmp ] || mkdir -p 02770 {{ $app_base }}/tmp;
@endtask
@task('updaterepo_localsrc', ['on' => 'local'])
echo "LocalSource Repository update...";
if [ ! {{ $release_current }} ]; then
if [ -d {{ $localdeploy_source_dir }}/.git ]; then
echo "Repository exists only update...";
echo "Cleaning up old _resources"
[ -d {{ $localdeploy_source_dir }}/public/_resources ] && rm -rf {{ $localdeploy_source_dir }}/public/_resources
cd {{ $localdeploy_source_dir }};
git fetch origin;
git checkout -B {{ $branch }} origin/{{ $branch }};
git pull origin {{ $branch }};
else
echo "No Previous Repository exits and cloning...";
git clone {{ $source_repo }} --branch={{ $branch }} --depth=1 {{ $localdeploy_source_dir }};
fi
echo "LocalSource Repository updated.";
fi
@endtask
@task('depsinstall_localsrc', ['on' => 'local'])
if [ ! {{ $release_current }} ]; then
echo "LocalSource Dependencies install...";
cd {{ $localdeploy_source_dir }};
echo "Composer install...";
if [ "{{ $remote_server }}" = "production" ]; then
make composerinstallproduction
else
make composerinstalldev
fi
echo "Composer installed.";
echo "Generate the artisan key...";
make generatekey
echo "Yarn install...";
make yarn;
echo "Yarn installed.";
echo "Compile assets...";
make buildproduction;
echo "Assets compiled.";
echo "LocalSource Dependencies installed.";
fi
@endtask
@task('packrelease_localsrc', ['on' => 'local'])
if [ ! {{ $release_current }} ]; then
echo "LocalSource Pack release...";
[ -f {{ $localdeploy_tmp_dir }}/release_{{ $release_hash }}.tgz ] && rm -rf {{ $localdeploy_tmp_dir }}/release_{{ $release_hash }}.tgz;
cd {{ $localdeploy_base }}/;
tar --exclude=storage --exclude=node_modules --exclude-vcs -czf {{ $localdeploy_tmp_dir }}/release_{{ $release_hash }}.tgz {{ $source_name }};
echo "LocalSource Pack release Done.";
fi
@endtask
@task('rcpreleasepack_to_remote', ['on' => 'local'])
echo "rcp localpack release to remote...";
if [ -f {{ $localdeploy_tmp_dir }}/release_{{ $release_hash }}.tgz ]; then
rsync -avz --info=progress2 --port 22 {{ $localdeploy_tmp_dir }}/release_{{ $release_hash }}.tgz {{ $server_map[$remote_server] }}:{{ $tmp_dir }}/;
else
echo "localpack release NOT EXISTS.";
exit 1;
fi
echo "rcp localpack release to remote Done.";
@endtask
@task('clean_localsrc', ['on' => 'local'])
echo "cleaning previous local release files...";
find {{ $localdeploy_tmp_dir }} -type f -name "*.tgz" ! -name "release_{{ $release_hash }}.tgz" -exec rm -f {} \;
echo "previous local release files cleaned"
@endtask
@task('extractreleasepack_on_remote', ['on' => $remote_server])
echo "extract pack release on remote...";
if [ -f {{ $tmp_dir }}/release_{{ $release_hash }}.tgz ]; then
[ -d {{ $tmp_dir }}/{{ $source_name }} ] && rm -rf {{ $tmp_dir }}/{{ $source_name }};
tar zxf {{ $tmp_dir }}/release_{{ $release_hash }}.tgz -C {{ $tmp_dir }} --warning=no-timestamp;
if [ -d {{ $tmp_dir }}/{{ $source_name }} ]; then
if [ -d {{ $source_dir }} ]; then
echo "Previous Remote Source Dir Exists,Moving.";
[ -d {{ $app_base }}/source_prev ] && rm -rf {{ $app_base }}/source_prev;
mv {{ $source_dir }} {{ $app_base }}/source_prev;
mv {{ $tmp_dir }}/{{ $source_name }} {{ $source_dir }};
else
mv {{ $tmp_dir }}/{{ $source_name }} {{ $source_dir }};
fi
else
echo "extract pack release on remote ERROR.";
exit 1;
fi
else
echo "pack release NOT EXISTS.";
exit 1;
fi
echo "extract pack release on remote Done.";
@endtask
@task('syncshareddata_remotesrc', ['on' => $remote_server])
echo "RemoteSource Sync SharedData...";
ln -fs {{ $shared_dir }}/storage {{ $source_dir }}/storage
ln -fs {{ $shared_dir }}/storage/app/public {{ $source_dir }}/public/_static
echo "RemoteSource Sync SharedData Done.";
@endtask
@task('baseenvlink_remoterelease', ['on' => $remote_server])
echo "RemoteRelease Environment file setup...";
if [ -f {{ $app_base }}/.env ]; then
[ -f {{ $source_dir }}/.env ] && rm -rf {{ $source_dir }}/.env;
ln -nfs {{ $app_base }}/.env {{ $source_dir }}/.env && chgrp -h {{$serviceowner}} {{ $source_dir }}/.env;
fi
echo "RemoteRelease Environment file setup Done.";
@endtask
@task('prepare_remoterelease', ['on' => $remote_server])
echo "RemoteRelease Prepare...";
mv {{ $source_dir }} {{ $release_dir }}/{{ $release }}
chmod -R g+s {{ $release_dir }}/{{ $release }}
echo "RemoteRelease Prepare Done.";
@endtask
@task('cache_remote_release', ['on' => $remote_server])
shopt -s expand_aliases
source ~/.bashrc
echo "Clearing view cache"
php82 {{ $release_dir }}/{{ $release }}/artisan view:clear
echo "Caching configs...";
php82 {{ $release_dir }}/{{ $release }}/artisan config:cache
echo "Caching routes...";
php82 {{ $release_dir }}/{{ $release }}/artisan route:cache
echo "Done caching ";
@endtask
@task('link_newrelease_on_remote', ['on' => $remote_server])
echo "Deploy new Release link";
cd {{ $app_base }};
[ -d {{ $prev_dir }} ] && unlink {{ $prev_dir }};
[ -d {{ $app_dir }} ] && mv {{ $app_dir }} {{ $prev_dir }};
ln -nfs {{ $release_dir }}/{{ $release }} {{ $app_dir }} && chgrp -h {{$serviceowner}} {{ $app_dir }};
echo "Deployment ({{ $release }}) symbolic link created";
@endtask
@task('link_rollback_on_remote', ['on' => $remote_server])
if [ -d {{ $last_dir }} ]; then
[ ! -d {{ $app_dir }} ] || mv {{ $app_dir }} {{ $prev_dir }};
[ ! -d {{ $last_dir }} ] || mv {{ $last_dir }} {{ $app_dir }};
echo "Reset to last symbolic link";
elif [ -d {{ $prev_dir }} ] && [ ! -d {{ $last_dir }} ]; then
[ ! -d {{ $app_dir }} ] || mv {{ $app_dir }} {{ $last_dir }};
[ ! -d {{ $prev_dir }} ] || mv {{ $prev_dir }} {{ $app_dir }};
echo "Rollback to previous symbolic link";
else
echo "No previous link to rollback";
fi
@endtask
@task('cleanup_oldreleases_on_remote', ['on' => $remote_server])
echo 'Cleanup up old releases';
cd {{ $release_dir }} && ls -d release_* | sort -r | tail -n +4
nohup bash -c "(cd {{ $release_dir }} && ls -d release_* | sort -r | tail -n +4 | xargs rm -rf) > /dev/null 2>&1 &"
echo 'Remove everything out of the remote temp directory'
find {{ $tmp_dir }} -type f -name "*.tgz" ! -name "release_{{ $release_hash }}.tgz" -exec rm -f {} \;
echo "Cleanup up done.";
@endtask
@task('notice_done')
echo "Deployment ({{ $release }}) done.";
@endtask
@task('update_conf_for_refresh', ['on' => $remote_server])
echo "Updating config for refreshing opcache";
sed -i -e 's/#release_.*/#{{ $release }}/g' {{ $app_base }}/conf/{{ $appname }}.conf
@endtask