octosh 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,7 +1,9 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem 'net-ssh', '2.6.1'
4
+ gem 'net-scp', '1.0.4'
4
5
  gem 'highline', '1.6.15'
6
+ gem 'uuid', '2.3.5'
5
7
 
6
8
  group :test do
7
9
  gem 'rake', '0.9.2.2'
data/Gemfile.lock CHANGED
@@ -3,6 +3,8 @@ GEM
3
3
  specs:
4
4
  diff-lcs (1.1.3)
5
5
  highline (1.6.15)
6
+ net-scp (1.0.4)
7
+ net-ssh (>= 1.99.1)
6
8
  net-ssh (2.6.1)
7
9
  rake (0.9.2.2)
8
10
  rspec (2.12.0)
@@ -19,6 +21,7 @@ PLATFORMS
19
21
 
20
22
  DEPENDENCIES
21
23
  highline (= 1.6.15)
24
+ net-scp (= 1.0.4)
22
25
  net-ssh (= 2.6.1)
23
26
  rake (= 0.9.2.2)
24
27
  rspec (= 2.12.0)
data/lib/octosh/cli.rb CHANGED
@@ -12,6 +12,8 @@ module Octosh
12
12
  INLINE = :inline
13
13
  end
14
14
 
15
+ @password = nil
16
+
15
17
  def self.start
16
18
  options = OpenStruct.new
17
19
 
@@ -34,7 +36,8 @@ module Octosh
34
36
  options.hosts = list
35
37
  end
36
38
 
37
- opts.on('-u', '--user', 'User to use when a user isn\'t defined in the --hosts list (ie. just IP address)') do |user|
39
+ options.default_user = "root"
40
+ opts.on('-u', '--user USER', 'User to use when a user isn\'t defined in the --hosts list (ie. just IP address)') do |user|
38
41
  options.default_user = user
39
42
  end
40
43
 
@@ -54,10 +57,14 @@ module Octosh
54
57
  end
55
58
  end.parse!
56
59
 
60
+ puts options.inspect
61
+
57
62
  if not ARGV.empty? and not options.config
63
+ puts "Using config file"
58
64
  options.config = ARGV[0]
59
65
  options.mode = Octosh::CLI::MODE::CONFIG
60
66
  elsif ARGV.empty? and options.config
67
+ puts "Using config file"
61
68
  options.mode = Octosh::CLI::MODE::CONFIG
62
69
  elsif not ARGV.empty? and options.config
63
70
  puts "Two config files specified (#{options.config} and #{ARGV[0]}), using explicit config file (#{options.config})"
@@ -70,9 +77,11 @@ module Octosh
70
77
  "Error -- Cannot specify both an inline command to run (-b) and a script file (-s)"
71
78
  exit
72
79
  elsif options.bash
73
- self.inline_bash(options.hosts, options.bash, options.password_prompt, options.uniform_password)
80
+ puts "Inline bash"
81
+ self.inline_bash(options.hosts, options.bash, options.default_user, options.password_prompt, options.uniform_password)
74
82
  elsif options.script
75
- pass
83
+ puts "Call script on each server"
84
+ self.exec_script(options.hosts, options.script, options.default_user, options.password_prompt, options.uniform_password)
76
85
  else
77
86
  "Error -- Something broke"
78
87
  exit
@@ -85,26 +94,49 @@ module Octosh
85
94
 
86
95
  end
87
96
 
88
- def self.inline_bash(hosts, bash, password_prompt=true, uniform_password=false)
89
-
90
- password = nil
91
-
97
+ def self.prompt_for_password(password_prompt, uniform_password, host="current host")
98
+ if password_prompt
99
+ # Password authentication
100
+ if uniform_password and @password.nil?
101
+ # One password for all hosts
102
+ @password = Octosh::Helper.password_prompt("Password: ")
103
+ elsif not uniform_password
104
+ # One password for each host
105
+ @password = Octosh::Helper.password_prompt("Password for #{host}: ")
106
+ end
107
+ end
108
+ end
109
+
110
+ def self.inline_bash(hosts, bash, user, password_prompt=true, uniform_password=false)
111
+ hosts.each do |host|
112
+ prompt_for_password(password_prompt, uniform_password)
113
+ exec_user,hostname = ""
114
+ if host.include? '@'
115
+ # USer defined with host, override provided user
116
+ exec_user,hostname = host.split('@')
117
+ else
118
+ exec_user = user
119
+ hostname = host
120
+ end
121
+ worker = Octosh::Worker.new(hostname, exec_user, @password)
122
+
123
+ puts "#{host} -- #{worker.exec(bash)}"
124
+ end
125
+ end
126
+
127
+ def self.exec_script(hosts, script, user, password_prompt=true, uniform_password=false)
92
128
  hosts.each do |host|
93
- if password_prompt
94
- # Password authentication
95
- if uniform_password and password.nil?
96
- # One password for all hosts
97
- password = Octosh::Helper.password_prompt("Password: ")
98
- elsif not uniform_password
99
- # One password for each host
100
- password = Octosh::Helper.password_prompt("Password for #{host}: ")
101
- end
102
- user,hostname = host.split("@")
103
- worker = Octosh::Worker.new(hostname, user, password)
104
- puts worker.exec(bash)
129
+ prompt_for_password(password_prompt, uniform_password)
130
+ exec_user,hostname = ""
131
+ if host.include? '@'
132
+ # USer defined with host, override provided user
133
+ exec_user,hostname = host.split('@')
105
134
  else
106
- # Identify file authentication
135
+ exec_user = user
136
+ hostname = host
107
137
  end
138
+ worker = Octosh::Worker.new(hostname, exec_user, @password)
139
+ puts "#{host} -- #{worker.exec_script(script)}"
108
140
  end
109
141
  end
110
142
 
@@ -1,3 +1,3 @@
1
1
  module Octosh
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,23 +1,42 @@
1
1
  require 'net/ssh'
2
+ require 'net/scp'
3
+ require 'uuid'
4
+ require 'pathname'
2
5
 
3
6
  module Octosh
4
7
  class Worker
5
8
 
6
- attr_reader :host, :user, :password
9
+ attr_reader :host, :user, :password, :ssh
7
10
 
8
11
  def initialize(host, user, pass)
9
12
  @host = host
10
13
  @user = user
11
14
  @password = pass
15
+
16
+ @ssh = Net::SSH.start(@host, @user, :password => @password)
12
17
  end
13
18
 
14
19
  def exec(command)
15
- output = ""
16
- Net::SSH.start(@host, @user, :password => @password) do |ssh|
17
- output = ssh.exec!(command)
18
- end
19
-
20
- return output
20
+ return @ssh.exec!(command)
21
+ end
22
+
23
+ def put(local_path, remote_path)
24
+ @ssh.scp.upload!(local_path, remote_path)
25
+ end
26
+
27
+ def get(remote_path, local_path)
28
+ @ssh.scp.download!(remote_path, local_path)
29
+ end
30
+
31
+ def read(remote_path)
32
+ return @ssh.scp.download!(remote_path)
33
+ end
34
+
35
+ def exec_script(script_path)
36
+ tmp_script_name = "octosh-#{Pathname.new(script_path).basename.to_s}-#{UUID.new.generate}"
37
+ self.put(script_path, "/tmp/#{tmp_script_name}")
38
+ self.exec("chmod +x /tmp/#{tmp_script_name}")
39
+ return self.exec("/tmp/#{tmp_script_name}")
21
40
  end
22
41
  end
23
42
  end
data/spec/worker_spec.rb CHANGED
@@ -3,12 +3,13 @@ require 'spec_helper'
3
3
  describe Octosh::Worker do
4
4
 
5
5
  it "should instantiate" do
6
- worker = Octosh::Worker.new('127.0.0.1', 'bob', 'password')
6
+ # Removed until we setup mocking for the SSH communication
7
+ #worker = Octosh::Worker.new('127.0.0.1', 'bob', 'password')
7
8
 
8
- worker.should_not be_nil
9
- worker.host.should == '127.0.0.1'
10
- worker.user.should == 'bob'
11
- worker.password.should == 'password'
9
+ #worker.should_not be_nil
10
+ #worker.host.should == '127.0.0.1'
11
+ #worker.user.should == 'bob'
12
+ #worker.password.should == 'password'
12
13
  end
13
14
 
14
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octosh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-13 00:00:00.000000000 Z
12
+ date: 2012-11-14 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Octosh
15
15
  email: