-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathserver.py
113 lines (91 loc) · 2.89 KB
/
server.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
# -*- coding: utf-8 -*-
"""
Configure Server
"""
import os
import time
import pexpect
import platform
import argparse
import ConfigParser as CP
from SimpleXMLRPCServer import SimpleXMLRPCServer
docker = False
def get_state():
check_start()
m = os.popen('service mysql status')
s = m.readlines()[2]
s = s.split(':')[1].replace(' ', '').split('(')[0]
if s == 'failed':
return -1
return 1
def check_start():
a = sudo_exec('sudo tail -1 /var/log/mysql/ubunturmw.err', '123456')
a = a.strip('\n\r')
if a.find('pid ended') != -1:
sudo_exec('sudo service mysql start', '123456')
def sudo_exec(cmdline, passwd):
osname = platform.system()
if osname == 'Linux':
prompt = r'\[sudo\] password for %s: ' % os.environ['USER']
elif osname == 'Darwin':
prompt = 'Password:'
else:
assert False, osname
child = pexpect.spawn(cmdline)
idx = child.expect([prompt, pexpect.EOF], 3)
if idx == 0:
child.sendline(passwd)
child.expect(pexpect.EOF)
return child.before
def start_mysql(instance_name, configs):
"""
Args:
instance_name: str, MySQL Server Instance Name eg. ["mysql1", "mysql2"]
configs: str, Formatted MySQL Parameters, e.g. "name:value,name:value"
"""
params = configs.split(',')
if docker:
_params = ''
for param in params:
pair_ = param.split(':')
_params += ' --%s=%s' % (pair_[0], pair_[1])
sudo_exec('sudo docker stop %s' % instance_name, '123456')
sudo_exec('sudo docker rm %s' % instance_name, '123456')
time.sleep(2)
cmd = 'sudo docker run --name mysql1 -e MYSQL_ROOT_PASSWORD=12345678 ' \
'-d -p 0.0.0.0:3365:3306 -v /data/{}/:/var/lib/mysql mysql:5.6 {}'.format(instance_name, _params)
print(cmd)
sudo_exec(cmd, '123456')
else:
write_cnf_file(params)
sudo_exec('sudo service mysql restart', '123456')
time.sleep(5)
return 1
def write_cnf_file(configs):
"""
Args:
configs: str, Formatted MySQL Parameters, e.g. "--binlog_size=xxx"
"""
cnf_file = '/etc/mysql/my.cnf'
config_parser = CP.ConfigParser()
sudo_exec('sudo chmod 777 %s' % cnf_file, '123456')
time.sleep(2)
config_parser.read(cnf_file)
for param in configs:
pair_ = param.split(':')
config_parser.set('mysqld', pair_[0], pair_[1])
config_parser.write(open(cnf_file, 'w'))
sudo_exec('sudo chmod 744 %s' % cnf_file, '123456')
time.sleep(2)
def serve():
server = SimpleXMLRPCServer(('0.0.0.0', 20000))
server.register_function(start_mysql)
server.register_function(get_state)
server.serve_forever()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--docker', action='store_true')
opt = parser.parse_args()
if opt.docker:
docker = True
serve()