-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli
executable file
·90 lines (72 loc) · 2.5 KB
/
cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
import os
import sys
import logging
from client.cli import CLI
from client.log import Log
import argparse
parser = argparse.ArgumentParser(description='Postgres Admin')
parser.add_argument('command', help="Commands: script | grants")
parser.add_argument('--script',
help='The script to run')
parser.add_argument('--user',
help='The user you want to include in SQL commands')
parser.add_argument('--out',
help='A file to write the output')
parser.add_argument('--params',
help='Space delimited string of name1=value1 pairs')
# parser.add_argument('--project',
# help='research project')
# parser.add_argument('--token',
# help='token')
# parser.add_argument('--private', default=False, action='store_true',
# help='make the repository private so Issues are enabled')
# parser.add_argument('--repo',
# help='repository')
# parser.add_argument('--external_url',
# help='external_url')
# parser.add_argument('--branch',
# help='branch')
# parser.add_argument('--team',
# help='share team project')
# parser.add_argument('--destroy', default=False, type=bool,
# help='destroy objects')
args = parser.parse_args()
print(args.command)
log = Log(os.environ['LOG_LEVEL'], args.out)
creds = {
"PGDATABASE": os.environ['PGDATABASE']
}
if args.command == "script":
script = args.script
params = {}
if args.params is not None:
for arg in args.params.split(' '):
print(arg)
parts = arg.split('=')
params[parts[0]] = parts[1]
admin = CLI(creds, False)
try:
admin.execute_template("sql/%s.sql.tpl" % script, **params)
finally:
admin.close()
if args.command == "grants":
admin = CLI(creds, False)
script = "list_all_grants"
user = args.user
print(user)
try:
users =admin.get_users()
if user not in users:
raise Exception("User not found %s" % user)
dbs = admin.get_databases()
print("DATABASES : %s" % dbs)
for db in dbs:
print("--- DATABASE %s" % db)
specificdb = CLI({"PGDATABASE":db}, False)
try:
specificdb.execute_template("sql/%s.sql.tpl" % script, DATABASE=db, USER=user)
finally:
specificdb.close()
finally:
admin.close()