awt 0.0.2 → 0.0.3
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 +4 -4
- data/README.md +22 -0
- data/lib/awt.rb +1 -0
- data/lib/awt/server.rb +24 -3
- data/lib/awt/version.rb +1 -1
- data/spec/awt/server_spec.rb +27 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1814106982fdedf5ba321096decea1ea902bec0
|
4
|
+
data.tar.gz: 4ef116fdbc3c99b4fd309fe35d1a265c04a71131
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee9dc653d1a9ac8feafa9ea221a46a533dffb28a1991421a2f9e774817750dcad133e0285b38e9002e663e1488b0947c8c6784751d3e14bf8fe0a50ad3216135
|
7
|
+
data.tar.gz: f2b372d74d5df8c0695d30e12d12a8e6b126550c10629bcf05c73f565810784a6cffcd40755334ebf0171b10a966a47f3315abe05490a4efc413812d226afaad
|
data/README.md
CHANGED
@@ -49,6 +49,28 @@ task :task_name do
|
|
49
49
|
end
|
50
50
|
```
|
51
51
|
|
52
|
+
`run` is returns a result object.
|
53
|
+
Result object with a exit-status, etc.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
if run("cat /etc/centos-release").status == 0
|
57
|
+
puts "Hello CentOS"
|
58
|
+
end
|
59
|
+
|
60
|
+
if run("uname -a").data.chomp == "Linux"
|
61
|
+
puts "Hello Linux"
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
Using the `with_env` method if you want to use the environment variable.
|
66
|
+
Key is applied upcase it is converted to String.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
with_env key: "value" do
|
70
|
+
run "echo $KEY"
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
52
74
|
#### Execute
|
53
75
|
|
54
76
|
Awt task executable as follows:
|
data/lib/awt.rb
CHANGED
data/lib/awt/server.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Awt
|
2
2
|
class Server
|
3
|
-
attr_accessor :host, :user, :options
|
3
|
+
attr_accessor :host, :user, :options, :envs
|
4
4
|
|
5
5
|
def initialize(host: "localhost", port: 22, user: nil, key: "~/.ssh/id_rsa")
|
6
6
|
@host = host
|
@@ -9,13 +9,34 @@ module Awt
|
|
9
9
|
@printer = Printer.new(@host)
|
10
10
|
end
|
11
11
|
|
12
|
+
def with_env(envs, &block)
|
13
|
+
env = []
|
14
|
+
envs.each {|k,v| env << "export #{k.to_s.upcase}=\"#{v}\""}
|
15
|
+
@envs = env.join(";") unless env.empty?
|
16
|
+
self.instance_eval &block
|
17
|
+
@envs = nil
|
18
|
+
end
|
19
|
+
|
12
20
|
def run(cmd)
|
13
21
|
reset_printer_host
|
22
|
+
cmd = "#{@envs};#{cmd}" if @envs
|
23
|
+
result = OpenStruct.new
|
24
|
+
|
14
25
|
Net::SSH.start(@host, @user, @options) do |ssh|
|
15
26
|
@printer.print_run cmd
|
16
|
-
|
17
|
-
|
27
|
+
ssh.open_channel do |channel|
|
28
|
+
channel.exec cmd do |ch, success|
|
29
|
+
channel.on_data {|ch,data| result.data = data}
|
30
|
+
channel.on_extended_data {|ch,type,data| result.extended_data = data}
|
31
|
+
channel.on_request("exit-status") {|ch,data| result.status = data.read_long}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
ssh.loop
|
18
35
|
end
|
36
|
+
|
37
|
+
@printer.print_out result.data if result.data
|
38
|
+
@printer.print_ext result.extended_data if result.extended_data
|
39
|
+
result
|
19
40
|
end
|
20
41
|
|
21
42
|
def put(local, remote)
|
data/lib/awt/version.rb
CHANGED
data/spec/awt/server_spec.rb
CHANGED
@@ -53,22 +53,47 @@ describe Awt::Server do
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
+
describe "#with_env" do
|
57
|
+
it "should call instance methods" do
|
58
|
+
server.should_receive(:run)
|
59
|
+
server.with_env(key: :value) {run "example"}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
56
63
|
describe "#run" do
|
64
|
+
let(:channel) {double.as_null_object}
|
65
|
+
|
57
66
|
before do
|
67
|
+
channel.stub(:exec).with("example").and_yield(double, double)
|
68
|
+
ssh.stub(:open_channel).and_yield(channel)
|
58
69
|
Net::SSH.stub(:start).and_yield(ssh)
|
59
70
|
Awt::Printer.any_instance.stub(:print_run)
|
60
71
|
Awt::Printer.any_instance.stub(:print_out)
|
72
|
+
Awt::Printer.any_instance.stub(:print_ext)
|
61
73
|
end
|
62
74
|
|
63
75
|
after {server.run("example")}
|
64
76
|
|
65
77
|
it "should call Awt::Printer#print_*" do
|
66
78
|
Awt::Printer.any_instance.should_receive(:print_run)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should call #on_data" do
|
82
|
+
channel.should_receive(:on_data).and_yield(double, double)
|
67
83
|
Awt::Printer.any_instance.should_receive(:print_out)
|
68
84
|
end
|
69
85
|
|
70
|
-
it "should call #
|
71
|
-
|
86
|
+
it "should call #on_extended_data" do
|
87
|
+
channel.should_receive(:on_extended_data).and_yield(double, double, double)
|
88
|
+
Awt::Printer.any_instance.should_receive(:print_ext)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should call #on_request" do
|
92
|
+
channel.should_receive(:on_request).with("exit-status").and_yield(double, double.as_null_object)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should call #loop" do
|
96
|
+
ssh.should_receive(:loop)
|
72
97
|
end
|
73
98
|
|
74
99
|
it "should call #reset_printer_host" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- i2bskn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-ssh
|
@@ -131,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
131
|
version: '0'
|
132
132
|
requirements: []
|
133
133
|
rubyforge_project:
|
134
|
-
rubygems_version: 2.1.
|
134
|
+
rubygems_version: 2.1.9
|
135
135
|
signing_key:
|
136
136
|
specification_version: 4
|
137
137
|
summary: Awt is cli tool for system administration.
|