]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - sbin/ifupdown
2 # PYTHON_ARGCOMPLETE_OK
8 from ifupdown
.ifupdownmain
import *
12 lockfile
="/run/network/.lock"
16 logger
.debug('args = %s' %str
(args
))
20 if len(args
.iflist
) == 0:
22 logger
.debug('creating ifupdown object ..')
23 cachearg
=(False if (iflist
or args
.nocache
or
24 args
.perfmode
or args
.noact
)
26 ifupdown_handle
= ifupdownMain(force
=args
.force
,
27 withdepends
=args
.withdepends
,
28 perfmode
=args
.perfmode
,
32 addons_enable
=not args
.noaddons
,
33 statemanager_enable
=not args
.noaddons
)
36 ifupdown_handle
.up(['up'], args
.all
, args
.CLASS
, iflist
,
37 excludepats
=args
.excludepats
,
38 printdependency
=args
.printdependency
)
40 ifupdown_handle
.up(['pre-up', 'up', 'post-up'],
41 args
.all
, args
.CLASS
, iflist
,
42 excludepats
=args
.excludepats
,
43 printdependency
=args
.printdependency
)
48 logger
.debug('args = %s' %str
(args
))
52 logger
.debug('creating ifupdown object ..')
53 cachearg
=(False if (iflist
or args
.nocache
or
54 args
.perfmode
or args
.noact
)
56 ifupdown_handle
= ifupdownMain(force
=args
.force
,
57 withdepends
=args
.withdepends
,
58 perfmode
=args
.perfmode
,
62 addons_enable
=not args
.noaddons
,
63 statemanager_enable
=not args
.noaddons
)
65 ifupdown_handle
.down(['pre-down', 'down', 'post-down'],
66 args
.all
, args
.CLASS
, iflist
,
67 excludepats
=args
.excludepats
,
68 printdependency
=args
.printdependency
,
69 usecurrentconfig
=args
.usecurrentconfig
)
74 logger
.debug('args = %s' %str
(args
))
86 elif args
.printdependency
:
87 qop
= 'query-dependency'
88 elif args
.printsavedstate
:
89 qop
= 'query-savedstate'
92 cachearg
=(False if (iflist
or args
.nocache
or
93 args
.perfmode
or args
.syntaxhelp
or
94 (qop
!= 'query-checkcurr' and
95 qop
!= 'query-running')) else True)
96 if not iflist
and qop
== 'query-running':
97 iflist
= [i
for i
in os
.listdir('/sys/class/net/')
98 if os
.path
.isdir('/sys/class/net/%s' %i)]
99 logger
.debug('creating ifupdown object ..')
100 ifupdown_handle
= ifupdownMain(withdepends
=args
.withdepends
,
101 perfmode
=args
.perfmode
,
105 ifupdown_handle
.query([qop
], args
.all
, args
.CLASS
, iflist
,
106 excludepats
=args
.excludepats
,
107 printdependency
=args
.printdependency
,
112 def run_reload(args
):
113 logger
.debug('args = %s' %str
(args
))
116 logger
.debug('creating ifupdown object ..')
117 cachearg
=(False if (args
.nocache
or
118 args
.perfmode
or args
.noact
) else True)
119 ifupdown_handle
= ifupdownMain(withdepends
=args
.withdepends
,
120 perfmode
=args
.perfmode
,
123 ifupdown_handle
.reload(['pre-up', 'up', 'post-up'],
124 ['pre-down', 'down', 'post-down'],
125 args
.all
, None, None,
126 excludepats
=args
.excludepats
,
127 downchangediface
=args
.downchangediface
)
134 log_level
= logging
.WARNING
136 log_level
= logging
.INFO
138 log_level
= logging
.DEBUG
141 logging
.basicConfig(level
=log_level
,
142 format
='%(levelname)s: %(message)s')
143 logger
= logging
.getLogger('ifupdown')
151 def update_argparser(argparser
):
152 """ base parser, common to all commands """
154 argparser
.add_argument('-a', '--all', action
='store_true', required
=False,
155 help='process all interfaces marked \"auto\"')
156 argparser
.add_argument('iflist', metavar
='IFACE',
157 nargs
='*', help='interface list separated by spaces. ' +
158 'IFACE list is mutually exclusive with -a option.')
159 argparser
.add_argument('-v', '--verbose', dest
='verbose',
160 action
='store_true', help='verbose')
161 argparser
.add_argument('-d', '--debug', dest
='debug',
163 help='output debug info')
164 argparser
.add_argument('-q', '--quiet', dest
='quiet',
166 help=argparse
.SUPPRESS
)
167 argparser
.add_argument('--allow', dest
='CLASS',
168 help='ignore non-\"allow-CLASS\" interfaces')
169 argparser
.add_argument('--with-depends', dest
='withdepends',
170 action
='store_true', help='run with all dependent interfaces.'+
171 ' This option is redundant when \'-a\' is specified. With ' +
172 '\'-a\' interfaces are always executed in dependency order')
173 argparser
.add_argument('--perfmode', dest
='perfmode',
174 action
='store_true', help=argparse
.SUPPRESS
)
175 argparser
.add_argument('-j', '--jobs', dest
='jobs', type=int,
176 default
=-1, choices
=range(1,12), help=argparse
.SUPPRESS
)
177 argparser
.add_argument('--nocache', dest
='nocache', action
='store_true',
178 help=argparse
.SUPPRESS
)
179 argparser
.add_argument('-X', '--exclude', dest
='excludepats',
181 help='Exclude interfaces from the list of interfaces' +
182 ' to operate on. Can be specified multiple times.')
184 def update_ifupdown_argparser(argparser
):
185 """ common arg parser for ifup and ifdown """
186 argparser
.add_argument('-f', '--force', dest
='force',
188 help='force run all operations')
189 group
= argparser
.add_mutually_exclusive_group(required
=False)
190 group
.add_argument('-n', '--no-act', dest
='noact',
191 action
='store_true', help='print out what would happen,' +
193 group
.add_argument('--print-dependency',
194 dest
='printdependency', choices
=['list', 'dot'],
195 help='print iface dependency')
196 group
.add_argument('--no-scripts', '--no-addons',
197 dest
='noaddons', action
='store_true',
198 help='dont run any addon modules or scripts. Runs only link ' +
201 def update_ifup_argparser(argparser
):
202 update_ifupdown_argparser(argparser
)
204 def update_ifdown_argparser(argparser
):
205 update_ifupdown_argparser(argparser
)
206 argparser
.add_argument('--use-current-config',
207 dest
='usecurrentconfig', action
='store_true',
208 help=argparse
.SUPPRESS
)
209 #help='By default ifdown looks at the saved state for ' +
210 #'interfaces to bring down. This option allows ifdown to ' +
211 #'look at the current interfaces file. Useful when your ' +
212 #'state file is corrupted or you want down to use the latest '
213 #'from the interfaces file')
215 def update_ifquery_argparser(argparser
):
216 """ arg parser for ifquery options """
218 # -l is same as '-a', only here for backward compatibility
219 argparser
.add_argument('-l', '--list', action
='store_true', dest
='all',
220 help=argparse
.SUPPRESS
)
221 group
= argparser
.add_mutually_exclusive_group(required
=False)
222 group
.add_argument('-r', '--running', dest
='running',
224 help='query running state of an interface')
225 group
.add_argument('-c', '--check', dest
='checkcurr',
227 help='check interface file contents against ' +
228 'running state of an interface')
229 group
.add_argument('--raw', action
='store_true', dest
='raw',
230 help='print raw config file entries')
231 group
.add_argument('--print-savedstate', action
='store_true',
232 dest
='printsavedstate',
233 help=argparse
.SUPPRESS
)
234 argparser
.add_argument('--format', dest
='format', default
='native',
235 choices
=['native', 'json'], help=argparse
.SUPPRESS
)
236 argparser
.add_argument('--print-dependency',
237 dest
='printdependency', choices
=['list', 'dot'],
238 help='print interface dependency')
239 argparser
.add_argument('--syntax-help', action
='store_true',
241 help='print supported interface config syntax')
243 def update_ifreload_argparser(argparser
):
244 """ parser for ifreload """
245 argparser
.add_argument('-a', '--all', action
='store_true', required
=True,
246 help='process all interfaces marked \"auto\"')
247 argparser
.add_argument('iflist', metavar
='IFACE',
248 nargs
='*', help=argparse
.SUPPRESS
)
249 argparser
.add_argument('-n', '--no-act', dest
='noact',
250 action
='store_true', help=argparse
.SUPPRESS
)
251 argparser
.add_argument('-v', '--verbose', dest
='verbose',
252 action
='store_true', help='verbose')
253 argparser
.add_argument('-d', '--debug', dest
='debug',
255 help='output debug info')
256 argparser
.add_argument('--with-depends', dest
='withdepends',
257 action
='store_true', help=argparse
.SUPPRESS
)
258 argparser
.add_argument('--perfmode', dest
='perfmode',
259 action
='store_true', help=argparse
.SUPPRESS
)
260 argparser
.add_argument('--nocache', dest
='nocache', action
='store_true',
261 help=argparse
.SUPPRESS
)
262 argparser
.add_argument('-X', '--exclude', dest
='excludepats',
264 help=argparse
.SUPPRESS
)
265 argparser
.add_argument('-j', '--jobs', dest
='jobs', type=int,
266 default
=-1, choices
=range(1,12), help=argparse
.SUPPRESS
)
267 argparser
.add_argument('--down-changediface', dest
='downchangediface',
268 action
='store_true', help='down interfaces whose ' +
269 'config have changed before bringing them up. By' +
270 ' default all interfaces are brought up')
272 def parse_args(argsv
, op
):
274 descr
= 'query interfaces (all or interface list)'
276 descr
= 'interface management'
277 argparser
= argparse
.ArgumentParser(description
=descr
)
279 update_ifreload_argparser(argparser
)
281 update_argparser(argparser
)
283 update_ifup_argparser(argparser
)
285 update_ifdown_argparser(argparser
)
287 update_ifquery_argparser(argparser
)
289 update_ifreload_argparser(argparser
)
291 argcomplete
.autocomplete(argparser
)
293 return argparser
.parse_args(argsv
)
295 handlers
= {'up' : run_up
,
298 'reload' : run_reload
}
301 """ main function """
305 if argv
[0].endswith('ifup'):
307 elif argv
[0].endswith('ifdown'):
309 elif argv
[0].endswith('ifquery'):
311 elif argv
[0].endswith('ifreload'):
314 print ('Unexpected executable.' +
315 ' Should be \'ifup\' or \'ifdown\' or \'ifquery\'')
317 # Command line arg parser
318 args
= parse_args(argv
[1:], op
)
319 if not args
.iflist
and not args
.all
:
320 if op
!= 'query' or not args
.syntaxhelp
:
321 print '\'-a\' option or interface list are required'
324 if args
.iflist
and args
.all
:
325 print '\'-a\' option and interface list are mutually exclusive'
328 handlers
.get(op
)(args
)
332 if args
and args
.debug
:
339 if args
and not args
.debug
:
340 print '\nRerun the command with \'-d\' for a detailed errormsg'
345 if __name__
== "__main__":
347 if not os
.geteuid() == 0:
348 print 'Error: Must be root to run this command'
352 XXX: Cannot use this. A spawned dhclient process can hold the lock
353 if not utilities.lockFile(lockfile):
354 print 'Another instance of this program is already running.'