dtk-client 0.5.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +15 -0
  2. data/Gemfile +5 -0
  3. data/Gemfile_dev +12 -0
  4. data/README.md +78 -0
  5. data/bin/dtk +54 -0
  6. data/bin/dtk-shell +15 -0
  7. data/dtk-client.gemspec +49 -0
  8. data/lib/auxiliary.rb +13 -0
  9. data/lib/bundler_monkey_patch.rb +9 -0
  10. data/lib/client.rb +48 -0
  11. data/lib/command_helper.rb +16 -0
  12. data/lib/command_helpers/git_repo.rb +391 -0
  13. data/lib/command_helpers/jenkins_client/config_xml.rb +271 -0
  14. data/lib/command_helpers/jenkins_client.rb +91 -0
  15. data/lib/command_helpers/service_importer.rb +99 -0
  16. data/lib/command_helpers/service_link.rb +18 -0
  17. data/lib/command_helpers/ssh_processing.rb +43 -0
  18. data/lib/commands/common/thor/assembly_workspace.rb +1089 -0
  19. data/lib/commands/common/thor/clone.rb +39 -0
  20. data/lib/commands/common/thor/common.rb +34 -0
  21. data/lib/commands/common/thor/edit.rb +168 -0
  22. data/lib/commands/common/thor/list_diffs.rb +84 -0
  23. data/lib/commands/common/thor/pull_clone_changes.rb +11 -0
  24. data/lib/commands/common/thor/pull_from_remote.rb +99 -0
  25. data/lib/commands/common/thor/purge_clone.rb +26 -0
  26. data/lib/commands/common/thor/push_clone_changes.rb +45 -0
  27. data/lib/commands/common/thor/push_to_remote.rb +45 -0
  28. data/lib/commands/common/thor/reparse.rb +36 -0
  29. data/lib/commands/common/thor/set_required_params.rb +29 -0
  30. data/lib/commands/common/thor/task_status.rb +81 -0
  31. data/lib/commands/thor/account.rb +213 -0
  32. data/lib/commands/thor/assembly.rb +329 -0
  33. data/lib/commands/thor/attribute.rb +62 -0
  34. data/lib/commands/thor/component.rb +52 -0
  35. data/lib/commands/thor/component_module.rb +829 -0
  36. data/lib/commands/thor/component_template.rb +153 -0
  37. data/lib/commands/thor/dependency.rb +18 -0
  38. data/lib/commands/thor/developer.rb +105 -0
  39. data/lib/commands/thor/dtk.rb +117 -0
  40. data/lib/commands/thor/library.rb +107 -0
  41. data/lib/commands/thor/node.rb +411 -0
  42. data/lib/commands/thor/node_group.rb +211 -0
  43. data/lib/commands/thor/node_template.rb +88 -0
  44. data/lib/commands/thor/project.rb +17 -0
  45. data/lib/commands/thor/provider.rb +155 -0
  46. data/lib/commands/thor/repo.rb +35 -0
  47. data/lib/commands/thor/service.rb +656 -0
  48. data/lib/commands/thor/service_module.rb +806 -0
  49. data/lib/commands/thor/state_change.rb +10 -0
  50. data/lib/commands/thor/target.rb +94 -0
  51. data/lib/commands/thor/task.rb +100 -0
  52. data/lib/commands/thor/utils.rb +4 -0
  53. data/lib/commands/thor/workspace.rb +437 -0
  54. data/lib/commands.rb +40 -0
  55. data/lib/config/cacert.pem +3785 -0
  56. data/lib/config/client.conf.header +18 -0
  57. data/lib/config/configuration.rb +82 -0
  58. data/lib/config/default.conf +14 -0
  59. data/lib/config/disk_cacher.rb +60 -0
  60. data/lib/configurator.rb +92 -0
  61. data/lib/context_router.rb +23 -0
  62. data/lib/core.rb +460 -0
  63. data/lib/domain/git_adapter.rb +221 -0
  64. data/lib/domain/response.rb +234 -0
  65. data/lib/dtk-client/version.rb +3 -0
  66. data/lib/dtk_constants.rb +23 -0
  67. data/lib/dtk_logger.rb +96 -0
  68. data/lib/error.rb +74 -0
  69. data/lib/git-logs/git.log +0 -0
  70. data/lib/parser/adapters/option_parser.rb +53 -0
  71. data/lib/parser/adapters/thor/common_option_defs.rb +12 -0
  72. data/lib/parser/adapters/thor.rb +509 -0
  73. data/lib/require_first.rb +87 -0
  74. data/lib/search_hash.rb +27 -0
  75. data/lib/shell/context.rb +975 -0
  76. data/lib/shell/context_aux.rb +29 -0
  77. data/lib/shell/domain.rb +447 -0
  78. data/lib/shell/header_shell.rb +27 -0
  79. data/lib/shell/help_monkey_patch.rb +221 -0
  80. data/lib/shell/interactive_wizard.rb +233 -0
  81. data/lib/shell/parse_monkey_patch.rb +22 -0
  82. data/lib/shell/status_monitor.rb +105 -0
  83. data/lib/shell.rb +219 -0
  84. data/lib/util/console.rb +143 -0
  85. data/lib/util/dtk_puppet.rb +46 -0
  86. data/lib/util/os_util.rb +265 -0
  87. data/lib/view_processor/augmented_simple_list.rb +27 -0
  88. data/lib/view_processor/hash_pretty_print.rb +106 -0
  89. data/lib/view_processor/simple_list.rb +139 -0
  90. data/lib/view_processor/table_print.rb +277 -0
  91. data/lib/view_processor.rb +112 -0
  92. data/puppet/manifests/init.pp +72 -0
  93. data/puppet/manifests/params.pp +16 -0
  94. data/puppet/r8meta.puppet.yml +18 -0
  95. data/puppet/templates/bash_profile.erb +2 -0
  96. data/puppet/templates/client.conf.erb +1 -0
  97. data/puppet/templates/dtkclient.erb +2 -0
  98. data/spec/assembly_spec.rb +50 -0
  99. data/spec/assembly_template_spec.rb +51 -0
  100. data/spec/component_template_spec.rb +40 -0
  101. data/spec/dependency_spec.rb +6 -0
  102. data/spec/dtk_shell_spec.rb +13 -0
  103. data/spec/dtk_spec.rb +33 -0
  104. data/spec/lib/spec_helper.rb +10 -0
  105. data/spec/lib/spec_thor.rb +105 -0
  106. data/spec/module_spec.rb +35 -0
  107. data/spec/node_spec.rb +43 -0
  108. data/spec/node_template_spec.rb +25 -0
  109. data/spec/project_spec.rb +6 -0
  110. data/spec/repo_spec.rb +7 -0
  111. data/spec/response_spec.rb +52 -0
  112. data/spec/service_spec.rb +41 -0
  113. data/spec/state_change_spec.rb +7 -0
  114. data/spec/table_print_spec.rb +48 -0
  115. data/spec/target_spec.rb +57 -0
  116. data/spec/task_spec.rb +28 -0
  117. data/views/assembly/augmented_simple_list.rb +12 -0
  118. data/views/assembly_template/augmented_simple_list.rb +12 -0
  119. data/views/list_task/augmented_simple_list.rb +12 -0
  120. metadata +351 -0
@@ -0,0 +1,806 @@
1
+ #TODO: putting in version as hidden coption that can be enabled when code ready
2
+ #TODO: may be consistent on whether service module id or service module name used as params
3
+ dtk_require_from_base('command_helpers/ssh_processing')
4
+ dtk_require_from_base('command_helpers/service_importer')
5
+ dtk_require_common_commands('thor/clone')
6
+ dtk_require_common_commands('thor/push_to_remote')
7
+ dtk_require_common_commands('thor/pull_from_remote')
8
+ dtk_require_common_commands('thor/push_clone_changes')
9
+ dtk_require_common_commands('thor/edit')
10
+ dtk_require_common_commands('thor/reparse')
11
+ dtk_require_from_base("dtk_logger")
12
+ dtk_require_from_base("util/os_util")
13
+ dtk_require_from_base("commands/thor/assembly")
14
+ dtk_require_common_commands('thor/task_status')
15
+ dtk_require_common_commands('thor/set_required_params')
16
+ dtk_require_common_commands('thor/purge_clone')
17
+
18
+ module DTK::Client
19
+ class ServiceModule < CommandBaseThor
20
+
21
+ no_tasks do
22
+ include CloneMixin
23
+ include PushToRemoteMixin
24
+ include PullFromRemoteMixin
25
+ include PushCloneChangesMixin
26
+ include EditMixin
27
+ include ReparseMixin
28
+ include ServiceImporter
29
+ include PurgeCloneMixin
30
+
31
+ def get_service_module_name(service_module_id)
32
+ get_name_from_id_helper(service_module_id)
33
+ end
34
+ end
35
+
36
+ def self.valid_children()
37
+ [:"assembly"]
38
+ end
39
+
40
+ def self.all_children()
41
+ [:"assembly"]
42
+ end
43
+
44
+ def self.valid_child?(name_of_sub_context)
45
+ return ServiceModule.valid_children().include?(name_of_sub_context.to_sym)
46
+ end
47
+
48
+ def self.pretty_print_cols()
49
+ PPColumns.get(:service_module)
50
+ end
51
+
52
+ def self.whoami()
53
+ return :service_module, "service_module/list", nil
54
+ end
55
+
56
+ def self.override_allowed_methods()
57
+ return DTK::Shell::OverrideTasks.new({
58
+ :command_only => {
59
+ :self => [
60
+ ["list"," list [--remote] [--diff]","# List service modules (local/remote). Use --diff to compare loaded and remote modules."]
61
+ ],
62
+ :"assembly" => [
63
+ ["list","list","# List assemblies for given service module."]
64
+ ]
65
+ },
66
+ :identifier_only => {
67
+ :self => [
68
+ ["list-assemblies","list-assemblies","# List assemblies associated with service module."],
69
+ ["list-modules","list-modules","# List modules associated with service module."]
70
+ ],
71
+ :"assembly" => [
72
+ ["info","info","# Info for given assembly in current service module."],
73
+ ["stage", "stage [INSTANCE-NAME] [-t TARGET-NAME/ID]", "# Stage assembly in target."],
74
+ # ["deploy","deploy [-v VERSION] [INSTANCE-NAME] [-t TARGET-NAME/ID] [-m COMMIT-MSG]", "# Stage and deploy assembly in target."],
75
+ ["deploy","deploy [INSTANCE-NAME] [-t TARGET-NAME/ID] [-m COMMIT-MSG]", "# Stage and deploy assembly in target."],
76
+ ["list-nodes","list-nodes", "# List all nodes for given assembly."],
77
+ ["list-components","list-components", "# List all components for given assembly."]
78
+ ]
79
+ }
80
+
81
+ })
82
+ end
83
+
84
+ ##MERGE-QUESTION: need to add options of what info is about
85
+ desc "SERVICE-MODULE-NAME/ID info", "Provides information about specified service module"
86
+ def info(context_params)
87
+ #TODO Aldin
88
+ if context_params.is_there_identifier?(:assembly)
89
+ response = DTK::Client::ContextRouter.routeTask("assembly", "info", context_params, @conn)
90
+ else
91
+ service_module_id = context_params.retrieve_arguments([:service_module_id!],method_argument_names)
92
+
93
+ post_body = {
94
+ :service_module_id => service_module_id
95
+ }
96
+
97
+ response = post rest_url('service_module/info'), post_body
98
+ response.render_custom_info("module")
99
+ end
100
+ end
101
+
102
+ desc "SERVICE-MODULE-NAME/ID list-assemblies","List assemblies associated with service module."
103
+ method_option :remote, :type => :boolean, :default => false
104
+ def list_assemblies(context_params)
105
+ context_params.method_arguments = ["assembly"]
106
+ list(context_params)
107
+ end
108
+
109
+ desc "SERVICE-MODULE-NAME/ID list-component-modules","List component modules associated with service module."
110
+ method_option :remote, :type => :boolean, :default => false
111
+ def list_comoponent_modules(context_params)
112
+ context_params.method_arguments = ["modules"]
113
+ list(context_params)
114
+ end
115
+
116
+ desc "list [--remote] [--diff]","List service modules (local/remote). Use --diff to compare loaded and remote modules."
117
+ method_option :remote, :type => :boolean, :default => false
118
+ method_option :diff, :type => :boolean, :default => false
119
+ def list(context_params)
120
+ service_module_id, about, service_module_name = context_params.retrieve_arguments([:service_module_id, :option_1, :option_2],method_argument_names)
121
+ datatype = nil
122
+
123
+ if context_params.is_there_command?(:"assembly")
124
+ about = "assembly"
125
+ end
126
+
127
+ if service_module_id.nil? && !service_module_name.nil?
128
+ service_module_id = service_module_name
129
+ end
130
+
131
+ # If user is on service level, list task can't have about value set
132
+ if (context_params.last_entity_name == :"service-module") and about.nil?
133
+ action = options.remote? ? "list_remote" : "list"
134
+ # post_body = (options.remote? ? { :rsa_pub_key => SshProcessing.rsa_pub_key_content() } : {:detail_to_include => ["remotes","versions"]})
135
+ post_body = (options.remote? ? { :rsa_pub_key => SshProcessing.rsa_pub_key_content() } : {:detail_to_include => ["remotes"]})
136
+ post_body[:diff] = options.diff? ? options.diff : {}
137
+
138
+ response = post rest_url("service_module/#{action}"), post_body
139
+ # If user is on service identifier level, list task can't have '--remote' option.
140
+ else
141
+ # TODO: this is temp; will shortly support this
142
+ raise DTK::Client::DtkValidationError.new("Not supported '--remote' option when listing service module assemblies, component templates or modules", true) if options.remote?
143
+ raise DTK::Client::DtkValidationError.new("Not supported type '#{about}' for list for current context level. Possible type options: 'assembly'", true) unless(about == "assembly" || about == "modules")
144
+
145
+ if about
146
+ case about
147
+ when "assembly"
148
+ data_type = :assembly_template
149
+ action = "list_assemblies"
150
+ when "modules"
151
+ #data_type is set by server
152
+ data_type = options.remote? ? :component_remote : :component
153
+ action = "list_component_modules"
154
+ else
155
+ raise_validation_error_method_usage('list')
156
+ end
157
+ end
158
+ response = post rest_url("service_module/#{action}"), { :service_module_id => service_module_id }
159
+ end
160
+ return response unless response.ok?
161
+ response.render_table(data_type) unless response.nil?
162
+
163
+ response
164
+ end
165
+
166
+ desc "SERVICE-MODULE-NAME/ID list-instances","List all instances associated with this service module."
167
+ def list_instances(context_params)
168
+ service_module_id = context_params.retrieve_arguments([:service_module_id!],method_argument_names)
169
+ post_body = {
170
+ :service_module_id => service_module_id,
171
+ }
172
+ response = post rest_url("service_module/list_instances"), post_body
173
+
174
+ response.render_table(:assembly_template)
175
+ end
176
+
177
+ # desc "SERVICE-MODULE-NAME/ID list-versions","List all versions associated with this service module."
178
+ # def list_versions(context_params)
179
+ # service_module_id = context_params.retrieve_arguments([:service_module_id!],method_argument_names)
180
+ # post_body = {
181
+ # :service_module_id => service_module_id,
182
+ # :detail_to_include => ["remotes"],
183
+ # :rsa_pub_key => SshProcessing.rsa_pub_key_content()
184
+ # }
185
+ # response = post rest_url("service_module/versions"), post_body
186
+
187
+ # response.render_table(:module_version)
188
+ # end
189
+
190
+ # version_method_option
191
+ desc "install [NAMESPACE/]REMOTE-SERVICE-MODULE-NAME [-y] [-i]", "Install remote service module into local environment. -y will automatically clone component modules. -i will ignore component import error."
192
+ method_option :force, :aliases => '-y', :type => :boolean, :default => false
193
+ method_option :ignore, :aliases => '-i', :type => :boolean, :default => false
194
+ def install(context_params)
195
+ create_missing_clone_dirs()
196
+ check_direct_access(::DTK::Client::Configurator.check_direct_access)
197
+ remote_module_name = context_params.retrieve_arguments([:option_1!],method_argument_names)
198
+ ignore_component_error = options.ignore?
199
+
200
+ remote_namespace, local_module_name = get_namespace_and_name(remote_module_name)
201
+
202
+ version = options["version"]
203
+ if clone_dir = Helper(:git_repo).local_clone_dir_exists?(:service_module,local_module_name)
204
+ raise DtkValidationError,"Module's directory (#{clone_dir}) exists on client. To import this needs to be renamed or removed."
205
+ end
206
+
207
+ post_body = {
208
+ :remote_module_name => remote_module_name,
209
+ :local_module_name => local_module_name,
210
+ :rsa_pub_key => SshProcessing.rsa_pub_key_content()
211
+ }
212
+
213
+ response = post rest_url("service_module/import"), post_body
214
+
215
+ # case when we need to import additional components
216
+ if (response.ok? && (missing_components = response.data(:missing_module_components)))
217
+ opts = {:do_not_raise=>true}
218
+ module_opts = ignore_component_error ? opts.merge(:ignore_component_error => true) : opts.merge(:additional_message=>true)
219
+ trigger_module_component_import(missing_components,module_opts)
220
+ puts "Resuming DTK network import for service module '#{remote_module_name}' ..."
221
+ # repeat import call for service
222
+ post_body.merge!(opts)
223
+ response = post rest_url("service_module/import"), post_body
224
+ end
225
+
226
+ return response unless response.ok?
227
+ @@invalidate_map << :service_module
228
+
229
+ if error = response.data(:dsl_parsed_info)
230
+ dsl_parsed_message = ServiceImporter.error_message(remote_module_name, error)
231
+ DTK::Client::OsUtil.print(dsl_parsed_message, :red)
232
+ end
233
+
234
+ service_module_id, module_name, namespace, repo_url, branch = response.data(:module_id, :module_name, :namespace, :repo_url, :workspace_branch)
235
+ response = Helper(:git_repo).create_clone_with_branch(:service_module,module_name,repo_url,branch,version)
236
+ resolve_missing_components(service_module_id, module_name, namespace, options.force?)
237
+
238
+ response
239
+ end
240
+
241
+ =begin
242
+ # desc "SERVICE-MODULE-NAME/ID validate-model [-v VERSION]", "Check the DSL Model for Errors"
243
+ # version_method_option
244
+ desc "SERVICE-MODULE-NAME/ID validate-model", "Check the DSL Model for Errors"
245
+ def validate_model(context_params)
246
+ service_module_id, service_module_name = context_params.retrieve_arguments([:service_module_id!, :service_module_name],method_argument_names)
247
+ version = options["version"]
248
+
249
+ if service_module_name.to_s =~ /^[0-9]+$/
250
+ service_module_id = service_module_name
251
+ service_module_name = get_service_module_name(service_module_id)
252
+ end
253
+
254
+ modules_path = OsUtil.service_clone_location()
255
+ module_location = "#{modules_path}/#{service_module_name}#{version && "-#{version}"}"
256
+
257
+ raise DTK::Client::DtkValidationError, "Unable to parse service '#{service_module_name}#{version && "-#{version}"}' that doesn't exist on your local machine!" unless File.directory?(module_location)
258
+
259
+ reparse_aux(module_location)
260
+ end
261
+ =end
262
+
263
+ # desc "SERVICE-MODULE-NAME/ID import-version VERSION", "Import a specific version from a linked service module"
264
+ # def import_version(context_params)
265
+ # service_module_id,version = context_params.retrieve_arguments([:service_module_id!,:option_1!],method_argument_names)
266
+ # post_body = {
267
+ # :service_module_id => service_module_id,
268
+ # :version => version
269
+ # }
270
+ # response = post rest_url("service_module/import_version"), post_body
271
+ # @@invalidate_map << :module_service
272
+
273
+ # return response unless response.ok?
274
+ # module_name,repo_url,branch,version = response.data(:module_name,:repo_url,:workspace_branch,:version)
275
+
276
+ # if error = response.data(:dsl_parsed_info)
277
+ # dsl_parsed_message = ServiceImporter.error_message("#{module_name}-#{version}", error)
278
+ # DTK::Client::OsUtil.print(dsl_parsed_message, :red)
279
+ # end
280
+
281
+ # #TODO: need to check if local clone directory exists
282
+ # Helper(:git_repo).create_clone_with_branch(:service_module,module_name,repo_url,branch,version)
283
+ # end
284
+
285
+ # desc "SERVICE-MODULE-NAME/ID create-on-dtkn [[NAMESPACE/]REMOTE-MODULE-NAME]","Export service module to remote repository"
286
+ # def create_on_dtkn(context_params)
287
+ # service_module_id, input_remote_name = context_params.retrieve_arguments([:service_module_id!, :option_1],method_argument_names)
288
+
289
+ # post_body = {
290
+ # :service_module_id => service_module_id,
291
+ # :remote_component_name => input_remote_name,
292
+ # :rsa_pub_key => SshProcessing.rsa_pub_key_content()
293
+ # }
294
+
295
+ # post rest_url("service_module/export"), post_body
296
+ # end
297
+
298
+ desc "SERVICE-MODULE-NAME/ID publish [[NAMESPACE/]REMOTE-SERVICE-MODULE-NAME]","Publish service module to remote repository"
299
+ def publish(context_params)
300
+ service_module_id, input_remote_name = context_params.retrieve_arguments([:service_module_id!, :option_1],method_argument_names)
301
+
302
+ post_body = {
303
+ :service_module_id => service_module_id,
304
+ :remote_component_name => input_remote_name,
305
+ :rsa_pub_key => SshProcessing.rsa_pub_key_content()
306
+ }
307
+
308
+ post rest_url("service_module/export"), post_body
309
+ end
310
+
311
+ # desc "SERVICE-MODULE-NAME/ID push-to-dtkn [-n NAMESPACE] [-v VERSION]", "Push local copy of service module to remote repository."
312
+ # version_method_option
313
+ # desc "SERVICE-MODULE-NAME/ID push-to-dtkn [-n NAMESPACE]", "Push local copy of service module to remote repository."
314
+ # method_option "namespace",:aliases => "-n",
315
+ # :type => :string,
316
+ # :banner => "NAMESPACE",
317
+ # :desc => "Remote namespace"
318
+ # def push_to_dtkn(context_params)
319
+ # service_module_id, service_module_name = context_params.retrieve_arguments([:service_module_id!, :service_module_name],method_argument_names)
320
+ # version = options["version"]
321
+
322
+ # if service_module_name.to_s =~ /^[0-9]+$/
323
+ # service_id = service_module_name
324
+ # service_module_name = get_service_module_name(service_id)
325
+ # end
326
+
327
+ # modules_path = OsUtil.service_clone_location()
328
+ # module_location = "#{modules_path}/#{service_module_name}#{version && "-#{version}"}"
329
+
330
+ # unless File.directory?(module_location)
331
+ # if Console.confirmation_prompt("Unable to push to remote because module '#{service_module_name}#{version && "-#{version}"}' has not been cloned. Would you like to clone module now"+'?')
332
+ # response = clone_aux(:service_module,service_module_id,version,false)
333
+
334
+ # if(response.nil? || response.ok?)
335
+ # reparse_aux(module_location)
336
+ # push_to_remote_aux(:service_module, service_module_id, service_module_name, options["namespace"], version) if Console.confirmation_prompt("Would you like to push changes to remote"+'?')
337
+ # end
338
+
339
+ # return response
340
+ # else
341
+ # # user choose not to clone needed module
342
+ # return
343
+ # end
344
+ # end
345
+
346
+ # reparse_aux(module_location)
347
+ # push_to_remote_aux(:service_module, service_module_id, service_module_name, options["namespace"], options["version"])
348
+ # end
349
+
350
+ # desc "SERVICE-MODULE-NAME/ID pull-from-dtkn [-v VERSION]", "Update local service module from remote repository."
351
+ # version_method_option
352
+ # desc "SERVICE-MODULE-NAME/ID pull-from-dtkn", "Update local service module from remote repository."
353
+ # def pull_from_dtkn(context_params)
354
+ # service_module_id, service_module_name = context_params.retrieve_arguments([:service_module_id!,:service_module_name],method_argument_names)
355
+ # version = options["version"]
356
+
357
+ # response = pull_from_remote_aux(:service_module,service_module_id,version)
358
+ # return response unless response.ok?
359
+
360
+ # if service_module_name.to_s =~ /^[0-9]+$/
361
+ # service_module_id = service_module_name
362
+ # service_module_name = get_service_module_name(service_module_id)
363
+ # end
364
+
365
+ # modules_path = OsUtil.service_clone_location()
366
+ # module_location = "#{modules_path}/#{service_module_name}#{version && "-#{version}"}"
367
+
368
+ # # server repo needs to be sync with local clone, so we will use push-clone-changes when pull changes from remote to local
369
+ # # to automatically sync local with server repo
370
+ # push_clone_changes_aux(:service_module,service_module_id,version,nil,true) if File.directory?(module_location)
371
+ # Response::Ok.new()
372
+ # end
373
+
374
+ # desc "SERVICE-MODULE-NAME/ID pull dtkn", "Update local service module from remote repository."
375
+ desc "SERVICE-MODULE-NAME/ID pull-dtkn", "Update local service module from remote repository."
376
+ def pull_dtkn(context_params)
377
+ # service_module_id, service_module_name, catalog = context_params.retrieve_arguments([:service_module_id!,:service_module_name, :option_1],method_argument_names)
378
+ service_module_id, service_module_name = context_params.retrieve_arguments([:service_module_id!,:service_module_name],method_argument_names)
379
+ catalog = 'dtkn'
380
+ version = options["version"]
381
+
382
+ raise DtkValidationError, "You have to provide valid catalog to pull changes from! Valid catalogs: #{PullCatalogs}" unless catalog
383
+
384
+ if catalog.to_s.eql?("dtkn")
385
+ response = pull_from_remote_aux(:service_module,service_module_id,version)
386
+ return response unless response.ok?
387
+
388
+ if service_module_name.to_s =~ /^[0-9]+$/
389
+ service_module_id = service_module_name
390
+ service_module_name = get_service_module_name(service_module_id)
391
+ end
392
+
393
+ modules_path = OsUtil.service_clone_location()
394
+ module_location = "#{modules_path}/#{service_module_name}#{version && "-#{version}"}"
395
+
396
+ # server repo needs to be sync with local clone, so we will use push-clone-changes when pull changes from remote to local
397
+ # to automatically sync local with server repo
398
+ push_clone_changes_aux(:service_module,service_module_id,version,nil,true) if File.directory?(module_location)
399
+ Response::Ok.new()
400
+ #elsif catalog.to_s.eql?("origin")
401
+ #needs to be implemented
402
+ else
403
+ raise DtkValidationError, "You have to provide valid catalog to pull changes from! Valid catalogs: #{PullCatalogs}"
404
+ end
405
+ end
406
+ PullCatalogs = ["dtkn"]
407
+
408
+ ##
409
+ #
410
+ # internal_trigger: this flag means that other method (internal) has trigger this.
411
+ # This will change behaviour of method
412
+ #
413
+ # desc "SERVICE-MODULE-NAME/ID clone [-v VERSION] [-n]", "Locally clone the service module files. Use -n to skip edit prompt"
414
+ # version_method_option
415
+ desc "SERVICE-MODULE-NAME/ID clone [-n]", "Locally clone the service module files. Use -n to skip edit prompt"
416
+ method_option :skip_edit, :aliases => '-n', :type => :boolean, :default => false
417
+ def clone(context_params, internal_trigger=false)
418
+ service_module_id, service_module_name = context_params.retrieve_arguments([:service_module_id!, :service_module_name],method_argument_names)
419
+ internal_trigger = true if options.skip_edit?
420
+ version = options["version"]
421
+
422
+ # if this is not name it will not work, we need module name
423
+ if service_module_name.to_s =~ /^[0-9]+$/
424
+ service_module_id = service_module_name
425
+ service_module_name = get_service_module_name(service_module_id)
426
+ end
427
+
428
+ modules_path = OsUtil.service_clone_location()
429
+ module_location = "#{modules_path}/#{service_module_name}#{version && "-#{version}"}"
430
+
431
+ raise DTK::Client::DtkValidationError, "Trying to clone a service module '#{service_module_name}#{version && "-#{version}"}' that exists already!" if File.directory?(module_location)
432
+ clone_aux(:service_module,service_module_id,version,internal_trigger)
433
+ end
434
+
435
+ # desc "SERVICE-MODULE-NAME/ID edit [-v VERSION]","Switch to unix editing for given service module."
436
+ # version_method_option
437
+ desc "SERVICE-MODULE-NAME/ID edit","Switch to unix editing for given service module."
438
+ def edit(context_params)
439
+ service_module_id, service_module_name = context_params.retrieve_arguments([:service_module_id!, :service_module_name],method_argument_names)
440
+ version = options["version"]
441
+
442
+ # if this is not name it will not work, we need module name
443
+ if service_module_name.to_s =~ /^[0-9]+$/
444
+ service_module_id = service_module_name
445
+ service_module_name = get_service_module_name(service_module_id)
446
+ end
447
+
448
+ edit_aux(:service_module,service_module_id,service_module_name,version)
449
+ end
450
+
451
+ # desc "SERVICE-MODULE-NAME/ID create-version NEW-VERSION", "Snapshot current state of service module as a new version"
452
+ # def create_version(context_params)
453
+ # service_module_id,version = context_params.retrieve_arguments([:service_module_id!,:option_1!],method_argument_names)
454
+ # post_body = {
455
+ # :service_module_id => service_module_id,
456
+ # :rsa_pub_key => SshProcessing.rsa_pub_key_content()
457
+ # }
458
+ # response = post rest_url("service_module/versions"), post_body
459
+ # return response unless response.ok?
460
+ # versions = (response.data.first && response.data.first['versions'])||Array.new
461
+ # if versions.include?(version)
462
+ # return Response::Error::Usage.new("Version #{version} exists already")
463
+ # end
464
+
465
+ # service_module_name = get_service_module_name(service_module_id)
466
+ # module_location = OsUtil.module_location(:service_module,service_module_name,version)
467
+ # if File.directory?(module_location)
468
+ # raise DtkError, "Target service module directory for version #{version} (#{module_location}) exists already; it must be deleted and this comamnd retried"
469
+ # end
470
+
471
+ # post_body = {
472
+ # :service_module_id => service_module_id,
473
+ # :version => version
474
+ # }
475
+
476
+ # response = post rest_url("service_module/create_new_version"), post_body
477
+ # return response unless response.ok?
478
+
479
+ # internal_trigger = omit_output = true
480
+ # clone_aux(:service_module,service_module_name,version,internal_trigger,omit_output)
481
+ # end
482
+
483
+ # desc "SERVICE-MODULE-NAME/ID set-component-module-version COMPONENT-MODULE-NAME VERSION", "Set the version of the component module to use in the service module's assemblies"
484
+ # def set_component_module_version(context_params)
485
+ # service_module_id,component_module_id,version = context_params.retrieve_arguments([:service_module_id!,:option_1!,:option_2!],method_argument_names)
486
+ # post_body = {
487
+ # :service_module_id => service_module_id,
488
+ # :component_module_id => component_module_id,
489
+ # :version => version
490
+ # }
491
+ # response = post rest_url("service_module/set_component_module_version"), post_body
492
+ # @@invalidate_map << :service_module
493
+ # return response unless response.ok?()
494
+ # module_name,commit_sha,workspace_branch = response.data(:module_name,:commit_sha,:workspace_branch)
495
+ # Helper(:git_repo).synchronize_clone(:service_module,module_name,commit_sha,:local_branch=>workspace_branch)
496
+ # end
497
+
498
+ # TODO: put in two versions, one that creates empty and anotehr taht creates from local dir; use --empty flag
499
+ desc "import SERVICE-MODULE-NAME", "Create new service module from local clone"
500
+ def import(context_params)
501
+ module_name = context_params.retrieve_arguments([:option_1!],method_argument_names)
502
+
503
+ # first check that there is a directory there and it is not already a git repo, and it ha appropriate content
504
+ response = Helper(:git_repo).check_local_dir_exists_with_content(:service_module,module_name)
505
+ return response unless response.ok?
506
+ service_directory = response.data(:module_directory)
507
+
508
+ #check for yaml/json parsing errors before import
509
+ reparse_aux(service_directory)
510
+
511
+ # first call to create empty module
512
+ response = post rest_url("service_module/create"), { :module_name => module_name }
513
+ return response unless response.ok?
514
+ @@invalidate_map << :service_module
515
+
516
+ if error = response.data(:dsl_parsed_info)
517
+ dsl_parsed_message = ServiceImporter.error_message(module_name, error)
518
+ DTK::Client::OsUtil.print(dsl_parsed_message, :red)
519
+ end
520
+
521
+ # initial commit for given service module
522
+ service_module_id, repo_info, module_id = response.data(:service_module_id, :repo_info)
523
+ repo_url,repo_id,module_id,branch = [:repo_url,:repo_id,:module_id,:workspace_branch].map { |k| repo_info[k.to_s] }
524
+ response = Helper(:git_repo).initialize_client_clone_and_push(:service_module, module_name,branch,repo_url,service_directory)
525
+ return response unless response.ok?
526
+ repo_obj,commit_sha = response.data(:repo_obj,:commit_sha)
527
+
528
+ context_params.add_context_to_params(module_name, :"service-module", module_id)
529
+ push(context_params,true)
530
+ end
531
+
532
+
533
+ # desc "SERVICE-MODULE-NAME/ID push [-v VERSION] [-m COMMIT-MSG]", "Push changes from local copy of service module to server"
534
+ # version_method_option
535
+ =begin
536
+ desc "SERVICE-MODULE-NAME/ID push origin|dtkn [-n NAMESPACE] [-m COMMIT-MSG]", "Push changes from local copy of service module to server (origin) or to remote repository (dtkn)."
537
+ method_option "message",:aliases => "-m" ,
538
+ :type => :string,
539
+ :banner => "COMMIT-MSG",
540
+ :desc => "Commit message"
541
+ method_option "namespace",:aliases => "-n",
542
+ :type => :string,
543
+ :banner => "NAMESPACE",
544
+ :desc => "Remote namespace"
545
+ #hidden option for dev
546
+ method_option 'force-parse', :aliases => '-f', :type => :boolean, :default => false
547
+ def push(context_params, internal_trigger=false)
548
+ service_module_id, service_module_name, catalog = context_params.retrieve_arguments([:service_module_id!, :service_module_name, :option_1],method_argument_names)
549
+ version = options["version"]
550
+
551
+ raise DtkValidationError, "You have to provide valid catalog to push changes to! Valid catalogs: #{PushCatalogs}" unless catalog
552
+
553
+ if service_module_name.to_s =~ /^[0-9]+$/
554
+ service_module_id = service_module_name
555
+ service_module_name = get_service_module_name(service_module_id)
556
+ end
557
+
558
+ modules_path = OsUtil.service_clone_location()
559
+ module_location = "#{modules_path}/#{service_module_name}#{version && "-#{version}"}"
560
+ reparse_aux(module_location) unless internal_trigger
561
+
562
+ if catalog.to_s.eql?("origin")
563
+ push_clone_changes_aux(:service_module,service_module_id,version,nil,internal_trigger)
564
+ elsif catalog.to_s.eql?("dtkn")
565
+ unless File.directory?(module_location)
566
+ if Console.confirmation_prompt("Unable to push to remote because module '#{service_module_name}#{version && "-#{version}"}' has not been cloned. Would you like to clone module now"+'?')
567
+ response = clone_aux(:service_module,service_module_id,version,false)
568
+
569
+ if(response.nil? || response.ok?)
570
+ reparse_aux(module_location)
571
+ push_to_remote_aux(:service_module, service_module_id, service_module_name, options["namespace"], version) if Console.confirmation_prompt("Would you like to push changes to remote"+'?')
572
+ end
573
+
574
+ return response
575
+ else
576
+ # user choose not to clone needed module
577
+ return
578
+ end
579
+ end
580
+
581
+ push_to_remote_aux(:service_module, service_module_id, service_module_name, options["namespace"], options["version"])
582
+ else
583
+ raise DtkValidationError, "You have to provide valid catalog to push changes to! Valid catalogs: #{PushCatalogs}"
584
+ end
585
+ end
586
+ PushCatalogs = ["origin", "dtkn"]
587
+ =end
588
+
589
+ desc "SERVICE-MODULE-NAME/ID push [-m COMMIT-MSG]", "Push changes from local copy to server (origin)."
590
+ method_option "message",:aliases => "-m" ,
591
+ :type => :string,
592
+ :banner => "COMMIT-MSG",
593
+ :desc => "Commit message"
594
+ #hidden option for dev
595
+ method_option 'force-parse', :aliases => '-f', :type => :boolean, :default => false
596
+ def push(context_params, internal_trigger=false)
597
+ service_module_id, service_module_name = context_params.retrieve_arguments([:service_module_id!, :service_module_name],method_argument_names)
598
+ version = options["version"]
599
+
600
+ if service_module_name.to_s =~ /^[0-9]+$/
601
+ service_module_id = service_module_name
602
+ service_module_name = get_service_module_name(service_module_id)
603
+ end
604
+
605
+ modules_path = OsUtil.service_clone_location()
606
+ module_location = "#{modules_path}/#{service_module_name}#{version && "-#{version}"}"
607
+ reparse_aux(module_location) unless internal_trigger
608
+ push_clone_changes_aux(:service_module,service_module_id,version,nil,internal_trigger)
609
+ end
610
+
611
+ desc "SERVICE-MODULE-NAME/ID push-dtkn [-n NAMESPACE] [-m COMMIT-MSG]", "Push changes from local copy of service module to remote repository (dtkn)."
612
+ method_option "message",:aliases => "-m" ,
613
+ :type => :string,
614
+ :banner => "COMMIT-MSG",
615
+ :desc => "Commit message"
616
+ method_option "namespace",:aliases => "-n",
617
+ :type => :string,
618
+ :banner => "NAMESPACE",
619
+ :desc => "Remote namespace"
620
+ def push_dtkn(context_params, internal_trigger=false)
621
+ service_module_id, service_module_name = context_params.retrieve_arguments([:service_module_id!, :service_module_name],method_argument_names)
622
+ catalog = 'dtkn'
623
+ version = options["version"]
624
+
625
+ if service_module_name.to_s =~ /^[0-9]+$/
626
+ service_module_id = service_module_name
627
+ service_module_name = get_service_module_name(service_module_id)
628
+ end
629
+
630
+ modules_path = OsUtil.service_clone_location()
631
+ module_location = "#{modules_path}/#{service_module_name}#{version && "-#{version}"}"
632
+ reparse_aux(module_location) unless internal_trigger
633
+ if catalog.to_s.eql?("dtkn")
634
+ unless File.directory?(module_location)
635
+ if Console.confirmation_prompt("Unable to push to remote because module '#{service_module_name}#{version && "-#{version}"}' has not been cloned. Would you like to clone module now"+'?')
636
+ response = clone_aux(:service_module,service_module_id,version,false)
637
+
638
+ if(response.nil? || response.ok?)
639
+ reparse_aux(module_location)
640
+ push_to_remote_aux(:service_module, service_module_id, service_module_name, options["namespace"], version) if Console.confirmation_prompt("Would you like to push changes to remote"+'?')
641
+ end
642
+
643
+ return response
644
+ else
645
+ # user choose not to clone needed module
646
+ return
647
+ end
648
+ end
649
+
650
+ push_to_remote_aux(:service_module, service_module_id, service_module_name, options["namespace"], options["version"])
651
+ else
652
+ raise DtkValidationError, "You have to provide valid catalog to push changes to!"
653
+ end
654
+ end
655
+
656
+
657
+ # desc "delete SERVICE-MODULE-NAME [-v VERSION] [-y] [-p]", "Delete service module or service module version and all items contained in it. Optional parameter [-p] is to delete local directory."
658
+ # version_method_option
659
+ desc "delete SERVICE-MODULE-NAME [-y] [-p]", "Delete service module and all items contained in it. Optional parameter [-p] is to delete local directory."
660
+ method_option :force, :aliases => '-y', :type => :boolean, :default => false
661
+ method_option :purge, :aliases => '-p', :type => :boolean, :default => false
662
+ def delete(context_params)
663
+ module_location, modules_path = nil, nil
664
+ service_module_id = context_params.retrieve_arguments([:option_1!],method_argument_names)
665
+ version = options.version
666
+ service_module_name = get_service_module_name(service_module_id)
667
+
668
+ unless options.force?
669
+ # Ask user if really want to delete service module and all items contained in it, if not then return to dtk-shell without deleting
670
+ return unless Console.confirmation_prompt("Are you sure you want to delete service-module #{version.nil? ? '' : 'version '}'#{service_module_name}#{version.nil? ? '' : ('-' + version.to_s)}' and all items contained in it"+'?')
671
+ end
672
+
673
+ response =
674
+ if options.purge?
675
+ opts = {:module_name => service_module_name}
676
+ if version then opts.merge!(:version => version)
677
+ else opts.merge!(:delete_all_versions => true)
678
+ end
679
+
680
+ purge_clone_aux(:service_module,opts)
681
+ else
682
+ Helper(:git_repo).unlink_local_clone?(:service_module,service_module_name,version)
683
+ end
684
+ return response unless response.ok?
685
+
686
+ post_body = {
687
+ :service_module_id => service_module_id
688
+ }
689
+
690
+ action = (version ? "delete_version" : "delete")
691
+ post_body[:version] = version if version
692
+
693
+ response = post rest_url("service_module/#{action}"), post_body
694
+ return response unless response.ok?
695
+ module_name = response.data(:module_name)
696
+
697
+ # when changing context send request for getting latest services instead of getting from cache
698
+ @@invalidate_map << :service_module
699
+
700
+ msg = "Service module '#{service_module_name}' "
701
+ if version then msg << "version #{version} has been deleted"
702
+ else msg << "has been deleted"; end
703
+ OsUtil.print(msg,:yellow)
704
+
705
+ Response::Ok.new()
706
+ end
707
+
708
+ desc "delete-from-catalog [NAMESPACE/]REMOTE-SERVICE-MODULE-NAME [-y]", "Delete the service module from the DTK Network catalog"
709
+ method_option :force, :aliases => '-y', :type => :boolean, :default => false
710
+ def delete_from_catalog(context_params)
711
+ remote_service_name = context_params.retrieve_arguments([:option_1!],method_argument_names)
712
+
713
+ unless options.force?
714
+ # Ask user if really want to delete service module and all items contained in it, if not then return to dtk-shell without deleting
715
+ return unless Console.confirmation_prompt("Are you sure you want to delete remote service-module '#{remote_service_name}' and all items contained in it"+'?')
716
+ end
717
+
718
+ post_body = {
719
+ :remote_service_name => remote_service_name,
720
+ :rsa_pub_key => SshProcessing.rsa_pub_key_content()
721
+ }
722
+ response = post rest_url("service_module/delete_remote"), post_body
723
+ @@invalidate_map << :module_service
724
+
725
+ return response
726
+ end
727
+
728
+ desc "SERVICE-MODULE-NAME/ID delete-assembly ASSEMBLY-ID [-y]", "Delete assembly from service module."
729
+ method_option :force, :aliases => '-y', :type => :boolean, :default => false
730
+ def delete_assembly(context_params)
731
+ service_module_id, assembly_template_id = context_params.retrieve_arguments([:service_module_id!,:option_1!], method_argument_names)
732
+ service_module_name = context_params.retrieve_arguments([:service_module_name],method_argument_names)
733
+ assembly_template_name = (assembly_template_id.to_s =~ /^[0-9]+$/) ? DTK::Client::Assembly.get_assembly_template_name_for_service(assembly_template_id, service_module_name) : assembly_template_id
734
+ assembly_template_id = DTK::Client::Assembly.get_assembly_template_id_for_service(assembly_template_id, service_module_name) unless assembly_template_id.to_s =~ /^[0-9]+$/
735
+
736
+ if service_module_name.to_s =~ /^[0-9]+$/
737
+ service_module_id = service_module_name
738
+ service_module_name = get_service_module_name(service_module_id)
739
+ end
740
+
741
+ return unless Console.confirmation_prompt("Are you sure you want to delete assembly '#{assembly_template_name||assembly_template_id}'"+'?') unless options.force?
742
+
743
+ post_body = {
744
+ :service_module_id => service_module_id,
745
+ :assembly_id => assembly_template_id,
746
+ :subtype => :template
747
+ }
748
+
749
+ response = post rest_url("service_module/delete_assembly_template"), post_body
750
+ return response unless response.ok?
751
+
752
+ modules_path = OsUtil.service_clone_location()
753
+ module_location = "#{modules_path}/#{service_module_name}" if service_module_name
754
+ assembly_template_location = "#{module_location}/assemblies/#{assembly_template_name}" if (module_location && assembly_template_name)
755
+
756
+ if File.directory?(assembly_template_location)
757
+ unless (assembly_template_location.nil? || ("#{module_location}/assemblies/" == assembly_template_location))
758
+ FileUtils.rm_rf("#{assembly_template_location}")
759
+ end
760
+ end
761
+ version = nil
762
+ commit_msg = "Deleting assembly template #{assembly_template_name.to_s}"
763
+ internal_trigger = true
764
+ push_clone_changes_aux(:service_module, service_module_id, version, commit_msg, internal_trigger)
765
+ @@invalidate_map << :assembly
766
+ Response::Ok.new()
767
+ end
768
+ =begin
769
+ desc "SERVICE-NAME/ID assembly-templates list", "List assembly templates optionally filtered by service ID/NAME."
770
+ def assembly_template(context_params)
771
+
772
+ service_id, method_name = context_params.retrieve_arguments([:service_name!, :option_1!],method_argument_names)
773
+
774
+ options_args = ["-s", service_id]
775
+
776
+ entity_name = "assembly_template"
777
+ load_command(entity_name)
778
+ entity_class = DTK::Client.const_get "#{cap_form(entity_name)}"
779
+
780
+ response = entity_class.execute_from_cli(@conn, method_name, DTK::Shell::ContextParams.new, options_args, false)
781
+
782
+ end
783
+ =end
784
+ =begin
785
+ TODO: needs to be rewritten
786
+ desc "create-jenkins-project SERVICE-ID", "Create Jenkins project for service module"
787
+ def create_jenkins_project(context_params)
788
+ service_module_id = context_params.retrieve_arguments([:service_id],method_argument_names)
789
+ #require put here so dont necessarily have to install jenkins client gems
790
+
791
+ dtk_require_from_base('command_helpers/jenkins_client')
792
+ response = get rest_url("service_module/workspace_branch_info/#{service_module_id.to_s}")
793
+ unless response.ok?
794
+ errors_message = ''
795
+ response['errors'].each { |error| errors_message += ", reason='#{error['code']}' message='#{error['message']}'" }
796
+ raise DTK::Client::DtkError, "Invalid jenkins response#{errors_message}"
797
+ end
798
+ module_name,repo_url,branch = response.data_ret_and_remove!(:module_name,:repo_url,:workspace_branch)
799
+ JenkinsClient.create_service_module_project?(service_module_id,module_name,repo_url,branch)
800
+ #TODO: right now JenkinsClient wil throw error if problem; better to create an error resonse
801
+ response
802
+ end
803
+ =end
804
+ end
805
+ end
806
+