forked from pinterest/mysql_utils
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mysql_cli.py
executable file
·154 lines (136 loc) · 5.81 KB
/
mysql_cli.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
import argparse
from lib import environment_specific
import socket
import string
import subprocess
from lib import host_utils
from lib import mysql_lib
MYSQL_CLI = ('/usr/bin/mysql -A -h {host} -P {port} {sql_safe} '
'--user={user} --password={password} '
'--prompt="\h:\p \d \u> " {db}')
# if we just want to run a command and disconnect, no
# point in setting a prompt.
MYSQL_CLI_EX = ('/usr/bin/mysql -A -h {host} -P {port} {sql_safe} '
'--user={user} --password={password} '
'{db} -e "{execute}"')
DEFAULT_ROLE = 'read-only'
def main():
parser = argparse.ArgumentParser()
parser.add_argument('db',
help='What server, shard or replica set to connect to '
'(ie sharddb021b[:3306], db00003, pbdata03862, '
'follower_zendata001002)')
parser.add_argument('-p',
'--privileges',
help=''.join(('Default is ', DEFAULT_ROLE)),
default=DEFAULT_ROLE,
choices=environment_specific.CLI_ROLES.keys())
parser.add_argument('-l',
'--longquery',
default=False,
action='store_true',
help='For standard read or write access, use this '
'flag if you expect the query to take more than '
'10 seconds.')
parser.add_argument('-e',
'--execute',
help='An optional SQL command to run.',
default=False)
args = parser.parse_args()
zk = host_utils.MysqlZookeeper()
host = None
db = ''
role_modifier = 'default'
long_query = ''
if args.longquery:
role_modifier = 'long'
long_query = '(long queries enabled)'
# check if db exists in dns, if so the supplied argument will be considered
# a hostname, otherwise a replica set.
try:
socket.gethostbyname(args.db)
host = host_utils.HostAddr(args.db)
log.info('{db} appears to be a hostname'.format(db=args.db))
except:
log.info('{db} appears not to be a hostname'.format(db=args.db))
# Maybe it is a replica set
if not host:
config = zk.get_all_mysql_config()
if args.db in config:
master = config[args.db]['master']
log.info('{db} appears to be a replica set'.format(db=args.db))
host = host_utils.HostAddr(''.join((master['host'],
':',
str(master['port']))))
else:
log.info('{db} appears not to be a replica set'.format(db=args.db))
# Perhaps a shard?
if not host:
shard_map = zk.get_host_shard_map()
for master in shard_map:
if args.db in shard_map[master]:
log.info('{db} appears to be a shard'.format(db=args.db))
host = host_utils.HostAddr(master)
db = environment_specific.convert_shard_to_db(args.db)
break
if not host:
log.info('{db} appears not to be a shard'.format(db=args.db))
if not host:
raise Exception('Could not determine what host to connect to')
log.info('Will connect to {host} with {privileges} '
'privileges {lq}'.format(host=host,
privileges=args.privileges,
lq=long_query))
(username, password) = mysql_lib.get_mysql_user_for_role(
environment_specific.CLI_ROLES[args.privileges][role_modifier])
# we may or may not know what replica set we're connecting to at
# this point.
sql_safe = ''
try:
replica_set, _ = zk.get_replica_set_from_instance(host)
except Exception as e:
if 'is not in zk' in e.message:
log.warning('SERVER IS NOT IN ZK!!!')
replica_set = None
else:
raise
try:
# do we need a prompt?
if replica_set in environment_specific.EXTRA_PARANOID_REPLICA_SETS:
if args.privileges in ['read-write', 'admin']:
resp = raw_input("You've asked for {priv} access to replica "
"set {rs}. Are you sure? (Y/N): ".format(
priv=args.privileges,
rs=replica_set))
if not resp or resp[0] not in ['Y', 'y']:
raise Exception('Connection aborted by user!')
# should we enable safe-updates?
if replica_set in environment_specific.PARANOID_REPLICA_SETS:
if args.privileges in ['read-write', 'admin']:
sql_safe = '--init-command="SET SESSION SQL_SAFE_UPDATES=ON"'
except Exception as e:
log.error("Unable to continue: {}".format(e))
return
if args.execute:
execute_escaped = string.replace(args.execute, '"', '\\"')
cmd = MYSQL_CLI_EX.format(host=host.hostname,
port=host.port,
db=db,
user=username,
password=password,
sql_safe=sql_safe,
execute=execute_escaped)
else:
cmd = MYSQL_CLI.format(host=host.hostname,
port=host.port,
db=db,
user=username,
password=password,
sql_safe=sql_safe)
log.info(cmd)
proc = subprocess.Popen(cmd, shell=True)
proc.wait()
if __name__ == "__main__":
log = environment_specific.setup_logging_defaults(__name__)
main()