From be60595e3a14f30dcb28ceafae2b020a7cb64f67 Mon Sep 17 00:00:00 2001 From: MusikAnimal Date: Fri, 14 Jul 2023 11:56:42 -0400 Subject: [PATCH] SshCommand: add option to also connect to tools.db (#59) This is for convenience for apps that need a tools-db connection Make --bind-address require a value Also add .editorconfig --- .editorconfig | 12 ++++++++++++ Command/SshCommand.php | 17 +++++++++++++++-- README.md | 11 +++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ec7e61a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# https://EditorConfig.org + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +# tab indentation +[*.{php,js,less,twig}] +indent_style = space +indent_size = 4 diff --git a/Command/SshCommand.php b/Command/SshCommand.php index f85fcf1..5d35392 100644 --- a/Command/SshCommand.php +++ b/Command/SshCommand.php @@ -68,9 +68,15 @@ protected function configure(): void $this->addOption( 'bind-address', 'b', - InputOption::VALUE_OPTIONAL, + InputOption::VALUE_REQUIRED, "Sets the binding address of the SSH tunnel. For Docker installations you may need to set this to 0.0.0.0" ); + $this->addOption( + 'toolsdb', + null, + InputOption::VALUE_NONE, + 'Sets up an SSH tunnel to tools.db.svc.wikimedia.cloud' + ); } /** @@ -84,10 +90,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int $username = $input->getArgument('username'); $service = $input->getOption('service'); $bindAddress = $input->getOption('bind-address'); + $toolsDb = $input->getOption('toolsdb'); $host = "$service".self::HOST_SUFFIX; $login = $username.'@'.self::LOGIN_URL; - $output->writeln("Connecting to *.$host via $login... use ^C to cancel or terminate connection."); + $toolsDbStr = $toolsDb ? ' and tools'.self::HOST_SUFFIX : ''; + $output->writeln("Connecting to *.$host$toolsDbStr via $login... use ^C to cancel or terminate connection."); $slices = array_unique(array_values($this->client->getDbList())); $processArgs = ['ssh', '-N']; @@ -99,6 +107,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $processArgs[] = $arg; } + if ($toolsDb) { + $processArgs[] = '-L'; + $port = $this->client->getPortForSlice('toolsdb'); + $processArgs[] = $port.':tools'.self::HOST_SUFFIX.':3306'; + } $processArgs[] = $login; $process = Process::fromShellCommandline(implode(' ', $processArgs)); diff --git a/README.md b/README.md index 9e56128..3d1ac73 100644 --- a/README.md +++ b/README.md @@ -264,6 +264,12 @@ doctrine: port: '%env(REPLICAS_PORT_S8)%' user: '%env(REPLICAS_USERNAME)%' password: '%env(REPLICAS_PASSWORD)%' + # If you need to work with toolsdb + toolforge_toolsdb: + host: '%env(TOOLSDB_HOST)%' + port: '%env(TOOLSDB_PORT)%' + user: '%env(TOOLSDB_USERNAME)%' + password: '%env(TOOLSDB_PASSWORD)%' ``` @@ -284,6 +290,11 @@ Next, establish an SSH tunnel to the replicas (only necessary on local environme Use the `--bind-address` flag to change the binding address, if needed. This may be necessary for Docker installations. +If you need to work against [tools-db](https://wikitech.wikimedia.org/wiki/Help:Toolforge/Database#User_databases), +pass the `--toolsdb` flag and make sure the `TOOLSBD_` env variables are set correctly. +Unless you have a private database, you should be able to use the same username and password +as `REPLICAS_USERNAME` and `REPLICAS_PASSWORD`. + To query the replicas, inject the `ReplicasClient` service then call the `getConnection()` method, passing in a valid database, and you should get a `Doctrine\DBAL\Connection` object. For example: