Class: SnakeEyes

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def config
  @config
end

#debug_levelObject

Returns the value of attribute debug_level.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def debug_level
  @debug_level
end

#masterObject

Returns the value of attribute master.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def master
  @master
end

#omasterObject

Returns the value of attribute omaster.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def omaster
  @omaster
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def path
  @path
end

#run_countObject

Returns the value of attribute run_count.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def run_count
  @run_count
end

#sleepObject

Returns the value of attribute sleep.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def sleep
  @sleep
end

Instance Method Details

#custom_sleepObject



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(message = "", level = 0)
  if level <= @debug_level
    tabs = "\t" * level
    puts tabs + message 
  end
end

#first_runObject



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_commitsObject



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_configObject



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

#heartbeatObject



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_loopObject



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.message} !!!"
    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, message, author, sha)
  config = hawk_config
  data = {
      "agent"       => config[:agent],
      "description" => config[:description],
      "url"         => config[:url],
      "branch"      => "master",
      "author"      => author,
      "sha"         => sha,
      "status"      => status,
      "message"     => 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)
  message, author = data.split(":;:")
  post_results(status, output, message, author, @master)
end

#reset_to_newest_commitObject

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_testsObject



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_timeObject



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

#startObject



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