@@ -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: <info> ' . $ env
254+ . '</info> ' );
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 ('<error>You can not use '
262+ . 'Cloud SQL First Generation with '
263+ . self ::FLEXIBLE_ENV . '.</error> ' );
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: <info> ' . $ db_region
280+ . '</info> ' );
281+ }
282+ $ db_connection_pattern = "%s: $ db_region:%s " ;
283+ break ;
284+ default :
285+ $ output ->writeln (
286+ sprintf (
287+ '<error>Invalid value for sql_gen: %s.</error> ' ,
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) ' ,
0 commit comments