This repository has been archived by the owner on Jan 31, 2019. It is now read-only.
/
mysql_runner.groovy
222 lines (183 loc) · 7.77 KB
/
mysql_runner.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*******************************************************************************
* 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 java.text.SimpleDateFormat
def static runMysqlQuery(binFolder,execFile,osName,currQuery,dbName,dbUser,debugMsg,outputpropertyName,displayOutputProperty) {
return runMysqlQuery(binFolder,execFile,osName,currQuery,dbName,dbUser,"",debugMsg,outputpropertyName,displayOutputProperty)
}
def static runMysqlQuery(binFolder,execFile,osName,currQuery,dbName,dbUser,dbUserPassword,debugMsg,outputpropertyName,displayOutputProperty) {
def outputPropertyStr
def builder
def currPassword = ""
if ( dbUserPassword.length() > 0 ) {
currPassword = "-p${dbUserPassword}"
}
try {
builder = new AntBuilder()
builder.sequential {
echo(message:"runMysqlQuery: os ${osName}: ${debugMsg}")
echo(message:"runMysqlQuery: ${binFolder}/${execFile} -u ${dbUser} ${currPassword} -D ${dbName} -e ${currQuery}")
exec(executable:"${binFolder}/${execFile}", osfamily:"${osName}", outputproperty:"${outputpropertyName}") {
arg(line:"-u ${dbUser} ${currPassword} -D ${dbName} -e ${currQuery}")
}
}
}
catch (Exception ioe) {
println "runMysqlQuery: Connection Failed!"
ioe.printStackTrace();
}
outputPropertyStr = builder.project.properties."${outputpropertyName}"
if ( displayOutputProperty ) {
println "runMysqlQuery: outputproperty (${outputpropertyName}) is : ${outputPropertyStr}"
}
println "runMysqlQuery: Ended"
return outputPropertyStr
}
def static importMysqlDB(binFolder,execFile,osName,importedFile,dbName,dbUser,debugMsg,outputpropertyName,displayOutputProperty) {
def outputPropertyStr
def builder
try {
builder = new AntBuilder()
builder.sequential {
echo(message:"importMysqlDB: ${debugMsg}")
exec(executable:"${binFolder}/${execFile}", osfamily:"${osName}" ,input:"${importedFile}", outputproperty:"${outputpropertyName}") {
arg(value:"-u")
arg(value:"${dbUser}")
arg(value:"${dbName}")
}
}
}
catch (Exception ioe) {
println "importMysqlDB: Connection Failed!"
ioe.printStackTrace();
}
outputPropertyStr = builder.project.properties."${outputpropertyName}"
if ( displayOutputProperty ) {
println "importMysqlDB: outputproperty (${outputpropertyName}) is : ${outputPropertyStr}"
}
println "importMysqlDB: Ended"
return outputPropertyStr
}
def static runMysqlAdmin(binFolder,execFile,osName,actionName,dbName,dbUser,debugMsg,outputpropertyName,displayOutputProperty) {
def outputPropertyStr
def builder
try {
builder = new AntBuilder()
builder.sequential {
echo(message:"runMysqlAdmin: ${debugMsg}")
exec(executable:"${binFolder}/${execFile}", osfamily:"${osName}", outputproperty:"${outputpropertyName}") {
arg(value:"-u")
arg(value:"${dbUser}")
arg(value:"${actionName}")
arg(value:"${dbName}")
}
}
}
catch (Exception ioe) {
println "runMysqlAdmin: Connection Failed!"
ioe.printStackTrace();
}
outputPropertyStr = builder.project.properties."${outputpropertyName}"
if ( displayOutputProperty ) {
println "runMysqlAdmin: outputproperty (${outputpropertyName}) is : ${outputPropertyStr}"
}
println "runMysqlAdmin: Ended"
return outputPropertyStr
}
def static runMysqlDump(binFolder,execFile,osName,actionArgs,dbName,dbUser,debugMsg,dumpFolder,dumpPrefix,requiredBackupType,bucketName,backupFile,s3Folder) {
try {
def dbFlag
if ( "${dbName}" == "" ) {
dbFlag = "--all-databases"
}
else {
dbFlag = "--databases ${dbName}"
}
def argsLine = "-u ${dbUser} ${actionArgs} ${dbFlag} "
def currMillis=System.currentTimeMillis()
def currTime = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(currMillis)
def baseDumpName = "${dumpPrefix}${currTime}"
def sqlFileName = "${baseDumpName}.sql"
def baseRootName = "${dumpFolder}/${baseDumpName}"
def dumpSqlFullPath = "${baseRootName}.sql"
def dumpZipFullPath = "${baseRootName}.zip"
def builder = new AntBuilder()
builder.sequential {
echo(message:"runMysqlDump: ${debugMsg}")
exec(executable:"${binFolder}/${execFile}", osfamily:"${osName}", output:"${dumpSqlFullPath}") {
arg(line:"${argsLine}")
}
zip(destFile:"${dumpZipFullPath}", basedir: "${dumpFolder}" ,includes:"${sqlFileName}", update:true )
if ( requiredBackupType == "s3" ) {
if ( bucketName != "" ) {
def baseFolder = System.properties["user.home"]
exec(executable:"${backupFile}", osfamily:"${osName}") {
arg(value:"${baseFolder}")
arg(value:"${s3Folder}")
arg(value:"${bucketName}")
arg(value:"${dumpZipFullPath}")
arg(value:"${baseDumpName}.zip")
}
}
}
delete(file:"${dumpSqlFullPath}")
delete(file:"${dumpZipFullPath}")
}
}
catch (Exception ioe) {
println "runMysqlDump: Connection Failed!"
ioe.printStackTrace();
}
println "runMysqlDump: Ended"
}
def static importFileToDB(binFolder,osConfig,currOsName,currActionDbName,currImportZip,importedFile,importedFileUrl,builder,context,currActionUser,currDebugMsg,config) {
builder.sequential {
echo(message:"importFileToDB: Getting ${importedFileUrl} to ${currImportZip} ...")
get(src:"${importedFileUrl}", dest:"${currImportZip}", skipexisting:true)
echo(message:"importFileToDB: Unzipping ${currImportZip} to ${context.serviceDirectory} ...")
unzip(src:"${currImportZip}", dest:"${context.serviceDirectory}", overwrite:true)
}
def fullPathToImport="${context.serviceDirectory}/${importedFile}"
def currImportFile = new File(fullPathToImport)
def importText = currImportFile.text
println "importFileToDB: Replacing REPLACE_WITH_DB_NAME with ${config.dbName} in ${context.serviceDirectory}/${importedFile} ..."
currImportFile.text = importText.replace("REPLACE_WITH_DB_NAME",currActionDbName)
importMysqlDB(binFolder,osConfig.mysqlProgram,currOsName,fullPathToImport,currActionDbName,currActionUser,currDebugMsg,"dummy",false)
}
def static showMasterStatus(context,binFolder,execFile,currOsName,currQuery,currActionDbName,currActionUser,currDebugMsg,outputpropertyName,displayOutputProperty) {
def binLogData = runMysqlQuery(binFolder,execFile,currOsName,currQuery,currActionDbName,currActionUser,currDebugMsg,outputpropertyName,displayOutputProperty)
def binLogName="mysql-bin"
println "showMasterStatus: binLogData is "+binLogData
println "showMasterStatus: binLogData length is "+binLogData.length()
if ( binLogData.length() > 0 && binLogData.contains("Position") ) {
println "showMasterStatus: splitting binLogData ... "
def rawData = binLogData.split(binLogName)[1]
println "showMasterStatus: splitting rawData ... "
def dataArr = rawData.split()
println "showMasterStatus: setting binLog ... "
def binLog=binLogName+dataArr[0].trim()
println "showMasterStatus: setting logPos ... "
def logPos=dataArr[1].trim()
println "showMasterStatus: masterBinLogFile is "+binLog
println "showMasterStatus: masterBinLogPos is "+logPos
context.attributes.thisApplication["masterBinLogFile"] = binLog
context.attributes.thisApplication["masterBinLogPos"] = logPos
return true
}
else {
println "showMasterStatus: master is NOT ready yet"
return false
}
}