Class: TunnelVision::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/tunnel_vision/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner

Returns a new instance of Runner.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tunnel_vision/runner.rb', line 6

def initialize args
  if args.empty?
    puts "Command me!"
    puts help
    exit 0
  end

  @tunnel = TunnelVision::Tunnel.new

  case args.first
  when 'generate'
    generate
  when 'start'
    start
  when 'status'
    status
  when 'stop'
    stop
  else
    puts "Unrecognized command"
  end
end

Instance Method Details

#generateObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tunnel_vision/runner.rb', line 78

def generate
  puts "Generating tunnels.yaml"
  example = [
    {
      'description' => 'irc on staging server',
      "user" => 'bob',
      'host' => 'example.com',
      'tunnel' => '123:123.0.0.1:80'
    },
    {
      'description' => 'db from database server',
      'user' => 'clyde',
      'host' => 'bar.foo.com',
      'tunnel' => '7777:bar.foo.com:8080'
    }
  ]

  begin
    File.open('tunnels.yaml', 'w') do |file|
      YAML::dump(example, file)
    end

  rescue "Error when creating tunnels.yaml"
  end
  puts "Created!"
end

#helpObject



70
71
72
73
74
75
76
# File 'lib/tunnel_vision/runner.rb', line 70

def help
  "    tunnelvision generate - generate example tunnels file\n    tunnelvision start - start all ssh tunnels defined in tunnels.yaml\n    tunnelvision stop - stop all opened ssh tunnels\n  HELP\nend\n"

#startObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tunnel_vision/runner.rb', line 29

def start
  begin
    tunnels = YAML::load_file 'tunnels.yaml'
  rescue
    puts "No tunnels file!"
    exit 1
  end

  current = {}
  tunnels.each do |tunnel_def|
    puts "Starting:\n\t#{tunnel_def['description']}"
    pid = @tunnel.add tunnel_def['tunnel'], tunnel_def['user'], tunnel_def['host']
    current[pid] = tunnel_def['description']
  end

  File.open('.opened_tunnels','w') do |file|
    YAML::dump(current, file)
  end
end

#statusObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tunnel_vision/runner.rb', line 49

def status
  begin
    current = YAML::load_file '.opened_tunnels'
  rescue
    puts "No tunnels or .opened_tunnels files is locked/deleted"
    exit 0
  end

  puts "Current tunnels"
  current.each do |id, description|
    puts "\t#{description} (#{id})"
  end
end

#stopObject



63
64
65
66
67
68
# File 'lib/tunnel_vision/runner.rb', line 63

def stop
  @tunnel.pids = YAML::load_file('.opened_tunnels').keys
  @tunnel.close_all!
  FileUtils.rm '.opened_tunnels'

end