Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves wp-gae tool into google/cloud-tools #54

Merged
merged 4 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"bin": [
"src/Utils/Flex/flex_exec",
"scripts/install_test_deps.sh"
"src/Utils/WordPress/wp-gae"
],
"autoload": {
"psr-4": {
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@
<file>src/*.php</file>
</whitelist>
</filter>
<php>
<env name="PHPUNIT_TESTS" value="1"/>
</php>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
* limitations under the License.
*/

namespace Google\Cloud\Utils;
namespace Google\Cloud\Utils\WordPress;

use Exception;
use Google\Cloud\Utils\Project as BaseProject;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
Expand All @@ -26,7 +27,7 @@
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Filesystem\Filesystem;

class WordPressProject extends Project
class Project extends BaseProject
{
const DEFAULT_DIR = 'my-wordpress-project';
const DEFAULT_DB_REGION = 'us-central1';
Expand Down
24 changes: 24 additions & 0 deletions src/Utils/WordPress/files/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
runtime: php72

# Defaults to "serve index.php" and "serve public/index.php". Can be used to
# serve a custom PHP front controller (e.g. "serve backend/index.php") or to
# run a long-running PHP script as a worker process (e.g. "php worker.php").
entrypoint: serve gae-app.php

handlers:
- url: /(.*\.(htm|html|css|js))
static_files: \1
upload: .*\.(htm|html|css|js)$

- url: /wp-content/(.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg))
static_files: wp-content/\1
upload: wp-content/.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg)$

- url: /(.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg))
static_files: \1
upload: .*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg)$

- url: /wp-includes/images/media/(.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg))
static_files: wp-includes/images/media/\1
upload: wp-includes/images/media/.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg)$

4 changes: 4 additions & 0 deletions src/Utils/WordPress/files/cron.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cron:
- description: "wordpress cron tasks"
url: /wp-cron.php
schedule: every 15 minutes
53 changes: 53 additions & 0 deletions src/Utils/WordPress/files/gae-app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* This file handles all routing for a WordPress project running on App Engine.
* It serves up the appropriate PHP file depending on the request URI.
*
* @see https://cloud.google.com/appengine/docs/standard/php7/runtime#application_startup
*/

/**
* Function to return a PHP file to load based on the request URI.
*
* @param string $full_request_uri The request URI derivded from $_SERVER['REQUEST_URI'].
*/
function get_real_file_to_load($full_request_uri)
{
$request_uri = @parse_url($full_request_uri)['path'];

// Redirect /wp-admin to /wp-admin/ (adds a trailing slash)
if ($request_uri === '/wp-admin') {
header('Location: /wp-admin/');
exit;
}

// Serve up "index.php" when /wp-admin/ is requested
if ($request_uri === '/wp-admin/') {
return '/wp-admin/index.php';
}

// Load the file requested if it exists
if (is_file(__DIR__ . $request_uri)) {
return $request_uri;
}

// Send everything else through index.php
return '/index.php';
}

// fixes b/111391534
$_SERVER['HTTPS'] = $_SERVER['HTTP_X_APPENGINE_HTTPS'];

// Loads the expected WordPress framework file
// (e.g index.php, wp-admin/* or wp-login.php)
$file = get_real_file_to_load($_SERVER['REQUEST_URI']);

// Set the environment variables to reflect the script we're loading
// (in order to trick WordPress)
$_SERVER['DOCUMENT_URI'] = $_ENV['DOCUMENT_URI'] = $file;
$_SERVER['PHP_SELF'] = $_ENV['PHP_SELF'] = $file;
$_SERVER['SCRIPT_NAME'] = $_ENV['SCRIPT_NAME'] = $file;
$_SERVER['SCRIPT_FILENAME'] = $_ENV['SCRIPT_FILENAME'] = __DIR__ . $file;

require __DIR__ . $file;
5 changes: 5 additions & 0 deletions src/Utils/WordPress/files/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
allow_url_include = "1"
upload_max_filesize = 8M

; for debugging purposes only. Please remove for production instances
display_errors=On
122 changes: 122 additions & 0 deletions src/Utils/WordPress/files/wp-config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* The base configurations of the WordPress.
*
* This file has the following configurations: MySQL settings, Table Prefix,
* Secret Keys, WordPress Language, and ABSPATH. You can find more information
* by visiting {@link http://codex.wordpress.org/Editing_wp-config.php Editing
* wp-config.php} Codex page. You can get the MySQL settings from your web host.
*
* This file is used by the wp-config.php creation script during the
* installation. You don't have to use the web site, you can just copy this file
* to "wp-config.php" and fill in the values.
*
* @package WordPress
*/

// $onGae is true on production.
if (isset($_SERVER['GAE_ENV'])) {
$onGae = true;
} else {
$onGae = false;
}

// Cache settings
// Disable cache for now, as this does not work on App Engine for PHP 7.2
define('WP_CACHE', false);

// Disable pseudo cron behavior
define('DISABLE_WP_CRON', true);

// Determine HTTP or HTTPS, then set WP_SITEURL and WP_HOME
if ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)) {
$protocol_to_use = 'https://';
} else {
$protocol_to_use = 'http://';
}
if (isset($_SERVER['HTTP_HOST'])) {
define('HTTP_HOST', $_SERVER['HTTP_HOST']);
} else {
define('HTTP_HOST', 'localhost');
}
define('WP_SITEURL', $protocol_to_use . HTTP_HOST);
define('WP_HOME', $protocol_to_use . HTTP_HOST);

// ** MySQL settings - You can get this info from your web host ** //
if ($onGae) {
/** The name of the Cloud SQL database for WordPress */
define('DB_NAME', 'YOUR_DB_NAME');
/** Production login info */
define('DB_HOST', ':/cloudsql/YOUR_DB_CONNECTION');
define('DB_USER', 'YOUR_DB_USER');
define('DB_PASSWORD', 'YOUR_DB_PASSWORD');
} else {
/** The name of the local database for WordPress */
define('DB_NAME', 'YOUR_DB_NAME');
/** Local environment MySQL login info */
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'YOUR_LOCAL_DB_USER');
define('DB_PASSWORD', 'YOUR_LOCAL_DB_PASSWORD');
}

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

/**#@+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* @since 2.6.0
*/
define('AUTH_KEY', 'YOUR_AUTH_KEY');
define('SECURE_AUTH_KEY', 'YOUR_SECURE_AUTH_KEY');
define('LOGGED_IN_KEY', 'YOUR_LOGGED_IN_KEY');
define('NONCE_KEY', 'YOUR_NONCE_KEY');
define('AUTH_SALT', 'YOUR_AUTH_SALT');
define('SECURE_AUTH_SALT', 'YOUR_SECURE_AUTH_SALT');
define('LOGGED_IN_SALT', 'YOUR_LOGGED_IN_SALT');
define('NONCE_SALT', 'YOUR_NONCE_SALT');

/**#@-*/
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each a unique
* prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';

/**
* WordPress Localized Language, defaults to English.
*
* Change this to localize WordPress. A corresponding MO file for the chosen
* language must be installed to wp-content/languages. For example, install
* de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
* language support.
*/
define('WPLANG', '');

/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*/
define('WP_DEBUG', !$onGae);

/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if (!defined('ABSPATH')) {
define('ABSPATH', dirname(__FILE__) . '/wordpress/');
}

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
120 changes: 120 additions & 0 deletions src/Utils/WordPress/wp-gae
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

if (file_exists(__DIR__ . '/../../../vendor/autoload.php')) {
// Run the command from the git clone.
require_once __DIR__ . '/../../../vendor/autoload.php';
} else if (file_exists(__DIR__ . '/../../../../../../vendor/autoload.php')) {
// Run the command from the vendor dir installation.
require_once __DIR__ . '/../../../../../../vendor/autoload.php';
}

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Google\Cloud\Utils\WordPress\Project;

$wordPressOptions = new InputDefinition([
new InputOption('project_id', null, InputOption::VALUE_REQUIRED, 'Google Cloud project id'),
new InputOption('db_region', null, InputOption::VALUE_REQUIRED, 'Cloud SQL region'),
new InputOption('db_instance', null, InputOption::VALUE_REQUIRED, 'Cloud SQL instance id'),
new InputOption('db_name', null, InputOption::VALUE_REQUIRED, 'Cloud SQL database name'),
new InputOption('db_user', null, InputOption::VALUE_REQUIRED, 'Cloud SQL database username'),
new InputOption('db_password', null, InputOption::VALUE_REQUIRED, 'Cloud SQL database password'),
new InputOption('local_db_user', null, InputOption::VALUE_REQUIRED, 'Local SQL database username'),
new InputOption('local_db_password', null, InputOption::VALUE_REQUIRED, 'Local SQL database password'),
]);

$application = new Application('Wordpress Helper');
$application->add(new Command('create'))
->setDescription('Create a new WordPress site for Google Cloud')
->setDefinition(clone $wordPressOptions)
->addOption('dir', null, InputOption::VALUE_REQUIRED, 'Directory for the new project', Project::DEFAULT_DIR)
->addOption('wordpress_url', null, InputOption::VALUE_REQUIRED, 'URL of the WordPress archive', Project::LATEST_WP)
->setCode(function (InputInterface $input, OutputInterface $output) {
$wordpress = new Project($input, $output);
// Run the wizard to prompt user for project and database parameters.
$dir = $wordpress->promptForProjectDir();

// download wordpress
$wordpress->downloadWordpress($dir);

// initialize the project and download the plugins
$wordpress->initializeProject($dir);
$wordpress->downloadGcsPlugin();

$dbParams = $wordpress->initializeDatabase();

// populate random key params
$params = $dbParams + $wordpress->generateRandomValueParams();

// copy all the sample files into the project dir and replace parameters
$wordpress->copyFiles(__DIR__ . '/files', [
//'.gcloudignore' => '/',
'app.yaml' => '/',
'cron.yaml' => '/',
'php.ini' => '/',
'gae-app.php' => '/',
'wp-config.php' => '/',
], $params);

$output->writeln("<info>Your WordPress project is ready at $dir</info>");
});

$application->add(new Command('update'))
->setDescription('Update an existing WordPress site for Google Cloud')
->setDefinition(clone $wordPressOptions)
->addArgument('dir', InputArgument::REQUIRED, 'Directory for the existing project')
->setCode(function (InputInterface $input, OutputInterface $output) {
// use the supplied dir for the wordpress project
$dir = $input->getArgument('dir');

$wordpress = new Project($input, $output);
$wordpress->initializeProject($dir);

// Download the plugins if they don't exist
if (!file_exists($dir . '/wp-content/plugins/gcs')) {
$wordpress->downloadGcsPlugin();
}

$dbParams = $wordpress->initializeDatabase();

// populate random key params
$params = $dbParams + $wordpress->generateRandomValueParams();

// copy all the sample files into the project dir and replace parameters
$wordpress->copyFiles(__DIR__ . '/files', [
//'.gcloudignore' => '/',
'app.yaml' => '/',
'cron.yaml' => '/',
'php.ini' => '/',
'gae-app.php' => '/',
'wp-config.php' => '/',
], $params);

$output->writeln("<info>Your WordPress project has been updated at $dir</info>");
});

if (getenv('PHPUNIT_TESTS') === '1') {
return $application;
}

$application->run();
Loading