def start(self, pkg_id, install_path, update_status=True): conf_file = self.getConfigFile(pkg_id, install_path) user, group = self.getUser(conf_file) # 修改文件属主 common.chown_by_name(install_path, user, group) # 添加crontab self.opLog.info("start to add crontab") ret = self.pkgCom.getLock(os.path.dirname(self.CONF_BASE), filename="crontab.lock") if not ret: self.exit_proc(2005, "get lock error, please try again") shell = 'export VISUAL="%s/crontab.py add %s";echo "no"|crontab -e'%(self.curr_folder, install_path) code,msg = common.runShell(shell,user=user) if code != 0: self.exit_proc(2010, "add crontab error,code:%s,msg:%s"%(code,msg)) ret = self.pkgCom.unLock(os.path.dirname(self.CONF_BASE), filename ="crontab.lock") if not ret: self.exit_proc(2009, "unlock error") # 执行启动脚本 self.opLog.info("start to start") code, start_msg = common.runConfig(conf_file, "start_script", install_path) if code != 0: msg = '执行启动脚本失败, code=%s,msg=%s' % (code,start_msg) self.opLog.info(msg) return code, msg msg = start_msg self.opLog.info("start end, start to check process") # 检查启动结果 err_app, ok_app, err_port, ok_port = self.pkgCom.checkStart(conf_file, install_path) # 更新包状态 if update_status: self.opLog.info("check end,update status") status = { 'status': 'started', 'time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), 'successProcess': ok_app, 'errorProcess': err_app, 'successPort': ok_port, 'errorPort': err_port } self.updateStatus(status) err_msg = "" if err_app or err_port: if err_app: err_msg += ",异常进程:" + ";".join(err_app) if err_port: err_msg += ",异常端口:" + ";".join(map(str, err_port)) msg = '启动失败 %s,启动返回信息:%s' % (err_msg,start_msg) self.opLog.info(msg) code = 1009 return code, msg self.opLog.info("start successfully ") return code, msg
def stop(self, pkg_id, install_path, update_status=True): conf_file = self.getConfigFile(pkg_id, install_path) user, group = self.getUser(conf_file) # 修改文件属主 common.chown_by_name(install_path, user, group) #删除crontab self.opLog.info("start to clear crontab") ret = self.pkgCom.getLock(os.path.dirname(self.CONF_BASE), filename="crontab.lock") if not ret: self.exit_proc(2005, "get lock error, please try again") shell = 'export VISUAL="%s/crontab.py del %s";crontab -e'%(self.curr_folder, install_path) code,msg = common.runShell(shell,user=user) if code != 0: self.exit_proc(2010, "del crontab error,code:%s,msg:%s"%(code,msg)) ret = self.pkgCom.unLock(os.path.dirname(self.CONF_BASE), filename="crontab.lock") if not ret: self.exit_proc(2009, "unlock error") self.opLog.info("start to stop") code, msg = common.runConfig(conf_file, "stop_script", install_path) if code != 0: msg = '执行停止脚本失败,code=%s,msg=%s' % (code,msg) self.opLog.info(msg) return code, msg self.opLog.info("stop end, start to check process") err_app, ok_app, ok_port, err_port = self.pkgCom.checkStop(conf_file, install_path) # 如果是restart的话是不更新状态的 if update_status: self.opLog.info("check end,update status") status = { 'status': 'stopped', 'time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), 'successProcess': ok_app, 'errorProcess': err_app, 'successPort': ok_port, 'errorPort': err_port } self.updateStatus(status) err_msg = "" code = 0 if err_app or err_port: if err_app: err_msg += ",error process:" + ";".join(err_app) if err_port: err_msg += ",error port:" + ";".join(map(str, err_port)) msg = 'stop failed %s' % (err_msg) code = 1010 self.opLog.info(msg) return code, msg self.opLog.info("stop successfully") return code, msg
def preScript(pkg_file, user): instance_conf_file = os.path.join(pkg_file, "package.conf.yaml") code, out = common.runConfig(instance_conf_file, 'install_prescript', pkg_file, user) logging.info(out) print out if code != 0: code = 1002 msg = "run pre script failed " + out logging.error(msg) print msg return code, msg return 0, 'ok'
def postScript(install_path, pkg_id, user): conf_path = getConfPath(install_path, pkg_id) instance_conf_file = os.path.join(conf_path, "package.conf.yaml") code, out = common.runConfig(instance_conf_file, 'install_postscript', install_path, user) if code != 0: code = 1002 msg = "run post script failed " + out logging.error(msg) print msg return code, msg logging.info(out) print out return 0, 'ok'
def postScript(install_path, pkg_id, user): pkg_real_path = os.path.realpath(install_path) m = hashlib.md5() m.update(pkg_real_path) instance_md5 = m.hexdigest() conf_path = os.path.join(conf_base_path, pkg_id, instance_md5[:7]) instance_conf_file = os.path.join(conf_path, "package.conf.yaml") code, out = common.runConfig(instance_conf_file, 'update_postscript', install_path, user) if code != 0: code = 1002 msg = "run post script failed " + out logging.error(msg) print msg return code, msg logging.error(out) print out return 0, 'ok'
def resolve(self, pkg_id, install_path): self.opLog.info("run resolve") conf_file = self.getConfigFile(pkg_id, install_path) code, msg = common.runConfig(conf_file, "resolve_script", install_path) self.opLog.info("resolve end,code:%s,msg:%s" % (code, msg)) return code, msg