OSDN Git Service

Remove pdb
[karesansui/karesansui.git] / karesansui / gadget / hostby1staticroute.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Karesansui.
4 #
5 # Copyright (C) 2009-2010 HDE, Inc.
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12
13 import re
14
15 import web
16 import simplejson as json
17
18 import karesansui
19 from karesansui.lib.rest import Rest, auth
20 from karesansui.db.access.machine import findbyhost1
21
22 from karesansui.lib.checker import Checker, \
23     CHECK_EMPTY, CHECK_VALID, CHECK_LENGTH, \
24     CHECK_CHAR, CHECK_MIN, CHECK_MAX, CHECK_ONLYSPACE, \
25     CHECK_UNIQUE
26
27 from karesansui.lib.utils import is_param, is_empty, preprint_r, \
28     base64_encode, get_ifconfig_info
29
30 from karesansui.lib.networkaddress import NetworkAddress
31 from karesansui.lib.parser.staticroute import staticrouteParser as Parser
32 from karesansui.lib.conf import read_conf, write_conf
33
34 def validates_staticroute(obj):
35     checker = Checker()
36     check = True
37
38     _ = obj._
39     checker.errors = []
40
41     if not is_param(obj.input, 'target'):
42         check = False
43         checker.add_error(_('Specify target address for the route.'))
44     else:
45         check = checker.check_ipaddr(
46                 _('Target'), 
47                 obj.input.target,
48                 CHECK_EMPTY | CHECK_VALID,
49                 ) and check
50
51     if not is_param(obj.input, 'gateway'):
52         check = False
53         checker.add_error(_('Specify gateway address for the route.'))
54     else:
55         check = checker.check_ipaddr(
56                 _('Gateway'), 
57                 obj.input.gateway,
58                 CHECK_VALID,
59                 ) and check
60
61     obj.view.alert = checker.errors
62     return check
63
64 class HostBy1StaticRoute(Rest):
65
66     @auth
67     def _GET(self, *param, **params):
68         host_id = self.chk_hostby1(param)
69         if host_id is None: return web.notfound()
70
71         host = findbyhost1(self.orm, host_id)
72
73         self.view.host_id = host_id
74
75         # unremovable entries
76         excludes = {
77                    "device": ["^peth","^virbr","^sit","^xenbr","^lo"],
78                    "ipaddr": ["^0\.0\.0\.0$", "^169\.254\.0\.0$"],
79                    }
80
81         devices = []
82         phydev_regex = re.compile(r"^eth[0-9]+")
83         for dev,dev_info in get_ifconfig_info().iteritems():
84             if phydev_regex.match(dev):
85                 try:
86                     if dev_info['ipaddr'] is not None:
87                         devices.append(dev)
88                         net = NetworkAddress("%s/%s" % (dev_info['ipaddr'],dev_info['mask'],))
89                         excludes['ipaddr'].append(net.network)
90                 except:
91                     pass
92
93         self.view.devices = devices
94
95         parser = Parser()
96         status = parser.do_status()
97         routes = {}
98         for _k,_v in status.iteritems():
99             for _k2,_v2 in _v.iteritems():
100                 name = base64_encode("%s@%s" % (_k2,_k,))
101                 routes[name] = {}
102                 routes[name]['name']    = name
103                 routes[name]['device']  = _k
104                 routes[name]['gateway'] = _v2['gateway']
105                 routes[name]['flags']   = _v2['flags']
106                 routes[name]['ref']     = _v2['ref']
107                 routes[name]['use']     = _v2['use']
108                 net = NetworkAddress(_k2)
109                 routes[name]['ipaddr']  = net.ipaddr
110                 routes[name]['netlen']  = net.netlen
111                 routes[name]['netmask'] = net.netmask
112
113                 removable = True
114                 for _ex_key,_ex_val in excludes.iteritems():
115                     ex_regex = "|".join(_ex_val)
116                     mm = re.search(ex_regex,routes[name][_ex_key])
117                     if mm:
118                         removable = False
119
120                 routes[name]['removable'] = removable
121
122         self.view.routes = routes
123
124         if self.is_mode_input():
125             pass
126
127         return True
128
129     @auth
130     def _POST(self, *param, **params):
131         host_id = self.chk_hostby1(param)
132         if host_id is None: return web.notfound()
133
134         host = findbyhost1(self.orm, host_id)
135
136         if not validates_staticroute(self):
137             return web.badrequest(self.view.alert)
138
139         modules = ["staticroute"]
140
141         dop = read_conf(modules, self, host)
142         if dop is False:
143             return web.internalerror('Internal Server Error. (Timeout)')
144
145         target  = self.input.target
146         net = NetworkAddress(target)
147         ipaddr  = net.ipaddr
148         netmask = net.netmask
149         netlen  = net.netlen
150         network = net.network
151         target = "%s/%s" % (ipaddr,netlen,)
152         gateway = self.input.gateway
153         device  = self.input.device
154
155         dop.set("staticroute", [device,target], gateway)
156
157         from karesansui.lib.parser.staticroute import PARSER_COMMAND_ROUTE
158         if net.netlen == 32:
159             command = "%s add -host %s gw %s dev %s" % (PARSER_COMMAND_ROUTE,ipaddr,gateway,device,)
160             command = "%s add -host %s dev %s" % (PARSER_COMMAND_ROUTE,ipaddr,device,)
161         else:
162             command = "%s add -net %s netmask %s gw %s dev %s" % (PARSER_COMMAND_ROUTE,network,netmask,gateway,device,)
163         extra_args = {"post-command": command}
164
165         retval = write_conf(dop, self, host, extra_args=extra_args)
166         if retval is False:
167             return web.internalerror('Internal Server Error. (Adding Task)')
168
169         return web.accepted(url=web.ctx.path)
170
171 urls = (
172     '/host/(\d+)/staticroute[/]?(\.html|\.part|\.json)?$', HostBy1StaticRoute,
173     )
174