Completed
Push — master ( fa4a47...a325ef )
by -
01:44
created

spike-server.py (11 issues)

1
#! /usr/bin/env python
2
3
import os
4
import logging
5
import argparse
6
from os.path import dirname, abspath
7
import sys
8
from time import time, strftime, localtime
9
10
from spike import create_app, version
11
from spike.model import db, rulesets_seeds, whitelists_seeds
12
from spike.model.naxsi_rulesets import NaxsiRuleSets
13
from spike.model.naxsi_whitelistsets import NaxsiWhitelistSets
14
15
from sqlalchemy.exc import SQLAlchemyError
16
17
18
def run(debug=False):
19
    app = create_app(__get_config_file())
20
    db.init_app(app)
21
22
    if debug:
23
        app.test_request_context().push()
24
25
    try:
26
        host = app.config["APP_HOST"]
27
    except KeyError:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable KeyError does not seem to be defined.
Loading history...
28
        host = '127.0.0.1'
29
30
    try:
31
        port = int(app.config["APP_PORT"])
0 ignored issues
show
The variable app does not seem to be defined for all execution paths.
Loading history...
32
    except KeyError:
33
        port = 5555
34
35
    app.run(debug=debug, host=host, port=port)
0 ignored issues
show
The variable debug does not seem to be defined for all execution paths.
Loading history...
36
37
38
def spike_init():
39
    logging.info("Initializing Spike")
40
    timestamp = int(time())
41
42
    app = create_app(__get_config_file())
43
    db.init_app(app)
44
45
    with app.app_context():
46
        db.create_all()
47
48
    for r in rulesets_seeds:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable rulesets_seeds does not seem to be defined.
Loading history...
49
        logging.info("Adding ruleset: %s", r)
50
        rmks = "Ruleset for %s / auto-created %s" % (r, strftime("%F - %H:%M", localtime(time())))
51
        db.session.add(NaxsiRuleSets(r, rmks, timestamp))
52
    for w in whitelists_seeds:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable whitelists_seeds does not seem to be defined.
Loading history...
53
        logging.info("Adding whitelistset: %s", w)
54
        rmks = "Ruleset for %s / auto-created %s" % (w, strftime("%F - %H:%M", localtime(time())))
55
        db.session.add(NaxsiWhitelistSets(w, rmks, timestamp))
56
    try:
57
        db.session.commit()
58
    except SQLAlchemyError:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SQLAlchemyError does not seem to be defined.
Loading history...
59
        logging.error('It seems that the database was already initialized. Did you meant to run `%s run` instead?',
60
                      sys.argv[0])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sys does not seem to be defined.
Loading history...
61
    logging.info('Spike initialization completed')
62
63
64
def __get_config_file():
65
    return os.path.join(dirname(abspath(__name__)), 'config.cfg')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable __name__ does not seem to be defined.
Loading history...
66
67
68
if __name__ == "__main__":
69
    logging.basicConfig(level=logging.DEBUG, format='%(message)s')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable logging does not seem to be defined.
Loading history...
70
    parser = argparse.ArgumentParser(description='Spike %s' % version)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable version does not seem to be defined.
Loading history...
71
    parser.add_argument('command', help='Run the spike server', choices=['run', 'init'])
72
    parser.add_argument('-d', '--debug', help='Run server in debug mode', action='store_true')
73
    args = parser.parse_args()
74
75
    if args.command == 'run':
76
        if not os.path.exists(os.path.join(dirname(abspath(__name__)), 'spike', 'rules.db')):
77
            print('You should run `python %s init` before using Spike' % sys.argv[0])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sys does not seem to be defined.
Loading history...
78
        else:
79
            run(args.debug)
80
    elif args.command == 'init':
81
        spike_init()
82