forked from webgriffe/deployer-magento
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magento.php
141 lines (118 loc) · 4.67 KB
/
magento.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
<?php
namespace Deployer;
use Deployer\Task\Context;
use Symfony\Component\Console\Input\InputOption;
// TODO Add deployer version check (now it works only with Deployer >= 5.0)
require 'recipe/common.php';
set('magento_root_path', function () {
$magentoRoot = get('magento_root');
return empty($magentoRoot) ? '' : (rtrim($magentoRoot, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR);
});
// Magento shared dirs
set(
'shared_dirs',
['{{magento_root_path}}' . 'var', '{{magento_root_path}}' . 'media', '{{magento_root_path}}' . 'sitemaps']
);
// Magento shared files
set('shared_files', ['{{magento_root_path}}' . 'app/etc/local.xml', '{{magento_root_path}}' . '.htaccess']);
// Magento writable dirs
set(
'writable_dirs',
['{{magento_root_path}}' . 'var', '{{magento_root_path}}' . 'media', '{{magento_root_path}}' . 'sitemaps']
);
// Magento media pull exclude dirs (paths must be relative to the media dir)
set('media_pull_exclude_dirs', ['css', 'css_secure', 'js', 'catalog/product/cache']);
// Magento clear paths
set(
'clear_paths',
[
'{{magento_root_path}}' . 'LICENSE.html',
'{{magento_root_path}}' . 'LICENSE.txt',
'{{magento_root_path}}' . 'LICENSE_AFL.txt',
'{{magento_root_path}}' . 'RELEASE_NOTES.txt',
'{{magento_root_path}}' . 'downloader',
]
);
// Setup run timeout
set('setup-run-timeout', 300);
// DB pull strip tables
set('db_pull_strip_tables', ['@stripped']);
// Local/remote magerun path
set('magerun_remote', 'n98-magerun.phar');
set('magerun_local', getenv('DEPLOYER_MAGERUN_LOCAL') ?: 'n98-magerun.phar');
// Tasks
desc('Run the Magento setup scripts');
task('magento:setup-run', function () {
if (test('[ -f {{release_path}}/{{magento_root_path}}app/etc/local.xml ]')) {
$installed = run('cat {{release_path}}/{{magento_root_path}}app/etc/local.xml | grep "<date>"; true');
if ($installed) {
run(
'cd {{release_path}}/{{magento_root_path}} && {{magerun_remote}} sys:setup:run',
['timeout' => get('setup-run-timeout')]
);
}
}
});
desc('Clear Magento cache');
task('magento:clear-cache', function () {
if (test('[ -f {{release_path}}/{{magento_root_path}}app/etc/local.xml ]')) {
$installed = run('cat {{release_path}}/{{magento_root_path}}app/etc/local.xml | grep "<date>"; true');
if ($installed) {
run('cd {{release_path}}/{{magento_root_path}} && {{magerun_remote}} cache:clean');
}
}
});
desc('Create Magento database dump');
task('magento:db-dump', function () {
run('cd {{current_path}}/{{magento_root_path}} && {{magerun_remote}} db:dump -n -c gz ~');
});
desc('Pull Magento database to local');
task('magento:db-pull', function () {
$fileName = uniqid('dbdump_');
$stripTables = implode(' ', get('db_pull_strip_tables'));
$remoteDump = "/tmp/{$fileName}.sql.gz";
run(
'cd {{current_path}}/{{magento_root_path}} && ' .
'{{magerun_remote}} db:dump -n --strip="'. $stripTables .'" -c gz ' . $remoteDump
);
$dumpName = tempnam(sys_get_temp_dir(), 'deployer_');
$localDumpGz = $dumpName . '.sql.gz';
$localDumpSql = $dumpName . '.sql';
download($remoteDump, $localDumpGz);
run('rm ' . $remoteDump);
runLocally('gunzip ' . $localDumpGz);
runLocally('cd ./{{magento_root_path}} && {{magerun_local}} db:import -n --drop-tables --optimize ' . $localDumpSql);
runLocally('cd ./{{magento_root_path}} && {{magerun_local}} cache:disable');
runLocally('rm ' . $localDumpSql);
});
option(
'media-pull-timeout',
null,
InputOption::VALUE_OPTIONAL,
'Timeout for media-pull task in seconds (default is 300s)'
);
desc('Pull Magento media to local');
task('magento:media-pull', function () {
$remotePath = '{{current_path}}/{{magento_root_path}}/media/';
$localPath = './{{magento_root_path}}/media/';
$excludeDirs = array_map(function($dir) {
return '--exclude '.$dir;
}, get('media_pull_exclude_dirs'));
$timeout = 300;
if (input()->hasOption('media-pull-timeout')) {
$timeout = input()->getOption('media-pull-timeout');
}
$config = [
'options' => $excludeDirs,
'timeout' => $timeout
];
download($remotePath, $localPath, $config);
});
desc('Set "copy" as Magento deploy strategy');
task('magento:set-copy-deploy-strategy', function(){
run('cd {{release_path}} && {{bin/composer}} config extra.magento-deploystrategy copy');
run('cd {{release_path}} && {{bin/composer}} config extra.magento-force true');
});
after('deploy:shared', 'magento:set-copy-deploy-strategy');
before('deploy:symlink', 'magento:setup-run');
after('deploy:symlink', 'magento:clear-cache');