forked from fanshengshuai/VPSMate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
284 lines (238 loc) · 9.56 KB
/
install.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012, VPSMate development team
# All rights reserved.
#
# VPSMate is distributed under the terms of the (new) BSD License.
# The full license can be found in 'LICENSE.txt'.
""" Install Script for VPSMate """
import getpass
import os
import platform
import shlex
import socket
import subprocess
import sys
class Install(object):
def __init__(self):
self.user = getpass.getuser()
if (self.user != 'root'):
print('Must run in root')
sys.exit()
if hasattr(platform, 'linux_distribution'):
self.dist = platform.linux_distribution(full_distribution_name=0)
else:
self.dist = platform.dist()
self.arch = platform.machine()
if self.arch != 'x86_64': self.arch = 'i386'
self.installpath = '/usr/local/vpsmate'
self.distname = self.dist[0].lower()
self.version = self.dist[1]
self.version = self.version[0:self.version.find('.', self.version.index('.') + 1)]
self.os = platform.system()
print 'Platform %s %s [%s]' % (self.dist[0], self.dist[1], self.os)
def _run(self, cmd, shell=False):
if shell:
return subprocess.call(cmd, shell=shell)
else:
return subprocess.call(shlex.split(cmd))
def check_platform(self):
supported = True
if self.distname == 'centos':
if float(self.version) < 5.4:
supported = False
elif self.distname == 'redhat':
if float(self.version) < 5.4:
supported = False
elif self.os == 'Darwin':
supported = True
# elif self.distname == 'ubuntu':
# if float(self.version) < 10.10:
# supported = False
# elif self.distname == 'debian':
# if float(self.version) < 6.0:
# supported = False
else:
supported = False
return supported
def check_git(self):
supported = True
try:
if self.distname == 'centos' or self.distname == 'redhat':
self._run("yum install -y git")
except:
pass
# if self.distname == 'centos':
# if float(self.version) < 5.4:
# supported = False
# elif self.distname == 'redhat':
# if float(self.version) < 5.4:
# supported = False
# elif self.os == 'Darwin':
# supported = True
# #elif self.distname == 'ubuntu':
# # if float(self.version) < 10.10:
# # supported = False
# #elif self.distname == 'debian':
# # if float(self.version) < 6.0:
# # supported = False
# else:
# supported = False
return supported
def install_python(self):
if self.distname in ('centos', 'redhat'):
# following this: http://fedoraproject.org/wiki/EPEL/FAQ
if int(float(self.version)) == 5:
epelrpm = 'epel-release-5-4.noarch.rpm'
epelurl = 'http://download.fedoraproject.org/pub/epel/5/%s/%s' % (self.arch, epelrpm)
# install fastestmirror plugin for yum
fastestmirror = 'http://mirror.centos.org/centos/5/os/%s/CentOS/yum-fastestmirror-1.1.16-21.el5.centos.noarch.rpm' % (
self.arch,)
self._run('rpm -Uvh %s' % fastestmirror)
elif int(float(self.version)) == 6:
epelrpm = 'epel-release-6-7.noarch.rpm'
epelurl = 'http://download.fedoraproject.org/pub/epel/6/%s/' % (self.arch, epelrpm)
fastestmirror = 'http://mirror.centos.org/centos/6/os/%s/Packages/yum-plugin-fastestmirror-1.1.30-14.el6.noarch.rpm' % (
self.arch,)
self._run('rpm -Uvh %s' % fastestmirror)
elif int(float(self.version)) == 7:
epelrpm = 'epel-release-7-6.noarch.rpm'
epelurl = 'http://dl.fedoraproject.org/pub/epel/7/%s/e/%s' % (self.arch, epelrpm)
fastestmirror = 'http://mirror.centos.org/centos/7/os/%s/Packages/kabi-yum-plugins-1.0-3.el7.centos.noarch.rpm' % (
self.arch,)
self._run('rpm -Uvh %s' % fastestmirror)
self._run('wget -nv -c %s' % epelurl)
self._run('rpm -Uvh %s' % epelrpm)
self._run('yum -y install python26')
if self.distname == 'centos':
pass
elif self.distname == 'redhat':
pass
elif self.distname == 'ubuntu':
pass
elif self.distname == 'debian':
pass
def install_vpsmate(self):
# localpkg_found = False
# if os.path.exists(os.path.join(os.path.dirname(__file__), 'vpsmate.tar.gz')):
# # local install package found
# localpkg_found = True
# else:
# # or else install online
# print '* Downloading install package from www.vpsmate.org'
# f = urllib2.urlopen('http://www.vpsmate.org/api/latest')
# data = f.read()
# f.close()
# downloadurl = re.search('"download":"([^"]+)"', data).group(1).replace('\/', '/')
# self._run('wget -nv -c "%s" -O vpsmate.tar.gz' % downloadurl)
#
# # uncompress and install it
# self._run('mkdir vpsmate')
# self._run('tar zxmf vpsmate.tar.gz -C vpsmate')
# if not localpkg_found: os.remove('vpsmate.tar.gz')
# stop service
print
if os.path.exists('/etc/init.d/vpsmate'):
self._run('/etc/init.d/vpsmate stop')
# backup data and remove old code
if os.path.exists('%s/data/' % self.installpath):
self._run('mkdir /tmp/vpsmate_data', True)
self._run('cp -r %s/data/* /tmp/vpsmate_data/' % self.installpath, True)
self._run('rm -rf %s' % self.installpath)
self._run('git clone https://github.com/fanshengshuai/VPSMate.git %s' % self.installpath)
# install new code
# self._run('mv vpsmate %s' % self.installpath)
self._run('chmod +x %s/config.py %s/server.py' % (self.installpath, self.installpath))
# install service
initscript = '%s/tools/init.d/%s/vpsmate' % (self.installpath, self.distname)
self._run('cp %s /etc/init.d/vpsmate' % initscript)
self._run('chmod +x /etc/init.d/vpsmate')
# start service
if self.distname in ('centos', 'redhat'):
self._run('chkconfig vpsmate on')
self._run('service vpsmate start')
elif self.distname == 'ubuntu':
pass
elif self.distname == 'debian':
pass
def config_firewall(self):
self._run('iptables -A INPUT -p tcp --dport 8888 -j ACCEPT')
self._run('iptables -A OUTPUT -p tcp --sport 8888 -j ACCEPT')
def config(self, username, password):
self._run('%s/config.py username "%s"' % (self.installpath, username))
self._run('%s/config.py password "%s"' % (self.installpath, password))
def detect_ip(self):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('www.baidu.com', 80))
ip = s.getsockname()[0]
s.close()
return ip
def install(self):
# check platform environment
print '* Checking platform...',
supported = self.check_platform()
if not supported:
print 'FAILED'
print 'Unsupport platform %s %s %s' % self.dist
sys.exit()
else:
print 'OK'
# check python version
print '* Current python version [%s.%s] ...' % (sys.version_info[:2][0], sys.version_info[:2][1]),
if (sys.version_info[:2] == (2, 6) or sys.version_info[:2] == (2, 7)):
print 'OK'
else:
print 'FAILED'
# install the right version
print '* Installing python 2.6 ...'
self.install_python()
# check GIT version
print '* Checking GIT ...',
if self.check_git():
print 'OK'
else:
print 'FAILED'
print '* Install depend software ...'
self._run('yum install -y wget net-tools vim psmisc rsync libxslt-devel GeoIP GeoIP-devel gd gd-devel')
# if sys.version_info[:2] == (2, 6):
# print 'OK'
# else:
# print 'FAILED'
#
# # install the right version
# print '* Installing python 2.6 ...'
# self.install_python()
# stop firewall
if os.path.exists('/etc/init.d/iptables'):
self._run('/etc/init.d/iptables stop')
# get the latest vpsmate version
print '* Installing latest VPSMate'
self.install_vpsmate()
# set username and password
print
print '============================'
print '* INSTALL COMPLETED! *'
print '============================'
print
username = raw_input('Admin username [default: admin]: ').strip()
password = raw_input('Admin password [default: admin]: ').strip()
if len(username) == 0:
username = 'admin'
if len(password) == 0:
password = 'admin'
self.config(username, password)
print
print '* Username and password set successfully!'
print
print '* Config iptables'
self.config_firewall()
print '* The URL of your VPSMate is:',
print 'http://%s:8888/' % self.detect_ip()
print
pass
def main():
install = Install()
install.install()
if __name__ == "__main__":
main()