diff --git a/.gitignore b/.gitignore index 59ea39c..3f849c2 100644 --- a/.gitignore +++ b/.gitignore @@ -74,3 +74,5 @@ __pycache__ /common/libex/test /client/tp_rdp /server/share/etc/maintenance-mode +/dist/client/windows/assist/apps +/dist/client/windows/assist/cfg diff --git a/build/builder/build-assist.py b/build/builder/build-assist.py index 04610a3..a068001 100644 --- a/build/builder/build-assist.py +++ b/build/builder/build-assist.py @@ -111,7 +111,7 @@ class BuilderWin(BuilderBase): @staticmethod def _build_installer(): - tmp_path = os.path.join(env.root_path, 'dist', 'windows', 'client', 'assist') + tmp_path = os.path.join(env.root_path, 'dist', 'client', 'windows', 'assist') tmp_app_path = os.path.join(tmp_path, 'apps') tmp_cfg_path = os.path.join(tmp_path, 'cfg') @@ -137,7 +137,7 @@ class BuilderWin(BuilderBase): utils.copy_file(os.path.join(env.root_path, 'client', 'tools', 'winscp'), os.path.join(tmp_app_path, 'tools', 'winscp'), 'license.txt') utils.copy_file(os.path.join(env.root_path, 'client', 'tools'), os.path.join(tmp_app_path, 'tools'), 'securecrt-telnet.vbs') - utils.nsis_build(os.path.join(env.root_path, 'dist', 'windows', 'client', 'assist', 'installer.nsi')) + utils.nsis_build(os.path.join(env.root_path, 'dist', 'client', 'windows', 'assist', 'installer.nsi')) class BuilderLinux(BuilderBase): diff --git a/build/builder/build-installer.py b/build/builder/build-installer.py index e850cc1..8899f95 100644 --- a/build/builder/build-installer.py +++ b/build/builder/build-installer.py @@ -50,7 +50,7 @@ class BuilderWin(BuilderBase): self.name = 'teleport-server-windows-{}-{}'.format(ctx.bits_path, VER_TELEPORT_SERVER) self._final_file = os.path.join(env.root_path, 'out', 'installer', '{}.zip'.format(self.name)) - self.dist_path = os.path.join(env.root_path, 'dist', ctx.dist, 'server') + self.dist_path = os.path.join(env.root_path, 'dist', 'server') self.base_path = os.path.join(env.root_path, 'out', 'installer') self.base_tmp = os.path.join(self.base_path, '_tmp_') @@ -77,8 +77,8 @@ class BuilderWin(BuilderBase): utils.copy_ex(os.path.join(env.root_path, 'out', 'pysrt'), bin_path, (ctx.dist_path, 'pysrt')) # 复制安装所需的脚本 - utils.copy_ex(os.path.join(self.dist_path, 'script'), self.path_tmp, 'setup.bat') - utils.copy_ex(os.path.join(self.dist_path, 'script'), self.path_tmp, 'script') + utils.copy_ex(os.path.join(self.dist_path), self.path_tmp, 'setup.bat') + utils.copy_ex(os.path.join(self.dist_path), self.path_tmp, 'script') if os.path.exists(self._final_file): utils.remove(self._final_file) @@ -94,7 +94,7 @@ class BuilderLinux(BuilderBase): self.name = 'teleport-server-linux-{}-{}'.format(ctx.bits_path, VER_TELEPORT_SERVER) self._final_file = os.path.join(env.root_path, 'out', 'installer', '{}.tar.gz'.format(self.name)) - self.dist_path = os.path.join(env.root_path, 'dist', ctx.dist, 'server') + self.dist_path = os.path.join(env.root_path, 'dist', 'server') self.base_path = os.path.join(env.root_path, 'out', 'installer') self.base_tmp = os.path.join(self.base_path, '_tmp_') @@ -160,9 +160,9 @@ class BuilderLinux(BuilderBase): # 复制安装所需的脚本 # utils.copy_ex(os.path.join(self.dist_path, 'script'), self.path_tmp, 'install.sh') # utils.copy_ex(os.path.join(self.dist_path, 'script'), self.path_tmp, 'uninst.sh') - utils.copy_ex(os.path.join(self.dist_path, 'script'), self.path_tmp, 'setup.sh') - utils.copy_ex(os.path.join(self.dist_path, 'script'), self.path_tmp, 'script') - utils.copy_ex(os.path.join(self.dist_path, 'script'), self.path_tmp, 'daemon') + utils.copy_ex(os.path.join(self.dist_path), self.path_tmp, 'setup.sh') + utils.copy_ex(os.path.join(self.dist_path), self.path_tmp, 'script') + utils.copy_ex(os.path.join(self.dist_path), self.path_tmp, 'daemon') if os.path.exists(self._final_file): utils.remove(self._final_file) diff --git a/dist/windows/client/assist/LangSimpChinese.nsh b/dist/client/windows/assist/LangSimpChinese.nsh similarity index 100% rename from dist/windows/client/assist/LangSimpChinese.nsh rename to dist/client/windows/assist/LangSimpChinese.nsh diff --git a/dist/windows/client/assist/installer.nsi b/dist/client/windows/assist/installer.nsi similarity index 100% rename from dist/windows/client/assist/installer.nsi rename to dist/client/windows/assist/installer.nsi diff --git a/dist/windows/client/assist/main.nsh b/dist/client/windows/assist/main.nsh similarity index 100% rename from dist/windows/client/assist/main.nsh rename to dist/client/windows/assist/main.nsh diff --git a/dist/windows/client/assist/rc/header_inst.bmp b/dist/client/windows/assist/rc/header_inst.bmp similarity index 100% rename from dist/windows/client/assist/rc/header_inst.bmp rename to dist/client/windows/assist/rc/header_inst.bmp diff --git a/dist/windows/client/assist/rc/header_uninst.bmp b/dist/client/windows/assist/rc/header_uninst.bmp similarity index 100% rename from dist/windows/client/assist/rc/header_uninst.bmp rename to dist/client/windows/assist/rc/header_uninst.bmp diff --git a/dist/windows/client/assist/rc/install.ico b/dist/client/windows/assist/rc/install.ico similarity index 100% rename from dist/windows/client/assist/rc/install.ico rename to dist/client/windows/assist/rc/install.ico diff --git a/dist/windows/client/assist/rc/setup.ico b/dist/client/windows/assist/rc/setup.ico similarity index 100% rename from dist/windows/client/assist/rc/setup.ico rename to dist/client/windows/assist/rc/setup.ico diff --git a/dist/windows/client/assist/rc/uninstall.ico b/dist/client/windows/assist/rc/uninstall.ico similarity index 100% rename from dist/windows/client/assist/rc/uninstall.ico rename to dist/client/windows/assist/rc/uninstall.ico diff --git a/dist/windows/client/assist/rc/win_inst.bmp b/dist/client/windows/assist/rc/win_inst.bmp similarity index 100% rename from dist/windows/client/assist/rc/win_inst.bmp rename to dist/client/windows/assist/rc/win_inst.bmp diff --git a/dist/windows/client/assist/rc/win_uninst.bmp b/dist/client/windows/assist/rc/win_uninst.bmp similarity index 100% rename from dist/windows/client/assist/rc/win_uninst.bmp rename to dist/client/windows/assist/rc/win_uninst.bmp diff --git a/dist/windows/client/assist/setup.nsh b/dist/client/windows/assist/setup.nsh similarity index 100% rename from dist/windows/client/assist/setup.nsh rename to dist/client/windows/assist/setup.nsh diff --git a/dist/linux/server/script/install.sh b/dist/linux/server/script/install.sh deleted file mode 100755 index b514937..0000000 --- a/dist/linux/server/script/install.sh +++ /dev/null @@ -1,101 +0,0 @@ -#!/bin/bash - -#################################################################### -# EOM Teleport Server Install Script -#################################################################### - -if [ `id -u` -ne 0 ];then - echo "" - echo -e "\e[31mPlease run the installer with ROOT.\033[0m" - echo "" - exit 1 -fi - - -PATH_ROOT=$(cd "$(dirname "$0")"; pwd) -PATH_TARGET=/usr/local/eom - -if [ ! -d "${PATH_TARGET}" ]; then - mkdir -p "${PATH_TARGET}" -fi - -if [ -f /etc/init.d/eom_ts ]; then - service eom_ts stop - rm -rf /etc/init.d/eom_ts -fi -rm -rf /etc/rc2.d/S50eom_ts -rm -rf /etc/rc3.d/S50eom_ts -rm -rf /etc/rc4.d/S50eom_ts -rm -rf /etc/rc5.d/S50eom_ts - - -if [ -f /etc/init.d/teleport ]; then - service teleport stop - rm -rf /etc/init.d/teleport -fi -rm -rf /etc/rc2.d/S50teleport -rm -rf /etc/rc3.d/S50teleport -rm -rf /etc/rc4.d/S50teleport -rm -rf /etc/rc5.d/S50teleport - -sleep 1 -echo "" -echo "Installing EOM Teleport Server..." - -cd "${PATH_TARGET}" -tar -zxvf "${PATH_ROOT}/data/teleport.tar.gz" >/dev/null -cd "${PATH_ROOT}" - -if [ ! -d "${PATH_TARGET}/teleport/etc" ]; then - cp -r "${PATH_TARGET}/teleport/tmp/etc" "${PATH_TARGET}/teleport/etc" -else - if [ ! -f "${PATH_TARGET}/teleport/etc/web.ini" ]; then - cp "${PATH_TARGET}/teleport/tmp/etc/web.ini" "${PATH_TARGET}/teleport/etc/web.ini" - fi - if [ ! -f "${PATH_TARGET}/teleport/etc/core.ini" ]; then - cp "${PATH_TARGET}/teleport/tmp/etc/core.ini" "${PATH_TARGET}/teleport/etc/core.ini" - fi -fi - -if [ ! -d "${PATH_TARGET}/teleport/data" ]; then - cp -r "${PATH_TARGET}/teleport/tmp/data" "${PATH_TARGET}/teleport/data" -fi - -chmod +x "${PATH_TARGET}/teleport/bin/tp_core" -chmod +x "${PATH_TARGET}/teleport/bin/tp_web" - -echo "Generate daemon startup script..." - -cp "${PATH_ROOT}/data/start.sh" "${PATH_TARGET}/teleport/." -chmod +x "${PATH_TARGET}/teleport/start.sh" -cp "${PATH_ROOT}/data/stop.sh" "${PATH_TARGET}/teleport/." -chmod +x "${PATH_TARGET}/teleport/stop.sh" -cp "${PATH_ROOT}/data/status.sh" "${PATH_TARGET}/teleport/." -chmod +x "${PATH_TARGET}/teleport/status.sh" - -cp "${PATH_ROOT}/data/daemon" /etc/init.d/teleport -chmod +x /etc/init.d/teleport - -ln -s /etc/init.d/teleport /etc/rc2.d/S50teleport -ln -s /etc/init.d/teleport /etc/rc3.d/S50teleport -ln -s /etc/init.d/teleport /etc/rc4.d/S50teleport -ln -s /etc/init.d/teleport /etc/rc5.d/S50teleport - -# Upgrade database... -"${PATH_TARGET}/teleport/bin/tp_web" --py "${PATH_TARGET}/teleport/www/teleport/app/eom_upgrade.py" - -echo "" -echo "Start teleport server..." -echo "" -service teleport start -echo "" -sleep 1 -echo "Check teleport server status..." -echo "" -service teleport status -echo "" - - -echo "" -echo -e "\e[32mInstallation done.\033[0m" -echo "" diff --git a/dist/linux/server/script/uninst.sh b/dist/linux/server/script/uninst.sh deleted file mode 100755 index 61e9276..0000000 --- a/dist/linux/server/script/uninst.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash - -#################################################################### -# EOM Teleport Server Uninstall Script -#################################################################### - -if [ `id -u` -ne 0 ];then - echo "" - echo -e "\e[31mPlease run the uninstaller with ROOT.\033[0m" - echo "" - exit 1 -fi - - -echo "" -echo "Uninstalling EOM Teleport Server..." - -if [ -f /etc/init.d/eom_ts ]; then - service eom_ts stop - rm -rf /etc/init.d/eom_ts -fi -rm -rf /etc/rc2.d/S50eom_ts -rm -rf /etc/rc3.d/S50eom_ts -rm -rf /etc/rc4.d/S50eom_ts -rm -rf /etc/rc5.d/S50eom_ts - - -if [ -f /etc/init.d/teleport ]; then - service teleport stop - rm -rf /etc/init.d/teleport -fi -rm -rf /etc/rc2.d/S50teleport -rm -rf /etc/rc3.d/S50teleport -rm -rf /etc/rc4.d/S50teleport -rm -rf /etc/rc5.d/S50teleport - - -if [ -d /usr/local/eom/teleport ]; then - rm -rf /usr/local/eom/teleport -fi - -echo "" -echo -e "\e[32mUninstallation done.\033[0m" -echo "" diff --git a/dist/linux/server/script/daemon/daemon.in b/dist/server/daemon/daemon.in similarity index 100% rename from dist/linux/server/script/daemon/daemon.in rename to dist/server/daemon/daemon.in diff --git a/dist/linux/server/script/daemon/start.sh.in b/dist/server/daemon/start.sh.in old mode 100755 new mode 100644 similarity index 100% rename from dist/linux/server/script/daemon/start.sh.in rename to dist/server/daemon/start.sh.in diff --git a/dist/linux/server/script/daemon/status.sh.in b/dist/server/daemon/status.sh.in old mode 100755 new mode 100644 similarity index 100% rename from dist/linux/server/script/daemon/status.sh.in rename to dist/server/daemon/status.sh.in diff --git a/dist/linux/server/script/daemon/stop.sh.in b/dist/server/daemon/stop.sh.in old mode 100755 new mode 100644 similarity index 100% rename from dist/linux/server/script/daemon/stop.sh.in rename to dist/server/daemon/stop.sh.in diff --git a/dist/linux/server/script/script/core/__init__.py b/dist/server/script/core/__init__.py similarity index 100% rename from dist/linux/server/script/script/core/__init__.py rename to dist/server/script/core/__init__.py diff --git a/dist/linux/server/script/script/core/colorconsole.py b/dist/server/script/core/colorconsole.py similarity index 100% rename from dist/linux/server/script/script/core/colorconsole.py rename to dist/server/script/core/colorconsole.py diff --git a/dist/linux/server/script/script/core/env.py b/dist/server/script/core/env.py similarity index 100% rename from dist/linux/server/script/script/core/env.py rename to dist/server/script/core/env.py diff --git a/dist/linux/server/script/script/core/utils.py b/dist/server/script/core/utils.py similarity index 100% rename from dist/linux/server/script/script/core/utils.py rename to dist/server/script/core/utils.py diff --git a/dist/linux/server/script/script/main.py b/dist/server/script/main.py similarity index 98% rename from dist/linux/server/script/script/main.py rename to dist/server/script/main.py index 39fe51a..441c6dd 100644 --- a/dist/linux/server/script/script/main.py +++ b/dist/server/script/main.py @@ -532,13 +532,26 @@ class InstallerLinux(InstallerBase): self._def_install_path = '/usr/local/teleport' def _check_installation(self): + cc.o(' - check local installation ... ', end='') + # old version, the daemon named `eom_ts`. # from 2.0.0.1, the daemon rename to `teleport`. # we must check both. - if os.path.exists('/etc/init.d/eom_ts') or os.path.exists('/etc/init.d/teleport'): + if os.path.exists('/etc/init.d/eom_ts'): + self._is_installed = True + self._install_path = '/usr/local/eom/teleport' + # self._fix_path() + elif os.path.exists('/etc/init.d/teleport'): self._is_installed = True self._install_path = '/usr/local/teleport' + # self._fix_path() + + if self._is_installed: + cc.i('[exists]') self._fix_path() + else: + cc.i('[not exists]') + return def _fix_path(self): self._config_path = '/etc/teleport' diff --git a/dist/windows/server/script/setup.bat b/dist/server/setup.bat similarity index 100% rename from dist/windows/server/script/setup.bat rename to dist/server/setup.bat diff --git a/dist/linux/server/script/setup.sh b/dist/server/setup.sh old mode 100755 new mode 100644 similarity index 100% rename from dist/linux/server/script/setup.sh rename to dist/server/setup.sh diff --git a/dist/windows/server/script/script/core/__init__.py b/dist/windows/server/script/script/core/__init__.py deleted file mode 100644 index 633f866..0000000 --- a/dist/windows/server/script/script/core/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -# -*- coding: utf-8 -*- - diff --git a/dist/windows/server/script/script/core/colorconsole.py b/dist/windows/server/script/script/core/colorconsole.py deleted file mode 100644 index 30bdb87..0000000 --- a/dist/windows/server/script/script/core/colorconsole.py +++ /dev/null @@ -1,476 +0,0 @@ -# -*- coding: utf-8 -*- - -import os -import sys -import platform -import traceback - -__all__ = ['o', 'v', 'i', 'w', 'e', 'f'] - -# ====================================== -# 颜色 -# ====================================== -CR_RESTORE = 0 # 恢复正常 - 浅灰色 -# BOLD = "[1m" # 高亮显示 -# UNDERSCORE = "[4m" # 下划线 -# REVERSE = "[7m" # 反白显示 -CR_BLACK = 1 # 黑色 -CR_LIGHT_GRAY = 2 # 浅灰色 - 普通文字 -CR_GRAY = 3 # 深灰色 - 捕获别的命令的输出 -CR_WHITE = 4 # 白色 -CR_RED = 5 # 红色 -CR_GREEN = 6 # 绿色 -CR_YELLOW = 7 # 黄色 - Windows平台称之为棕色(Brown) -CR_BLUE = 8 # 蓝色 -CR_MAGENTA = 9 # 紫红 -CR_CYAN = 10 # 青色 -CR_LIGHT_RED = 11 # 亮红色 - 失败 -CR_LIGHT_GREEN = 12 # 亮绿色 - 成功 -CR_LIGHT_YELLOW = 13 # 亮黄色 - 重要 -CR_LIGHT_BLUE = 14 # 亮蓝色 - 其实在黑色背景上还是比较深 -CR_LIGHT_MAGENTA = 15 # 亮紫色 - 警告 -CR_LIGHT_CYAN = 16 # 亮青色 - -CR_VERBOSE = CR_LIGHT_GRAY -CR_NORMAL = CR_WHITE -CR_INFO = CR_GREEN -CR_WARN = CR_LIGHT_YELLOW -CR_ERROR = CR_LIGHT_RED - -COLORS = { - # 常量定义 Linux色彩 WinConsole色彩 - CR_RESTORE: ('[0m', 7), # 7 = 浅灰色 - 普通文字 - CR_BLACK: ('[0;30m', 0), # 0 = 黑色 - CR_RED: ("[0;31m", 4), # 红色 - CR_GREEN: ("[0;32m", 2), # 绿色 - CR_YELLOW: ("[0;33m", 6), # 黄色 - Windows平台称之为棕色(Brown) - CR_BLUE: ("[0;34m", 1), # 蓝色 - CR_MAGENTA: ("[0;35m", 5), # 紫红 - CR_CYAN: ("[0;36m", 3), # 青色 - CR_LIGHT_GRAY: ('[0;37m', 7), # 浅灰色 - 普通文字 - CR_GRAY: ("[1;30m", 8), # 深灰色 - 捕获别的命令的输出 - CR_LIGHT_RED: ("[1;31m", 12), # 亮红色 - 失败 - CR_LIGHT_GREEN: ("[1;32m", 10), # 亮绿色 - 成功 - CR_LIGHT_YELLOW: ("[1;33m", 14), # 亮黄色 - 重要 - CR_LIGHT_BLUE: ("[1;34m", 9), # 亮蓝色 - 其实在黑色背景上还是比较深 - CR_LIGHT_MAGENTA: ("[1;35m", 13), # 亮紫色 - 警告 - CR_LIGHT_CYAN: ("[1;36m", 11), # 亮青色 - CR_WHITE: ("[1;37m", 15) # 白色 -} - - -class ColorConsole: - """ - :type _win_color : Win32ColorConsole - """ - - def __init__(self): - - # self._log_console = self._console_default # 输出到控制台的方式,为None时表示不输出到控制台 - # self._console_set_color = self._console_set_color_default - - self._sep = ' ' - self._end = '\n' - - self._win_color = None - - self.o = self._func_output - self.v = self._func_verbose - self.n = self._func_normal - self.i = self._func_info - self.w = self._func_warn - self.e = self._func_error - self.f = self._func_fail - - if sys.stdout is None: - self.o = self._func_pass - self.v = self._func_pass - self.n = self._func_pass - self.i = self._func_pass - self.w = self._func_pass - self.e = self._func_pass - self.f = self._func_pass - # self._log_console = self._func_pass - # self._console_set_color = self._console_set_color_default - - else: - # python2.7 on Ubuntu, sys.platform is 'linux2', so we use platform.system() instead. - - _platform = platform.system().lower() - - if _platform == 'linux' or _platform == 'darwin': - self._console_set_color = self._console_set_color_linux - self._console_restore_color = self._console_restore_color_linux - elif _platform == 'windows': - if 'TERM' in os.environ and os.environ['TERM'] in ['xterm']: - self._console_set_color = self._console_set_color_linux - self._console_restore_color = self._console_restore_color_linux - - else: - self._win_color = Win32ColorConsole() - if self._win_color.available(): - self._console_set_color = self._console_set_color_win - self._console_restore_color = self._console_restore_color_win - - else: - self._console_set_color = self._func_pass - self._console_restore_color = self._func_pass - - def set_default(self, *args, **kwargs): - if 'sep' in kwargs: - self._sep = kwargs['sep'] - if 'end' in kwargs: - self._end = kwargs['end'] - - def _func_pass(self, *args, **kwargs): - # do nothing. - pass - - def _func_output(self, *args, **kwargs): - sep = kwargs['sep'] if 'sep' in kwargs else self._sep - end = kwargs['end'] if 'end' in kwargs else self._end - - first = True - for x in args: - if not first: - sys.stdout.writelines(sep) - - if isinstance(x, tuple): - cl = x[0] - z = x[1:] - self._console_set_color(cl) - self._console_output(*z, sep='', end='') - sys.stdout.flush() - - elif isinstance(x, str): - self._console_output(x, sep='', end='') - sys.stdout.flush() - - else: - raise RuntimeError('Invalid param.') - - sys.stdout.writelines(end) - self._console_restore_color() - sys.stdout.flush() - - def _func_verbose(self, *args, **kwargs): - self._console_set_color(CR_VERBOSE) - self._console_output(*args, **kwargs) - self._console_restore_color() - sys.stdout.flush() - - # 普通的日志数据 - def _func_normal(self, *args, **kwargs): - self._console_set_color(CR_NORMAL) - self._console_output(*args, **kwargs) - self._console_restore_color() - sys.stdout.flush() - - # 重要信息 - def _func_info(self, *args, **kwargs): - self._console_set_color(CR_INFO) - self._console_output(*args, **kwargs) - self._console_restore_color() - sys.stdout.flush() - - # 警告 - def _func_warn(self, *args, **kwargs): - self._console_set_color(CR_WARN) - self._console_output(*args, **kwargs) - self._console_restore_color() - sys.stdout.flush() - - def _func_error(self, *args, **kwargs): - self._console_set_color(CR_ERROR) - self._console_output(*args, **kwargs) - self._console_restore_color() - sys.stdout.flush() - - def _func_fail(self, *args, **kwargs): - self._console_set_color(CR_ERROR) - self._console_output('[FAIL] ', end='') - self._console_output(*args, **kwargs) - - _type, _value, _tb = sys.exc_info() - if _type is not None: - x = traceback.format_exception_only(_type, _value) - self._console_output('[EXCEPTION] ', end='') - self._console_output(x[0], end='') - - x = traceback.extract_tb(_tb) - c = len(x) - self._console_set_color(CR_RED) - for i in range(0, c): - self._console_output(os.path.abspath(x[i][0]), '(', x[i][1], '): ', x[i][3], sep='') - else: - s = traceback.extract_stack() - c = len(s) - self._console_set_color(CR_RED) - for i in range(2, c): - self._console_output(' ', os.path.abspath(s[c - i - 1][0]), '(', s[c - i - 1][1], '): ', s[c - i - 1][3], sep='') - - self._console_restore_color() - sys.stdout.flush() - - # def _do_log(self, msg, color=None): - # self._log_console(msg, color) - # - # def _console_default(self, msg, color=None): - # if msg is None: - # return - # - # sys.stdout.writelines(msg) - # sys.stdout.flush() - # - # def _console_win(self, msg, color=None): - # if msg is None: - # return - # - # # 这里的问题很复杂,日常使用没有问题,但是当在工作机上使用时,部分内容是捕获另一个脚本执行的结果再输出 - # # 如果结果中有中文,这里就会显示乱码。如果尝试编码转换,会抛出异常。目前暂时采用显示乱码的方式了。 - # - # # if CONSOLE_WIN_CMD == self.console_type: - # # try: - # # _msg = unicode(msg, 'utf-8') - # # except: - # # _msg = msg - # # else: - # # _msg = msg - # # _msg = None - # # if isinstance(msg, unicode): - # # _msg = msg - # # else: - # # # _msg = unicode(msg, 'utf-8') - # # try: - # # _msg = unicode(msg, 'utf-8') - # # except: - # # _msg = unicode(msg, 'gb2312') - # # # _msg = msg - # # - # # # if CONSOLE_WIN_CMD == self.console_type: - # # # sys.stdout.writelines(msg.encode('gb2312')) - # # # else: - # # # sys.stdout.writelines(msg.encode('utf-8')) - # # - # # - # # # try: - # # # _msg = unicode(msg, 'utf-8') - # # # except: - # # _msg = msg - # - # if color is None: - # sys.stdout.writelines(msg) - # else: - # self._win_color.set_color(COLORS[color][1]) - # sys.stdout.writelines(msg) - # sys.stdout.flush() - # self._win_color.set_color(COLORS[CR_RESTORE][1]) - # - # sys.stdout.flush() - # - # def _console_linux(self, msg, cr=None): - # if msg is None: - # return - # - # if cr is None: - # sys.stdout.writelines(msg) - # else: - # sys.stdout.writelines('\x1B') - # sys.stdout.writelines(COLORS[cr][0]) - # sys.stdout.writelines(msg) - # sys.stdout.writelines('\x1B[0m') - # - # sys.stdout.flush() - - # def _console_set_color_default(self, cr=None): - # pass - - def _console_set_color_win(self, cr=None): - if cr is None: - return - self._win_color.set_color(COLORS[cr][1]) - sys.stdout.flush() - - def _console_set_color_linux(self, cr=None): - if cr is None: - return - sys.stdout.writelines('\x1B') - sys.stdout.writelines(COLORS[cr][0]) - sys.stdout.flush() - - def _console_restore_color_win(self): - self._win_color.set_color(COLORS[CR_RESTORE][1]) - sys.stdout.flush() - - def _console_restore_color_linux(self): - sys.stdout.writelines('\x1B[0m') - sys.stdout.flush() - - def _console_output(self, *args, **kwargs): - sep = kwargs['sep'] if 'sep' in kwargs else self._sep - end = kwargs['end'] if 'end' in kwargs else self._end - first = True - for x in args: - if not first: - sys.stdout.writelines(sep) - - first = False - if isinstance(x, str): - sys.stdout.writelines(x) - continue - - else: - sys.stdout.writelines(x.__str__()) - - sys.stdout.writelines(end) - sys.stdout.flush() - - # def _test(self): - # self.log('###################', CR_RESTORE) - # self.log(' CR_RESTORE\n') - # self.log('###################', CR_BLACK) - # self.log(' CR_BLACK\n') - # self.log('###################', CR_LIGHT_GRAY) - # self.log(' CR_LIGHT_GRAY\n') - # self.log('###################', CR_GRAY) - # self.log(' CR_GRAY\n') - # self.log('###################', CR_WHITE) - # self.log(' CR_WHITE\n') - # self.log('###################', CR_RED) - # self.log(' CR_RED\n') - # self.log('###################', CR_GREEN) - # self.log(' CR_GREEN\n') - # self.log('###################', CR_YELLOW) - # self.log(' CR_YELLOW\n') - # self.log('###################', CR_BLUE) - # self.log(' CR_BLUE\n') - # self.log('###################', CR_MAGENTA) - # self.log(' CR_MAGENTA\n') - # self.log('###################', CR_CYAN) - # self.log(' CR_CYAN\n') - # self.log('###################', CR_LIGHT_RED) - # self.log(' CR_LIGHT_RED\n') - # self.log('###################', CR_LIGHT_GREEN) - # self.log(' CR_LIGHT_GREEN\n') - # self.log('###################', CR_LIGHT_YELLOW) - # self.log(' CR_LIGHT_YELLOW\n') - # self.log('###################', CR_LIGHT_BLUE) - # self.log(' CR_LIGHT_BLUE\n') - # self.log('###################', CR_LIGHT_MAGENTA) - # self.log(' CR_LIGHT_MAGENTA\n') - # self.log('###################', CR_LIGHT_CYAN) - # self.log(' CR_LIGHT_CYAN\n') - # # data = b'This is a test string and you can see binary format data here.' - # # self.bin('Binary Data:\n', data) - # # data = b'' - # # self.bin('Empty binary\n', data) - # # self.bin('This is string\n\n', 'data') - # - # # self.d('This is DEBUG message.\n') - # self.v('This is VERBOSE message.\n') - # self.i('This is INFORMATION message.\n') - # self.w('This is WARNING message.\n') - # self.e('This is ERROR message.\n') - # self.f('This is FAIL message.\n') - # - # self.v('test auto\nsplit lines.\nYou should see\nmulti-lines.\n') - - -class Win32ColorConsole: - def __init__(self): - from ctypes import WINFUNCTYPE, windll - from ctypes.wintypes import BOOL, HANDLE, DWORD, WORD - - self.__original_stderr = sys.stderr - self.__stdout = None - self.__SetConsoleTextAttribute = None - - # Work around . - # codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None) - - # Make Unicode console output work independently of the current code page. - # This also fixes . - # Credit to Michael Kaplan - # and TZOmegaTZIOY - # . - try: - # - # HANDLE WINAPI GetStdHandle(DWORD nStdHandle); - # returns INVALID_HANDLE_VALUE, NULL, or a valid handle - # - # - # DWORD WINAPI GetFileType(DWORD hFile); - # - # - # BOOL WINAPI GetConsoleMode(HANDLE hConsole, LPDWORD lpMode); - - STD_OUTPUT_HANDLE = DWORD(-11) - INVALID_HANDLE_VALUE = DWORD(-1).value - - GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(("GetStdHandle", windll.kernel32)) - - self.__SetConsoleTextAttribute = WINFUNCTYPE(BOOL, HANDLE, WORD)(("SetConsoleTextAttribute", windll.kernel32)) - - self.__stdout = GetStdHandle(STD_OUTPUT_HANDLE) - if self.__stdout == INVALID_HANDLE_VALUE: - self.__stdout = None - - except Exception as e: - self.__stdout = None - self._complain("exception %r while fixing up sys.stdout and sys.stderr\n" % (str(e),)) - - # If any exception occurs in this code, we'll probably try to print it on stderr, - # which makes for frustrating debugging if stderr is directed to our wrapper. - # So be paranoid about catching errors and reporting them to original_stderr, - # so that we can at least see them. - @staticmethod - def _complain(message): - # print >> self.__original_stderr, message if isinstance(message, str) else repr(message) - sys.stderr.writelines(message) - - def available(self): - if self.__stdout is None or self.__SetConsoleTextAttribute is None: - return False - else: - return True - - def set_color(self, color): - # if not self.available(): - # return - self.__SetConsoleTextAttribute(self.__stdout, color) - - -_cc = ColorConsole() -del ColorConsole - - -def set_default(*args, **kwargs): - _cc.set_default(*args, **kwargs) - - -def o(*args, **kwargs): - _cc.o(*args, **kwargs) - - -def v(*args, **kwargs): - _cc.v(*args, **kwargs) - - -def n(*args, **kwargs): - _cc.n(*args, **kwargs) - - -def i(*args, **kwargs): - _cc.i(*args, **kwargs) - - -def w(*args, **kwargs): - _cc.w(*args, **kwargs) - - -def e(*args, **kwargs): - _cc.e(*args, **kwargs) - - -def f(*args, **kwargs): - _cc.f(*args, **kwargs) diff --git a/dist/windows/server/script/script/core/env.py b/dist/windows/server/script/script/core/env.py deleted file mode 100644 index 387f55e..0000000 --- a/dist/windows/server/script/script/core/env.py +++ /dev/null @@ -1,214 +0,0 @@ -# -*- coding: utf8 -*- - -import os -import platform -import sys -import configparser - -from . import colorconsole as cc - -if platform.system().lower() == 'windows': - try: - import winreg - except ImportError: - cc.e('Can not load module `winreg`, so I can not locate toolchain for you.') - - -class Env(object): - BITS_32 = 32 - BITS_64 = 64 - - def __init__(self): - _this_path = os.path.abspath(os.path.dirname(__file__)) - self.root_path = os.path.abspath(os.path.join(_this_path, '..', '..')) - self.src_path = os.path.join(self.root_path, 'data') - - self.bits = self.BITS_32 - self.bits_str = 'x86' - - _bits = platform.architecture()[0] - if _bits == '64bit': - self.bits = self.BITS_64 - self.bits_str = 'x64' - - self.is_win = False - self.is_win_x64 = False - self.is_linux = False - self.is_macos = False - - _os = platform.system().lower() - self.plat = '' - if _os == 'windows': - self.is_win = True - self.plat = 'windows' - self.is_win_x64 = 'PROGRAMFILES(X86)' in os.environ - elif _os == 'linux': - self.is_linux = True - self.plat = 'linux' - elif _os == 'darwin': - self.is_macos = True - self.plat = 'macos' - - def init(self, warn_miss_tool=False): - # if not self._load_config(warn_miss_tool): - # return False - - return True - - def _load_config(self, warn_miss_tool): - _cfg_file = os.path.join(self.root_path, 'config.ini') - if not os.path.exists(_cfg_file): - cc.e('can not load configuration.\n\nplease copy `config.ini.in` into `config.ini` and modify it to fit your condition and try again.') - return False - - _cfg = configparser.ConfigParser() - _cfg.read(_cfg_file) - if 'external_ver' not in _cfg.sections() or 'toolchain' not in _cfg.sections(): - cc.e('invalid configuration file: need `external_ver` and `toolchain` section.') - return False - - _tmp = _cfg['external_ver'] - try: - _v_openssl = _tmp['openssl'].split(',') - self.ver_openssl = _v_openssl[0].strip() - self.ver_openssl_number = _v_openssl[1].strip() - - self.ver_libuv = _tmp['libuv'] - self.ver_mbedtls = _tmp['mbedtls'] - self.ver_sqlite = _tmp['sqlite'] - self.ver_libssh = _tmp['libssh'] - self.ver_jsoncpp = _tmp['jsoncpp'] - self.ver_mongoose = _tmp['mongoose'] - except KeyError: - cc.e('invalid configuration file: not all necessary external version are set.') - return False - - _tmp = _cfg['toolchain'] - if self.is_win: - if 'wget' in _tmp: - self.wget = _tmp['wget'] - else: - self.wget = None - - if self.wget is None or not os.path.exists(self.wget): - if warn_miss_tool: - cc.w(' - can not find `wget.exe`, you can get it at https://eternallybored.org/misc/wget/') - - if '7z' in _tmp: - self.zip7 = _tmp['7z'] - else: - self.zip7 = None - if self.zip7 is None or not os.path.exists(self.zip7): - if warn_miss_tool: - cc.w(' - can not find `7z.exe`, you can get it at http://www.7-zip.org') - - if 'nasm' in _tmp: - self.nasm = _tmp['nasm'] - else: - self.nasm = self._get_nasm() - - if self.nasm is None or not os.path.exists(self.nasm): - if warn_miss_tool: - cc.w(' - can not locate `nasm`, so I can build openssl.') - - if 'perl' in _tmp: - self.perl = _tmp['perl'] - else: - self.perl = self._get_perl() - - if self.perl is None or not os.path.exists(self.perl): - if warn_miss_tool: - cc.w(' - can not locate `perl`, so I can build openssl.') - - self.visual_studio_path = self._get_visual_studio_path() - if self.visual_studio_path is None or not os.path.exists(self.visual_studio_path): - if warn_miss_tool: - cc.w(' - can not locate Visual Studio installation, so I can build openssl.') - - if 'msbuild' in _tmp: - self.msbuild = _tmp['msbuild'] - else: - self.msbuild = self._get_msbuild() - - if self.msbuild is None or not os.path.exists(self.msbuild): - if warn_miss_tool: - cc.w(' - can not locate `MSBuild`, so I can build nothing.') - - if 'nsis' in _tmp: - self.nsis = _tmp['nsis'] - else: - self.nsis = self._get_nsis() - - if self.nsis is None or not os.path.exists(self.nsis): - if warn_miss_tool: - cc.w(' - can not locate `nsis`, so I can not make installer.') - - elif self.is_linux: - if 'cmake' in _tmp: - self.cmake = _tmp['cmake'] - else: - self.cmake = '/usr/bin/cmake' - - if not os.path.exists(self.cmake): - if warn_miss_tool: - cc.e(' - can not locate `cmake`, so I can not build binary from source.') - - return True - - def _get_msbuild(self): - # 14.0 = VS2015 - # 12.0 = VS2012 - # 4.0 = VS2008 - chk = ['14.0', '12.0', '4.0'] - - p = None - for c in chk: - p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\MSBuild\ToolsVersions\{}'.format(c), r'MSBuildToolsPath') - if p is not None: - break - - return os.path.join(p[0], 'MSBuild.exe') if p is not None else None - - def _get_visual_studio_path(self): - chk = ['14.0', '12.0', '4.0'] - p = None - for c in chk: - p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\VisualStudio\{}'.format(c), r'ShellFolder') - if p is not None: - break - - return p[0] if p is not None else None - - def _get_perl(self): - p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\perl', 'BinDir') - return p[0] if p is not None else None - - def _get_nasm(self): - p = self._winreg_read(winreg.HKEY_CURRENT_USER, r'SOFTWARE\nasm', '') - return os.path.join(p[0], 'nasm.exe') if p is not None else None - - def _get_nsis(self): - p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\NSIS\Unicode', '') - if p is None: - p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\NSIS', '') - return os.path.join(p[0], 'makensis.exe') if p is not None else None - - def _winreg_read(self, base, path, key): - try: - if self.is_win_x64: - hkey = winreg.CreateKeyEx(base, path, 0, winreg.KEY_READ | winreg.KEY_WOW64_32KEY) - else: - hkey = winreg.CreateKeyEx(base, path, 0, winreg.KEY_READ) - - value = winreg.QueryValueEx(hkey, key) - return value - - except OSError: - return None - - -env = Env() -del Env - -if __name__ == '__main__': - pass diff --git a/dist/windows/server/script/script/core/utils.py b/dist/windows/server/script/script/core/utils.py deleted file mode 100644 index 6f0e552..0000000 --- a/dist/windows/server/script/script/core/utils.py +++ /dev/null @@ -1,144 +0,0 @@ -# -*- coding: utf8 -*- - -import os -import platform -import shutil -import subprocess -import sys -import time - -from . import colorconsole as cc -from .env import env - - -def remove(*args): - path = os.path.join(*args) - - cc.v(' - remove [%s] ... ' % path, end='') - if not os.path.exists(path): - cc.v('not exists, skip.') - return - - for i in range(5): - cc.v('.', end='') - try: - if os.path.isdir(path): - shutil.rmtree(path, ignore_errors=True) - time.sleep(0.5) - else: - os.unlink(path) - except: - pass - - if os.path.exists(path): - time.sleep(1) - else: - break - - if os.path.exists(path): - cc.e('[failed]') - raise RuntimeError('') - else: - cc.i('[done]') - - -def make_dirs(path, exist_ok=True): - if os.path.exists(path): - if not exist_ok: - raise RuntimeError('already exists [{}].'.format(path)) - else: - return True - - for i in range(5): - try: - os.makedirs(path) - except: - time.sleep(0.5) - - if not os.path.exists(path): - time.sleep(0.5) - else: - break - - if os.path.exists(path): - return True - else: - raise RuntimeError('can not create path [{}].'.format(path)) - - -def copy_ex(s_path, t_path, item_name=None, force=True): - if item_name is None: - s = s_path - t = t_path - else: - if isinstance(item_name, str): - f_from = item_name - f_to = item_name - elif isinstance(item_name, tuple): - f_from = item_name[0] - f_to = item_name[1] - else: - raise RuntimeError('utils.copy_ex() got invalid param.') - - s = os.path.join(s_path, f_from) - t = os.path.join(t_path, f_to) - - if os.path.exists(t): - if force: - remove(t) - else: - cc.w(t, 'already exists, skip copy.') - return - - if os.path.isdir(s): - cc.v(' - copy [%s]\n -> [%s]' % (s, t)) - shutil.copytree(s, t) - else: - if not os.path.exists(t_path): - os.makedirs(t_path) - cc.v(' - copy [%s]\n -> [%s]' % (s, t)) - shutil.copy(s, t) - - -def ensure_file_exists(filename): - if not os.path.exists(filename): - raise RuntimeError('file not exists: {}'.format(filename)) - if not os.path.isfile(filename): - raise RuntimeError('path exists but not a file: {}'.format(filename)) - - -def sys_exec(cmd, direct_output=False, output_codec=None): - if output_codec is None: - if env.is_win: - output_codec = 'gb2312' - else: - output_codec = 'utf8' - - p = None - if env.is_win: - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=True) - - else: - p = subprocess.Popen(cmd, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=True) - - output = list() - f = p.stdout - while True: - line = f.readline() - if 0 == len(line): - break - - line = line.rstrip('\r\n') - - if direct_output: - cc.o((cc.CR_GRAY, line), end='\n') - - output.append(line) - - ret = p.wait() - - return ret, output - - -if __name__ == '__main__': - pass diff --git a/dist/windows/server/script/script/main.py b/dist/windows/server/script/script/main.py deleted file mode 100644 index d1a9258..0000000 --- a/dist/windows/server/script/script/main.py +++ /dev/null @@ -1,719 +0,0 @@ -# -*- coding: utf-8 -*- - -import os -import sys -import time -from core.env import env -import core.colorconsole as cc -import core.utils as utils - -WIN_CORE_SERVICE_NAME = 'EOM Teleport Core Service' -WIN_WEB_SERVICE_NAME = 'EOM Teleport Web Service' - - -class InstallerBase: - def __init__(self): - self._all_ok = True - self._err_msg = list() - - self._is_installed = False - self._install_path = '' - self._config_path = '' - self._data_path = '' - self._log_path = '' - - self._installed_ver_str = 'UNKNOWN' - self._current_ver = 'UNKNOWN' - - self._def_install_path = '' - - ver_file = os.path.join(env.root_path, 'data', 'www', 'teleport', 'app', 'eom_ver.py') - try: - with open(ver_file, 'r') as f: - x = f.readlines() - for i in x: - s = i.split('=', 1) - if 'TS_VER' == s[0].strip(): - self._current_ver = s[1].strip()[1:-1] - break - except FileNotFoundError: - raise RuntimeError('Cannot detect installer version.') - - def init(self): - _width = 79 - cc.v('') - cc.v('[]{}[]'.format('=' * (_width - 4))) - _str = 'Teleport Server Installation' - cc.o((cc.CR_VERBOSE, ' | '), (cc.CR_INFO, _str), (cc.CR_VERBOSE, '{}|'.format(' ' * (_width - 5 - len(_str))))) - cc.v(' |{}|'.format('=' * (_width - 4))) - cc.o((cc.CR_VERBOSE, ' | ver: '), (cc.CR_NORMAL, self._current_ver), (cc.CR_VERBOSE, '{}|'.format(' ' * (_width - 13 - len(self._current_ver))))) - _str = 'author: apexliu@eomsoft.net' - cc.v(' | {}{}|'.format(_str, ' ' * (_width - 5 - len(_str)))) - cc.v('[]{}[]'.format('=' * (_width - 4))) - cc.v('') - cc.v('Welcome to install Teleport Server!') - cc.v('') - # cc.v(' NOTICE: if you want to use the default settings, just press `Enter`...') - cc.o((cc.CR_VERBOSE, 'NOTICE: There are a few steps need you enter information or make choice,\n if you want to use the '), (cc.CR_WARN, 'default settings'), (cc.CR_VERBOSE, ', just press `Enter` key.')) - cc.o((cc.CR_VERBOSE, ' Otherwise you need enter the '), (cc.CR_NORMAL, 'highlight character'), (cc.CR_VERBOSE, ' to make choice.')) - cc.v('') - cc.v('') - - cc.v('Prepare installation...') - self._check_installation() - self._check_installation_ver() - cc.v('') - - def run(self): - if not self._is_installed: - self.install() - else: - cc.v('') - cc.v('Found teleport server have installed at `{}` already.'.format(self._install_path)) - while True: - x = self._prompt_choice('What are you wanna to do?', [('upgrade', 2, True), ('uninstall', 0, False), ('quit', 0, False)]) - x = x.lower() - if 'q' == x: - return - elif 'u' == x: - self.uninstall() - return - elif 'g' == x: - self._upgrade() - return - - def install(self): - while True: - cc.v('') - self._install_path = self._prompt_input('Set installation path', self._def_install_path) - _use_always = False - if os.path.exists(self._install_path): - while True: - cc.v('') - x = self._prompt_choice('The target path `{}` has already exists,\ndo you want to use it anyway?'.format(self._install_path), [('Yes', 0, True), ('No', 0, False)]) - x = x.lower() - if 'y' == x: - _use_always = True - break - elif 'n' == x: - break - - if _use_always: - break - else: - break - - self._config_path = os.path.join(self._install_path, 'etc') - self._data_path = os.path.join(self._install_path, 'data') - self._log_path = os.path.join(self._install_path, 'log') - - utils.makedirs(self._install_path) - self._copy_files() - self._install_service() - self._start_service() - time.sleep(2) - self._check_service() - - cc.v('\nall done..') - - def uninstall(self): - if not self._is_installed: - return - - _del_settings = False - while True: - cc.v('') - x = self._prompt_choice('Do you want to keep your database and settings?', [('Yes', 0, True), ('No', 0, False)]) - x = x.lower() - if 'y' == x: - break - elif 'n' == x: - _del_settings = True - break - - if _del_settings: - while True: - cc.v('') - x = self._prompt_choice('Seriously!! Are you sure to remove all data and settings?', [('Yes', 0, False), ('No', 0, True)]) - x = x.lower() - if 'y' == x: - break - elif 'n' == x: - _del_settings = False - break - - self._stop_service() - time.sleep(2) - self._uninstall_service() - self._delete_files(_del_settings) - - cc.v('\nall done..') - pass - - def _upgrade(self): - x = self._ver_compare(self._current_ver, self._installed_ver_str) - if x == 0: - while True: - cc.v('') - x = self._prompt_choice('The same version `{}` installed, are you sure to overwrite?'.format(self._current_ver), [('Yes', 0, False), ('No', 0, True)]) - x = x.lower() - if 'y' == x: - break - elif 'n' == x: - return - elif x < 0: - while True: - cc.v('') - x = self._prompt_choice('A new version `{}` installed, rollback to old version `{}` may cause Teleport Server not functionally.\nAre you sure to rollback to old version?'.format(self._installed_ver_str, self._current_ver), [('Yes', 0, False), ('No', 0, True)]) - x = x.lower() - if 'y' == x: - break - elif 'n' == x: - return - else: - while True: - cc.v('') - x = self._prompt_choice('Now upgrade from version `{}` to `{}`, \nAre you sure to upgrade to new version?'.format(self._installed_ver_str, self._current_ver), [('Yes', 0, False), ('No', 0, True)]) - x = x.lower() - if 'y' == x: - break - elif 'n' == x: - return - - while True: - cc.v('') - x = self._prompt_choice('Make sure you have backup your database and settings.\nAre you sure to continue?', [('Yes', 0, False), ('No', 0, True)]) - x = x.lower() - if 'y' == x: - break - elif 'n' == x: - return - - self._stop_service() - time.sleep(2) - self._uninstall_service() - self._delete_files(False) - time.sleep(1) - self._copy_files() - self._install_service() - self._start_service() - time.sleep(2) - self._check_service() - - cc.v('All done.') - - @staticmethod - def _prompt_choice(message, choices): - cc.v('{} ['.format(message), end='') - - def_choice = '' - - for i in range(len(choices)): - if i > 0: - cc.v('/', end='') - msg = choices[i][0] - idx = choices[i][1] - if choices[i][2]: - msg = msg.upper() - def_choice = msg[idx].lower() - cc.w(msg[:idx], end='') - cc.n(msg[idx], end='') - cc.w(msg[idx + 1:], end='') - else: - msg = msg.lower() - cc.v(msg[:idx], end='') - cc.n(msg[idx], end='') - cc.v(msg[idx + 1:], end='') - - cc.v(']: ', end='') - try: - x = input().strip() - if len(x) == 0: - x = def_choice - except EOFError: - x = def_choice - - return x - - @staticmethod - def _prompt_input(message, def_value): - cc.v('{} ['.format(message), end='') - - cc.w(def_value, end='') - cc.v(']: ', end='') - try: - x = input().strip() - if len(x) == 0: - x = def_value - except EOFError: - x = def_value - - return x - - @staticmethod - def _ver_compare(left, right): - l = left.split('.') - r = right.split('.') - - len_l = len(l) - len_r = len(r) - - if len_l < len_r: - for i in range(len_r - len_l): - l.append('0') - elif len_l > len_r: - for i in range(len_l - len_r): - r.append('0') - - cnt = len(l) - for i in range(cnt): - if int(l[i] < r[i]): - return -1 - elif int(l[i] > r[i]): - return 1 - - return 0 - - def _check_installation(self): - pass - - def _check_installation_ver(self): - if not self._is_installed: - return - - # try to get the installed version from www/teleport/app/eom_ver.py - cc.v(' - check installed version ... ', end='') - ver_file = os.path.join(self._install_path, 'www', 'teleport', 'app', 'eom_ver.py') - try: - with open(ver_file) as f: - x = f.readlines() - for i in x: - s = i.split('=', 1) - if 'TS_VER' == s[0].strip(): - self._installed_ver_str = s[1].strip()[1:-1] - cc.i('[{}]'.format(self._installed_ver_str)) - # self._installed_ver = self._ver_str_to_ver(self._installed_ver_str) - break - except FileNotFoundError: - cc.e('[failed]') - cc.e(' the installation maybe broken') - - def _copy_files(self): - pass - - def _delete_files(self, del_settings): - pass - - def _install_service(self): - pass - - def _start_service(self): - pass - - def _stop_service(self): - pass - - def _uninstall_service(self): - pass - - def _check_service(self): - pass - - -class InstallerWin(InstallerBase): - def __init__(self): - super().__init__() - self._def_install_path = r'{}\teleport-server'.format(os.environ['SystemDrive']) - - def _check_installation(self): - cc.o(' - check local installation ... ', end='') - _err, _ = utils.sys_exec(r'sc query "{}"'.format(WIN_CORE_SERVICE_NAME)) - if 1060 == _err: - # core service not install - pass - else: - self._is_installed = True - _err, _o = utils.sys_exec(r'sc qc "{}"'.format(WIN_CORE_SERVICE_NAME)) - if _err != 0: - raise RuntimeError('Can not get core service installation information.') - for i in _o: - _x = i.split(':', 1) - if 'BINARY_PATH_NAME' == _x[0].strip(): - _path = _x[1].strip() - self._install_path = os.path.abspath(os.path.join(os.path.dirname(_path), '..')) - break - - _err, _ = utils.sys_exec(r'sc query "{}"'.format(WIN_WEB_SERVICE_NAME)) - if 1060 == _err: - # web service not install. - pass - else: - self._is_installed = True - _err, _o = utils.sys_exec(r'sc qc "{}"'.format(WIN_WEB_SERVICE_NAME)) - if _err != 0: - raise RuntimeError('Can not get web service installation information.') - for i in _o: - _x = i.split(':', 1) - if 'BINARY_PATH_NAME' == _x[0].strip(): - _path = _x[1].strip() - self._install_path = os.path.abspath(os.path.join(os.path.dirname(_path), '..')) - break - - if self._is_installed: - cc.i('[exists]') - self._config_path = os.path.join(self._install_path, 'etc') - self._data_path = os.path.join(self._install_path, 'data') - self._log_path = os.path.join(self._install_path, 'log') - else: - cc.i('[not exists]') - return - - def _copy_files(self): - utils.copy_ex(os.path.join(env.src_path, 'bin'), os.path.join(self._install_path, 'bin')) - utils.copy_ex(os.path.join(env.src_path, 'www'), os.path.join(self._install_path, 'www')) - - if not os.path.exists(self._config_path): - utils.copy_ex(os.path.join(env.src_path, 'tmp', 'etc'), self._config_path) - - def _delete_files(self, del_settings): - utils.remove(os.path.join(self._install_path, 'bin')) - utils.remove(os.path.join(self._install_path, 'www')) - if del_settings: - utils.remove(self._data_path) - utils.remove(self._config_path) - utils.remove(self._log_path) - - # only remove the installation path when it empty. - os.rmdir(self._install_path) - - def _install_service(self): - cc.o(' - install teleport core service ... ', end='') - _core = os.path.join(self._install_path, 'bin', 'tp_core.exe') - _err, _ = utils.sys_exec(r'"{}" -i'.format(_core)) - if _err == 0 or _err == 1: - cc.i('[done]') - else: - cc.e('[failed]') - raise RuntimeError('Install core service failed. error code: {}'.format(_err)) - - cc.o(' - install teleport web service ... ', end='') - _core = os.path.join(self._install_path, 'bin', 'tp_web.exe') - _err, _ = utils.sys_exec(r'"{}" -i'.format(_core)) - if _err == 0 or _err == 1: - cc.i('[done]') - else: - cc.e('[failed]') - raise RuntimeError('Install web service failed. error code: {}'.format(_err)) - - return True - - def _start_service(self): - cc.o(' - start teleport core service ... ', end='') - _err, _o = utils.sys_exec(r'sc start "{}"'.format(WIN_CORE_SERVICE_NAME)) - # print('start core', _err, _o) - if _err == 0: - cc.i('[done]') - else: - cc.e('[failed]') - raise RuntimeError('Can not start core service.') - - cc.o(' - start teleport web service ...', end='') - _err, _o = utils.sys_exec(r'sc start "{}"'.format(WIN_WEB_SERVICE_NAME)) - # print('start web', _err, _o) - if _err == 0: - cc.i('[done]') - else: - cc.e('[failed]') - raise RuntimeError('Can not start web service.') - - def _stop_service(self): - cc.o(' - stop teleport core service ... ', end='') - _err, _o = utils.sys_exec(r'sc stop "{}"'.format(WIN_CORE_SERVICE_NAME)) - # print('stop core', _err, _o) - if _err == 1060 or _err == 1062 or _err == 0: - cc.i('[done]') - else: - cc.e('[failed]') - raise RuntimeError('Can not stop core service.') - - cc.o(' - stop teleport web service ... ', end='') - _err, _o = utils.sys_exec(r'sc stop "{}"'.format(WIN_WEB_SERVICE_NAME)) - # print('stop web', _err, _o) - if _err == 1060 or _err == 1062 or _err == 0: - cc.i('[done]') - else: - cc.e('[failed]') - raise RuntimeError('Can not stop web service.') - - def _uninstall_service(self): - cc.o(' - delete teleport core service ... ', end='') - _err, _o = utils.sys_exec(r'sc delete "{}"'.format(WIN_CORE_SERVICE_NAME)) - # print('del core', _err, _o) - if _err == 1060 or _err == 0: - cc.i('[done]') - else: - cc.e('[failed]') - raise RuntimeError('Can not uninstall core service.') - - cc.o(' - delete teleport web service ... ', end='') - _err, _o = utils.sys_exec(r'sc delete "{}"'.format(WIN_WEB_SERVICE_NAME)) - # print('del web', _err, _o) - if _err == 1060 or _err == 0: - cc.i('[done]') - else: - cc.e('[failed]') - raise RuntimeError('Can not uninstall web service.') - - def _check_service(self): - cc.o(' - check teleport core service status ... ', end='') - _err, _o = utils.sys_exec(r'sc query "{}"'.format(WIN_CORE_SERVICE_NAME)) - # print('chk core', _err, _o) - if _err == 1060 or _err == 0: - cc.i('[running]') - else: - cc.e('[not running]') - - cc.o(' - check teleport web service status ... ', end='') - _err, _o = utils.sys_exec(r'sc delete "{}"'.format(WIN_WEB_SERVICE_NAME)) - # print('chk web', _err, _o) - if _err == 1060 or _err == 0: - cc.i('[running]') - else: - cc.e('[not running]') - - -class InstallerLinux(InstallerBase): - def __init__(self): - super().__init__() - self._is_installed = False - self._install_path = '' - - if env.is_win: - self._def_install_path = r'{}\teleport-server'.format(os.environ['SystemDrive']) - self._check_installation = self._check_installation_win - self._copy_files = self._copy_files_win - self._install_service = self._install_service_win - self._stop_service = self._stop_service_win - self._delete_service = self._delete_service_win - else: - self._def_install_path = r'/usr/local/eom/teleport' - self._check_installation = self._check_installation_linux - self._copy_files = self._copy_files_linux - self._install_service = self._install_service_linux - self._stop_service = self._stop_service_linux - self._delete_service = self._delete_service_linux - - self._check_installation() - - def init(self): - pass - # check if teleport server installed or not. - - def run(self): - cc.v('') - cc.v('Welcome to install Teleport Server for Windows!') - if not self._is_installed: - self.install() - else: - cc.v('') - cc.v('Found teleport server already installed, now what are you want to do?') - while True: - x = self._prompt_choice('Please choice', [('Upgrade', 2, True), ('Uninstall', 0, False), ('Quit', 0, False)]) - x = x.lower() - if 'q' == x: - return - elif 'u' == x: - self.uninstall() - return - elif 'g' == x: - self._upgrade() - return - - def install(self): - cc.n('Notice: if you want to use the default settings, just press `Enter`...') - cc.v('') - self._install_path = self._prompt_input('Set installation path', self._def_install_path) - - utils.makedirs(self._install_path) - # self._copy_files() - self._install_service() - - pass - # if self._check_installation(): - # self._install() - - def uninstall(self): - if not self._is_installed: - return - - self._stop_service() - self._delete_service() - - cc.e('uninstall not implemented.') - pass - - def _upgrade(self): - cc.e('upgrade not implemented.') - pass - - @staticmethod - def _prompt_choice(message, choices): - cc.v('{} ['.format(message), end='') - - def_choice = '' - - # cc.v('(', end='') - for i in range(len(choices)): - if i > 0: - cc.v('/', end='') - msg = choices[i][0] - idx = choices[i][1] - if choices[i][2]: - def_choice = msg[idx] - cc.w(msg[:idx], end='') - cc.n(msg[idx], end='') - cc.w(msg[idx + 1:], end='') - else: - cc.v(msg[:idx], end='') - cc.n(msg[idx], end='') - cc.v(msg[idx + 1:], end='') - - # cc.v(') ', end='') - - # cc.v('[', end='') - - # cc.w(def_choice, end='') - cc.v(']: ', end='') - try: - x = input().strip() - if len(x) == 0: - x = def_choice - except EOFError: - x = def_choice - - return x - - @staticmethod - def _prompt_input(message, def_value): - cc.v('{} ['.format(message), end='') - - cc.w(def_value, end='') - cc.v(']: ', end='') - try: - x = input().strip() - if len(x) == 0: - x = def_value - except EOFError: - x = def_value - - return x - - def _check_installation_win(self): - _err, _ = utils.sys_exec(r'sc query "{}"'.format(WIN_CORE_SERVICE_NAME)) - if 1060 == _err: - # core service not install - pass - else: - self._is_installed = True - _err, _o = utils.sys_exec(r'sc qc "{}"'.format(WIN_CORE_SERVICE_NAME)) - if _err != 0: - raise RuntimeError('Can not get core service installation information.') - for i in _o: - _x = i.split(':', 1) - if 'BINARY_PATH_NAME' == _x[0].strip(): - _path = _x[1].strip() - self._install_path = os.path.abspath(os.path.join(os.path.dirname(_path), '..')) - return - - _err, _ = utils.sys_exec(r'sc query "{}"'.format(WIN_WEB_SERVICE_NAME)) - if 1060 == _err: - # web service not install. - pass - else: - self._is_installed = True - _err, _o = utils.sys_exec(r'sc qc "{}"'.format(WIN_WEB_SERVICE_NAME)) - if _err != 0: - raise RuntimeError('Can not get web service installation information.') - for i in _o: - _x = i.split(':', 1) - if 'BINARY_PATH_NAME' == _x[0].strip(): - _path = _x[1].strip() - self._install_path = os.path.abspath(os.path.join(os.path.dirname(_path), '..')) - return - - def _copy_files_win(self): - utils.copy_ex(os.path.join(env.src_path, 'bin'), os.path.join(self._install_path, 'bin')) - utils.copy_ex(os.path.join(env.src_path, 'www'), os.path.join(self._install_path, 'www')) - - _tmp = os.path.join(self._install_path, 'etc') - if not os.path.exists(_tmp): - utils.copy_ex(os.path.join(env.src_path, 'etc'), _tmp) - - def _install_service_win(self): - _core = os.path.join(self._install_path, 'bin', 'tp_core.exe') - _err, _o = utils.sys_exec(r'"{}" -i'.format(_core)) - if _err == 0: - return True - elif _err == 1: - cc.v('Core service has been installed already.') - else: - cc.e('Install core service failed. error code: ', _err) - return False - - def _stop_service_win(self): - _err, _o = utils.sys_exec(r'sc stop "{}"'.format(WIN_CORE_SERVICE_NAME)) - if _err == 1060 or _err == 1062 or _err == 0: - # 1060 = 服务尚未安装 - # 1062 = 服务尚未启动 - # 0 = 操作成功 - pass - else: - raise RuntimeError('Can not stop core service.') - - _err, _o = utils.sys_exec(r'sc stop "{}"'.format(WIN_WEB_SERVICE_NAME)) - if _err == 1060 or _err == 1062 or _err == 0: - pass - else: - raise RuntimeError('Can not stop web service.') - - -def _main(): - cc.set_default(sep='', end='\n') - env.init() - - if env.is_win: - _installer = InstallerWin() - elif env.is_linux: - _installer = InstallerLinux() - else: - raise RuntimeError('Sorry, teleport server does not support this platform yet.') - - _installer.init() - - if len(sys.argv) > 0: - _cmd = sys.argv[0].lower() - if 'uninstall' == _cmd: - _installer.uninstall() - return - - _installer.run() - - -def main(): - try: - _main() - except KeyboardInterrupt: - pass - except RuntimeError as err: - cc.v('') - cc.v('') - cc.e(err.__str__()) - cc.v('') - except: - cc.f('got exception.') - - return 0 - - -if __name__ == '__main__': - main()