-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjenkins_status_light.py
184 lines (140 loc) · 4.86 KB
/
jenkins_status_light.py
1
2
3
4
5
6
7
8
9
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
import optparse
import signal
import sys
import threading
import time
import Queue
import shutil
from jenkinsapi.jenkins import Jenkins
from bibliopixel.animation import BaseStripAnim
from bibliopixel.drivers.LPD8806 import DriverLPD8806
from bibliopixel.drivers.driver_base import ChannelOrder
from bibliopixel.led import *
import bibliopixel.colors as colors
driver = DriverLPD8806(74, c_order=ChannelOrder.GRB, SPISpeed=2)
led = LEDStrip(driver)
# Global colors
failed_base_color = colors.Red
success_base_color = colors.Green
aborted_base_color = colors.SlateGray
building_color = colors.Gold
def parse_args():
"""Parses args"""
parser = optparse.OptionParser()
parser.add_option("", "--jenkins-url",
help="Base url of your jenkins server (ie: https://jenkins.example.com")
parser.add_option("", "--job",
help="Job for build light to monitor")
(opts, args) = parser.parse_args()
if not opts.jenkins_url and opts.job:
print >> sys.stderr, "Please specify a jenkins url and job"
sys.exit(2)
return opts
class FailurePattern(threading.Thread):
"""Failure not building Pattern"""
def __init__(self, queue):
self.led = led
self.queue = queue
threading.Thread.__init__(self)
def run(self):
while True:
previous_build_status = self.queue.get()[0]
currently_building = self.queue.get()[1]
if currently_building == False:
if previous_build_status == 'FAILURE':
self.led.fill(failed_base_color)
self.led.update()
time.sleep(.5)
class SuccessPattern(threading.Thread):
"""Success not building Pattern"""
def __init__(self, queue):
self.led = led
self.queue = queue
threading.Thread.__init__(self)
def run(self):
while True:
previous_build_status = self.queue.get()[0]
currently_building = self.queue.get()[1]
if currently_building == False:
if previous_build_status == 'SUCCESS':
self.led.fill(success_base_color)
self.led.update()
time.sleep(.5)
class AbortedPattern(threading.Thread):
"""Aborted not building Pattern"""
def __init__(self, queue):
self.led = led
self.queue = queue
threading.Thread.__init__(self)
def run(self):
while True:
previous_build_status = self.queue.get()[0]
currently_building = self.queue.get()[1]
if currently_building == False:
if previous_build_status == 'ABORTED':
self.led.fill(aborted_base_color)
self.led.update()
time.sleep(.5)
class BuildingPattern(threading.Thread):
"""Building Pattern"""
def __init__(self, queue):
self.led = led
self.queue = queue
threading.Thread.__init__(self)
def run(self):
while True:
previous_build_status = self.queue.get()[0]
currently_building = self.queue.get()[1]
if currently_building == True:
self.led.fill(building_color)
self.led.update()
time.sleep(.5)
class JenkinsStatus(threading.Thread):
"""Queries the status of Jenkins"""
def __init__(self, job, queue):
self.job = job
self.queue = queue
threading.Thread.__init__(self)
def run(self):
while True:
# Determine last completed build
previous_build = self.job.get_last_completed_build()
previous_build_status = previous_build.get_status()
# Determine if there's a build building
currently_building = self.job.is_running()
# Add jenkins statuses to the queue
self.queue.put((previous_build_status, currently_building))
time.sleep(.1)
def ctrlc(signal, frame):
print "Exiting . . ."
led.all_off()
led.update()
sys.exit(0)
def main():
opts = parse_args()
server = Jenkins(opts.jenkins_url)
job = server[(opts.job)]
queue = Queue.Queue()
led.all_off()
led.update()
jenkins_status = JenkinsStatus(job, queue)
success_pattern = SuccessPattern(queue)
building_pattern = BuildingPattern(queue)
failure_pattern = FailurePattern(queue)
aborted_pattern = AbortedPattern(queue)
print "Starting Jenkins Status Reader"
jenkins_status.start()
time.sleep(1)
print "Starting Success pattern"
success_pattern.start()
print "Starting Building pattern"
building_pattern.start()
print "Starting Failure pattern"
failure_pattern.start()
print "Starting Aborted pattern"
aborted_pattern.start()
if __name__ == "__main__":
# Override ctrl-c to kill threads
signal.signal(signal.SIGINT, ctrlc)
main()