Add myssh script
This commit is contained in:
parent
5a99f3eda7
commit
39b7da4adc
4 changed files with 160 additions and 4 deletions
|
|
@ -21,7 +21,7 @@
|
|||
./kicad.nix
|
||||
./mpv.nix
|
||||
./mute-indicator.nix
|
||||
./mycli.nix
|
||||
./mycli
|
||||
./neo.nix
|
||||
./neovim.nix
|
||||
./nix-index.nix
|
||||
|
|
|
|||
|
|
@ -194,6 +194,10 @@
|
|||
complete -c mycli -f -s d -l dsn -r -a '(mycli --list-dsn)'
|
||||
'';
|
||||
|
||||
xdg.configFile."fish/completions/myssh.fish".text = ''
|
||||
complete -c myssh -f -a '(myssh --list)'
|
||||
'';
|
||||
|
||||
xdg.configFile."fish/completions/just.fish".source = pkgs.runCommand "just-fish-completions" { } ''
|
||||
${pkgs.just}/bin/just --completions fish > $out
|
||||
'';
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ let
|
|||
)
|
||||
ini;
|
||||
|
||||
solarized = import ./solarized.nix;
|
||||
solarized = import ../solarized.nix;
|
||||
|
||||
config = {
|
||||
main = {
|
||||
|
|
@ -133,8 +133,18 @@ let
|
|||
};
|
||||
in
|
||||
{
|
||||
home.packages = with pkgs; [
|
||||
mycli
|
||||
home.packages = [
|
||||
pkgs.mycli
|
||||
(pkgs.stdenv.mkDerivation {
|
||||
name = "myssh";
|
||||
propagatedBuildInputs = [
|
||||
(pkgs.python3.withPackages (pp: with pp; [
|
||||
pyyaml
|
||||
]))
|
||||
];
|
||||
dontUnpack = true;
|
||||
installPhase = "install -Dm755 ${./myssh.py} $out/bin/myssh";
|
||||
})
|
||||
];
|
||||
|
||||
xdg.configFile = lib.attrsets.mapAttrs'
|
||||
142
users/jalr/modules/mycli/myssh.py
Executable file
142
users/jalr/modules/mycli/myssh.py
Executable file
|
|
@ -0,0 +1,142 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
|
||||
def get_db_connecition_from_typo3_config(ssh_host, ssh_user, config_file):
|
||||
php_code = """
|
||||
<?php
|
||||
error_reporting(E_ALL);
|
||||
|
||||
$config_file = $argv[1];
|
||||
$db = (require $config_file)["DB"]["Connections"]["Default"];
|
||||
print(json_encode($db));
|
||||
"""
|
||||
|
||||
try:
|
||||
proc = subprocess.run(
|
||||
[
|
||||
"ssh",
|
||||
"-l",
|
||||
ssh_user,
|
||||
ssh_host,
|
||||
"--",
|
||||
"php",
|
||||
"-d",
|
||||
"display_errors=1",
|
||||
"--",
|
||||
config_file,
|
||||
],
|
||||
check=True,
|
||||
input=php_code,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error: Command failed with return code {e.returncode}")
|
||||
print(f"stdout: {e.stdout}")
|
||||
print(f"stderr: {e.stderr}")
|
||||
raise
|
||||
|
||||
return json.loads(proc.stdout)
|
||||
|
||||
|
||||
def get_db_connection_from_env_file(ssh_host, ssh_user, staging_level):
|
||||
try:
|
||||
proc = subprocess.run(
|
||||
[
|
||||
"ssh",
|
||||
"-l",
|
||||
ssh_user,
|
||||
ssh_host,
|
||||
"--",
|
||||
"cat",
|
||||
f"/srv/services/app/app_{staging_level}/app/.env",
|
||||
],
|
||||
check=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error: Command failed with return code {e.returncode}")
|
||||
print(f"stdout: {e.stdout}")
|
||||
print(f"stderr: {e.stderr}")
|
||||
raise
|
||||
|
||||
return {
|
||||
s[0]: s[1]
|
||||
for line in proc.stdout.splitlines()
|
||||
if (s := line.split("=", 1)) and len(s) > 1
|
||||
}
|
||||
|
||||
|
||||
def connect(connection_name, connection):
|
||||
if "t3_config" in connection:
|
||||
db = get_db_connecition_from_typo3_config(
|
||||
connection["ssh_host"], connection["ssh_user"], connection["t3_config"]
|
||||
)
|
||||
db_host = db["host"]
|
||||
db_user = db["user"]
|
||||
db_password = db["password"]
|
||||
db_name = db["dbname"]
|
||||
elif "staging_level" in connection:
|
||||
env = get_db_connection_from_env_file(
|
||||
connection["ssh_host"], connection["ssh_user"], connection["staging_level"]
|
||||
)
|
||||
db_host = env["TYPO3_DB_HOST"]
|
||||
db_user = env["TYPO3_DB_USER"]
|
||||
db_password = env["TYPO3_DB_PASSWORD"]
|
||||
db_name = env["TYPO3_DB_DATABASE"]
|
||||
|
||||
os.execl(
|
||||
"/usr/bin/env",
|
||||
"env",
|
||||
"mycli",
|
||||
"--prompt",
|
||||
f"{connection_name}>",
|
||||
"--ssh-user",
|
||||
connection["ssh_user"],
|
||||
"--ssh-host",
|
||||
connection["ssh_host"],
|
||||
"-h",
|
||||
db_host,
|
||||
"-u",
|
||||
db_user,
|
||||
"-p",
|
||||
db_password,
|
||||
db_name,
|
||||
)
|
||||
|
||||
|
||||
def read_config(path):
|
||||
with open(path, "r") as fh:
|
||||
config = yaml.safe_load(fh.read())
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def main():
|
||||
connections = read_config(os.path.expanduser("~/.config/mycli/connections.yaml"))
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--list", action="store_true")
|
||||
parser.add_argument("connection", type=str, nargs="?")
|
||||
|
||||
args = vars(parser.parse_args())
|
||||
|
||||
if args["list"]:
|
||||
print("\n".join(connections.keys()))
|
||||
|
||||
if args["connection"] is not None:
|
||||
connect(args["connection"], connections[args["connection"]])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue