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.
- checksums.yaml +7 -0
- data/Gemfile +5 -0
- data/Gemfile_dev +13 -0
- data/README.md +121 -0
- data/bin/dtk-execute +32 -0
- data/bin/dtk-run +92 -0
- data/bin/dtk-shell +31 -0
- data/dtk-shell.gemspec +50 -0
- data/lib/auxiliary.rb +61 -0
- data/lib/bundler_monkey_patch.rb +26 -0
- data/lib/client.rb +58 -0
- data/lib/command_helper.rb +33 -0
- data/lib/command_helpers/git_repo.rb +589 -0
- data/lib/command_helpers/git_repo/merge.rb +153 -0
- data/lib/command_helpers/jenkins_client.rb +106 -0
- data/lib/command_helpers/jenkins_client/config_xml.rb +288 -0
- data/lib/command_helpers/service_importer.rb +251 -0
- data/lib/command_helpers/service_link.rb +33 -0
- data/lib/command_helpers/test_module_creator.rb +69 -0
- data/lib/command_helpers/test_module_templates/dtk.model.yaml.eruby +10 -0
- data/lib/command_helpers/test_module_templates/spec_helper.rb.eruby +10 -0
- data/lib/command_helpers/test_module_templates/temp_component_spec.rb.eruby +5 -0
- data/lib/commands.rb +57 -0
- data/lib/commands/common/thor/access_control.rb +133 -0
- data/lib/commands/common/thor/action_result_handler.rb +74 -0
- data/lib/commands/common/thor/assembly_template.rb +92 -0
- data/lib/commands/common/thor/assembly_workspace.rb +1801 -0
- data/lib/commands/common/thor/base_command_helper.rb +59 -0
- data/lib/commands/common/thor/clone.rb +82 -0
- data/lib/commands/common/thor/common.rb +88 -0
- data/lib/commands/common/thor/common_base.rb +49 -0
- data/lib/commands/common/thor/create_target.rb +70 -0
- data/lib/commands/common/thor/edit.rb +255 -0
- data/lib/commands/common/thor/inventory_parser.rb +98 -0
- data/lib/commands/common/thor/list_diffs.rb +128 -0
- data/lib/commands/common/thor/module.rb +1011 -0
- data/lib/commands/common/thor/module/import.rb +210 -0
- data/lib/commands/common/thor/node.rb +53 -0
- data/lib/commands/common/thor/poller.rb +65 -0
- data/lib/commands/common/thor/pull_clone_changes.rb +28 -0
- data/lib/commands/common/thor/pull_from_remote.rb +152 -0
- data/lib/commands/common/thor/puppet_forge.rb +72 -0
- data/lib/commands/common/thor/purge_clone.rb +101 -0
- data/lib/commands/common/thor/push_clone_changes.rb +162 -0
- data/lib/commands/common/thor/push_to_remote.rb +94 -0
- data/lib/commands/common/thor/remotes.rb +71 -0
- data/lib/commands/common/thor/reparse.rb +40 -0
- data/lib/commands/common/thor/set_required_attributes.rb +46 -0
- data/lib/commands/thor/account.rb +239 -0
- data/lib/commands/thor/assembly.rb +356 -0
- data/lib/commands/thor/attribute.rb +79 -0
- data/lib/commands/thor/component.rb +70 -0
- data/lib/commands/thor/component_module.rb +501 -0
- data/lib/commands/thor/component_template.rb +174 -0
- data/lib/commands/thor/dependency.rb +34 -0
- data/lib/commands/thor/developer.rb +144 -0
- data/lib/commands/thor/dtk.rb +152 -0
- data/lib/commands/thor/library.rb +125 -0
- data/lib/commands/thor/node.rb +504 -0
- data/lib/commands/thor/node_template.rb +94 -0
- data/lib/commands/thor/project.rb +34 -0
- data/lib/commands/thor/provider.rb +233 -0
- data/lib/commands/thor/remotes.rb +49 -0
- data/lib/commands/thor/service.rb +941 -0
- data/lib/commands/thor/service_module.rb +914 -0
- data/lib/commands/thor/state_change.rb +25 -0
- data/lib/commands/thor/target.rb +250 -0
- data/lib/commands/thor/task.rb +116 -0
- data/lib/commands/thor/test_module.rb +310 -0
- data/lib/commands/thor/utils.rb +21 -0
- data/lib/commands/thor/workspace.rb +685 -0
- data/lib/config/cacert.pem +3785 -0
- data/lib/config/client.conf.header +20 -0
- data/lib/config/configuration.rb +99 -0
- data/lib/config/default.conf +16 -0
- data/lib/config/disk_cacher.rb +80 -0
- data/lib/configurator.rb +176 -0
- data/lib/context_router.rb +44 -0
- data/lib/core.rb +497 -0
- data/lib/domain/git_adapter.rb +412 -0
- data/lib/domain/git_error_handler.rb +64 -0
- data/lib/domain/response.rb +285 -0
- data/lib/domain/response/error_handler.rb +86 -0
- data/lib/dtk-shell/version.rb +20 -0
- data/lib/dtk_constants.rb +40 -0
- data/lib/dtk_error.rb +114 -0
- data/lib/dtk_logger.rb +126 -0
- data/lib/dtk_shell.rb +31 -0
- data/lib/error.rb +85 -0
- data/lib/execute.rb +29 -0
- data/lib/execute/cli_pure/cli_rerouter.rb +102 -0
- data/lib/execute/command.rb +40 -0
- data/lib/execute/command/api_call.rb +60 -0
- data/lib/execute/command/api_call/map.rb +60 -0
- data/lib/execute/command/api_call/service.rb +91 -0
- data/lib/execute/command/api_call/translation_term.rb +119 -0
- data/lib/execute/command/rest_call.rb +37 -0
- data/lib/execute/command_processor.rb +30 -0
- data/lib/execute/command_processor/rest_call.rb +59 -0
- data/lib/execute/error_usage.rb +21 -0
- data/lib/execute/execute_context.rb +86 -0
- data/lib/execute/execute_context/result_store.rb +37 -0
- data/lib/execute/script.rb +64 -0
- data/lib/execute/script/add_tenant.rb +121 -0
- data/lib/git-logs/git.log +0 -0
- data/lib/parser/adapters/option_parser.rb +70 -0
- data/lib/parser/adapters/thor.rb +555 -0
- data/lib/parser/adapters/thor/common_option_defs.rb +40 -0
- data/lib/require_first.rb +104 -0
- data/lib/search_hash.rb +44 -0
- data/lib/shell.rb +261 -0
- data/lib/shell/context.rb +1065 -0
- data/lib/shell/context_aux.rb +46 -0
- data/lib/shell/domain/active_context.rb +186 -0
- data/lib/shell/domain/context_entity.rb +89 -0
- data/lib/shell/domain/context_params.rb +223 -0
- data/lib/shell/domain/override_tasks.rb +88 -0
- data/lib/shell/domain/shadow_entity.rb +76 -0
- data/lib/shell/header_shell.rb +44 -0
- data/lib/shell/help_monkey_patch.rb +283 -0
- data/lib/shell/interactive_wizard.rb +225 -0
- data/lib/shell/message_queue.rb +63 -0
- data/lib/shell/parse_monkey_patch.rb +39 -0
- data/lib/shell/status_monitor.rb +124 -0
- data/lib/task_status.rb +83 -0
- data/lib/task_status/refresh_mode.rb +77 -0
- data/lib/task_status/snapshot_mode.rb +28 -0
- data/lib/task_status/stream_mode.rb +48 -0
- data/lib/task_status/stream_mode/element.rb +101 -0
- data/lib/task_status/stream_mode/element/format.rb +101 -0
- data/lib/task_status/stream_mode/element/hierarchical_task.rb +100 -0
- data/lib/task_status/stream_mode/element/hierarchical_task/result.rb +72 -0
- data/lib/task_status/stream_mode/element/hierarchical_task/result/action.rb +93 -0
- data/lib/task_status/stream_mode/element/hierarchical_task/result/components.rb +26 -0
- data/lib/task_status/stream_mode/element/hierarchical_task/result/node_level.rb +26 -0
- data/lib/task_status/stream_mode/element/hierarchical_task/steps.rb +34 -0
- data/lib/task_status/stream_mode/element/hierarchical_task/steps/action.rb +53 -0
- data/lib/task_status/stream_mode/element/hierarchical_task/steps/components.rb +53 -0
- data/lib/task_status/stream_mode/element/hierarchical_task/steps/node_level.rb +42 -0
- data/lib/task_status/stream_mode/element/no_results.rb +26 -0
- data/lib/task_status/stream_mode/element/render.rb +59 -0
- data/lib/task_status/stream_mode/element/stage.rb +84 -0
- data/lib/task_status/stream_mode/element/stage/render.rb +76 -0
- data/lib/task_status/stream_mode/element/task_end.rb +35 -0
- data/lib/task_status/stream_mode/element/task_start.rb +37 -0
- data/lib/util/common_util.rb +37 -0
- data/lib/util/console.rb +235 -0
- data/lib/util/dtk_puppet.rb +65 -0
- data/lib/util/module_util.rb +66 -0
- data/lib/util/os_util.rb +385 -0
- data/lib/util/permission_util.rb +31 -0
- data/lib/util/remote_dependency_util.rb +84 -0
- data/lib/util/ssh_util.rb +94 -0
- data/lib/view_processor.rb +129 -0
- data/lib/view_processor/augmented_simple_list.rb +44 -0
- data/lib/view_processor/hash_pretty_print.rb +123 -0
- data/lib/view_processor/simple_list.rb +156 -0
- data/lib/view_processor/table_print.rb +309 -0
- data/lib/violation.rb +86 -0
- data/lib/violation/attribute.rb +76 -0
- data/lib/violation/fix.rb +26 -0
- data/lib/violation/fix/result.rb +73 -0
- data/lib/violation/fix/result/error.rb +34 -0
- data/lib/violation/fix/set_attribute.rb +41 -0
- data/lib/violation/sub_classes.rb +60 -0
- data/puppet/manifests/init.pp +72 -0
- data/puppet/manifests/params.pp +16 -0
- data/puppet/r8meta.puppet.yml +35 -0
- data/puppet/templates/bash_profile.erb +2 -0
- data/puppet/templates/client.conf.erb +1 -0
- data/puppet/templates/dtkclient.erb +2 -0
- data/spec/component_module_spec.rb +34 -0
- data/spec/dependency_spec.rb +6 -0
- data/spec/dtk_shell_spec.rb +13 -0
- data/spec/dtk_spec.rb +33 -0
- data/spec/lib/spec_helper.rb +10 -0
- data/spec/lib/spec_thor.rb +108 -0
- data/spec/node_template_spec.rb +24 -0
- data/spec/project_spec.rb +6 -0
- data/spec/repo_spec.rb +7 -0
- data/spec/response_spec.rb +52 -0
- data/spec/service_module_spec.rb +38 -0
- data/spec/service_spec.rb +50 -0
- data/spec/state_change_spec.rb +7 -0
- data/spec/table_print_spec.rb +48 -0
- data/spec/target_spec.rb +57 -0
- data/spec/task_spec.rb +28 -0
- data/views/assembly/augmented_simple_list.rb +12 -0
- data/views/assembly_template/augmented_simple_list.rb +12 -0
- data/views/list_task/augmented_simple_list.rb +12 -0
- metadata +421 -0
|
@@ -0,0 +1,31 @@
|
|
|
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
|
|
19
|
+
module Client
|
|
20
|
+
module PermissionUtil
|
|
21
|
+
class << self
|
|
22
|
+
def validate_permissions!(permission_string)
|
|
23
|
+
# matches example: u-rw, ugo+r, go+w
|
|
24
|
+
match = permission_string.match(/^[ugo]+[+\-][rwd]+$/)
|
|
25
|
+
raise DTK::Client::DtkValidationError, "Provided permission expression ('#{permission_string}') is not valid" unless match
|
|
26
|
+
permission_string
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
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
|
+
#
|
|
19
|
+
# Managment of remote dependencies detection (print warning) or providing data from local resources
|
|
20
|
+
#
|
|
21
|
+
module DTK
|
|
22
|
+
module Client
|
|
23
|
+
module RemoteDependencyUtil
|
|
24
|
+
MODULE_REF_FILE = 'module_refs.yaml'
|
|
25
|
+
|
|
26
|
+
class << self
|
|
27
|
+
def print_dependency_warnings(response, success_msg = nil, opts = {})
|
|
28
|
+
are_there_warnings = false
|
|
29
|
+
return are_there_warnings if response.nil? || response.data.nil?
|
|
30
|
+
|
|
31
|
+
warnings = response.data['dependency_warnings']
|
|
32
|
+
if warnings && !warnings.empty?
|
|
33
|
+
if opts[:ignore_permission_warnings]
|
|
34
|
+
warnings.delete_if { |warning| warning['error_type'].eql?('no_permission') }
|
|
35
|
+
return if warnings.empty?
|
|
36
|
+
end
|
|
37
|
+
print_out "Following warnings have been detected for current module by Repo Manager:\n"
|
|
38
|
+
warnings.each { |w| print_out(" - #{w['message']}") }
|
|
39
|
+
puts
|
|
40
|
+
are_there_warnings = true
|
|
41
|
+
end
|
|
42
|
+
print_out success_msg, :green if success_msg
|
|
43
|
+
are_there_warnings
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def check_permission_warnings(response)
|
|
47
|
+
errors = ''
|
|
48
|
+
dependency_warnings = response.data['dependency_warnings']
|
|
49
|
+
|
|
50
|
+
if dependency_warnings && !dependency_warnings.empty?
|
|
51
|
+
no_permissions = dependency_warnings.select { |warning| warning['error_type'].eql?('no_permission') }
|
|
52
|
+
|
|
53
|
+
unless no_permissions.empty?
|
|
54
|
+
errors << "\n\nYou do not have (R) permissions for modules:\n\n"
|
|
55
|
+
no_permissions.each { |np| errors << " - #{np['module_namespace']}:#{np['module_name']} (owner: #{np['module_owner']})\n" }
|
|
56
|
+
errors << "\nPlease contact owner(s) to change permissions for those modules."
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
raise DtkError, errors unless errors.empty?
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# check if all dependent modules are frozen; if they are don't display prompt for update
|
|
64
|
+
def check_for_frozen_modules(required_modules)
|
|
65
|
+
return true if required_modules.nil? || required_modules.empty?
|
|
66
|
+
|
|
67
|
+
modules_to_update = required_modules.select{ |md| (md['frozen'].nil? || md['frozen'] == false)}
|
|
68
|
+
return modules_to_update.empty?
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def module_ref_content(location)
|
|
72
|
+
abs_location = File.join(location, MODULE_REF_FILE)
|
|
73
|
+
File.exists?(abs_location) ? File.read(abs_location) : nil
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def print_out(message, color=:yellow)
|
|
79
|
+
DTK::Client::OsUtil.print(message, color)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
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
|
|
19
|
+
module Client
|
|
20
|
+
module SSHUtil
|
|
21
|
+
|
|
22
|
+
def self.read_and_validate_pub_key(path_to_pub_key)
|
|
23
|
+
is_ssh_key_path_valid?(path_to_pub_key)
|
|
24
|
+
rsa_pub_key = File.open(path_to_pub_key) { |f| f.read }
|
|
25
|
+
is_ssh_key_content_valid?(rsa_pub_key)
|
|
26
|
+
rsa_pub_key
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.update_ssh_known_hosts(server_dns,server_fingerprint)
|
|
30
|
+
known_hosts_path = ssh_known_hosts_path()
|
|
31
|
+
if File.file?(known_hosts_path)
|
|
32
|
+
`ssh-keygen -f #{known_hosts_path} -R #{server_dns} 2> /dev/null`
|
|
33
|
+
File.open(known_hosts_path,"a"){|f|f << server_fingerprint}
|
|
34
|
+
else
|
|
35
|
+
ssh_base_dir = ssh_base_dir()
|
|
36
|
+
unless File.directory?(ssh_base_dir)
|
|
37
|
+
Dir.mkdir(ssh_base_dir)
|
|
38
|
+
end
|
|
39
|
+
File.open(known_hosts_path,"w"){|f|f << server_fingerprint}
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.default_rsa_pub_key_path()
|
|
44
|
+
"#{ssh_base_dir()}/id_rsa.pub"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.rsa_pub_key_content()
|
|
48
|
+
path_to_key = self.default_rsa_pub_key_path()
|
|
49
|
+
unless File.file?(path_to_key)
|
|
50
|
+
raise DtkError,"No File found at (#{path_to_key}). Path is wrong or it is necessary to generate the public rsa key (e.g., run ssh-keygen -t rsa)"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
content = File.open(path_to_key){ |f| f.read }
|
|
54
|
+
content.chomp
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def self.ssh_reachable?(user, server, timeout = 3)
|
|
58
|
+
output = `ssh -o ConnectTimeout=#{timeout} -o \"StrictHostKeyChecking no\" -o \"UserKnownHostsFile /dev/null\" -q #{user}@#{server} exit; echo $?`
|
|
59
|
+
|
|
60
|
+
# if response 0 than it is able to connect
|
|
61
|
+
"0".eql?(output.strip())
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def self.ssh_base_dir()
|
|
68
|
+
"#{OsUtil.dtk_home_dir}/.ssh"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def self.ssh_known_hosts_path()
|
|
72
|
+
"#{ssh_base_dir()}/known_hosts"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def self.is_ssh_key_path_valid?(path_to_key)
|
|
76
|
+
unless path_to_key.include?(".pub")
|
|
77
|
+
raise DtkError, "[ERROR] Invalid public key file path (#{path_to_key}). Please provide valid path and try again."
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
unless File.exists?(path_to_key)
|
|
81
|
+
raise DtkError, "[ERROR] Not able to find provided key (#{path_to_key}). Please provide valid path and try again."
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def self.is_ssh_key_content_valid?(rsa_pub_key)
|
|
86
|
+
# checking know ssh rsa pub key content
|
|
87
|
+
if(rsa_pub_key.empty? || !rsa_pub_key.include?("AAAAB3NzaC1yc2EA"))
|
|
88
|
+
raise DtkError, "[ERROR] SSH public key (#{path_to_key}) does not have valid content. Please check your key and try again."
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,129 @@
|
|
|
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
|
|
19
|
+
module Client
|
|
20
|
+
module RenderView
|
|
21
|
+
SIMPLE_LIST = "simple_list"
|
|
22
|
+
TABLE = "table_print"
|
|
23
|
+
PRETTY_PRINT = "hash_pretty_print"
|
|
24
|
+
AUG_SIMPLE_LIST = "augmented_simple_list"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class ViewProcessor
|
|
28
|
+
class << self
|
|
29
|
+
include Auxiliary
|
|
30
|
+
def render(command_class, ruby_obj, type, data_type, adapter=nil, print_error_table=false)
|
|
31
|
+
adapter ||= get_adapter(type,command_class,data_type)
|
|
32
|
+
if type == RenderView::TABLE
|
|
33
|
+
# for table there is only one rendering, we use command class to
|
|
34
|
+
# determine output of the table
|
|
35
|
+
adapter.render(ruby_obj, command_class, data_type, nil, print_error_table)
|
|
36
|
+
|
|
37
|
+
# saying no additional print needed (see core class)
|
|
38
|
+
return false
|
|
39
|
+
elsif ruby_obj.kind_of?(Hash)
|
|
40
|
+
adapter.render(ruby_obj)
|
|
41
|
+
elsif ruby_obj.kind_of?(Array)
|
|
42
|
+
ruby_obj.map{|el|render(command_class,el,type,nil,adapter)}
|
|
43
|
+
elsif ruby_obj.kind_of?(String)
|
|
44
|
+
ruby_obj
|
|
45
|
+
else
|
|
46
|
+
raise Error.new("ruby_obj has unexepected type")
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def get_adapter(type,command_class,data_type=nil)
|
|
51
|
+
|
|
52
|
+
data_type_index = use_data_type_index?(command_class,data_type)
|
|
53
|
+
cached =
|
|
54
|
+
if data_type_index
|
|
55
|
+
((AdapterCacheAug[type]||{})[command_class]||{})[data_type_index]
|
|
56
|
+
else
|
|
57
|
+
(AdapterCache[type]||{})[command_class]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
return cached if cached
|
|
61
|
+
dtk_nested_require("view_processor",type)
|
|
62
|
+
klass = DTK::Client.const_get "ViewProc#{cap_form(type)}"
|
|
63
|
+
if data_type_index
|
|
64
|
+
AdapterCacheAug[type] ||= Hash.new
|
|
65
|
+
AdapterCacheAug[type][command_class] ||= Hash.new
|
|
66
|
+
AdapterCacheAug[type][command_class][data_type_index] = klass.new(type,command_class,data_type_index)
|
|
67
|
+
else
|
|
68
|
+
AdapterCache[type] ||= Hash.new
|
|
69
|
+
AdapterCache[type][command_class] = klass.new(type,command_class)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
AdapterCache = Hash.new
|
|
74
|
+
AdapterCacheAug = Hash.new
|
|
75
|
+
end
|
|
76
|
+
private
|
|
77
|
+
def initialize(type,command_class,data_type_index=nil)
|
|
78
|
+
@command_class = command_class
|
|
79
|
+
@data_type_index = data_type_index
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
#data_type_index is used if there is adata type passed and it is different than command_class defualt data type
|
|
83
|
+
def self.use_data_type_index?(command_class,data_type)
|
|
84
|
+
if data_type
|
|
85
|
+
data_type_index = data_type.downcase
|
|
86
|
+
if data_type_index != snake_form(command_class)
|
|
87
|
+
data_type_index
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def get_meta(type,command_class,data_type_index=nil)
|
|
93
|
+
ret = nil
|
|
94
|
+
view = data_type_index||snake_form(command_class)
|
|
95
|
+
view = command_class if view.empty?
|
|
96
|
+
# TODO: Fix this logic, but we first need to see what to do with simple lists
|
|
97
|
+
if type.eql?('hash_pretty_print')
|
|
98
|
+
return pretty_print_meta(command_class, data_type_index)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
begin
|
|
102
|
+
dtk_require("../views/#{view}/#{type}")
|
|
103
|
+
view_const = DTK::Client::ViewMeta.const_get cap_form(view)
|
|
104
|
+
ret = view_const.const_get cap_form(type)
|
|
105
|
+
rescue Exception
|
|
106
|
+
ret = failback_meta(command_class.respond_to?(:pretty_print_cols) ? command_class.pretty_print_cols() : [])
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
return ret
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def pretty_print_meta(command_class,data_type_index=nil)
|
|
113
|
+
view = data_type_index||snake_form(command_class)
|
|
114
|
+
view = command_class if view.empty?
|
|
115
|
+
# content = DiskCacher.new.fetch("http://localhost/mockup/get_pp_metadata", ::DTK::Configuration.get(:meta_table_ttl))
|
|
116
|
+
content = DiskCacher.new.fetch("pp_metadata", ::DTK::Configuration.get(:meta_table_ttl))
|
|
117
|
+
raise DTK::Client::DtkError, "Pretty print metadata is empty, please contact DTK team." if content.empty?
|
|
118
|
+
hash_content = JSON.parse(content, {:symbolize_names => true})
|
|
119
|
+
(view && (not view.empty?) && hash_content[view.to_sym])||empty_pretty_print_meta()
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def empty_pretty_print_meta()
|
|
123
|
+
{:top_type=>:top, :defs=>{:top_def=>[]}}
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
module ViewMeta
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
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: test for assembly list/display; want to make assembly specfic stuff datadriven
|
|
19
|
+
dtk_require 'simple_list'
|
|
20
|
+
module DTK
|
|
21
|
+
module Client
|
|
22
|
+
class ViewProcAugmentedSimpleList < ViewProcSimpleList
|
|
23
|
+
private
|
|
24
|
+
def initialize(type,command_class,data_type_index=nil)
|
|
25
|
+
super
|
|
26
|
+
@meta = get_meta(type,command_class)
|
|
27
|
+
end
|
|
28
|
+
def failback_meta(ordered_cols)
|
|
29
|
+
nil
|
|
30
|
+
end
|
|
31
|
+
def simple_value_render(ordered_hash,ident_info)
|
|
32
|
+
augmented_def?(ordered_hash,ident_info) || super
|
|
33
|
+
end
|
|
34
|
+
def augmented_def?(ordered_hash,ident_info)
|
|
35
|
+
return nil unless @meta
|
|
36
|
+
if aug_def = @meta["#{ordered_hash.object_type}_def".to_sym]
|
|
37
|
+
ident_str = ident_str(ident_info[:ident]||0)
|
|
38
|
+
vals = aug_def[:keys].map{|k|ordered_hash[k.to_s]}
|
|
39
|
+
"#{ident_str}#{aug_def[:fn].call(*vals)}\n"
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,123 @@
|
|
|
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
|
|
19
|
+
module Client
|
|
20
|
+
class ViewProcHashPrettyPrint < ViewProcessor
|
|
21
|
+
include ::DTK::Client::Auxiliary
|
|
22
|
+
def render(hash)
|
|
23
|
+
object_def = get_top_def()
|
|
24
|
+
raise_error() unless object_def
|
|
25
|
+
render_object_def(hash,object_def)
|
|
26
|
+
end
|
|
27
|
+
private
|
|
28
|
+
attr_reader :meta
|
|
29
|
+
def initialize(type,command_class,data_type_index=nil)
|
|
30
|
+
super
|
|
31
|
+
@meta = get_meta(type,command_class,data_type_index)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def failback_meta(ordered_cols)
|
|
35
|
+
{
|
|
36
|
+
:top_type => :top,
|
|
37
|
+
:defs => {:top_def => ordered_cols}
|
|
38
|
+
}
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def get_top_def()
|
|
42
|
+
raise_error("No Top def") unless top_object_type = meta[:top_type]
|
|
43
|
+
get_object_def(top_object_type)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def get_object_def(object_type)
|
|
47
|
+
if defs = meta[:defs] and object_type
|
|
48
|
+
{object_type => defs["#{object_type}_def".to_sym]}
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
def raise_error(msg=nil)
|
|
52
|
+
msg ||= "No hash pretty print view defined"
|
|
53
|
+
raise Error.new(msg)
|
|
54
|
+
end
|
|
55
|
+
def render_object_def(object,object_def,opts={})
|
|
56
|
+
#TODO: stub making it only first level
|
|
57
|
+
return object unless object.kind_of?(Hash)
|
|
58
|
+
hash = object
|
|
59
|
+
ret = ViewPrettyPrintHash.new(object_def.keys.first)
|
|
60
|
+
|
|
61
|
+
object_def.values.first.each do |item|
|
|
62
|
+
if item.kind_of?(Hash)
|
|
63
|
+
render_object_def__hash_def!(ret,hash,item)
|
|
64
|
+
else
|
|
65
|
+
key = item.to_s
|
|
66
|
+
target_key = replace_with_key_alias?(key)
|
|
67
|
+
#TODO: may want to conditionally include nil values
|
|
68
|
+
ret[target_key] = hash[key] if hash[key]
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
#catch all for keys not defined
|
|
72
|
+
unless opts[:only_explicit_cols]
|
|
73
|
+
(hash.keys.map{|k|replace_with_key_alias?(k)} - ret.keys).each do |key|
|
|
74
|
+
ret[key] = hash[key] if hash[key]
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
return ret
|
|
78
|
+
end
|
|
79
|
+
def replace_with_key_alias?(key)
|
|
80
|
+
#TODO: fix
|
|
81
|
+
return key
|
|
82
|
+
if ret = GlobalKeyAliases[key.to_sym] then ret.to_s
|
|
83
|
+
else key
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
GlobalKeyAliases = {
|
|
87
|
+
:library_library_id => :library_id,
|
|
88
|
+
:datacenter_datacenter_id => :target_id
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
def render_object_def__hash_def!(ret,hash,hash_def_item)
|
|
92
|
+
key = hash_def_item.keys.first.to_s
|
|
93
|
+
return unless input = hash[key]
|
|
94
|
+
hash_def_info = hash_def_item.values.first
|
|
95
|
+
nested_object_def = get_object_def(hash_def_info[:type])
|
|
96
|
+
raise_error("object def of type (#{hash_def_info[:type]||""}) does not exist") unless nested_object_def
|
|
97
|
+
|
|
98
|
+
opts = Hash.new
|
|
99
|
+
if hash_def_info[:only_explicit_cols]
|
|
100
|
+
opts.merge!(:only_explicit_cols => true)
|
|
101
|
+
end
|
|
102
|
+
if hash_def_info[:is_array]
|
|
103
|
+
raise_error("hash subpart should be an array") unless input.kind_of?(Array)
|
|
104
|
+
ret[key] = input.map{|el|render_object_def(el,nested_object_def,opts)}
|
|
105
|
+
else
|
|
106
|
+
ret[key] = render_object_def(input,nested_object_def,opts)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
class ViewPrettyPrintHash < Common::PrettyPrintHash
|
|
111
|
+
def initialize(object_type=nil)
|
|
112
|
+
super()
|
|
113
|
+
@object_type = object_type
|
|
114
|
+
end
|
|
115
|
+
attr_accessor :object_type
|
|
116
|
+
def slice(*keys)
|
|
117
|
+
ret = super
|
|
118
|
+
ret.object_type = object_type
|
|
119
|
+
ret
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|