merged three actions into manage_client.py

This commit is contained in:
Stefan Midjich 2017-03-07 17:15:39 +01:00
parent 3a0346b164
commit 3059459915
3 changed files with 30 additions and 55 deletions

View file

@ -63,7 +63,9 @@ class Client(object):
def commit(self):
self.commit_client()
self.commit_rule()
if self.enabled:
self.commit_rule()
def commit_client(self):

View file

@ -11,7 +11,23 @@ import errors
from storage import StoragePostgres
from client import Client
parser = ArgumentParser()
parser = ArgumentParser((
'Handle clients in the captive portal. Default mode of operation is to'
' create new clients and enable them. Other mode is to --disable the '
'client. And last mode is to --delete the client completely.'
))
parser.add_argument(
'--disable',
default=False,
help='Disable the client in the DB and delete from firewall'
)
parser.add_argument(
'--delete',
default=False,
help='Delete the client from DB and firewall'
)
parser.add_argument(
'--protocol',
@ -46,8 +62,15 @@ try:
chain=config.get('iptables', 'chain')
)
except errors.StorageNotFound:
print('Could not find client')
print('Client not found')
exit(1)
client.enabled = True
client.commit()
if args.disable:
# For non-existing clients this actually creates them in disabled mode.
client.enabled = False
client.commit()
elif args.delete:
client.delete()
else:
client.enabled = True
client.commit()

View file

@ -1,50 +0,0 @@
#!/usr/bin/env python
from sys import exit
from argparse import ArgumentParser, FileType
from pprint import pprint as pp
from configparser import RawConfigParser
import errors
from storage import StoragePostgres
from client import Client
parser = ArgumentParser()
parser.add_argument(
'--protocol',
required=True,
choices=['tcp', 'udp'],
help='Protocol for client'
)
parser.add_argument(
'--config',
type=FileType('r'),
required=True,
help='Configuration file'
)
parser.add_argument(
'src_ip',
help='Client source IP to add'
)
args = parser.parse_args()
config = RawConfigParser()
config.readfp(args.config)
sr = StoragePostgres(config=config)
try:
client = Client(
storage=sr,
ip_address=args.src_ip,
protocol=args.protocol,
chain=config.get('iptables', 'chain')
)
except errors.StorageNotFound:
print('Could not find client')
exit(1)
client.delete()