-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathteleceptorcmd
executable file
·361 lines (286 loc) · 10.9 KB
/
teleceptorcmd
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env python
"""
Authors: Victor Szczepanski
Evan Salazar
Cyrille Gindreau
"""
import runpy
import sys
import os
import atexit
import shutil
import platform
import json
import time
import requests
from os import fdopen, remove
from subprocess import call
from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
from sqlalchemy.engine import reflection
from sqlalchemy.schema import (
MetaData,
Table,
DropTable,
ForeignKeyConstraint,
DropConstraint,
)
import teleceptor
from teleceptor import models, testFixtures, server, sessionManager, USE_ELASTICSEARCH, WEBROOT, SQLDATA, USE_SQL_ALWAYS, USEPG
Stdin = '/dev/null'
Stdout = '/dev/null'
Stderr = '/dev/null'
def delpid():
os.remove(self.pidfile)
def runserver(*args):
"""
do the UNIX double-fork magic, see Stevens' "Advanced
Programming in the UNIX Environment" for details (ISBN 0201563177)
http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
"""
server.runserver(server.get_cp_config())
def btcmote(*args):
btcmotecmd = os.path.join(teleceptor.__path__[0], 'softSensors', 'BitcoinSensor', 'BTCMote.py')
print btcmotecmd
runpy.run_path(btcmotecmd, None, '__main__')
def apiTest(*args):
print "Running api test."
runpy.run_path('unittests/apiTests.py', None, '__main__')
def poller(*args):
pollercmd = os.path.join(teleceptor.__path__[0], 'basestation', 'poller.py')
print pollercmd
runpy.run_path(pollercmd, None, '__main__')
def serialPoller(*args):
sys.argv = ["something here "]
if len(args[0]) is not 1:
print "Please enter the name of the USB device you are trying to connect to. Read the documentation to find out how to find your device name."
serialpollercmd = os.path.join(teleceptor.__path__[0], 'basestation', 'serialPoller.py')
print serialpollercmd
runpy.run_path(serialpollercmd, {"DEVICENAME": args[0]}, '__main__')
def tcpPoller(*args):
tcppollercmd = os.path.join(teleceptor.__path__[0], 'basestation', 'tcpPoller.py')
print tcppollercmd
runpy.run_path(tcppollercmd, None, '__main__')
def copyConfig(*args):
if platform.system() == 'Windows':
appdata = os.path.join(os.getenv("APPDATA"), "teleceptor")
else:
appdata = os.path.join(os.getenv("HOME"), ".config", "teleceptor")
# Check if file exists
if os.path.exists(os.path.join(appdata, 'config.json')):
print "Config Exists at: " + str(os.path.join(appdata, 'config.json'))
return
if not os.path.exists(appdata):
os.makedirs(appdata)
fromfile = os.path.join(teleceptor.PATH, 'config', "defaults.json")
tofile = os.path.join(appdata, "config.json")
shutil.copy2(fromfile, tofile)
print "Config installed at:" + str(tofile)
def displayVersion(*args):
print "teleceptor Version: " + teleceptor.__version__
def rebuild(*args):
print 'This command will wipe your database and start fresh. Are you sure you want to continue?'
user_resp = None
user_resp = raw_input('(Y/n) ').rstrip()
while user_resp not in ['Y', 'n']:
print 'Did not understand your response. Please enter \'Y\' or \'n\'.'
print 'This command will wipe your database and start fresh. Are you sure you want to continue?'
user_resp = raw_input('(Y/n) ').rstrip()
if user_resp == 'n':
sys.exit(0)
assert user_resp == 'Y', 'Something Bad Happened!'
if teleceptor.USEPG:
rebuild_postgres()
else:
rebuild_sqlite()
print "Runing setup."
setup()
def rebuild_sqlite():
"""
Handles rebuilding sqlite db.
"""
print("Deleting " + str(teleceptor.DBFILE) + " ...")
try:
os.remove(teleceptor.DBFILE)
print teleceptor.DBFILE + " deleted."
except OSError:
print('base_station.db does not exist. Creating new db.')
def rebuild_postgres():
"""
Handles rebuilding postgres db. Drops all tables in the postgres db.
The following receipe is taken from Atlassian for dropping all tables even if there are cyclical dependencies.
See https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/DropEverything
"""
# Get a connection to the postgres db
dboptions = {}
dboptions['drivername'] = 'postgres'
dboptions['host'] = teleceptor.PGSQLDBHOST
dboptions['port'] = teleceptor.PGSQLDBPORT
dboptions['username'] = teleceptor.PGSQLDBUSERNAME
dboptions['password'] = teleceptor.PASSWORD
dboptions['database'] = teleceptor.PGSQLDBNAME
dbURL = URL(**dboptions)
engine = create_engine(dbURL)
print("Dropping all tables from " + str(dbURL) + " ...")
conn = engine.connect()
# the transaction only applies if the DB supports
# transactional DDL, i.e. Postgresql, MS SQL Server
trans = conn.begin()
inspector = reflection.Inspector.from_engine(engine)
# gather all data first before dropping anything.
# some DBs lock after things have been dropped in
# a transaction.
metadata = MetaData()
tbs = []
all_fks = []
for table_name in inspector.get_table_names():
fks = []
for fk in inspector.get_foreign_keys(table_name):
if not fk['name']:
continue
fks.append(
ForeignKeyConstraint((), (), name=fk['name'])
)
t = Table(table_name, metadata, *fks)
tbs.append(t)
all_fks.extend(fks)
for fkc in all_fks:
conn.execute(DropConstraint(fkc))
for table in tbs:
conn.execute(DropTable(table))
trans.commit()
print("Done dropping tables for " + str(dbURL))
def setup(*args):
if not teleceptor.USEPG:
print "Creating new base_station.db file..."
if not os.path.exists(os.path.dirname(teleceptor.DBFILE)):
os.makedirs(os.path.dirname(teleceptor.DBFILE))
open(teleceptor.DBFILE, 'a').close()
print teleceptor.DBFILE + " created."
dbURL = 'sqlite:///' + teleceptor.DBFILE
else:
dboptions = {}
dboptions['drivername'] = 'postgres'
dboptions['host'] = teleceptor.PGSQLDBHOST
dboptions['port'] = teleceptor.PGSQLDBPORT
dboptions['username'] = teleceptor.PGSQLDBUSERNAME
dboptions['password'] = teleceptor.PASSWORD
dboptions['database'] = teleceptor.PGSQLDBNAME
dbURL = URL(**dboptions)
db = create_engine(dbURL)
print "Initializing database tables..."
models.Base.metadata.create_all(db)
if USE_ELASTICSEARCH:
# TODO: Use index and doc from config
conf = json.load(open(os.path.join(teleceptor.PATH, 'config', 'es_template.json')))
r = requests.put(teleceptor.ELASTICSEARCH_URI+'/_template/teleceptor', json=conf)
print r
print "Run teleceptorcmd loadfixtures for example data"
def loadfixtures(*args):
print "Loading fixtures..."
testFixtures.main()
def shell(*args):
import IPython
print "Starting IPython Shell"
session = sessionManager.createSession()
IPython.embed()
def minorVersion(*args):
print "Current version: {}".format(teleceptor.__version__)
version = teleceptor.__version__.split('.')
version[2] = str(int(version[2]) + 1)
version = ".".join(version)
saveVersion(version)
def majorVersion(*args):
print "Current version: {}".format(teleceptor.__version__)
version = teleceptor.__version__.split('.')
version[1] = str(int(version[1]) + 1)
version[2] = "0"
version = ".".join(version)
saveVersion(version)
def saveVersion(version):
newTime = time.time()
print "New version is: {}".format(version)
newFile = ""
with open('teleceptor/version.py', 'r') as versionFile:
for line in versionFile:
if line.startswith("__version__"):
line = "__version__ = '{}'\n".format(version)
if line.startswith("__buildDate__"):
line = "__buildDate__ = {}\n".format(newTime)
newFile += line
f = open('teleceptor/version.py', 'w+')
for i in newFile:
f.write(i)
f.close()
with open('package.json') as package:
data = json.load(package)
data['version'] = version
data['buildDate'] = newTime
os.remove('package.json')
with open('package.json', 'w') as package:
json.dump(data, package, indent=2, sort_keys=True)
if __name__ == "__main__":
cmds = {
'runserver': [runserver, "Start the server"],
'copyconfig': [copyConfig, "Copy default config to home"],
'rebuild': [rebuild, "Rebuild the database and datafiles"],
'setup': [setup, "Build the database"],
'btcmote': [btcmote, "Example sensor that collects BTC informaiotn"],
'poller': [poller, "General poller"],
'serialpoller': [serialPoller, "Serial poller"],
'tcppoller': [tcpPoller, "TCP poller (Hosts are added in config.json)"],
'loadfixtures': [loadfixtures, "Load test database data"],
'version': [displayVersion, "Display version"],
'shell': [shell, "Ipython shell"],
'apitest': [apiTest, "Run tests against the api."],
'minorversion': [minorVersion, "Increment the minor version number"],
'majorversion': [majorVersion, "Increment the major version number"]
}
args = sys.argv
print args
if len(args) > 1 and args[1].lower() in cmds:
if len(args) > 2 and args[2] == "-d":
print "Starting process %s as daemon..." % args[1].lower()
pidfilename = "/tmp/teleceptor-" + args[1].lower() + '.pid'
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# decouple from parent environment
os.chdir("/")
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
print "pid: %s" % os.getpid()
# redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = file(Stdin, 'r')
so = file(Stdout, 'a+')
se = file(Stderr, 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
# write pidfile
atexit.register(delpid)
pid = str(os.getpid())
file(pidfilename, 'w+').write("%s\n" % pid)
cmds[args[1].lower()][0](args[2:])
print "Done!"
else:
print "Please enter a valid command"
print "Commands Are:"
for c in sorted(cmds):
print " %s : %s" % (c, cmds[c][1])