# -*- coding: utf-8 -*- """ Support for Activemq This module uses the manager webapp to manage Activemq. If the manager Activemq is not configured some of the functions won't work. :configuration: - Java bin path should be in default path - If ipv6 is enabled make sure you permit manager access to ipv6 interface "0:0:0:0:0:0:0:1" - If you are using tomcat.tar.gz it has to be installed or symlinked under ``/opt``, preferably using name Activemq - "tomcat.signal start/stop" works but it does not use the startup scripts """ # Import python libs import platform import sys import subprocess import os import re import json #隐藏部分内部调用模块 import threading import time if sys.platform.startswith('win'): # 系统为windows SYSTEM = "windows" MAIN_DIR = 'apache-activemq-5.13.1' JDK_DIR = "jdk1.8.0_91" JDKPKG = 'jdk1.8.0_91.zip' BASE_DIR = 'C:\' PKG_PATH = 'C:\' X86 = platform.architecture()[0] else: # 系统不是windows SYSTEM = "linux" MAIN_DIR = 'apache-activemq-5.13.2' JDK_DIR = "jdk1.8.0_45" JDKPKG = 'jdk-8u45-linux-x64.gz' BASE_DIR = '/usr/local' PKG_PATH = '/usr/local' X86 = platform.architecture()[0] class InstallError(Exception): pass def get_java_success(java_home): out = utils_cmd.get_output(java_home + ' -v') utils.logger.info(java_home + ' -v') result = ''.join(out) utils.logger.info(result) invalid_msg = ['is valid', 'ERROR', 'not compatible'] for i in invalid_msg: if not filter((lambda list_item: i in list_item), out) == []: raise InstallError('install java fail:{}'.format(result)) def _send_message(message, jid=None): __salt__['exec_module.module_send_event'](message, jid=jid) def install(baseDir=None, port=None, package=None, dependPkg=None, jid=None, execAccount=None, outputParam=[], **kwargs): u""" :param activemq:activemq package name :param installDir:activemq install path :param pkg_dir:activemq package path :return:{"success": False, "message": message} """ try: ret, param = utils_errors.check_inputs(locals(), outs=['dependPkg']) if not ret: return {'success': False, 'message': 'input params error,please check input params:{}'.format(param)} runas = execAccount main_path = baseDir.split(os.sep)[-1] # 参数预处理 if SYSTEM == "windows" and baseDir.endswith(":"): baseDir = baseDir + '\' elif SYSTEM == "windows" and baseDir.endswith(":\"): pass else: baseDir = baseDir.rstrip(os.sep) # 使用已编译好的包 _send_message(u'{0}{1}{2}'.format(u"start install in ", SYSTEM, u" system"), jid=jid) # 判断服务是否已存在且正在运行 if SYSTEM == "windows": # 若更新,可能需要更改服务名称 out = __salt__['service.status']("activemq") else: out = __salt__['ps.pgrep']("activemq") if out: return {"success": False, "message": u"activemq has been installed and is running,please stop first"} if not package: return {"success": False, "message": u'activemq install fail,package param is null,please read the log!'} # 解压主包 if SYSTEM == "windows": activemq_home = utils_pack.install_pkgs(package[0], baseDir) else: activemq_home = utils_pack.unpack_tar(package[0], baseDir, path=main_path) if os.path.isdir(activemq_home) and len(os.listdir(activemq_home)) != 0: _send_message(u"activemq unpack success.installation progress:30%.", jid=jid) utils.logger.info('activemq_path:{}'.format(activemq_home)) else: return {"success": False, "message": 'activemq install fail,unpack main package fail'} jdk_dir = JDK_DIR # 解压依赖包 if dependPkg not in ([], None, ''): for dependPkg_item in dependPkg: package_name = dependPkg_item.split(os.sep)[-1] if not dependPkg_item.endswith('.exe'): file_path = utils_pack.unpack_tar(dependPkg_item, baseDir, path=package_name) if 'jdk' in file_path or 'java' in file_path: jdk_dir = file_path if os.path.isdir(file_path) and len(os.listdir(file_path)) != 0: _send_message(u"{0}install success".format(package_name), jid=jid) else: return {"success": False, "message": 'activemq install fail,{} unpack fail'.format(package_name)} elif dependPkg_item.endswith('.exe') and platform.platform() >= 'Windows-7': install_dir = os.path.join(baseDir, package_name) cmd = '{0} /s INSTALLDIR={1} INSTALL_SILENT=Enable'.format(dependPkg_item, install_dir) utils.logger.info('install java:{}'.format(cmd)) if runas: cmd = 'sudo -u {} "{}"'.format(runas, cmd) out = __salt__['cmd.run_stdout'](cmd, cwd=baseDir, python_shell=False).splitlines() while 1: if os.path.exists(install_dir): break else: install_dir = os.path.join(baseDir, package_name) cmd = '{0} /s INSTALLDIR={1} INSTALL_SILENT=Enable /L C:setup.log'.format( dependPkg_item, install_dir) utils.logger.info('install java:{}'.format(cmd)) try: if runas: cmd = 'sudo -u {} "{}"'.format(runas, cmd) p = subprocess.Popen(cmd, cwd=baseDir, shell=False) p.wait() except Exception as e: utils.logger.info('{}'.format(e)) pass start_time = time.time() while 1: if os.path.exists(install_dir): utils.logger.info(u'java is installing...') break if time.time() - start_time > 1000000: raise InstallError(u'java install fail') start_time = time.time() while 1: with open('C:\setup.log') as f: f.seek(0, 1) if '=== Logging stopped:' in f.readline(): utils.logger.info(f.readline()) break if time.time() - start_time > 10000: raise InstallError(u'java install fail') utils.logger.info(u'unpack dependency packages success') _send_message(u"unpack dependency packages success.installation progress:90%", jid=jid) # add Jdk path into Activemq environment variables if SYSTEM == "windows": # add activemq service win_doc = os.path.join(baseDir, MAIN_DIR, "bin\{0}".format("win32" if '32' in X86 else "win64")) java_home = os.path.join(baseDir, JDK_DIR, "bin", "java") get_java_success(java_home) __salt__['file.replace'](win_doc + "\wrapper.conf", '^wrapper.java.command=java', 'wrapper.java.command=' + java_home.replace("\", "/")) cmd = win_doc + "\InstallService.bat" result = utils_cmd.get_output(cmd, runas=runas) else: activemq_bin_home = '{0}/bin/activemq'.format(activemq_home) java_home = 'JAVA_HOME={0} '.format(jdk_dir) get_java_success(java_home) out = __salt__['file.prepend'](activemq_bin_home, java_home) install_path = json.dumps({'install_path': activemq_home}) return {"success": True, "message": u'activemq install success', 'outputParam': install_path} except Exception as e: return {"success": False, "message": "{0}".format(e)} finally: if os.path.exists('C:\setup.log'): os.remove('C:\setup.log') def start(baseDir=BASE_DIR, jid=None, execAccount=None, outputParam=[], **kwargs): try: ret, param = utils_errors.check_inputs(locals()) if not ret: return {'success': False, 'message': 'input params error,please check input params:{}'.format(param)} runas = execAccount baseDir = baseDir.rstrip(os.sep) cwd = os.path.join(baseDir, 'bin') if not os.path.exists(cwd): cwd = os.path.join(baseDir, MAIN_DIR, 'bin') if not os.path.exists(cwd): return {'success': False, "message": 'no activemq execfile in {} directory,please input again'.format(baseDir)} utils.logger.info('activemq directory:{}'.format(cwd)) if SYSTEM == "windows": __salt__['service.start']("ActiveMQ") out = __salt__['service.status']("ActiveMQ") else: result = "" cmd = "sh activemq start" req_cmd = "sh activemq status" # 运行bat文件启动 i = 0 while i <= 1000: i += 1 result = utils_cmd.get_output(cmd, cwd=cwd, wait=True, runas=runas) out = not filter( (lambda list_item: "ActiveMQ is running" in list_item), result) == [] if out: break out = os.path.exists( '{0}/data/activemq.pid'.format(cwd.rstrip('bin'))) if out: break if out: message = utils.make_msg(u"Activemq start success") return {"success": True, "message": message} else: message = utils.make_msg(u"Activemq start fail", other_msg=str(result)) return {"success": False, "message": message} except Exception as e: return {"success": False, "message": 'Activemq start fail:{}'.format(e)} def stop(baseDir=BASE_DIR, jid=None, execAccount=None, outputParam=[], **kwargs): try: ret, param = utils_errors.check_inputs(locals()) if not ret: return {'success': False, 'message': 'input params error,please check input params:{}'.format(param)} runas = execAccount baseDir = baseDir.rstrip(os.sep) path = os.path.join(baseDir, 'bin/activemq') if not os.path.exists(path): path = os.path.join(baseDir, MAIN_DIR, 'bin/activemq') if not os.path.exists(path): return {'success': False, "message": 'no activemq execfile in {} directory,please input again'.format(baseDir)} out, result = __salt__['exec_module.manageService'](SYSTEM, 'activemq', 'service.stop', 'activemq', 'sh {0} stop'.format(path)) if SYSTEM == "windows": __salt__['service.stop']("ActiveMQ") out = not __salt__['service.status']("ActiveMQ") else: result, message = __salt__['exec_module.manageService'](SYSTEM, 'activemq', 'service.stop', 'activemq', 'sh {0} stop'.format(path)) i = 0 cwd = path.rstrip('activemq') while i <= 1000: i += 1 p = subprocess.Popen("sh activemq status", shell=True, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=None) result = p.stdout.readlines() out = not filter( (lambda list_item: "ActiveMQ not running" in list_item), result) == [] if out: break out = not os.path.exists('{0}/data/activemq.pid'.format(cwd)) if out: break if out: message = utils.make_msg(u'Activemq stop success!') return {"success": True, "message": message} else: message = utils.make_msg(u'Activemq stop fail,error message:', other_msg=str(result)) return {"success": False, "message": message} except Exception as e: return {"success": False, "message": 'Activemq stop fail,error message:{}'.format(e)} def restart(baseDir=BASE_DIR, jid=None, outputParam=[], execAccount=None, **kwargs): baseDir = baseDir.rstrip(os.sep) try: ret, param = utils_errors.check_inputs(locals()) if not ret: return {'success': False, 'message': 'input params error,please check input params:{}'.format(param)} runas = execAccount if not os.path.exists(baseDir): return {'success': False, "message": 'no activemq execfile in {} directory,please input again'.format(baseDir)} if SYSTEM == "windows": __salt__['service.restart']("ActiveMQ") out = __salt__['service.status']("ActiveMQ") if out: message = utils.make_msg(u"Activemq restart success") return {"success": True, "message": message} else: message = utils.make_msg(u"Activemq restart fail") return {"success": False, "message": message} else: cwd = '{0}/bin'.format(baseDir) if not os.path.exists(cwd): cwd = '{0}/{1}/bin'.format(baseDir, MAIN_DIR) result = utils_cmd.get_output("sh activemq status", cwd=cwd, wait=True, runas=runas) out = not filter( (lambda list_item: "ActiveMQ is running" in list_item), result) == [] utils.logger.info('restart:{}'.format(out)) if not out: message = utils.make_msg(u"Activemq is not running,try to start") result = start(baseDir=baseDir, jid=jid) message = utils.make_msg(result["message"]) if result["success"]: return {"success": True, "message": u"Activemq restart success"} else: return {"success": False, "message": u"Activemq restart fail, but service has stop"} else: result = stop(baseDir=baseDir, jid=jid) utils.logger.info('stop:{}'.format(result)) if not result["success"]: return {"success": False, "message": u"Activemq restart fail,stop service fail"} out = start(baseDir=baseDir, jid=jid) utils.logger.info('start:{}'.format(out)) if out["success"]: return {"success": True, "message": u"Activemq restart success"} else: return {"success": False, "message": u"Activemq restart fail,start fail"} except Exception as e: return {"success": False, "message": "Activemq restart fail:{}".format(e)}