This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #112: Add dramble-node-monitor script and service for Blinkstic…
…k LED control.
- Loading branch information
1 parent
bbbb8a1
commit 4ec1312
Showing
5 changed files
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[Unit] | ||
Description=Start the Pi Dramble Node monitor script. | ||
After=network.target | ||
|
||
[Service] | ||
Type=simple | ||
ExecStart=/usr/bin/dramble-node-monitor | ||
RemainAfterExit=true | ||
Restart=on-failure | ||
RestartSec=10s | ||
StandardOutput=journal | ||
StandardError=journal | ||
User=root | ||
Group=root | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
- name: Copy systemd unit file into place. | ||
copy: | ||
src: dramble-node-monitor.service | ||
dest: /lib/systemd/system/dramble-node-monitor.service | ||
mode: 0644 | ||
register: dramble_node_monitor_service | ||
|
||
- name: Reload systemd after adding service. | ||
systemd: | ||
state: stopped | ||
daemon_reload: true | ||
name: dramble-node-monitor | ||
when: dramble_node_monitor_service.changed | ||
|
||
- name: Ensure dramble-node-monitor is running and enabled at boot. | ||
service: | ||
name: dramble-node-monitor | ||
state: started | ||
enabled: yes | ||
when: deploy_target == 'pi' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Kubernetes node monitoring script for the Raspberry Pi Dramble. | ||
|
||
from blinkstick import blinkstick | ||
import urllib2 | ||
import time | ||
import sys, signal | ||
|
||
# Class to handle SIGINT/SIGTERM gracefully. | ||
class GracefulKiller: | ||
kill_now = False | ||
def __init__(self): | ||
signal.signal(signal.SIGINT, self.exit_gracefully) | ||
signal.signal(signal.SIGTERM, self.exit_gracefully) | ||
|
||
def exit_gracefully(self,signum, frame): | ||
self.kill_now = True | ||
|
||
# Define what to do when script is stopped externally. | ||
def signal_handler(signal, frame): | ||
print "\nStopping node monitor." | ||
nano.turn_off() | ||
sys.exit(0) | ||
signal.signal(signal.SIGINT, signal_handler) | ||
|
||
# Function to check if Kubernetes node is ready or not. | ||
def check_node_status(): | ||
status = False; | ||
|
||
try: | ||
contents = urllib2.urlopen(url="http://localhost:10248/healthz", timeout=1).read() | ||
if contents == 'ok': | ||
status = True | ||
else: | ||
status = False | ||
|
||
except urllib2.URLError: | ||
status = False | ||
|
||
return status | ||
|
||
# Function to update Blinkstick light to reflect current node status. | ||
def update_blinkenlights(nano): | ||
current_color = nano.get_color() | ||
if check_node_status(): | ||
if current_color != [0, 128, 0]: | ||
nano.set_color(red=0, green=128, blue=0) | ||
print 'Blinkstick changed to reflect Ready status.' | ||
else: | ||
if current_color != [128, 0, 0]: | ||
nano.set_color(red=128, green=0, blue=0) | ||
print 'Blinkstick changed to reflect NotReady status.' | ||
time.sleep(5) | ||
|
||
# Main. | ||
nano = blinkstick.find_first() | ||
nano.set_mode(3) | ||
time.sleep(0.05) | ||
killer = GracefulKiller() | ||
while True: | ||
try: | ||
update_blinkenlights(nano) | ||
if killer.kill_now: | ||
break | ||
except Exception, e: | ||
nano.turn_off() | ||
|
||
print >> sys.stderr, "Exiting for exception." | ||
print >> sys.stderr, "Exception: %s" % str(e) | ||
sys.exit(1) | ||
nano.turn_off() | ||
print "Exiting gracefully." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
--- | ||
blinkstick_scripts: [] | ||
blinkstick_scripts: | ||
- dramble-node-monitor |