PageRenderTime 34ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/builder/extra/tools/ruby/configure/packages/configure_service_package.rb

http://tungsten-replicator.googlecode.com/
Ruby | 315 lines | 273 code | 40 blank | 2 comment | 49 complexity | a667973683eace942b1257368d3b4b15 MD5 | raw file
Possible License(s): GPL-2.0
  1. TEMP_DEPLOYMENT_SERVICE = "temp"
  2. class ConfigureServicePackage < ConfigurePackage
  3. SERVICE_CREATE = "create_service"
  4. SERVICE_DELETE = "delete_service"
  5. SERVICE_UPDATE = "update_service"
  6. @deploy_service_key = nil
  7. def parsed_options?(arguments)
  8. if Configurator.instance.display_help? && !Configurator.instance.display_preview?()
  9. return true
  10. end
  11. arguments = load_target_config(arguments)
  12. unless Configurator.instance.display_preview?
  13. unless @config.props.size > 0
  14. error("Unable to run configure-service because this directory has not been setup")
  15. error("Try running this command from the directory where Tungsten is installed or adding --host, --user and --release-directory for the server and directory you are trying to modify. These values should match what you provided when calling 'tools/configure'.")
  16. return false
  17. end
  18. end
  19. @config.setProperty(DEPLOYMENT_TYPE, nil)
  20. @config.setProperty(DEPLOY_CURRENT_PACKAGE, nil)
  21. @config.setProperty(DEPLOY_PACKAGE_URI, nil)
  22. service_config = Properties.new
  23. datasource_options = Properties.new()
  24. datasource_options.setProperty([DATASOURCES, "master"], {})
  25. datasource_options.setProperty([DATASOURCES, "ds"], {})
  26. opts=OptionParser.new
  27. opts.on("-C", "--create") { @config.setProperty(DEPLOYMENT_TYPE, SERVICE_CREATE) }
  28. opts.on("-D", "--delete") { @config.setProperty(DEPLOYMENT_TYPE, SERVICE_DELETE) }
  29. opts.on("-U", "--update") { @config.setProperty(DEPLOYMENT_TYPE, SERVICE_UPDATE) }
  30. each_service_prompt{
  31. |prompt|
  32. if (av = prompt.get_command_line_argument_value()) != nil
  33. opts.on("--#{prompt.get_command_line_argument()}") {
  34. service_config.setProperty(prompt.name, av)
  35. }
  36. else
  37. opts.on("--#{prompt.get_command_line_argument()} String") {
  38. |val|
  39. service_config.setProperty(prompt.name, val)
  40. }
  41. end
  42. if Configurator.instance.advanced_mode?()
  43. case prompt.class().name()
  44. when "ReplicationServiceDatasource"
  45. each_datasource_prompt{
  46. |dsp|
  47. if (av = dsp.get_command_line_argument_value()) != nil
  48. opts.on("--#{dsp.get_command_line_argument()}") {
  49. datasource_options.setProperty([DATASOURCES, "ds", dsp.name], av)
  50. }
  51. else
  52. opts.on("--#{dsp.get_command_line_argument()} String") {
  53. |val|
  54. datasource_options.setProperty([DATASOURCES, "ds", dsp.name], val)
  55. }
  56. end
  57. }
  58. when "ReplicationServiceMasterDatasource"
  59. each_datasource_prompt{
  60. |dsp|
  61. if (av = dsp.get_command_line_argument_value()) != nil
  62. opts.on("--master-#{dsp.get_command_line_argument()}") {
  63. datasource_options.setProperty([DATASOURCES, "master", dsp.name], av)
  64. }
  65. else
  66. opts.on("--master-#{dsp.get_command_line_argument()} String") {
  67. |val|
  68. datasource_options.setProperty([DATASOURCES, "master", dsp.name], val)
  69. }
  70. end
  71. }
  72. opts.on("--disable-relay-logs") {
  73. datasource_options.setProperty([DATASOURCES, "master", REPL_DISABLE_RELAY_LOGS], "true")
  74. datasource_options.setProperty([DATASOURCES, "ds", REPL_DISABLE_RELAY_LOGS], "true")
  75. }
  76. end
  77. end
  78. }
  79. opts.on("--master-host String") {
  80. |val|
  81. service_config.setProperty(REPL_MASTERHOST, val)
  82. warning("--master-host is deprecated, use --master-thl-host instead.")
  83. }
  84. begin
  85. remainder = Configurator.instance.run_option_parser(opts, arguments, false, "Invalid option for configure-service")
  86. unless @config.getNestedProperty([DEPLOYMENT_TYPE])
  87. error("You must specify -C, -D or -U")
  88. end
  89. begin
  90. ds_alias = service_config.getProperty(REPL_DATASOURCE)
  91. if ds_alias && @config.getPropertyOr(DATASOURCES, {}).keys().include?(ds_alias)
  92. if datasource_options.getPropertyOr([DATASOURCES, "ds"], {}).size > 0
  93. raise "The '#{ds_alias}' datasource already exists, but you provided configuration settings for it."
  94. else
  95. # The datasource is properly configured
  96. end
  97. else
  98. if datasource_options.getPropertyOr([DATASOURCES, "ds"], {}).size > 0
  99. unless ds_alias
  100. ds_alias = datasource_options.getProperty([DATASOURCES, "ds", REPL_DBHOST]).gsub(".", "_") + "_" + datasource_options.getProperty([DATASOURCES, "ds", REPL_DBPORT])
  101. end
  102. if @config.getPropertyOr(DATASOURCES, {}).keys().include?(ds_alias)
  103. raise "The '#{ds_alias}' datasource already exists, but you provided configuration settings for it."
  104. end
  105. service_config.setProperty(REPL_DATASOURCE, ds_alias)
  106. @config.setProperty([DATASOURCES, ds_alias], datasource_options.getProperty([DATASOURCES, "ds"]))
  107. elsif @config.getNestedProperty([DEPLOYMENT_TYPE]) == SERVICE_CREATE
  108. raise "You must specify a datasource alias or datasource configuration information"
  109. end
  110. end
  111. rescue => e
  112. error(e.message)
  113. end
  114. begin
  115. master_alias = service_config.getProperty(REPL_MASTER_DATASOURCE)
  116. if master_alias && @config.getPropertyOr(DATASOURCES, {}).keys().include?(master_alias)
  117. if datasource_options.getPropertyOr([DATASOURCES, "master"], {}).size > 0
  118. raise "The '#{master_alias}' master datasource already exists, but you provided configuration settings for it."
  119. else
  120. # The master datasource is properly configured
  121. end
  122. else
  123. if service_config.getProperty(REPL_ROLE) == REPL_ROLE_DI && datasource_options.getPropertyOr([DATASOURCES, "master"], {}).size > 0
  124. unless master_alias
  125. master_alias = datasource_options.getProperty([DATASOURCES, "master", REPL_DBHOST]).gsub(".", "_") + "_" + datasource_options.getProperty([DATASOURCES, "master", REPL_DBPORT])
  126. end
  127. if @config.getPropertyOr(DATASOURCES, {}).keys().include?(master_alias)
  128. raise "The '#{master_alias}' master datasource already exists, but you provided configuration settings for it."
  129. end
  130. service_config.setProperty(REPL_MASTER_DATASOURCE, master_alias)
  131. @config.setProperty([DATASOURCES, master_alias], datasource_options.getProperty([DATASOURCES, "master"]))
  132. end
  133. end
  134. rescue => e
  135. error(e.message)
  136. end
  137. case remainder.size()
  138. when 0
  139. raise "No service_name specified"
  140. when 1
  141. @deploy_service_key = false
  142. @config.getPropertyOr(REPL_SERVICES, {}).each_key{
  143. |s_key|
  144. if @config.getProperty([REPL_SERVICES, s_key, DEPLOYMENT_SERVICE]) == remainder[0]
  145. @deploy_service_key = s_key
  146. end
  147. }
  148. case @config.getProperty(DEPLOYMENT_TYPE)
  149. when SERVICE_CREATE
  150. if @deploy_service_key != false
  151. raise "A service named '#{remainder[0]}' already exists"
  152. else
  153. @deploy_service_key = remainder[0]
  154. service_config.setProperty(DEPLOYMENT_SERVICE, @deploy_service_key)
  155. @config.setProperty([REPL_SERVICES, remainder[0]], service_config.props)
  156. end
  157. @config.setProperty([REPL_SERVICES, remainder[0], FIXED_PROPERTY_STRINGS],
  158. Configurator.instance.fixed_properties)
  159. when SERVICE_UPDATE
  160. if @deploy_service_key == false
  161. raise "Unable to find an existing service config for '#{remainder[0]}'"
  162. else
  163. service_config.props.each{
  164. |sc_key, sc_val|
  165. @config.setProperty([REPL_SERVICES, @deploy_service_key, sc_key], sc_val)
  166. }
  167. @config.setProperty([REPL_SERVICES, @deploy_service_key, FIXED_PROPERTY_STRINGS],
  168. @config.getPropertyOr([REPL_SERVICES, @deploy_service_key, FIXED_PROPERTY_STRINGS], []) +
  169. Configurator.instance.fixed_properties)
  170. end
  171. when SERVICE_DELETE
  172. if @deploy_service_key == false
  173. raise "Unable to find an existing service config for '#{remainder[0]}'"
  174. end
  175. end
  176. else
  177. raise "Multiple service names specified: #{remainder.join(', ')}"
  178. end
  179. rescue => e
  180. error("Argument parsing failed: #{e.to_s()}")
  181. end
  182. if Configurator.instance.display_preview?()
  183. if @deploy_service_key == nil
  184. @deploy_service_key = 'service-name'
  185. end
  186. service_config.props.each{
  187. |sc_key, sc_val|
  188. @config.setProperty([REPL_SERVICES, @deploy_service_key, sc_key], sc_val)
  189. }
  190. end
  191. @config.setProperty(DEPLOYMENT_SERVICE, @deploy_service_key)
  192. is_valid?()
  193. end
  194. def output_usage
  195. if Configurator.instance.display_preview?
  196. svc = @deploy_service_key
  197. applier = @config.getProperty([REPL_SERVICES, svc, REPL_DATASOURCE])
  198. extractor = @config.getProperty([REPL_SERVICES, svc, REPL_MASTER_DATASOURCE])
  199. end
  200. puts "Usage: configure-service [general-options] {-C|-D|-U} [target-options] [service-options] service-name"
  201. output_general_usage()
  202. Configurator.instance.write_divider()
  203. puts "Target options:"
  204. output_usage_line("--host", "Host to connect to configure the service", @target_host)
  205. output_usage_line("--user", "User to connect to the host as", @target_user)
  206. output_usage_line("--release-directory", "The release directory that holds the Tungsten runtime files", @target_home_directory)
  207. Configurator.instance.write_divider()
  208. puts "Service options:"
  209. output_usage_line("-C", "Create a replication service")
  210. output_usage_line("-D", "Delete a replication service")
  211. output_usage_line("-U", "Update a replication service")
  212. each_service_prompt{
  213. |prompt|
  214. if Configurator.instance.display_preview?
  215. prompt.set_member(svc)
  216. end
  217. prompt.output_usage()
  218. if Configurator.instance.advanced_mode?()
  219. case prompt.class().name()
  220. when "ReplicationServiceDatasource"
  221. output_usage_line("--disable-relay-logs", "Disable the use of relay-logs?")
  222. each_datasource_prompt{
  223. |prompt|
  224. if Configurator.instance.display_preview?
  225. prompt.set_member(applier)
  226. end
  227. prompt.output_usage()
  228. }
  229. when "ReplicationServiceMasterDatasource"
  230. each_datasource_prompt{
  231. |prompt|
  232. if Configurator.instance.display_preview?
  233. prompt.set_member(extractor)
  234. end
  235. output_usage_line("--master-#{prompt.get_command_line_argument()}", prompt.get_prompt(), prompt.get_value(true, true), nil, prompt.get_prompt_description())
  236. }
  237. end
  238. end
  239. }
  240. end
  241. def get_prompts
  242. [
  243. Datasources.new(),
  244. ReplicationServices.new(),
  245. DeploymentServicePrompt.new()
  246. ]
  247. end
  248. def get_non_interactive_prompts
  249. cluster_prompts = ConfigurePackageCluster.new(@config).get_prompts()
  250. cluster_prompts.delete_if{ |prompt|
  251. (prompt.is_a?(ReplicationServices) || prompt.is_a?(Datasources))
  252. }
  253. cluster_prompts
  254. end
  255. def get_validation_checks
  256. [
  257. ReplicationServiceChecks.new()
  258. ]
  259. end
  260. def allow_interactive?
  261. false
  262. end
  263. def allow_batch?
  264. false
  265. end
  266. end
  267. module ConfigureServicePrompt
  268. def enabled_for_command_line?
  269. if Configurator.instance.package.is_a?(ConfigureServicePackage)
  270. super() && true
  271. else
  272. false
  273. end
  274. end
  275. end