__init__.py 4.0 KB
Newer Older
N
Nicolas Hennion 已提交
1 2
# -*- coding: utf-8 -*-
#
3
# This file is part of Glances.
N
Nicolas Hennion 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
N
Nicolas Hennion 已提交
19 20 21
"""
Init the Glances software
"""
N
Nicolas Hennion 已提交
22

23
# Import system lib
24 25
import gettext
import locale
26
import signal
A
Alessio Sergi 已提交
27
import sys
28

N
Nicolas Hennion 已提交
29
# Import Glances libs
30
# Note: others Glances libs will be imported optionally
31
from glances.core.glances_globals import gettext_domain, locale_dir
32
from glances.core.glances_main import GlancesMain
N
Nicolas Hennion 已提交
33

A
Alessio Sergi 已提交
34

35 36 37 38 39 40 41 42 43
def __signal_handler(signal, frame):
    """
    Call back for CTRL-C
    """
    end()


def end():
    """
N
Pep8  
Nicolas Hennion 已提交
44
    Stop Glances
45 46
    """

47
    if core.is_standalone():
48 49
        # Stop the standalone (CLI)
        standalone.end()
50
    elif core.is_client():
51 52
        # Stop the client
        client.end()
53
    elif core.is_server():
54 55 56 57 58 59 60
        # Stop the server
        server.end()

    # The end...
    sys.exit(0)


N
Nicolas Hennion 已提交
61
def main():
62 63
    """
    Main entry point for Glances
64

65 66 67
    Select the mode (standalone, client or server)
    Run it...
    """
68 69 70
    # Setup translations
    locale.setlocale(locale.LC_ALL, '')
    gettext.install(gettext_domain, locale_dir)
71 72 73 74

    # Share global var
    global core, standalone, client, server

75 76
    # Create the Glances main instance
    core = GlancesMain()
N
Nicolas Hennion 已提交
77

78 79 80
    # Catch the CTRL-C signal
    signal.signal(signal.SIGINT, __signal_handler)

N
Nicolas Hennion 已提交
81
    # Glances can be ran in standalone, client or server mode
82
    if core.is_standalone():
N
Nicolas Hennion 已提交
83

84
        # Import the Glances standalone module
85
        from glances.core.glances_standalone import GlancesStandalone
86 87

        # Init the standalone mode
N
Nicolas Hennion 已提交
88
        standalone = GlancesStandalone(config=core.get_config(),
N
Nicolas Hennion 已提交
89
                                       args=core.get_args())
90 91 92 93

        # Start the standalone (CLI) loop
        standalone.serve_forever()

94
    elif core.is_client():
N
Nicolas Hennion 已提交
95 96

        # Import the Glances client module
97
        from glances.core.glances_client import GlancesClient
N
Nicolas Hennion 已提交
98 99

        # Init the client
N
Nicolas Hennion 已提交
100 101
        client = GlancesClient(config=core.get_config(),
                               args=core.get_args())
N
Nicolas Hennion 已提交
102 103

        # Test if client and server are in the same major version
104
        if not client.login():
105
            print(_("Error: The server version is not compatible with the client"))
N
Nicolas Hennion 已提交
106 107 108 109 110 111
            sys.exit(2)

        # Start the client loop
        client.serve_forever()

        # Shutdown the client
112
        client.close()
N
Nicolas Hennion 已提交
113

114
    elif core.is_server():
N
Nicolas Hennion 已提交
115

N
Nicolas Hennion 已提交
116
        # Import the Glances server module
117
        from glances.core.glances_server import GlancesServer
N
Nicolas Hennion 已提交
118

N
Nicolas Hennion 已提交
119 120 121 122 123
        args = core.get_args()

        server = GlancesServer(cached_time=core.cached_time,
                               config=core.get_config(),
                               args=args)
A
Alessio Sergi 已提交
124
        print(_("Glances server is running on {0}:{1}").format(args.bind_address, args.port))
N
Nicolas Hennion 已提交
125 126

        # Set the server login/password (if -P/--password tag)
127
        if args.password != "":
N
Nicolas Hennion 已提交
128
            server.add_user(args.username, args.password)
N
Nicolas Hennion 已提交
129

N
Nicolas Hennion 已提交
130 131
        # Start the server loop
        server.serve_forever()
N
Nicolas Hennion 已提交
132

133
        # Shutdown the server?
N
Nicolas Hennion 已提交
134
        server.server_close()
135

136
    elif core.is_webserver():
137 138 139 140 141 142 143 144 145 146

        # Import the Glances web server module
        from glances.core.glances_webserver import GlancesWebServer

        # Init the web server mode
        webserver = GlancesWebServer(config=core.get_config(),
                                     args=core.get_args())

        # Start the web server loop
        webserver.serve_forever()