-
Notifications
You must be signed in to change notification settings - Fork 1
/
up.php
332 lines (276 loc) · 8.71 KB
/
up.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
<?php
/**
* Script Name: WP CLI Up
* Description: Setup local dev environment using Ubuntu Multipass
* License: GPLv3
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'up', 'WP_CLI_Up', array( 'when' => 'before_wp_load' ) );
}
/**
* Control a Multipass-powered dev environment
*
* Usages:
*
* wp up init
* wp up add
* wp up remove
* wp up ssh
*
*/
class WP_CLI_Up {
/**
* Removes a site from the wp-cli-up multipass instance
*
* ## OPTIONS
*
* <domain>
* : Domain name of the site to remove
*
* ## EXAMPLES
*
* # Remove site.
* $ wp up remove acmepublishing.com
* Success: Added object 'my_key' in group 'my_value'.
*/
public function remove( $args, $assoc_args ) {
if ( ! is_writable( '/etc/hosts' ) ) {
WP_CLI::warning( "Not running as root. Your /etc/hosts file won't be updated." );
WP_CLI::confirm( "Are you sure you want to proceed?" );
}
$domain = $args[0];
WP_CLI::confirm( "Are you sure you want to remove the site '$domain', its database, and all its files?", $assoc_args );
$defaults = [
'path' => $_SERVER['HOME'] . "/wp-cli-up/sites/$domain/files/public"
];
$args = array_merge( $defaults, $assoc_args );
$wp_path = $args['path'];
if ( ! is_dir( $wp_path ) ) {
WP_CLI::error( "Can't find folder $wp_path" );
}
$dbname = WP_CLI::runcommand( "config get DB_NAME --path=$wp_path", [ 'return' => true, 'exit_error' => false ] );
$dbuser = WP_CLI::runcommand( "config get DB_USER --path=$wp_path", [ 'return' => true, 'exit_error' => false ] );
if ( ! $dbname || ! $dbuser ) {
WP_CLI::error( "Can't find a WordPress install in $wp_path" );
}
$bash_script = $this->get_bash_script( 'remove', compact(
'domain', 'dbname', 'dbuser'
) );
system( $bash_script );
$this->remove_from_hosts_file( $domain );
WP_CLI::success( "$domain has been removed." );
}
/**
* Adds a site to the wp-cli-up multipass instance
*
* ## OPTIONS
*
* <domain>
* : Domain name to configure for the site
*
* [--dbname=<dbname>]
* : Set the database name. Defaults to the first part of the domain name.
*
* [--dbuser=<dbuser>]
* : Set the database user. Defaults to the first part of the domain name.
*
* [--dbpass=<dbpass>]
* : Set the database user password. Defaults to randomly generated string.
*
* [--dbprefix=<dbprefix>]
* : Set the database table prefix.
* ---
* default: wp_
* ---
*
* [--title=<site-title>]
* : The title of the new site. Defaults to the domain name.
*
* [--admin_user=<username>]
* : The name of the admin user.
* ---
* default: admin
* ---
*
* [--admin_password=<password>]
* : The password for the admin user. Defaults to randomly generated string.
*
* [--admin_email=<email>]
* : The email address for the admin user.
* ---
* default: nobody@nobody.com
* ---
*
* ## EXAMPLES
*
* # Add site.
* $ wp up add acmepublishing.com
* Success: Your site is now live at https://acmepublishing.com
* You can login at https://acmepublishing.com/wp-login.php with the following credentials:
* Username: admin
* Password: 6gfaae6653c2b9
*/
public function add( $args, $assoc_args ) {
if ( ! is_writable( '/etc/hosts' ) ) {
WP_CLI::warning( "Not running as root. Your /etc/hosts file won't be updated." );
WP_CLI::confirm( "Are you sure you want to proceed?" );
}
$domain = $args[0];
if ( empty( $domain ) ) {
WP_CLI::error( "You must provide a domain name to add a site." );
}
$username = $this->get_username_from_domain( $domain );
$defaults = [
'dbname' => $username,
'dbuser' => $username,
'dbpass' => base_convert( uniqid( 'pass', true ), 10, 20 ),
'dbprefix' => 'wp_',
'title' => $domain,
'admin_user' => 'admin',
'admin_password' => base_convert( uniqid( 'pass', true ), 10, 20 ),
'admin_email' => 'nobody@nobody.com'
];
$args = array_merge( $defaults, $assoc_args );
$args['domain'] = $domain;
WP_CLI::debug( 'Setting up site for ' . $domain . '...' );
$bash_script = $this->get_bash_script( 'add', $args );
system( $bash_script );
$this->add_update_hosts_file( $domain );
WP_CLI::success( "Your site is now live at https://$domain\nYou can login at https://$domain/wp-login.php with the following credentials:\nUsername: {$args['admin_user']}\nPassword: {$args['admin_password']}" );
}
/**
* Removes a domain from the macOS /etc/hosts file
*
* ## OPTIONS
*
* <domain>
* : Domain name to remove from the hosts file
*
* ## EXAMPLES
*
* # Remove domain from hosts file.
* $ sudo wp up hosts_remove acmepublishing.com
* Success: Hosts file updated.
*/
public function hosts_remove( $args, $assoc_args ) {
$domain = $args[0];
if ( is_null( WP_CLI::get_runner()->config['allow-root'] ) || ! is_writable( '/etc/hosts' ) ) {
WP_CLI::error( "This command must be run as root with --allow-root." );
}
if ( empty( $domain ) ) {
WP_CLI::error( "You must provide a domain name to add to your hosts file." );
}
$current_hosts_entry = $this->get_current_hosts_entry( $domain );
if ( ! $current_hosts_entry ) {
WP_CLI::error( "Can't find this domain in the macOS /etc/hosts file." );
}
$this->remove_from_hosts_file( $domain );
$current_hosts_entry = $this->get_current_hosts_entry( $domain );
if ( ! $current_hosts_entry ) {
WP_CLI::success( "Hosts file updated.");
}
else {
WP_CLI::error( "Could not remove domain from hosts file." );
}
}
private function remove_from_hosts_file( $domain ) {
$current_hosts_entry = $this->get_current_hosts_entry( $domain );
WP_CLI::debug( 'Removing domain from hosts file...' );
$cmd = "perl -p -i -e 's/^" . preg_quote( $current_hosts_entry ) . ".*\n//g' /etc/hosts";
WP_CLI::launch( $cmd );
}
/**
* Adds a domain to the macOS /etc/hosts file with the IP of the wp-cli-up multipass instance
*
* ## OPTIONS
*
* <domain>
* : Domain name to add to the hosts file
*
* ## EXAMPLES
*
* # Add domain to hosts file.
* $ sudo wp up hosts_add acmepublishing.com
* Success: Hosts file updated.
*/
public function hosts_add( $args, $assoc_args ) {
$domain = $args[0];
if ( is_null( WP_CLI::get_runner()->config['allow-root'] ) || ! is_writable( '/etc/hosts' ) ) {
WP_CLI::error( "This command must be run as root with --allow-root." );
}
if ( empty( $domain ) ) {
WP_CLI::error( "You must provide a domain name to add to your hosts file." );
}
$this->add_update_hosts_file( $domain );
$current_hosts_entry = $this->get_current_hosts_entry( $domain );
if ( $current_hosts_entry ) {
WP_CLI::success( "Hosts file updated.");
}
else {
WP_CLI::error( "Could add/update domain in hosts file." );
}
}
private function get_bash_script( $script, $args ) {
extract( $args );
ob_start();
include "bash/$script.php";
$output = ob_get_clean();
//file_put_contents( "$script.sh", $output );
return $output;
}
private function add_update_hosts_file( $domain ) {
if ( ! is_writable( '/etc/hosts' ) ) {
return false;
}
$ip_address = $this->get_multipass_info( 'IPv4' );
$new_hosts_entry = $ip_address . ' ' . $domain;
$current_hosts_entry = $this->get_current_hosts_entry( $domain );
if ( $current_hosts_entry ) {
WP_CLI::debug( 'Updating domain in hosts file...' );
$cmd = "perl -p -i -e 's/$current_hosts_entry/$new_hosts_entry/g' /etc/hosts";
WP_CLI::launch( $cmd, false );
}
else {
WP_CLI::debug( 'Adding domain to hosts file...' );
$cmd = 'echo %s >> /etc/hosts';
WP_CLI::launch( \WP_CLI\Utils\esc_cmd( $cmd, $new_hosts_entry ), false );
}
return true;
}
private function get_current_hosts_entry( $domain ) {
$cmd = "perl -wln -e 'print if /^(?:\d{1,3}\.){3}\d{1,3}\s+%s\b/' /etc/hosts";
$result = WP_CLI::launch( \WP_CLI\Utils\esc_cmd( $cmd, $domain ), false, true );
return trim( $result->stdout );
}
private function get_multipass_info( $key ) {
$info = WP_CLI::launch( 'multipass info wp-cli-up', true, true );
$info = explode( PHP_EOL, $info );
$new_info = [];
foreach ( $info as $i => $line ) {
if ( false === strpos( $line, ':' ) ) continue;
$line = preg_replace( '/:\s+/', ':', $line );
$line = explode( ':', $line );
$new_info[$line[0]] = $line[1];
}
$info = $new_info;
return isset( $info[$key] ) ? $info[$key] : false;
}
private function get_username_from_domain( $domain ) {
if ( false !== strpos( $domain, '.' ) ) {
$parts = explode( '.', $domain );
if ( strlen( $parts[1] ) >= strlen( $parts[0] ) ) {
return $parts[1];
}
else {
return $parts[0];
}
}
else {
$username = $domain;
}
return $username;
}
public function ssh( $args, $assoc_args ) {
system( "multipass shell wp-cli-up" );
}
}