diff options
Diffstat (limited to 'meta-facebook/meta-wedge/recipes-wedge/rest-api/files')
7 files changed, 446 insertions, 0 deletions
diff --git a/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest.py b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest.py new file mode 100644 index 0000000..6d59fd6 --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python +# +# Copyright 2014-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 +# + + +from ctypes import * +from bottle import route, run, template, request, response +from bottle import abort +import json +from rest_fruid import * +from rest_server import * +from rest_sensors import * +from rest_bmc import * +from rest_gpios import * + +# Handler for root resource endpoint +@route('/api') +def rest_api(): + result = { + "Information": { + "Description": "Wedge RESTful API Entry", + }, + "Actions": [], + "Resources": [ "sys"], + } + + return result + +# Handler for sys resource endpoint +@route('/api/sys') +def rest_sys(): + result = { + "Information": { + "Description": "Wedge System", + }, + "Actions": [], + "Resources": [ "mb", "bmc", "server", "sensors", "gpios"], + } + + return result + +# Handler for sys/mb resource endpoint +@route('/api/sys/mb') +def rest_sys(): + result = { + "Information": { + "Description": "System Motherboard", + }, + "Actions": [], + "Resources": [ "fruid"], + } + + return result + +# Handler for sys/mb/fruid resource endpoint +@route('/api/sys/mb/fruid') +def rest_fruid(): + return get_fruid() + +# Handler for sys/bmc resource endpoint +@route('/api/sys/bmc') +def rest_bmc(): + return get_bmc() + +# Handler for sys/server resource endpoint +@route('/api/sys/server') +def rest_bmc(): + return get_server() + +# Handler for uServer resource endpoint +@route('/api/sys/server', method='POST') +def rest_server(): + data = json.load(request.body) + return server_action(data) + +# Handler for sensors resource endpoint +@route('/api/sys/sensors') +def rest_sensors(): + return get_sensors() + +# Handler for sensors resource endpoint +@route('/api/sys/gpios') +def rest_gpios(): + return get_gpios() + +run(host = "::", port = 8080) diff --git a/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_bmc.py b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_bmc.py new file mode 100644 index 0000000..d9600ae --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_bmc.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# +# Copyright 2014-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 +# + + +from subprocess import * + +# Handler for FRUID resource endpoint +def get_bmc(): + # Get BMC Reset Reason + wdt_counter = Popen('devmem 0x1e785010', \ + shell=True, stdout=PIPE).stdout.read() + wdt_counter = int(wdt_counter, 0) + + wdt_counter &= 0xff00 + + if wdt_counter: + por_flag = 0 + else: + por_flag = 1 + + if por_flag: + reset_reason = "Power ON Reset" + else: + reset_reason = "User Initiated Reset or WDT Reset" + + # Get BMC's Up Time + uptime = Popen('uptime', \ + shell=True, stdout=PIPE).stdout.read() + + # Get Usage information + data = Popen('top -b n1', \ + shell=True, stdout=PIPE).stdout.read() + adata = data.split('\n') + mem_usage = adata[0] + cpu_usage = adata[1] + + result = { + "Information": { + "Description": "Wedge BMC", + "Reset Reason": reset_reason, + "Uptime": uptime, + "Memory Usage": mem_usage, + "CPU Usage": cpu_usage, + }, + "Actions": [], + "Resources": [], + } + + return result; + diff --git a/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_fruid.py b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_fruid.py new file mode 100644 index 0000000..167e1fa --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_fruid.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python +# +# Copyright 2014-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 +# + + +from ctypes import * + +# Handler for FRUID resource endpoint +fru = CDLL("libwedge_eeprom.so") + +class FRU(Structure): + _fields_ = [ ("fbw_version", c_ubyte), + ("fbw_product_name", c_char * 13), + ("fbw_product_number", c_char * 10), + ("fbw_assembly_number", c_char * 15), + ("fbw_facebook_pcb_number", c_char * 15), + ("fbw_odm_pcb_number", c_char * 14), + ("fbw_odm_pcb_serial", c_char * 13), + ("fbw_production_state", c_ubyte), + ("fbw_product_version", c_ubyte), + ("fbw_product_subversion", c_ubyte), + ("fbw_product_serial", c_char * 13), + ("fbw_product_asset", c_char * 13), + ("fbw_system_manufacturer", c_char * 9), + ("fbw_system_manufacturing_date", c_char * 10), + ("fbw_pcb_manufacturer", c_char * 9), + ("fbw_assembled", c_char * 9), + ("fbw_local_mac", c_ubyte * 6), + ("fbw_mac_base", c_ubyte * 6), + ("fbw_dummy", c_char), + ("fbw_mac_size", c_ushort), + ("fbw_location", c_char * 9), + ("fbw_crc8", c_ubyte) ] + +def get_fruid(): + myfru = FRU() + p_myfru = pointer(myfru) + fru.wedge_eeprom_parse(None, p_myfru) + + mac2str = lambda mac: ':'.join(['{:02X}'.format(b) for b in mac]) + + fruinfo = { "Version": myfru.fbw_version, + "Product Name": myfru.fbw_product_name, + "Product Part Number": myfru.fbw_product_number, + "System Assembly Part Number": myfru.fbw_assembly_number, + "Facebook PCB Part Number": myfru.fbw_facebook_pcb_number, + "ODM PCB Part Number": myfru.fbw_odm_pcb_number, + "ODM PCB Serial Number": myfru.fbw_odm_pcb_serial, + "Product Production State": myfru.fbw_production_state, + "Product Version": myfru.fbw_product_version, + "Product Sub-Version": myfru.fbw_product_subversion, + "Product Serial Number": myfru.fbw_product_serial, + "Product Asset Tag": myfru.fbw_product_asset, + "System Manufacturer": myfru.fbw_system_manufacturer, + "System Manufacturing Date": myfru.fbw_system_manufacturing_date, + "PCB Manufacturer": myfru.fbw_pcb_manufacturer, + "Assembled At": myfru.fbw_assembled, + "Local MAC": mac2str(myfru.fbw_local_mac), + "Extended MAC Base": mac2str(myfru.fbw_mac_base), + "Extended MAC Address Size": myfru.fbw_mac_size, + "Location on Fabric": myfru.fbw_location, + "CRC8": hex((myfru.fbw_crc8)) + } + + result = { + "Information": fruinfo, + "Actions": [], + "Resources": [], + } + + return result diff --git a/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_gpios.py b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_gpios.py new file mode 100644 index 0000000..340c5c3 --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_gpios.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# +# Copyright 2014-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 +# + +from rest_fruid import get_fruid + +WEDGES = ["Wedge-AC-F", "Wedge-DC-F"] + + +def read_gpio_sysfs(gpio): + with open('/sys/class/gpio/gpio%d/value' % gpio, 'r') as f: + val_string = f.read() + if val_string == '1\n': + return 1 + if val_string == '0\n': + return 0 + return None + + +def read_wedge_back_ports(): + bhinfo = { "port_1": { "pin_1": read_gpio_sysfs(120), + "pin_2": read_gpio_sysfs(121), + "pin_3": read_gpio_sysfs(122), + "pin_4": read_gpio_sysfs(123) + }, + "port_2": { "pin_1": read_gpio_sysfs(124), + "pin_2": read_gpio_sysfs(125), + "pin_3": read_gpio_sysfs(126), + "pin_4": read_gpio_sysfs(52) + } + } + return bhinfo + + +def get_gpios(): + fruinfo = get_fruid() + gpioinfo = {} + if fruinfo["Information"]["Product Name"] in WEDGES: + gpioinfo["back_ports"] = read_wedge_back_ports() + return gpioinfo diff --git a/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_sensors.py b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_sensors.py new file mode 100644 index 0000000..f4f83d3 --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_sensors.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# +# Copyright 2014-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 json +import os +import re + +# Handler for sensors resource endpoint +def get_sensors(): + result = [] + data = os.popen('sensors').read() + data = re.sub(r'\(.+?\)', '', data) + for edata in data.split('\n\n'): + adata = edata.split('\n', 1) + sresult = {} + if (len(adata) < 2): + break; + sresult['name'] = adata[0] + for sdata in adata[1].split('\n'): + tdata = sdata.split(':') + if (len(tdata) < 2): + continue + sresult[tdata[0].strip()] = tdata[1].strip() + result.append(sresult) + + fresult = { + "Information": result, + "Actions": [], + "Resources": [], + } + + return fresult diff --git a/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_server.py b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_server.py new file mode 100644 index 0000000..0acba27 --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/rest_server.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# +# Copyright 2014-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 os +from subprocess import * + +# Handler for uServer resource endpoint +def get_server(): + ret = Popen('/usr/local/bin/wedge_power.sh status', \ + shell=True, stdout=PIPE).stdout.read() + status = ret.rsplit()[-1] + + result = { + "Information": { "status": status }, + "Actions": ["power-on", "power-off", "power-reset"], + "Resources": [], + } + + return result + +def server_action(data): + if data["action"] == 'power-on': + ret = Popen('/usr/local/bin/wedge_power.sh status', \ + shell=True, stdout=PIPE).stdout.read() + status = ret.rsplit()[-1] + if status == 'on': + res = 'failure' + reason = 'already on' + else: + ret = Popen('/usr/local/bin/wedge_power.sh on', \ + shell=True, stdout=PIPE).stdout.read() + res = "success" + elif data["action"] == 'power-off': + ret = Popen('/usr/local/bin/wedge_power.sh off', \ + shell=True, stdout=PIPE).stdout.read() + res = "success" + elif data["action"] == 'power-reset': + ret = Popen('/usr/local/bin/wedge_power.sh reset', \ + shell=True, stdout=PIPE).stdout.read() + res = "success" + else: + res = 'failure' + reason = 'invalid action' + + if res == 'failure': + result = { "result": res, "reason": reason} + else: + result = { "result": res } + + return result diff --git a/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/setup-rest-api.sh b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/setup-rest-api.sh new file mode 100644 index 0000000..fe01c23 --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rest-api/files/setup-rest-api.sh @@ -0,0 +1,16 @@ +#!/bin/sh +# +# Copyright 2014-present Facebook. All Rights Reserved. +# +### BEGIN INIT INFO +# Provides: setup-rest-api +# Required-Start: +# Required-Stop: +# Default-Start: S +# Default-Stop: +# Short-Description: Set REST API handler +### END INIT INFO + +echo -n "Setup REST API handler... " +/usr/local/bin/rest.py > /tmp/rest.log 2>&1 & +echo "done." |