Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
Fixes #112: Add dramble-node-monitor script and service for Blinkstic…
Browse files Browse the repository at this point in the history
…k LED control.
  • Loading branch information
geerlingguy committed Feb 20, 2019
1 parent bbbb8a1 commit 4ec1312
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 7 deletions.
17 changes: 17 additions & 0 deletions roles/leds-blinkstick/files/dramble-node-monitor.service
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
8 changes: 2 additions & 6 deletions roles/leds-blinkstick/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@
- name: Make Blinkstick control scripts available in the user's PATH.
file:
path: "/usr/bin/{{ item }}"
src: "/opt/gpio-leds/{{ item }}"
src: "/opt/blinkstick-leds/{{ item }}"
state: link
with_items: "{{ blinkstick_scripts }}"

- name: Set up cron jobs for Blinkstick control.
cron: 'name="{{ item.name }}" special_time=reboot job="{{ item.job }}"'
with_items:
- name: "Set up blinkstick on startup"
job: "blinkstick --set-mode 3 && blinkstick --set-color {{ blinkstick_startup_color }}"
- include_tasks: systemd-service.yml
21 changes: 21 additions & 0 deletions roles/leds-blinkstick/tasks/systemd-service.yml
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'
73 changes: 73 additions & 0 deletions roles/leds-blinkstick/templates/dramble-node-monitor.j2
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."
3 changes: 2 additions & 1 deletion roles/leds-blinkstick/vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
blinkstick_scripts: []
blinkstick_scripts:
- dramble-node-monitor

0 comments on commit 4ec1312

Please sign in to comment.