Skip to content

Commit cbdbdcf

Browse files
author
Takashi Matsuo
authored
Added some options for choosing Cloud SQL generation and the db region. (GoogleCloudPlatform#121)
* Added some options for choosing Cloud SQL generation and the db region.
1 parent fd214b3 commit cbdbdcf

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed

appengine/wordpress/src/WordPressSetup.php

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ class WordPressSetup extends Command
4040
const FLEXIBLE_ENV = 'Flexible Environment';
4141
const STANDARD_ENV = 'Standard Environment';
4242

43+
const DEFAULT_DB_REGION = 'us-central1';
44+
45+
private static $availableDbRegions = array(
46+
'us-central1',
47+
'us-east1',
48+
'europe-west1',
49+
'asia-east1',
50+
);
51+
4352
protected function configure()
4453
{
4554
$this
@@ -63,13 +72,29 @@ protected function configure()
6372
'Directory for the new project',
6473
self::DEFAULT_DIR
6574
)
75+
->addOption(
76+
'sql_gen',
77+
'',
78+
InputOption::VALUE_OPTIONAL,
79+
sprintf('Cloud SQL generation to use; 2: %s, 1: %s',
80+
'Second Generation',
81+
'First Generation'),
82+
2
83+
)
6684
->addOption(
6785
'project_id',
6886
'p',
6987
InputOption::VALUE_OPTIONAL,
7088
'Google Cloud project id',
7189
''
7290
)
91+
->addOption(
92+
'db_region',
93+
null,
94+
InputOption::VALUE_OPTIONAL,
95+
'Cloud SQL region',
96+
''
97+
)
7398
->addOption(
7499
'db_instance',
75100
null,
@@ -225,15 +250,53 @@ protected function execute(InputInterface $input, OutputInterface $output)
225250
$q->setErrorMessage('Environment %s is invalid.');
226251
$env = $helper->ask($input, $output, $q);
227252
}
228-
$output->writeln('Creating a new project for: ' . $env);
253+
$output->writeln('Creating a new project for: ' . $env
254+
. '');
255+
256+
// Determine the Cloud SQL Generation to use.
257+
$sql_gen = $input->getOption('sql_gen');
258+
switch ($sql_gen) {
259+
case '1':
260+
if ($env === self::FLEXIBLE_ENV) {
261+
$output->writeln('You can not use '
262+
. 'Cloud SQL First Generation with '
263+
. self::FLEXIBLE_ENV . '.');
264+
return self::DEFAULT_ERROR;
265+
}
266+
$db_connection_pattern = '%s:%s';
267+
break;
268+
case '2':
269+
$db_region = $input->getOption('db_region');
270+
if (! in_array($db_region, self::$availableDbRegions)) {
271+
$q = new ChoiceQuestion(
272+
'Please select the region of your Cloud SQL instance '
273+
. '(defaults to ' . self::DEFAULT_DB_REGION . ')',
274+
self::$availableDbRegions,
275+
self::DEFAULT_DB_REGION
276+
);
277+
$q->setErrorMessage('DB region %s is invalid.');
278+
$db_region = $helper->ask($input, $output, $q);
279+
$output->writeln('Using a db_region: ' . $db_region
280+
. '');
281+
}
282+
$db_connection_pattern = "%s:$db_region:%s";
283+
break;
284+
default:
285+
$output->writeln(
286+
sprintf(
287+
'Invalid value for sql_gen: %s.',
288+
$sql_gen
289+
)
290+
);
291+
return self::DEFAULT_ERROR;
292+
}
229293

230294
$output->writeln('Downloading the WordPress archive...');
231295
$wpUrl = $input->getOption('wordpress_url');
232296
$project->downloadArchive('the WordPress archive', $wpUrl);
233297
if (!$this->report($output, $project)) {
234298
return self::DEFAULT_ERROR;
235299
}
236-
237300
$output->writeln('Downloading the Batcache plugin...');
238301
$project->downloadArchive(
239302
'Batcache plugin', self::LATEST_BATCACHE,
@@ -301,6 +364,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
301364
}
302365
$params = array();
303366
$this->askParameters($keys, $params, $input, $output, $helper);
367+
$params['db_connection'] = sprintf(
368+
$db_connection_pattern,
369+
$params['project_id'],
370+
$params['db_instance']
371+
);
304372
$q = new ConfirmationQuestion(
305373
'Do you want to use the same db user and password for '
306374
. 'local run? (Y/n)',

appengine/wordpress/src/files/flexible/app.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ runtime: php
22
vm: true
33

44
beta_settings:
5-
cloud_sql_instances: {{project_id}}:us-central1:{{db_instance}}
5+
cloud_sql_instances: {{db_connection}}
66

77
runtime_config:
88
document_root: wordpress

appengine/wordpress/src/files/flexible/wp-config.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,22 @@
7070
// ** MySQL settings - You can get this info from your web host ** //
7171
if ($onGae) {
7272
/** Production environment */
73+
define('DB_HOST', ':/cloudsql/{{db_connection}}');
7374
/** The name of the database for WordPress */
7475
define('DB_NAME', '{{db_name}}');
7576
/** MySQL database username */
7677
define('DB_USER', '{{db_user}}');
7778
/** MySQL database password */
7879
define('DB_PASSWORD', '{{db_password}}');
79-
define(
80-
'DB_HOST',
81-
'localhost:/cloudsql/{{project_id}}:us-central1:{{db_instance}}'
82-
);
8380
} else {
8481
/** Local environment */
82+
define('DB_HOST', '127.0.0.1');
8583
/** The name of the database for WordPress */
8684
define('DB_NAME', '{{db_name}}');
8785
/** MySQL database username */
8886
define('DB_USER', '{{local_db_user}}');
8987
/** MySQL database password */
9088
define('DB_PASSWORD', '{{local_db_password}}');
91-
define('DB_HOST', '127.0.0.1');
9289
}
9390

9491
/** Database Charset to use in creating database tables. */

appengine/wordpress/src/files/standard/wp-config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
/** The name of the Cloud SQL database for WordPress */
5454
define('DB_NAME', '{{db_name}}');
5555
/** Production login info */
56-
define('DB_HOST', ':/cloudsql/{{project_id}}:{{db_instance}}');
56+
define('DB_HOST', ':/cloudsql/{{db_connection}}');
5757
define('DB_USER', '{{db_user}}');
5858
define('DB_PASSWORD', '{{db_password}}');
5959
} else {

0 commit comments

Comments
 (0)