PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/services/mysql/mysql_query.groovy

https://github.com/agilemobiledev/cloudify-recipes
Groovy | 87 lines | 49 code | 14 blank | 24 comment | 4 complexity | db6e0b00e918cc3a80905c5641ae4823 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*******************************************************************************
  2. * Copyright (c) 2012 GigaSpaces Technologies Ltd. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *******************************************************************************/
  16. import org.hyperic.sigar.OperatingSystem
  17. import org.cloudifysource.dsl.utils.ServiceUtils;
  18. import org.cloudifysource.dsl.context.ServiceContextFactory
  19. import static mysql_runner.*
  20. /*
  21. This file enables users to invoke an SQL statement
  22. Usage : invoke mysql query actionUser [puserPassword] dbName query
  23. Examples:
  24. 1. invoke mysql query root myDbName "update users set city=\"NY\" where uid=15"
  25. 2. invoke mysql query root pmyRootPassword myDbName "update users set city=\"NY\" where uid=15"
  26. */
  27. config=new ConfigSlurper().parse(new File('mysql-service.properties').toURL())
  28. osConfig=ServiceUtils.isWindows() ? config.win64 : config.linux
  29. context = ServiceContextFactory.getServiceContext()
  30. mysqlHost=context.attributes.thisInstance["dbHost"]
  31. binFolder=context.attributes.thisInstance["binFolder"]
  32. println "mysql_query.groovy: mysqlHost is ${mysqlHost} "
  33. def currActionQuery
  34. def currOsName
  35. def os = OperatingSystem.getInstance()
  36. def currVendor=os.getVendor()
  37. switch (currVendor) {
  38. case ["Ubuntu", "Debian", "Mint"]:
  39. currOsName="unix"
  40. break
  41. case ["Red Hat", "CentOS", "Fedora", "Amazon",""]:
  42. currOsName="unix"
  43. break
  44. case ~/.*(?i)(Microsoft|Windows).*/:
  45. currOsName="windows"
  46. break
  47. default: throw new Exception("Support for ${currVendor} is not implemented")
  48. }
  49. if (args.length < 3) {
  50. println "mysql_query.groovy: query error: Missing parameters\nUsage: invoke serviceName query actionUser [-puserPassword] dbName query"
  51. System.exit(-1)
  52. }
  53. def currActionDbName
  54. def currPassword
  55. def currQuery
  56. def tmpArg
  57. def currActionUser = args[0]
  58. tmpArg = args[1]
  59. if ( tmpArg.toLowerCase().startsWith("p") ) {
  60. /* retrieve the password , remove the 1st char(p)*/
  61. currPassword = tmpArg.substring(1)
  62. currActionDbName = args[2]
  63. currQuery = "\"" + args[3] + "\""
  64. }
  65. else {
  66. /* invoke without a password */
  67. currPassword = ""
  68. currActionDbName = tmpArg
  69. currQuery = "\"" + args[2] + "\""
  70. }
  71. def currDebugMsg = "Invoking query: ${currQuery}"
  72. runMysqlQuery(binFolder,osConfig.mysqlProgram,currOsName,currQuery,currActionDbName,currActionUser,currPassword,currDebugMsg,"queryOutput",true)
  73. println "mysql_query.groovy: End"