This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
wlanctl
171 lines (147 loc) · 5.43 KB
/
wlanctl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
try:
_path = os.path.abspath(os.path.dirname(__file__))
execfile('%s/venv/bin/activate_this.py'%_path, dict(__file__='%s/venv/bin/activate_this.py'%_path))
except:
pass
from toughlib import choosereactor
choosereactor.install_optimal_reactor(False)
import sys, os,click,signal
sys.path.insert(0, os.path.split(__file__)[0])
from twisted.python import log
from twisted.internet import reactor
from toughlib import config as iconfig
from toughlib.dbengine import get_engine
from toughlib import dispatch,logger
from toughwlan import __version__
import platform as pf
reactor.suggestThreadPoolSize(60)
def setup_logger(config):
log.startLogging(sys.stdout)
syslog = logger.Logger(config)
dispatch.register(syslog)
return syslog
def update_timezone(config):
if 'TZ' not in os.environ:
os.environ["TZ"] = config.system.tz
try:time.tzset()
except:pass
def reactor_run():
def ExitHandler(signum, stackframe):
print "Got signal: %s" % signum
reactor.callFromThread(reactor.stop)
signal.signal(signal.SIGTERM, ExitHandler)
reactor.run()
def print_version(ctx, param, value):
if not value or ctx.resilient_parsing:
return
click.echo(click.style("ToughWLAN {0}".format(__version__),fg='cyan'))
ctx.exit()
def print_info(ctx, param, value):
if not value or ctx.resilient_parsing:
return
click.echo(click.style("Linux distribution: {0}".format(','.join(pf.linux_distribution())),fg='cyan'))
click.echo(click.style("ToughWLAN version: {0}".format(__version__),fg='cyan'))
click.echo(click.style("Env_home: {0}".format(os.environ.get("HOME")),fg='cyan'))
click.echo(click.style("Env_path: {0}".format(os.environ.get("PATH")),fg='cyan'))
click.echo(click.style("Server platform: {0},{1}".format(pf.platform(),pf.machine()),fg='cyan'))
click.echo(click.style("Python version: {0},{1}".format(pf.python_implementation(), pf.python_version()),fg='cyan'))
ctx.exit()
@click.group()
@click.option('--version', is_flag=True, callback=print_version,expose_value=False, is_eager=True)
@click.option('--info', is_flag=True, callback=print_info,expose_value=False, is_eager=True, help="server info")
def cli():
pass
@click.command()
@click.option('-p','--port', default=0,type=click.INT, help='customer http port')
@click.option('-c','--conf', default='/etc/toughwlan.json', help='config file')
@click.option('-d','--debug', is_flag=True)
def httpd(port,conf,debug):
""" httpd option """
from toughwlan.manage import httpd
config = iconfig.find_config(conf)
update_timezone(config)
if debug:
config.defaults.debug = True
if port > 0:
config['admin']['port'] = port
dbengine = get_engine(config)
setup_logger(config)
httpd.run(config,dbengine)
reactor_run()
@click.command()
@click.option('-c','--conf', default='/etc/toughwlan.json', help='config file')
@click.option('-d','--debug', is_flag=True)
def task(conf,debug):
""" task option """
from toughwlan.manage import ddns_task
config = iconfig.find_config(conf)
update_timezone(config)
if debug:
config.defaults.debug = True
dbengine = get_engine(config)
setup_logger(config)
ddns_task.run(config,dbengine)
reactor_run()
@click.command()
@click.option('-c','--conf', default='/etc/toughwlan.json', help='config file')
@click.option('-d','--debug', is_flag=True)
def portald(conf,debug):
""" portal listen option """
from toughwlan.manage.portal import portald
config = iconfig.find_config(conf)
update_timezone(config)
if debug:
config.defaults.debug = True
dbengine = get_engine(config)
setup_logger(config)
portald.run(config,dbengine)
reactor_run()
@click.command()
@click.option('-c','--conf', default='/etc/toughwlan.json', help='config file')
@click.option('-f','--force', is_flag=True)
def initdb(conf,force):
""" initdb option """
from toughwlan.common import initdb as init_db
config = iconfig.find_config(conf)
init_db.update(config,force=force)
# @click.command()
# @click.option('-c','--conf', default='/etc/toughwlan.json', help='config file')
# @click.option('-f','--force', is_flag=True)
# def inittest(conf,force):
# """ initdb option """
# from toughwlan.common import inittest as init_test
# config = iconfig.find_config(conf)
# init_test.update(config)
@click.command()
@click.option('-c','--conf', default='/etc/toughwlan.json', help='config file')
@click.option('-d','--debug', is_flag=True)
@click.option('--exitwith', default=0,type=click.INT, help='exit after sec')
def standalone(conf,debug,exitwith):
""" wlan standalone mode option """
from toughwlan.manage import httpd
from toughwlan.manage import ddns_task
from toughwlan.manage.portal import portald
config = iconfig.find_config(conf)
update_timezone(config)
if debug:
config.defaults.debug = True
dbengine = get_engine(config)
log = setup_logger(config)
httpd.run(config,dbengine)
ddns_task.run(config,dbengine)
portald.run(config,dbengine)
if exitwith > 0:
print "testing application running and exit after %s seconds" % exitwith
reactor.callLater(exitwith,reactor.stop)
reactor_run()
cli.add_command(httpd)
cli.add_command(task)
cli.add_command(portald)
cli.add_command(initdb)
# cli.add_command(inittest)
cli.add_command(standalone)
if __name__ == '__main__':
cli()