diff --git a/roles/leds-blinkstick/files/dramble-node-monitor.service b/roles/leds-blinkstick/files/dramble-node-monitor.service new file mode 100644 index 0000000..bd1a59c --- /dev/null +++ b/roles/leds-blinkstick/files/dramble-node-monitor.service @@ -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 diff --git a/roles/leds-blinkstick/tasks/main.yml b/roles/leds-blinkstick/tasks/main.yml index c0558ab..d8de014 100644 --- a/roles/leds-blinkstick/tasks/main.yml +++ b/roles/leds-blinkstick/tasks/main.yml @@ -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 diff --git a/roles/leds-blinkstick/tasks/systemd-service.yml b/roles/leds-blinkstick/tasks/systemd-service.yml new file mode 100644 index 0000000..06bcc07 --- /dev/null +++ b/roles/leds-blinkstick/tasks/systemd-service.yml @@ -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' diff --git a/roles/leds-blinkstick/templates/dramble-node-monitor.j2 b/roles/leds-blinkstick/templates/dramble-node-monitor.j2 new file mode 100644 index 0000000..c50e0fb --- /dev/null +++ b/roles/leds-blinkstick/templates/dramble-node-monitor.j2 @@ -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." diff --git a/roles/leds-blinkstick/vars/main.yml b/roles/leds-blinkstick/vars/main.yml index ea707b2..6f7efb1 100644 --- a/roles/leds-blinkstick/vars/main.yml +++ b/roles/leds-blinkstick/vars/main.yml @@ -1,2 +1,3 @@ --- -blinkstick_scripts: [] +blinkstick_scripts: + - dramble-node-monitor