Skip to content

Commit

Permalink
Merge branch 'clean-repo' into 'master'
Browse files Browse the repository at this point in the history
Clean repo

See merge request doylet9/2018-CA326-tdoyle-distributedwifimonitoring!18
  • Loading branch information
Thomas Doyle committed Mar 5, 2018
2 parents 066861e + efdaf0e commit 8869e5a
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 165 deletions.
2 changes: 2 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ test:
#- echo -e "slack_token = \"$slack_token\"\nslack_channel = \"#random\"" > config.py # Build config with environment variable
# Testing begins here
- python3.6 -m unittest discover -v
# Clean up after tests
- ./stop
- coverage run --source=. -m unittest discover -v
- coverage report -m
artifacts:
Expand Down
12 changes: 7 additions & 5 deletions code/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def daemonise(self):
# write pidfile
atexit.register(self.delpid)
pid = str(os.getpid())
open(self.pidfile, 'w+').write(f'{pid}|n')
open(self.pidfile, 'w+').write(f'{pid}')

pid = str(os.getpid())
with open(self.pidfile, 'w+') as f:
Expand All @@ -84,15 +84,18 @@ def start(self):
"""
try:
with open(self.pidfile, 'r') as pf:
pid = int(pf.read().strip())
pf.close()
pid = pf.read().strip()
#if pf.read().strip() != '':
# pid = pf.read().strip()
#else:
# pid = None
except IOError:
pid = None

if pid:
sys.stderr.write(f'pidfile {self.pidfile} already exists.' + \
'Daemon already running?\n')
sys.exit(1)
sys.exit(7)

# "real" start
self.daemonise()
Expand All @@ -112,7 +115,6 @@ def stop(self):
# somtimes pid = ''
if pid:
pid = int(pid)
pf.close()
except IOError:
pid = None

Expand Down
21 changes: 0 additions & 21 deletions code/plugins/ap_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,6 @@ def run(self):
self.data = []
time.sleep(self.sleeptime)

# How panoptes controls daemon
def command(order):
"""
Recieves command from panoptes to start stop or restart
:order: string command to decide what to do
"""
metric = Ap_Metrics('/tmp/apMetrics.pid')
if 'start' == order:
return 'Starting'
metric.start()
elif 'restart' == order:
return 'Restarted'
metric.restart()
elif 'stop' == order:
metric.stop()
return 'Stopped'
else:
return 'Command Unknown'
sys.exit(2)

if __name__ == '__main__':
metric = Ap_Metrics('/tmp/apMetrics.pid')
if len(sys.argv) >= 2:
Expand Down
43 changes: 16 additions & 27 deletions code/plugins/nodes_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,22 @@ def run(self):
self.metric.tcp_fling(metric)
time.sleep(self.sleeptime)

# How panoptes controls daemon
def command(order):
stats = Nodes_Up('/tmp/nodesUp.pid')
if 'start' == order:
return 'Starting'
stats.start()
elif 'restart' == order:
stats.restart()
return 'Restarted'
elif 'stop' == order:
stats.stop()
return 'Stopped'
else:
sys.exit(2)

if __name__ == '__main__':
order = sys.argv[1]
stats = Nodes_Up('/tmp/nodesUp.pid')
if 'start' == order:
print('Starting')
stats.start()
elif 'restart' == order:
stats.restart()
print('Restarted')
elif 'stop' == order:
stats.stop()
print('Stopped')
if len(sys.argv) <= 2:
order = sys.argv[1]
stats = Nodes_Up('/tmp/nodesUp.pid')
if 'start' == order:
print('Starting')
stats.start()
elif 'restart' == order:
stats.restart()
print('Restarted')
elif 'stop' == order:
stats.stop()
print('Stopped')
else:
print('Unknown command given')
sys.exit(2)
else:
print('Unknown command given')
print('No command given')
sys.exit(2)
21 changes: 0 additions & 21 deletions code/plugins/packet_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,6 @@ def run(self):
f.write(f'Packet_Stats plugin has encountered a problem {e}\n')
f.close()

# How panoptes controls daemon
def command(order):
"""
Recieves orders from panoptes and decides to start stop restart
:order: string to start stop or restart
"""
pktstats = Packet_Stats('/tmp/packetStats.pid')
if 'start' == order:
return 'Starting'
pktstats.start()
elif 'restart' == order:
return 'Restarted'
pktstats.restart()
elif 'stop' == order:
pktstats.stop()
return 'Stopped'
else:
return 'Command Unknown'
sys.exit(2)

if __name__ == '__main__':
pktstats = Packet_Stats('/tmp/packetStats.pid')
if len(sys.argv) >= 2:
Expand Down
22 changes: 0 additions & 22 deletions code/plugins/ping_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,6 @@ def run(self):
except Exception:
pass

# How panoptes controls daemon
def command(order):
"""
Recieves order and executes comand given by panoptes
:order: string given by panoptes to start stop or restart
"""
# Create object of class above and run it
pinger = Ping_Metric('/tmp/pingMetric.pid')
if order == 'start':
return 'Starting'
pinger.start()
elif order == 'restart':
pinger.restart()
return 'Restarted'
elif order == 'stop':
pinger.stop()
return 'Stopped'
else:
sys.exit(2)


if __name__ == '__main__':
order = sys.argv[1]
pinger = Ping_Metric('/tmp/pingMetric.pid')
Expand Down
21 changes: 0 additions & 21 deletions code/plugins/ssid_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,6 @@ def run(self):
self.read_ssid()
self.find_ap()

# How panoptes controls daemon
def command(order):
"""
Recieves orders from panoptes and decides to start stop restart
:order: string to start stop or restart
"""
spoof = Ssid_Detection('/tmp/ssidDetection.pid')
if 'start' == order:
return 'Starting'
spoof.start()
elif 'restart' == order:
return 'Restarted'
spoof.restart()
elif 'stop' == order:
spoof.stop()
return 'Stopped'
else:
return 'Command Unknown'
sys.exit(2)

if __name__ == '__main__':
spoof = Ssid_Detection('/tmp/ssidDetection.pid')
if len(sys.argv) >= 2:
Expand Down
18 changes: 0 additions & 18 deletions code/plugins/system_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,6 @@ def run(self):
self.metric.tcp_fling(data)
time.sleep(self.sleeptime)

# How panoptes controls daemon
def command(order):
"""
Starts the daemon from panoptes
"""
stats = System_Stats('/tmp/systemStats.pid')
if 'start' == order:
return 'Starting'
stats.start()
elif 'restart' == order:
return 'Restarted'
stats.restart()
elif 'stop' == order:
stats.stop()
return 'Stopped'
else:
sys.exit(2)

if __name__ == '__main__':
stats = System_Stats('/tmp/systemStats.pid')
if len(sys.argv) >= 2:
Expand Down
26 changes: 12 additions & 14 deletions code/tests/test_node_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

import os
from time import strftime
from plugins.nodes_up import command
from plugins.nodes_up import Nodes_Up
import time
import unittest

class Nodes_Up_TestCase(unittest.TestCase):
"""Test Nodes Up"""
def setUp(self):
self.stats = Nodes_Up('/tmp/nodesUp.pid')

def test01_nodes_up_start(self):
"""Test Nodes up start"""
ans = command('start')
self.assertEqual('Starting', ans)
with self.assertRaises(SystemExit) as ex:
self.stats.start()
self.assertEqual(ex.exception.code, 0)

def test02_nodes_up_start(self):
def test02_nodes_up_restart(self):
"""Test Nodes up restart"""
with self.assertRaises(SystemExit) as cm:
command('restart')
self.stats.stop()
self.stats.start()
self.assertEqual(cm.exception.code, 0)

def test03_nodes_up_stop(self):
"""Test Nodes stop"""
ans = command('stop')
self.assertEqual('Stopped', ans)

def test04_unknown_command(self):
"""Test Nodes up bad command"""
with self.assertRaises(SystemExit) as cm:
command('hoogle boodle')
self.assertEqual(cm.exception.code, 2)

self.stats.stop()

def tearDown(self):
self.stats.stop()
27 changes: 11 additions & 16 deletions code/tests/test_system_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import unittest
from plugins.system_stats import System_Stats
from plugins.system_stats import command

class System_Stats_TestCase(unittest.TestCase):
"""Test System Stats"""
Expand All @@ -26,24 +25,20 @@ def test03_parse(self):

def test04_nodes_up_start(self):
"""Test system stats start"""
ans = command('start')
self.assertEqual('Starting', ans)
with self.assertRaises(SystemExit) as ex:
self.stater.start()
self.assertEqual(ex.exception.code, 0)

def test05_nodes_up_restart(self):
"""Test system stats restart"""
#with self.assertRaises(SystemExit) as cm:
# command('restart')
#self.assertEqual(cm.exception.code, 0)
ans = command('restart')
self.assertEqual('Restarted', ans)

with self.assertRaises(SystemExit) as cm:
self.stater.stop()
self.stater.start()
self.assertEqual(cm.exception.code, 0)

def test06_nodes_up_stop(self):
"""Test system stats stop"""
ans = command('stop')
self.assertEqual('Stopped', ans)
self.stater.stop()

def test07_unknown_command(self):
"""Test system stats bad command"""
with self.assertRaises(SystemExit) as cm:
command('hooglety boodlety')
self.assertEqual(cm.exception.code, 2)
def tearDown(self):
self.stater.stop()

0 comments on commit 8869e5a

Please sign in to comment.