From 0f03a621d7a1ae8027923abb8f58e96244751607 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 8 Nov 2018 16:54:38 -0500 Subject: [PATCH 1/3] First round of changes for postgres plugin --- defaults.yaml | 12 +++++++ lib/plugins/TaskRunner/Drupal.js | 9 +++-- lib/plugins/TaskRunner/LAMPApp.js | 48 +++++++++++++++++++++----- lib/plugins/TaskRunner/WordPressApp.js | 2 +- 4 files changed, 56 insertions(+), 15 deletions(-) diff --git a/defaults.yaml b/defaults.yaml index 981e9e60..9eff2d3f 100644 --- a/defaults.yaml +++ b/defaults.yaml @@ -41,6 +41,8 @@ images: port: 80 mysql: command: 'mysqld_safe' + postgresql: + command: 'sudo -u postgres /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main --config_file=/etc/postgresql/9.3/main/postgresql.conf' redis: command: 'redis-server' solr: @@ -75,6 +77,8 @@ images: port: 80 mysql: command: 'mysqld_safe' + postgresql: + command: 'sudo -u postgres /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main --config_file=/etc/postgresql/9.3/main/postgresql.conf' redis: command: 'redis-server' solr: @@ -92,6 +96,8 @@ images: port: 80 mysql: command: 'mysqld_safe' + postgresql: + command: 'sudo -u postgres /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main --config_file=/etc/postgresql/9.3/main/postgresql.conf' redis: command: 'redis-server' solr: @@ -109,6 +115,8 @@ images: port: 80 mysql: command: 'mysqld_safe' + postgresql: + command: 'sudo -u postgres /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.5/main --config_file=/etc/postgresql/9.5/main/postgresql.conf' redis: command: 'redis-server' solr: @@ -126,6 +134,8 @@ images: port: 80 mysql: command: 'mysqld_safe' + postgresql: + command: 'sudo -u postgres /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.5/main --config_file=/etc/postgresql/9.5/main/postgresql.conf' redis: command: 'redis-server' solr: @@ -143,6 +153,8 @@ images: port: 80 mysql: command: 'mysqld_safe' + postgresql: + command: 'sudo -u postgres /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.5/main --config_file=/etc/postgresql/9.5/main/postgresql.conf' redis: command: 'redis-server' solr: diff --git a/lib/plugins/TaskRunner/Drupal.js b/lib/plugins/TaskRunner/Drupal.js index 3a4f3eb4..1c079f55 100644 --- a/lib/plugins/TaskRunner/Drupal.js +++ b/lib/plugins/TaskRunner/Drupal.js @@ -110,7 +110,7 @@ class Drupal extends LAMPApp { } this.addScriptApachePhp(); - this.addScriptMysql(); + this.addScriptDatabase(); } addScriptAppendSettingsPHPSettings() { @@ -134,7 +134,7 @@ class Drupal extends LAMPApp { ` 'password' => '${constants.DATABASE_PASSWORD}',`, ` 'prefix' => '${this.options.databasePrefix}',`, ` 'host' => 'localhost',`, - ` 'driver' => 'mysql',`, + ` 'driver' => '${this.options.databaseEngine}',`, ` ),`, ` ),`, `);`, @@ -162,11 +162,10 @@ class Drupal extends LAMPApp { ` 'password' => '${constants.DATABASE_PASSWORD}',`, ` 'prefix' => '${this.options.databasePrefix}',`, ` 'host' => 'localhost',`, - ` 'driver' => 'mysql',`, + ` 'driver' => '${this.options.databaseEngine}',`, ` 'port' => '3306',`, // This is pretty gross. We should find a better way to handle this. - ` 'namespace' => 'Drupal\\\\\\\\Core\\\\\\\\Database\\\\\\\\Driver\\\\\\\\mysql',`, - ` 'driver' => 'mysql',`, + ` 'namespace' => 'Drupal\\\\\\\\Core\\\\\\\\Database\\\\\\\\Driver\\\\\\\\${this.options.databaseEngine}',`, ` ),`, ` ),`, `);`, diff --git a/lib/plugins/TaskRunner/LAMPApp.js b/lib/plugins/TaskRunner/LAMPApp.js index 74b6be30..b7ac4f57 100644 --- a/lib/plugins/TaskRunner/LAMPApp.js +++ b/lib/plugins/TaskRunner/LAMPApp.js @@ -38,6 +38,8 @@ class LAMPApp extends Script { this.databaseUser = options.databaseUser || 'lampdb'; this.databaseName = options.databaseName || 'lampdb'; + this.options.databasePrefix = options.databasePrefix || ''; + this.options.databaseEngine = options.databaseEngine || 'mysql'; this.options.cliDefines = options.cliDefines || {}; this.options.mysqlCnfOptions = options.mysqlCnfOptions || {}; this.options.phpIniOptions = options.phpIniOptions || {}; @@ -49,6 +51,7 @@ class LAMPApp extends Script { this.options.restartApache = options.restartApache || false; this.options.varnish = options.varnish || {}; this.options.restartMysql = options.restartMysql || false; + this.options.restartPostgresql = options.restartPostgres || false; // TODO: Add some kind of validation. @@ -78,7 +81,7 @@ class LAMPApp extends Script { this.addScriptImportDatabase(); } this.addScriptApachePhp(); - this.addScriptMysql(); + this.addScriptDatabase(); } addScriptHeader() { @@ -106,12 +109,25 @@ class LAMPApp extends Script { } addScriptCreateDatbase() { - this.script = this.script.concat([ - 'echo \'Creating MySQL Database, user and granting access.\'', - 'mysql -e \'create database ' + this.databaseName + '\'', - 'mysql -e \'grant all on ' + this.databaseName + '.* to "root"@"localhost"\'', - 'mysql -e \'flush privileges\'', - ]); + switch (this.options.databaseEngine) { + case 'mysql': + this.script = this.script.concat([ + 'echo \'Creating MySQL Database, user and granting access.\'', + 'mysql -e \'create database ' + this.databaseName + '\'', + 'mysql -e \'grant all on ' + this.databaseName + '.* to "root"@"localhost"\'', + 'mysql -e \'flush privileges\'', + ]); + break; + case 'pgsql': + this.script = this.script.concat([ + 'echo \'Creating PostgreSQL Database, user and granting access.\'', + //'sudo -u postgres createuser root', + 'sudo -u postgres createdb --encoding=UTF8 --owner=root --template=template0 ' + this.databaseName, + //'sudo -u postgres psql -c \'alter user "root" with encrypted password "strongpassword"\'', + //'sudo -u postgres psql -c \'grant all privileges on ' + this.databaseName + ' to "root"\'', + ]); + break; + } } addScriptImportDatabase() { @@ -126,7 +142,14 @@ class LAMPApp extends Script { else { databaseImportBase = 'cat'; } - this.script.push(`${databaseImportBase} $ASSET_DIR/${this.options.database} | $(mysql -u ${constants.DATABASE_USER} --password=${constants.DATABASE_PASSWORD} ${this.databaseName});`); + switch (this.options.databaseEngine) { + case 'mysql': + this.script.push(`${databaseImportBase} $ASSET_DIR/${this.options.database} | $(mysql -u ${constants.DATABASE_USER} --password=${constants.DATABASE_PASSWORD} ${this.databaseName});`); + break; + case 'pgsql': + this.script.push(`sudo -u postgres ${databaseImportBase} $ASSET_DIR/${this.options.database} | $(psql ${this.databaseName});`); + break; + } this.script.push(`rm $ASSET_DIR/${this.options.database};`); } } @@ -154,9 +177,10 @@ class LAMPApp extends Script { } // a meta-script that installs all mysql related scripts. - addScriptMysql() { + addScriptDatabase() { this.addScriptMysqlCnfOptions(); this.addScriptRestartMysql(); + this.addScriptRestartPostgresql(); } @@ -293,6 +317,12 @@ class LAMPApp extends Script { } } + addScriptRestartPostgresql() { + if (this.options.restartMysql) { + this.script = this.script.concat('service postgresql restart'); + } + } + isEmptyObject(o) { return !Object.keys(o).length; } diff --git a/lib/plugins/TaskRunner/WordPressApp.js b/lib/plugins/TaskRunner/WordPressApp.js index a49e0892..852b9cb5 100644 --- a/lib/plugins/TaskRunner/WordPressApp.js +++ b/lib/plugins/TaskRunner/WordPressApp.js @@ -82,7 +82,7 @@ class WordPressApp extends LAMPApp { this.addScriptFlushCaches(); } this.addScriptApachePhp(); - this.addScriptMysql(); + this.addScriptDatabase(); } /** From e2f00aebe3132c386e6a585d906d904bbbf27a64 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 13 Nov 2018 13:47:18 -0500 Subject: [PATCH 2/3] Adding postgres options into comment header, adding postgresql command to 14.04-5.6 startup --- defaults.yaml | 2 ++ lib/plugins/TaskRunner/LAMPApp.js | 7 +++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/defaults.yaml b/defaults.yaml index 9eff2d3f..a8dc43c5 100644 --- a/defaults.yaml +++ b/defaults.yaml @@ -60,6 +60,8 @@ images: port: 80 mysql: command: 'mysqld_safe' + postgresql: + command: 'sudo -u postgres /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main --config_file=/etc/postgresql/9.3/main/postgresql.conf' redis: command: 'redis-server' solr: diff --git a/lib/plugins/TaskRunner/LAMPApp.js b/lib/plugins/TaskRunner/LAMPApp.js index b7ac4f57..4650ba38 100644 --- a/lib/plugins/TaskRunner/LAMPApp.js +++ b/lib/plugins/TaskRunner/LAMPApp.js @@ -12,7 +12,8 @@ class LAMPApp extends Script { * @param {object} container - An instantiated and configured Container object. * @param {object} options - A hash of configuration options specific to this task. * @param {string} [options.database] - The file name of the database to import if specified. Note that this database *must be added to the assets array separately*. - * @param {string} [options.databaseName] - The name of the database to use. + * @param {string} [options.database] - The file name of the database to import if specified. Note that this database *must be added to the assets array separately*. + * @param {string} [options.databaseEngine] - The name of the database engine to use. Must be either `mysql` or `pgsql`. Defaults to `mysql`. * @param {string} [options.databaseUser] - The username of the database to use. * @param {boolean} [options.databaseGzipped] - Whether the database was sent gzipped and whether it should therefore be gunzipped before importing. * @param {boolean} [options.databaseBzipped] - Whether the database was sent bzipped and whether it should therefore be bunzipped before importing. @@ -31,6 +32,7 @@ class LAMPApp extends Script { * Options are {'enabled' (boolean, defaults to false) and 'pathToVcl' (string)}. * @param {boolean} [options.restartMysql] - Whether to restart MySQL. If mysqlCnfOptions is set, MySQL will be restarted automatically, * so you probably won't need to use this. + * @param {boolean} [options.restartPostgresql] - Whether to restart PostgreSQL. */ constructor(container, options) { @@ -121,10 +123,7 @@ class LAMPApp extends Script { case 'pgsql': this.script = this.script.concat([ 'echo \'Creating PostgreSQL Database, user and granting access.\'', - //'sudo -u postgres createuser root', 'sudo -u postgres createdb --encoding=UTF8 --owner=root --template=template0 ' + this.databaseName, - //'sudo -u postgres psql -c \'alter user "root" with encrypted password "strongpassword"\'', - //'sudo -u postgres psql -c \'grant all privileges on ' + this.databaseName + ' to "root"\'', ]); break; } From f796b797e7bcb4be9955f20bef42e79bdde23324 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 13 Nov 2018 16:05:13 -0500 Subject: [PATCH 3/3] Tests for postgreSQL plugin --- lib/plugins/TaskRunner/LAMPApp.js | 12 ++++-- test/tasks/DrupalApp.js | 62 +++++++++++++++++++++---------- test/tasks/LAMPApp.js | 12 ++++++ 3 files changed, 63 insertions(+), 23 deletions(-) diff --git a/lib/plugins/TaskRunner/LAMPApp.js b/lib/plugins/TaskRunner/LAMPApp.js index 4650ba38..2bbaa846 100644 --- a/lib/plugins/TaskRunner/LAMPApp.js +++ b/lib/plugins/TaskRunner/LAMPApp.js @@ -177,9 +177,15 @@ class LAMPApp extends Script { // a meta-script that installs all mysql related scripts. addScriptDatabase() { - this.addScriptMysqlCnfOptions(); - this.addScriptRestartMysql(); - this.addScriptRestartPostgresql(); + switch (this.options.databaseEngine) { + case 'mysql': + this.addScriptMysqlCnfOptions(); + this.addScriptRestartMysql(); + break; + case 'pgsql': + this.addScriptRestartPostgresql(); + break; + } } diff --git a/test/tasks/DrupalApp.js b/test/tasks/DrupalApp.js index 00af8b1d..6a88316f 100644 --- a/test/tasks/DrupalApp.js +++ b/test/tasks/DrupalApp.js @@ -16,8 +16,10 @@ const mockContainer = { describe('Drupal App', function() { let options; let options2; + let options3; let app; let app2; + let app3; before(function(done) { options = { @@ -26,11 +28,18 @@ describe('Drupal App', function() { }; options2 = { database: 'my-cool-db.sql', + databaseEngine: 'mysql', databaseGzipped: true, clearCaches: false, databasePrefix: 'my_custom_prefix', varnish: { enable: true }, }; + options3 = { + database: 'my-cool-db.sql', + databaseEngine: 'pgsql', + databaseGzipped: true, + clearCaches: false, + }; done(); }); @@ -45,6 +54,11 @@ describe('Drupal App', function() { app2.should.have.property('id').which.is.a.String; app2.id.should.match(/[0-9a-z]{16}/g); + app3 = new DrupalApp(mockContainer, options3); + app3.should.be.ok; + app3.should.have.property('id').which.is.a.String; + app3.id.should.match(/[0-9a-z]{16}/g); + done(); }); @@ -73,11 +87,11 @@ describe('Drupal App', function() { let support = app.drupalVersionSupported(); support.should.be.ok; - const app3 = new DrupalApp(mockContainer, Object.assign({}, options, {drupalVersion: 1})); - app3.script = []; - app3.addScriptUnsupportedDrupalVersion(); - app3.script.should.have.length(2); - support = app3.drupalVersionSupported(); + const app4 = new DrupalApp(mockContainer, Object.assign({}, options, {drupalVersion: 1})); + app4.script = []; + app4.addScriptUnsupportedDrupalVersion(); + app4.script.should.have.length(2); + support = app4.drupalVersionSupported(); support.should.not.be.ok; done(); }); @@ -90,11 +104,11 @@ describe('Drupal App', function() { app.script.should.eql(['drush --root=/var/www/html cache-clear all']); // testing D8 - const app3 = new DrupalApp(mockContainer, Object.assign({}, options, {drupalVersion: 8})); - app3.script = []; - app3.addScriptClearCaches(); - app3.script.should.have.length(1); - app3.script.should.eql(['drush --root=/var/www/html cache-rebuild']); + const app4 = new DrupalApp(mockContainer, Object.assign({}, options, {drupalVersion: 8})); + app4.script = []; + app4.addScriptClearCaches(); + app4.script.should.have.length(1); + app4.script.should.eql(['drush --root=/var/www/html cache-rebuild']); done(); }); @@ -161,11 +175,11 @@ describe('Drupal App', function() { }); it('should append custom settings to script', function(done) { - const app3 = new DrupalApp(mockContainer, Object.assign({}, options, {settingsRequireFile: 'dummy.php', settingsAppend: 'dummy'})); - app3.script = []; - app3.appendCustomSettings(); - app3.script.should.have.length(2); - app3.script.should.eql([ + const app4 = new DrupalApp(mockContainer, Object.assign({}, options, {settingsRequireFile: 'dummy.php', settingsAppend: 'dummy'})); + app4.script = []; + app4.appendCustomSettings(); + app4.script.should.have.length(2); + app4.script.should.eql([ 'echo "require_once(\'dummy.php\');" >> /var/www/html/sites/default/settings.php', 'echo dummy >> /var/www/html/sites/default/settings.php', ]); @@ -175,7 +189,7 @@ describe('Drupal App', function() { it('should add D8 settings', function(done) { app.script = []; app.addD8PHPSettings(); - app.script.should.have.length(24); + app.script.should.have.length(23); done(); }); @@ -183,9 +197,17 @@ describe('Drupal App', function() { app.script = []; app.addD7PHPSettings(); app.script.should.have.length(19); + app.script.should.containEql(' \'driver\' => \'mysql\','); done(); }); + it('should add postgreSQL settings', function(done) { + app3.script = []; + app3.addD7PHPSettings(); + app3.script.should.have.length(19); + app3.script.should.containEql(' \'driver\' => \'pgsql\','); + done(); + }); it('should add Drupal settings and any custom settings', function(done) { // testing D7 (default) @@ -194,10 +216,10 @@ describe('Drupal App', function() { app.script.should.have.length(19); // testing D8 - const app3 = new DrupalApp(mockContainer, Object.assign({}, options, {drupalVersion: 8})); - app3.script = []; - app3.addScriptAppendSettingsPHPSettings(); - app3.script.should.have.length(24); + const app4 = new DrupalApp(mockContainer, Object.assign({}, options, {drupalVersion: 8})); + app4.script = []; + app4.addScriptAppendSettingsPHPSettings(); + app4.script.should.have.length(23); done(); }); diff --git a/test/tasks/LAMPApp.js b/test/tasks/LAMPApp.js index a35d18ac..e36e8231 100644 --- a/test/tasks/LAMPApp.js +++ b/test/tasks/LAMPApp.js @@ -59,6 +59,14 @@ describe('LAMP App', function() { }; var appGZ = new LAMPApp(mockContainer, optionsGZ); + var optionsPgsql = { + database: 'my-cool-db.sql', + databaseEngine: 'pgsql', + databaseName: 'my-cool-db', + databaseGzipped: false, + }; + var appPgsql = new LAMPApp(mockContainer, optionsPgsql); + it('builds proper lamp script', function() { app.script.should.containEql('mkdir -p $SRC_DIR; cd $SRC_DIR'); @@ -73,6 +81,10 @@ describe('LAMP App', function() { `cat $ASSET_DIR/my-cool-db.sql | $(mysql -u ${constants.DATABASE_USER} --password=${constants.DATABASE_PASSWORD} my-cool-db)` ); + appPgsql.script.should.not.containEql('mysql -e \'create database my-cool-db\''); + appPgsql.script.should.containEql( + `sudo -u postgres cat $ASSET_DIR/my-cool-db.sql | $(psql my-cool-db);` + ); }); it('handles gzipped databases', function() {