def _chkpsql(self): self._sv.set("Checking PostgreSQL..") self._pb.step(0.5) if not cmdline.runningprocess('postgres'): self._sv.set("Starting PostgreSQL") if startpsql(pwd): self._sv.set("PostgreSQL started") else: self._sv.set("Failed to start PostgreSQL") else: self._sv.set("PostgreSQL already running") self._bpsql = True self._pb.step(2.0)
def _create(self): # read in conf file, exit on error msgs = [(time.strftime('%H:%M:%S'), "Wraith v{0}".format(wraith.__version__), gui.LOG_NOERR)] self._conf = readconf() if 'err' in self._conf: msgs.append( (time.strftime('%H:%M:%S'), "Invalid configuration file: {0}".format(self._conf['err']), gui.LOG_ERR)) return # determine if postgresql is running if cmdline.runningprocess('postgres'): # update self, msg and connect msgs.append((time.strftime('%H:%M:%S'), "PostgreSQL running", gui.LOG_NOERR)) if not self._pwd: self._bSQL = True self._setstate(_STATE_STORE_) (self._conn, ret) = cmdline.psqlconnect(self._conf['store']) if not self._conn is None: self._setstate(_STATE_CONN_) msgs.append((time.strftime('%H:%M:%S'), "Connected to DB", gui.LOG_NOERR)) else: msgs.append( (time.strftime('%H:%M:%S'), "DB connect failed: {0}".format(ret), gui.LOG_ERR)) return else: msgs.append((time.strftime('%H:%M:%S'), "PostgreSQL not running", gui.LOG_WARN)) msgs.append((time.strftime('%H:%M:%S'), "Not connected to DB", gui.LOG_WARN)) # Iyri running? if cmdline.runningservice(wraith.IYRIPID): msgs.append( (time.strftime('%H:%M:%S'), "Iyri running", gui.LOG_NOERR)) self._setstate(_STATE_IYRI_) else: msgs.append( (time.strftime('%H:%M:%S'), "Iyri not running", gui.LOG_WARN)) # set initial state to initialized self._setstate(_STATE_INIT_) # show log, write messages & display any saved panels self.viewlog() self.getpanel("log", True).delayedwrite(msgs) if os.path.exists('default.ts'): self.guiload('default.ts') # check if something outside this program stopped a service self.after(500, self._poll)
def stoppsql(pwd): """ shut down posgresql. pwd: the sudo password """ try: cmdline.service('postgresql',pwd,False) while cmdline.runningprocess('postgres'): time.sleep(0.5) except RuntimeError: return False else: return True
def startpsql(pwd): """ start postgresl server pwd: the sudo password """ try: cmdline.service('postgresql',pwd) time.sleep(0.5) if not cmdline.runningprocess('postgres'): raise RuntimeError except RuntimeError: return False else: return True
def _updatestate(self): """ reevaluates internal state """ # state of Iyri if cmdline.runningservice(wraith.IYRIPID): self._setstate(_STATE_IYRI_) else: self._setstate(_STATE_IYRI_, False) # state of postgres i.e. store if cmdline.runningprocess('postgres'): self._setstate(_STATE_STORE_) else: self._setstate(_STATE_STORE_, False) # state of our connection - should figure out a way to determine if # connection is still 'alive' if self._conn: self._setstate(_STATE_CONN_) else: self._setstate(_STATE_CONN_, False)
def _stoppsql(self): """ shut down posgresql. NOTE: 1) no state checking done here 2) assumes sudo password is entered """ try: self.logwrite("Shutting down PostgreSQL",gui.LOG_NOTE) cmdline.service('postgresql',self._pwd,False) while cmdline.runningprocess('postgres'): self.logwrite("PostgreSQL shutting down...",gui.LOG_NOTE) time.sleep(0.5) except RuntimeError as e: self.logwrite("Error shutting down PostgreSQL",gui.LOG_ERR) else: self._setstate(_STATE_STORE_,False) self.logwrite("PostgreSQL shut down")
def _stoppsql(self): """ shut down posgresql. NOTE: 1) no state checking done here 2) assumes sudo password is entered """ try: self.logwrite("Shutting down PostgreSQL", gui.LOG_NOTE) cmdline.service('postgresql', self._pwd, False) while cmdline.runningprocess('postgres'): self.logwrite("PostgreSQL shutting down...", gui.LOG_NOTE) time.sleep(0.5) except RuntimeError as e: self.logwrite("Error shutting down PostgreSQL", gui.LOG_ERR) else: self._setstate(_STATE_STORE_, False) self.logwrite("PostgreSQL shut down")
def _updatestate(self): """ reevaluates internal state """ # state of nidus if cmdline.nidusrunning(wraith.NIDUSPID): self._setstate(_STATE_NIDUS_) else: self._setstate(_STATE_NIDUS_,False) # state of dyskt if cmdline.dysktrunning(wraith.DYSKTPID): self._setstate(_STATE_DYSKT_) else: self._setstate(_STATE_DYSKT_,False) # state of postgres i.e. store if cmdline.runningprocess('postgres'): self._setstate(_STATE_STORE_) else: self._setstate(_STATE_STORE_,False) # state of our connection - should figure out a way to determine if # connection is still 'alive' if self._conn: self._setstate(_STATE_CONN_) else: self._setstate(_STATE_CONN_,False)
def _startpsql(self): """ start postgresl server NOTE: 1) no state checking done here 2) assumes sudo password is entered """ # attempt to start psql try: self.logwrite("Starting PostgreSQL...",gui.LOG_NOTE) cmdline.service('postgresql',self._pwd) time.sleep(0.5) if not cmdline.runningprocess('postgres'): raise RuntimeError('unknown') except RuntimeError as e: self.logwrite("Error starting PostgreSQL: %s" % e,gui.LOG_ERR) return else: self.logwrite("PostgreSQL started") self._setstate(_STATE_STORE_)
def _startpsql(self): """ start postgresl server NOTE: 1) no state checking done here 2) assumes sudo password is entered """ # attempt to start psql try: self.logwrite("Starting PostgreSQL...", gui.LOG_NOTE) cmdline.service('postgresql', self._pwd) time.sleep(0.5) if not cmdline.runningprocess('postgres'): raise RuntimeError('unknown') except RuntimeError as e: self.logwrite("Error starting PostgreSQL: %s" % e, gui.LOG_ERR) return else: self.logwrite("PostgreSQL started") self._setstate(_STATE_STORE_)
def _create(self): # read in conf file, exit on error msgs = [(time.strftime('%H:%M:%S'), "Wraith v%s" % wraith.__version__, gui.LOG_NOERR)] self._conf = readconf() if 'err' in self._conf: msgs.append((time.strftime('%H:%M:%S'), "Configuration file is invalid. %s" % self._conf['err'], gui.LOG_ERR)) return # determine if postgresql is running if cmdline.runningprocess('postgres'): # update self,msg and connect msgs.append((time.strftime('%H:%M:%S'), 'PostgreSQL is running', gui.LOG_NOERR)) if not self._pwd: self._bSQL = True self._setstate(_STATE_STORE_) (self._conn,ret) = psqlconnect(self._conf['store']) if not self._conn is None: self._setstate(_STATE_CONN_) else: msgs.append((time.strftime('%H:%M:%S'), "Error connecting to PostgreSQL: %s" % ret, gui.LOG_ERR)) return else: msgs.append((time.strftime('%H:%M:%S'), "PostgreSQL is not running", gui.LOG_WARN)) msgs.append((time.strftime('%H:%M:%S'), "Not connected to database", gui.LOG_WARN)) # nidus running? if cmdline.nidusrunning(wraith.NIDUSPID): msgs.append((time.strftime('%H:%M:%S'), "Nidus is running", gui.LOG_NOERR)) self._setstate(_STATE_NIDUS_) else: msgs.append((time.strftime('%H:%M:%S'), "Nidus is not running", gui.LOG_WARN)) # dyskt running? if cmdline.dysktrunning(wraith.DYSKTPID): msgs.append((time.strftime('%H:%M:%S'), "DySKT is running", gui.LOG_NOERR)) self._setstate(_STATE_DYSKT_) else: msgs.append((time.strftime('%H:%M:%S'), "DySKt is not running", gui.LOG_WARN)) # set initial state to initialized self._setstate(_STATE_INIT_) # adjust menu options accordingly self._menuenable() # show log and write messages self.viewlog() self.getpanel("log",True).delayedwrite(msgs)
while not cmdline.testsudopwd(pwd) and i > 0: pwd = getpass.unix_getpass(prompt="Incorrect password, try again:") i -= 1 if not pwd: ap.error("Three incorrect password attempts") # stop DySKT, then Nidus, then PostgreSQL sd = sn = sp = True if cmdline.dysktrunning(wraith.DYSKTPID): ret = 'ok' if stopdyskt(pwd) else 'fail' print "Stopping DySKT\t\t\t\t[%s]" % ret else: print "DySKT not running" if cmdline.nidusrunning(wraith.NIDUSPID): ret = 'ok' if stopnidus(pwd) else 'fail' print "Stopping Nidus\t\t\t\t[%s]" % ret else: print "Nidus not running" if cmdline.runningprocess('postgres') and not exclude: ret = 'ok' if stoppsql(pwd) else 'fail' print "Stopping PostgreSQL\t\t\t[%s]" % ret else: print "PostgreSQL not running" sys.exit(0) # start specified services if sopts == 'nogui': # verify pwd is present i = 2 pwd = getpass.unix_getpass(prompt="Password [for %s]:" % getpass.getuser()) while not cmdline.testsudopwd(pwd) and i > 0: pwd = getpass.unix_getpass(prompt="Incorrect password, try again:") i -= 1 if not pwd: ap.error("Three incorrect password attempts")
def _create(self): """ create Iryi and member processes """ # read in and validate the conf file self._readconf() # intialize shared objects self._ic = mp.Queue() # comms for children self._pConns = {} # dict of connections to children # initialize children # Each child is expected to initialize in the _init_ function and throw # a RuntimeError on failure. For non-mandatory components, all exceptions # are caught in in inner exceptions, mandatory are caught in the the outer # exception and result in shutting down try: # don't even bother if psql is local and it's not running host = self._conf['store']['host'] if host == '127.0.0.1' or host == 'localhost': if not runningprocess('postgres'): raise RuntimeError( "Backend:PostgreSQL:service not running") # create circular buffers abuffer = memoryview(mp.Array('B', DIM_M * DIM_N, lock=False)) if self._conf['shama']: sbuffer = memoryview(mp.Array('B', DIM_M * DIM_N, lock=False)) else: sbuffer = None # start Collator first (IOT accept comms) -> don't catch the exception # here if it fails but allow it to pass to the outer loop logging.info("Initializing subprocesses...") logging.info("Starting Collator...") (conn1, conn2) = mp.Pipe() self._pConns['collator'] = conn1 self._collator = Collator(self._ic, conn2, abuffer, sbuffer, self._conf) # start the gps logging.info("Starting GPS...") try: (conn1, conn2) = mp.Pipe() self._gpsd = GPSController(self._ic, conn2, self._conf['gps']) self._pConns['gpsd'] = conn1 except RuntimeError as e: logging.warning("GPSD failed: %s, continuing w/out" % e) conn1.close() conn2.close() # set the region? if so, do it prior to starting the radio(s) rd = self._conf['local']['region'] if rd: logging.info("Setting regulatory domain to %s...", rd) self._rd = radio.getrd radio.setrd(rd) # abad radio is mandatory -> as with the Collator, we cannot continue # w/out this one, allow the outer block to catch any failures amode = smode = None amode = 'paused' if self._conf['abad']['paused'] else 'scan' logging.info("Starting Abad...") (conn1, conn2) = mp.Pipe() self._pConns['abad'] = conn1 self._ar = RadioController(self._ic, conn2, abuffer, self._conf['abad']) # shama if present if self._conf['shama']: smode = 'paused' if self._conf['shama']['paused'] else 'scan' logging.info("Starting Shama...") try: (conn1, conn2) = mp.Pipe() self._sr = RadioController(self._ic, conn2, sbuffer, self._conf['shama']) self._pConns['shama'] = conn1 except RuntimeError as e: logging.warning("Shama failed: %s, continuing w/out", e) conn1.close() conn2.close() # c2c socket -> on failure log it and keep going logging.info("Starting C2C...") try: (conn1, conn2) = mp.Pipe() self._c2c = C2C(conn2, self._conf['local']['c2c']) self._pConns['c2c'] = conn1 except Exception as e: logging.warn("C2C failed: %s, continuing without", e) conn1.close() conn2.close() except RuntimeError as e: # e should have the form "Major:Minor:Description" ms = e.message.split(':') logging.error("%s (%s) %s", ms[0], ms[1], ms[2]) self._state = IYRI_INVALID except (EOFError, OSError): pass # hide any closed pipe errors except Exception as e: # catchall logging.error("Unidentified: %s %s\n%s", type(e).__name__, e, valrep.tb()) self._state = IYRI_INVALID else: # start children execution self._state = IYRI_CREATED self._collator.start() logging.info("Collator-%d started", self._collator.pid) if self._gpsd: self._gpsd.start() logging.info("GPS-%d started", self._gpsd.pid) self._ar.start() logging.info("Abad-%d started in %s mode", self._ar.pid, amode) if self._sr: self._sr.start() logging.info("Shama-%d started in %s mode", self._sr.pid, smode) if self._c2c: logging.info("C2C listening on port %d", self._conf['local']['c2c']) self._c2c.start()
def _initialize(self): """ initialize gui, determine initial state """ # configure panel & write initial message # have to manually enter the desired size, as the menu does not expand # the visibile portion automatically self.tk.wm_geometry("300x1+0+0") self.tk.resizable(0,0) self.logwrite("Wraith v%s" % wraith.__version__) # read in conf file, exit on error confMsg = self._readconf() if confMsg: self.logwrite("Configuration file is invalid. " + confMsg,gui.LOG_ERR) return # determine if postgresql is running if cmdline.runningprocess('postgres'): self.logwrite('PostgreSQL is running',gui.LOG_NOTE) self._bSQL = True # update state self._setstate(_STATE_STORE_) curs = None try: # attempt to connect and set state accordingly self._conn = psql.connect(host=self._conf['store']['host'], dbname=self._conf['store']['db'], user=self._conf['store']['user'], password=self._conf['store']['pwd']) # set to use UTC and enable CONN flag curs = self._conn.cursor() curs.execute("set time zone 'UTC';") self._conn.commit() self._setstate(_STATE_CONN_) self.logwrite("Connected to database",gui.LOG_NOTE) except psql.OperationalError as e: if e.__str__().find('connect') > 0: self.logwrite("PostgreSQL is not running",gui.LOG_WARN) self._setstate(_STATE_STORE_,False) elif e.__str__().find('authentication') > 0: self.logwrite("Authentication string is invalid",gui.LOG_ERR) else: self.logwrite("Unspecified DB error occurred",gui.LOG_ERR) self._conn.rollback() self.logwrite("Not connected to database",gui.LOG_WARN) finally: if curs: curs.close() else: self.logwrite("PostgreSQL is not running",gui.LOG_WARN) self.logwrite("Not connected to database",gui.LOG_WARN) # nidus running? if cmdline.nidusrunning(wraith.NIDUSPID): self.logwrite("Nidus is running") self._setstate(_STATE_NIDUS_) else: self.logwrite("Nidus is not running",gui.LOG_WARN) if cmdline.dysktrunning(wraith.DYSKTPID): self.logwrite("DySKT is running") self._setstate(_STATE_DYSKT_) else: self.logwrite("DySKT is not running",gui.LOG_WARN) # set initial state to initialized self._setstate(_STATE_INIT_) # adjust menu options accordingly self._menuenable()
def _initialize(self): """ initialize gui, determine initial state """ # configure panel & write initial message # have to manually enter the desired size, as the menu does not expand # the visibile portion automatically self.tk.wm_geometry("300x1+0+0") self.tk.resizable(0, 0) self.logwrite("Wraith v%s" % wraith.__version__) # read in conf file, exit on error confMsg = self._readconf() if confMsg: self.logwrite("Configuration file is invalid. " + confMsg, gui.LOG_ERR) return # determine if postgresql is running if cmdline.runningprocess('postgres'): self.logwrite('PostgreSQL is running', gui.LOG_NOTE) self._bSQL = True # update state self._setstate(_STATE_STORE_) curs = None try: # attempt to connect and set state accordingly self._conn = psql.connect(host=self._conf['store']['host'], dbname=self._conf['store']['db'], user=self._conf['store']['user'], password=self._conf['store']['pwd']) # set to use UTC and enable CONN flag curs = self._conn.cursor() curs.execute("set time zone 'UTC';") self._conn.commit() self._setstate(_STATE_CONN_) self.logwrite("Connected to database", gui.LOG_NOTE) except psql.OperationalError as e: if e.__str__().find('connect') > 0: self.logwrite("PostgreSQL is not running", gui.LOG_WARN) self._setstate(_STATE_STORE_, False) elif e.__str__().find('authentication') > 0: self.logwrite("Authentication string is invalid", gui.LOG_ERR) else: self.logwrite("Unspecified DB error occurred", gui.LOG_ERR) self._conn.rollback() self.logwrite("Not connected to database", gui.LOG_WARN) finally: if curs: curs.close() else: self.logwrite("PostgreSQL is not running", gui.LOG_WARN) self.logwrite("Not connected to database", gui.LOG_WARN) # nidus running? if cmdline.nidusrunning(wraith.NIDUSPID): self.logwrite("Nidus is running") self._setstate(_STATE_NIDUS_) else: self.logwrite("Nidus is not running", gui.LOG_WARN) if cmdline.dysktrunning(wraith.DYSKTPID): self.logwrite("DySKT is running") self._setstate(_STATE_DYSKT_) else: self.logwrite("DySKT is not running", gui.LOG_WARN) # set initial state to initialized self._setstate(_STATE_INIT_) # adjust menu options accordingly self._menuenable()