1 # -*- coding: utf-8 -*-
3 # This file is part of Karesansui.
5 # Copyright (C) 2009-2010 HDE, Inc.
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.
16 import simplejson as json
19 from karesansui.lib.rest import Rest, auth
20 from karesansui.db.access.machine import findbyhost1
22 from karesansui.lib.checker import Checker, \
23 CHECK_EMPTY, CHECK_VALID, CHECK_LENGTH, \
24 CHECK_CHAR, CHECK_MIN, CHECK_MAX, CHECK_ONLYSPACE, \
27 from karesansui.lib.utils import is_param, is_empty, preprint_r, \
28 base64_encode, get_ifconfig_info
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
34 def validates_staticroute(obj):
41 if not is_param(obj.input, 'target'):
43 checker.add_error(_('Specify target address for the route.'))
45 check = checker.check_ipaddr(
48 CHECK_EMPTY | CHECK_VALID,
51 if not is_param(obj.input, 'gateway'):
53 checker.add_error(_('Specify gateway address for the route.'))
55 check = checker.check_ipaddr(
61 obj.view.alert = checker.errors
64 class HostBy1StaticRoute(Rest):
67 def _GET(self, *param, **params):
68 host_id = self.chk_hostby1(param)
69 if host_id is None: return web.notfound()
71 host = findbyhost1(self.orm, host_id)
73 self.view.host_id = host_id
77 "device": ["^peth","^virbr","^sit","^xenbr","^lo"],
78 "ipaddr": ["^0\.0\.0\.0$", "^169\.254\.0\.0$"],
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):
86 if dev_info['ipaddr'] is not None:
88 net = NetworkAddress("%s/%s" % (dev_info['ipaddr'],dev_info['mask'],))
89 excludes['ipaddr'].append(net.network)
93 self.view.devices = devices
96 status = parser.do_status()
98 for _k,_v in status.iteritems():
99 for _k2,_v2 in _v.iteritems():
100 name = base64_encode("%s@%s" % (_k2,_k,))
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
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])
120 routes[name]['removable'] = removable
122 self.view.routes = routes
124 if self.is_mode_input():
130 def _POST(self, *param, **params):
131 host_id = self.chk_hostby1(param)
132 if host_id is None: return web.notfound()
134 host = findbyhost1(self.orm, host_id)
136 if not validates_staticroute(self):
137 return web.badrequest(self.view.alert)
139 modules = ["staticroute"]
141 dop = read_conf(modules, self, host)
143 return web.internalerror('Internal Server Error. (Timeout)')
145 target = self.input.target
146 net = NetworkAddress(target)
148 netmask = net.netmask
150 network = net.network
151 target = "%s/%s" % (ipaddr,netlen,)
152 gateway = self.input.gateway
153 device = self.input.device
155 dop.set("staticroute", [device,target], gateway)
157 from karesansui.lib.parser.staticroute import PARSER_COMMAND_ROUTE
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,)
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}
165 retval = write_conf(dop, self, host, extra_args=extra_args)
167 return web.internalerror('Internal Server Error. (Adding Task)')
169 return web.accepted(url=web.ctx.path)
172 '/host/(\d+)/staticroute[/]?(\.html|\.part|\.json)?$', HostBy1StaticRoute,