jackal-commander 0.1.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/CHANGELOG.md +2 -0
- data/CONTRIBUTING.md +25 -0
- data/LICENSE +13 -0
- data/README.md +92 -0
- data/jackal-commander.gemspec +16 -0
- data/lib/jackal-commander/executor.rb +96 -0
- data/lib/jackal-commander/version.rb +6 -0
- data/lib/jackal-commander.rb +8 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e1efecf35ee306e288645dd49c8a37275fb57327
|
4
|
+
data.tar.gz: 6be1982db812ed0ea02f96d7f76c98b51cbb5652
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c6e383a25524b9b2c6d908f959c9b07de814b9e5b4f7ea06dda59cab1d34eaf78d9d329fdb2a7e5311d8678e6bab7a7dfa63b27ce23eea3184e55eae1bc67be4
|
7
|
+
data.tar.gz: 555dfde9a2c74d55c2fa264d63649f49ebfb81baaf2037a22b6930ee23b10c2d0b8de4cfd9b9dec65afad9b25aca08c076fc46fb5952b5b83e0a76af9894a0f8
|
data/CHANGELOG.md
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
## Branches
|
4
|
+
|
5
|
+
### `master` branch
|
6
|
+
|
7
|
+
The master branch is the current stable released version.
|
8
|
+
|
9
|
+
### `develop` branch
|
10
|
+
|
11
|
+
The develop branch is the current edge of development.
|
12
|
+
|
13
|
+
## Pull requests
|
14
|
+
|
15
|
+
* https://github.com/carnivore-rb/jackal-commander/pulls
|
16
|
+
|
17
|
+
Please base all pull requests of the `develop` branch. Merges to
|
18
|
+
`master` only occur through the `develop` branch. Pull requests
|
19
|
+
based on `master` will likely be cherry picked.
|
20
|
+
|
21
|
+
## Issues
|
22
|
+
|
23
|
+
Need to report an issue? Use the github issues:
|
24
|
+
|
25
|
+
* https://github.com/carnivore-rb/jackal-commander/issues
|
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright 2014 Chris Roberts
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Jackal Commander
|
2
|
+
|
3
|
+
Execute actions via jackal
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Set action to execute within payload:
|
8
|
+
|
9
|
+
|
10
|
+
### Single action
|
11
|
+
|
12
|
+
```json
|
13
|
+
{
|
14
|
+
...
|
15
|
+
"data": {
|
16
|
+
"commander": {
|
17
|
+
"action": "toucher"
|
18
|
+
}
|
19
|
+
}
|
20
|
+
}
|
21
|
+
```
|
22
|
+
|
23
|
+
### Multiple actions
|
24
|
+
|
25
|
+
|
26
|
+
```json
|
27
|
+
{
|
28
|
+
...
|
29
|
+
"data": {
|
30
|
+
"commander": {
|
31
|
+
"actions": [
|
32
|
+
"toucher",
|
33
|
+
"remover"
|
34
|
+
]
|
35
|
+
}
|
36
|
+
}
|
37
|
+
}
|
38
|
+
```
|
39
|
+
|
40
|
+
### Mixed actions
|
41
|
+
|
42
|
+
```json
|
43
|
+
{
|
44
|
+
...
|
45
|
+
"data": {
|
46
|
+
"commander": {
|
47
|
+
"action": "toucher",
|
48
|
+
"actions": [
|
49
|
+
"remover"
|
50
|
+
]
|
51
|
+
}
|
52
|
+
}
|
53
|
+
}
|
54
|
+
```
|
55
|
+
|
56
|
+
### With extra arguments
|
57
|
+
|
58
|
+
```json
|
59
|
+
{
|
60
|
+
...
|
61
|
+
"data": {
|
62
|
+
"commander": {
|
63
|
+
"action": {
|
64
|
+
"name": "custom_touch",
|
65
|
+
"arguments": "/tmp/my-custom-file"
|
66
|
+
}
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}
|
70
|
+
```
|
71
|
+
|
72
|
+
## Configuration
|
73
|
+
|
74
|
+
Define the action within the configuration:
|
75
|
+
|
76
|
+
```json
|
77
|
+
{
|
78
|
+
...
|
79
|
+
"config": {
|
80
|
+
"actions": {
|
81
|
+
"toucher": "touch /tmp/test-file",
|
82
|
+
"remover": "rm -f /tmp/test-file",
|
83
|
+
"custom_touch": "touch"
|
84
|
+
}
|
85
|
+
}
|
86
|
+
}
|
87
|
+
```
|
88
|
+
|
89
|
+
## Info
|
90
|
+
|
91
|
+
* Repository: https://github.com/carnviore-rb/jackal-cfn
|
92
|
+
* IRC: Freenode @ #carnivore
|
@@ -0,0 +1,16 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + '/lib/'
|
2
|
+
require 'jackal-commander/version'
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'jackal-commander'
|
5
|
+
s.version = Jackal::Commander::VERSION.version
|
6
|
+
s.summary = 'Message processing helper'
|
7
|
+
s.author = 'Chris Roberts'
|
8
|
+
s.email = 'code@chrisroberts.org'
|
9
|
+
s.homepage = 'https://github.com/carnivore-rb/jackal-commander'
|
10
|
+
s.description = 'Command helpers'
|
11
|
+
s.require_path = 'lib'
|
12
|
+
s.license = 'Apache 2.0'
|
13
|
+
s.add_dependency 'jackal'
|
14
|
+
s.add_dependency 'childprocess'
|
15
|
+
s.files = Dir['lib/**/*'] + %w(jackal-commander.gemspec README.md CHANGELOG.md CONTRIBUTING.md LICENSE)
|
16
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'jackal-commander'
|
2
|
+
|
3
|
+
module Jackal
|
4
|
+
module Commander
|
5
|
+
# Run executions
|
6
|
+
class Executor < Jackal::Callback
|
7
|
+
|
8
|
+
# @return [String] root path for process output files
|
9
|
+
attr_reader :process_output_root
|
10
|
+
|
11
|
+
# default temp storage directory for process outputs
|
12
|
+
DEFAULT_PROCESS_OUTPUT_ROOT = '/tmp/jackal-executor'
|
13
|
+
|
14
|
+
# Setup the callback
|
15
|
+
def setup(*_)
|
16
|
+
require 'shellwords'
|
17
|
+
require 'tempfile'
|
18
|
+
require 'childprocess'
|
19
|
+
require 'fileutils'
|
20
|
+
@process_output_root = config.fetch(:process_output_root, DEFAULT_PROCESS_OUTPUT_ROOT)
|
21
|
+
FileUtils.mkdir_p(process_output_root)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Determine validity of message
|
25
|
+
#
|
26
|
+
# @param message [Carnivore::Message]
|
27
|
+
# @return [Truthy, Falsey]
|
28
|
+
def valid?(message)
|
29
|
+
super do |payload|
|
30
|
+
payload.get(:data, :commander, :action) ||
|
31
|
+
payload.get(:data, :commander, :actions)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Execute requested action
|
36
|
+
#
|
37
|
+
# @param message [Carnivore::Message]
|
38
|
+
def execute(message)
|
39
|
+
failure_wrap(message) do |payload|
|
40
|
+
actions = [
|
41
|
+
payload.get(:data, :commander, :action),
|
42
|
+
payload.get(:data, :commander, :actions)
|
43
|
+
].flatten.compact.uniq
|
44
|
+
cmds = actions.map do |action|
|
45
|
+
if(action.is_a?(Hash))
|
46
|
+
extra_args = action[:arguments]
|
47
|
+
action = action[:name]
|
48
|
+
else
|
49
|
+
extra_args = ''
|
50
|
+
end
|
51
|
+
if(cmd = config.get(:actions, action))
|
52
|
+
debug "Action maps to command: #{cmd.inspect}"
|
53
|
+
[action, cmd, extra_args]
|
54
|
+
else
|
55
|
+
error "No command mapping configured for requested action: #{action}"
|
56
|
+
raise KeyError.new("Invalid command mapping. Key not found: `#{action}`")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
results = cmds.map do |action, command, extra_args|
|
60
|
+
if(extra_args.is_a?(String))
|
61
|
+
command = "#{command} #{extra_args}"
|
62
|
+
end
|
63
|
+
cmd_input = Shellwords.shellsplit(command)
|
64
|
+
if(extra_args.is_a?(Array))
|
65
|
+
cmd_input += extra_args
|
66
|
+
end
|
67
|
+
process = ChildProcess.build(*cmd_input)
|
68
|
+
stdout = File.open(File.join(process_output_root, [payload[:id], 'stdout'].join('-')), 'w+')
|
69
|
+
stderr = File.open(File.join(process_output_root, [payload[:id], 'stderr'].join('-')), 'w+')
|
70
|
+
process.io.stdout = stdout
|
71
|
+
process.io.stderr = stderr
|
72
|
+
debug "Running requested action: #{action} (#{command})"
|
73
|
+
process.start
|
74
|
+
debug "Process started. Waiting for process completion (#{action})"
|
75
|
+
status = process.wait
|
76
|
+
stdout.rewind
|
77
|
+
stderr.rewind
|
78
|
+
if(status == 0)
|
79
|
+
info "Successfully executed action: #{action}"
|
80
|
+
info "STDOUT (#{action}): #{stdout.read.gsub("\n", '')}"
|
81
|
+
else
|
82
|
+
error "Failed to successfully execute action: #{action}"
|
83
|
+
error "Failed action exit code (#{action}): #{process.exit_code}"
|
84
|
+
error "STDOUT (#{action}): #{stdout.read.gsub("\n", '')}"
|
85
|
+
error "STDERR (#{action}): #{stderr.read.gsub("\n", '')}"
|
86
|
+
end
|
87
|
+
[action, {:exit_code => process.exit_code}]
|
88
|
+
end
|
89
|
+
payload.set(:data, :commander, :results, results)
|
90
|
+
job_completed(:commander, payload, message)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jackal-commander
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Roberts
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jackal
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: childprocess
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Command helpers
|
42
|
+
email: code@chrisroberts.org
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- CHANGELOG.md
|
48
|
+
- CONTRIBUTING.md
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
51
|
+
- jackal-commander.gemspec
|
52
|
+
- lib/jackal-commander.rb
|
53
|
+
- lib/jackal-commander/executor.rb
|
54
|
+
- lib/jackal-commander/version.rb
|
55
|
+
homepage: https://github.com/carnivore-rb/jackal-commander
|
56
|
+
licenses:
|
57
|
+
- Apache 2.0
|
58
|
+
metadata: {}
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 2.2.2
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Message processing helper
|
79
|
+
test_files: []
|
80
|
+
has_rdoc:
|