Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
feature(Setup/Cli): add --mysql command
Browse files Browse the repository at this point in the history
... to access the systems mysql client
 with the configured db params
  • Loading branch information
pschuele committed Dec 13, 2022
1 parent 1c37115 commit 0c44edd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tine20/Setup/Frontend/Cli.php
Expand Up @@ -134,6 +134,8 @@ public function handle(Zend_Console_Getopt $_opts, $exitAfterHandle = true)
$this->_restore($_opts);
} elseif(isset($_opts->compare)) {
$this->_compare($_opts);
} elseif(isset($_opts->mysql)) {
$this->_mysqlClient($_opts);
} elseif(isset($_opts->setpassword)) {
$this->_setPassword($_opts);
} elseif(isset($_opts->pgsqlMigration)) {
Expand Down Expand Up @@ -1497,4 +1499,56 @@ protected function _addAuthToken(Zend_Console_Getopt $_opts)
echo "Auth token created: " . print_r($result, true) . PHP_EOL;
}
}

/**
* allows to call the mysql-client with the configured db params
*
* @param Zend_Console_Getopt $_opts
* @return int
*
* TODO add more platforms?
* TODO use .my.cnf file? needs to be deleted afterwards (like in backup/restore)
* TODO use better process control library? i.e. https://symfony.com/doc/current/components/process.html
*/
protected function _mysqlClient(Zend_Console_Getopt $_opts)
{
$options = $this->_parseRemainingArgs($_opts->getRemainingArgs());
if (! empty($options['platform'])) {
switch ($options['platform']) {
case 'docker': // maybe add "alpine"?
// install mysql client if not available
system('apk add mysql-client');
}
}

$dbConf = Tinebase_Core::getConfig()->database;
$command ='mysql -h ' . $dbConf->host . ' -p' . $dbConf->password . ' -u ' . $dbConf->username
. ' ' . $dbConf->dbname;
$descriptorspec = [
0 => array("pty"),
1 => array("pty"),
2 => array("pty")
];
$process = proc_open($command, $descriptorspec, $pipes);

stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
stream_set_blocking(STDIN,0);
do {
echo stream_get_contents($pipes[1]);
echo stream_get_contents($pipes[2]);
while ($in = fgets(STDIN)) {
fwrite($pipes[0], $in);
if ($in[0] === "\004") {
// graceful exit (via EOT / CTRL-D)
break 2;
}
}
} while(is_resource($process));

fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
return proc_close($process);
}
}
4 changes: 4 additions & 0 deletions tine20/Setup/Server/Cli.php
Expand Up @@ -77,6 +77,9 @@ public function handle(\Laminas\Http\Request $request = null, $body = null)
'compare' => 'compare schemas with another database
Examples:
setup.php --compare -- otherdb=tine20other',
'mysql' => 'run mysql client
Examples:
setup.php --mysql -- platform=docker',
'setpassword' => 'set system user password
Examples:
setup.php --setpassword -- username=myusername password=myrandompw',
Expand Down Expand Up @@ -126,6 +129,7 @@ public function handle(\Laminas\Http\Request $request = null, $body = null)
empty($opts->backup) &&
empty($opts->restore) &&
empty($opts->compare) &&
empty($opts->mysql) &&
empty($opts->setpassword) &&
empty($opts->getconfig) &&
empty($opts->upgradeMysql564) &&
Expand Down

0 comments on commit 0c44edd

Please sign in to comment.