]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/__main__.py
addons: bridge-vlan-vni-map: add vlan reserved check
[mirror_ifupdown2.git] / ifupdown2 / __main__.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2016, 2017, 2018, 2019 Cumulus Networks, Inc. all rights reserved
3 #
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.
7 #
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.
12 #
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
16 # 02110-1301, USA.
17 #
18 # https://www.gnu.org/licenses/gpl-2.0-standalone.html
19 #
20 # Author:
21 # Julien Fortin, julien@cumulusnetworks.com
22 #
23 # ifupdown2 - Network Manager
24 #
25
26 import os
27 import sys
28
29 try:
30 from ifupdown2.lib.log import LogManager, root_logger
31 from ifupdown2.lib.status import Status
32 except Exception:
33 from lib.log import LogManager, root_logger
34 from lib.status import Status
35
36 # first thing first, setup the logging infra
37 LogManager.get_instance()
38
39 try:
40 import ifupdown2.ifupdown.config as config
41
42 from ifupdown2 import __version__
43
44 config.__version__ = __version__
45
46 from ifupdown2.lib.exceptions import ExitWithStatus, ExitWithStatusAndError
47
48 from ifupdown2.ifupdown.client import Client
49 from ifupdown2.ifupdown.exceptions import ArgvParseHelp, ArgvParseError
50 except Exception:
51 import ifupdown.config as config
52
53 config.__version__ = __import__("__init__").__version__
54
55 from lib.exceptions import ExitWithStatus, ExitWithStatusAndError
56
57 from ifupdown.client import Client
58 from ifupdown.exceptions import ArgvParseHelp, ArgvParseError
59
60
61 def daemon_mode():
62 """ Check ifupdown2 config to see if we should start the client """
63 try:
64 with open(config.IFUPDOWN2_CONF_PATH) as f:
65 return "use_daemon=yes" in f.read()
66 except Exception:
67 return False
68
69
70 def client():
71 try:
72 status = Client(sys.argv).run()
73 except ExitWithStatusAndError as e:
74 root_logger.error(e.message)
75 status = e.status
76 except ExitWithStatus as e:
77 status = e.status
78 return status
79
80
81 def stand_alone():
82 if not sys.argv[0].endswith("query") and os.geteuid() != 0:
83 sys.stderr.write('must be root to run this command\n')
84 return 1
85 try:
86 from ifupdown2.ifupdown.main import Ifupdown2
87 from ifupdown2.lib.nlcache import NetlinkListenerWithCache, NetlinkListenerWithCacheErrorNotInitialized
88 except Exception:
89 from ifupdown.main import Ifupdown2
90 from lib.nlcache import NetlinkListenerWithCache, NetlinkListenerWithCacheErrorNotInitialized
91 ifupdown2 = Ifupdown2(daemon=False, uid=os.geteuid())
92 try:
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
98 except ArgvParseHelp:
99 # on --help parse_args raises SystemExit, we catch it and raise a
100 # custom exception ArgvParseHelp to return 0
101 return 0
102 try:
103 status = ifupdown2.main()
104 finally:
105 try:
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)
111 return status
112
113
114 def main():
115 try:
116 if daemon_mode():
117 return client()
118 else:
119 return stand_alone()
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
127
128
129 if __name__ == '__main__':
130 try:
131 sys.exit(main())
132 except KeyboardInterrupt:
133 sys.exit(Status.Client.STATUS_KEYBOARD_INTERRUPT)