persistence-providers 0.0.3.6 → 0.0.5

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.
@@ -0,0 +1,26 @@
1
+ module DTK::State
2
+ class Workflow
3
+ require_relative 'workflow_instance/attribute_type_info'
4
+
5
+ WORKFLOW_CRD_VERSION = ENV["WORKFLOW_CRD_VERSION"]
6
+
7
+ attr_reader :name, :namespace, :references, :assembly, :workflow, :attribute_type_info
8
+
9
+ def initialize(namespace, name, crd_content)
10
+ @name = name
11
+ @namespace = namespace
12
+
13
+ @references = crd_content.references
14
+ @assembly = @references.assembly
15
+ @workflow = crd_content.spec.workflow || {}
16
+
17
+ @attribute_type_info = ::DTK::State::WorkflowInstance::AttributeTypeInfo.create_from_kube_hash(crd_content.spec.attributes.to_h || {})
18
+ end
19
+
20
+ def self.get(namespace, name, opts = {})
21
+ opts[:apiVersion] = WORKFLOW_CRD_VERSION
22
+ workflow = ::DTK::CrdClient.get_kubeclient(opts).get_workflow(name, namespace)
23
+ Workflow.new(namespace, name, workflow)
24
+ end
25
+ end
26
+ end
@@ -2,23 +2,75 @@ module DTK::State
2
2
  class WorkflowInstance
3
3
  require_relative 'workflow_instance/attribute_type_info'
4
4
 
5
- attr_reader :name, :namespace, :assembly, :workflow_template, :attributes, :workflow, :attribute_type_info
5
+ WORKFLOW_INSTANCE_CRD_VERSION = ENV["WORKFLOW_INSTANCE_CRD_VERSION"]
6
+
7
+ attr_reader :name, :namespace, :assembly, :workflow_template, :attributes, :attribute_type_info, :status, :workflow
8
+ attr_accessor :workflow
6
9
 
7
10
  def initialize(namespace, name, crd_content)
8
11
  @name = name
9
12
  @namespace = namespace
10
- @assembly = crd_content.references.assembly
11
- @workflow_template = crd_content.references.workflow
12
- @attributes = crd_content.spec.attributes
13
- @workflow = crd_content.spec.workflow
14
- @attribute_type_info = AttributeTypeInfo.create_from_kube_hash(@attributes.to_h || {})
13
+
14
+ @api_version = crd_content.apiVersion
15
+ @kind = crd_content.kind
16
+ @metadata = crd_content.metadata
17
+
18
+ @references = crd_content.references
19
+ @assembly = @references.assembly
20
+ @workflow_template = @references.workflow
21
+
22
+ @attributes = crd_content.spec.attributes || {}
23
+ @status = crd_content.spec.status || {}
24
+ @workflow = crd_content.spec.workflow || {}
15
25
  end
16
26
 
17
27
  def self.get(namespace, name, opts = {})
28
+ opts[:apiVersion] = WORKFLOW_INSTANCE_CRD_VERSION
18
29
  workflow_instance = ::DTK::CrdClient.get_kubeclient(opts).get_workflow_instance(name, namespace)
19
30
  WorkflowInstance.new(namespace, name, workflow_instance)
20
31
  end
21
32
 
33
+ def self.get_with_influx_data(namespace, workflow_instance_name, opts = {})
34
+ workflow_instance = get(namespace, workflow_instance_name, opts)
35
+ return unless workflow_instance
36
+
37
+ workflow_instance.workflow[:subtasks].each do |subtask|
38
+ component_name, action_name = subtask[:component].split('.')
39
+ assembly_name = workflow_instance.assembly[:name]
40
+
41
+ executable_action = ::DTK::State::ExecutableAction.get(namespace, assembly_name, component_name, action_name, opts)
42
+ attr_type_info = executable_action.attribute_type_info
43
+
44
+ attr_type_info.each do |attr_info|
45
+ if attr_info.temporal
46
+ attribute_name = attr_info.name
47
+ influxdb = ::DTK::State::Component::Attribute::Influxdb.new(:attributes)
48
+ influxdb_attribute = influxdb.get(namespace, component_name, assembly_name, attribute_name, opts)
49
+ if valid_attribute = influxdb_attribute.first
50
+ value = valid_attribute['_value']
51
+ subtask[:attributes][attribute_name] = value
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ workflow_instance
58
+ end
59
+
60
+ def to_hash
61
+ {
62
+ apiVersion: @api_version,
63
+ kind: @kind,
64
+ metadata: @metadata.to_hash,
65
+ references: @references.to_hash,
66
+ spec: {
67
+ attributes: @attributes.to_hash,
68
+ status: @status.to_hash,
69
+ workflow: @workflow.to_hash
70
+ }
71
+ }
72
+ end
73
+
22
74
  def self.get_attributes(namespace, name, opts = {})
23
75
  workflow_instance = get(namespace, name, opts)
24
76
  workflow_instance.attributes.to_h
@@ -26,13 +78,58 @@ module DTK::State
26
78
 
27
79
  def self.get_action_attributes(namespace, name, action_id, opts = {})
28
80
  workflow_instance = get(namespace, name, opts)
29
- action = workflow_instance.find_action(action_id)
81
+ action = WorkflowInstance.find_action(action_id, workflow_instance.workflow)
30
82
  return nil unless action
31
83
  attributes = action[:attributes] || {}
32
84
  attributes.to_h
33
85
  end
34
86
 
35
- def find_action(id, workflow = @workflow)
87
+ def self.update_action_level_result_attributes(namespace, name, attributes, action_id, opts = {})
88
+ return "Dynamic attributes do not exist for action with id #{@action_id}, nothing to update" if attributes.nil? || attributes.empty?
89
+ attributes.delete_if { |key, value| value.nil? || value.to_s.strip == '' }
90
+ opts[:apiVersion] = WORKFLOW_INSTANCE_CRD_VERSION
91
+ workflow_instance = ::DTK::CrdClient.get_kubeclient(opts).get_workflow_instance(name, namespace)
92
+ workflow = workflow_instance[:spec][:workflow]
93
+
94
+ action = WorkflowInstance.find_action(action_id, workflow)
95
+ action[:attributes] = {} if !action[:attributes]
96
+ attributes.each do |attr_name, attr_val|
97
+ action[:attributes][attr_name.to_sym] = {} unless action[:attributes][attr_name.to_sym]
98
+ unless action[:attributes][attr_name.to_sym][:hidden]
99
+ if attr_val.is_a? Hash
100
+ action[:attributes][attr_name.to_sym][:value] = attr_val[:value] || attr_val
101
+ else
102
+ action[:attributes][attr_name.to_sym][:value] = attr_val
103
+ end
104
+ end
105
+ end
106
+ ::DTK::CrdClient.get_kubeclient(opts).update_workflow_instance(workflow_instance)
107
+ end
108
+
109
+ def self.patchError!(patches, message, action_index_steps)
110
+ errorPatch = {
111
+ "op" => "add",
112
+ "path" => "/spec/status/steps/#{action_index_steps}/errorMsg",
113
+ "value" => message
114
+ }
115
+ patches << errorPatch
116
+ end
117
+
118
+ def self.update_action_status(namespace, name, parent_id, action_id, status, error_message = "", opts = {})
119
+ opts[:apiVersion] = WORKFLOW_INSTANCE_CRD_VERSION
120
+ workflow_instance = ::DTK::CrdClient.get_kubeclient(opts).get_workflow_instance(name, namespace)
121
+ steps = workflow_instance[:spec][:status][:steps]
122
+ action_index_steps = steps.find_index { |action| action[:id].eql? action_id }
123
+ patch = [{
124
+ "op" => "replace",
125
+ "path" => "/spec/status/steps/#{action_index_steps}/state",
126
+ "value" => status
127
+ }]
128
+ patchError!(patch, error_message, action_index_steps) unless error_message.empty? || error_message.nil?
129
+ ::DTK::CrdClient.get_kubeclient(opts).json_patch_workflow_instance(name, patch, namespace)
130
+ end
131
+
132
+ def self.find_action(id, workflow = @workflow)
36
133
  action = nil
37
134
  subtasks = workflow[:subtasks]
38
135
  subtasks.each do |subtask|
@@ -47,5 +144,38 @@ module DTK::State
47
144
  action
48
145
  end
49
146
 
147
+ def get_workflow_template(opts = {})
148
+ Workflow.get(@workflow_template.namespace, @workflow_template.name, opts)
149
+ end
150
+
151
+ def attribute_metadata
152
+ attributes = @attributes.to_hash
153
+ attr_type_info = get_workflow_template.attribute_type_info
154
+ attribute_metadata = {}
155
+
156
+ attr_type_info.each do |attr_info|
157
+ attr_info_hash = attr_info.to_hash
158
+ attribute_name = attr_info_hash[:name].to_sym
159
+
160
+ if attribute = attributes[attribute_name]
161
+ if attribute.is_a?(String)
162
+ attribute = { value: attribute }
163
+ end
164
+
165
+ attribute_metadata[attribute_name] = attr_info_hash.merge(attribute)
166
+ end
167
+ end
168
+
169
+ attribute_metadata
170
+ end
171
+
172
+ def attribute_values
173
+ attribute_with_values = {}
174
+ @attributes.each_pair do |name, content|
175
+ attribute_with_values.merge!(name => content[:value])
176
+ end
177
+ attribute_with_values
178
+ end
179
+
50
180
  end
51
181
  end
@@ -21,6 +21,19 @@ module DTK::State
21
21
  end
22
22
  end
23
23
 
24
+ def to_hash
25
+ type_info = {}
26
+
27
+ type_info.merge!(name: @name) if @name
28
+ type_info.merge!(type: @type) if @type
29
+ type_info.merge!(required: @required) if @required
30
+ type_info.merge!(dynamic: @dynamic) if @dynamic
31
+ type_info.merge!(temporal: @temporal) if @temporal
32
+ type_info.merge!(encrypted: @encrypted) if @encrypted
33
+
34
+ type_info
35
+ end
36
+
24
37
  end
25
38
  end
26
39
  end
@@ -0,0 +1,5 @@
1
+ module DTK
2
+ module Utils
3
+ require_relative 'utils/log'
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ module DTK::Utils
2
+ module Log
3
+ require 'logger'
4
+
5
+ def self.instance
6
+ @instance ||= Logger.new('/proc/1/fd/1', formatter: proc { |severity, datetime, progname, msg|
7
+ orange_color = "\x1b[33m"
8
+ white_color = "\x1b[37m"
9
+ red_color = "\x1b[31m"
10
+
11
+ date_format = datetime.strftime("%Y-%m-%d %H:%M:%S:%L")
12
+ if severity == "INFO"
13
+ "#{orange_color}[#{date_format}] - #{white_color}#{msg}\n"
14
+ elsif severity == "WARN"
15
+ "#{orange_color}[#{date_format}] [WARNING] - #{msg}\n"
16
+ elsif severity == "ERROR"
17
+ "#{red_color}[#{date_format}] [ERROR] - #{msg}\n"
18
+ end
19
+ })
20
+ end
21
+ end
22
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'persistence-providers'
3
- spec.version = '0.0.3.6'
3
+ spec.version = '0.0.5'
4
4
  spec.author = 'Reactor8'
5
5
  spec.email = 'support@reactor8.com'
6
6
  spec.description = %q{Persistence providers plugin}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: persistence-providers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3.6
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reactor8
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-14 00:00:00.000000000 Z
11
+ date: 2020-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kubeclient
@@ -77,8 +77,11 @@ files:
77
77
  - lib/state/crd_assembly.rb
78
78
  - lib/state/executable_action.rb
79
79
  - lib/state/executable_action/attribute_type_info.rb
80
+ - lib/state/workflow.rb
80
81
  - lib/state/workflow_instance.rb
81
82
  - lib/state/workflow_instance/attribute_type_info.rb
83
+ - lib/utils.rb
84
+ - lib/utils/log.rb
82
85
  - persistence-providers.gemspec
83
86
  - test-destroy-influxdb.rb
84
87
  - test-influxdb.rb