From 8aec0c1ac72b480d26690a48a065c759a31d0345 Mon Sep 17 00:00:00 2001 From: yumaojun <719118794@qq.com> Date: Fri, 26 Feb 2016 14:13:21 +0800 Subject: [PATCH] fix (install jumpserver): install jumpserver compatible with ubuntu 1. install.py add platform judge, fix get_ip_addr function 2. next.py little adjust 3. service.sh little adjust --- install/install.py | 109 ++++++++++++++++++++++++++++++--------------- install/next.py | 5 ++- service.sh | 93 ++++++++++++++++++++------------------ 3 files changed, 125 insertions(+), 82 deletions(-) diff --git a/install/install.py b/install/install.py index c5765ff12..bb3b2027e 100755 --- a/install/install.py +++ b/install/install.py @@ -5,16 +5,15 @@ import subprocess import time import os import sys -import MySQLdb from smtplib import SMTP, SMTPAuthenticationError, SMTPConnectError, SMTPSenderRefused import ConfigParser import socket -import fcntl -import struct -import readline import random import string +import re +import platform + jms_dir = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) sys.path.append(jms_dir) @@ -27,6 +26,13 @@ def bash(cmd): return subprocess.call(cmd, shell=True) +def valid_ip(ip): + if ('255' in ip) or (ip == "0.0.0.0"): + return False + else: + return True + + def color_print(msg, color='red', exits=False): """ Print colorful string. @@ -46,18 +52,17 @@ def color_print(msg, color='red', exits=False): return msg -def get_ip_addr(ifname='eth0'): +def get_ip_addr(): try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - return socket.inet_ntoa(fcntl.ioctl( - s.fileno(), - 0x8915, - struct.pack('256s', ifname[:15]) - )[20:24]) - except: - ips = os.popen("LANG=C ifconfig | grep \"inet addr\" | grep -v \"127.0.0.1\" | awk -F \":\" '{print $2}' | awk '{print $1}'").readlines() - if len(ips) > 0: - return ips[0] + s.connect(("8.8.8.8", 80)) + return s.getsockname()[0] + except Exception: + if_data = ''.join(os.popen("LANG=C ifconfig").readlines()) + ips = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', if_data, flags=re.MULTILINE) + ip = filter(valid_ip, ips) + if ip: + return ip[0] return '' @@ -75,6 +80,17 @@ class PreSetup(object): self.ip = '' self.key = ''.join(random.choice(string.ascii_lowercase + string.digits) \ for _ in range(16)) + self.dist = platform.dist()[0].lower() + + @property + def _is_redhat(self): + if self.dist == "centos" or self.dist == "redhat": + return True + + @property + def _is_ubuntu(self): + if self.dist == "ubuntu": + return True def write_conf(self, conf_file=os.path.join(jms_dir, 'jumpserver.conf')): color_print('开始写入配置文件', 'green') @@ -99,22 +115,38 @@ class PreSetup(object): def _setup_mysql(self): color_print('开始安装设置mysql (请手动设置mysql安全)', 'green') color_print('默认用户名: %s 默认密码: %s' % (self.db_user, self.db_pass), 'green') - bash('yum -y install mysql-server') - bash('service mysqld start') - bash('chkconfig mysqld on') - bash('mysql -e "create database %s default charset=utf8"' % self.db) - bash('mysql -e "grant all on %s.* to \'%s\'@\'%s\' identified by \'%s\'"' % (self.db, - self.db_user, - self.db_host, - self.db_pass)) + if self._is_redhat: + bash('yum -y install mysql-server') + bash('service mysqld start') + bash('chkconfig mysqld on') + bash('mysql -e "create database %s default charset=utf8"' % self.db) + bash('mysql -e "grant all on %s.* to \'%s\'@\'%s\' identified by \'%s\'"' % (self.db, + self.db_user, + self.db_host, + self.db_pass)) + if self._is_ubuntu: + bash('echo mysql-server mysql-server/root_password select '' | debconf-set-selections') + bash('echo mysql-server mysql-server/root_password_again select '' | debconf-set-selections') + bash('apt-get -y install mysql-server') + bash('mysql -e "create database %s default charset=utf8"' % self.db) + bash('mysql -e "grant all on %s.* to \'%s\'@\'%s\' identified by \'%s\'"' % (self.db, + self.db_user, + self.db_host, + self.db_pass)) - @staticmethod - def _set_env(): + def _set_env(self): color_print('开始关闭防火墙和selinux', 'green') - os.system("export LANG='en_US.UTF-8' && sed -i 's/LANG=.*/LANG=en_US.UTF-8/g' /etc/sysconfig/i18n") - bash('service iptables stop && chkconfig iptables off && setenforce 0') + if self._is_redhat: + os.system("export LANG='en_US.UTF-8' && sed -i 's/LANG=.*/LANG=en_US.UTF-8/g' /etc/sysconfig/i18n") + bash('service iptables stop && chkconfig iptables off && setenforce 0') + if self._is_ubuntu: + os.system("export LANG='en_US.UTF-8'") + bash("iptables -F") + bash('which selinux && setenforce 0') def _test_db_conn(self): + bash("pip install mysql-python") + import MySQLdb try: MySQLdb.connect(host=self.db_host, port=int(self.db_port), user=self.db_user, passwd=self.db_pass, db=self.db) @@ -141,15 +173,18 @@ class PreSetup(object): return True return False - @staticmethod - def _rpm_repo(): - color_print('开始安装epel源', 'green') - bash('yum -y install epel-release') + def _rpm_repo(self): + if self._is_redhat: + color_print('开始安装epel源', 'green') + bash('yum -y install epel-release') + + def _depend_rpm(self): + color_print('开始安装依赖包', 'green') + if self._is_redhat: + bash('yum -y install git python-pip mysql-devel gcc automake autoconf python-devel vim sshpass') + if self._is_ubuntu: + bash("apt-get -y install git python-pip gcc automake autoconf vim sshpass libmysqld-dev python-all-dev") - @staticmethod - def _depend_rpm(): - color_print('开始安装依赖rpm包', 'green') - bash('yum -y install git python-pip mysql-devel gcc automake autoconf python-devel vim sshpass') @staticmethod def _require_pip(): @@ -202,11 +237,11 @@ class PreSetup(object): print def start(self): - # self._rpm_repo() - # self._depend_rpm() - # self._require_pip() color_print('请务必先查看wiki https://github.com/ibuler/jumpserver/wiki/Quickinstall') time.sleep(3) + self._rpm_repo() + self._depend_rpm() + self._require_pip() self._set_env() self._input_ip() self._input_mysql() diff --git a/install/next.py b/install/next.py index 48a518b71..49d62db25 100755 --- a/install/next.py +++ b/install/next.py @@ -8,6 +8,8 @@ from django.core.management import execute_from_command_line import shutil import urllib import socket +import subprocess + jms_dir = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) sys.path.append(jms_dir) @@ -75,7 +77,8 @@ class Setup(object): 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) - os.system('id %s &> /dev/null || useradd %s' % (self.admin_user, self.admin_user)) + cmd = 'useradd %s' % self.admin_user + subprocess.call(cmd, shell=True) @staticmethod def _chmod_file(): diff --git a/service.sh b/service.sh index 9bbbf2f9a..d2a9d06c0 100755 --- a/service.sh +++ b/service.sh @@ -24,54 +24,62 @@ lockfile=/var/lock/subsys/${PROC_NAME} start() { - jump_start=$"Starting ${PROC_NAME} service:" - - if [ -f $lockfile ];then - echo "jumpserver is running..." - success "$jump_start" - else - daemon python $base_dir/manage.py runserver 0.0.0.0:80 &>> /tmp/jumpserver.log 2>&1 & - daemon python $base_dir/manage.py crontab add &>> /tmp/jumpserver.log 2>&1 - daemon python $base_dir/run_websocket.py &> /dev/null 2>&1 & + jump_start=$"Starting ${PROC_NAME} service:" + + if [ -f $lockfile ];then + echo "jumpserver is running..." + success "$jump_start" + else + daemon python $base_dir/manage.py runserver 0.0.0.0:80 &>> /tmp/jumpserver.log 2>&1 & + daemon python $base_dir/manage.py crontab add &>> /tmp/jumpserver.log 2>&1 + daemon python $base_dir/run_websocket.py &> /dev/null 2>&1 & sleep 4 - echo -n "$jump_start" - nums=0 + echo -n "$jump_start" + nums=0 for i in manage.py run_websocket.py;do - ps aux | grep "$i" | grep -v 'grep' &> /dev/null && let nums+=1 || echo "$i not running" + if ps aux | grep "$i" | grep -v 'grep' &> /dev/null; then + nums=$[nums+1] + else + echo "$i not running" + fi done if [ "x$nums" == "x2" ];then success "$jump_start" + if [ ! -e $lockfile ]; then + lockfile_dir=`dirname $lockfile` + mkdir -pv $lockfile_dir + fi touch "$lockfile" echo else failure "$jump_start" echo fi - fi - - + fi + + } stop() { - echo -n $"Stopping ${PROC_NAME} service:" - - daemon python $base_dir/manage.py crontab remove &>> /tmp/jumpserver.log 2>&1 - ps aux | grep -E 'manage.py|run_websocket.py' | grep -v grep | awk '{print $2}' | xargs kill -9 &> /dev/null - ret=$? + echo -n $"Stopping ${PROC_NAME} service:" - if [ $ret -eq 0 ]; then - echo_success - echo + daemon python $base_dir/manage.py crontab remove &>> /tmp/jumpserver.log 2>&1 + ps aux | grep -E 'manage.py|run_websocket.py' | grep -v grep | awk '{print $2}' | xargs kill -9 &> /dev/null + ret=$? + + if [ $ret -eq 0 ]; then + echo_success + echo rm -f "$lockfile" - else - echo_failure - echo + else + echo_failure + echo rm -f "$lockfile" - fi + fi } @@ -83,22 +91,19 @@ restart(){ } # See how we were called. -case "$1" in - start) - start - ;; - stop) - stop - ;; +case "$1" in + start) + start + ;; + stop) + stop + ;; - restart) - restart - ;; - - *) + restart) + restart + ;; + + *) echo $"Usage: $0 {start|stop|restart}" - exit 2 -esac - - - + exit 2 +esac