]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/ifupdown/main.py
3 # Copyright 2014-2019 Cumulus Networks, Inc. All rights reserved.
5 # Roopa Prabhu, roopa@cumulusnetworks.com
6 # Julien Fortin, julien@cumulusnetworks.com
8 # tool to configure network interfaces
18 from ifupdown2
.ifupdown
.argv
import Parse
19 from ifupdown2
.ifupdown
.config
import IFUPDOWN2_CONF_PATH
20 from ifupdown2
.ifupdown
.ifupdownmain
import ifupdownMain
22 from ifupdown2
.lib
.dry_run
import DryRunManager
24 except (ImportError, ModuleNotFoundError
):
25 from ifupdown
.argv
import Parse
26 from ifupdown
.config
import IFUPDOWN2_CONF_PATH
27 from ifupdown
.ifupdownmain
import ifupdownMain
29 from lib
.dry_run
import DryRunManager
32 log
= logging
.getLogger()
37 def __init__(self
, daemon
, uid
):
43 self
.interfaces_filename
= None
44 self
.interfaces_file_iobuf
= None
48 'down': self
.run_down
,
49 'query': self
.run_query
,
50 'reload': self
.run_reload
53 def parse_argv(self
, argv
):
54 args_parse
= Parse(argv
)
57 self
.args
= args_parse
.get_args()
58 self
.op
= args_parse
.get_op()
60 def main(self
, stdin_buffer
=None):
61 if self
.op
!= 'query' and self
.uid
!= 0:
62 raise Exception('must be root to run this command')
66 self
.init(stdin_buffer
)
67 self
.handlers
.get(self
.op
)(self
.args
)
68 except Exception as e
:
71 # if args and args.debug:
75 log
.error('main exception: ' + str(e
))
77 #traceback.print_exc()
80 # if args and not args.debug:
81 # print '\nrerun the command with \'-d\' for a detailed errormsg'
85 def init(self
, stdin_buffer
):
89 dry_run_mode_on
= DryRunManager
.get_instance().is_dry_mode_on()
91 if hasattr(self
.args
, 'noact'):
92 if self
.args
.noact
and not dry_run_mode_on
:
93 DryRunManager
.get_instance().dry_run_mode_on()
94 elif not self
.args
.noact
and dry_run_mode_on
:
95 DryRunManager
.get_instance().dry_run_mode_off()
97 # if noact is not in self.args we are probably in
98 # ifquery mode so we need to turn off dry run mode.
99 DryRunManager
.get_instance().dry_run_mode_off()
103 if hasattr(self
.args
, 'interfacesfile') and self
.args
.interfacesfile
!= None:
104 # Check to see if -i option is allowed by config file
105 # But for ifquery, we will not check this
106 if (not self
.op
== 'query' and
107 configmap_g
.get('disable_cli_interfacesfile', '0') == '1'):
108 log
.error('disable_cli_interfacesfile is set so users '
109 'not allowed to specify interfaces file on cli.')
111 if self
.args
.interfacesfile
== '-':
112 # If interfaces file is stdin, read
114 self
.interfaces_file_iobuf
= stdin_buffer
116 self
.interfaces_file_iobuf
= sys
.stdin
.read()
118 self
.interfaces_filename
= self
.args
.interfacesfile
120 # if the ifupdown2 config file does not have it, default to standard
121 self
.interfaces_filename
= configmap_g
.get('default_interfaces_configfile',
122 '/etc/network/interfaces')
124 def read_config(self
):
127 with
open(IFUPDOWN2_CONF_PATH
, 'r') as f
:
129 configStr
= '[ifupdown2]\n' + config
130 configFP
= io
.StringIO(configStr
)
131 parser
= configparser
.RawConfigParser()
132 parser
.readfp(configFP
)
133 configmap_g
= dict(parser
.items('ifupdown2'))
135 # Preprocess config map
136 configval
= configmap_g
.get('multiple_vlan_aware_bridge_support', '0')
138 # if multiple bridges not allowed, set the bridge-vlan-aware
139 # attribute in the 'no_repeats' config, so that the ifupdownmain
140 # module can catch it appropriately
141 configmap_g
['no_repeats'] = {'bridge-vlan-aware': 'yes'}
143 configval
= configmap_g
.get('link_master_slave', '0')
145 # link_master_slave is only valid when all is set
146 if hasattr(self
.args
, 'all') and not self
.args
.all
:
147 configmap_g
['link_master_slave'] = '0'
149 configval
= configmap_g
.get('delay_admin_state_change', '0')
151 # reset link_master_slave if delay_admin_state_change is on
152 configmap_g
['link_master_slave'] = '0'
154 def run_up(self
, args
):
155 log
.debug('args = %s' % str(args
))
159 if len(args
.iflist
) == 0:
161 log
.debug('creating ifupdown object ..')
162 cachearg
= (False if (iflist
or args
.nocache
or args
.noact
)
164 ifupdown_handle
= ifupdownMain(daemon
=self
.daemon
,
167 withdepends
=args
.withdepends
,
168 perfmode
=args
.perfmode
,
171 addons_enable
=not args
.noaddons
,
172 statemanager_enable
=not args
.noaddons
,
173 interfacesfile
=self
.interfaces_filename
,
174 interfacesfileiobuf
=self
.interfaces_file_iobuf
,
175 interfacesfileformat
=args
.interfacesfileformat
)
177 ifupdown_handle
.up(['up'], args
.all
, args
.CLASS
, iflist
,
178 excludepats
=args
.excludepats
,
179 printdependency
=args
.printdependency
,
180 syntaxcheck
=args
.syntaxcheck
, type=args
.type,
181 skipupperifaces
=args
.skipupperifaces
)
183 ifupdown_handle
.up(['pre-up', 'up', 'post-up'],
184 args
.all
, args
.CLASS
, iflist
,
185 excludepats
=args
.excludepats
,
186 printdependency
=args
.printdependency
,
187 syntaxcheck
=args
.syntaxcheck
, type=args
.type,
188 skipupperifaces
=args
.skipupperifaces
)
192 def run_down(self
, args
):
193 log
.debug('args = %s' % str(args
))
197 log
.debug('creating ifupdown object ..')
198 ifupdown_handle
= ifupdownMain(daemon
=self
.daemon
,
199 config
=configmap_g
, force
=args
.force
,
200 withdepends
=args
.withdepends
,
201 perfmode
=args
.perfmode
,
203 addons_enable
=not args
.noaddons
,
204 statemanager_enable
=not args
.noaddons
,
205 interfacesfile
=self
.interfaces_filename
,
206 interfacesfileiobuf
=self
.interfaces_file_iobuf
,
207 interfacesfileformat
=args
.interfacesfileformat
)
209 ifupdown_handle
.down(['pre-down', 'down', 'post-down'],
210 args
.all
, args
.CLASS
, iflist
,
211 excludepats
=args
.excludepats
,
212 printdependency
=args
.printdependency
,
213 usecurrentconfig
=args
.usecurrentconfig
,
218 def run_query(self
, args
):
219 log
.debug('args = %s' % str(args
))
224 qop
= 'query-checkcurr'
226 qop
= 'query-running'
229 elif args
.syntaxhelp
:
231 elif args
.printdependency
:
232 qop
= 'query-dependency'
233 elif args
.printsavedstate
:
234 qop
= 'query-savedstate'
237 cachearg
= (False if (iflist
or args
.nocache
or args
.syntaxhelp
or
238 (qop
!= 'query-checkcurr' and
239 qop
!= 'query-running')) else True)
240 if not iflist
and qop
== 'query-running':
241 iflist
= [i
for i
in os
.listdir('/sys/class/net/')
242 if os
.path
.isdir('/sys/class/net/%s' % i
)]
243 log
.debug('creating ifupdown object ..')
244 ifupdown_handle
= ifupdownMain(daemon
=self
.daemon
,
246 withdepends
=args
.withdepends
,
247 perfmode
=args
.perfmode
,
249 interfacesfile
=self
.interfaces_filename
,
250 interfacesfileiobuf
=self
.interfaces_file_iobuf
,
251 interfacesfileformat
=args
.interfacesfileformat
,
252 withdefaults
=args
.withdefaults
)
253 # list implies all auto interfaces (this is how ifupdown behaves)
256 ifupdown_handle
.query([qop
], args
.all
, args
.list, args
.CLASS
, iflist
,
257 excludepats
=args
.excludepats
,
258 printdependency
=args
.printdependency
,
259 format
=args
.format
, type=args
.type)
263 def run_reload(self
, args
):
264 log
.debug('args = %s' % str(args
))
267 log
.debug('creating ifupdown object ..')
268 ifupdown_handle
= ifupdownMain(daemon
=self
.daemon
,
270 interfacesfile
=self
.interfaces_filename
,
271 withdepends
=args
.withdepends
,
272 perfmode
=args
.perfmode
,
274 ifupdown_handle
.reload(['pre-up', 'up', 'post-up'],
275 ['pre-down', 'down', 'post-down'],
276 auto
=args
.all
, allow
=args
.CLASS
, ifacenames
=None,
277 excludepats
=args
.excludepats
,
278 usecurrentconfig
=args
.usecurrentconfig
,
279 syntaxcheck
=args
.syntaxcheck
,
280 currentlyup
=args
.currentlyup
)