<commit_before><?php
class Vps_Controller_Action_Cli_Web_SetupOnlineController extends Vps_Controller_Action_Cli_Abstract
{
    public static function getHelp()
    {
        return "setup online";
    }

    public static function getHelpOptions()
    {
        $ret = array();
        $ret[] = array(
            'param' => 'server',
            'value' => 'vivid-test-server,test,production',
            'valueOptional' => true
        );
        $ret[] = array('param' => 'debug');
        return $ret;
    }

    private function _systemSshVps($config, $cmd)
    {
        $sshHost = $config->server->user.'@'.$config->server->host;
        $sshDir = $config->server->dir;

        $cmd = "ssh -p {$config->server->port} $sshHost ".escapeshellarg("cd $sshDir && ".Vps_Util_Git::getAuthorEnvVars()." $cmd");
        if ($this->_getParam('debug')) {
            echo $cmd."\n";
        }
        return $this->_systemCheckRet($cmd);
    }

    private function _execSql($config, $sql)
    {
        $sshHost = $config->server->user.'@'.$config->server->host;
        $sshDir = $config->server->dir;
        $cmd = "echo ".escapeshellarg($sql)." | mysql";
        $cmd = "ssh -p {$config->server->port} $sshHost ".escapeshellarg("$cmd");
        $ret = null;
        /*if ($this->_getParam('debug'))*/ echo "$cmd\n";
        system($cmd, $ret);
        return !$ret;
    }

    public function indexAction()
    {
        if (Vps_Registry::get('config')->server->host != 'vivid') {
            throw new Vps_Exception("setup-online may only be called on local vivid server");
        }

        $servers = explode(',', $this->_getParam('server'));
        $setupVividTestServer = false;
        foreach ($servers as $server) {
            $config = Vps_Config_Web::getInstance($server);
            $sshHost = $config->server->user.'@'.$config->server->host;
            $sshDir = $config->server->dir;
            $cmd = "if [ -d $sshDir ]; then echo \"yes\"; else echo \"no\"; fi";
            $cmd = "ssh -p {$config->server->port} $sshHost ".escapeshellarg($cmd);
            if ($this->_getParam('debug')) echo "$cmd\n";
            if (trim(exec($cmd)) != "yes") {
                if ($server != 'vivid-test-server') {
                    throw new Vps_Exception_Client("$sshDir ist nicht vorhanden.");
                } else {
                    $setupVividTestServer = true;
                    continue;
                }
            }
            $cmd = "ssh -p {$config->server->port} $sshHost ".escapeshellarg("cd $sshDir && ls -l");
            if ($this->_getParam('debug')) echo "$cmd\n";
            $l = trim(exec($cmd));
            if ($l != "total 0" && $l != "insgesamt 0") {
                throw new Vps_Exception_Client("$server ist nicht leer, bitte alle Dateien löschen.");
            }
        }

        if ($setupVividTestServer) {
            $config = Vps_Config_Web::getInstance('vivid-test-server');
            $sshHost = $config->server->user.'@'.$config->server->host;
            $args = array(
                $config->application->id,
                $config->server->dir,
                $config->server->domain
            );
            $cmd  = "sudo /usr/local/bin/setup-web";
            foreach ($args as $a) {
                $cmd .= " ".escapeshellarg($a);
            }
            $cmd = "ssh -p {$config->server->port} $sshHost ".escapeshellarg($cmd);
            if ($this->_getParam('debug')) echo "$cmd\n";
            $this->_systemCheckRet($cmd);
        }


        $servers = explode(',', $this->_getParam('server'));
        foreach ($servers as $server) {
            $config = Vps_Config_Web::getInstance($server);

            if ($server != 'vivid-test-server' && in_array('web', $config->server->databases->toArray())) {
                if (!isset($dbPassword)) {
                    $filter = new Vps_Filter_Random(16);
                    $dbPassword = $filter->filter('');
                    $dbUser = Vps_Registry::get('config')->application->id;
                    while (strlen($dbUser) > 16) {
                        echo "Kann MySQL Benutzername $dbUser ist laenger als 16 Zeichen (".strlen($dbUser)."),\n";
                        echo "darf jedoch max. 16 Zeichen haben, neuer Name: ";
                        $stdin = fopen('php://stdin', 'r');
                        $dbUser = trim(strtolower(fgets($stdin, 128)));
                        fclose($stdin);
                    }
                    $createSql = "CREATE USER '$dbUser'@'localhost' IDENTIFIED BY '$dbPassword'";
                    if (!$this->_execSql($config, $createSql)) {
                        echo "Kann Benutzer $dbUser nicht anlegen, er existiert\n";
                        echo "moeglicherweise bereits.\n";
                        echo "* (l)oeschen und neu anlegen\n";
                        echo "* bestehenden verwenden und (p)asswort eingeben\n";
                        $stdin = fopen('php://stdin', 'r');
                        $input = trim(strtolower(fgets($stdin, 2)));
                        fclose($stdin);
                        if ($input == 'l') {
                            $this->_execSql($config, "DROP USER '$dbUser'@'localhost'");
                            $createSql = "CREATE USER '$dbUser'@'localhost' IDENTIFIED BY '$dbPassword'";
                            if (!$this->_execSql($config, $createSql)) {
                                throw new Vps_ClientException("Kann Benutzer nicht anlegen");
                            }
                        } else if ($input == 'p') {
                            $stdin = fopen('php://stdin', 'r');
                            echo "enter password: ";
                            $dbPassword = trim(fgets($stdin, 128));
                            fclose($stdin);
                        } else {
                            throw new Vps_ClientException("unbekannte option");
                        }
                    }

                    $sql = "GRANT FILE ON *.* TO '$dbUser'@'localhost'";
                    $cmd = "echo ".escapeshellarg($sql)." | mysql";
                    $cmd = "ssh -p {$config->server->port} $sshHost ".escapeshellarg("$cmd");
                    if ($this->_getParam('debug')) echo "$cmd\n";
                    system($cmd, $ret);
                    if ($ret) {
                        throw new Vps_ClientException("Konnte berechtigungen nicht setzen");
                    }

                    $dbName = Vps_Registry::get('config')->application->id;
                    $sql = "GRANT ALL PRIVILEGES ON `$dbName%` . * TO '$dbUser'@'localhost'";
                    $cmd = "echo ".escapeshellarg($sql)." | mysql";
                    $cmd = "ssh -p {$config->server->port} $sshHost ".escapeshellarg("$cmd");
                    if ($this->_getParam('debug')) echo "$cmd\n";
                    system($cmd, $ret);
                    if ($ret) {
                        throw new Vps_ClientException("Konnte berechtigungen nicht setzen");<commit_msg>fix bug mit laaangem bart: berechtiungen von angelegten sql benutzern passten nicht<commit_after><?php
class Vps_Controller_Action_Cli_Web_SetupOnlineController extends Vps_Controller_Action_Cli_Abstract
{
    public static function getHelp()
    {
        return "setup online";
    }

    public static function getHelpOptions()
    {
        $ret = array();
        $ret[] = array(
            'param' => 'server',
            'value' => 'vivid-test-server,test,production',
            'valueOptional' => true
        );
        $ret[] = array('param' => 'debug');
        return $ret;
    }

    private function _systemSshVps($config, $cmd)
    {
        $sshHost = $config->server->user.'@'.$config->server->host;
        $sshDir = $config->server->dir;

        $cmd = "ssh -p {$config->server->port} $sshHost ".escapeshellarg("cd $sshDir && ".Vps_Util_Git::getAuthorEnvVars()." $cmd");
        if ($this->_getParam('debug')) {
            echo $cmd."\n";
        }
        return $this->_systemCheckRet($cmd);
    }

    private function _execSql($config, $sql)
    {
        $sshHost = $config->server->user.'@'.$config->server->host;
        $sshDir = $config->server->dir;
        $cmd = "echo ".escapeshellarg($sql)." | mysql";
        $cmd = "ssh -p {$config->server->port} $sshHost ".escapeshellarg("$cmd");
        $ret = null;
        /*if ($this->_getParam('debug'))*/ echo "$cmd\n";
        system($cmd, $ret);
        return !$ret;
    }

    public function indexAction()
    {
        if (Vps_Registry::get('config')->server->host != 'vivid') {
            throw new Vps_Exception("setup-online may only be called on local vivid server");
        }

        $servers = explode(',', $this->_getParam('server'));
        $setupVividTestServer = false;
        foreach ($servers as $server) {
            $config = Vps_Config_Web::getInstance($server);
            $sshHost = $config->server->user.'@'.$config->server->host;
            $sshDir = $config->server->dir;
            $cmd = "if [ -d $sshDir ]; then echo \"yes\"; else echo \"no\"; fi";
            $cmd = "ssh -p {$config->server->port} $sshHost ".escapeshellarg($cmd);
            if ($this->_getParam('debug')) echo "$cmd\n";
            if (trim(exec($cmd)) != "yes") {
                if ($server != 'vivid-test-server') {
                    throw new Vps_Exception_Client("$sshDir ist nicht vorhanden.");
                } else {
                    $setupVividTestServer = true;
                    continue;
                }
            }
            $cmd = "ssh -p {$config->server->port} $sshHost ".escapeshellarg("cd $sshDir && ls -l");
            if ($this->_getParam('debug')) echo "$cmd\n";
            $l = trim(exec($cmd));
            if ($l != "total 0" && $l != "insgesamt 0") {
                throw new Vps_Exception_Client("$server ist nicht leer, bitte alle Dateien löschen.");
            }
        }

        if ($setupVividTestServer) {
            $config = Vps_Config_Web::getInstance('vivid-test-server');
            $sshHost = $config->server->user.'@'.$config->server->host;
            $args = array(
                $config->application->id,
                $config->server->dir,
                $config->server->domain
            );
            $cmd  = "sudo /usr/local/bin/setup-web";
            foreach ($args as $a) {
                $cmd .= " ".escapeshellarg($a);
            }
            $cmd = "ssh -p {$config->server->port} $sshHost ".escapeshellarg($cmd);
            if ($this->_getParam('debug')) echo "$cmd\n";
            $this->_systemCheckRet($cmd);
        }


        $servers = explode(',', $this->_getParam('server'));
        foreach ($servers as $server) {
            $config = Vps_Config_Web::getInstance($server);

            if ($server != 'vivid-test-server' && in_array('web', $config->server->databases->toArray())) {
                if (!isset($dbPassword)) {
                    $filter = new Vps_Filter_Random(16);
                    $dbPassword = $filter->filter('');
                    $dbUser = Vps_Registry::get('config')->application->id;
                    while (strlen($dbUser) > 16) {
                        echo "Kann MySQL Benutzername $dbUser ist laenger als 16 Zeichen (".strlen($dbUser)."),\n";
                        echo "darf jedoch max. 16 Zeichen haben, neuer Name: ";
                        $stdin = fopen('php://stdin', 'r');
                        $dbUser = trim(strtolower(fgets($stdin, 128)));
                        fclose($stdin);
                    }
                    $createSql = "CREATE USER '$dbUser'@'localhost' IDENTIFIED BY '$dbPassword'";
                    if (!$this->_execSql($config, $createSql)) {
                        echo "Kann Benutzer $dbUser nicht anlegen, er existiert\n";
                        echo "moeglicherweise bereits.\n";
                        echo "* (l)oeschen und neu anlegen\n";
                        echo "* bestehenden verwenden und (p)asswort eingeben\n";
                        $stdin = fopen('php://stdin', 'r');
                        $input = trim(strtolower(fgets($stdin, 2)));
                        fclose($stdin);
                        if ($input == 'l') {
                            $this->_execSql($config, "DROP USER '$dbUser'@'localhost'");
                            $createSql = "CREATE USER '$dbUser'@'localhost' IDENTIFIED BY '$dbPassword'";
                            if (!$this->_execSql($config, $createSql)) {
                                throw new Vps_Exception_Client("Kann Benutzer nicht anlegen");
                            }
                        } else if ($input == 'p') {
                            $stdin = fopen('php://stdin', 'r');
                            echo "enter password: ";
                            $dbPassword = trim(fgets($stdin, 128));
                            fclose($stdin);
                        } else {
                            throw new Vps_Exception_Client("unbekannte option");
                        }
                    }

                    $sql = "GRANT FILE ON *.* TO '$dbUser'@'localhost'";
                    if (!$this->_execSql($config, $sql)) {
                        throw new Vps_Exception_Client("Konnte berechtigungen nicht setzen");
                    }

                    $dbName = Vps_Registry::get('config')->application->id;
                    $sql = "GRANT ALL PRIVILEGES ON `$dbName%` . * TO '$dbUser'@'localhost'";
                    if (!$this->_execSql($config, $sql)) {
                        throw new Vps_Exception_Client("Konnte berechtigungen nicht setzen");
{'fraction_non_alphanumeric': 0.15681818181818183, 'fraction_numerical': 0.00249266862170088, 'mean_word_length': 1.6102181400688864, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6537094', 'n_tokens_mistral': 3955, 'n_tokens_neox': 3766, 'n_words': 804}