Class: ZkExec::Runner

Inherits:
Object
  • Object
show all
Includes:
ZkExec
Defined in:
lib/zkexec/runner.rb

Constant Summary

Constants included from ZkExec

VERSION

Instance Method Summary collapse

Methods included from ZkExec

#log, #silence!

Constructor Details

#initializeRunner

Returns a new instance of Runner.



7
8
# File 'lib/zkexec/runner.rb', line 7

def initialize
end

Instance Method Details

#run(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/zkexec/runner.rb', line 10

def run(args)
  options = { :mirrors => [], :cluster => "localhost:2181", :health_interval => 30, :health_delay => 30 }
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: zkexec [options]\n\nRun a command, and restart if the config files change on the remote zookeeper.\n\n"

    opts.on("-e", "--exec COMMAND", "Run this command") do |s|
      options[:exec] = s
    end
    
    opts.on("-c", "--cluster HOST:PORT,...", "Comma-delimited list of zookeeper hosts") do |s|
      options[:cluster] = s
    end
    
    opts.on("-H", "--health COMMAND", "Run this command to health-check") do |s|
      options[:health] = s
    end
    
    opts.on("-i", "--health-interval INTERVAL", "Health-check every INTERVAL seconds") do |s|
      options[:health_interval] = s.to_f
    end
    
    opts.on("-d", "--health-delay INTERVAL", "Wait before starting health checks") do |s|
      options[:health_delay] = s.to_f
    end
    
    opts.on("-m", "--mirror LOCAL_PATH=ZK_PATH", "Mirror a config file from zookeeper to localhost") do |s|
      options[:mirrors] << s.split("=")
    end
    
    opts.on("-l", "--lock NAME", "Name of a zk lockfile, used to enforce rolling restarts") do |s|
      options[:lock] = s
    end
            
    opts.on("-a", "--alert COMMAND", "Run this command if the primary command returns","falsey or health checks fail for too long") do |s|
      options[:alert] = s
    end
    
    opts.on("-v", "--verbose") do
      options[:verbose] = true
    end

    opts.on("-s", "--silent") do 
      options[:silent] = true
    end
    
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end
  
  opts.parse!
  
  unless options[:exec]
    puts "Missing required option --exec.  See --help for usage."
    exit 1
  end
  
  silence! if options[:silent]
  
  begin
    Executor.new(options).execute
  rescue => e
    if options[:verbose]
      raise
    else
      log(e.message)
      exit 1
    end
  end
end