This repository has been archived by the owner on Jan 31, 2019. It is now read-only.
/
mysql_query.groovy
87 lines (73 loc) · 2.88 KB
/
mysql_query.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
/*******************************************************************************
* 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 org.cloudifysource.dsl.utils.ServiceUtils;
import org.cloudifysource.dsl.context.ServiceContextFactory
import static mysql_runner.*
/*
This file enables users to invoke an SQL statement
Usage : invoke mysql query actionUser [puserPassword] dbName query
Examples:
1. invoke mysql query root myDbName "update users set city=\"NY\" where uid=15"
2. invoke mysql query root pmyRootPassword myDbName "update users set city=\"NY\" where uid=15"
*/
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_query.groovy: mysqlHost is ${mysqlHost} "
def currActionQuery
def currOsName
def os = OperatingSystem.getInstance()
def currVendor=os.getVendor()
switch (currVendor) {
case ["Ubuntu", "Debian", "Mint"]:
currOsName="unix"
break
case ["Red Hat", "CentOS", "Fedora", "Amazon",""]:
currOsName="unix"
break
case ~/.*(?i)(Microsoft|Windows).*/:
currOsName="windows"
break
default: throw new Exception("Support for ${currVendor} is not implemented")
}
if (args.length < 3) {
println "mysql_query.groovy: query error: Missing parameters\nUsage: invoke serviceName query actionUser [-puserPassword] dbName query"
System.exit(-1)
}
def currActionDbName
def currPassword
def currQuery
def tmpArg
def currActionUser = args[0]
tmpArg = args[1]
if ( tmpArg.toLowerCase().startsWith("p") ) {
/* retrieve the password , remove the 1st char(p)*/
currPassword = tmpArg.substring(1)
currActionDbName = args[2]
currQuery = "\"" + args[3] + "\""
}
else {
/* invoke without a password */
currPassword = ""
currActionDbName = tmpArg
currQuery = "\"" + args[2] + "\""
}
def currDebugMsg = "Invoking query: ${currQuery}"
runMysqlQuery(binFolder,osConfig.mysqlProgram,currOsName,currQuery,currActionDbName,currActionUser,currPassword,currDebugMsg,"queryOutput",true)
println "mysql_query.groovy: End"