diff options
Diffstat (limited to 'common/recipes-utils/spatula')
-rw-r--r-- | common/recipes-utils/spatula/files/setup-spatula.sh | 80 | ||||
-rw-r--r-- | common/recipes-utils/spatula/files/spatula_wrapper.py | 120 | ||||
-rw-r--r-- | common/recipes-utils/spatula/spatula_0.1.bb | 47 |
3 files changed, 247 insertions, 0 deletions
diff --git a/common/recipes-utils/spatula/files/setup-spatula.sh b/common/recipes-utils/spatula/files/setup-spatula.sh new file mode 100644 index 0000000..391a212 --- /dev/null +++ b/common/recipes-utils/spatula/files/setup-spatula.sh @@ -0,0 +1,80 @@ +#!/bin/sh +# +# Copyright 2015-present Facebook. All Rights Reserved. +# +# This program file is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program in a file named COPYING; if not, write to the +# Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301 USA +# + +### BEGIN INIT INFO +# Provides: setup-spatula-wrapper +# Required-Start: +# Required-Stop: +# Default-Start: S +# Default-Stop: +# Short-Description: Set Spatula Wrapper +### END INIT INFO + +# source function library +. /etc/init.d/functions + +ACTION="$1" +CMD="/usr/local/bin/spatula_wrapper.py" +case "$ACTION" in + start) + echo -n "Setting up Spatula: " + pid=$(ps | grep -v grep | grep $CMD | awk '{print $1}') + if [ $pid ]; then + echo "already running" + else + $CMD > /var/log/spatula.log 2>&1 & + echo "done." + fi + ;; + stop) + echo -n "Stopping Spatula: " + pid=$(ps | grep -v grep | grep $CMD | awk '{print $1}') + if [ $pid ]; then + kill $pid + fi + echo "done." + ;; + restart) + echo -n "Restarting Spatula: " + pid=$(ps | grep -v grep | grep $CMD | awk '{print $1}') + if [ $pid ]; then + kill $pid + fi + sleep 1 + $CMD > /var/log/spatula.log 2>&1 & + echo "done." + ;; + status) + if [[ -n $(ps | grep -v grep | grep $CMD | awk '{print $1}') ]]; then + echo "Spatula is running" + else + echo "Spatula is stopped" + fi + ;; + *) + N=${0##*/} + N=${N#[SK]??} + echo "Usage: $N {start|stop|status|restart}" >&2 + exit 1 + ;; +esac + +exit 0 + diff --git a/common/recipes-utils/spatula/files/spatula_wrapper.py b/common/recipes-utils/spatula/files/spatula_wrapper.py new file mode 100644 index 0000000..2717bef --- /dev/null +++ b/common/recipes-utils/spatula/files/spatula_wrapper.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python +# +# Copyright 2015-present Facebook. All Rights Reserved. +# +# This program file is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program in a file named COPYING; if not, write to the +# Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301 USA +# + +import urllib2, urllib +import logging +import os +import subprocess +import time +import argparse + +SPATULA_FILE = '/etc/spatula/spatula' +LOG_FORMAT = '[%(levelname)s] %(asctime)s: %(message)s' +DATE_FORMAT = '%Y-%m-%d %H:%M:%S' +DEFAULT_SLEEP = 900 # default sleep 15 mins + +class SpatulaWrapper(object): + def __init__(self, address='fe80::2', interface='usb0', + port=8087, ssl=False): + proto = 'http' + if ssl: + proto = 'https' + self.url = '{proto}://{address}%{iface}:{port}'.format( + proto = proto, + address = address, + iface = interface, + port = port) + + def _getSpatula(self, endpoint='/spatula'): + ''' + Get the executable spatula script from the host. + ''' + url = '{}{}'.format(self.url, endpoint) + try: + request = urllib2.Request(url) + response = urllib2.urlopen(request) + return response.read() + except Exception as err: + raise Exception('failed getting Spatula {}: {}'.format(url, err)) + + def _success(self, endpoint='/success'): + ''' + Api to report the timestamp of a successful run + ''' + query = urllib.urlencode({'timestamp': time.time()}) + url = '{}{}?{}'.format(self.url, endpoint, query) + try: + request = urllib2.Request(url) + response = urllib2.urlopen(request) + return response.read() + except Exception as err: + raise Exception('failed report success {}: {}'.format(url, err)) + + def _error(self, error, endpoint='/error'): + ''' + Api to report the timestamp and the error when spatula fails + ''' + query = urllib.urlencode({'timestamp': time.time(), 'error': error}) + url = '{}{}?{}'.format(self.url, endpoint, query) + try: + request = urllib2.Request(url) + response = urllib2.urlopen(request) + return response.read() + except Exception as err: + raise Exception('failed report error [{}]: {}'.format(url, err)) + + def _execute(self): + try: + # get the executable from host + if not os.path.exists(os.path.dirname(SPATULA_FILE)): + os.makedirs(os.path.dirname(SPATULA_FILE)) + with open(SPATULA_FILE, 'w+') as file: + file.write(self._getSpatula()) + file.close() + # set the permission + os.chmod(SPATULA_FILE, 0755) + # run the executable file + spatula = subprocess.Popen([SPATULA_FILE], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, err = spatula.communicate() + if spatula.returncode != 0: + raise Exception('spatula failed: {}'.format(err)) + self._success() + except Exception as err: + self._error(err) + raise err + + def run(self, sleep): + while True: + try: + self._execute() + except Exception as err: + logging.error(err) + time.sleep(sleep) + +if __name__ == '__main__': + args = argparse.ArgumentParser() + args.add_argument('-s', '--sleep', default=DEFAULT_SLEEP, + help='Sleep time between spatula runs (default=%(default)s)') + params = args.parse_args() + logging.basicConfig(format=LOG_FORMAT, datefmt=DATE_FORMAT) + wrapper = SpatulaWrapper() + wrapper.run(params.sleep) diff --git a/common/recipes-utils/spatula/spatula_0.1.bb b/common/recipes-utils/spatula/spatula_0.1.bb new file mode 100644 index 0000000..dd54ccd --- /dev/null +++ b/common/recipes-utils/spatula/spatula_0.1.bb @@ -0,0 +1,47 @@ +# Copyright 2015-present Facebook. All Rights Reserved. +# +# This program file is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program in a file named COPYING; if not, write to the +# Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301 USA +SUMMARY = "Configure the BMC" +DESCRIPTION = "The script communicates with host and configures BMC." +SECTION = "base" +PR = "r1" +LICENSE = "GPLv2" +LIC_FILES_CHKSUM = "file://spatula_wrapper.py;beginline=5;endline=18;md5=0b1ee7d6f844d472fa306b2fee2167e0" + + +DEPENDS_append = " update-rc.d-native" + +SRC_URI = "file://setup-spatula.sh \ + file://spatula_wrapper.py \ + " + +S = "${WORKDIR}" + +binfiles = "spatula_wrapper.py" + +do_install() { + bin="${D}/usr/local/bin" + install -d $bin + for f in ${binfiles}; do + install -m 755 $f ${bin}/$f + done + install -d ${D}${sysconfdir}/init.d + install -d ${D}${sysconfdir}/rcS.d + install -m 755 setup-spatula.sh ${D}${sysconfdir}/init.d/setup-spatula.sh + update-rc.d -r ${D} setup-spatula.sh start 95 2 3 4 5 . +} + +FILES_${PN} = "${prefix}/local/bin ${sysconfdir} " |