-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjenkins_tower_light_hat.py
executable file
·241 lines (200 loc) · 6.5 KB
/
jenkins_tower_light_hat.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python
from __future__ import print_function
import time
import threading
import jenkinsapi
import config as cfg
import signal
import automationhat
import copy
from jenkinsapi.jenkins import Jenkins
from requests.exceptions import ConnectionError
# ---------------------------------------------------- #
# -- General output information -- #
# ---------------------------------------------------- #
# -- RED = output.one -- #
# -- YELLOW = output.two -- #
# -- GREEN = output.three -- #
# -- BUZZER = relay.one = unused at this moment -- #
# ---------------------------------------------------- #
# Global variables
building = False
error = False
keepAlive = True
# ---------------------------------------------------- #
# -- All globally used functions -- #
# ---------------------------------------------------- #
# Function to toggle everything off
def all_tower_off():
automationhat.output.one.off()
automationhat.output.two.off()
automationhat.output.three.off()
automationhat.relay.one.off()
return
# For the automation HAT, turn of the LED's.
def all_hat_off():
if automationhat.is_automation_hat():
automationhat.light.power.off()
automationhat.light.comms.off()
automationhat.light.warn.off()
return
# Toggle function with parameter output and duration
def toggle(output_type, index, duration):
# Create the function based on the parameters
result = getattr(automationhat, output_type)
full_function = getattr(result, index)
full_function.on()
time.sleep(duration)
full_function.off()
return
# Set according to status
def set_status(status):
if status != "":
all_tower_off()
if status == "SUCCESS":
automationhat.output.three.on()
if status == "UNSTABLE":
automationhat.output.two.on()
if status == "FAILURE":
automationhat.output.one.on()
return
def set_error(value):
global error
if value is True or value is False:
error = value
if value is True:
if automationhat.is_automation_hat():
automationhat.light.warn.on()
else:
if automationhat.is_automation_hat():
automationhat.light.warn.off()
else:
error = True
if automationhat.is_automation_hat():
automationhat.light.warn.on()
print("[ERROR] Only supply True or False to the setError function")
# Get all the jobs by a given name.
def get_jenkins_job_by_name(name):
# Support sub-folders.
if '/' in name:
folder, name = name.rsplit('/', 1)
server = copy.deepcopy(J)
folder_path = '/'.join([f'job/{x}' for x in folder.split('/')])
server.baseurl = server.baseurl + '/' + folder_path
return server.get_job(name)
else:
return J.get_job(name)
def check_jobs_build_status():
if automationhat.is_automation_hat():
automationhat.light.comms.on()
jobs = cfg.jobs
success = 0
unstable = 0
failed = 0
for item in jobs:
try:
job = get_jenkins_job_by_name(item)
except jenkinsapi.custom_exceptions.UnknownJob:
set_error(True)
else:
getstatus = job.get_last_build().get_status()
if getstatus == "SUCCESS":
success += 1
continue
if getstatus == "UNSTABLE":
unstable += 1
continue
if getstatus == "FAILURE":
failed += 1
continue
if success > 0 and unstable == 0 and failed == 0:
set_status('SUCCESS')
if unstable > 0 and failed == 0:
set_status('UNSTABLE')
if failed > 0:
set_status('FAILURE')
if automationhat.is_automation_hat():
automationhat.light.comms.off()
def check_jobs_building():
if automationhat.is_automation_hat():
automationhat.light.comms.on()
global building
jobs = cfg.jobs
building = 0
for item in jobs:
try:
job = get_jenkins_job_by_name(item)
except jenkinsapi.custom_exceptions.UnknownJob:
set_error(True)
else:
if job.is_running():
building += 1
if building > 0:
building = True
else:
building = False
check_jobs_build_status()
if automationhat.is_automation_hat():
automationhat.light.comms.off()
# ---------------------------------------------------- #
# -- Startup of the script -- #
# ---------------------------------------------------- #
# Turn all off if any inputs are still high
all_hat_off()
all_tower_off()
if automationhat.is_automation_hat():
automationhat.light.power.on()
# Toggle everything once
toggle("output", "one", .4)
toggle("output", "two", .4)
toggle("output", "three", .4)
# Configure the Jenkins parameter and get the job
try:
J = Jenkins(cfg.jenkinsurl, username=cfg.username, password=cfg.password)
except jenkinsapi.custom_exceptions.JenkinsAPIException:
set_error(True)
print("[ERROR] Jenkins authentication error!")
except ConnectionError:
set_error(True)
print("[ERROR] Jenkins connection error!")
else:
set_error(False)
print("[INFO] No Jenkins error!")
# Get the status of the latest build before starting the threads
check_jobs_build_status()
# Thread for the blinking function
# Every 10s blink for 3s when we are building
def blinking():
if keepAlive:
threading.Timer(10.0, blinking).start()
# Only blink when we are actually building
if building or error:
# If error, blink red.
if error:
all_tower_off()
toggle("output", "one", 3)
else:
all_tower_off()
toggle("output", "two", 3)
# Check every 10s if we are building, if not or done get latest status
def build_running():
if keepAlive:
threading.Timer(10.0, build_running).start()
if not error:
check_jobs_building()
# Initiate the threads
blinking()
if not error:
build_running()
# Catch the keyboard interrupt and stop the threads from executing
def main():
global keepAlive
while keepAlive:
try:
time.sleep(1)
except KeyboardInterrupt:
print("[NOTICE] Jenkins Light terminated!")
all_tower_off()
all_hat_off()
keepAlive = False
main()