Class: SnakeEyes
- Inherits:
-
Object
- Object
- SnakeEyes
- Defined in:
- lib/snakeeyes.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#debug_level ⇒ Object
Returns the value of attribute debug_level.
-
#master ⇒ Object
Returns the value of attribute master.
-
#omaster ⇒ Object
Returns the value of attribute omaster.
-
#path ⇒ Object
Returns the value of attribute path.
-
#run_count ⇒ Object
Returns the value of attribute run_count.
-
#sleep ⇒ Object
Returns the value of attribute sleep.
Instance Method Summary collapse
- #custom_sleep ⇒ Object
- #debug(message = "", level = 0) ⇒ Object
- #first_run ⇒ Object
- #git(command) ⇒ Object
- #has_new_commits ⇒ Object
- #hawk_config ⇒ Object
- #heartbeat ⇒ Object
-
#initialize(path) ⇒ SnakeEyes
constructor
A new instance of SnakeEyes.
- #main_loop ⇒ Object
-
#post_results(status, output, message, author, sha) ⇒ Object
General Hawk Stuff ##.
- #post_update(data) ⇒ Object
-
#report_tests(pass, output) ⇒ Object
report the output to general hawk.
-
#reset_to_newest_commit ⇒ Object
if something is new, reset to it.
- #run_tests ⇒ Object
- #sleepy_time ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(path) ⇒ SnakeEyes
Returns a new instance of SnakeEyes.
12 13 14 15 16 17 |
# File 'lib/snakeeyes.rb', line 12 def initialize(path) @path = path @sleep = 5 * 60 # 5 minutes @debug_level = 99 @run_count = 0 end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
10 11 12 |
# File 'lib/snakeeyes.rb', line 10 def config @config end |
#debug_level ⇒ Object
Returns the value of attribute debug_level.
10 11 12 |
# File 'lib/snakeeyes.rb', line 10 def debug_level @debug_level end |
#master ⇒ Object
Returns the value of attribute master.
10 11 12 |
# File 'lib/snakeeyes.rb', line 10 def master @master end |
#omaster ⇒ Object
Returns the value of attribute omaster.
10 11 12 |
# File 'lib/snakeeyes.rb', line 10 def omaster @omaster end |
#path ⇒ Object
Returns the value of attribute path.
10 11 12 |
# File 'lib/snakeeyes.rb', line 10 def path @path end |
#run_count ⇒ Object
Returns the value of attribute run_count.
10 11 12 |
# File 'lib/snakeeyes.rb', line 10 def run_count @run_count end |
#sleep ⇒ Object
Returns the value of attribute sleep.
10 11 12 |
# File 'lib/snakeeyes.rb', line 10 def sleep @sleep end |
Instance Method Details
#custom_sleep ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/snakeeyes.rb', line 50 def custom_sleep timer = git("config cijoe.sleep") if timer.size > 0 debug "SETTING CUSTOM TIMER: #{timer}" @sleep = timer.to_i end end |
#debug(message = "", level = 0) ⇒ Object
140 141 142 143 144 145 |
# File 'lib/snakeeyes.rb', line 140 def debug( = "", level = 0) if level <= @debug_level tabs = "\t" * level puts tabs + end end |
#first_run ⇒ Object
46 47 48 |
# File 'lib/snakeeyes.rb', line 46 def first_run @run_count == 0 end |
#git(command) ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/snakeeyes.rb', line 132 def git(command) out = '' status = POpen4::popen4("git #{command}") do |stdout, stderr, stdin, pid| out = stdout.read end out.chomp end |
#has_new_commits ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/snakeeyes.rb', line 89 def has_new_commits debug "check for new commits" # look at origin/master branch current_master = git("rev-parse origin/master") debug "current o/master : #{current_master}", 1 debug "fetching commits", 1 git('fetch') # look at origin/master branch again new_master = git("rev-parse origin/master") debug "new o/master : #{new_master}", 1 # set master branch SHA internally @omaster = new_master @master = git("rev-parse refs/heads/master") debug "master : #{@master}", 1 # return true if they differ new_master != current_master end |
#hawk_config ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/snakeeyes.rb', line 165 def hawk_config c = {} config = git('config --list') config.split("\n").each do |line| k, v = line.split('=') c[k] = v end url = '' u = c['remote.origin.url'] if m = /github\.com.(.*?)\/(.*?)\.git/.match(u) user = m[1] proj = m[2] url = "https://github.com/#{user}/#{proj}" end @config = { :server => c['hawk.server'], :token => c['hawk.token'], :agent => c['hawk.agent'], :description => c['hawk.description'], :url => url } end |
#heartbeat ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/snakeeyes.rb', line 113 def heartbeat debug "heartbeating" config = hawk_config data = { "agent" => config[:agent], "description" => config[:description], "url" => config[:url], "heartbeat" => '1', } post_update(data.to_json) # POST JSON TO URL end |
#main_loop ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/snakeeyes.rb', line 27 def main_loop while true begin debug "Start #{@path}" if has_new_commits || first_run reset_to_newest_commit pass, output = run_tests report_tests(pass, output) @run_count += 1 else heartbeat end rescue StandardError => e puts "!!! There was some issue or another: #{e.} !!!" end sleepy_time end end |
#post_results(status, output, message, author, sha) ⇒ Object
General Hawk Stuff ##
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/snakeeyes.rb', line 149 def post_results(status, output, , , sha) config = hawk_config data = { "agent" => config[:agent], "description" => config[:description], "url" => config[:url], "branch" => "master", "author" => , "sha" => sha, "status" => status, "message" => , "output" => output } post_update(data.to_json) # POST JSON TO URL end |
#post_update(data) ⇒ Object
189 190 191 192 193 194 |
# File 'lib/snakeeyes.rb', line 189 def post_update(data) config = hawk_config ws = "#{config[:server]}/update/#{config[:token]}" x = Net::HTTP.post_form(URI.parse(ws), {'data' => data}) pp x end |
#report_tests(pass, output) ⇒ Object
report the output to general hawk
81 82 83 84 85 86 87 |
# File 'lib/snakeeyes.rb', line 81 def report_tests(pass, output) status = pass ? 'good' : 'bad' debug "reporting test results [#{status}] {#{@master}}" data = git('log -1 --format="%s:;:%an" ' + @master) , = data.split(":;:") post_results(status, output, , , @master) end |
#reset_to_newest_commit ⇒ Object
if something is new, reset to it
59 60 61 62 63 |
# File 'lib/snakeeyes.rb', line 59 def reset_to_newest_commit debug "reset to newest commit (#{@omaster})" git("reset --hard #{@omaster}") @master = @omaster end |
#run_tests ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/snakeeyes.rb', line 65 def run_tests debug "run tests" command = git("config cijoe.runner") debug "running '#{command}'...", 1 output = '' status = POpen4::popen4(command) do |stdout, stderr, stdin, pid| stdin.close out = stdout.read err = stderr.read output = out + err end debug "test exitstatus : #{ status.exitstatus }", 2 [(status.exitstatus == 0), output] end |
#sleepy_time ⇒ Object
125 126 127 128 129 130 |
# File 'lib/snakeeyes.rb', line 125 def sleepy_time debug debug "OK, sleeping for a while (#{@sleep})..." debug Kernel.sleep @sleep end |
#start ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/snakeeyes.rb', line 19 def start Dir.chdir(@path) do custom_sleep debug "Starting loop" main_loop end end |