__init__.py 3.8 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 24
# Import system lib
import sys
25
import signal
26

N
Nicolas Hennion 已提交
27 28
# Import Glances libs
# Note: others Glances libs will be imported optionnaly
29
from glances.core.glances_main import GlancesMain
N
Nicolas Hennion 已提交
30

A
Alessio Sergi 已提交
31

32 33 34 35 36 37 38 39 40
def __signal_handler(signal, frame):
    """
    Call back for CTRL-C
    """
    end()


def end():
    """
N
Pep8  
Nicolas Hennion 已提交
41
    Stop Glances
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    """

    if (core.is_standalone()):
        # Stop the standalone (CLI)
        standalone.end()
    elif (core.is_client()):
        # Stop the client
        client.end()
    elif (core.is_server()):
        # Stop the server
        server.end()

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


N
Nicolas Hennion 已提交
58
def main():
59 60 61 62 63 64 65 66 67
    """
    Main entry point for Glances
    Select the mode (standalone, client or server)
    Run it...
    """

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

68 69
    # Create the Glances main instance
    core = GlancesMain()
N
Nicolas Hennion 已提交
70

71 72 73
    # Catch the CTRL-C signal
    signal.signal(signal.SIGINT, __signal_handler)

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

77
        # Import the Glances standalone module
78
        from glances.core.glances_standalone import GlancesStandalone
79 80

        # Init the standalone mode
N
Nicolas Hennion 已提交
81
        standalone = GlancesStandalone(config=core.get_config(),
N
Nicolas Hennion 已提交
82
                                       args=core.get_args())
83 84 85 86

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

N
Nicolas Hennion 已提交
87
    elif (core.is_client()):
N
Nicolas Hennion 已提交
88 89

        # Import the Glances client module
90
        from glances.core.glances_client import GlancesClient
N
Nicolas Hennion 已提交
91 92

        # Init the client
N
Nicolas Hennion 已提交
93 94
        client = GlancesClient(config=core.get_config(),
                               args=core.get_args())
N
Nicolas Hennion 已提交
95 96 97

        # Test if client and server are in the same major version
        if (not client.login()):
98
            print(_("Error: The server version is not compatible with the client"))
N
Nicolas Hennion 已提交
99 100 101 102 103 104
            sys.exit(2)

        # Start the client loop
        client.serve_forever()

        # Shutdown the client
105
        client.close()
N
Nicolas Hennion 已提交
106

N
Nicolas Hennion 已提交
107
    elif (core.is_server()):
N
Nicolas Hennion 已提交
108

N
Nicolas Hennion 已提交
109
        # Import the Glances server module
110
        from glances.core.glances_server import GlancesServer
N
Nicolas Hennion 已提交
111

N
Nicolas Hennion 已提交
112 113 114 115 116 117
        args = core.get_args()

        server = GlancesServer(cached_time=core.cached_time,
                               config=core.get_config(),
                               args=args)
        print("{} {}:{}".format(_("Glances server is running on"), args.bind, args.port))
N
Nicolas Hennion 已提交
118 119

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

N
Nicolas Hennion 已提交
123 124
        # Start the server loop
        server.serve_forever()
N
Nicolas Hennion 已提交
125

126
        # Shutdown the server?
N
Nicolas Hennion 已提交
127
        server.server_close()
128 129 130 131 132 133 134 135 136 137 138 139

    elif (core.is_webserver()):

        # 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()