From dda472f96d4b3f790fcaacad6773522aadc253f9 Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Sat, 18 May 2013 23:25:14 -0400 Subject: [PATCH 1/7] Implemented MySQL file imports --- PHPCI/Plugin/Mysql.php | 76 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/PHPCI/Plugin/Mysql.php b/PHPCI/Plugin/Mysql.php index a8d377b8..589617ab 100644 --- a/PHPCI/Plugin/Mysql.php +++ b/PHPCI/Plugin/Mysql.php @@ -14,17 +14,28 @@ use PDO; /** * MySQL Plugin - Provides access to a MySQL database. * @author Dan Cryer +* @author Steve Kamerman * @package PHPCI * @subpackage Plugins */ class Mysql implements \PHPCI\Plugin { + + /** + * @var \PHPCI\Builder + */ protected $phpci; protected $queries = array(); protected $host; protected $user; protected $pass; + + /** + * Database Connection + * @var PDO + */ + protected $pdo; public function __construct(\PHPCI\Builder $phpci, array $options = array()) { @@ -51,18 +62,75 @@ class Mysql implements \PHPCI\Plugin */ public function execute() { + $success = true; + try { $opts = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); - $pdo = new PDO('mysql:host=' . $this->host, $this->user, $this->pass, $opts); - + $this->pdo = new PDO('mysql:host=' . $this->host, $this->user, $this->pass, $opts); + foreach ($this->queries as $query) { - $pdo->query($query); + if (!is_array($query)) { + // Simple query + $this->pdo->query($query); + } else { + // SQL file execution + $this->executeFile($query); + } } } catch (\Exception $ex) { $this->phpci->logFailure($ex->getMessage()); return false; } + return $success; + } + + protected function executeFile($query) + { + if (!isset($query['import'])) { + throw new \Exception("Import statement must contiain an 'import' key"); + } + + $import_file = $query['import']; + if (!is_readable($import_file)) { + throw new \Exception("Cannot open SQL import file"); + } + + $database = isset($query['database'])? $query['database']: null; + + $import_command = $this->getImportCommand($import_file, $database); + if (!$this->phpci->executeCommand($import_command)) { + throw new \Exception("Unable to execute SQL file"); + } + return true; } -} + + /** + * Builds the MySQL import command required to import/execute the specified file + * @param string $import_file Path to file, relative to the build root + * @param string $database If specified, this database is selected before execution + * @return string + */ + protected function getImportCommand($import_file, $database=null) { + $decompression = array( + 'bz2' => '| bzip2 --decompress', + 'gz' => '| gzip --decompress', + ); + + $extension = strtolower(pathinfo($import_file, PATHINFO_EXTENSION)); + $decomp_cmd = ''; + if (array_key_exists($extension, $decompression)) { + $decomp_cmd = $decompression[$extension]; + } + + $args = array( + ':import_file' => escapeshellarg($import_file), + ':decomp_cmd' => $decomp_cmd, + ':user' => escapeshellarg($this->user), + ':pass' => escapeshellarg($this->pass), + ':database' => ($database === null)? '': escapeshellarg($database), + ); + return strtr('cat :import_file :decomp_cmd | mysql -u:user -p:pass :database', $args); + } +} \ No newline at end of file From 60894db8dc547ab97fb14a75613fcb0f81bd268d Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Sat, 18 May 2013 23:33:39 -0400 Subject: [PATCH 2/7] Syntax changes --- PHPCI/Plugin/Mysql.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/PHPCI/Plugin/Mysql.php b/PHPCI/Plugin/Mysql.php index 589617ab..41f99aa2 100644 --- a/PHPCI/Plugin/Mysql.php +++ b/PHPCI/Plugin/Mysql.php @@ -72,9 +72,9 @@ class Mysql implements \PHPCI\Plugin if (!is_array($query)) { // Simple query $this->pdo->query($query); - } else { + } else if (isset($query['import'])) { // SQL file execution - $this->executeFile($query); + $this->executeFile($query['import']); } } } catch (\Exception $ex) { @@ -87,11 +87,11 @@ class Mysql implements \PHPCI\Plugin protected function executeFile($query) { - if (!isset($query['import'])) { - throw new \Exception("Import statement must contiain an 'import' key"); + if (!isset($query['file'])) { + throw new \Exception("Import statement must contiain an 'file' key"); } - $import_file = $query['import']; + $import_file = $query['file']; if (!is_readable($import_file)) { throw new \Exception("Cannot open SQL import file"); } From c03d5f3af366ac1f8b26769826cecc3bfe491d14 Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Sat, 18 May 2013 23:40:30 -0400 Subject: [PATCH 3/7] Fixed file path problem --- PHPCI/Plugin/Mysql.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PHPCI/Plugin/Mysql.php b/PHPCI/Plugin/Mysql.php index 41f99aa2..1d261300 100644 --- a/PHPCI/Plugin/Mysql.php +++ b/PHPCI/Plugin/Mysql.php @@ -91,9 +91,9 @@ class Mysql implements \PHPCI\Plugin throw new \Exception("Import statement must contiain an 'file' key"); } - $import_file = $query['file']; + $import_file = $this->phpci->buildPath . '/' . $query['file']; if (!is_readable($import_file)) { - throw new \Exception("Cannot open SQL import file"); + throw new \Exception("Cannot open SQL import file: $import_file"); } $database = isset($query['database'])? $query['database']: null; From 5f66ff6199398322e3f9eda75f13cf0a7b4fc421 Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Sat, 18 May 2013 23:46:26 -0400 Subject: [PATCH 4/7] Fixed file path problem --- PHPCI/Plugin/Mysql.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PHPCI/Plugin/Mysql.php b/PHPCI/Plugin/Mysql.php index 1d261300..e67c31a4 100644 --- a/PHPCI/Plugin/Mysql.php +++ b/PHPCI/Plugin/Mysql.php @@ -91,7 +91,7 @@ class Mysql implements \PHPCI\Plugin throw new \Exception("Import statement must contiain an 'file' key"); } - $import_file = $this->phpci->buildPath . '/' . $query['file']; + $import_file = $this->phpci->buildPath . $query['file']; if (!is_readable($import_file)) { throw new \Exception("Cannot open SQL import file: $import_file"); } From 7245b351a0c262e2915a624ed763e9da915c1517 Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Sun, 19 May 2013 01:47:41 -0400 Subject: [PATCH 5/7] Implemented environment plugin --- PHPCI/Builder.php | 8 ++++++++ PHPCI/Plugin/Env.php | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 PHPCI/Plugin/Env.php diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 34c934b3..5a26be8d 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -246,6 +246,14 @@ class Builder $buildId = 'project' . $this->build->getProject()->getId() . '-build' . $this->build->getId(); $this->ciDir = realpath(dirname(__FILE__) . '/../') . '/'; $this->buildPath = $this->ciDir . 'build/' . $buildId . '/'; + + // Setup environment vars that will be accessible during exec() + putenv("PHPCI=1"); + putenv("PHPCI_COMMIT=".$commitId); + putenv("PHPCI_PROJECT=".$this->build->getProject()->getId()); + putenv("PHPCI_BUILD=".$this->build->getId()); + putenv("PHPCI_PROJECT_TITLE=".$this->build->getProject()->getTitle()); + putenv("PHPCI_BUILD_PATH=".$this->buildPath); // Create a working copy of the project: if (!$this->build->createWorkingCopy($this, $this->buildPath)) { diff --git a/PHPCI/Plugin/Env.php b/PHPCI/Plugin/Env.php new file mode 100644 index 00000000..e052ef6f --- /dev/null +++ b/PHPCI/Plugin/Env.php @@ -0,0 +1,46 @@ + +* @package PHPCI +* @subpackage Plugins +*/ +class Env implements \PHPCI\Plugin +{ + protected $phpci; + protected $env_vars; + + public function __construct(\PHPCI\Builder $phpci, array $options = array()) + { + $this->phpci = $phpci; + $this->env_vars = $options; + } + + /** + * Adds the specified environment variables to the builder environment + */ + public function execute() + { + $success = true; + foreach ($this->env_vars as $key => $value) { + // This allows the developer to specify env vars like " - FOO=bar" or " - FOO: bar" + $env_var = is_array($value)? key($value).'='.current($value): $value; + + if (!putenv($env_var)) { + $success = false; + $this->phpci->logFailure("Unable to set environment variable"); + } + } + return $success; + } +} From 3b72e124915b501b854ed63ae75e96a439c75911 Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Sun, 19 May 2013 11:28:27 -0400 Subject: [PATCH 6/7] Removed unused local var --- PHPCI/Plugin/Env.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PHPCI/Plugin/Env.php b/PHPCI/Plugin/Env.php index e052ef6f..4984d100 100644 --- a/PHPCI/Plugin/Env.php +++ b/PHPCI/Plugin/Env.php @@ -32,7 +32,7 @@ class Env implements \PHPCI\Plugin public function execute() { $success = true; - foreach ($this->env_vars as $key => $value) { + foreach ($this->env_vars as $value) { // This allows the developer to specify env vars like " - FOO=bar" or " - FOO: bar" $env_var = is_array($value)? key($value).'='.current($value): $value; From e04e49bb32dc9a0bec60fee2cb86900ff61853e0 Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Sun, 19 May 2013 16:54:35 -0400 Subject: [PATCH 7/7] Added support for standard YAML key/value pairs --- PHPCI/Plugin/Env.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/PHPCI/Plugin/Env.php b/PHPCI/Plugin/Env.php index 4984d100..1de016fb 100644 --- a/PHPCI/Plugin/Env.php +++ b/PHPCI/Plugin/Env.php @@ -32,9 +32,14 @@ class Env implements \PHPCI\Plugin public function execute() { $success = true; - foreach ($this->env_vars as $value) { - // This allows the developer to specify env vars like " - FOO=bar" or " - FOO: bar" - $env_var = is_array($value)? key($value).'='.current($value): $value; + foreach ($this->env_vars as $key => $value) { + if (is_numeric($key)) { + // This allows the developer to specify env vars like " - FOO=bar" or " - FOO: bar" + $env_var = is_array($value)? key($value).'='.current($value): $value; + } else { + // This allows the standard syntax: "FOO: bar" + $env_var = "$key=$value"; + } if (!putenv($env_var)) { $success = false;