RS code
RS code
anqiyouyou 发表于5个月前
RS code
  • 发表于 5个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

 

import os

import shutil

import xml.dom.minidom

import revitfarm.core.util.general as gr

 

from revitfarm.core.util.log import get_empty_logger

 

class RevitServerTools:

SERVER_ROOT_DIR = "C:/Temp/ServerRootDir"

DEBUG_MODES = "[DebugModes]"

REGRESSION_TEST_VALUE = "RegressionTestServerBasedWorkflow=1"

 

def __init__(self, versionID, rsOnIIS_exe_path, rs_release_path, rs_program, logger):

self.versionId = versionID

self.rsOnIIS_exe_path = rsOnIIS_exe_path

self.rs_release_path = rs_release_path

self.rs_program = rs_program

self.logger = logger

 

def set_env(self, versionId):

setEnv_cmd = '@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v RSROLE' + versionId + ' /t reg_expand_sz /d "Accelerator,Admin,Host" /f'

return gr.run_simple_cmd(setEnv_cmd, input=None, success_code=0, logger=self.logger)

 

def rm_env(self, versionId):

rmEnv_cmd = '@reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v RSROLE' + versionId + ' /f'

return gr.run_simple_cmd(rmEnv_cmd, input=None, success_code=0, logger=self.logger)

 

def rsOnIIS_install(self, versionID, rsOnIIS_exe_path, rs_release_path):

"""

install RevitServer

"""

cmd_line = rsOnIIS_exe_path+" i=\""+rs_release_path+"\" r="+versionID+" e"

subst_result = self.run_subst()

cmd_line = self.analyze_virtual_disk(subst_result, cmd_line)

run_cmd_result = gr.run_simple_cmd(cmd_line, input=None, success_code=0, logger=self.logger)

return run_cmd_result

 

def rsOnIIS_uninstall(self, versionID, rsOnIIS_exe_path):

"""

uninstall RevitServer

"""

cmd_line = rsOnIIS_exe_path+" u"

subst_result = self.run_subst()

cmd_line = self.analyze_virtual_disk(subst_result, cmd_line)

run_cmd_result = gr.run_simple_cmd(cmd_line, input=None, success_code=0, logger=self.logger)

return run_cmd_result

 

def run_subst(self):

subst_result = gr.run_command("subst", success_code=0, logger=None, print_output=False, progress_bar_pattern=None)

return subst_result[1]

 

def analyze_virtual_disk(self, subst_result, cmd_line):

"""

replace virtual replace with real disk path

:return: real path

"""

if str(subst_result) != "[]":

self.logger.info("slave machine have virtual disk need make install disk replace")

subst_reuslt = str(subst_result).replace("[","").replace("]","").replace("'","").replace(">","").split(": =")

truth_disk = str(subst_reuslt[0]).replace("\\\\", "\\").replace(" ", "")

virtual_disk = str(subst_reuslt[1]).replace("\\\\", "\\").replace(" ", "")

cmd_line = cmd_line.replace(truth_disk, virtual_disk+"\\")

self.logger.info("replace cmd is ".format(cmd_line))

return cmd_line

 

@classmethod

def find_word(cls, filename, word):

with open(filename, 'r') as f:

for l in f.readlines():

if word in l:

return True

return False

 

@classmethod

def find_word_location(cls, filename, word):

with open(filename, "r") as f:

all_text = f.read()

index = all_text.find(word)

return index

 

@classmethod

def insert_text(cls, filename, index, word):

with open(filename, "r") as f:

all_text = f.read()

new_text = all_text[:index] + word + all_text[index:]

with open(filename, "w+") as file:

file.write(new_text)

return new_text

 

@classmethod

def remove_text(cls, filename, word):

with open(filename, "r") as f:

all_text = f.read()

new_text = all_text.replace("\n" + word, "")

with open(filename, "w+") as file:

file.write(new_text)

return new_text

 

@classmethod

def check_create_rsn(self, rsn_ini):

if not os.path.exists(rsn_ini):

with open(rsn_ini, "w+") as rsn:

rsn.write("localhost")

 

@classmethod

def check_create_debug(cls, debug_ini):

if not os.path.exists(debug_ini):

with open(debug_ini, "w+") as debug:

debug.write("{}\n{}".format(cls.DEBUG_MODES, cls.REGRESSION_TEST_VALUE))

else:

if not cls.find_word(debug_ini, cls.REGRESSION_TEST_VALUE):

if (cls.find_word(debug_ini, cls.DEBUG_MODES)):

index = cls.find_word_location(debug_ini, cls.DEBUG_MODES)

cls.insert_text(debug_ini, index+12, "\n")

cls.insert_text(debug_ini, index+13, cls.REGRESSION_TEST_VALUE)

else:

index = 0

cls.insert_text(debug_ini, index, "\n")

cls.insert_text(debug_ini, index, cls.REGRESSION_TEST_VALUE)

cls.insert_text(debug_ini, index, "\n")

cls.insert_text(debug_ini, index, cls.DEBUG_MODES)

 

@classmethod

def remove_debug(cls, debug_ini):

if os.path.exists(debug_ini):

if (cls.find_word(debug_ini, cls.REGRESSION_TEST_VALUE)):

cls.remove_text(debug_ini, cls.REGRESSION_TEST_VALUE)

 

 

def execute_RS(self, versionId, ServerRootDir, rsOnIIS_exe_path, rs_release_path, rs_program):

rsn_ini = os.path.join(rs_program, "RSN.ini")

 

#Step1:set env execute setEnv.bat ,and check if failed will stop

self.logger.info("set ENV RSROLE%s" %versionId)

cmd_result = self.set_env(versionId)

if(cmd_result[0] is not True):

self.logger.error("set Env Failed")

 

#Step 2 prepare temp dir and file

self.logger.info("mkdir temp/ServerRootDir")

try:

if not os.path.exists(ServerRootDir):

os.mkdir(ServerRootDir)

except FileExistsError as e:

self.logger.error(e)

 

# make rsn.ini in Program dir

self.logger.info("create rsn.ini ")

self.check_create_rsn(rsn_ini)

 

#Step 3 unstall and install rsOnIIS,and check if failed python will exit

self.logger.info("uninstall RevitServer ")

unrs_result = self.rsOnIIS_uninstall(versionId, rsOnIIS_exe_path)

if(unrs_result[0] is not True):

self.logger.error("unstall rsOnIIS failed")

 

self.logger.info("install RevitServer ")

inrs_result = self.rsOnIIS_install(versionId, rsOnIIS_exe_path,rs_release_path)

if(inrs_result[0] is not True):

self.logger.error("install rsOnIIS failed")

 

def clean_RS(self, versionId, ServerRootDir, rsOnIIS_exe_path, rs_program):

# uninstall_cmd = self.rsOnIIS_uninstall(versionId, rsOnIIS_exe_path)

# rmEnv_cmd = '@reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v RSROLE'+versionId+' /f'

rsn_ini = os.path.join(rs_program, "RSN.ini")

 

# clean rsn.ini

if os.path.exists(rsn_ini):

os.remove(rsn_ini)

 

#Step 2 Uninstall rsOnIIS.exe

self.logger.info("uninstall RevitServer ")

unrs_result = self.rsOnIIS_uninstall(versionId, rsOnIIS_exe_path)

if(unrs_result[0] is not True):

self.logger.info("unstall rsOnIIS failed")

#Step3:delete env execute and check if failed will stop

self.logger.info("rm env RSROLE%s" %versionId)

cmd_result=self.rm_env(versionId)

if(cmd_result[0] is not True):

self.logger.error("rm env failed")

 

@classmethod

def get_version_id(cls, web_config_path, logger=None):

try:

if logger is None:

logger = get_empty_logger()

dom = xml.dom.minidom.parse(web_config_path)

service_node = dom.getElementsByTagName("client")

service_model = service_node[0].childNodes[1].getAttribute("address")

start = service_model.rfind("ModelService")

version_id = service_model[int(start + 12):int(start + 16)]

except SyntaxError as error:

logger.error(error)

return version_id

 

@classmethod

def generate_rs_onIIS_exe_path(cls, local_job_package_root):

return os.path.join(local_job_package_root, "Regression", "_RegressionTools", "RSInstallUtils", "rsOnIIS.exe")

 

@classmethod

def generate_rs_path(cls, local_job_package_root):

return os.path.join(local_job_package_root, "RevitServer")

 

@classmethod

def generate_rs_release_path(cls, local_job_package_root):

return os.path.join(local_job_package_root, "RevitServer", "Enterprise", "Releasex64")

 

@classmethod

def generate_web_config_path(cls,local_job_package_root):

return os.path.join(local_job_package_root, "RevitServer", "Enterprise","Releasex64", "Services", "RS.Service.Protected.Model", "web.config")

共有 人打赏支持
粉丝 0
博文 3
码字总数 1474
评论 (0)
×
anqiyouyou
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: