dtk-action-agent 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ceb9bc809895890686ccd58e4783db04dd98607
4
- data.tar.gz: dd303a2fe410acaaa1e1bebb2b5c37d2be231658
3
+ metadata.gz: 59abb9f8d4c53e5d82e17b93033b27601585ff70
4
+ data.tar.gz: b3885713293250f5beb51480f31669bfc723054f
5
5
  SHA512:
6
- metadata.gz: 8b9d0492fbb95fa778718c900bb7accc860b795a2ae53609a6aea453223a5153a7c851f5c0a1775877dc42ad2d2fd9f8d338918df0befd4dd12bd89b69962c49
7
- data.tar.gz: 80e42e95161fd18359a5d0988180a6d27e405371d5835de83b2bf7f79a0b5c97c6dee84ecfca4afac784ca6cedd4ded3a3370db5c2c0d5df5d78b059f4fd1a03
6
+ metadata.gz: ee0795deb8734cac59bfad5fb537602f6947c7c1e9e6dd6f674798950941752fd18f09e5c53e857b8facffcf9ee9c8c9b57c6c003bcc075dfe3ab2234cf4abaf
7
+ data.tar.gz: 8391a8ebaaf3a2efbc5c14a5b273e7e18977555ad5d036040ae1cf098756a1927111fe68f4527cdc39ca5ab7156346a2d903b8105df5a21cc65373b2b1a3f53a
data/bin/dtk-action-agent CHANGED
@@ -5,7 +5,6 @@ require 'cgi'
5
5
 
6
6
  require File.expand_path('../../lib/logger', __FILE__)
7
7
  require File.expand_path('../../lib/arbiter', __FILE__)
8
- require File.expand_path('../../lib/positioner', __FILE__)
9
8
  require File.expand_path('../../lib/commander', __FILE__)
10
9
 
11
10
  unless ARGV[0]
data/lib/arbiter.rb CHANGED
@@ -18,18 +18,12 @@ module DTK
18
18
  return
19
19
  end
20
20
 
21
- # sets enviorment variables
22
- Commander.set_environment_variables(@received_message['env_vars'])
23
- @positioner = Positioner.new(@received_message['positioning'])
24
21
  @commander = Commander.new(@received_message['execution_list'])
25
22
  end
26
23
 
27
24
  def run
28
25
  return { :results => [], :errors => Log.execution_errors } if @execution_list.empty?
29
26
 
30
- # start positioning files
31
- @positioner.run()
32
-
33
27
  # start commander runnes
34
28
  @commander.run()
35
29
 
data/lib/command.rb CHANGED
@@ -27,6 +27,9 @@ module DTK
27
27
  @if_fail = value_hash['unless']
28
28
  @spawned = false
29
29
  @child_task = value_hash['child_task'] || false
30
+ @timeout = (value_hash['timeout'] || 0).to_i
31
+
32
+ @env_vars = value_hash['env_vars']
30
33
 
31
34
  if @if_success && @if_fail
32
35
  Log.warn "Unexpected case, both if/unless conditions have been set for command #{@command}(#{@command_type})"
@@ -37,11 +40,17 @@ module DTK
37
40
  # Creates Posix Spawn of given process
38
41
  #
39
42
  def start_task
43
+
40
44
  begin
41
- @process = POSIX::Spawn::Child.new(formulate_command)
45
+ Commander.set_environment_variables(@env_vars)
46
+ @process = POSIX::Spawn::Child.new(formulate_command, :timeout => @timeout)
42
47
  Log.debug("Command started: '#{self.to_s}'")
48
+ rescue POSIX::Spawn::TimeoutExceeded => e
49
+ @error_message = "Timeout (#{@timeout} sec) for this action has been exceeded"
43
50
  rescue Exception => e
44
51
  @error_message = e.message
52
+ ensure
53
+ Commander.clear_environment_variables(@env_vars)
45
54
  end
46
55
  end
47
56
 
@@ -54,6 +63,10 @@ module DTK
54
63
  !!command_to_run
55
64
  end
56
65
 
66
+ def is_positioning?
67
+ 'file'.eql?(@command_type)
68
+ end
69
+
57
70
 
58
71
  ##
59
72
  # Creates Command object for callback, first check 'if' than 'unless'. There should be no both set so priority is given
@@ -87,6 +100,7 @@ module DTK
87
100
  end
88
101
 
89
102
  def started?
103
+ return true if @error_message
90
104
  !!self.process
91
105
  end
92
106
 
data/lib/commander.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require File.expand_path('../command', __FILE__)
2
+ require File.expand_path('../position', __FILE__)
2
3
 
3
4
 
4
5
  module DTK
@@ -8,7 +9,13 @@ module DTK
8
9
  PARALLEL_EXECUTION = ENV['DTK_ACTION_AGENT_PARALLEL_EXEC'] || false
9
10
 
10
11
  def initialize(execution_list)
11
- @command_tasks = execution_list.collect { |command| Command.new(command) }
12
+ @command_tasks = execution_list.collect do |command|
13
+ if (command['type'].eql?('file'))
14
+ Position.new(command)
15
+ else
16
+ Command.new(command)
17
+ end
18
+ end
12
19
  end
13
20
 
14
21
  def run
@@ -98,6 +105,14 @@ module DTK
98
105
 
99
106
  private
100
107
 
108
+ def self.clear_environment_variables(env_vars_hash)
109
+ return unless env_vars_hash
110
+ env_vars_hash.keys.each do |k|
111
+ ENV.delete(k)
112
+ Log.debug("Environment variable cleared (#{k})")
113
+ end
114
+ end
115
+
101
116
  ##
102
117
  # Sets environmental variables
103
118
  def self.set_environment_variables(env_vars_hash)
@@ -1,5 +1,5 @@
1
1
  module DTK
2
2
  module ActionAgent
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
data/lib/position.rb ADDED
@@ -0,0 +1,115 @@
1
+ require 'thread'
2
+ require 'git'
3
+ require 'fileutils'
4
+
5
+ module DTK
6
+ module Agent
7
+ class Position
8
+
9
+ attr_accessor :position_file_info, :exitstatus, :started, :out, :err, :child_task
10
+
11
+ def initialize(command_hash)
12
+ source_info, target_info = command_hash['source'], command_hash['target']
13
+
14
+ @type = source_info['type'].to_sym
15
+ @git_url = source_info['url']
16
+ @branch = source_info['ref'] || 'master'
17
+ @content = source_info['content']
18
+
19
+ @env_vars = command_hash['env_vars']
20
+
21
+ @target_path = target_info['path']
22
+
23
+ @exited = false
24
+ @started = false
25
+ @exitstatus = 0
26
+ @child_task = false
27
+ end
28
+
29
+ def start_task()
30
+ @started = true
31
+ prepare_path()
32
+
33
+ Commander.set_environment_variables(@env_vars)
34
+
35
+ begin
36
+ case @type
37
+ when :git
38
+ position_git()
39
+ when :in_payload
40
+ position_in_payload()
41
+ end
42
+ rescue Exception => e
43
+ cleanup_path()
44
+ raise e
45
+ ensure
46
+ Commander.clear_environment_variables(@env_vars)
47
+ end
48
+
49
+ end
50
+
51
+ def exited?
52
+ @exited
53
+ end
54
+
55
+ #
56
+ # This are not standard commands and as such we are ignoring their output
57
+ #
58
+ def started?
59
+ @started
60
+ end
61
+
62
+ def spawn_callback_task
63
+ raise "Callback task is not supported for positioner"
64
+ end
65
+
66
+ def callback_pending?
67
+ # not supported at the moment
68
+ false
69
+ end
70
+
71
+ def to_s
72
+ :git.eql?(@type) ? "git clone #{@git_url}:#{@branch} > #{@target_path}" : "create #{@target_path} with provided content"
73
+ end
74
+
75
+ private
76
+
77
+ def position_git()
78
+ unless File.directory?(@target_path)
79
+ begin
80
+ g_repo = Git.clone("#{@git_url}", '', :path => @target_path, :branch => @branch)
81
+ Log.info("Positioner successfully cloned git repository '#{@git_url}@#{@branch}' to location '#{@target_path}'")
82
+ rescue Exception => e
83
+ cleanup_path()
84
+ @exitstatus = 1
85
+ Log.error("Positioner unable to clone #{@git_url}")
86
+ Log.error(e.message)
87
+ end
88
+ else
89
+ Log.warn("Positioner detected folder '#{@target_path}' skipping git clone")
90
+ end
91
+
92
+ @exited = true
93
+ end
94
+
95
+ def position_in_payload(position_info)
96
+ # write to file
97
+ File.open(@target_path, 'w') { |file| file.write(@content) }
98
+ Log.info("Positioner successfully created 'IN_PAYLOAD' file '#{@target_path}'")
99
+ @exited = true
100
+ end
101
+
102
+ def prepare_path()
103
+ # create necessery dir structure
104
+ FileUtils.mkdir_p(File.dirname(@target_path))
105
+
106
+ @target_path
107
+ end
108
+
109
+ def cleanup_path()
110
+ FileUtils.rm_rf(@target_path)
111
+ end
112
+
113
+ end
114
+ end
115
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dtk-action-agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rich PELAVIN
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-08 00:00:00.000000000 Z
11
+ date: 2015-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: posix-spawn
@@ -82,7 +82,7 @@ files:
82
82
  - lib/commander.rb
83
83
  - lib/dtk-action-agent/version.rb
84
84
  - lib/logger.rb
85
- - lib/positioner.rb
85
+ - lib/position.rb
86
86
  homepage: ''
87
87
  licenses:
88
88
  - GPL-3.0
data/lib/positioner.rb DELETED
@@ -1,65 +0,0 @@
1
- require 'thread'
2
- require 'git'
3
- require 'fileutils'
4
-
5
- module DTK
6
- module Agent
7
- class Positioner
8
-
9
- attr_accessor :position_file_info
10
-
11
- def initialize(*position_file_info)
12
- @position_file_info = position_file_info.flatten.compact
13
- end
14
-
15
- def run()
16
- @position_file_info.each do |pfi|
17
- case pfi['source']['type'].to_sym
18
- when :git
19
- position_git(pfi)
20
- when :in_payload
21
- position_in_payload(pfi)
22
- end
23
- end
24
- end
25
-
26
- private
27
-
28
- def position_git(position_info)
29
- folder_path = prepare_path(position_info)
30
- git_url = position_info['source']['url']
31
- git_branch = position_info['source']['ref']
32
-
33
- unless File.directory?(folder_path)
34
- begin
35
- g_repo = Git.clone("#{git_url}", '', :path => folder_path, :branch => git_branch)
36
- Log.info("Positioner successfully cloned git repository '#{git_url}@#{git_branch}' to location '#{folder_path}'")
37
- rescue Exception => e
38
- Log.error("Positioner unable to clone #{git_url}")
39
- Log.error(e.message)
40
- end
41
- else
42
- Log.warn("Positioner detected folder '#{folder_path}' skipping git clone")
43
- end
44
- end
45
-
46
- def position_in_payload(position_info)
47
- file_path = prepare_path(position_info)
48
- file_content = position_info['source']['content']
49
- # write to file
50
- File.open(file_path, 'w') { |file| file.write(file_content) }
51
- Log.info("Positioner successfully created 'IN_PAYLOAD' file '#{file_path}'")
52
- end
53
-
54
- def prepare_path(position_info)
55
- path = position_info['target']['path']
56
-
57
- # create necessery dir structure
58
- FileUtils.mkdir_p(File.dirname(path))
59
-
60
- path
61
- end
62
-
63
- end
64
- end
65
- end