dtk-shell 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
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,225 @@
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 'colorize'
19
+ require 'readline'
20
+ require 'highline/import'
21
+
22
+ module DTK::Client
23
+ module Shell
24
+ class InteractiveWizard
25
+
26
+ PP_LINE_HEAD = '--------------------------------- DATA ---------------------------------'
27
+ PP_LINE = '------------------------------------------------------------------------'
28
+ INVALID_INPUT = OsUtil.colorize(" Input is not valid. ", :yellow)
29
+
30
+ def initialize
31
+ end
32
+
33
+ # InteractiveWizard.interactive_user_input is generic wizard which will return hash map based on metadata input
34
+ #
35
+ # Example of form of param 'wizard_params'
36
+ #
37
+ # wizard_params = [
38
+ # {:target_name => {}},
39
+ # {:description => {:optional => true }}
40
+ # {:iaas_type => { :type => :selection, :options => [:ec2] }},
41
+ # {:aws_install => { :type => :question,
42
+ # :question => "Do we have your permission to add necessery 'key-pair' and 'security-group' to your EC2 account?",
43
+ # :options => ["yes","no"],
44
+ # :required_options => ["yes"],
45
+ # :explanation => "This permission is necessary for creation of a custom target."
46
+ # }}
47
+ # ]
48
+ #
49
+ def self.interactive_user_input(wizard_dsl, recursion_call = false)
50
+ results = {}
51
+ wizard_dsl = [wizard_dsl].flatten
52
+ begin
53
+ wizard_dsl.each do |meta_input|
54
+ input_name = meta_input.keys.first
55
+ display_name = input_name.to_s.gsub(/_/,' ').capitalize
56
+ metadata = meta_input.values.first
57
+
58
+ # default values
59
+ output = display_name
60
+ validation = metadata[:validation]
61
+ is_password = false
62
+ is_required = metadata[:requried]
63
+
64
+ case metadata[:type]
65
+ when nil
66
+ when :email
67
+ validation = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
68
+ when :question
69
+ output = "#{metadata[:question]}"
70
+ validation = metadata[:validation]
71
+ when :password
72
+ is_password = true
73
+ is_required = true
74
+ when :repeat_password
75
+ is_password = true
76
+ is_required = true
77
+ validation = /^#{results[:password]}$/
78
+ when :selection
79
+ options = ""
80
+ display_field = metadata[:display_field]
81
+ metadata[:options].each_with_index do |o,i|
82
+ if display_field
83
+ options += "\t#{i+1}. #{o[display_field]}\n"
84
+ else
85
+ options += "\t#{i+1}. #{o}\n"
86
+ end
87
+ end
88
+ options += OsUtil.colorize("\t0. Skip\n", :yellow) if metadata[:skip_option]
89
+ output = "Select '#{display_name}': \n\n #{options} \n "
90
+ validation_range_start = metadata[:skip_option] ? 0 : 1
91
+ validation = (validation_range_start..metadata[:options].size).to_a
92
+ when :group
93
+ # recursion call to populate second level of hash params
94
+ puts " Enter '#{display_name}': "
95
+ results[input_name] = self.interactive_user_input(metadata[:options], true)
96
+ next
97
+ end
98
+
99
+ input = text_input(output, is_required, validation, is_password)
100
+
101
+ if metadata[:required_options] && !metadata[:required_options].include?(input)
102
+ # case where we have to give explicit permission, if answer is not affirmative
103
+ # we terminate rest of the wizard
104
+ OsUtil.print(" #{metadata[:explanation]}", :red)
105
+ return nil
106
+ end
107
+
108
+ # post processing
109
+ if metadata[:type] == :selection
110
+ input = input.to_i == 0 ? nil : metadata[:options][input.to_i - 1]
111
+ end
112
+
113
+ results[input_name] = input
114
+ end
115
+
116
+ return results
117
+ rescue Interrupt => e
118
+ puts
119
+ raise DtkValidationError, "Exiting the wizard ..."
120
+ end
121
+ end
122
+
123
+ def self.text_input(output, is_required = false, validation_regex = nil, is_password = false)
124
+ while line = ask("#{output}: ") { |q| q.echo = !is_password }
125
+ if is_required && line.strip.empty?
126
+ puts OsUtil.colorize("Field '#{output}' is required field. ", :red)
127
+ next
128
+ end
129
+
130
+ if validation_regex && !validation_regex.match(line)
131
+ puts OsUtil.colorize("Input is not valid, please enter it again. ", :red)
132
+ next
133
+ end
134
+
135
+ return line
136
+ end
137
+ end
138
+
139
+ # response_type can be
140
+ # :value_provided
141
+ # :skipped
142
+ PromptResponse = Struct.new(:response_type, :value)
143
+ def self.prompt_user_for_value(prompt_msg)
144
+ value = nil
145
+ puts "#{prompt_msg}:"
146
+ begin
147
+ value = Readline.readline(": ", true)
148
+ response_type = :value_provided
149
+ rescue Interrupt
150
+ response_type = :skipped
151
+ end
152
+ PromptResponse.new(response_type, value)
153
+ end
154
+
155
+ # takes hash maps with description of missing params and
156
+ # returns array of hash map with key, value for each missed param
157
+ def self.resolve_missing_params(param_list, additional_message = nil)
158
+ begin
159
+ user_provided_params, checkup_hash = [], {}
160
+
161
+ puts "\nPlease fill in missing data.\n"
162
+ param_list.each do |param_info|
163
+ description =
164
+ if param_info['display_name'] =~ Regexp.new(param_info['description'])
165
+ param_info['display_name']
166
+ else
167
+ "#{param_info['display_name']} (#{param_info['description']})"
168
+ end
169
+ datatype_info = (param_info['datatype'] ? OsUtil.colorize(" [#{param_info['datatype'].upcase}]", :yellow) : '')
170
+ string_identifier = OsUtil.colorize(description, :green) + datatype_info
171
+
172
+ puts "Please enter #{string_identifier}:"
173
+ while line = Readline.readline(": ", true)
174
+ id = param_info['id']
175
+ user_provided_params << {:id => id, :value => line, :display_name => param_info['display_name']}
176
+ checkup_hash[id] = {:value => line, :description => description}
177
+ break
178
+ end
179
+
180
+ end
181
+
182
+ # pp print for provided parameters
183
+ pretty_print_provided_user_info(checkup_hash)
184
+
185
+ # make sure this is satisfactory
186
+ while line = Readline.readline("Is provided information ok? (yes|no) ", true)
187
+ # start all over again
188
+ return resolve_missing_params(param_list) if 'no'.eql? line
189
+ # continue with the code
190
+ break if 'yes'.eql? line
191
+ end
192
+
193
+ rescue Interrupt => e
194
+ raise DtkError::InteractiveWizardError, "You have decided to skip correction wizard.#{additional_message}"
195
+ end
196
+
197
+ return user_provided_params
198
+ end
199
+
200
+ private
201
+
202
+ def self.pretty_print_provided_user_info(user_information)
203
+ puts PP_LINE_HEAD
204
+ user_information.each do |key,info|
205
+ description = info[:description]
206
+ value = info[:value]
207
+ printf "%48s : %s\n", OsUtil.colorize(description, :green), OsUtil.colorize(value, :yellow)
208
+ end
209
+ puts PP_LINE
210
+ puts
211
+ end
212
+ end
213
+ end
214
+ end
215
+
216
+ =begin
217
+ TODO: remove deprecated
218
+
219
+ def self.validate_region(region)
220
+ unless EC2_REGIONS.include? region
221
+ raise DtkValidationError.new("Region '#{region}' is not EC2 region, use one of: #{EC2_REGIONS.join(',')}")
222
+ end
223
+ end
224
+ EC2_REGIONS = ['us-east-1','us-west-1','us-west-2','eu-west-1','sa-east-1','ap-northeast-1','ap-southeast-1','ap-southeast-2' ]
225
+ =end
@@ -0,0 +1,63 @@
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
+ # TODO-REMOVE: Check if we need this anymore
19
+
20
+ dtk_require_from_base('util/os_util')
21
+
22
+ module DTK
23
+ module Shell
24
+ class MessageQueue
25
+ include Singleton
26
+
27
+ MESSAGE_TYPES = [:info, :warn, :error]
28
+
29
+ def initialize
30
+ @queue = {}
31
+ init_or_clear()
32
+ end
33
+
34
+ def self.process_response(response_obj)
35
+ self.instance.process_response(response_obj)
36
+ end
37
+
38
+ def self.print_messages()
39
+ self.instance.print_messages()
40
+ end
41
+
42
+ def init_or_clear()
43
+ MESSAGE_TYPES.each { |msg_type| @queue[msg_type] = [] }
44
+ end
45
+
46
+ def print_messages()
47
+ @queue[:info].each { |msg| DTK::Client::OsUtil.print(msg, :white) }
48
+ @queue[:warn].each { |msg| DTK::Client::OsUtil.print(msg, :yellow) }
49
+ @queue[:error].each { |msg| DTK::Client::OsUtil.print(msg, :red) }
50
+
51
+ init_or_clear()
52
+ end
53
+
54
+ def process_response(response_obj)
55
+ MESSAGE_TYPES.each do |msg_type|
56
+ msg = response_obj[msg_type.to_s] || response_obj[msg_type]
57
+ @queue[msg_type] << msg if msg
58
+ end
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,39 @@
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
+ class Thor
19
+ class Options < Arguments #:nodoc:
20
+
21
+ protected
22
+
23
+ def parse_boolean(switch)
24
+ if current_is_value?
25
+ if ["true", "TRUE", "t", "T", true].include?(peek)
26
+ # shift
27
+ true
28
+ elsif ["false", "FALSE", "f", "F", false].include?(peek)
29
+ # shift
30
+ true
31
+ else
32
+ true
33
+ end
34
+ else
35
+ @switches.key?(switch) || !no_or_skip?(switch)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,124 @@
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
+ # TODO-REMOVE: Check if we need this anymore
19
+
20
+ require 'thread'
21
+ require 'singleton'
22
+ require 'colorize'
23
+ #dtk_require('../commands')
24
+
25
+ # This singleton is used to check status on running processes on the server side
26
+ # Once certain task is complete it will give notice to user da certain task has been completed
27
+ # At the moment this only
28
+
29
+ module DTK
30
+ module Shell
31
+ class TaskStatusThread < Thread
32
+ attr_accessor :task_id, :finished, :status
33
+
34
+ def initialize
35
+ @finished = false
36
+ super
37
+ end
38
+ end
39
+
40
+ class StatusMonitor
41
+ include Singleton
42
+ include DTK::Client::CommandBase
43
+
44
+ THREAD_SLEEP_TIME = DTK::Configuration.get(:task_check_frequency)
45
+
46
+ def initialize
47
+ @threads = []
48
+ @finished_tasks = []
49
+ @conn = DTK::Client::Session.get_connection()
50
+ end
51
+
52
+ def self.start_monitoring(task_id)
53
+ self.instance.start_monitoring(task_id)
54
+ end
55
+
56
+ def self.check_status
57
+ self.instance.check_status
58
+ end
59
+
60
+ def check_status
61
+ @threads.each do |t|
62
+ if t.finished
63
+ @finished_tasks << t
64
+ end
65
+ end
66
+
67
+ # removes finished tasks from the main queue
68
+ @threads = @threads - @finished_tasks
69
+
70
+ @finished_tasks.each do |t|
71
+ puts ""
72
+ puts "[TASK NOTICE] Task with ID: #{t.task_id}, has finished with status: #{colorize_status(t.status)}"
73
+ end
74
+
75
+ @finished_tasks.clear
76
+ end
77
+
78
+ def start_monitoring(task_id)
79
+ puts "Client has started monitoring task [ID:#{task_id}]. You will be notified when task has been completed."
80
+ @threads << DTK::Shell::TaskStatusThread.new do
81
+ begin
82
+ response, post_hash_body = nil, {}
83
+ post_hash_body[:task_id] = task_id
84
+ DTK::Shell::TaskStatusThread.current.task_id = task_id
85
+
86
+ # pooling server for task status
87
+ while task_running?(response)
88
+ sleep(THREAD_SLEEP_TIME) unless response.nil?
89
+ response = post rest_url("task/status"),post_hash_body
90
+ # we break if there is error in response
91
+ break unless response.ok?
92
+ end
93
+
94
+ DTK::Shell::TaskStatusThread.current.finished = true
95
+
96
+ if response.ok?
97
+ DTK::Shell::TaskStatusThread.current.status = response.data['status'].upcase
98
+ else
99
+ DTK::Shell::TaskStatusThread.current.status = "RESPONSE NOT OK, RESPONSE: #{response}"
100
+ end
101
+
102
+ rescue Exception => e
103
+ DtkLogger.instance.error_pp("[THREAD ERROR] Error getting task status with message: #{e.message}", e.backtrace)
104
+ end
105
+ end
106
+ end
107
+
108
+ private
109
+
110
+ def colorize_status(status)
111
+ color = status.eql?('FAILED') ? :red : :green
112
+ return DTK::Client::OsUtil.colorize(status, color)
113
+ end
114
+
115
+ # return true if status SUCCEDED, FAILED
116
+ # returns false if status EXECUTING
117
+ def task_running?(response)
118
+ return true if response.nil?
119
+ return !(response.data['status'].eql?("succeeded") || response.data['status'].eql?("failed"))
120
+ end
121
+
122
+ end
123
+ end
124
+ end