dtk-shell 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- 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,60 @@
|
|
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 DTK::Client::Execute
|
19
|
+
class Command::APICall
|
20
|
+
class Map < Hash
|
21
|
+
def initialize(hash={})
|
22
|
+
super()
|
23
|
+
replace(hash)
|
24
|
+
end
|
25
|
+
|
26
|
+
def translate(api_params={},opts={})
|
27
|
+
if Rest::Post.matches?(type())
|
28
|
+
Command::RestCall::Post.new(:path => path(),:body => translate_to_rest_body(api_params,opts))
|
29
|
+
else
|
30
|
+
raise "Type in following map is not defined not defined: #{self.inspect}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def translate_to_rest_body(api_params,opts)
|
36
|
+
body().inject(Hash.new) do |h,(k,v)|
|
37
|
+
# if TranslationTerm.matches is false then v is a constant
|
38
|
+
processed_v =
|
39
|
+
if TranslationTerm.matches?(v)
|
40
|
+
v.instance_form().translate(k,api_params,opts)
|
41
|
+
else
|
42
|
+
v
|
43
|
+
end
|
44
|
+
h.merge(k => processed_v)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def type()
|
49
|
+
self[:type]
|
50
|
+
end
|
51
|
+
def path()
|
52
|
+
self[:path]
|
53
|
+
end
|
54
|
+
def body()
|
55
|
+
self[:body]
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,91 @@
|
|
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 DTK::Client::Execute
|
19
|
+
class Command::APICall
|
20
|
+
class Service < self
|
21
|
+
def self.object_type()
|
22
|
+
:service
|
23
|
+
end
|
24
|
+
|
25
|
+
CommandMap = {
|
26
|
+
:add_component => Map.new(
|
27
|
+
:type => Rest::Post,
|
28
|
+
:path => 'assembly/add_component',
|
29
|
+
:body => {
|
30
|
+
:assembly_id => Required(:service),
|
31
|
+
:subtype => 'instance',
|
32
|
+
:node_id => Required(:node),
|
33
|
+
:component_template_id => Required(:component),
|
34
|
+
:namespace => Required(:namespace),
|
35
|
+
:idempotent => Equal::OrDefault(true),
|
36
|
+
:donot_update_workflow => Equal::OrDefault(false)
|
37
|
+
}
|
38
|
+
),
|
39
|
+
|
40
|
+
:set_attribute => Map.new(
|
41
|
+
:type => Rest::Post,
|
42
|
+
:path => 'assembly/set_attributes',
|
43
|
+
:body => {
|
44
|
+
:assembly_id => Required(:service),
|
45
|
+
:pattern => Required(:attribute_path),
|
46
|
+
# TODO: support FN
|
47
|
+
# :pattern => FN{"#{Required(:node)}/#{Required(:component)}/#{Required(:attribute)}"
|
48
|
+
:value => Required::Equal
|
49
|
+
}
|
50
|
+
),
|
51
|
+
|
52
|
+
:link_components => Map.new(
|
53
|
+
:type => Rest::Post,
|
54
|
+
:path => 'assembly/add_service_link',
|
55
|
+
:body => {
|
56
|
+
:assembly_id => Required(:service),
|
57
|
+
:input_component_id => Required(:input_component),
|
58
|
+
:output_component_id => Required(:output_component),
|
59
|
+
}
|
60
|
+
),
|
61
|
+
|
62
|
+
:execute_workflow =>
|
63
|
+
[
|
64
|
+
Map.new(
|
65
|
+
:type => Rest::Post,
|
66
|
+
:path => 'assembly/create_task',
|
67
|
+
:body => {
|
68
|
+
:assembly_id => Required(:service),
|
69
|
+
:task_action => Required(:workflow_name),
|
70
|
+
:task_params => Required(:workflow_params)
|
71
|
+
}
|
72
|
+
),
|
73
|
+
Map.new(
|
74
|
+
:type => Rest::Post,
|
75
|
+
:path => 'task/execute',
|
76
|
+
:body => {
|
77
|
+
:task_id => PreviousResponse(:task_id)
|
78
|
+
}
|
79
|
+
)]
|
80
|
+
}
|
81
|
+
|
82
|
+
module CustomMapping
|
83
|
+
# example would be
|
84
|
+
# def self.command(params)
|
85
|
+
# ...
|
86
|
+
# end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,119 @@
|
|
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 DTK::Client::Execute
|
19
|
+
class Command::APICall
|
20
|
+
|
21
|
+
# abstract classes
|
22
|
+
class TranslationTerm
|
23
|
+
def self.matches?(obj)
|
24
|
+
obj.kind_of?(self) or
|
25
|
+
(obj.kind_of?(Class) and obj <= self)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.instance_form()
|
29
|
+
new()
|
30
|
+
end
|
31
|
+
def instance_form()
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
class Operation < self
|
36
|
+
end
|
37
|
+
|
38
|
+
class Param < self
|
39
|
+
private
|
40
|
+
def index(api_params,key)
|
41
|
+
api_params[key]
|
42
|
+
end
|
43
|
+
|
44
|
+
def index_required(api_params,key)
|
45
|
+
unless has_key?(api_params,key)
|
46
|
+
raise ErrorUsage.new("Missing key '#{key}' in params: #{api_params.inspect}")
|
47
|
+
end
|
48
|
+
index(api_params,key)
|
49
|
+
end
|
50
|
+
|
51
|
+
def has_key?(api_params,key)
|
52
|
+
api_params.has_key?(key)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# concrete classes
|
59
|
+
|
60
|
+
class Rest < TranslationTerm::Operation
|
61
|
+
class Post < self
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class Required < TranslationTerm::Param
|
66
|
+
def initialize(input_key=nil)
|
67
|
+
@input = input_key
|
68
|
+
end
|
69
|
+
|
70
|
+
def translate(key,api_params,opts={})
|
71
|
+
index_required(api_params,@input||key)
|
72
|
+
end
|
73
|
+
|
74
|
+
class Equal < self
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class Equal < TranslationTerm::Param
|
79
|
+
def translate(key,api_params,opts={})
|
80
|
+
index(api_params,key)
|
81
|
+
end
|
82
|
+
|
83
|
+
class Required < self
|
84
|
+
def translate(key,api_params,opts={})
|
85
|
+
index_required(api_params,key)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.OrDefault(default_value)
|
90
|
+
OrDefault.new(default_value)
|
91
|
+
end
|
92
|
+
|
93
|
+
class OrDefault < self
|
94
|
+
def initialize(default_value)
|
95
|
+
@default_value = default_value
|
96
|
+
end
|
97
|
+
def translate(key,api_params,opts={})
|
98
|
+
has_key?(api_params,key) ? index(api_params,key) : @default_value
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class PreviousResponse < TranslationTerm::Param
|
104
|
+
def initialize(response_key)
|
105
|
+
@response_key = response_key
|
106
|
+
end
|
107
|
+
def translate(key,api_params,opts={})
|
108
|
+
unless last_result = opts[:last_result]
|
109
|
+
raise ErrorUsage.new("PreviousResponse used before results computed")
|
110
|
+
end
|
111
|
+
match = nil
|
112
|
+
unless matching_key = [@response_key.to_s,@response_key.to_sym].find{|k|last_result.has_key?(k)}
|
113
|
+
raise ErrorUsage.new("Key #{@response_key} in PreviousResponse not found in last results (#{last_result.inspect})")
|
114
|
+
end
|
115
|
+
last_result[matching_key]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,37 @@
|
|
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 DTK::Client::Execute
|
19
|
+
class Command
|
20
|
+
class RestCall < self
|
21
|
+
# TODO: allow calls like 'converge' which is a macro; we either directly support or have a sub class of RestCall called macro
|
22
|
+
attr_reader :path
|
23
|
+
def initialize(hash)
|
24
|
+
super
|
25
|
+
@path = required(:path)
|
26
|
+
end
|
27
|
+
|
28
|
+
class Post < self
|
29
|
+
attr_reader :body
|
30
|
+
def initialize(hash)
|
31
|
+
super
|
32
|
+
@body = optional?(:body)||{}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,30 @@
|
|
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 DTK::Client::Execute
|
19
|
+
class CommandProcessor
|
20
|
+
dtk_require('command_processor/rest_call')
|
21
|
+
|
22
|
+
def self.execute(command)
|
23
|
+
if command.kind_of?(Command::RestCall)
|
24
|
+
RestCall.execute(command)
|
25
|
+
else
|
26
|
+
raise ErrorUsage.new("Unexpected Command type: #{command.class}")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,59 @@
|
|
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 DTK::Client::Execute; class CommandProcessor
|
19
|
+
class RestCall< self
|
20
|
+
extend ::DTK::Client::CommandBase # TODO: have a Base that is not just for commands (CommandBase)
|
21
|
+
|
22
|
+
def self.execute(rest_command)
|
23
|
+
response = raw_execute(rest_command)
|
24
|
+
if response.ok?
|
25
|
+
response.data
|
26
|
+
else
|
27
|
+
raise Error.new(response,rest_command)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def self.raw_execute(rest_command)
|
33
|
+
if rest_command.kind_of?(Command::RestCall::Post)
|
34
|
+
post(rest_url(rest_command.path),rest_command.body)
|
35
|
+
else
|
36
|
+
raise ErrorUsage.new("Unexpected Rest Command type: #{rest_command.class}")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Error < ErrorUsage
|
41
|
+
def initialize(response_not_ok,rest_command)
|
42
|
+
error_print_form = error_print_form(response_not_ok)
|
43
|
+
error_msg = "Bad Rest response from call (#{rest_command.input_hash.inspect}:\n #{error_print_form}"
|
44
|
+
super(error_msg)
|
45
|
+
end
|
46
|
+
private
|
47
|
+
def error_print_form(response_not_ok)
|
48
|
+
ret_obj =
|
49
|
+
if errors = response_not_ok['errors']
|
50
|
+
errors.size == 1 ? errors.first : errors
|
51
|
+
else
|
52
|
+
response_not_ok
|
53
|
+
end
|
54
|
+
ret_obj.kind_of?(String) ? ret_obj : ret_obj.inspect
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end; end
|
@@ -0,0 +1,21 @@
|
|
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 DTK::Client::Execute
|
19
|
+
class ErrorUsage < ::DTK::Client::Error
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,86 @@
|
|
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 DTK::Client::Execute
|
19
|
+
class ExecuteContext
|
20
|
+
dtk_require('execute_context/result_store')
|
21
|
+
|
22
|
+
module ClassMixin
|
23
|
+
def ExecuteContext(opts={},&block)
|
24
|
+
ExecuteContext.new(opts).execute(&block)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize(opts={})
|
29
|
+
@print_results = opts[:print_results]
|
30
|
+
@proxy = Proxy.new()
|
31
|
+
end
|
32
|
+
|
33
|
+
def execute(&block)
|
34
|
+
result, command = instance_eval(&block)
|
35
|
+
result
|
36
|
+
end
|
37
|
+
|
38
|
+
def method_missing(m, *args, &block)
|
39
|
+
result, command = @proxy.send(m, *args, &block)
|
40
|
+
if @print_results
|
41
|
+
pp(:command => command.input_hash(),:result => result)
|
42
|
+
end
|
43
|
+
[result, command]
|
44
|
+
end
|
45
|
+
|
46
|
+
# commands in the execute context
|
47
|
+
class Proxy
|
48
|
+
def initialize()
|
49
|
+
@result_store = ResultStore.new()
|
50
|
+
end
|
51
|
+
|
52
|
+
def call(object_type__method,params={})
|
53
|
+
object_type, method = split_object_type__method(object_type__method)
|
54
|
+
api_command = Command::APICall.new(:object_type => object_type, :method => method, :params => params)
|
55
|
+
result = nil
|
56
|
+
api_command.raw_executable_commands() do |raw_command|
|
57
|
+
last_result = @result_store.get_last_result?()
|
58
|
+
command = raw_command.translate(params,:last_result => last_result)
|
59
|
+
result = CommandProcessor.execute(command)
|
60
|
+
@result_store.store(result)
|
61
|
+
end
|
62
|
+
[result, api_command]
|
63
|
+
end
|
64
|
+
|
65
|
+
def post_rest_call(path,body={})
|
66
|
+
command = Command::RestCall::Post.new(:path => path, :body => body, :last_result => @last_result)
|
67
|
+
result = CommandProcessor.execute(command)
|
68
|
+
[result, command]
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
# returns [object_type, method]
|
73
|
+
def split_object_type__method(str)
|
74
|
+
DelimitersObjectTypeMethod.each do |d|
|
75
|
+
if str =~ Regexp.new("(^[^#{d}]+)#{d}([^#{d}]+$)")
|
76
|
+
return [$1,$2]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
raise ErrorUsage.new("Illegal term '#{str}'")
|
80
|
+
end
|
81
|
+
|
82
|
+
DelimitersObjectTypeMethod = ['\/']
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|