aboutsummaryrefslogtreecommitdiffstats
path: root/server/alias.py
blob: ae3d19180c7eca192870620fa2c941da7cafba16 (plain)
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
#!/usr/bin/python2
import logging
from argparse import ArgumentParser
from config import filename, config
import daemon
import daemon.pidfile
from server import ObjectComponent

if __name__ == '__main__':
    commandline = ArgumentParser(description = 'Connect the alias \
                                component to a given server')
    commandline.add_argument('-p', '--port',
                             help = 'Port to connect to',
                             type = int)
    commandline.add_argument('-s', '--secret',
                             help = 'password')
    commandline.add_argument('-n', '--name',
                             help = 'Name the component will have')
    commandline.add_argument('-r', '--root',
                             help = 'Root directory of the user files')
    commandline.add_argument('-c', '--config',
                             help = 'Name of the config file to use')
    commandline.add_argument('-d', '--debug',
                             help = 'Set log level to DEBUG',
                             action = 'store_const',
                             const = logging.DEBUG,
                             default = logging.INFO)
    commandline.add_argument('-o', '--host',
                             help = 'Host to connect to')
    commandline.add_argument('-b', '--background',
                             help = 'run the server in the background',
                             action = 'store_true')
    commandline.add_argument('--logfile',
                             help = 'location of the log file (default /var/log/${name}.log',
                             action = 'store_const')
    commandline.add_argument('--pidfile',
                             help = 'location of the pid file (default /var/run/${name}.pid',
                             action = 'store_const')
    
    args = commandline.parse_args()

    if args.config is None:
        logging.basicConfig(level = args.debug)
        config.name = args.name
        config.port = args.port
        config.secret = args.secret
        config.root = args.root
        config.host = args.host
        config.background = args.background
        config.logfile = args.logfile
        config.pidfile = args.pidfile
    else:
        filename = args.config
        logging.basicConfig(level = args.debug)
        config.read(filename)

    logger = logging.getLogger("DaemonLog")
    logger.setLevel(logging.INFO)
    formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    handler = logging.FileHandler(config.logfile)
    logger.addHandler(handler)
    context = daemon.DaemonContext(detach_process = config.background,
                                   pidfile = daemon.pidfile.TimeoutPIDLockFile(config.pidfile,10),
                                   files_preserve=[handler.stream])
    with context:
        component = ObjectComponent(config.name, config.secret,
                                    config.host, config.port,
                                    config.root)
        if component.connect():
            logging.info('Component {} connected'.format(component.boundjid))
            component.process(False)
        else:
            logging.error("Component {} couldn't connect".format(component.boundjid))