-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflightplan.remote.js
232 lines (202 loc) · 6.81 KB
/
flightplan.remote.js
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
const path = require('path');
require('dotenv').config({ path: path.resolve(process.cwd(), '.env.local') });
require('dotenv').config({ path: path.resolve(process.cwd(), '.env') });
const plan = require('flightplan');
const cfg = require('./flightplan.config');
/* ======
* Config
* ====== */
/**
* Target servers
*/
plan.target('production', cfg.production, cfg.production.opts);
plan.target('production-db', cfg.productionDB, cfg.productionDB.opts);
/**
* Setup folders etc. ready for files
*/
let sshUser,
sshPort,
sshHost,
webRoot,
url,
domain,
wpHome,
dbName,
dbUser,
dbPw;
const date = new Date().getTime();
const devDomain = process.env.DEVELOPMENT_URL.replace(/(^\w+:|^)\/\//, '');
const tmpDir = `wp-update-${date}`;
plan.local('start', local => {
const input = local.prompt(
'Are you sure you want to continue with the process? [y/n]',
);
if (input.indexOf('y') === -1) {
plan.abort('Plan canceled.');
}
});
plan.local(['start', 'update', 'assets-push', 'db-replace'], local => {
sshHost = plan.runtime.hosts[0].host;
sshUser = plan.runtime.hosts[0].username;
sshPort = plan.runtime.hosts[0].port;
webRoot = plan.runtime.options.webRoot;
wpHome =
plan.runtime.options.wpHome === '/'
? (plan.runtime.options.wpHome = '')
: plan.runtime.options.wpHome || '';
url = plan.runtime.options.url;
domain = url ? url.replace(/(^\w+:|^)\/\//, '') : '';
dbName = plan.runtime.options.dbName;
dbUser = plan.runtime.options.dbUser;
dbPw = plan.runtime.options.dbPw;
});
/* ======
* Install WordPress
* ====== */
plan.remote(['start', 'update'], remote => {
remote.exec(`mkdir -p ${webRoot}/tmp/wp-deployments`, {
silent: true,
failsafe: true,
});
});
plan.local(['start', 'update'], local => {
const filesToCopy = [
'web/.htaccess.example',
'web/index.php',
'web/wp-config.php',
'web/wp-cli.yml',
'web/composer.json',
'web/auth.json',
];
local.log(
`Transferring local files to ${webRoot}/tmp/wp-deployments/${tmpDir}/`,
);
local.transfer(filesToCopy, `${webRoot}/tmp/wp-deployments/${tmpDir}/`, {
failsafe: true,
});
});
plan.remote(['start', 'update'], remote => {
remote.log('Installing Composer...');
remote.exec(
`curl -sS https://getcomposer.org/installer | php && mv composer.phar ${webRoot}/`,
);
remote.log('Copying files...');
const deploymentPath = `${webRoot}/tmp/wp-deployments/${tmpDir}/web`;
remote.exec(`cp ${deploymentPath}/composer.json ${webRoot}/`);
remote.exec(`cp ${deploymentPath}/auth.json ${webRoot}/`);
if (plan.runtime.task === 'start') {
remote.exec(`cp ${deploymentPath}/.htaccess.example ${webRoot}/.htaccess`);
remote.exec(`cp ${deploymentPath}/index.php ${webRoot}/`);
remote.exec(`cp ${deploymentPath}/wp-config.php ${webRoot}/`);
remote.exec(`cp ${deploymentPath}/wp-cli.yml ${webRoot}/`);
remote.exec(`mkdir -p ${webRoot}/wp-content/mu-plugins/`, {
silent: true,
failsafe: true,
});
}
remote.log('Installing Composer dependencies...');
remote.exec(`mkdir ${webRoot}/vendor`, { failsafe: true });
remote.with(`cd ${webRoot}/`, { failsafe: true }, () => {
remote.exec(`
php composer.phar clearcache &&
php composer.phar update --prefer-dist --no-dev --optimize-autoloader --no-interaction
`);
});
remote.log(`Removing uploaded files from ${webRoot} & ${deploymentPath}/`);
remote.exec(`rm -r ${webRoot}/composer.json`, { failsafe: true });
remote.exec(`rm -r ${webRoot}/composer.lock`, { failsafe: true });
remote.exec(`rm -r ${webRoot}/auth.json`, { failsafe: true });
remote.exec(`rm -r ${deploymentPath}/`);
});
plan.local(['start', 'update'], local => {
local.log('Deploying mu-plugins folder...');
local.exec(
`rsync -avz -e "ssh -p ${sshPort}" \
web/wp-content/mu-plugins/*.php \
${sshUser}@${sshHost}:${webRoot}/wp-content/mu-plugins
`,
{ failsafe: true },
);
});
/* ======
* Push Assets
* ====== */
plan.local(['assets-push'], local => {
local.log('Deploying uploads folder...');
local.exec(
`rsync -avz -e "ssh -p ${sshPort}" \
web/wp-content/uploads ${sshUser}@${sshHost}:${webRoot}/wp-content`,
{ failsafe: true },
);
});
/* ======
* Replace Database
* ====== */
plan.local(['db-replace'], local => {
const input = local.prompt(
'Are you sure you want to continue with the process? [y/n]',
);
if (input.indexOf('y') === -1) {
plan.abort('Plan canceled.');
}
});
plan.local(['db-replace'], local => {
local.log('Creating local database dump...');
local.exec(`mkdir -p database/local`, { silent: true, failsafe: true });
local.exec(`docker-compose exec -T db bash -c "mysqldump -uroot -proot \
wordpress > docker-entrypoint-initdb.d/local/wordpress-${date}.sql"`);
local.log('Pushing local database dump to remote...');
local.transfer([`database/local/wordpress-${date}.sql`], `${webRoot}/tmp`);
});
plan.remote(['db-replace'], remote => {
remote.log(
`Backing up remote database to ${webRoot}/tmp/database/remote/wordpress-${date}.sql`,
);
remote.exec(`mkdir -p ${webRoot}/tmp/database/remote`, { failsafe: true });
remote.exec(
`mysqldump -u${dbUser} -p${dbPw} -f ${dbName} > \
${webRoot}/tmp/database/remote/wordpress-${date}.sql;`,
{ failsafe: true },
);
remote.log(`Dropping remote database ${dbName}`);
remote.exec(`mysql -u${dbUser} -p${dbPw} -e 'drop database ${dbName};'`, {
failsafe: true,
});
remote.log(
`Replacing remote database with ${webRoot}/tmp/database/local/wordpress-${date}.sql`,
);
remote.exec(
`
mysql -u${dbUser} -p${dbPw} \
-e ' \
create database ${dbName}; use ${dbName}; \
source ${webRoot}/tmp/database/local/wordpress-${date}.sql;'
`,
{ failsafe: true },
);
remote.log('Installing wp-cli and replacing strings in database...');
remote.exec(
`
if [ ! -f ${webRoot}/wp-cli.phar ]; then
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar &&
mv wp-cli.phar ${webRoot}
fi
if $(cd ${webRoot} && php wp-cli.phar --url=${domain} core is-installed --network);
then
cd ${webRoot}
php wp-cli.phar search-replace '${process.env.DEVELOPMENT_URL}' '${url}${wpHome}' --network --skip-tables=wp_users,wp_blogs,wp_site --url=${domain}
php wp-cli.phar search-replace '${devDomain}' '${domain}' wp_blogs wp_site --network --url=${domain}
php wp-cli.phar search-replace '^\/' '${wpHome}\/' wp_blogs --regex --network --url=${domain}
else
cd ${webRoot}
php wp-cli.phar search-replace '${process.env.DEVELOPMENT_URL}' '${url}${wpHome}' --skip-tables=wp_users --url=${domain}
fi
`,
{ failsafe: true },
);
remote.log('Removing transferred material from remote...');
remote.exec(`rm -r ${webRoot}/tmp/database/local`, {
silent: true,
failsafe: true,
});
});