2015-12-19 09:27:12 +00:00
|
|
|
|
#!/usr/bin/python
|
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
|
|
|
|
import django
|
|
|
|
|
from django.core.management import execute_from_command_line
|
2016-02-27 04:01:44 +00:00
|
|
|
|
import shlex
|
2015-12-20 11:09:18 +00:00
|
|
|
|
import urllib
|
2015-12-21 04:18:36 +00:00
|
|
|
|
import socket
|
2016-02-26 06:13:21 +00:00
|
|
|
|
import subprocess
|
|
|
|
|
|
2015-12-19 09:27:12 +00:00
|
|
|
|
|
|
|
|
|
jms_dir = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
|
|
|
|
|
sys.path.append(jms_dir)
|
|
|
|
|
|
|
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'jumpserver.settings'
|
|
|
|
|
if django.get_version() != '1.6':
|
|
|
|
|
setup = django.setup()
|
|
|
|
|
|
|
|
|
|
from juser.user_api import db_add_user, get_object, User
|
|
|
|
|
from install import color_print
|
2016-01-25 10:21:07 +00:00
|
|
|
|
from jumpserver.api import get_mac_address, bash
|
2015-12-19 09:27:12 +00:00
|
|
|
|
|
2015-12-21 04:18:36 +00:00
|
|
|
|
socket.setdefaulttimeout(2)
|
|
|
|
|
|
2015-12-19 09:27:12 +00:00
|
|
|
|
|
|
|
|
|
class Setup(object):
|
|
|
|
|
"""
|
|
|
|
|
安装jumpserver向导
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.admin_user = 'admin'
|
|
|
|
|
self.admin_pass = '5Lov@wife'
|
|
|
|
|
|
2015-12-20 11:09:18 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def _pull():
|
|
|
|
|
color_print('开始更新jumpserver', 'green')
|
|
|
|
|
# bash('git pull')
|
2015-12-21 04:18:36 +00:00
|
|
|
|
try:
|
|
|
|
|
mac = get_mac_address()
|
|
|
|
|
version = urllib.urlopen('http://jumpserver.org/version/?id=%s' % mac)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2015-12-20 11:09:18 +00:00
|
|
|
|
|
2015-12-19 09:27:12 +00:00
|
|
|
|
def _input_admin(self):
|
|
|
|
|
while True:
|
2015-12-20 09:45:57 +00:00
|
|
|
|
print
|
2015-12-19 09:27:12 +00:00
|
|
|
|
admin_user = raw_input('请输入管理员用户名 [%s]: ' % self.admin_user).strip()
|
|
|
|
|
admin_pass = raw_input('请输入管理员密码: [%s]: ' % self.admin_pass).strip()
|
|
|
|
|
admin_pass_again = raw_input('请再次输入管理员密码: [%s]: ' % self.admin_pass).strip()
|
|
|
|
|
|
|
|
|
|
if admin_user:
|
|
|
|
|
self.admin_user = admin_user
|
|
|
|
|
|
|
|
|
|
if not admin_pass_again:
|
|
|
|
|
admin_pass_again = self.admin_pass
|
|
|
|
|
|
|
|
|
|
if admin_pass:
|
|
|
|
|
self.admin_pass = admin_pass
|
|
|
|
|
|
|
|
|
|
if self.admin_pass != admin_pass_again:
|
|
|
|
|
color_print('两次密码不相同请重新输入')
|
|
|
|
|
else:
|
|
|
|
|
break
|
|
|
|
|
print
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _sync_db():
|
|
|
|
|
os.chdir(jms_dir)
|
|
|
|
|
execute_from_command_line(['manage.py', 'syncdb', '--noinput'])
|
|
|
|
|
|
|
|
|
|
def _create_admin(self):
|
|
|
|
|
user = get_object(User, username=self.admin_user)
|
|
|
|
|
if user:
|
|
|
|
|
user.delete()
|
|
|
|
|
db_add_user(username=self.admin_user, password=self.admin_pass, role='SU', name='admin', groups='',
|
|
|
|
|
admin_groups='', email='admin@jumpserver.org', uuid='MayBeYouAreTheFirstUser', is_active=True)
|
2016-02-27 04:01:44 +00:00
|
|
|
|
cmd = 'id %s 2> /dev/null 1> /dev/null || useradd %s' % (self.admin_user, self.admin_user)
|
|
|
|
|
shlex.os.system(cmd)
|
2015-12-19 09:27:12 +00:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2016-02-23 10:29:23 +00:00
|
|
|
|
def _chmod_file():
|
|
|
|
|
os.chdir(jms_dir)
|
|
|
|
|
os.chmod('init.sh', 0755)
|
|
|
|
|
os.chmod('connect.py', 0755)
|
|
|
|
|
os.chmod('manage.py', 0755)
|
2016-02-28 08:41:44 +00:00
|
|
|
|
os.chmod('run_server.py', 0755)
|
2016-02-23 10:29:23 +00:00
|
|
|
|
os.chmod('service.sh', 0755)
|
|
|
|
|
os.chmod('logs', 0777)
|
|
|
|
|
os.chmod('keys', 0777)
|
2015-12-19 09:27:12 +00:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _run_service():
|
2016-02-27 04:01:44 +00:00
|
|
|
|
cmd = 'bash %s start' % os.path.join(jms_dir, 'service.sh')
|
|
|
|
|
shlex.os.system(cmd)
|
2015-12-19 09:27:12 +00:00
|
|
|
|
print
|
2016-02-28 08:41:44 +00:00
|
|
|
|
color_print('安装成功,请访问web, 祝你使用愉快。\n请访问 https://github.com/jumpserver/jumpserver/wiki 查看文档', 'green')
|
2015-12-19 09:27:12 +00:00
|
|
|
|
|
|
|
|
|
def start(self):
|
2016-02-28 06:40:17 +00:00
|
|
|
|
print "开始安装Jumpserver ..."
|
2015-12-20 11:09:18 +00:00
|
|
|
|
self._pull()
|
2015-12-19 09:27:12 +00:00
|
|
|
|
self._sync_db()
|
|
|
|
|
self._input_admin()
|
|
|
|
|
self._create_admin()
|
2016-02-23 10:29:23 +00:00
|
|
|
|
self._chmod_file()
|
2015-12-19 09:27:12 +00:00
|
|
|
|
self._run_service()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
setup = Setup()
|
|
|
|
|
setup.start()
|