Permalink
Switch branches/tags
ver2.2.m4 vXAP9.0.2 vXAP-9.0.2 v2.2.0m2 v2.2.0M3_2491_59 v2.2.m5 v2.2.m4 v2.1.1GA Chef-10.12.0-Cloudify-2.2m4 2.7.1_m5_build6205_04_13_2014 2.7.1_m4_build6204_04_01_2014 2.7.1_m3_build6203_03_18_2014 2.7.1_m2_build6202_03_03_2014 2.7.1_m1_build6201_02_17_2014 2.7.1_ga_build6300_08_14_2014 2.7.1_ga_build6210_08_07_2014 2.7.0_rc2_build6000_11_27_2014 2.7.0_rc2_build5999_04_08_2014 2.7.0_rc2_build5999_03_26_2014 2.7.0_rc2_build5998_03_28_2014 2.7.0_rc2_build5997_03_25_2014 2.7.0_rc2_build5995_01_07_2014 2.7.0_rc_build5994_12_23_2013 2.7.0_m8_build5993_12_11_2013 2.7.0_m7_build5992_11_27_2013 2.7.0_m6_build5991_11_13_2013 2.7.0_m5_build5988_10_30_2013 2.7.0_m4_build5987_10_01_2013 2.7.0_m3_build5985_09_08_2013 2.7.0_m2_build5984_08_19_2013 2.7.0_m1_build5985_08_06_2013 2.7.0_m1_build5981_08_01_2013 2.7.0_ga_build5996_01_20_2014 2.6.2_ga_build5400_10_13_2013 2.6.1_ga_build5223_11_25_2013 2.6.1_ga_build5222_11_22_2013 2.6.1_ga_build5221_11_12_2013 2.6.1_ga_build5220_10_29_2013 2.6.1_ga_build5210_10_25_2013 2.6.1_ga_build5200_08_29_2013 2.6.0_rc_build4986_06_26_2013 2.6.0_rc_build4985_06_11_2013 2.6.0_m4_build4985_06_12_2013 2.6.0_m4_build4984_06_11_2013 2.6.0_m3_build4983_05_21_2013 2.6.0_m2_build4982_05_06_2013 2.6.0_m1_build4981_04_22_2013 2.6.0_ga_build5000_07_09_2013 2.5.2_ga_build4400_06_23_2013 2.5.1_ga_build4200_05_01_2013 2.5.0_rc_build3993_03_11_2013 2.5.0_m7_build3991_02_27_2013 2.5.0_m6_build3987_02_04_2013 2.5.0_m5_build3986_01_24_2013 2.5.0_m4_build3985_01_06_2013 2.5.0_m3_build3984_12_30_2012 2.5.0_ga_build4510_04_09_2013 2.5.0_ga_build4023_07_03_2013 2.5.0_ga_build4022_04_28_2013 2.5.0_ga_build4021_04_28_2013 2.5.0_ga_build4020_04_23_2013 2.5.0_ga_build4010_04_10_2013 2.5.0_ga_build4000_04_03_2013 2.3.1_ga_build3720_02_10_2013 2.3.1_ga_build3720_02_09_2013 2.3.1_ga_build3700_01_23_2013 2.3.0_rc_build3483_12_06_2012 2.3.0_m2_build3482_11_20_2012 2.3.0_m1_build3481_11_06_2012 2.3.0_ga_build3510_01_14_2013 2.3.0_ga_build3500_12_16_2012 2.3.0_ga_build3500_12_13_2012 2.2.0_rc_build2496_10_03_2012 2.2.0_m5_build2495_09_21_2012 2.2.0_m4_build2494_09_09_2012 2.2.0_ga_build2500_10_19_2012
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
232 lines (200 sloc) 9.44 KB
/*******************************************************************************
* Copyright (c) 2012 GigaSpaces Technologies Ltd. All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
import org.hyperic.sigar.OperatingSystem
import java.util.concurrent.TimeUnit
import org.cloudifysource.dsl.utils.ServiceUtils;
import org.cloudifysource.utilitydomain.context.ServiceContextFactory
import static mysql_runner.*
config=new ConfigSlurper().parse(new File('mysql-service.properties').toURL())
osConfig=ServiceUtils.isWindows() ? config.win64 : config.linux
context = ServiceContextFactory.getServiceContext()
mysqlHost=context.attributes.thisInstance["dbHost"]
binFolder=context.attributes.thisInstance["binFolder"]
println "mysql_start.groovy: mysqlHost is ${mysqlHost} "
def runPostStartActions(config,binFolder,osConfig,currOsName,builder,context,mysqlHost) {
def currActionQuery
def actionCounter = 1
for ( currActionObj in config.postStartActions ) {
println "mysql_start.runPostStartActions: postStartActions #${actionCounter}:"
def currActionType = currActionObj["actionType"]
def currActionUser = currActionObj["actionUser"]
def currActionDbName = currActionObj["actionDbName"]
def currDebugMsg = currActionObj["debugMsg"]
currDebugMsg = currDebugMsg.replaceAll("MYSQLHOST",mysqlHost)
switch (currActionType) {
case "mysqladmin":
currActionQuery = currActionObj["actionQuery"]
currActionQuery = currActionQuery.replaceAll("MYSQLHOST",mysqlHost)
runMysqlAdmin(binFolder,osConfig.mysqladmin,currOsName,currActionQuery,currActionDbName,currActionUser,currDebugMsg,"mysqladminOutput",true)
break
case "mysql":
currActionQuery = currActionObj["actionQuery"]
currActionQuery = currActionQuery.replaceAll("MYSQLHOST",mysqlHost)
runMysqlQuery(binFolder,osConfig.mysqlProgram,currOsName,currActionQuery,currActionDbName,currActionUser,currDebugMsg,"mysqlOutput",true)
break
case "import":
def currImportZip="${context.serviceDirectory}/"+currActionObj["importedZip"]
def importedFile=currActionObj["importedFile"]
def importedFileUrl=currActionObj["importedFileUrl"]
importFileToDB(binFolder,osConfig,currOsName,currActionDbName,currImportZip,importedFile,importedFileUrl,builder,context,currActionUser,currDebugMsg,config)
break
case "mysqldump":
currActionArgs = currActionObj["actionArgs"]
currDumpPrefix = currActionObj["dumpPrefix"]
def dumpFolder = System.properties["user.home"]
runMysqlDump(binFolder,osConfig.mysqldump,currOsName,currActionArgs,currActionDbName,currActionUser,currDebugMsg,dumpFolder,currDumpPrefix)
break
default:
println "Ignoring Action Type ${currActionType} ... "
break
}
actionCounter++
}
}
def addSlaveToMaster(context,config,mysqlHost) {
while ( !context.attributes.thisService["masterIsReady"] ) {
println "mysql_start.groovy: addSlaveToMaster: Slave is waiting for the master..."
sleep 10000
}
println "mysql_start.groovy: addSlaveToMaster: Waiting for my future master ..."
def mysqlService = context.waitForService("mysql", 180, TimeUnit.SECONDS)
def masterInstances = mysqlService.waitForInstances(mysqlService.numberOfActualInstances, 180, TimeUnit.SECONDS)
def index
def currInstance
def masterInstance
def masterHostAddress
def masterID = context.attributes.thisService["masterID"]
def instancesCount = masterInstances.length
println "mysql_start.groovy: addSlaveToMaster: ${instancesCount} instances are available..."
for (index=0; index < instancesCount; index++) {
println "mysql_start.groovy: addSlaveToMaster: In loop b4 index ${index} ... "
currInstance = masterInstances[index]
println "mysql_start.groovy: addSlaveToMaster: In loop after index ${index} currInstance.getInstanceID = " + currInstance.getInstanceID()
if ( masterID == currInstance.getInstanceID() ) {
masterHostAddress = currInstance.hostAddress
masterInstance = currInstance
println "mysql_start.groovy: addSlaveToMaster: master instance id is ${masterID}"
break;
}
}
println "mysql_start.groovy: masterHostAddress is ${masterHostAddress}"
println "mysql_start.groovy: addSlaveToMaster: About to invoke addSlave on the master (instance # ${masterID}) ... - Using dbName ${config.dbName}, user ${config.dbUser}, mysqlHost ${mysqlHost} ... "
def currActionUser = "root"
def currDbName = "${config.dbName}"
def currDbUser = "${config.dbUser}"
def currDbPassw = "${config.dbPassW}"
/* This will grant replication permissions to the slave. */
def currResult = masterInstance.invoke("addSlave", currActionUser as String, currDbName as String, currDbUser as String, currDbPassw as String, mysqlHost as String)
println "mysql_start.groovy: addSlaveToMaster : addSlave result is ${currResult}"
println "mysql_start.groovy: addSlaveToMaster : Invoking addSlave on the master (instance # ${masterID}) ended."
return masterHostAddress
}
def changeMasterTo(config,binFolder,osConfig,currOsName,context,masterHost) {
println "mysql_start.groovy: In changeMasterTo ..."
while ( !context.attributes.thisService["masterIsReady"] ) {
println "mysql_start.groovy: changeMasterTo: Slave is waiting for the master..."
sleep 10000
}
def binLog = context.attributes.thisApplication["masterBinLogFile"]
def logPos = context.attributes.thisApplication["masterBinLogPos"]
def currActionQuery = "\"" + "CHANGE MASTER TO MASTER_HOST='${masterHost}', MASTER_USER='${config.dbUser}', MASTER_PASSWORD='${config.dbPassW}', MASTER_LOG_FILE='${binLog}', MASTER_LOG_POS=${logPos};" + "\""
def currDebugMsg = "About to invoke ${currActionQuery}..."
runMysqlQuery(binFolder,osConfig.mysqlProgram,currOsName,currActionQuery,"${config.dbName}","root",currDebugMsg,"changeMasterOutput",true)
println "mysql_start.groovy: changeMasterTo Ended"
}
def startSlave(config,binFolder,osConfig,currOsName) {
println "mysql_start.groovy: In startSlave ..."
def currActionQuery = "\"" + "start slave;" + "\""
def currDebugMsg = "About to invoke ${currActionQuery}..."
runMysqlQuery(binFolder,osConfig.mysqlProgram,currOsName,currActionQuery,"${config.dbName}","root",currDebugMsg,"startSlaveOutput",true)
println "mysql_start.groovy: startSlave Ended"
}
def stopSlave(config,binFolder,osConfig,currOsName) {
println "mysql_start.groovy: In stopSlave ..."
def currActionQuery = "\"" + "stop slave;" + "\""
def currDebugMsg = "About to invoke ${currActionQuery}..."
runMysqlQuery(binFolder,osConfig.mysqlProgram,currOsName,currActionQuery,"${config.dbName}","root",currDebugMsg,"startSlaveOutput",true)
println "mysql_start.groovy: stopSlave Ended"
}
builder = new AntBuilder()
def os = OperatingSystem.getInstance()
def currVendor=os.getVendor()
switch (currVendor) {
case ["Ubuntu", "Debian", "Mint"]:
script="${context.serviceDirectory}/runOnUbuntu.sh"
currOsName="unix"
break
case ["Red Hat", "CentOS", "Fedora", "Amazon",""]:
script="${context.serviceDirectory}/run.sh"
currOsName="unix"
break
case ~/.*(?i)(Microsoft|Windows).*/:
script="${binFolder}/${osConfig.mysqlD}"
currOsName="windows"
break
default: throw new Exception("Support for ${currVendor} is not implemented")
}
builder.sequential {
echo(message:"mysql_start.groovy: Running ${script} on ${currOsName} ...")
exec(executable:"${script}", osfamily:"${currOsName}",failonerror: "true")
}
/* Restart does not require postStart, so if the postStartRequired attribute is false , postStart will NOT be executed. */
def postStartRequired = context.attributes.thisInstance["postStartRequired"]
println "mysql_start.groovy: postStartRequired is ${postStartRequired}"
def masterHost
def isSlave = context.attributes.thisInstance["isSlave"]
if ( postStartRequired ) {
try {
println "mysql_start.groovy: postStart"
runPostStartActions(config,binFolder,osConfig,currOsName,builder,context,mysqlHost)
if ( config.masterSlaveMode ) {
println "mysql_start.groovy: Running in a masterSlaveMode mode"
if ( isSlave ) {
println "mysql_start.groovy: I am a slave..."
masterHost = addSlaveToMaster(context,config,mysqlHost)
context.attributes.thisInstance["masterHost"] = masterHost
}
}
}
catch (Exception ioe) {
println "mysql_start.groovy: Connection Failed!"
println ioe
}
}
else {
if ( config.masterSlaveMode ) {
if ( isSlave ) {
masterHost = context.attributes.thisInstance["masterHost"]
}
}
}
if ( config.masterSlaveMode ) {
println "mysql_start.groovy: masterSlaveMode"
if ( isSlave ) {
println "mysql_start.groovy: I am a slave b4 invoking changeMasterTo..."
changeMasterTo(config,binFolder,osConfig,currOsName,context,masterHost)
println "mysql_start.groovy: Starting slave threads..."
startSlave(config,binFolder,osConfig,currOsName)
}
else {
println "mysql_start.groovy: I am a master..."
}
}
else {
println "mysql_start.groovy: Running in a standalone mode"
}
context.attributes.thisInstance["postStartRequired"] = false
println "mysql_start.groovy: End"