dtk-shell 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +5 -0
  3. data/Gemfile_dev +13 -0
  4. data/README.md +121 -0
  5. data/bin/dtk-execute +32 -0
  6. data/bin/dtk-run +92 -0
  7. data/bin/dtk-shell +31 -0
  8. data/dtk-shell.gemspec +50 -0
  9. data/lib/auxiliary.rb +61 -0
  10. data/lib/bundler_monkey_patch.rb +26 -0
  11. data/lib/client.rb +58 -0
  12. data/lib/command_helper.rb +33 -0
  13. data/lib/command_helpers/git_repo.rb +589 -0
  14. data/lib/command_helpers/git_repo/merge.rb +153 -0
  15. data/lib/command_helpers/jenkins_client.rb +106 -0
  16. data/lib/command_helpers/jenkins_client/config_xml.rb +288 -0
  17. data/lib/command_helpers/service_importer.rb +251 -0
  18. data/lib/command_helpers/service_link.rb +33 -0
  19. data/lib/command_helpers/test_module_creator.rb +69 -0
  20. data/lib/command_helpers/test_module_templates/dtk.model.yaml.eruby +10 -0
  21. data/lib/command_helpers/test_module_templates/spec_helper.rb.eruby +10 -0
  22. data/lib/command_helpers/test_module_templates/temp_component_spec.rb.eruby +5 -0
  23. data/lib/commands.rb +57 -0
  24. data/lib/commands/common/thor/access_control.rb +133 -0
  25. data/lib/commands/common/thor/action_result_handler.rb +74 -0
  26. data/lib/commands/common/thor/assembly_template.rb +92 -0
  27. data/lib/commands/common/thor/assembly_workspace.rb +1801 -0
  28. data/lib/commands/common/thor/base_command_helper.rb +59 -0
  29. data/lib/commands/common/thor/clone.rb +82 -0
  30. data/lib/commands/common/thor/common.rb +88 -0
  31. data/lib/commands/common/thor/common_base.rb +49 -0
  32. data/lib/commands/common/thor/create_target.rb +70 -0
  33. data/lib/commands/common/thor/edit.rb +255 -0
  34. data/lib/commands/common/thor/inventory_parser.rb +98 -0
  35. data/lib/commands/common/thor/list_diffs.rb +128 -0
  36. data/lib/commands/common/thor/module.rb +1011 -0
  37. data/lib/commands/common/thor/module/import.rb +210 -0
  38. data/lib/commands/common/thor/node.rb +53 -0
  39. data/lib/commands/common/thor/poller.rb +65 -0
  40. data/lib/commands/common/thor/pull_clone_changes.rb +28 -0
  41. data/lib/commands/common/thor/pull_from_remote.rb +152 -0
  42. data/lib/commands/common/thor/puppet_forge.rb +72 -0
  43. data/lib/commands/common/thor/purge_clone.rb +101 -0
  44. data/lib/commands/common/thor/push_clone_changes.rb +162 -0
  45. data/lib/commands/common/thor/push_to_remote.rb +94 -0
  46. data/lib/commands/common/thor/remotes.rb +71 -0
  47. data/lib/commands/common/thor/reparse.rb +40 -0
  48. data/lib/commands/common/thor/set_required_attributes.rb +46 -0
  49. data/lib/commands/thor/account.rb +239 -0
  50. data/lib/commands/thor/assembly.rb +356 -0
  51. data/lib/commands/thor/attribute.rb +79 -0
  52. data/lib/commands/thor/component.rb +70 -0
  53. data/lib/commands/thor/component_module.rb +501 -0
  54. data/lib/commands/thor/component_template.rb +174 -0
  55. data/lib/commands/thor/dependency.rb +34 -0
  56. data/lib/commands/thor/developer.rb +144 -0
  57. data/lib/commands/thor/dtk.rb +152 -0
  58. data/lib/commands/thor/library.rb +125 -0
  59. data/lib/commands/thor/node.rb +504 -0
  60. data/lib/commands/thor/node_template.rb +94 -0
  61. data/lib/commands/thor/project.rb +34 -0
  62. data/lib/commands/thor/provider.rb +233 -0
  63. data/lib/commands/thor/remotes.rb +49 -0
  64. data/lib/commands/thor/service.rb +941 -0
  65. data/lib/commands/thor/service_module.rb +914 -0
  66. data/lib/commands/thor/state_change.rb +25 -0
  67. data/lib/commands/thor/target.rb +250 -0
  68. data/lib/commands/thor/task.rb +116 -0
  69. data/lib/commands/thor/test_module.rb +310 -0
  70. data/lib/commands/thor/utils.rb +21 -0
  71. data/lib/commands/thor/workspace.rb +685 -0
  72. data/lib/config/cacert.pem +3785 -0
  73. data/lib/config/client.conf.header +20 -0
  74. data/lib/config/configuration.rb +99 -0
  75. data/lib/config/default.conf +16 -0
  76. data/lib/config/disk_cacher.rb +80 -0
  77. data/lib/configurator.rb +176 -0
  78. data/lib/context_router.rb +44 -0
  79. data/lib/core.rb +497 -0
  80. data/lib/domain/git_adapter.rb +412 -0
  81. data/lib/domain/git_error_handler.rb +64 -0
  82. data/lib/domain/response.rb +285 -0
  83. data/lib/domain/response/error_handler.rb +86 -0
  84. data/lib/dtk-shell/version.rb +20 -0
  85. data/lib/dtk_constants.rb +40 -0
  86. data/lib/dtk_error.rb +114 -0
  87. data/lib/dtk_logger.rb +126 -0
  88. data/lib/dtk_shell.rb +31 -0
  89. data/lib/error.rb +85 -0
  90. data/lib/execute.rb +29 -0
  91. data/lib/execute/cli_pure/cli_rerouter.rb +102 -0
  92. data/lib/execute/command.rb +40 -0
  93. data/lib/execute/command/api_call.rb +60 -0
  94. data/lib/execute/command/api_call/map.rb +60 -0
  95. data/lib/execute/command/api_call/service.rb +91 -0
  96. data/lib/execute/command/api_call/translation_term.rb +119 -0
  97. data/lib/execute/command/rest_call.rb +37 -0
  98. data/lib/execute/command_processor.rb +30 -0
  99. data/lib/execute/command_processor/rest_call.rb +59 -0
  100. data/lib/execute/error_usage.rb +21 -0
  101. data/lib/execute/execute_context.rb +86 -0
  102. data/lib/execute/execute_context/result_store.rb +37 -0
  103. data/lib/execute/script.rb +64 -0
  104. data/lib/execute/script/add_tenant.rb +121 -0
  105. data/lib/git-logs/git.log +0 -0
  106. data/lib/parser/adapters/option_parser.rb +70 -0
  107. data/lib/parser/adapters/thor.rb +555 -0
  108. data/lib/parser/adapters/thor/common_option_defs.rb +40 -0
  109. data/lib/require_first.rb +104 -0
  110. data/lib/search_hash.rb +44 -0
  111. data/lib/shell.rb +261 -0
  112. data/lib/shell/context.rb +1065 -0
  113. data/lib/shell/context_aux.rb +46 -0
  114. data/lib/shell/domain/active_context.rb +186 -0
  115. data/lib/shell/domain/context_entity.rb +89 -0
  116. data/lib/shell/domain/context_params.rb +223 -0
  117. data/lib/shell/domain/override_tasks.rb +88 -0
  118. data/lib/shell/domain/shadow_entity.rb +76 -0
  119. data/lib/shell/header_shell.rb +44 -0
  120. data/lib/shell/help_monkey_patch.rb +283 -0
  121. data/lib/shell/interactive_wizard.rb +225 -0
  122. data/lib/shell/message_queue.rb +63 -0
  123. data/lib/shell/parse_monkey_patch.rb +39 -0
  124. data/lib/shell/status_monitor.rb +124 -0
  125. data/lib/task_status.rb +83 -0
  126. data/lib/task_status/refresh_mode.rb +77 -0
  127. data/lib/task_status/snapshot_mode.rb +28 -0
  128. data/lib/task_status/stream_mode.rb +48 -0
  129. data/lib/task_status/stream_mode/element.rb +101 -0
  130. data/lib/task_status/stream_mode/element/format.rb +101 -0
  131. data/lib/task_status/stream_mode/element/hierarchical_task.rb +100 -0
  132. data/lib/task_status/stream_mode/element/hierarchical_task/result.rb +72 -0
  133. data/lib/task_status/stream_mode/element/hierarchical_task/result/action.rb +93 -0
  134. data/lib/task_status/stream_mode/element/hierarchical_task/result/components.rb +26 -0
  135. data/lib/task_status/stream_mode/element/hierarchical_task/result/node_level.rb +26 -0
  136. data/lib/task_status/stream_mode/element/hierarchical_task/steps.rb +34 -0
  137. data/lib/task_status/stream_mode/element/hierarchical_task/steps/action.rb +53 -0
  138. data/lib/task_status/stream_mode/element/hierarchical_task/steps/components.rb +53 -0
  139. data/lib/task_status/stream_mode/element/hierarchical_task/steps/node_level.rb +42 -0
  140. data/lib/task_status/stream_mode/element/no_results.rb +26 -0
  141. data/lib/task_status/stream_mode/element/render.rb +59 -0
  142. data/lib/task_status/stream_mode/element/stage.rb +84 -0
  143. data/lib/task_status/stream_mode/element/stage/render.rb +76 -0
  144. data/lib/task_status/stream_mode/element/task_end.rb +35 -0
  145. data/lib/task_status/stream_mode/element/task_start.rb +37 -0
  146. data/lib/util/common_util.rb +37 -0
  147. data/lib/util/console.rb +235 -0
  148. data/lib/util/dtk_puppet.rb +65 -0
  149. data/lib/util/module_util.rb +66 -0
  150. data/lib/util/os_util.rb +385 -0
  151. data/lib/util/permission_util.rb +31 -0
  152. data/lib/util/remote_dependency_util.rb +84 -0
  153. data/lib/util/ssh_util.rb +94 -0
  154. data/lib/view_processor.rb +129 -0
  155. data/lib/view_processor/augmented_simple_list.rb +44 -0
  156. data/lib/view_processor/hash_pretty_print.rb +123 -0
  157. data/lib/view_processor/simple_list.rb +156 -0
  158. data/lib/view_processor/table_print.rb +309 -0
  159. data/lib/violation.rb +86 -0
  160. data/lib/violation/attribute.rb +76 -0
  161. data/lib/violation/fix.rb +26 -0
  162. data/lib/violation/fix/result.rb +73 -0
  163. data/lib/violation/fix/result/error.rb +34 -0
  164. data/lib/violation/fix/set_attribute.rb +41 -0
  165. data/lib/violation/sub_classes.rb +60 -0
  166. data/puppet/manifests/init.pp +72 -0
  167. data/puppet/manifests/params.pp +16 -0
  168. data/puppet/r8meta.puppet.yml +35 -0
  169. data/puppet/templates/bash_profile.erb +2 -0
  170. data/puppet/templates/client.conf.erb +1 -0
  171. data/puppet/templates/dtkclient.erb +2 -0
  172. data/spec/component_module_spec.rb +34 -0
  173. data/spec/dependency_spec.rb +6 -0
  174. data/spec/dtk_shell_spec.rb +13 -0
  175. data/spec/dtk_spec.rb +33 -0
  176. data/spec/lib/spec_helper.rb +10 -0
  177. data/spec/lib/spec_thor.rb +108 -0
  178. data/spec/node_template_spec.rb +24 -0
  179. data/spec/project_spec.rb +6 -0
  180. data/spec/repo_spec.rb +7 -0
  181. data/spec/response_spec.rb +52 -0
  182. data/spec/service_module_spec.rb +38 -0
  183. data/spec/service_spec.rb +50 -0
  184. data/spec/state_change_spec.rb +7 -0
  185. data/spec/table_print_spec.rb +48 -0
  186. data/spec/target_spec.rb +57 -0
  187. data/spec/task_spec.rb +28 -0
  188. data/views/assembly/augmented_simple_list.rb +12 -0
  189. data/views/assembly_template/augmented_simple_list.rb +12 -0
  190. data/views/list_task/augmented_simple_list.rb +12 -0
  191. metadata +421 -0
@@ -0,0 +1,58 @@
1
+ #
2
+ # Copyright (C) 2010-2016 dtk contributors
3
+ #
4
+ # This file is part of the dtk project.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require 'rubygems'
19
+
20
+ begin
21
+ require File.expand_path('require_first', File.dirname(__FILE__))
22
+
23
+ if gem_only_available?
24
+ # loads it only if there is no common folder, and gem is installed
25
+ require 'dtk_common_core'
26
+ end
27
+
28
+ # Load DTK Common
29
+ dtk_require_dtk_common_core('dtk_common_core')
30
+
31
+ # Monkey Patching bundler to support loading specific Gemfile from dtk-client project's root - Ticket: DTK-585
32
+ dtk_require('config/configuration')
33
+
34
+ # we don't need Bundler.setup but will leave it commented just in case
35
+ # TODO: This is temp solution which will not use bundler.setup when in dev mode
36
+ # thus allowing us to use system gems and not just the ones specified in Gemfile
37
+ unless DTK::Configuration.get(:development_mode)
38
+ require 'bundler'
39
+ #TODO: rich temp hack becaus eof problem with gem dependencies; changed this because it was not working in 0.19.0
40
+ if Bundler.respond_to?(:start)
41
+ Bundler.start
42
+ end
43
+ dtk_require('bundler_monkey_patch')
44
+ end
45
+
46
+ ########
47
+ dtk_require('auxiliary')
48
+ dtk_require('core')
49
+ dtk_require('error')
50
+ dtk_require('dtk_constants')
51
+ dtk_require('commands')
52
+ dtk_require('view_processor')
53
+ dtk_require('search_hash')
54
+ dtk_require('dtk_logger')
55
+ dtk_require('violation')
56
+ rescue SystemExit, Interrupt
57
+ exit(1)
58
+ end
@@ -0,0 +1,33 @@
1
+ #
2
+ # Copyright (C) 2010-2016 dtk contributors
3
+ #
4
+ # This file is part of the dtk project.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ module DTK; module Client
19
+ module CommandHelperMixin
20
+ def Helper(helper_class_name)
21
+ unless Loaded[helper_class_name]
22
+ dtk_nested_require('command_helpers',helper_class_name)
23
+ Loaded[helper_class_name] = true
24
+ end
25
+ CommandHelper.const_get Common::Aux.snake_to_camel_case(helper_class_name.to_s)
26
+ end
27
+ Loaded = Hash.new
28
+ end
29
+
30
+ #TODO: make all commands helpers a subclass of this
31
+ class CommandHelper
32
+ end
33
+ end; end
@@ -0,0 +1,589 @@
1
+ #
2
+ # Copyright (C) 2010-2016 dtk contributors
3
+ #
4
+ # This file is part of the dtk project.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ # This code is predciated on assumption that they is only one local branch (with with documented exceptions)
19
+ # so checkout branch is not done in most cases
20
+ #TODO : make sure all functions that use local_repo_dir( inside pass in full_moudle_name, not just module_name
21
+ require 'fileutils'
22
+ dtk_require("../domain/git_adapter")
23
+ dtk_require("../domain/git_error_handler")
24
+
25
+ module DTK; module Client; class CommandHelper
26
+ class GitRepo < self; class << self
27
+ dtk_require('git_repo/merge')
28
+
29
+ def create(repo_dir,branch=nil,opts={})
30
+ GitAdapter.new(repo_dir, branch)
31
+ end
32
+
33
+ def create_clone_from_optional_branch(type, module_name, repo_url, opts={})
34
+ branch = opts[:branch]
35
+ version = opts[:version]
36
+ namespace = opts[:namespace]
37
+ create_clone_with_branch(type,module_name,repo_url,branch,version,namespace,{:track_remote_branch => true}.merge(opts))
38
+ end
39
+
40
+ # TODO: should we deprecate below for above, subsituting the body of below for above ?
41
+ def create_clone_with_branch(type, module_name, repo_url, branch=nil, version=nil, module_namespace=nil, opts={})
42
+ Response.wrap_helper_actions do
43
+ full_name = module_namespace ? ModuleUtil.resolve_name(module_name, module_namespace) : module_name
44
+
45
+ modules_dir = modules_dir(type,full_name,version,opts)
46
+ FileUtils.mkdir_p(modules_dir) unless File.directory?(modules_dir)
47
+
48
+ target_repo_dir = local_repo_dir(type,full_name,version,opts)
49
+ if File.exists?(target_repo_dir)
50
+ # if local copy of module exists then move that module to backups location
51
+ if opts[:backup_if_exist]
52
+ backup_dir = backup_dir(type, full_name)
53
+ FileUtils.mv(target_repo_dir, backup_dir)
54
+ puts "Backup of existing module directory moved to '#{backup_dir}'"
55
+ else
56
+ raise ErrorUsage.new("Directory '#{target_repo_dir}' is not empty; it must be deleted or removed before retrying the command", :log_error => false)
57
+ end
58
+ end
59
+
60
+ begin
61
+ opts_clone = (opts[:track_remote_branch] ? {:track_remote_branch => true} : {})
62
+ GitAdapter.clone(repo_url, target_repo_dir, branch, opts_clone)
63
+ rescue => e
64
+ # Handling Git error messages with more user friendly messages
65
+ e = GitErrorHandler.handle(e)
66
+
67
+ #cleanup by deleting directory
68
+ FileUtils.rm_rf(target_repo_dir) if File.directory?(target_repo_dir)
69
+ error_msg = "Clone to directory (#{target_repo_dir}) failed"
70
+
71
+ DtkLogger.instance.error_pp(e.message, e.backtrace)
72
+
73
+ raise ErrorUsage.new(error_msg, :log_error => false)
74
+ end
75
+ {"module_directory" => target_repo_dir}
76
+ end
77
+ end
78
+
79
+ #TODO: this does not push; may make that an option
80
+ def add_file(repo_obj,path,content,msg=nil)
81
+ Response.wrap_helper_actions() do
82
+ ret = Hash.new
83
+ repo_obj.add_file(path,content)
84
+ ret["message"] = msg if msg
85
+ ret
86
+ end
87
+ end
88
+
89
+ # opts can have the following keys
90
+ #
91
+ # :remote_repo
92
+ # :remote_branch
93
+ # :remote_repo_url
94
+ # :local_branch
95
+ # :override_repo_dir_location
96
+ # :no_fetch
97
+ #
98
+ def push_changes(type, full_module_name, version, opts={})
99
+ Response.wrap_helper_actions() do
100
+ repo_dir = opts[:override_repo_dir_location] ? opts[:override_repo_dir_location] : local_repo_dir(type, full_module_name, version, opts)
101
+ repo = create(repo_dir, opts[:local_branch])
102
+ push_repo_changes_aux(repo, opts)
103
+ end
104
+ end
105
+
106
+ def get_diffs(type, module_name, version, opts={})
107
+ Response.wrap_helper_actions() do
108
+ repo_dir = local_repo_dir(type,module_name,version)
109
+ repo = create(repo_dir,opts[:local_branch])
110
+ get_diffs_aux(repo,opts)
111
+ end
112
+ end
113
+
114
+ def get_remote_diffs(type, module_name, version, opts={})
115
+ Response.wrap_helper_actions() do
116
+ repo_dir = local_repo_dir(type,module_name,version)
117
+ repo = create(repo_dir,opts[:local_branch])
118
+ get_remote_diffs_aux(repo,opts)
119
+ end
120
+ end
121
+
122
+ # opts can have the following keys
123
+ #
124
+ # :remote_repo
125
+ # :remote_branch
126
+ # :remote_repo_url
127
+ # :local_branch
128
+ # :version
129
+ # :commit_sha
130
+ # :full_module_name
131
+ # :namespace
132
+ # returns:
133
+ # { :diffs => , :commit_sha => }
134
+ def pull_changes(type,module_name,opts={})
135
+ Response.wrap_helper_actions() do
136
+ full_module_name = full_module_name(module_name,opts)
137
+ repo_dir = local_repo_dir(type,full_module_name,opts[:version],opts)
138
+ repo = create(repo_dir,opts[:local_branch])
139
+ opts.merge!(:full_module_name => full_module_name)
140
+ response = pull_repo_changes_aux(repo,opts)
141
+ response
142
+ end
143
+ end
144
+ def pull_changes?(type,module_name,opts={})
145
+ if local_clone_dir_exists?(type,module_name,opts)
146
+ pull_changes(type,module_name,opts)
147
+ else
148
+ Response.wrap_helper_actions()
149
+ end
150
+ end
151
+
152
+ def hard_reset_branch_to_sha(type, module_name, opts={})
153
+ Response.wrap_helper_actions() do
154
+ full_module_name = full_module_name(module_name,opts)
155
+ repo_dir = local_repo_dir(type, full_module_name, opts[:version], opts)
156
+ repo = create(repo_dir, opts[:local_branch])
157
+ current_branch_sha = opts[:current_branch_sha]
158
+ repo.reset_hard(current_branch_sha)
159
+ end
160
+ end
161
+
162
+ # opts can have the following keys
163
+ #
164
+ # :version
165
+ # :full_module_name
166
+ # :namespace
167
+ def local_clone_dir_exists?(type,module_name,opts={})
168
+ full_module_name = full_module_name(module_name,opts)
169
+ ret = local_repo_dir(type, full_module_name, opts[:version], opts)
170
+ File.directory?(ret) && ret
171
+ end
172
+
173
+ def full_module_name(module_name,opts)
174
+ opts[:full_module_name] || ModuleUtil.resolve_name(module_name, opts[:namespace])
175
+ end
176
+ private :full_module_name
177
+
178
+ def synchronize_clone(type,module_name,commit_sha,opts={})
179
+ pull_changes?(type,module_name,{:commit_sha => commit_sha}.merge(opts))
180
+ Response::Ok.new()
181
+ end
182
+
183
+ # if local clone exists remove its .git directory
184
+ def unlink_local_clone?(type,module_name,version=nil)
185
+ Response.wrap_helper_actions() do
186
+ local_repo_dir = local_repo_dir(type,module_name,version)
187
+ git_dir = "#{local_repo_dir}/.git"
188
+ if File.directory?(git_dir)
189
+ FileUtils.rm_rf(git_dir)
190
+ end
191
+ end
192
+ end
193
+
194
+ def check_local_dir_exists_with_content(type, module_name, version=nil, module_namespace=nil)
195
+ full_module_name = ModuleUtil.join_name(module_name, module_namespace)
196
+ Response.wrap_helper_actions() do
197
+ ret = Hash.new
198
+ local_repo_dir = local_repo_dir(type,full_module_name,version)
199
+
200
+ unless File.directory?(local_repo_dir)
201
+ raise ErrorUsage.new("The content for module (#{full_module_name}) should be put in directory (#{local_repo_dir})",:log_error=>false)
202
+ end
203
+
204
+ # transfered this part to initialize_client_clone_and_push because if we remove .git folder and
205
+ # if create on server fails we will lose this .git folder and will not be able to push local changes to server
206
+ # if File.directory?("#{local_repo_dir}/.git")
207
+ # response = unlink_local_clone?(type,module_name,version)
208
+ # unless response.ok?
209
+ # # in case delete went wrong, we raise usage error
210
+ # raise ErrorUsage.new("Directory (#{local_repo_dir} is set as a git repo; to continue it must be a non git repo; this can be handled by shell command 'rm -rf #{local_repo_dir}/.git'")
211
+ # end
212
+
213
+ # # we return to normal flow, since .git dir is removed
214
+ # end
215
+
216
+ if Dir["#{local_repo_dir}/*"].empty?
217
+ raise ErrorUsage.new("Directory (#{local_repo_dir}) must have ths initial content for module (#{full_module_name})")
218
+ end
219
+ {"module_directory" => local_repo_dir}
220
+ end
221
+ end
222
+
223
+ def cp_r_to_new_namespace(type, module_name, src_namespace, dest_namespace, version = nil)
224
+ full_name_src = ModuleUtil.join_name(module_name, src_namespace)
225
+ full_name_dest = ModuleUtil.join_name(module_name, dest_namespace)
226
+
227
+ local_src_dir = local_repo_dir(type, full_name_src, version)
228
+ local_dest_dir = local_repo_dir(type, full_name_dest, version)
229
+
230
+ Response.wrap_helper_actions() do
231
+ if File.directory?(local_src_dir) && !(Dir["#{local_src_dir}/*"].empty?)
232
+ raise ErrorUsage.new("Directory (#{local_dest_dir}) already exist with content.",:log_error=>false) if File.directory?(local_dest_dir) && !(Dir["#{local_dest_dir}/*"].empty?)
233
+
234
+ # create namespace directory if does not exist, and copy source to destination module directory
235
+ namespace_dir = local_repo_dir(type, dest_namespace, version)
236
+ FileUtils.mkdir_p(namespace_dir)
237
+ FileUtils.cp_r(local_src_dir, local_dest_dir)
238
+ else
239
+ raise ErrorUsage.new("The content for module (#{full_name_src}) should be put in directory (#{local_src_dir})",:log_error=>false)
240
+ end
241
+ {"module_directory" => local_dest_dir}
242
+ end
243
+ end
244
+
245
+ def create_new_version(type, src_branch, module_name, src_namespace, version, repo_url, opts = {})
246
+ full_name_src = ModuleUtil.join_name(module_name, src_namespace)
247
+ full_name_dest = "#{full_name_src}-#{version}"
248
+
249
+ local_src_dir = local_repo_dir(type, full_name_src)
250
+ local_dest_dir = local_repo_dir(type, full_name_dest)
251
+ branch = "#{src_branch}-v#{version}"
252
+ exist_already = false
253
+
254
+ if File.directory?(local_src_dir) && !(Dir["#{local_src_dir}/*"].empty?)
255
+ if File.directory?(local_dest_dir) && !(Dir["#{local_dest_dir}/*"].empty?)
256
+ # raise ErrorUsage.new("Directory (#{local_dest_dir}) already exist with content.",:log_error=>false)
257
+ exist_already = true
258
+ else
259
+ FileUtils.cp_r(local_src_dir, local_dest_dir)
260
+ end
261
+ else
262
+ raise ErrorUsage.new("The content for module (#{full_name_src}) should be put in directory (#{local_src_dir})",:log_error=>false)
263
+ end
264
+
265
+ unless exist_already
266
+ Response.wrap_helper_actions() do
267
+ repo_dir = local_dest_dir
268
+ remote_branch = local_branch = branch
269
+ repo = create(repo_dir, branch, :no_initial_commit => true)
270
+ repo.add_remote(remote(branch), repo_url)
271
+ repo.checkout(branch, { :new_branch => true })
272
+ repo.push_with_remote(remote(), remote_branch)
273
+ repo.delete_branch(src_branch)
274
+ repo.fetch()
275
+ end
276
+ end
277
+
278
+ {'module_directory' => local_dest_dir, 'version' => version, 'branch' => branch, 'exist_already' => exist_already}
279
+ end
280
+
281
+ def rename_and_initialize_clone_and_push(type, module_name, new_module_name, branch, repo_url, local_repo_dir, version = nil)
282
+ # check to see if the new dir has proper naming e.g. (~/dtk/component_modules/dtk::java)
283
+ unless local_repo_dir.match(/\/#{new_module_name.gsub(ModuleUtil::NAMESPACE_SEPERATOR,'/')}$/)
284
+ old_dir = local_repo_dir
285
+ new_dir = local_repo_dir.gsub(/#{module_name}$/, new_module_name.split(ModuleUtil::NAMESPACE_SEPERATOR).join('/'))
286
+
287
+ # creates directory if missing
288
+ parent_path = new_dir.gsub(/(\/\w+)$/,'')
289
+ FileUtils::mkdir_p(parent_path) unless File.directory?(parent_path)
290
+ # raise ErrorUsage.new("Destination folder already exists '#{new_dir}', aborting initialization.") if File.directory?(new_dir)
291
+ if File.directory?(new_dir)
292
+ # return empty response if user does not want to overwrite current directory
293
+ return unless Console.confirmation_prompt("Destination directory #{new_dir} exists already. Do you want to overwrite it with content from #{old_dir}"+'?')
294
+ FileUtils.rm_rf(new_dir)
295
+ end
296
+ # FileUtils.mv(old_dir, new_dir)
297
+ FileUtils.cp_r(old_dir, new_dir)
298
+ else
299
+ new_dir = local_repo_dir
300
+ end
301
+
302
+ # Continue push
303
+ response = initialize_client_clone_and_push(type, new_module_name, branch, repo_url, new_dir, version)
304
+ return response unless response.ok?
305
+
306
+ response.data.merge!(:old_dir => old_dir)
307
+ response
308
+ end
309
+
310
+ # makes repo_dir (determined from type and module_name) into a git dir, pulls, adds, content and then pushes
311
+ def initialize_client_clone_and_push(type, module_name, branch, repo_url, local_repo_dir, version=nil)
312
+ # moved this part from 'check_local_dir_exists_with_content' to this method since this only deletes .git folder
313
+ # which can cause us problems if import fails
314
+ if File.directory?("#{local_repo_dir}/.git")
315
+ response = unlink_local_clone?(type,module_name,version)
316
+ unless response.ok?
317
+ # in case delete went wrong, we raise usage error
318
+ raise DtkError.new("Directory (#{local_repo_dir} is set as a git repo; to continue it must be a non git repo; this can be handled by shell command 'rm -rf #{local_repo_dir}/.git'")
319
+ end
320
+ # we return to normal flow, since .git dir is removed
321
+ end
322
+
323
+ Response.wrap_helper_actions() do
324
+ ret = Hash.new
325
+ repo_dir = local_repo_dir(type,module_name)
326
+
327
+ repo = create(repo_dir,branch,:init => true, :no_initial_commit => true)
328
+ repo.add_remote(remote(),repo_url)
329
+ remote_branch = local_branch = branch
330
+
331
+ repo.pull_remote_to_local(remote_branch,local_branch,remote())
332
+ repo.stage_changes()
333
+ repo.commit("Adding files during initialization")
334
+ repo.push_with_remote(remote(), remote_branch)
335
+
336
+ commit_sha = repo.head_commit_sha()
337
+ {"repo_obj" => repo,"commit_sha" => commit_sha}
338
+ end
339
+ end
340
+
341
+ private
342
+ # TODO: in common expose Common::GritAdapter at less nested level
343
+ class DiffSummary < ::DTK::Common::SimpleHashObject
344
+ def self.new_version(repo)
345
+ new(repo.new_version())
346
+ end
347
+
348
+ def self.diff(repo,local_branch,remote_reference)
349
+ new(repo.diff_summary(local_branch,remote_reference))
350
+ end
351
+
352
+ def self.diff_remote(repo,local_branch,remote_reference)
353
+ new(repo.diff_remote_summary(local_branch,remote_reference))
354
+ end
355
+
356
+ # def self.diff_remote(repo,ref1)
357
+ # new(repo.diff(ref1).ret_summary())
358
+ # end
359
+
360
+ def any_diffs?
361
+ changes = false
362
+ self.each do |k,v|
363
+ unless v.empty?
364
+ changes = true
365
+ break
366
+ end
367
+ end
368
+ changes
369
+ end
370
+ end
371
+
372
+ #returns hash with keys
373
+ #: diffs - hash with diffs
374
+ # commit_sha - sha of currenet_commit
375
+ def push_repo_changes_aux(repo, opts={})
376
+ diffs = DiffSummary.new()
377
+
378
+ # adding untracked files (newly added files)
379
+ repo.stage_changes()
380
+
381
+ # commit if there has been changes
382
+ if repo.changed?
383
+ repo.commit(opts[:commit_msg]||"Pushing changes from client") #TODO: make more descriptive
384
+ end
385
+
386
+ if opts[:remote_repo] and opts[:remote_repo_url]
387
+ repo.add_remote(opts[:remote_repo],opts[:remote_repo_url])
388
+ end
389
+
390
+ unless opts[:no_fetch]
391
+ repo.fetch(remote(opts[:remote_repo]))
392
+ end
393
+
394
+ local_branch = repo.local_branch_name
395
+
396
+ remote_branch_ref = remote_branch_ref(local_branch, opts)
397
+
398
+ #check if merge needed
399
+ commit_shas = Hash.new
400
+ merge_rel = repo.merge_relationship(:remote_branch,remote_branch_ref, :ret_commit_shas => commit_shas)
401
+ commit_sha = nil
402
+ diffs = DiffSummary.new_version(repo)
403
+
404
+ if merge_rel == :equal
405
+ commit_sha = commit_shas[:other_sha]
406
+ elsif [:branchpoint,:local_behind].include?(merge_rel)
407
+ if opts[:force]
408
+ diffs = DiffSummary.diff(repo,local_branch, remote_branch_ref)
409
+ if diffs.any_diffs?()
410
+ repo.push(remote_branch_ref, {:force => opts[:force]})
411
+ end
412
+ commit_sha = repo.find_remote_sha(remote_branch_ref)
413
+ else
414
+ where = opts[:where] || 'server'
415
+ msg = "Merge needed before module (#{pp_module(repo)}) can be pushed to #{where}. "
416
+ if where.to_sym == :catalog
417
+ msg << " Either a merge into the local module can be done with pull-dtkn command, followed by push-dtkn or force"
418
+ else
419
+ msg << "Force"
420
+ end
421
+ msg << " push can be used with the '--force' option, but this will overwrite remote contents"
422
+ raise ErrorUsage.new(msg)
423
+ end
424
+ elsif merge_rel == :no_remote_ref
425
+ repo.push(remote_branch_ref)
426
+ commit_sha = commit_shas[:local_sha]
427
+ elsif merge_rel == :local_ahead
428
+ # see if any diffs between fetched remote and local branch
429
+ # this has be done after commit
430
+ diffs = DiffSummary.diff(repo,local_branch, remote_branch_ref)
431
+
432
+ if diffs.any_diffs?()
433
+ repo.push(remote_branch_ref, {:force => opts[:force]})
434
+ end
435
+
436
+ commit_sha = repo.find_remote_sha(remote_branch_ref)
437
+ else
438
+ raise Error.new("Unexpected merge_rel (#{merge_rel})")
439
+ end
440
+
441
+ {"diffs" => diffs, "commit_sha" => commit_sha, "repo_obj" => repo}
442
+ end
443
+
444
+ def get_diffs_aux(repo,opts={})
445
+ diffs = DiffSummary.new()
446
+ #add any file that is untracked
447
+
448
+ # repo.stage_changes()
449
+
450
+ if opts[:remote_repo] and opts[:remote_repo_url]
451
+ repo.add_remote(opts[:remote_repo],opts[:remote_repo_url])
452
+ end
453
+
454
+ unless opts[:no_fetch]
455
+ repo.fetch(remote(opts[:remote_repo]))
456
+ end
457
+
458
+ local_branch = repo.local_branch_name
459
+
460
+ remote_branch_ref = remote_branch_ref(local_branch, opts)
461
+
462
+ commit_shas = Hash.new
463
+ merge_rel = repo.merge_relationship(:remote_branch,remote_branch_ref, :ret_commit_shas => commit_shas)
464
+ commit_sha = nil
465
+
466
+ if merge_rel == :equal
467
+ commit_sha = commit_shas[:other_sha]
468
+ elsif merge_rel == :no_remote_ref
469
+ diffs = DiffSummary.new_version(repo)
470
+ commit_sha = commit_shas[:local_sha]
471
+ end
472
+
473
+ # diffs = DiffSummary.diff_remote(repo,"remotes/#{remote_branch_ref}")
474
+ diffs = DiffSummary.diff(repo,local_branch, remote_branch_ref)
475
+ commit_sha = repo.find_remote_sha(remote_branch_ref)
476
+
477
+ {"diffs" => diffs, "commit_sha" => commit_sha, "repo_obj" => repo, "status" => repo.local_summary() }
478
+ end
479
+
480
+ def get_remote_diffs_aux(repo,opts={})
481
+ diffs = DiffSummary.new()
482
+ #add any file that is untracked
483
+
484
+ # repo.stage_changes()
485
+ if opts[:remote_repo] and opts[:remote_repo_url]
486
+ repo.add_remote(opts[:remote_repo],opts[:remote_repo_url])
487
+ end
488
+
489
+ unless opts[:no_fetch]
490
+ repo.fetch(remote(opts[:remote_repo]))
491
+ end
492
+
493
+ local_branch = repo.local_branch_name
494
+ remote_branch_ref = remote_branch_ref(local_branch, opts)
495
+
496
+ commit_shas = Hash.new
497
+ merge_rel = repo.merge_relationship(:remote_branch, remote_branch_ref, :ret_commit_shas => commit_shas)
498
+ commit_sha = nil
499
+
500
+ if merge_rel == :equal
501
+ commit_sha = commit_shas[:other_sha]
502
+ elsif merge_rel == :no_remote_ref
503
+ diffs = DiffSummary.new_version(repo)
504
+ commit_sha = commit_shas[:local_sha]
505
+ end
506
+
507
+ diffs = DiffSummary.diff_remote(repo,local_branch, remote_branch_ref)
508
+ { "diffs" => (diffs[:diffs]||"").to_s, "status" => repo.local_summary() }
509
+ end
510
+
511
+
512
+ def diffs__no_diffs
513
+ DiffSummary.new
514
+ end
515
+ def compute_diffs(repo, remote_branch_ref)
516
+ DiffSummary.diff(repo, repo.local_branch_name, remote_branch_ref)
517
+ end
518
+ public :diffs__no_diffs, :compute_diffs
519
+
520
+ def pull_repo_changes_aux(repo, opts = {})
521
+ # TODO: cleanup use of hard_reset and force
522
+ force = opts[:hard_reset] || opts[:force]
523
+
524
+ # default commit in case it is needed
525
+ if repo.changed?
526
+ repo.stage_and_commit('Commit prior to pull from remote')
527
+ end
528
+
529
+ if commit_sha = opts[:commit_sha]
530
+ # no op if at commit_sha
531
+ return diffs__no_diffs if (commit_sha == repo.head_commit_sha()) && !force
532
+ end
533
+
534
+ if opts[:remote_repo] && opts[:remote_repo_url]
535
+ repo.add_remote(opts[:remote_repo], opts[:remote_repo_url])
536
+ end
537
+
538
+ repo.fetch(remote(opts[:remote_repo]))
539
+ remote_branch_ref = remote_branch_ref(repo.local_branch_name, opts)
540
+ # TODO: cleanup use of hard_reset and force; Merge.merge just uses :force
541
+ Merge.merge(repo, remote_branch_ref, opts.merge(:force => force))
542
+ end
543
+
544
+ def remote(remote_repo=nil)
545
+ remote_repo||"origin"
546
+ end
547
+
548
+ def remote_branch_ref(local_branch,opts={})
549
+ "#{remote(opts[:remote_repo])}/#{opts[:remote_branch]||opts[:local_branch]||local_branch}"
550
+ end
551
+
552
+ def modules_dir(type, module_name, version=nil, opts={})
553
+ type = type.to_sym
554
+ if assembly_module = opts[:assembly_module]
555
+ OsUtil.module_location_parts(type,module_name,version,opts)[0]
556
+ elsif type == :component_module
557
+ OsUtil.component_clone_location()
558
+ elsif type == :service_module
559
+ OsUtil.service_clone_location()
560
+ elsif type == :test_module
561
+ OsUtil.test_clone_location()
562
+ else
563
+ raise Error.new("Unexpected module type (#{type})")
564
+ end
565
+ end
566
+
567
+ def local_repo_dir(type,full_module_name,version=nil,opts={})
568
+ OsUtil.module_location(type, full_module_name, version, opts)
569
+ end
570
+
571
+ def backup_dir(type, full_module_name, opts={})
572
+ namespace, name = full_module_name.split(':', 2)
573
+ module_type = type.split('_').first
574
+ backups_dir = OsUtil.backups_location()
575
+
576
+ # create backups dir if deleted for some reason
577
+ FileUtils::mkdir_p(backups_dir) unless File.directory?(backups_dir)
578
+ "#{backups_dir}/#{module_type}-#{namespace}-#{name}-#{Time.now.to_i}"
579
+ end
580
+
581
+ def adapter_class()
582
+ Common::GritAdapter::FileAccess
583
+ end
584
+
585
+ def pp_module(repo)
586
+ repo.repo_dir.gsub(Regexp.new("/$"),"").split("/").last
587
+ end
588
+ end; end
589
+ end; end; end