dtk-client 0.9.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (310) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -0
  3. data/Gemfile +1 -3
  4. data/README.md +2 -121
  5. data/Rakefile +44 -0
  6. data/bin/dtk +10 -88
  7. data/dtk-client.gemspec +21 -44
  8. data/examples/simple/dtk.module.yaml +32 -0
  9. data/{lib/git-logs/git.log → examples/simple/test/README.md} +0 -0
  10. data/examples/spark/dtk.module.yaml +120 -0
  11. data/examples/with_repo_content/deploy/puppet/manifests/hostname.pp +54 -0
  12. data/examples/with_repo_content/dtk.module.yaml +49 -0
  13. data/examples/with_repo_content/test/README.txt +0 -0
  14. data/features/dtk.feature +8 -0
  15. data/features/step_definitions/dtk_steps.rb +6 -0
  16. data/features/support/env.rb +15 -0
  17. data/lib/cli/command/mixin.rb +101 -0
  18. data/lib/cli/command/module/clone.rb +42 -0
  19. data/lib/cli/command/module/install.rb +52 -0
  20. data/lib/cli/command/module/list.rb +31 -0
  21. data/lib/cli/command/module/list_assemblies.rb +39 -0
  22. data/lib/cli/command/module/push.rb +34 -0
  23. data/lib/cli/command/module/uninstall.rb +46 -0
  24. data/lib/cli/command/module.rb +33 -0
  25. data/lib/cli/command/options.rb +33 -0
  26. data/lib/cli/command/service/cancel_task.rb +33 -0
  27. data/lib/cli/command/service/converge.rb +37 -0
  28. data/lib/{parser/adapters/thor/common_option_defs.rb → cli/command/service/create_workspace.rb} +13 -17
  29. data/lib/cli/command/service/destroy.rb +47 -0
  30. data/lib/cli/command/service/edit.rb +47 -0
  31. data/lib/cli/command/service/exec.rb +42 -0
  32. data/lib/cli/command/service/exec_sync.rb +53 -0
  33. data/lib/cli/command/service/list.rb +30 -0
  34. data/lib/cli/command/service/list_actions.rb +39 -0
  35. data/lib/cli/command/service/list_attributes.rb +44 -0
  36. data/lib/cli/command/service/list_component_links.rb +33 -0
  37. data/lib/cli/command/service/list_components.rb +39 -0
  38. data/lib/cli/command/service/list_dependent_modules.rb +33 -0
  39. data/lib/cli/command/service/list_nodes.rb +33 -0
  40. data/lib/cli/command/service/list_violations.rb +33 -0
  41. data/lib/cli/command/service/pull.rb +32 -0
  42. data/lib/cli/command/service/push.rb +37 -0
  43. data/lib/{shell/parse_monkey_patch.rb → cli/command/service/set_default_target.rb} +9 -17
  44. data/lib/cli/command/service/set_required_attributes.rb +32 -0
  45. data/lib/cli/command/service/ssh.rb +42 -0
  46. data/lib/cli/command/service/stage.rb +55 -0
  47. data/lib/cli/command/service/start.rb +33 -0
  48. data/lib/{shell/context_aux.rb → cli/command/service/stop.rb} +10 -23
  49. data/lib/cli/command/service/task_status.rb +39 -0
  50. data/lib/cli/command/service.rb +55 -0
  51. data/lib/{commands.rb → cli/command/subcommand.rb} +22 -25
  52. data/lib/{bundler_monkey_patch.rb → cli/command/token/arg.rb} +6 -6
  53. data/lib/cli/command/token/class_mixin.rb +96 -0
  54. data/lib/cli/command/token/flag.rb +42 -0
  55. data/lib/{execute/command/rest_call.rb → cli/command/token/mixin.rb} +23 -14
  56. data/lib/cli/command/token/switch.rb +35 -0
  57. data/lib/cli/command/token.rb +76 -0
  58. data/lib/cli/command.rb +46 -0
  59. data/lib/cli/context/attributes.rb +50 -0
  60. data/lib/cli/context/type/module.rb +34 -0
  61. data/lib/{commands/thor/state_change.rb → cli/context/type/service.rb} +11 -5
  62. data/lib/cli/context/type/top.rb +32 -0
  63. data/lib/cli/context/type.rb +42 -0
  64. data/lib/cli/context.rb +174 -0
  65. data/lib/cli/directory_parser/file_system.rb +105 -0
  66. data/lib/{execute/command.rb → cli/directory_parser.rb} +14 -17
  67. data/lib/{execute.rb → cli/file_obj.rb} +13 -9
  68. data/lib/cli/processor/plugin/gli.rb +70 -0
  69. data/lib/cli/processor.rb +60 -0
  70. data/lib/cli/runner/dtkn_access.rb +75 -0
  71. data/lib/cli/runner.rb +58 -0
  72. data/lib/cli/version.rb +8 -0
  73. data/lib/{config → client/config}/cacert.pem +0 -0
  74. data/lib/client/config/default.conf +20 -0
  75. data/lib/client/config.rb +106 -0
  76. data/lib/client/configurator.rb +182 -0
  77. data/lib/client/conn.rb +197 -0
  78. data/lib/client/content_generator.rb +127 -0
  79. data/lib/client/error/subclasses.rb +105 -0
  80. data/lib/client/error.rb +98 -0
  81. data/lib/client/git_repo/adapter/git_gem/error_handler.rb +70 -0
  82. data/lib/client/git_repo/adapter/git_gem.rb +179 -0
  83. data/lib/client/git_repo.rb +122 -0
  84. data/lib/client/logger.rb +136 -0
  85. data/lib/client/operation/account.rb +66 -0
  86. data/lib/client/operation/client_module_dir/git_repo.rb +286 -0
  87. data/lib/client/operation/client_module_dir.rb +138 -0
  88. data/lib/client/operation/module/clone_module.rb +52 -0
  89. data/lib/client/operation/module/install/common_module.rb +58 -0
  90. data/lib/client/operation/module/install/external_module.rb +122 -0
  91. data/lib/client/operation/module/install.rb +91 -0
  92. data/lib/client/operation/module/install_from_catalog.rb +76 -0
  93. data/lib/client/operation/module/list.rb +34 -0
  94. data/lib/client/operation/module/list_assemblies.rb +35 -0
  95. data/lib/client/operation/module/push.rb +62 -0
  96. data/lib/client/operation/module/uninstall.rb +44 -0
  97. data/lib/client/operation/module.rb +58 -0
  98. data/lib/{commands/common/thor/reparse.rb → client/operation/module_service_common.rb} +27 -16
  99. data/lib/client/operation/service/cancel_task.rb +29 -0
  100. data/lib/client/operation/service/commit_and_push.rb +128 -0
  101. data/lib/client/operation/service/converge.rb +50 -0
  102. data/lib/{util/common_util.rb → client/operation/service/create_workspace.rb} +13 -13
  103. data/lib/client/operation/service/destroy.rb +42 -0
  104. data/lib/client/operation/service/edit.rb +54 -0
  105. data/lib/client/operation/service/exec.rb +73 -0
  106. data/lib/client/operation/service/list.rb +32 -0
  107. data/lib/client/operation/service/list_actions.rb +34 -0
  108. data/lib/{commands/thor/dependency.rb → client/operation/service/list_attributes.rb} +16 -12
  109. data/lib/client/operation/service/list_component_links.rb +29 -0
  110. data/lib/{commands/common/thor/pull_clone_changes.rb → client/operation/service/list_components.rb} +13 -7
  111. data/lib/client/operation/service/list_dependent_modules.rb +29 -0
  112. data/lib/client/operation/service/list_nodes.rb +29 -0
  113. data/lib/{commands/thor/project.rb → client/operation/service/list_violations.rb} +8 -13
  114. data/lib/client/operation/service/pull.rb +37 -0
  115. data/lib/client/operation/service/set_default_target.rb +31 -0
  116. data/lib/client/operation/service/set_required_attributes.rb +41 -0
  117. data/lib/client/operation/service/ssh.rb +118 -0
  118. data/lib/client/operation/service/stage.rb +54 -0
  119. data/lib/client/operation/service/start.rb +29 -0
  120. data/lib/client/operation/service/stop.rb +30 -0
  121. data/lib/{task_status → client/operation/service/task_status}/refresh_mode.rb +15 -25
  122. data/lib/{task_status → client/operation/service/task_status}/snapshot_mode.rb +4 -5
  123. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/format.rb +1 -1
  124. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/hierarchical_task/result/action.rb +1 -1
  125. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/hierarchical_task/result/components.rb +1 -1
  126. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/hierarchical_task/result/node_level.rb +1 -1
  127. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/hierarchical_task/result.rb +4 -4
  128. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/hierarchical_task/steps/action.rb +1 -1
  129. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/hierarchical_task/steps/components.rb +1 -1
  130. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/hierarchical_task/steps/node_level.rb +1 -1
  131. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/hierarchical_task/steps.rb +4 -4
  132. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/hierarchical_task.rb +3 -3
  133. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/no_results.rb +1 -1
  134. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/render.rb +1 -1
  135. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/stage/render.rb +1 -1
  136. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/stage.rb +3 -3
  137. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/task_end.rb +1 -1
  138. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element/task_start.rb +1 -1
  139. data/lib/{task_status → client/operation/service/task_status}/stream_mode/element.rb +13 -13
  140. data/lib/{task_status → client/operation/service/task_status}/stream_mode.rb +5 -9
  141. data/lib/client/operation/service/task_status.rb +77 -0
  142. data/lib/client/operation/service.rb +54 -0
  143. data/lib/client/operation.rb +67 -0
  144. data/lib/client/operation_args.rb +40 -0
  145. data/lib/{execute/execute_context/result_store.rb → client/render/view/simple.rb} +15 -14
  146. data/lib/client/render/view/table/processor.rb +248 -0
  147. data/lib/client/render/view/table.rb +75 -0
  148. data/lib/client/render.rb +113 -0
  149. data/lib/client/response/error_handler.rb +97 -0
  150. data/lib/client/response/render_helper.rb +78 -0
  151. data/lib/client/response/subclasses.rb +62 -0
  152. data/lib/client/response.rb +83 -0
  153. data/lib/client/session.rb +62 -0
  154. data/lib/client/util/auxiliary.rb +34 -0
  155. data/lib/client/util/console.rb +81 -0
  156. data/lib/client/util/disk_cacher.rb +66 -0
  157. data/lib/client/util/dtk_path.rb +28 -0
  158. data/lib/{search_hash.rb → client/util/hash_with_optional_keys.rb} +29 -21
  159. data/lib/client/util/interactive_wizard.rb +84 -0
  160. data/lib/client/util/module_ref.rb +92 -0
  161. data/lib/client/util/os_util/print.rb +121 -0
  162. data/lib/client/util/os_util.rb +122 -0
  163. data/lib/{commands/thor/utils.rb → client/util/post_body.rb} +1 -1
  164. data/lib/{execute/error_usage.rb → client/util/query_string_hash.rb} +2 -2
  165. data/lib/client/util/remote_dependency.rb +67 -0
  166. data/lib/client/util/ssh_util.rb +89 -0
  167. data/lib/client/util/validation.rb +28 -0
  168. data/lib/client/util.rb +18 -0
  169. data/lib/{util/permission_util.rb → dtk_cli.rb} +10 -9
  170. data/lib/dtk_client.rb +18 -14
  171. data/test/default_test.rb +14 -0
  172. data/test/test_helper.rb +9 -0
  173. metadata +196 -307
  174. data/Gemfile_dev +0 -13
  175. data/bin/dtk-execute +0 -32
  176. data/bin/dtk-shell +0 -31
  177. data/lib/auxiliary.rb +0 -61
  178. data/lib/client.rb +0 -58
  179. data/lib/command_helper.rb +0 -33
  180. data/lib/command_helpers/git_repo/merge.rb +0 -153
  181. data/lib/command_helpers/git_repo.rb +0 -589
  182. data/lib/command_helpers/jenkins_client/config_xml.rb +0 -288
  183. data/lib/command_helpers/jenkins_client.rb +0 -106
  184. data/lib/command_helpers/service_importer.rb +0 -251
  185. data/lib/command_helpers/service_link.rb +0 -33
  186. data/lib/command_helpers/test_module_creator.rb +0 -69
  187. data/lib/command_helpers/test_module_templates/dtk.model.yaml.eruby +0 -10
  188. data/lib/command_helpers/test_module_templates/spec_helper.rb.eruby +0 -10
  189. data/lib/command_helpers/test_module_templates/temp_component_spec.rb.eruby +0 -5
  190. data/lib/commands/common/thor/access_control.rb +0 -133
  191. data/lib/commands/common/thor/action_result_handler.rb +0 -74
  192. data/lib/commands/common/thor/assembly_template.rb +0 -92
  193. data/lib/commands/common/thor/assembly_workspace.rb +0 -1638
  194. data/lib/commands/common/thor/base_command_helper.rb +0 -59
  195. data/lib/commands/common/thor/clone.rb +0 -82
  196. data/lib/commands/common/thor/common.rb +0 -88
  197. data/lib/commands/common/thor/common_base.rb +0 -49
  198. data/lib/commands/common/thor/create_target.rb +0 -70
  199. data/lib/commands/common/thor/edit.rb +0 -255
  200. data/lib/commands/common/thor/inventory_parser.rb +0 -98
  201. data/lib/commands/common/thor/list_diffs.rb +0 -128
  202. data/lib/commands/common/thor/module/import.rb +0 -215
  203. data/lib/commands/common/thor/module.rb +0 -1011
  204. data/lib/commands/common/thor/node.rb +0 -53
  205. data/lib/commands/common/thor/poller.rb +0 -65
  206. data/lib/commands/common/thor/pull_from_remote.rb +0 -152
  207. data/lib/commands/common/thor/puppet_forge.rb +0 -72
  208. data/lib/commands/common/thor/purge_clone.rb +0 -101
  209. data/lib/commands/common/thor/push_clone_changes.rb +0 -162
  210. data/lib/commands/common/thor/push_to_remote.rb +0 -94
  211. data/lib/commands/common/thor/remotes.rb +0 -71
  212. data/lib/commands/common/thor/set_required_attributes.rb +0 -46
  213. data/lib/commands/thor/account.rb +0 -239
  214. data/lib/commands/thor/assembly.rb +0 -361
  215. data/lib/commands/thor/attribute.rb +0 -79
  216. data/lib/commands/thor/component.rb +0 -70
  217. data/lib/commands/thor/component_module.rb +0 -501
  218. data/lib/commands/thor/component_template.rb +0 -174
  219. data/lib/commands/thor/developer.rb +0 -144
  220. data/lib/commands/thor/dtk.rb +0 -152
  221. data/lib/commands/thor/library.rb +0 -125
  222. data/lib/commands/thor/node.rb +0 -504
  223. data/lib/commands/thor/node_group.rb +0 -203
  224. data/lib/commands/thor/node_template.rb +0 -94
  225. data/lib/commands/thor/provider.rb +0 -233
  226. data/lib/commands/thor/remotes.rb +0 -49
  227. data/lib/commands/thor/service.rb +0 -932
  228. data/lib/commands/thor/service_module.rb +0 -900
  229. data/lib/commands/thor/target.rb +0 -250
  230. data/lib/commands/thor/task.rb +0 -116
  231. data/lib/commands/thor/test_module.rb +0 -310
  232. data/lib/commands/thor/workspace.rb +0 -698
  233. data/lib/config/client.conf.header +0 -20
  234. data/lib/config/configuration.rb +0 -99
  235. data/lib/config/default.conf +0 -16
  236. data/lib/config/disk_cacher.rb +0 -80
  237. data/lib/configurator.rb +0 -176
  238. data/lib/context_router.rb +0 -44
  239. data/lib/core.rb +0 -489
  240. data/lib/domain/git_adapter.rb +0 -412
  241. data/lib/domain/git_error_handler.rb +0 -64
  242. data/lib/domain/response/error_handler.rb +0 -86
  243. data/lib/domain/response.rb +0 -285
  244. data/lib/dtk-client/version.rb +0 -20
  245. data/lib/dtk_constants.rb +0 -40
  246. data/lib/dtk_error.rb +0 -114
  247. data/lib/dtk_logger.rb +0 -113
  248. data/lib/error.rb +0 -85
  249. data/lib/execute/cli_pure/cli_rerouter.rb +0 -102
  250. data/lib/execute/command/api_call/map.rb +0 -60
  251. data/lib/execute/command/api_call/service.rb +0 -91
  252. data/lib/execute/command/api_call/translation_term.rb +0 -119
  253. data/lib/execute/command/api_call.rb +0 -60
  254. data/lib/execute/command_processor/rest_call.rb +0 -59
  255. data/lib/execute/command_processor.rb +0 -30
  256. data/lib/execute/execute_context.rb +0 -86
  257. data/lib/execute/script/add_tenant.rb +0 -121
  258. data/lib/execute/script.rb +0 -64
  259. data/lib/parser/adapters/option_parser.rb +0 -70
  260. data/lib/parser/adapters/thor.rb +0 -555
  261. data/lib/require_first.rb +0 -104
  262. data/lib/shell/context.rb +0 -1064
  263. data/lib/shell/domain/active_context.rb +0 -186
  264. data/lib/shell/domain/context_entity.rb +0 -89
  265. data/lib/shell/domain/context_params.rb +0 -223
  266. data/lib/shell/domain/override_tasks.rb +0 -88
  267. data/lib/shell/domain/shadow_entity.rb +0 -76
  268. data/lib/shell/header_shell.rb +0 -44
  269. data/lib/shell/help_monkey_patch.rb +0 -283
  270. data/lib/shell/interactive_wizard.rb +0 -256
  271. data/lib/shell/message_queue.rb +0 -63
  272. data/lib/shell/status_monitor.rb +0 -124
  273. data/lib/shell.rb +0 -261
  274. data/lib/task_status.rb +0 -83
  275. data/lib/util/console.rb +0 -235
  276. data/lib/util/dtk_puppet.rb +0 -65
  277. data/lib/util/module_util.rb +0 -66
  278. data/lib/util/os_util.rb +0 -385
  279. data/lib/util/remote_dependency_util.rb +0 -84
  280. data/lib/util/ssh_util.rb +0 -94
  281. data/lib/view_processor/augmented_simple_list.rb +0 -44
  282. data/lib/view_processor/hash_pretty_print.rb +0 -123
  283. data/lib/view_processor/simple_list.rb +0 -156
  284. data/lib/view_processor/table_print.rb +0 -309
  285. data/lib/view_processor.rb +0 -129
  286. data/puppet/manifests/init.pp +0 -72
  287. data/puppet/manifests/params.pp +0 -16
  288. data/puppet/r8meta.puppet.yml +0 -35
  289. data/puppet/templates/bash_profile.erb +0 -2
  290. data/puppet/templates/client.conf.erb +0 -1
  291. data/puppet/templates/dtkclient.erb +0 -2
  292. data/spec/component_module_spec.rb +0 -34
  293. data/spec/dependency_spec.rb +0 -6
  294. data/spec/dtk_shell_spec.rb +0 -13
  295. data/spec/dtk_spec.rb +0 -33
  296. data/spec/lib/spec_helper.rb +0 -10
  297. data/spec/lib/spec_thor.rb +0 -108
  298. data/spec/node_template_spec.rb +0 -24
  299. data/spec/project_spec.rb +0 -6
  300. data/spec/repo_spec.rb +0 -7
  301. data/spec/response_spec.rb +0 -52
  302. data/spec/service_module_spec.rb +0 -38
  303. data/spec/service_spec.rb +0 -50
  304. data/spec/state_change_spec.rb +0 -7
  305. data/spec/table_print_spec.rb +0 -48
  306. data/spec/target_spec.rb +0 -57
  307. data/spec/task_spec.rb +0 -28
  308. data/views/assembly/augmented_simple_list.rb +0 -12
  309. data/views/assembly_template/augmented_simple_list.rb +0 -12
  310. data/views/list_task/augmented_simple_list.rb +0 -12
data/lib/core.rb DELETED
@@ -1,489 +0,0 @@
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: user common utils in DTK::Common::Rest
19
-
20
- require 'rubygems'
21
- require 'singleton'
22
- require 'restclient'
23
- require 'colorize'
24
- require 'json'
25
- require 'pp'
26
-
27
- # Development Gems
28
- if ::DTK::Configuration.get(:development_mode)
29
- require 'ap'
30
- require 'looksee'
31
- end
32
-
33
- #TODO: for testing; fix by pass in commadn line argument
34
- #RestClient.log = STDOUT
35
-
36
- dtk_require_from_base('domain/response')
37
- dtk_require_from_base('util/os_util')
38
- dtk_require_from_base('util/ssh_util')
39
- dtk_require_from_base('util/common_util')
40
- dtk_require_from_base('util/permission_util')
41
- dtk_require_from_base('util/remote_dependency_util')
42
- dtk_require_from_base('util/module_util')
43
- dtk_require_from_base('shell/message_queue')
44
-
45
- dtk_require("config/configuration")
46
-
47
- def top_level_execute(entity_name, method_name, context_params=nil, options_args=nil, shell_execute=false)
48
- begin
49
- top_level_execute_core(entity_name, method_name, context_params, options_args, shell_execute)
50
- rescue DTK::Client::DtkLoginRequiredError
51
- # re-logging user and repeating request
52
- DTK::Client::OsUtil.print("Session expired: re-establishing session & repeating given task", :yellow)
53
- DTK::Client::Session.re_initialize
54
- top_level_execute_core(entity_name, method_name, context_params, options_args, shell_execute)
55
- end
56
- end
57
-
58
- def top_level_execute_core(entity_name, method_name, context_params=nil, options_args=nil, shell_execute=false)
59
- extend DTK::Client::OsUtil
60
-
61
- entity_class = nil
62
-
63
- begin
64
- include DTK::Client::Auxiliary
65
-
66
- entity_name = entity_name.gsub("-","_")
67
- load_command(entity_name)
68
- conn = DTK::Client::Session.get_connection
69
-
70
- # if connection parameters are not set up properly then don't execute any command
71
- return if validate_connection(conn)
72
-
73
- # call proper thor class and task
74
- entity_class = DTK::Client.const_get "#{cap_form(entity_name)}"
75
-
76
- # call forwarding, in case there is no task for given entity we switch to last (n-context) and try than
77
- unless (entity_class.task_names.include?(method_name))
78
- entity_class = DTK::Client.const_get "#{cap_form(context_params.last_entity_name.to_s)}"
79
- end
80
-
81
- response_ruby_obj = entity_class.execute_from_cli(conn,method_name,context_params,options_args,shell_execute)
82
-
83
- # it will raise DTK::Client::Error in case of error response
84
- print_method_response!(response_ruby_obj)
85
-
86
- # process/print queued message from server
87
- DTK::Shell::MessageQueue.print_messages
88
-
89
- rescue DTK::Client::DtkLoginRequiredError => e
90
- # this error is handled in method above
91
- raise e
92
- rescue DTK::Client::DSLParsing => e
93
- DTK::Client::OsUtil.print(e.message, :red)
94
- rescue DTK::Client::DtkValidationError => e
95
- validation_message = e.message
96
-
97
- # if !e.skip_usage_info && entity_class && method_name
98
- # usage_info = entity_class.get_usage_info(entity_name, method_name)
99
- # validation_message += ", usage: #{usage_info}"
100
- # end
101
-
102
- if e.display_usage_info && entity_class && method_name
103
- usage_info = entity_class.get_usage_info(entity_name, method_name)
104
- validation_message += ", usage: #{usage_info}"
105
-
106
- validation_message.gsub!("^^", '') if validation_message.include?("^^")
107
- validation_message.gsub!("HIDE_FROM_BASE ", '') if validation_message.include?("HIDE_FROM_BASE")
108
- end
109
-
110
- DTK::Client::OsUtil.print(validation_message, :yellow)
111
- rescue DTK::Client::DtkError => e
112
- # this are expected application errors
113
- DtkLogger.instance.error_pp(e.message, e.backtrace)
114
- rescue Exception => e
115
- client_internal_error = DTK::Client::DtkError::Client.label()
116
- DtkLogger.instance.fatal_pp("[#{client_internal_error}] DTK has encountered an error #{e.class}: #{e.message}", e.backtrace)
117
- end
118
- end
119
-
120
- def print_method_response!(response_ruby_obj)
121
- # this will raise error if found
122
- DTK::Client::ResponseErrorHandler.check(response_ruby_obj)
123
-
124
- # this will find appropriate render adapter and give output, returns boolean
125
- if print = response_ruby_obj.render_data
126
- print = [print] unless print.kind_of?(Array)
127
- print.each do |el|
128
- if el.kind_of?(String)
129
- el.each_line{|l| STDOUT << l}
130
- else
131
- PP.pp(el,STDOUT)
132
- end
133
- end
134
- end
135
- end
136
-
137
- def load_command(command_name)
138
- parser_adapter = DTK::Client::Config[:cli_parser] || "thor"
139
-
140
- dtk_nested_require("parser/adapters",parser_adapter)
141
- dtk_nested_require("commands/#{parser_adapter}",command_name)
142
- end
143
-
144
- # check if connection is set up properly
145
- def validate_connection(connection)
146
- if connection.connection_error?
147
- connection.print_warning
148
- puts "\nDTK will now exit. Please set up your connection properly and try again."
149
- return true
150
- end
151
-
152
- false
153
- end
154
-
155
- # check if .add_direct_access file exists, if not then add direct access and create .add_direct_access file
156
- def resolve_direct_access(params, config_exists=nil)
157
- return if params[:username_exists]
158
-
159
- puts "Processing ..." if config_exists
160
- # check to see if catalog credentials are set
161
- conn = DTK::Client::Session.get_connection
162
- response = conn.post DTK::Client::CommandBase.class, conn.rest_url("account/check_catalog_credentials"), {}
163
-
164
- # set catalog credentails
165
- if response.ok? && !response.data['catalog_credentials_set']
166
- # setting up catalog credentials
167
- catalog_creds = DTK::Client::Configurator.ask_catalog_credentials
168
- unless catalog_creds.empty?
169
- response = conn.post DTK::Client::CommandBase.class, conn.rest_url("account/set_catalog_credentials"), { :username => catalog_creds[:username], :password => catalog_creds[:password], :validate => true}
170
- if errors = response['errors']
171
- DTK::Client::OsUtil.print("#{errors.first['message']} You will have to set catalog credentials manually ('dtk account set-catalog-credentials').", :yellow)
172
- end
173
- end
174
- end
175
-
176
- # response = DTK::Client::Account.add_access(params[:ssh_key_path])
177
- response, matched_pub_key, matched_username = DTK::Client::Account.add_key(params[:ssh_key_path], true, "#{DTK::Client::Session.connection_username}-client")
178
-
179
- if !response.ok?
180
- DTK::Client::OsUtil.print("We were not able to add access for current user. #{response.error_message}. In order to properly use dtk-shell you will have to add access manually ('dtk account add-ssh-key').\n", :yellow)
181
- elsif matched_pub_key
182
- # message will be displayed by add key # TODO: Refactor this flow
183
- DTK::Client::OsUtil.print("Provided SSH PUB key has already been added.", :yellow)
184
- DTK::Client::Configurator.add_current_user_to_direct_access
185
- elsif matched_username
186
- DTK::Client::OsUtil.print("User with provided name already exists.", :yellow)
187
- else
188
- # commented out because 'add_key' method called above will also print the same message
189
- # DTK::Client::OsUtil.print("Your SSH PUB key has been successfully added.", :yellow)
190
- DTK::Client::Configurator.add_current_user_to_direct_access
191
- end
192
-
193
- response
194
- end
195
-
196
- module DTK
197
- module Client
198
- class ResponseErrorHandler
199
- class << self
200
-
201
- def check_for_session_expiried(response)
202
- error_code = nil
203
- if response && response['errors']
204
- response['errors'].each do |err|
205
- error_code = err["code"]||(err["errors"] && err["errors"].first["code"])
206
- end
207
- end
208
-
209
- return (error_code == "forbidden")
210
- end
211
-
212
- def check(response)
213
- DtkError.raise_if_error?(response)
214
- end
215
- end
216
- end
217
-
218
- class Log
219
- #TODO Stubs
220
- def self.info(msg)
221
- pp "info: #{msg}"
222
- end
223
- def self.error(msg)
224
- pp "error: #{msg}"
225
- end
226
- end
227
-
228
- module ParseFile
229
-
230
- def parse_key_value_file(file)
231
- DTK::Client::Configurator.parse_key_value_file(file)
232
- end
233
-
234
- end
235
- class Config < Hash
236
- include Singleton
237
- include ParseFile
238
- dtk_require_from_base('configurator')
239
-
240
- CONFIG_FILE = ::DTK::Client::Configurator::CONFIG_FILE
241
- CRED_FILE = ::DTK::Client::Configurator::CRED_FILE
242
-
243
- REQUIRED_KEYS = [:server_host]
244
-
245
- def self.[](k)
246
- Config.instance[k]
247
- end
248
- private
249
- def initialize
250
- set_defaults
251
- load_config_file
252
- validate
253
- end
254
- def set_defaults
255
- self[:server_port] = 80
256
- self[:assembly_module_base_location] = 'assemblies'
257
- self[:secure_connection] = true
258
- self[:secure_connection_server_port] = 443
259
- end
260
-
261
- def load_config_file
262
- parse_key_value_file(CONFIG_FILE).each{|k,v|self[k]=v}
263
- end
264
-
265
- def validate
266
- #TODO: need to check for legal values
267
- missing_keys = REQUIRED_KEYS - keys
268
- raise DTK::Client::DtkError, "Missing config keys (#{missing_keys.join(",")}). Please check your configuration file #{CONFIG_FILE} for required keys!" unless missing_keys.empty?
269
- end
270
-
271
- end
272
-
273
-
274
- ##
275
- # Session Singleton we will use to hold connection instance, just a singleton wrapper.
276
- # During shell input it will be needed only once, so singleton was obvious solution.
277
- #
278
- class Session
279
- include Singleton
280
-
281
- attr_accessor :conn
282
-
283
- def initialize
284
- @conn = DTK::Client::Conn.new
285
- end
286
-
287
- def self.get_connection
288
- Session.instance.conn
289
- end
290
-
291
- def self.connection_username
292
- Session.instance.conn.get_username
293
- end
294
-
295
- def self.re_initialize
296
- Session.instance.conn = nil
297
- Session.instance.conn = DTK::Client::Conn.new
298
- Session.instance.conn.cookies
299
- end
300
-
301
- def self.logout
302
- # from this point @conn is not valid, since there are no cookies set
303
- Session.instance.conn.logout
304
- end
305
- end
306
-
307
- class Conn
308
- def initialize
309
- @cookies = Hash.new
310
- @connection_error = nil
311
- login
312
- end
313
-
314
- VERBOSE_MODE_ON = ::DTK::Configuration.get(:verbose_rest_calls)
315
-
316
- attr_reader :connection_error, :cookies
317
-
318
- if VERBOSE_MODE_ON
319
- require 'ap'
320
- end
321
-
322
- def self.get_timeout
323
- DefaultRestOpts[:timeout]
324
- end
325
-
326
- def self.set_timeout(timeout_sec)
327
- DefaultRestOpts[:timeout] = timeout_sec
328
- end
329
-
330
- def get_username
331
- get_credentials[:username]
332
- end
333
-
334
- def rest_url(route=nil)
335
- protocol, port = "http", Config[:server_port].to_s
336
- protocol, port = "https", Config[:secure_connection_server_port].to_s if Config[:secure_connection] == "true"
337
-
338
- "#{protocol}://#{Config[:server_host]}:#{port}/rest/#{route}"
339
- end
340
-
341
- def get(command_class,url)
342
- ap "GET #{url}" if VERBOSE_MODE_ON
343
-
344
- check_and_wrap_response(command_class, Proc.new { json_parse_if_needed(get_raw(url)) })
345
- end
346
-
347
- def post(command_class,url,body=nil)
348
- if VERBOSE_MODE_ON
349
- ap "POST (REST) #{url}"
350
- ap "params: "
351
- ap body
352
- end
353
-
354
- check_and_wrap_response(command_class, Proc.new { json_parse_if_needed(post_raw(url,body)) })
355
- end
356
-
357
- def post_file(command_class,url,body=nil)
358
- if VERBOSE_MODE_ON
359
- ap "POST (FILE) #{url}"
360
- ap "params: "
361
- ap body
362
- end
363
-
364
- check_and_wrap_response(command_class, Proc.new { json_parse_if_needed(post_raw(url,body,{:content_type => 'avro/binary'})) })
365
- end
366
-
367
- # method will repeat request in case session has expired
368
- def check_and_wrap_response(command_class, rest_method_func)
369
- response = rest_method_func.call
370
-
371
- if ResponseErrorHandler.check_for_session_expiried(response)
372
- # re-logging user and repeating request
373
- DTK::Client::OsUtil.print("Session expired: re-establishing session & re-trying request ...", :yellow)
374
- @cookies = DTK::Client::Session.re_initialize
375
- response = rest_method_func.call
376
- end
377
-
378
- response_obj = Response.new(command_class, response)
379
-
380
- # queue messages from server to be displayed later
381
- DTK::Shell::MessageQueue.process_response(response_obj)
382
-
383
- response_obj
384
- end
385
-
386
-
387
-
388
- def connection_error?
389
- return !@connection_error.nil?
390
- end
391
-
392
- def logout
393
- response = get_raw rest_url("user/process_logout")
394
-
395
- # save cookies - no need to persist them
396
- # DiskCacher.new.save_cookie(@cookies)
397
-
398
- raise DTK::Client::DtkError, "Failed to logout, and terminate session!" unless response
399
- @cookies = nil
400
- end
401
-
402
- ##
403
- # Method will warn user that connection could not be established. User should check configuration
404
- # to make sure that connection is properly set.
405
- #
406
- def print_warning
407
- creds = get_credentials
408
- puts "[WARNING] Unable to connect to server, please check you configuration."
409
- puts "========================== Configuration =========================="
410
- printf "%15s %s\n", "REST endpoint:", rest_url
411
- printf "%15s %s\n", "Username:", "#{creds[:username]}"
412
- printf "%15s %s\n", "Password:", "#{creds[:password] ? creds[:password].gsub(/./,'*') : 'No password set'}"
413
- puts "==================================================================="
414
-
415
- if self.connection_error['errors'].first['errors']
416
- error_code = self.connection_error['errors'].first['errors'].first['code']
417
- print " Error code: "
418
- DTK::Client::OsUtil.print(error_code, :red)
419
- end
420
- end
421
-
422
- private
423
-
424
- include ParseFile
425
-
426
- def login
427
- creds = get_credentials
428
- response = post_raw rest_url("user/process_login"),creds
429
- errors = response['errors']
430
-
431
- if response.kind_of?(Common::Response) and not response.ok?
432
- if (errors && errors.first['code']=="pg_error")
433
- DTK::Client::OsUtil.print(errors.first['message'].gsub!("403 Forbidden", "[PG_ERROR]"), :red)
434
- exit
435
- end
436
- @connection_error = response
437
- else
438
- @cookies = response.cookies
439
- end
440
- end
441
-
442
- def set_credentials(username, password)
443
- @parsed_credentials = { :username => username, :password => password}
444
- end
445
-
446
- def get_credentials
447
- unless @parsed_credentials
448
- cred_file = Config::CRED_FILE
449
- raise DTK::Client::DtkError,"Authorization configuration file (#{cred_file}) does not exist" unless File.exists?(cred_file)
450
- ret = parse_key_value_file(cred_file)
451
- [:username,:password].each{ |k| raise DTK::Client::DtkError, "cannot find #{k}" unless ret[k] }
452
- @parsed_credentials = ret
453
- end
454
-
455
- @parsed_credentials
456
- end
457
-
458
- ####
459
- RestClientWrapper = Common::Response::RestClientWrapper
460
-
461
- # In development mode we want bigger timeout allowing us to debbug on server while still
462
- # keeping connection alive and receivinga response
463
- if ::DTK::Configuration.get(:development_mode)
464
- DefaultRestOpts = {:timeout => 2000, :open_timeout => 10, :error_response_class => Client::Response::Error}
465
- # DefaultRestOpts = {:timeout => 50, :open_timeout => 2, :error_response_class => Client::Response::Error}
466
- else
467
- DefaultRestOpts = {:timeout => 150, :open_timeout => 10, :error_response_class => Client::Response::Error}
468
- end
469
-
470
- # enable SSL verification
471
- DefaultRestOpts.merge!(:verify_ssl => OpenSSL::SSL::VERIFY_PEER)
472
- # Net:HTTP from Ruby 1.8.7 doesn't verify SSL certs correctly
473
- # this is a CA bundle downloaded from http://curl.haxx.se/docs/caextract.html,
474
- # and it will only be used for 1.8.7, otherwise the default (system) CA will be used
475
- DefaultRestOpts.merge!(:ssl_ca_file => File.expand_path('../lib/config/cacert.pem', File.dirname(__FILE__)))
476
-
477
- def get_raw(url)
478
- RestClientWrapper.get_raw(url, {}, DefaultRestOpts.merge(:cookies => @cookies))
479
- end
480
- def post_raw(url,body,params={})
481
- RestClientWrapper.post_raw(url, body, DefaultRestOpts.merge(:cookies => @cookies).merge(params))
482
- end
483
-
484
- def json_parse_if_needed(item)
485
- RestClientWrapper.json_parse_if_needed(item)
486
- end
487
- end
488
- end
489
- end