2 # Copyright (C) 2016, 2017, 2018, 2019 Cumulus Networks, Inc. all rights reserved
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; version 2.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # https://www.gnu.org/licenses/gpl-2.0-standalone.html
21 # Julien Fortin, julien@cumulusnetworks.com
23 # ifupdown2 - Network Manager
30 from ifupdown2
.lib
.log
import LogManager
, root_logger
31 from ifupdown2
.lib
.status
import Status
33 from lib
.log
import LogManager
, root_logger
34 from lib
.status
import Status
36 # first thing first, setup the logging infra
37 LogManager
.get_instance()
40 import ifupdown2
.ifupdown
.config
as config
42 from ifupdown2
import __version__
44 config
.__version
__ = __version__
46 from ifupdown2
.lib
.exceptions
import ExitWithStatus
, ExitWithStatusAndError
48 from ifupdown2
.ifupdown
.client
import Client
49 from ifupdown2
.ifupdown
.exceptions
import ArgvParseHelp
, ArgvParseError
51 import ifupdown
.config
as config
53 config
.__version
__ = __import__("__init__").__version
__
55 from lib
.exceptions
import ExitWithStatus
, ExitWithStatusAndError
57 from ifupdown
.client
import Client
58 from ifupdown
.exceptions
import ArgvParseHelp
, ArgvParseError
62 """ Check ifupdown2 config to see if we should start the client """
64 with
open(config
.IFUPDOWN2_CONF_PATH
) as f
:
65 return "use_daemon=yes" in f
.read()
72 status
= Client(sys
.argv
).run()
73 except ExitWithStatusAndError
as e
:
74 root_logger
.error(e
.message
)
76 except ExitWithStatus
as e
:
82 if not sys
.argv
[0].endswith("query") and os
.geteuid() != 0:
83 sys
.stderr
.write('must be root to run this command\n')
86 from ifupdown2
.ifupdown
.main
import Ifupdown2
87 from ifupdown2
.lib
.nlcache
import NetlinkListenerWithCache
, NetlinkListenerWithCacheErrorNotInitialized
89 from ifupdown
.main
import Ifupdown2
90 from lib
.nlcache
import NetlinkListenerWithCache
, NetlinkListenerWithCacheErrorNotInitialized
91 ifupdown2
= Ifupdown2(daemon
=False, uid
=os
.geteuid())
93 ifupdown2
.parse_argv(sys
.argv
)
94 LogManager
.get_instance().start_standalone_logging(ifupdown2
.args
)
95 except ArgvParseError
as e
:
96 LogManager
.get_instance().root_logger().error(str(e
))
97 return Status
.Client
.STATUS_ARGV_ERROR
99 # on --help parse_args raises SystemExit, we catch it and raise a
100 # custom exception ArgvParseHelp to return 0
103 status
= ifupdown2
.main()
106 if NetlinkListenerWithCache
.is_init():
107 NetlinkListenerWithCache
.get_instance().cleanup()
108 except NetlinkListenerWithCacheErrorNotInitialized
:
109 status
= Status
.Client
.STATUS_NLERROR
110 LogManager
.get_instance().write("exit status %s" % status
)
120 except ArgvParseHelp
:
121 return Status
.Client
.STATUS_SUCCESS
122 except KeyboardInterrupt:
123 return Status
.Client
.STATUS_KEYBOARD_INTERRUPT
124 except Exception as e
:
125 root_logger
.exception("main: %s" % str(e
))
126 return Status
.Client
.STATUS_EXCEPTION_MAIN
129 if __name__
== '__main__':
132 except KeyboardInterrupt:
133 sys
.exit(Status
.Client
.STATUS_KEYBOARD_INTERRUPT
)