screwcap 0.8.1 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/screwcap/runner.rb +67 -5
- data/lib/screwcap.rb +1 -1
- data/screwcap.gemspec +1 -1
- data/spec/runner_spec.rb +22 -0
- metadata +4 -4
data/Rakefile
CHANGED
@@ -11,7 +11,7 @@ Hoe.plugin :newgem
|
|
11
11
|
# Generate all the Rake tasks
|
12
12
|
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
13
13
|
$hoe = Hoe.spec 'screwcap' do
|
14
|
-
self.version = '0.8.
|
14
|
+
self.version = '0.8.2'
|
15
15
|
self.developer 'Grant Ammons', 'grant@pipelinedeals.com'
|
16
16
|
self.rubyforge_name = self.name # TODO this is default value
|
17
17
|
self.extra_deps = [['net-ssh','>= 2.0.23'],['net-ssh-gateway','>=1.0.1'], ['net-scp','>=1.0.4']]
|
data/lib/screwcap/runner.rb
CHANGED
@@ -6,18 +6,82 @@ class Runner
|
|
6
6
|
@@silent = options[:silent]
|
7
7
|
@@verbose = options[:verbose]
|
8
8
|
task = options[:task]
|
9
|
-
results = []
|
10
9
|
|
11
10
|
if (task.__servers.nil? or task.__servers == [] or task.__servers.compact == []) and task.__built_commands.any? {|c| c[:type] == :remote or c[:type] == :scp }
|
12
11
|
raise Screwcap::ConfigurationError, "The task #{task.name} includes remote commands, however no servers were defined for this task."
|
13
12
|
end
|
14
13
|
|
14
|
+
connections = []
|
15
|
+
|
16
|
+
_log "\nExecuting task #{task.name}\n", :color => :blue
|
17
|
+
|
18
|
+
if task.__options[:parallel] == false
|
19
|
+
run_serially(task, options)
|
20
|
+
else
|
21
|
+
run_parallel(task, options)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def self.run_serially(task, options)
|
28
|
+
results = []
|
29
|
+
connections = []
|
15
30
|
if options[:servers] and task.__servers
|
16
|
-
servers = options[:servers].select {|s| task.__servers.include? s.__name }
|
31
|
+
servers = options[:servers].select {|s| task.__servers.flatten.include? s.__name }
|
17
32
|
connections = servers.map {|server| server.connect! }.flatten
|
18
33
|
end
|
19
34
|
|
20
|
-
|
35
|
+
connections.each do |connection|
|
36
|
+
task.__built_commands.each do |command|
|
37
|
+
ret = case command[:type]
|
38
|
+
when :remote
|
39
|
+
results << run_remote_command(command, connection[:connection], options)
|
40
|
+
if command[:block]
|
41
|
+
opts = task.__options.clone.merge(:stderr => command[:stderr], :stdout => command[:stdout], :exit_code => command[:exit_code])
|
42
|
+
opts[:servers] = task.__servers
|
43
|
+
opts[:name] = "Run results"
|
44
|
+
|
45
|
+
inner_task = Task.new(opts, &command[:block])
|
46
|
+
inner_task.__build_commands(options[:tasks])
|
47
|
+
results << self.execute!(options.merge(:task => inner_task))
|
48
|
+
end
|
49
|
+
when :local
|
50
|
+
result = {}
|
51
|
+
result[:stdout] = `#{command[:command]}`
|
52
|
+
result[:exit_code] = $?.to_i
|
53
|
+
results << result
|
54
|
+
if $?.to_i == 0
|
55
|
+
if options[:verbose]
|
56
|
+
_log " O: #{ret}\n", :color => :green
|
57
|
+
else
|
58
|
+
_log(".", :color => :green)
|
59
|
+
end
|
60
|
+
else
|
61
|
+
_errorlog(" E: (local): #{command[:command]} return exit code: #{$?}\n", :color => :red) if $? != 0
|
62
|
+
end
|
63
|
+
when :scp
|
64
|
+
threads = []
|
65
|
+
servers.each do |server|
|
66
|
+
threads << Thread.new(server) { |_server| _server.upload! command[:local], command[:remote] }
|
67
|
+
end
|
68
|
+
threads.each {|t| t.join }
|
69
|
+
when :block
|
70
|
+
command[:block].call
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
_log "Complete\n", :color => :blue
|
75
|
+
results
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.run_parallel(task, options)
|
79
|
+
results = []
|
80
|
+
connections = []
|
81
|
+
if options[:servers] and task.__servers
|
82
|
+
servers = options[:servers].select {|s| task.__servers.flatten.include? s.__name }
|
83
|
+
connections = servers.map {|server| server.connect! }.flatten
|
84
|
+
end
|
21
85
|
|
22
86
|
task.__built_commands.each do |command|
|
23
87
|
ret = case command[:type]
|
@@ -72,8 +136,6 @@ class Runner
|
|
72
136
|
results
|
73
137
|
end
|
74
138
|
|
75
|
-
private
|
76
|
-
|
77
139
|
def self.run_remote_command(command, ssh, options)
|
78
140
|
stdout, stderr, exit_code, exit_signal = ssh_exec! ssh, command[:command]
|
79
141
|
ret = {:command => command[:command]}
|
data/lib/screwcap.rb
CHANGED
data/screwcap.gemspec
CHANGED
data/spec/runner_spec.rb
CHANGED
@@ -86,6 +86,28 @@ describe "The Runner" do
|
|
86
86
|
commands = Runner.execute! :name => :runblock, :task => task, :tasks => [task, revert_task], :servers => [@server], :silent => true
|
87
87
|
command_names(commands).should == %w(do_something revert failed)
|
88
88
|
end
|
89
|
+
|
90
|
+
it "should be able to run tasks parallel or serial" do
|
91
|
+
@server2 = Server.new :name => :server2, :address => "fake2.com", :user => "fake"
|
92
|
+
Runner.stubs(:ssh_exec!).returns(["ok\n","",0,nil])
|
93
|
+
task = Task.new :name => :parallel_task, :servers => [:server, :server2] do
|
94
|
+
run "one"
|
95
|
+
run "two"
|
96
|
+
end
|
97
|
+
task.__build_commands
|
98
|
+
task.validate([@server, @server2])
|
99
|
+
commands = Runner.execute! :name => :parallel_task, :task => task, :tasks => [task], :servers => [@server, @server2]
|
100
|
+
command_names(commands).should == %w(one one two two)
|
101
|
+
|
102
|
+
task = Task.new :name => :serial_task, :parallel => false, :servers => [:server, :server2] do
|
103
|
+
run "one"
|
104
|
+
run "two"
|
105
|
+
end
|
106
|
+
task.__build_commands
|
107
|
+
task.validate([@server, @server2])
|
108
|
+
commands = Runner.execute! :name => :serial_task, :task => task, :tasks => [task], :servers => [@server, @server2], :silent => true
|
109
|
+
command_names(commands).should == %w(one two one two)
|
110
|
+
end
|
89
111
|
end
|
90
112
|
|
91
113
|
def command_names(commands)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: screwcap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 59
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 8
|
9
|
-
-
|
10
|
-
version: 0.8.
|
9
|
+
- 2
|
10
|
+
version: 0.8.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Grant Ammons
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-04-
|
18
|
+
date: 2011-04-27 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|