Skip to content

Commit

Permalink
SshCommand: add option to also connect to tools.db (#59)
Browse files Browse the repository at this point in the history
This is for convenience for apps that need a tools-db connection

Make --bind-address require a value

Also add .editorconfig
  • Loading branch information
MusikAnimal committed Jul 14, 2023
1 parent c349b9b commit be60595
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
17 changes: 15 additions & 2 deletions Command/SshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}

/**
Expand All @@ -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'];
Expand All @@ -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));
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)%'
```

</details>
Expand All @@ -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:
Expand Down

0 comments on commit be60595

Please sign in to comment.