]> git.proxmox.com Git - mirror_ifupdown2.git/blob - sbin/ifupdown
A whole lot of fixes and some new code (needs some cleanup which will be
[mirror_ifupdown2.git] / sbin / ifupdown
1 #!/usr/bin/python
2
3 import sys
4 import os
5 import re
6 import argparse
7 from ifupdown.ifupdownmain import *
8
9 import logging
10
11 lockfile="/run/network/.lock"
12 logger = None
13
14 def run(args, op):
15 logger.debug('args = %s' %str(args))
16
17 try:
18 iflist = args.iflist
19 if len(args.iflist) == 0:
20 iflist = None
21
22 cachearg=False if iflist is not None or args.nocache == True else True
23 logger.debug('creating ifupdown object ..')
24 if op == 'up' or op == 'down' or op == 'reload':
25 ifupdown_handle = ifupdownMain(force=args.force,
26 nodepends=args.nodepends,
27 perfmode=args.perfmode,
28 njobs=args.jobs,
29 dryrun=args.noact,
30 cache=cachearg)
31 elif op == 'query':
32 ifupdown_handle = ifupdownMain(nodepends=args.nodepends,
33 perfmode=args.perfmode,
34 njobs=args.jobs,
35 format=args.format,
36 cache=cachearg)
37
38 logger.debug('calling %s' %op + ' for all interfaces ..')
39 if op == 'up':
40 ifupdown_handle.up(args.all, args.CLASS, iflist,
41 excludepats=args.excludepats,
42 printdependency=args.printdependency)
43 elif op == 'down':
44 ifupdown_handle.down(args.all, args.CLASS, iflist,
45 excludepats=args.excludepats)
46 elif op == 'query':
47 if args.checkcurstate == True:
48 qtype='query-checkcurr'
49 elif args.presumedstate == True:
50 qtype='query-presumed'
51 elif args.presumedstatedetailed == True:
52 qtype='query-presumeddetailed'
53 elif args.runningstate == True:
54 if iflist is None:
55 iflist = [i for i in os.listdir('/sys/class/net/')
56 if os.path.isdir('/sys/class/net/%s' %i) == True]
57 print iflist
58 qtype='query-running'
59 else:
60 qtype='query'
61
62 ifupdown_handle.query(qtype, args.all, args.CLASS, iflist,
63 excludepats=args.excludepats)
64 elif op == 'reload':
65 if iflist is not None:
66 raise Exception('iflist is currently not supported with reload')
67
68 ifupdown_handle.reload(args.all, args.CLASS, iflist,
69 excludepats=args.excludepats)
70 except:
71 raise
72
73 def init(args):
74 global logger
75
76 log_level = logging.WARNING
77
78 if args.verbose == True:
79 log_level = logging.INFO
80
81 if args.debug == True:
82 log_level = logging.DEBUG
83
84 try:
85 logging.basicConfig(level=log_level,
86 format='%(levelname)s: %(message)s')
87 logger = logging.getLogger('ifupdown')
88 except:
89 raise
90
91
92 def deinit():
93 {}
94
95 def update_argparser(argparser):
96 """ base parser """
97 argparser.add_argument('iflist', metavar='IFACE',
98 nargs='*', help='interfaces list')
99 argparser.add_argument('-v', '--verbose', dest='verbose',
100 action='store_true', help='verbose')
101 argparser.add_argument('-d', '--debug', dest='debug',
102 action='store_true',
103 help='output debug info')
104 argparser.add_argument('-q', '--quiet', dest='quiet',
105 action='store_true',
106 help=argparse.SUPPRESS)
107 argparser.add_argument('--allow', dest='CLASS',
108 help='ignore non-\"allow-CLASS\" interfaces')
109 argparser.add_argument('--nodepends', dest='nodepends',
110 action='store_true', help=argparse.SUPPRESS)
111 argparser.add_argument('--perfmode', dest='perfmode',
112 action='store_true', help=argparse.SUPPRESS)
113 argparser.add_argument('-j', '--jobs', dest='jobs', type=int,
114 default=-1, choices=range(1,12), help=argparse.SUPPRESS)
115 argparser.add_argument('--nocache', dest='nocache', action='store_true',
116 help=argparse.SUPPRESS)
117 argparser.add_argument('--print-dependency',
118 dest='printdependency', choices=['list', 'dot'],
119 help=argparse.SUPPRESS)
120 argparser.add_argument('-X', '--exclude', dest='excludepats',
121 action='append', help='exclude interfaces from the list of '
122 + 'interfaces to operate on by a PATTERN '
123 + '(note that this option doesn\'t disable mappings)')
124
125
126 def update_ifupdown_argparser(argparser):
127 argparser.add_argument('-a', '--all', action='store_true',
128 help='process all interfaces marked \"auto\"')
129 argparser.add_argument('-f', '--force', dest='force',
130 action='store_true',
131 help='force run all operations')
132 argparser.add_argument('-n', '--no-act', dest='noact',
133 action='store_true', help='print out what would happen,' +
134 'but don\'t do it')
135
136 def update_ifup_argparser(argparser):
137 update_ifupdown_argparser(argparser)
138
139 def update_ifdown_argparser(argparser):
140 update_ifupdown_argparser(argparser)
141
142 def update_ifquery_argparser(argparser):
143 argparser.add_argument('-l', '--list', action='store_true', dest='all',
144 help='process all interfaces marked \"auto\"')
145 group = argparser.add_mutually_exclusive_group(required=False)
146 group.add_argument('-r', '--running-state', dest='runningstate',
147 action='store_true',
148 help='query running state of an interface')
149
150 group.add_argument('-c', '--check-state', dest='checkcurstate',
151 action='store_true',
152 help='check running state of an interface')
153
154 # presumed-state is state maintained by ifupdown
155 group.add_argument('--presumed-state', dest='presumedstate',
156 action='store_true', help=argparse.SUPPRESS)
157
158 # presumed-state-detailed prints all details about the object stored
159 # by ifupdown
160 group.add_argument('--presumed-state-detailed',
161 dest='presumedstatedetailed',
162 action='store_true', help=argparse.SUPPRESS)
163
164 group.add_argument('--format', dest='format', default='nwifaces',
165 choices=['nwifaces', 'json'], help=argparse.SUPPRESS)
166
167 def update_ifreload_argparser(argparser):
168 update_ifupdown_argparser(argparser)
169
170 def parse_args(argsv, op):
171 descr = 'interface management'
172
173 argparser = argparse.ArgumentParser(description=descr)
174 update_argparser(argparser)
175 if op == 'up':
176 update_ifup_argparser(argparser)
177 elif op == 'down':
178 update_ifdown_argparser(argparser)
179 elif op == 'query':
180 update_ifquery_argparser(argparser)
181 elif op == 'reload':
182 update_ifreload_argparser(argparser)
183
184 return argparser.parse_args(argsv)
185
186 def main(argv):
187 """ main function """
188 try:
189 op = None
190 if re.search(r'ifup', argv[0]) != None:
191 op = 'up'
192 elif re.search(r'ifdown', argv[0]) != None:
193 op = 'down'
194 elif re.search(r'ifquery', argv[0]) != None:
195 op = 'query'
196 elif re.search(r'ifreload', argv[0]) != None:
197 op = 'reload'
198 else:
199 print ('Unexpected executable.' +
200 ' Should be \'ifup\' or \'ifdown\' or \'ifquery\'')
201 exit(1)
202
203 # Command line arg parser
204 args = parse_args(argv[1:], op)
205 if len(args.iflist) > 0 and args.all is True:
206 print 'interface list cannot be specified with all option'
207 exit(1)
208
209 init(args)
210 run(args, op)
211 except Exception, e:
212 if str(e) == '':
213 exit(1)
214
215 if args.debug == True:
216 raise
217 else:
218 logger.error(str(e))
219 print '\nRerun the command with \'-d\' for a detailed errormsg'
220 exit(1)
221 finally:
222 deinit()
223
224 if __name__ == "__main__":
225
226 if not os.geteuid() == 0:
227 print 'Error: Must be root to run this command'
228 exit(1)
229
230 """
231 XXX: Cannot use this. A spawned dhclient process can hold the lock
232 if not utilities.lockFile(lockfile):
233 print 'Another instance of this program is already running.'
234 exit(0)
235 """
236
237 main(sys.argv)