-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
autosetup.py
executable file
·227 lines (186 loc) · 6.46 KB
/
autosetup.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
#coding: UTF-8
import os
from os.path import abspath, basename, exists, dirname, join
import sys
import argparse
import re
from collections import namedtuple
import requests
from pexpect import spawn
from utils import green, red, debug, info, warning, cd, shell, chdir, setup_logging
USERNAME = 'test@seafiletest.com'
PASSWORD = 'testtest'
ADMIN_USERNAME = 'admin@seafiletest.com'
ADMIN_PASSWORD = 'adminadmin'
ServerConfig = namedtuple('ServerConfig', [
'installdir',
'tarball',
'version',
])
def setup_server(cfg, db):
'''Setup seafile server with the setup-seafile.sh script. We use pexpect to
interactive with the setup process of the script.
'''
info('uncompressing server tarball')
shell('tar xf seafile-server_{}_x86-64.tar.gz -C {}'
.format(cfg.version, cfg.installdir))
if db == 'mysql':
autosetup_mysql(cfg)
else:
autosetup_sqlite3(cfg)
with open(join(cfg.installdir, 'conf/seahub_settings.py'), 'a') as fp:
fp.write('\n')
fp.write('DEBUG = True')
fp.write('\n')
fp.write('''\
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_RATES': {
'ping': '600/minute',
'anon': '1000/minute',
'user': '1000/minute',
},
}''')
fp.write('\n')
def autosetup_sqlite3(cfg):
setup_script = get_script(cfg, 'setup-seafile.sh')
shell('''sed -i -e '/^check_root;.*/d' "{}"'''.format(setup_script))
info('setting up seafile server with pexepct, script %s', setup_script)
answers = [
('ENTER', ''),
# server name
('server name', 'my-seafile'),
# ip or domain
('ip or domain', '127.0.0.1'),
# seafile data dir
('seafile-data', ''),
# fileserver port
('seafile fileserver', ''),
('ENTER', ''),
('ENTER', ''),
]
_answer_questions(setup_script, answers)
def createdbs():
sql = '''\
create database `ccnet-existing` character set = 'utf8';
create database `seafile-existing` character set = 'utf8';
create database `seahub-existing` character set = 'utf8';
create user 'seafile'@'localhost' identified by 'seafile';
GRANT ALL PRIVILEGES ON `ccnet-existing`.* to `seafile`@localhost;
GRANT ALL PRIVILEGES ON `seafile-existing`.* to `seafile`@localhost;
GRANT ALL PRIVILEGES ON `seahub-existing`.* to `seafile`@localhost;
'''
shell('mysql -u root', inputdata=sql)
def autosetup_mysql(cfg):
createdbs()
setup_script = get_script(cfg, 'setup-seafile-mysql.sh')
info('setting up seafile server with pexepct, script %s', setup_script)
if not exists(setup_script):
print 'please specify seafile script path'
answers = [
('ENTER', ''),
# server name
('server name', 'my-seafile'),
# ip or domain
('ip or domain', '127.0.0.1'),
# seafile data dir
('seafile-data', ''),
# fileserver port
('seafile fileserver', ''),
# use existing
('choose a way to initialize seafile databases', '2'),
('host of mysql server', ''),
('port of mysql server', ''),
('Which mysql user', 'seafile'),
('password for mysql user', 'seafile'),
('ccnet database', 'ccnet-existing'),
('seafile database', 'seafile-existing'),
('seahub database', 'seahub-existing'),
('ENTER', ''),
]
_answer_questions(abspath(setup_script), answers)
def start_server(cfg):
with cd(cfg.installdir):
shell('find . -maxdepth 2 | sort | xargs ls -lhd')
seafile_sh = get_script(cfg, 'seafile.sh')
shell('{} start'.format(seafile_sh))
info('starting seahub')
seahub_sh = get_script(cfg, 'seahub.sh')
answers = [
# admin email/pass
('admin email', ADMIN_USERNAME),
('admin password', ADMIN_PASSWORD),
('admin password again', ADMIN_PASSWORD),
]
_answer_questions('{} start'.format(abspath(seahub_sh)), answers)
with cd(cfg.installdir):
shell('find . -maxdepth 2 | sort | xargs ls -lhd')
# shell('sqlite3 ccnet/PeerMgr/usermgr.db "select * from EmailUser"', cwd=INSTALLDIR)
shell('http -v localhost:8000/api2/server-info/ || true')
# shell('http -v -f POST localhost:8000/api2/auth-token/ username=admin@seafiletest.com password=adminadmin || true')
shell('netstat -nltp')
def _answer_questions(cmd, answers):
info('expect: spawing %s', cmd)
child = spawn(cmd)
child.logfile = sys.stdout
def autofill(pattern, line):
child.expect(pattern)
child.sendline(line)
for k, v in answers:
autofill(k, v)
child.sendline('')
child.logfile = None
child.interact()
def get_script(cfg, path):
"""
:type cfg: ServerConfig
"""
return join(server_dir(cfg), path)
def server_dir(cfg):
"""
:type cfg: ServerConfig
"""
return join(cfg.installdir, 'seafile-server-{}'.format(cfg.version))
def apiurl(path):
path = path.lstrip('/')
root = os.environ.get('SEAFILE_SERVER', 'http://127.0.0.1:8000')
return '{}/api2/{}'.format(root, path)
def create_test_user(cfg):
data = {'username': ADMIN_USERNAME, 'password': ADMIN_PASSWORD, }
res = requests.post(apiurl('/auth-token/'), data=data)
debug('%s %s', res.status_code, res.text)
token = res.json()['token']
data = {'password': PASSWORD, }
headers = {'Authorization': 'Token ' + token}
res = requests.put(
apiurl('/accounts/{}/'.format(USERNAME)),
data=data,
headers=headers)
assert res.status_code == 201
def main():
ap = argparse.ArgumentParser()
ap.add_argument('-v', '--verbose', action='store_true')
ap.add_argument('--db', choices=('sqlite3', 'mysql'), default='sqlite3')
ap.add_argument('installdir')
ap.add_argument('tarball')
args = ap.parse_args()
if not exists(args.installdir):
print 'directory {} does not exist'.format(args.installdir)
sys.exit(1)
if os.listdir(args.installdir):
print 'directory {} is not empty'.format(args.installdir)
sys.exit(1)
if not exists(args.tarball):
print 'file {} does not exist'.format(args.tarball)
sys.exit(1)
m = re.match(r'^.*?_([\d\.]+).*?\.tar\.gz$', basename(args.tarball))
version = m.group(1)
cfg = ServerConfig(installdir=args.installdir,
tarball=args.tarball,
version=version)
setup_server(cfg, args.db)
start_server(cfg)
create_test_user(cfg)
if __name__ == '__main__':
setup_logging()
main()