Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chixq
Created August 28, 2013 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chixq/6364299 to your computer and use it in GitHub Desktop.
Save chixq/6364299 to your computer and use it in GitHub Desktop.
'''
Created on Mar 16, 2013
@author: liutao
'''
import os
import re
import string
from domain.common.bl.abstraction.IComponentDeployer import IComponentDeployer
from infrastructure.common.shell.ShellCmdExecutor import ShellCmdExecutor
from infrastructure.common.utility import Utility
from infrastructure.common.file.FileUtil import FileUtil
from infrastructure.common.logger.LoggerFactory import LoggerFactory
logger = LoggerFactory.getLogger(__name__)
from domain.common.model.deploymenthistory.DeploymentRecord import DeploymentRecord
class DecompressInstallDeployer(IComponentDeployer):
'''
classdocs
'''
BUILD_SAVE_PATH = "/tmp/cloudpi/build"
def __init__(self,
buildDownloader=None,
deploymentPlanDAO=None,
deploymentHistoryDAO=None,
farmParameterDAO=None):
'''
Constructor
'''
self.buildDownloader = buildDownloader
self.deploymentPlanDAO = deploymentPlanDAO
self.deploymentHistoryDAO = deploymentHistoryDAO
self.farmParameterDAO = farmParameterDAO
def undeploy(self, localServerInfo, component_id):
policy = localServerInfo.getFarm().getOpsPolicy()
role_name = localServerInfo.getRole().getRoleName()
self.component_deploy_plan = self.deploymentPlanDAO.getComponentDeploymentPlan(policy, role_name, component_id)
undeploy_cmd = self.component_deploy_plan.getUnDeployCmd()
output, error = ShellCmdExecutor.execCmd(undeploy_cmd)
pass
def deploy(self, localServerInfo, component_id):
logger.info("Start to deploy component build!")
policy = localServerInfo.getFarm().getOpsPolicy()
role_name = localServerInfo.getRole().getRoleName()
self.component_deploy_plan = self.deploymentPlanDAO.getComponentDeploymentPlan(policy, role_name, component_id)
#self.build_info = self.component_deploy_plan.getBuildInfo() not get build info from deployment plan
component_build_info = self.farmParameterDAO.getComponentBuildInfo(policy, component_id)
logger.info("To deploy component_build is %s" % component_build_info.toJSON())
self.build_unzip_to_path = "%s/%s" % (self.BUILD_SAVE_PATH, component_build_info.getSystemName())
component_relative_dir_path_in_build = self.component_deploy_plan.getComponentRelativeDirPathInBuild()
self.deployment_source_dir_path = "%s/%s" % (self.build_unzip_to_path, component_relative_dir_path_in_build)
self.deployment_target_dir_path = self.component_deploy_plan.getDeploymentTargetDirPath()
logger.info("target dir%s"%self.deployment_target_dir_path)
if self.__isBuildDeployed(component_build_info, component_id, self.deployment_target_dir_path) == False :
try:
buildZipFilePath = self.__downloadBuildZip(policy, component_id, DecompressInstallDeployer.BUILD_SAVE_PATH)
unzipAutoOpsFolderPath = self.__unzipBuildZip(buildZipFilePath, self.build_unzip_to_path)
self.undeploy(localServerInfo, component_id)
self.__deployUnzipBuild(self.build_unzip_to_path,
unzipAutoOpsFolderPath,
self.deployment_target_dir_path)
if FileUtil.getDirsComparison(self.deployment_source_dir_path, self.deployment_target_dir_path) == True :
buildZipFilePath_md5 = FileUtil.getFileMD5(buildZipFilePath)
to_deploy_build_md5 = component_build_info.getBuildMD5()
if buildZipFilePath_md5 == to_deploy_build_md5 :
success = "True"
logger.info("deployment_source_dir_path files are equal to deployment_target_dir_path files, success is True.")
else :
success = "False"
logger.info("buildZipFilePath_md5 is different to to_deploy_build_md5, success is False.")
else :
success = "False"
logger.info("deployment_source_dir_path files are different to deployment_target_dir_path files, success is False.")
except Exception, e:
logger.error(e)
success = "False"
raise Exception(e)
finally :
self.__saveDeploymentHistory(component_build_info,
component_id,
success)
else :
msg = "Do not need to deploy build %s!" % component_build_info.toJSON()
logger.info(msg)
logger.info("End deploy component build!")
logger.info("Enter install step")
self.install(self.deployment_target_dir_path)
def install(self,install_file_path):
try:
run_install_command = "cd {installfilepath};./install.sh".format(installfilepath=install_file_path)
output, error = ShellCmdExecutor.execCmd(run_install_command)
except Exception, e:
raise Exception(str(e))
pass
def isDeploymentRequired(self, localServerInfo, component_id):
logger.info("Start to check if deployment is required!")
policy = localServerInfo.getFarm().getOpsPolicy()
component_build_info = self.farmParameterDAO.getComponentBuildInfo(policy, component_id)
policy = localServerInfo.getFarm().getOpsPolicy()
role_name = localServerInfo.getRole().getRoleName()
isRequired = True
self.component_deploy_plan = self.deploymentPlanDAO.getComponentDeploymentPlan(policy, role_name, component_id)
if self.component_deploy_plan != None:
self.deployment_target_dir_path = self.component_deploy_plan.getDeploymentTargetDirPath()
logger.info("component_build_info=%s" % component_build_info.toJSON())
if self.__isBuildDeployed(component_build_info, component_id, self.deployment_target_dir_path) == False :
isRequired = True
else :
isRequired = False
else:
isRequired = False
logger.info("Check if deployment is required done!")
return isRequired
def __saveDeploymentHistory(self, component_build_info, component_id, success):
deployment_record = DeploymentRecord()
deployment_time=Utility.getCurrentReadableTimeString()
deployment_record.setDeploymentTime(deployment_time)
deployment_record.setBuildInfo(component_build_info)
deployment_record.setSuccess(success)
self.deploymentHistoryDAO.saveDeploymentRecord(component_id, deployment_record)
def __isBuildDeployed(self, component_build_info, component_id, deployment_target_dir_path):
logger.info("To check if deployment is required!")
isDeployed = False
deployment_record = self.deploymentHistoryDAO.getLatestDeploymentRecord(component_id)
if deployment_record!=None :
local_deploy_build_md5 = deployment_record.getBuildInfo().getBuildMD5()
last_deployment_success = deployment_record.getSuccess()
to_deploy_build_md5 = component_build_info.getBuildMD5()
logger.info("Last deploy build md5 is %s" % local_deploy_build_md5)
logger.info("To deploy build md5 is %s" % to_deploy_build_md5)
logger.info("last_deployment_success is %s" % last_deployment_success)
if to_deploy_build_md5 == local_deploy_build_md5 and last_deployment_success == "True" :
isDeployed = True
if os.path.exists(deployment_target_dir_path) :
cmd = "ls {deployment_dir_path} | wc -l".format(deployment_dir_path=deployment_target_dir_path)
output, error = ShellCmdExecutor.execCmd(cmd)
files_num = string.atoi(output)
if files_num == 0 :
isDeployed = False
pass
else :
isDeployed = False
else :
logger.debug("Deployment record does not exist!")
if isDeployed == True :
logger.info("Deployment is not required!")
else :
logger.info("Deployment is required!")
logger.info("End check if deployment is required!")
return isDeployed
def __downloadBuildZip(self, farm_name, component_id, to_dir) :
logger.info("Start to download component build!")
output, error = ShellCmdExecutor.execCmd("mkdir -p " + to_dir)
logger.debug('Build is to be downloaded to %s' % to_dir)
build_zip_file_path = self.buildDownloader.downloadBuild(farm_name, component_id, to_dir)
logger.info("Build is downloaded successfully to %s!" % build_zip_file_path)
logger.info("End download component build!")
return build_zip_file_path
def __unzipBuildZip(self, buildZipFilePath, unzipBuildDirPath):
logger.info("Start to unzip downloaded component build!")
try:
rm_unzip_dir_command = "rm -rf {unzip_to_path}".format(unzip_to_path=unzipBuildDirPath)
logger.debug("rm_unzip_dir_command=%s" % rm_unzip_dir_command)
output, error = ShellCmdExecutor.execCmd(rm_unzip_dir_command)
mk_unzip_dir_command = "mkdir -p {unzip_to_path}".format(unzip_to_path=unzipBuildDirPath)
logger.debug("mk_unzip_dir_command=%s" % mk_unzip_dir_command)
output, error = ShellCmdExecutor.execCmd(mk_unzip_dir_command)
unzip_command = "unzip -d {unzip_to_path} {build_zip_file_path}".format(unzip_to_path=unzipBuildDirPath,
build_zip_file_path=buildZipFilePath)
logger.debug("unzip_command=%s" % unzip_command)
output, error = ShellCmdExecutor.execCmd(unzip_command)
#get unzip final dir path
getUnzipBuildDirPathCmd = "ls {unzip_build_dir_path}".format(unzip_build_dir_path=unzipBuildDirPath)
output, error = ShellCmdExecutor.execCmd(getUnzipBuildDirPathCmd)
unzipAutoOpsFolderPath = "{unzipBuildDirPath}/{unzipFolderName}".format(unzipBuildDirPath=unzipBuildDirPath, unzipFolderName=output.replace("\n",""))
return unzipAutoOpsFolderPath
except Exception, e:
raise Exception(str(e))
logger.info("End unzip downloaded component build!")
def __deployUnzipBuild(self, unzip_to_path, deployment_source_dir_path, deployment_target_dir_path):
logger.info("Start to deploy unzipped downloaded component build!")
logger.info("Start to Make deployment target dir!")
mkdir_deployment_dir_cmd = "mkdir -p {deployment_target_dir}".format(deployment_target_dir=deployment_target_dir_path)
logger.info("mkdir_deployment_dir_cmd=%s" % mkdir_deployment_dir_cmd)
ShellCmdExecutor.execCmd(mkdir_deployment_dir_cmd)
logger.info("Make deployment target dir done!")
logger.info("Start to copy unzipped build to deployment target dir!")
shopt_dotglob_command = "shopt -s dotglob"
cp_binaries_command = "cp -R {deployment_source_dir}/. {deployment_target_dir}".format(deployment_source_dir=deployment_source_dir_path,
deployment_target_dir=deployment_target_dir_path)
logger.info("shopt_dotglob_command=%s" % shopt_dotglob_command)
logger.info("cp_binaries_command=%s" % cp_binaries_command)
ShellCmdExecutor.execCmd(shopt_dotglob_command)
output, error = ShellCmdExecutor.execCmd(cp_binaries_command)
logger.info("Copy unzipped build to deployment target dir done!")
if error != None and error !="" :
logger.error("Copy unzipped build to deployment target dir failed!")
msg = "DeployUnzipBuild failed!"
logger.error(msg)
raise Exception(error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment