def processsystemflags(systemflags=None): from cupid import pilib from iiutilities import dblib from iiutilities import utility if not systemflags: systemflags = pilib.dbs.system.read_table('systemflags') flagnames = [] flagvalues = [] for flag in systemflags: flagnames.append(flag['name']) flagvalues.append(flag['value']) stop = False if 'reboot' in flagnames: if flagvalues[flagnames.index('reboot')]: stop = True dblib.setsinglevalue(pilib.dirs.dbs.system, 'systemflags', 'value', 0, "name='reboot'") import subprocess utility.log(pilib.dirs.logs.system, 'Rebooting for system flag', 0, pilib.loglevels.system) subprocess.call(['/sbin/reboot']) if 'netconfig' in flagnames: if flagvalues[flagnames.index('netconfig')]: stop = True dblib.setsinglevalue(pilib.dirs.dbs.system, 'systemflags', 'value', 0, "name='netconfig'") from netconfig import runconfig utility.log(pilib.dirs.logs.system, 'Restarting network configuration', 0, pilib.loglevels.system) runconfig() if 'updateiicontrollibs' in flagnames and not stop: if flagvalues[flagnames.index('updateiicontrollibs')]: stop = True dblib.setsinglevalue(pilib.dirs.dbs.system, 'systemflags', 'value', 0, 'name=\'updateiicontrollibs\'') from iiutilities.gitupdatelib import updateiicontrollibs utility.log(pilib.dirs.logs.system, 'Updating iicontrollibs', 0, pilib.loglevels.system) updateiicontrollibs(True) if 'updatecupidweblib' in flagnames and not stop: if flagvalues[flagnames.index('updatecupidweblib')]: stop = True dblib.setsinglevalue(pilib.dirs.dbs.system, 'systemflags', 'value', 0, 'name=\'updatecupidweblib\'') from iiutilities.gitupdatelib import updatecupidweblib utility.log(pilib.dirs.logs.system, 'Updating cupidweblib', 0, pilib.loglevels.system) updatecupidweblib(True)
def processsystemflags(systemflags=None): from pilib import writedatedlogmsg, systemstatuslog, systemstatusloglevel if not systemflags: systemflags = pilib.readalldbrows(pilib.systemdatadatabase, 'systemflags') flagnames = [] flagvalues = [] for flag in systemflags: flagnames.append(flag['name']) flagvalues.append(flag['value']) stop = False if 'reboot' in flagnames: if flagvalues[flagnames.index('reboot')]: stop = True pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'value', 0, "name='reboot'") import subprocess writedatedlogmsg(systemstatuslog, 'Rebooting for system flag', 0, systemstatusloglevel) subprocess.call(['reboot']) if 'netconfig' in flagnames: if flagvalues[flagnames.index('netconfig')]: stop = True pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'value', 0, "name='netconfig'") from netconfig import runconfig writedatedlogmsg(systemstatuslog, 'Restarting network configuration', 0, systemstatusloglevel) runconfig() if 'updateiicontrollibs' in flagnames and not stop: if flagvalues[flagnames.index('updateiicontrollibs')]: stop = True pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'value', 0, 'name=\'updateiicontrollibs\'') from misc.gitupdatelib import updateiicontrollibs writedatedlogmsg(systemstatuslog, 'Updating iicontrollibs', 0, systemstatusloglevel) updateiicontrollibs(True) if 'updatecupidweblib' in flagnames and not stop: if flagvalues[flagnames.index('updatecupidweblib')]: stop = True pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'value', 0, 'name=\'updatecupidweblib\'') from misc.gitupdatelib import updatecupidweblib writedatedlogmsg(systemstatuslog, 'Updating cupidweblib', 0, systemstatusloglevel) updatecupidweblib(True)
def processsystemflags(systemflags=None): import pilib from pilib import log, syslog, sysloglevel if not systemflags: systemflags = pilib.readalldbrows(pilib.systemdatadatabase, 'systemflags') flagnames = [] flagvalues = [] for flag in systemflags: flagnames.append(flag['name']) flagvalues.append(flag['value']) stop = False if 'reboot' in flagnames: if flagvalues[flagnames.index('reboot')]: stop = True pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'value', 0, "name='reboot'") import subprocess log(syslog, 'Rebooting for system flag', 0, sysloglevel) subprocess.call(['/sbin/reboot']) if 'netconfig' in flagnames: if flagvalues[flagnames.index('netconfig')]: stop = True pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'value', 0, "name='netconfig'") from netconfig import runconfig log(syslog, 'Restarting network configuration', 0, sysloglevel) runconfig() if 'updateiicontrollibs' in flagnames and not stop: if flagvalues[flagnames.index('updateiicontrollibs')]: stop = True pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'value', 0, 'name=\'updateiicontrollibs\'') from misc.gitupdatelib import updateiicontrollibs log(syslog, 'Updating iicontrollibs', 0, sysloglevel) updateiicontrollibs(True) if 'updatecupidweblib' in flagnames and not stop: if flagvalues[flagnames.index('updatecupidweblib')]: stop = True pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'value', 0, 'name=\'updatecupidweblib\'') from misc.gitupdatelib import updatecupidweblib log(syslog, 'Updating cupidweblib', 0, sysloglevel) updatecupidweblib(True)
def runboot(): import subprocess from time import sleep import pilib import spilights from iiutilities import utility, dblib, datalib try: pilib.set_all_wal(False) except: print('error setting wal mode') interfaces = pilib.dbs.control.read_table('interfaces') # Clear out status bits, if for no other reason to see the LEDs come on for statusvalue in [ 'systemstatusstatus', 'hamachistatus', 'picontrolstatus', 'updateiostatus', 'serialhandlerstatus' ]: dblib.setsinglevalue(pilib.dirs.dbs.system, 'systemstatus', statusvalue, 0) systemstatus = dblib.readonedbrow(pilib.dirs.dbs.system, 'systemstatus')[0] # Queue a message indicating we are rebooting # TODO: Make this an actions option, or put it somewhere. # try: import socket hostname = socket.gethostname() message = 'CuPID is booting:\r\n\r\n' notifications_email = '*****@*****.**' subject = 'CuPID : ' + hostname + ' : booting' notification_database = pilib.cupidDatabase(pilib.dirs.dbs.notifications) system_database = pilib.cupidDatabase(pilib.dirs.dbs.system) currenttime = datalib.gettimestring() notification_database.insert( 'queued', { 'type': 'email', 'message': message, 'options': 'email:' + notifications_email + ',subject:' + subject, 'queuedtime': currenttime }) system_database.set_single_value('notifications', 'lastnotification', currenttime, condition="item='boot'") # except Exception as e: # error_message = 'EXCEPTION in notification: {}'.format(e.message) # print (error_message) # utility.log(pilib.dirs.logs.system, error_message) # else: # utility.log(pilib.dirs.logs.system, 'Boot notificaiton complete. ') # Start pigpiod subprocess.call(['killall', 'pigpiod']) sleep(1) utility.log(pilib.dirs.logs.system, 'boot: starting pigpio daemon', 3, pilib.loglevels.system) subprocess.call(['/usr/local/bin/pigpiod']) # Start webserver subprocess.call(['killall', 'nginx']) subprocess.call(['killall', 'uwsgi']) subprocess.call(['killall', 'apache2']) if systemstatus['webserver'] == 'apache': utility.log(pilib.dirs.logs.system, 'boot: starting apache', 3, pilib.loglevels.system) subprocess.call(['service', 'apache2', 'start']) elif systemstatus['webserver'] == 'nginx': utility.log(pilib.dirs.logs.system, 'boot: starting nginx', 3, pilib.loglevels.system) subprocess.call(['service', 'nginx', 'start']) # Run uwsgi daemon if nginx is running try: result = subprocess.check_output(['service', 'nginx', 'status']).decode('utf-8') except subprocess.CalledProcessError as e: result = '' # print('I AM FAILING') # print e.output if result: utility.log(pilib.dirs.logs.system, 'boot: starting uwsgi based on nginx call', 0) subprocess.call([ 'uwsgi', '--emperor', '/usr/lib/iicontrollibs/wsgi/', '--daemonize', '/var/log/cupid/uwsgi.log' ]) else: # print(' I KNOW NGINX IS NOT RUNNING') pass # Mount 1wire master subprocess.call(['killall', 'owfs']) subprocess.call(['killall', 'owserver']) subprocess.call(['killall', 'owhttpd']) runi2cowfs = True runusbowfs = False temp_unit = 'C' for interface in interfaces: if interface['enabled']: from iiutilities.datalib import parseoptions options_dict = parseoptions(interface['options']) if 'tempunit' in options_dict: if options_dict['tempunit'] in [ 'F', 'f', 'Fahrenheit', 'fahrenheit' ]: temp_unit = 'F' if interface['interface'] == 'I2C' and interface[ 'type'] == 'DS2483': runi2cowfs = True if interface['interface'] == 'USB' and interface[ 'type'] == 'DS9490': runusbowfs = True if interface['interface'] == 'SPI1' and type == 'CuPIDlights': spilights.updatelightsfromdb(pilib.dirs.dbs.control, 'indicators', 1) if interface['interface'] == 'SPI0' and type == 'CuPIDlights': spilights.updatelightsfromdb(pilib.dirs.dbs.control, 'indicators', 0) if runi2cowfs or runusbowfs: if runi2cowfs: utility.log(pilib.dirs.logs.system, 'boot: Running i2c owserver', 3, pilib.loglevels.system) try: if temp_unit == 'F': subprocess.call([ '/opt/owfs/bin/owserver', '-F', '--i2c=/dev/i2c-1:ALL', '-p', '4304' ]) else: subprocess.call([ '/opt/owfs/bin/owserver', '--i2c=/dev/i2c-1:ALL', '-p', '4304' ]) except: utility.log(pilib.dirs.logs.system, 'boot: error running i2c owserver', 1, pilib.loglevels.system) if runusbowfs: utility.log(pilib.dirs.logs.system, 'boot: Running usb owserver', 3, pilib.loglevels.system) try: if temp_unit == 'F': subprocess.call( ['/opt/owfs/bin/owserver', '-F', '-u', '-p', '4304']) else: subprocess.call( ['/opt/owfs/bin/owserver', '-u', '-p', '4304']) except: utility.log(pilib.dirs.logs.system, 'error running usb owserver', 1, pilib.loglevels.system) utility.log(pilib.dirs.logs.system, 'boot: Running owfs/owserver mount', 3, pilib.loglevels.system) try: if temp_unit == 'F': subprocess.call( ['/opt/owfs/bin/owfs', '-F', '-s', '4304', '/var/1wire/']) else: subprocess.call( ['/opt/owfs/bin/owfs', '-s', '4304', '/var/1wire/']) except: utility.log(pilib.dirs.logs.system, 'boot: error running owfs', 1, pilib.loglevels.system) utility.log(pilib.dirs.logs.system, 'boot: Running owhttpd/owserver mount', 3, pilib.loglevels.system) try: if temp_unit == 'F': subprocess.call([ '/opt/owfs/bin/owhttpd', '-F', '-s', '4304', '-p', '4305' ]) else: subprocess.call( ['/opt/owfs/bin/owhttpd', '-s', '4304', '-p', '4305']) except: utility.log(pilib.dirs.logs.system, 'boot: error running owhttpd', 1, pilib.loglevels.system) else: utility.log(pilib.dirs.logs.system, 'boot: not running owfs', 3, pilib.loglevels.system) # Run netstart script if enabled if systemstatus['netconfigenabled']: from netconfig import runconfig utility.log(pilib.dirs.logs.system, 'boot: running boot netconfig', 2, pilib.loglevels.system) runconfig(onboot=True)
def netconfig(self): from netconfig import runconfig runconfig(True)
if interface['interface'] == 'SPI1' and type == 'CuPIDlights': spilights.updatelightsfromdb(pilib.controldatabase, 'indicators', 1) if interface['interface'] == 'SPI0' and type == 'CuPIDlights': spilights.updatelightsfromdb(pilib.controldatabase, 'indicators', 0) if runi2cowfs or runusbowfs: if runi2cowfs: subprocess.call([ '/opt/owfs/bin/owserver', '-F', '--i2c=/dev/i2c-1:ALL', '-p', '4304' ]) if runusbowfs: subprocess.call(['/opt/owfs/bin/owserver', '-F', '-u', '-p', '4304']) subprocess.call(['/opt/owfs/bin/owfs', '-F', '-s', '4304', '/var/1wire/']) subprocess.call( ['/opt/owfs/bin/owhttpd', '-F', '-s', '4304', '-p', '4305']) # Run netstart script from netconfig import runconfig runconfig(onboot=True) # Run daemon from cupiddaemon import rundaemon rundaemon() # Update hardware version in table from systemstatus import readhardwarefileintoversions readhardwarefileintoversions()
def watchdognetstatus(allnetstatus=None): import pilib import netconfig import subprocess from cupiddaemon import pgrepstatus """ And now comes the checking of configuration specific statuses and restarting them if enabled and necessary NOW Set number configuration types: eth0 is always dhcp if interface is not specified, it is assumed that the specified configuration applies to wlan0 ap : access point on wlan0. Currently, address is hard-coded as 192.168.0.1 as set in hostapd (to have dhcp all on same subnet) station : station mode on wlan0 IP address mode set in 'addtype' field. If DHCP, set as DHCP. If static, set as address field eth0wlan0bridge : wlan0 set as AP mode with bridge to eth0 wlan0wlan1bridge : wlan0 set as station mode, wlan1 as ap mode with bridge wlan1wlan0bridge wlan1 set as station mode, wlan0 as ap mode with bridge IN THE FUTURE netconfigdata will have a row for each interface that needs to be configured. Each interface can be either static, dhcp, or ap. Each interface can also bridge, but this will be limited at first, due to the number of combinations. If bridge is selected, the mode will be checked, and the appropriate IPTables will be set based on which one is AP """ if not allnetstatus: allnetstatus = updatenetstatus() netconfigdata = allnetstatus['netconfigdata'] netstatus = allnetstatus['netstatusdict'] # Refactor ifaces array. We could probably do this earlier and pass it around like this ifacedict = {} for iface in allnetstatus['ifacesdictarray']: # print(iface['name']) # print(iface) ifacedict[iface['name']] = iface # print(ifacedict) statusmsg = '' currenttime = pilib.gettimestring() runconfig = False if netconfigdata['mode'] in ['ap', 'tempap']: pilib.log(pilib.networklog, 'AP Mode is set. ', 1, pilib.networkloglevel) """ THIS NEEDS TO BE REWRITTEN ( or absorbed into eth0wlan0bridge ) """ if netconfigdata['mode'] == 'tempap': timesincelastretry = pilib.timestringtoseconds(currenttime) - pilib.timestringtoseconds(netconfigdata['laststationretry']) pilib.log(pilib.networklog, 'TempAP mode: Time since last retry: ' + str(timesincelastretry) + '. Station retry time: ' + str(netconfigdata['stationretrytime']), 1, pilib.networkloglevel) if timesincelastretry > netconfigdata['stationretrytime']: # We go back to station mode statusmsg += 'Time to go back to station mode. ' pilib.log(pilib.networklog, 'Time to go back to station mode. ', 1, pilib.networkloglevel) pilib.setsinglevalue(pilib.systemdatadatabase, 'netconfig', 'mode', 'station') pilib.setsinglevalue(pilib.systemdatadatabase, 'netconfig', 'laststationretry', '') netconfig.runconfig() else: # If we have ap up, do nothing if netstatus['dhcpstatus']: statusmsg += 'AP checked and ok. ' pilib.log(pilib.networklog, 'AP checked and ok. ', 1, pilib.networkloglevel) pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'mode', 'ap') pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'SSID', 'cupidwifi') pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'offlinetime', '') # If we don't have dhcp up, restart ap mode # this will currently cause reboot if we don't set onboot=True # We set status message in case we change our minds and reboot here. else: statusmsg += 'Restarting AP. ' pilib.log(pilib.networklog, 'Restarting AP mode. ', 1, pilib.networkloglevel) pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'statusmsg', statusmsg) netconfig.runconfig() # Station Mode. Again, like above, this should be modular. elif netconfigdata['mode'] == 'station': pilib.log(pilib.networklog, 'Station mode is set. ', 3, pilib.networkloglevel) # Check station address (not yet implemented) and wpa status (implemented) stationinterface = 'wlan0' try: stationifacedata = ifacedict[stationinterface] except KeyError: pilib.log(pilib.networklog, 'No stationiface data(' + stationinterface + ') present for mode ' + netconfigdata['mode'], 1, pilib.networkloglevel) statusmsg += 'No stationiface data(' + stationinterface + ') present for mode ' + netconfigdata['mode'] runconfig = True else: wpadata = pilib.parseoptions(stationifacedata['wpastate']) if wpadata['wpa_state'] == 'COMPLETED' and stationifacedata['address']: pilib.log(pilib.networklog, 'station interface ' + stationinterface + ' wpastatus appears ok with address ' + str(stationifacedata['address']), 3, pilib.networkloglevel) else: pilib.log(pilib.networklog, 'station interface for ' + stationinterface + ' does not appear ok judged by wpa_state: wpastate = ' + wpadata['wpa_state'] + ' address= ' + stationifacedata['address'], 1, pilib.networkloglevel) statusmsg += 'station interface does not appear ok judged by wpa_state. ' runconfig = True # Should do special handling in here to verify address for static mode. # # If we have wpa up, do nothing # if int(netstatus['connected']): # statusmsg += 'Station wpamode appears ok. ' # pilib.log(pilib.networklog, 'wpamode appears ok. ', 1, pilib.networkloglevel) # # # If wpa is not connected # else: # statusmsg += 'Station wpamode appears disconnected. ' # pilib.log(pilib.networklog, 'wpamode appears disconnected. ', 1, pilib.networkloglevel) # # if netstatus['offlinetime'] == '': # pilib.log(pilib.networklog, 'Setting offline time for empty value. ', 4, # pilib.networkloglevel) # pilib.setsinglevalue('netstatus', 'offlinetime', pilib.gettimestring()) # offlinetime = 0 # else: # pilib.log(pilib.networklog, 'Calculating offline time. ', 4, pilib.networkloglevel) # offlinetime = pilib.timestringtoseconds(currenttime) - pilib.timestringtoseconds( # netstatus['offlinetime']) # # pilib.log(pilib.networklog, 'wpa has been offline for ' + str(offlinetime) + '. ', 3, # pilib.networkloglevel) # statusmsg += 'We have been offline for ' + str(offlinetime) + '. ' # # # If aprevert is aprevert or temprevert and we've been offline long enough, flip over to ap # # then set offline time to now (otherwise this keeps happening) # if netconfigdata['aprevert'] in ['temprevert', 'aprevert'] and offlinetime > netconfigdata[ # 'apreverttime']: # # # set laststationretry to currenttime. This marks when we flipped over to ap # statusmsg += 'Setting last station retry time. ' # pilib.log(pilib.networklog, 'Reverting to AP mode', 3, pilib.networkloglevel) # pilib.log(pilib.networklog, # 'Setting last station retry time to ' + str(currenttime), 0, # pilib.networkloglevel) # pilib.setsinglevalue(pilib.systemdatadatabase, 'netconfig', 'laststationretry', currenttime) # pilib.setsinglevalue('netstatus', 'offlinetime', currenttime) # # if netconfigdata['aprevert'] == 'aprevert': # # set mode to ap # statusmsg += 'Setting mode to ap. ' # pilib.log(pilib.networklog, 'Setting mode to ap ' + str(currenttime), 3, # pilib.networkloglevel) # pilib.setsinglevalue(pilib.systemdatadatabase, 'netconfig', 'mode', 'ap') # elif netconfigdata['aprevert'] == 'temprevert': # # set mode to tempap # statusmsg += 'Setting mode to tempap. ' # pilib.log(pilib.networklog, 'Setting mode to tempap ' + str(currenttime), 3, # pilib.networkloglevel) # pilib.setsinglevalue(pilib.systemdatadatabase, 'netconfig', 'mode', 'tempap') # # # Unfortunately, to revert to ap mode successfully, we currently have to reboot # # this is built into the netconfig script - any time you set ap mode except at boot, it reboots # pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'statusmsg', statusmsg) # pilib.log(pilib.syslog, 'Running netconfig . ', 4, # pilib.sysloglevel) # netconfig.runconfig() # elif offlinetime > 15: # pilib.log(pilib.syslog, 'Restarting netconfig on bad wpastatus', 1, # pilib.sysloglevel) # runconfig = True # # # Here, we need to check the ifaces address. Netstatus address is ambiguous # if netstatus['ip_address'] != netconfigdata['address']: # pilib.log(pilib.networklog, 'IP address mismatch ( Configured for ' + netconfigdata['address'] + '. Reporting' + netstatus['ip_address'] + ' ). Running config.', 1, pilib.networkloglevel) # runconfig = True # else: # pilib.log(pilib.networklog, 'IP address match for ' + netconfigdata['address'] + '. ', 3, pilib.networkloglevel) elif netconfigdata['mode'] == 'eth0wlan0bridge': # We don't actually check eth0. This is because we shouldn't have to. Also, if we don't check on eth0, we can # use the same mode for wlan0 AP and eth0wlan0 bridge. Hot plug, works fine. # Check wlan0 dhcp and hostapd status try: wlan0ifacedata =ifacedict['wlan0'] except KeyError: pilib.log(pilib.networklog, 'No wlan0 data present in configuration of eth0wlan0bridge. ', 1, pilib.networkloglevel) statusmsg += 'wlan0 data is not present. ' runconfig = True else: pilib.log(pilib.networklog, 'Checking dhcp server status on wlan0. ', 4, pilib.networkloglevel) try: result = subprocess.check_output(['/usr/sbin/service', 'isc-dhcp-server', 'status'], stderr=subprocess.PIPE) except Exception, e: # If not running, the subprocess call will throw an error pilib.log(pilib.networklog, 'Error in reading dhcp server status. Assumed down. ', 1, pilib.networkloglevel) statusmsg += 'dhcp server appears down. ' runconfig = True else: pilib.log(pilib.networklog, 'DHCP server appears to be up. ', 1, pilib.networkloglevel) """ Check ifconfig ipaddress for wlan0 This should be programmable. """ if wlan0ifacedata['address'] == '192.168.0.1': pilib.log(pilib.networklog, 'wlan0 address is appears to be set properly: ' + str(wlan0ifacedata['address']), 3, pilib.networkloglevel) else: pilib.log(pilib.networklog, 'wlan0 address is not set properly: ' + str(wlan0ifacedata['address']), 1, pilib.networkloglevel) statusmsg += 'wlan0 address does not appear ok. ' runconfig = True if pgrepstatus('hostapd.*wlan0')['count'] == 1: pilib.log(pilib.networklog, 'hostapd on wlan0 appears to be ok. ', 3, pilib.networkloglevel) else: pilib.log(pilib.networklog, 'hostapd on wlan0 does NOT appear to be ok. ', 1, pilib.networkloglevel) statusmsg += 'wlan0 hostpad does not appear ok. ' runconfig=True
# Same thing for WAN offline time pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'offlinetime', '') bootcounts = int(pilib.getsinglevalue(pilib.systemdatadatabase, 'netstatus', 'netrebootcounter')) bootcounts += 1 pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'netrebootcounter', str(bootcounts)) # Set system flag to reboot pilib.log(pilib.syslog, 'REBOOTING to try to fix network', 0, pilib.sysloglevel) pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'reboot', 1) pilib.log(pilib.networklog, 'Running netconfig. ', 1, pilib.networkloglevel) statusmsg += 'Running netconfig to fix. ' pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'statusmsg', statusmsg) netconfig.runconfig() else: # Clear bad status in netstatus and set netoktime pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'statusmsg', 'Mode appears to be set.') pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'netstate', 1) pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'netstateoktime', pilib.gettimestring()) # if netconfigdata['mode'] in ['station', 'wlan0wlan1bridge']: # pilib.writedatedlogmsg(pilib.systemstatuslog, 'Checking WPA status for mode ' + netconfigdata['mode'], 2, pilib.systemstatusloglevel) # wpastatusdict = getwpaclientstatus() # # try: # # # COMPLETED is onlinetime # if wpastatusdict['wpa_state'] == 'COMPLETED': # wpastatusdict['connected'] = 1
try: subprocess.call(['/opt/owfs/bin/owfs', '-F', '-s', '4304', '/var/1wire/']) except: pilib.log(pilib.syslog, 'boot: error running owfs', 1, pilib.sysloglevel) pilib.log(pilib.syslog, 'boot: Running owhttpd/owserver mount', 3, pilib.sysloglevel) try: subprocess.call(['/opt/owfs/bin/owhttpd', '-F', '-s', '4304', '-p', '4305']) except: pilib.log(pilib.syslog, 'boot: error running owhttpd', 1, pilib.sysloglevel) else: pilib.log(pilib.syslog, 'boot: not running owfs', 3, pilib.sysloglevel) # Run netstart script if enabled if systemstatus['netconfigenabled']: from netconfig import runconfig pilib.log(pilib.syslog, 'boot: running boot netconfig', 2, pilib.sysloglevel) runconfig(onboot=True) # Run daemon from cupiddaemon import rundaemon rundaemon() # Update hardware version in table from systemstatus import readhardwarefileintoversions readhardwarefileintoversions()
def runboot(): import subprocess from time import sleep import pilib import spilights from iiutilities import utility, dblib, datalib try: pilib.set_all_wal(False) except: print('error setting wal mode') interfaces = pilib.dbs.control.read_table('interfaces') # Clear out status bits, if for no other reason to see the LEDs come on for statusvalue in ['systemstatusstatus', 'hamachistatus', 'picontrolstatus', 'updateiostatus', 'serialhandlerstatus' ]: dblib.setsinglevalue(pilib.dirs.dbs.system, 'systemstatus', statusvalue, 0) systemstatus = dblib.readonedbrow(pilib.dirs.dbs.system, 'systemstatus')[0] # Queue a message indicating we are rebooting # TODO: Make this an actions option, or put it somewhere. # try: import socket hostname = socket.gethostname() message = 'CuPID is booting:\r\n\r\n' notifications_email = '*****@*****.**' subject = 'CuPID : ' + hostname + ' : booting' notification_database = pilib.cupidDatabase(pilib.dirs.dbs.notifications) system_database = pilib.cupidDatabase(pilib.dirs.dbs.system) currenttime = datalib.gettimestring() notification_database.insert('queued', {'type': 'email', 'message': message, 'options': 'email:' + notifications_email + ',subject:' + subject, 'queuedtime': currenttime}) system_database.set_single_value('notifications', 'lastnotification', currenttime, condition="item='boot'") # except Exception as e: # error_message = 'EXCEPTION in notification: {}'.format(e.message) # print (error_message) # utility.log(pilib.dirs.logs.system, error_message) # else: # utility.log(pilib.dirs.logs.system, 'Boot notificaiton complete. ') # Start pigpiod subprocess.call(['killall','pigpiod']) sleep(1) utility.log(pilib.dirs.logs.system, 'boot: starting pigpio daemon', 3, pilib.loglevels.system) subprocess.call(['/usr/local/bin/pigpiod']) # Start webserver subprocess.call(['killall','nginx']) subprocess.call(['killall','uwsgi']) subprocess.call(['killall','apache2']) if systemstatus['webserver'] == 'apache': utility.log(pilib.dirs.logs.system, 'boot: starting apache', 3, pilib.loglevels.system) subprocess.call(['service', 'apache2', 'start']) elif systemstatus['webserver'] == 'nginx': utility.log(pilib.dirs.logs.system, 'boot: starting nginx', 3, pilib.loglevels.system) subprocess.call(['service', 'nginx', 'start']) # Run uwsgi daemon if nginx is running try: result = subprocess.check_output(['service', 'nginx', 'status']).decode('utf-8') except subprocess.CalledProcessError as e: result = '' # print('I AM FAILING') # print e.output if result: utility.log(pilib.dirs.logs.system, 'boot: starting uwsgi based on nginx call', 0) subprocess.call(['uwsgi', '--emperor', '/usr/lib/iicontrollibs/wsgi/', '--daemonize', '/var/log/cupid/uwsgi.log']) else: # print(' I KNOW NGINX IS NOT RUNNING') pass # Mount 1wire master subprocess.call(['killall','owfs']) subprocess.call(['killall','owserver']) subprocess.call(['killall','owhttpd']) runi2cowfs = True runusbowfs = False temp_unit = 'C' for interface in interfaces: if interface['enabled']: from iiutilities.datalib import parseoptions options_dict = parseoptions(interface['options']) if 'tempunit' in options_dict: if options_dict['tempunit'] in ['F','f','Fahrenheit','fahrenheit']: temp_unit = 'F' if interface['interface'] == 'I2C' and interface['type'] == 'DS2483': runi2cowfs = True if interface['interface'] == 'USB' and interface['type'] == 'DS9490': runusbowfs = True if interface['interface'] == 'SPI1' and type == 'CuPIDlights': spilights.updatelightsfromdb(pilib.dirs.dbs.control, 'indicators', 1) if interface['interface'] == 'SPI0' and type == 'CuPIDlights': spilights.updatelightsfromdb(pilib.dirs.dbs.control, 'indicators', 0) if runi2cowfs or runusbowfs: if runi2cowfs: utility.log(pilib.dirs.logs.system, 'boot: Running i2c owserver', 3, pilib.loglevels.system) try: if temp_unit == 'F': subprocess.call(['/opt/owfs/bin/owserver', '-F', '--i2c=/dev/i2c-1:ALL', '-p', '4304']) else: subprocess.call(['/opt/owfs/bin/owserver', '--i2c=/dev/i2c-1:ALL', '-p', '4304']) except: utility.log(pilib.dirs.logs.system, 'boot: error running i2c owserver', 1, pilib.loglevels.system) if runusbowfs: utility.log(pilib.dirs.logs.system, 'boot: Running usb owserver', 3, pilib.loglevels.system) try: if temp_unit == 'F': subprocess.call(['/opt/owfs/bin/owserver', '-F', '-u', '-p', '4304']) else: subprocess.call(['/opt/owfs/bin/owserver', '-u', '-p', '4304']) except: utility.log(pilib.dirs.logs.system, 'error running usb owserver', 1, pilib.loglevels.system) utility.log(pilib.dirs.logs.system, 'boot: Running owfs/owserver mount', 3, pilib.loglevels.system) try: if temp_unit == 'F': subprocess.call(['/opt/owfs/bin/owfs', '-F', '-s', '4304', '/var/1wire/']) else: subprocess.call(['/opt/owfs/bin/owfs', '-s', '4304', '/var/1wire/']) except: utility.log(pilib.dirs.logs.system, 'boot: error running owfs', 1, pilib.loglevels.system) utility.log(pilib.dirs.logs.system, 'boot: Running owhttpd/owserver mount', 3, pilib.loglevels.system) try: if temp_unit == 'F': subprocess.call(['/opt/owfs/bin/owhttpd', '-F', '-s', '4304', '-p', '4305']) else: subprocess.call(['/opt/owfs/bin/owhttpd', '-s', '4304', '-p', '4305']) except: utility.log(pilib.dirs.logs.system, 'boot: error running owhttpd', 1, pilib.loglevels.system) else: utility.log(pilib.dirs.logs.system, 'boot: not running owfs', 3, pilib.loglevels.system) # Run netstart script if enabled if systemstatus['netconfigenabled']: from netconfig import runconfig utility.log(pilib.dirs.logs.system, 'boot: running boot netconfig', 2, pilib.loglevels.system) runconfig(onboot=True)
pilib.networklog, 'Time since last retry: ' + str(timesincelastretry), 1, pilib.networkloglevel) # If it's time to go back to station mode, we don't care whether we are connected as ap or not # We use dhcp status as indicator of ap status. Imperfect, but functional. if netconfigdata[ 'mode'] == 'tempap' and timesincelastretry > netconfigdata[ 'stationretrytime']: # We go back to station mode wpastatusmsg += 'Time to go back to station mode. ' pilib.setsinglevalue(pilib.systemdatadatabase, 'netconfig', 'mode', 'station') pilib.setsinglevalue(pilib.systemdatadatabase, 'netconfig', 'laststationretry', '') netconfig.runconfig() else: # If we have ap up, do nothing if wpastatusdict['dhcpstatus']: wpastatusmsg += 'AP checked and ok. ' pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'mode', 'ap') pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'SSID', 'cupidwifi') pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'offlinetime', '') # If we don't have dhcp up, restart ap mode # this will currently cause reboot if we don't set onboot=True # We set status message in case we change our minds and reboot here. else: