sheepsafe 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ === 0.2.6
2
+
3
+ - Fix error with proxy when ssh_port is not configured
4
+ - Try to log if proxy exits repeatedly
5
+ - Don't reopen stdout but capture it to the log file
6
+
1
7
  === 0.2.5
2
8
 
3
9
  - Wait a bit before starting proxy when switching networks
@@ -32,5 +32,12 @@ module Sheepsafe
32
32
  def write
33
33
  File.open(FILE, "w") {|f| f << YAML.dump(config) }
34
34
  end
35
+
36
+ def ssh_args
37
+ args = ""
38
+ args << "-p #{ssh_port} " if ssh_port
39
+ args << "-ND #{socks_port} #{ssh_host}"
40
+ args
41
+ end
35
42
  end
36
43
  end
@@ -9,22 +9,37 @@ module Sheepsafe
9
9
  class Controller
10
10
  LOG_FILE = Sheepsafe::Config::FILE.sub(/\.yml/, '.log')
11
11
 
12
+ class TeeStdout
13
+ def initialize(controller)
14
+ @stdout, @controller = $stdout, controller
15
+ end
16
+
17
+ def write(*args,&block)
18
+ @stdout.write(*args, &block)
19
+ @controller.with_log_file {|f| f.write(*args, &block) }
20
+ end
21
+
22
+ def method_missing(meth, *args, &block)
23
+ @stdout.send(meth, *args, &block)
24
+ @controller.with_log_file {|f| f.send(meth, *args, &block) }
25
+ end
26
+ end
27
+
12
28
  def initialize(config = nil, network = nil, logger = nil)
13
29
  @config = config || Sheepsafe::Config.new
14
30
  @network = network || Sheepsafe::Network.new(@config)
15
- @logger = logger || begin
16
- STDOUT.reopen(File.open(LOG_FILE, (File::WRONLY | File::APPEND)))
17
- Logger.new(STDOUT)
18
- end
31
+ @logger = logger
32
+ $stdout = TeeStdout.new(self)
19
33
  end
20
34
 
21
35
  def run
36
+ log("Sheepsafe starting")
37
+
22
38
  if ARGV.first == 'proxy' # 'sheepsafe proxy up/down/kick'
23
39
  bring_socks_proxy ARGV[1]
24
40
  return
25
41
  end
26
42
 
27
- log("Sheepsafe starting")
28
43
  if network_up?
29
44
  if network_changed?
30
45
  if switch_to_trusted?
@@ -88,11 +103,17 @@ module Sheepsafe
88
103
  exit 0
89
104
  end
90
105
  sleep 2 # wait a bit before starting proxy
106
+ exit_count = 0
107
+ ssh_command = "ssh #{@config.ssh_args}"
91
108
  loop do
92
109
  pid = fork do
93
- exec("ssh -p #{@config.ssh_port } -ND #{@config.socks_port} #{@config.ssh_host}")
110
+ exec(ssh_command)
94
111
  end
95
112
  Process.waitpid(pid)
113
+ exit_count += 1
114
+ if exit_count % 2 == 1 && exit_count < 10
115
+ log "command exited #{exit_count} times:\n#{}\nlast time with #{$?.exitstatus}"
116
+ end
96
117
  sleep 1
97
118
  end
98
119
  end
@@ -117,7 +138,15 @@ module Sheepsafe
117
138
  end
118
139
 
119
140
  def log(msg)
120
- @logger.info(msg)
141
+ if @logger
142
+ @logger.info(msg)
143
+ else
144
+ with_log_file {|f| Logger.new(f).info(msg) }
145
+ end
146
+ end
147
+
148
+ def with_log_file(&block)
149
+ File.open(LOG_FILE, (File::WRONLY | File::APPEND), &block)
121
150
  end
122
151
  end
123
152
  end
data/lib/sheepsafe.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Sheepsafe
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
4
4
 
5
5
  require 'sheepsafe/config'
data/sheepsafe.gemspec CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'sheepsafe'
5
- s.version = '0.2.5'
6
- s.date = '2010-11-19'
5
+ s.version = '0.2.6'
6
+ s.date = '2011-01-31'
7
7
 
8
8
  s.rubyforge_project = %q{caldersphere}
9
9
 
@@ -104,6 +104,23 @@ describe Sheepsafe::Controller do
104
104
  end
105
105
  end
106
106
 
107
+ describe Sheepsafe::Config, "#ssh_args" do
108
+ let(:config) { Sheepsafe::Config.new({"ssh_host" => "dummyhost", "socks_port" => "1234"}) }
109
+
110
+ it "should include -ND socks_port with SOCKS proxy port" do
111
+ config.ssh_args.should =~ /-ND 1234/
112
+ end
113
+
114
+ it "should not include -p ssh_port if none is specified" do
115
+ config.ssh_args.should == "-ND 1234 dummyhost"
116
+ end
117
+
118
+ it "should include -p ssh_port if present" do
119
+ config.ssh_port = "2323"
120
+ config.ssh_args.should == "-p 2323 -ND 1234 dummyhost"
121
+ end
122
+ end
123
+
107
124
  describe Sheepsafe::Network do
108
125
  let(:current_network) { Sheepsafe::Network.new }
109
126
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sheepsafe
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 5
10
- version: 0.2.5
9
+ - 6
10
+ version: 0.2.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nick Sieger
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-19 00:00:00 -06:00
18
+ date: 2011-01-31 00:00:00 -06:00
19
19
  default_executable: sheepsafe
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency