#!/usr/bin/env python3 """Look up which user account to attach the swarm-program token to.""" import os, shlex, sys, paramiko db_pass = os.environ.get("MGR_DB_PASS", "") if not db_pass: print("MGR_DB_PASS not set", file=sys.stderr) sys.exit(2) c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("20.24.50.121", 22, "heicode", os.environ["MGR_PASS"], look_for_keys=False, allow_agent=False, timeout=30) sql = ''' SELECT id, username, email, "group", role, status FROM users WHERE email IN ('zsbgnw@gmail.com', 'chenchen@xinghanlab.com') OR username = 'chenchen' OR role >= 10 ORDER BY id LIMIT 10; ''' # Wrap via psql exec inside the heicode container. cmd = ( 'sudo docker exec heicode sh -c ' + shlex.quote( "PGPASSWORD=" + shlex.quote(db_pass) + " psql " + '-h heicode.postgres.database.azure.com -U heicode heicode ' + "-c " + shlex.quote(sql.replace("\n", " ")) ) ) _, o, e = c.exec_command(cmd, timeout=60, get_pty=True) for line in iter(o.readline, ""): if not line: break sys.stdout.write(line.encode("ascii","replace").decode("ascii")) sys.stdout.flush() err = e.read().decode("ascii","replace") if err.strip(): sys.stderr.write(err)