|
@@ -0,0 +1,120 @@
|
|
|
|
+import click
|
|
|
|
+import hashlib
|
|
|
|
+import json
|
|
|
|
+from sqlalchemy import create_engine
|
|
|
|
+from kamcli.ioutils import ioutils_dbres_print
|
|
|
|
+from kamcli.cli import pass_context
|
|
|
|
+from kamcli.cli import parse_user_spec
|
|
|
|
+from kamcli.iorpc import command_ctl
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+##
|
|
|
|
+#
|
|
|
|
+#
|
|
|
|
[email protected]('dispatcher', help='Manage dispatcher module (load balancer)')
|
|
|
|
+@pass_context
|
|
|
|
+def cli(ctx):
|
|
|
|
+ pass
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+##
|
|
|
|
+#
|
|
|
|
+#
|
|
|
|
[email protected]('add', short_help='Add a new dispatcher destination')
|
|
|
|
[email protected]('flags', '--flags', type=int, default=0,
|
|
|
|
+ help='Flags value')
|
|
|
|
[email protected]('priority', '--priority', type=int, default=0,
|
|
|
|
+ help='Priority value')
|
|
|
|
[email protected]('attrs', '--attrs', default='',
|
|
|
|
+ help='Attributes (default: "")')
|
|
|
|
[email protected]('description', '--desc', default='',
|
|
|
|
+ help='Description (default: "")')
|
|
|
|
[email protected]('setid', metavar='<setid>', type=int)
|
|
|
|
[email protected]('destination', metavar='<destination>')
|
|
|
|
+@pass_context
|
|
|
|
+def dispatcher_add(ctx, flags, priority, attrs, description, setid, destination):
|
|
|
|
+ """Add a new destination in a set of dispatcher db table
|
|
|
|
+
|
|
|
|
+ \b
|
|
|
|
+ Parameters:
|
|
|
|
+ <setid> - dispatching set id
|
|
|
|
+ <destination> - SIP URI for destination
|
|
|
|
+ """
|
|
|
|
+ ctx.vlog('Adding to setid [%d] destination [%s]', setid, destination)
|
|
|
|
+ e = create_engine(ctx.gconfig.get('db', 'rwurl'))
|
|
|
|
+ e.execute('insert into dispatcher (setid, destination, flags, priority, attrs, description) values ({0}, {1!r}, {2}, {3}, {4!r}, {5!r})'.format(setid, destination.encode('ascii','ignore'), flags, priority, attrs.encode('ascii','ignore'), description.encode('ascii','ignore')))
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+##
|
|
|
|
+#
|
|
|
|
+#
|
|
|
|
[email protected]('rm', short_help='Remove a destination from dispatcher table')
|
|
|
|
[email protected]('setid', metavar='<setid>', type=int)
|
|
|
|
[email protected]('destination', metavar='<destination>')
|
|
|
|
+@pass_context
|
|
|
|
+def dispatcher_rm(ctx, setid, destination):
|
|
|
|
+ """Remove a destination from db dispatcher table
|
|
|
|
+
|
|
|
|
+ \b
|
|
|
|
+ Parameters:
|
|
|
|
+ <setid> - dispatching set id
|
|
|
|
+ <destination> - SIP URI for destination
|
|
|
|
+ """
|
|
|
|
+ e = create_engine(ctx.gconfig.get('db', 'rwurl'))
|
|
|
|
+ e.execute('delete from dispatcher where setid={0} and destination={1!r}'.format(setid, destination.encode('ascii','ignore')))
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+##
|
|
|
|
+#
|
|
|
|
+#
|
|
|
|
[email protected]('showdb', short_help='Show dispatcher records in database')
|
|
|
|
[email protected]('oformat', '--output-format', '-F',
|
|
|
|
+ type=click.Choice(['raw', 'json', 'table', 'dict']),
|
|
|
|
+ default=None, help='Format the output')
|
|
|
|
[email protected]('ostyle', '--output-style', '-S',
|
|
|
|
+ default=None, help='Style of the output (tabulate table format)')
|
|
|
|
[email protected]('setid', nargs=-1, metavar='[<setid>]', type=int)
|
|
|
|
+@pass_context
|
|
|
|
+def dispatcher_showdb(ctx, oformat, ostyle, setid):
|
|
|
|
+ """Show details for records in dispatcher table
|
|
|
|
+
|
|
|
|
+ \b
|
|
|
|
+ Parameters:
|
|
|
|
+ <setid> - dispatching set id
|
|
|
|
+ """
|
|
|
|
+ e = create_engine(ctx.gconfig.get('db', 'rwurl'))
|
|
|
|
+ if not setid:
|
|
|
|
+ ctx.vlog('Showing all dispatcher records')
|
|
|
|
+ res = e.execute('select * from dispatcher')
|
|
|
|
+ else:
|
|
|
|
+ ctx.vlog('Showing dispatcher records for set id')
|
|
|
|
+ res = e.execute('select * from dispatcher where setid=%d', setid)
|
|
|
|
+ ioutils_dbres_print(ctx, oformat, ostyle, res)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+##
|
|
|
|
+#
|
|
|
|
+#
|
|
|
|
[email protected]('list', short_help='Show details for dispatcher records in memory')
|
|
|
|
[email protected]('setid', nargs=-1, metavar='[<setid>]', type=int)
|
|
|
|
+@pass_context
|
|
|
|
+def dispatcher_list(ctx, setid):
|
|
|
|
+ """Show details for dispatcher records in memory
|
|
|
|
+
|
|
|
|
+ \b
|
|
|
|
+ Parameters:
|
|
|
|
+ <setid> - dispatching set id
|
|
|
|
+ """
|
|
|
|
+ command_ctl(ctx, 'dispatcher.list', [ ])
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+##
|
|
|
|
+#
|
|
|
|
+#
|
|
|
|
[email protected]('reload', short_help='Reload dispatcher records from database into memory')
|
|
|
|
+@pass_context
|
|
|
|
+def dispatcher_reload(ctx):
|
|
|
|
+ """Reload dispatcher records from database into memory
|
|
|
|
+ """
|
|
|
|
+ command_ctl(ctx, 'dispatcher.reload', [ ])
|
|
|
|
+
|