def post(self, id): inst = self.get_instance(id, isowner=True) if not inst: return form = NetworkForm( self.request.arguments ) self.update_form( form ) if form.validate(): network = { 'type': form.type.data, 'mac': inst.mac, # TODO: use old mac 'ip': form.ip.data, 'netmask': form.netmask.data, 'gateway': form.gateway.data } if inst.config: config = json.loads(inst.config) else: config = {} config['network'] = [network] inst.config = json.dumps(config) #print 'config = ', inst.config self.db2.commit() # Update IpAssign table ipassign = self.db2.query(IpAssign).filter_by( ip = network['ip'] ).first() if ipassign: ipassign.user_id = self.current_user.id ipassign.instance_id = id updated = datetime.utcnow() else: c = IpAssign( ip = network['ip'], user = self.current_user, instance = inst ) self.db2.add( c ) if inst.is_running: inst.ischanged = True self.db2.commit() url = self.reverse_url('instance:view', id) url += '?view=network' return self.redirect( url ) # Get error d = { 'title': _('Edit Network'), 'instance': inst, 'form': form } self.render('instance/network_edit.html', **d)
def post(self, id): inst = self.get_instance(id, isowner=True) if not inst: return form = NetworkForm(self.request.arguments) self.update_form(form) if form.validate(): network = { "type": form.type.data, "mac": inst.mac, # TODO: use old mac "ip": form.ip.data, "netmask": form.netmask.data, "gateway": form.gateway.data, } if inst.config: config = json.loads(inst.config) else: config = {} config["network"] = [network] inst.config = json.dumps(config) # print 'config = ', inst.config self.db2.commit() # Update IpAssign table ipassign = self.db2.query(IpAssign).filter_by(ip=network["ip"]).first() if ipassign: ipassign.user_id = self.current_user.id ipassign.instance_id = id updated = datetime.utcnow() else: c = IpAssign(ip=network["ip"], user=self.current_user, instance=inst) self.db2.add(c) if inst.is_running: inst.ischanged = True self.db2.commit() url = self.reverse_url("instance:view", id) url += "?view=network" return self.redirect(url) # Get error d = {"title": _("Edit Network"), "instance": inst, "form": form} self.render("instance/network_edit.html", **d)
def get(self, id): # if not inst.config.network, just add inst = self.get_instance(id, isowner=True) if not inst: return form = NetworkForm() self.update_form( form ) if inst.config: config = json.loads(inst.config) if 'network' in config.keys(): network = config['network'] if len(network) > 0: network = network[0] form.type.data = network['type'] form.ip.data = network['ip'] form.netmask.data = network['netmask'] form.gateway.data = network['gateway'] d = { 'title': _('Edit Network'), 'instance': inst, 'form': form } self.render('instance/network_edit.html', **d)
def post_network(self, I): nic_total = I.get_network_count() ERROR = [] index = self.get_argument_int('index', 1) if index == 1: form = NetworkForm(self) if form.validate(): old_network = I.get_network() if form.type.data == old_network.get('type'): # TODO: same configure url = self.reverse_url('myun:instance:view', I.id) url += '?tab=network' return self.redirect( url ) nic_type = form.type.data if nic_type == 'default': nic_ip, nic_netmask, nic_gateway = '', '', '' old_ip = old_network.get('ip', None) if old_ip: IPL = self.db2.query(IPPool).filter_by( ip = old_ip).all() for x in IPL: # TODO: does this correct ? if I.id == x.instance_id: x.instance_id = None # unbinding x.updated = datetime.now() self.lytrace_ippool(x, I, release=True) else: logging.error("Release %s from instance %s failed, this is not it's ip" % (x.ip, I.id)) elif nic_type == 'networkpool': ok_ip = self.db2.query(IPPool).filter_by( instance_id = None ).order_by( asc(IPPool.id)).first() if ok_ip: nic_ip = ok_ip.ip nic_netmask = ok_ip.network.netmask nic_gateway = ok_ip.network.gateway ok_ip.instance_id = I.id # binding ok_ip.updated = datetime.now() self.lytrace_ippool(ok_ip, I) else: ERROR.append( self.trans(_('Can not find a useable ip from system ip pool.')) ) else: ERROR.append( self.trans(_('No such network type.')) ) else: if self.has_permission('network.add'): index = index if index <= nic_total + 1 else nic_total+1 # Static network configure form = StaticNetworkForm(self) if form.validate(): nic_type = 'static' nic_ip = form.ip.data nic_netmask = form.netmask.data nic_gateway = form.gateway.data else: return self.write( self.trans(_('You have not allowed to add another network !')) ) self.d['INDEX'] = index self.d['form'] = form self.d['ERROR'] = ERROR if self.d['ERROR']: return self.render( 'myun/instance/edit_network.html', **self.d) nic_config = { 'type': nic_type, 'mac': I.mac, # TODO: use old mac 'ip': nic_ip, 'netmask': nic_netmask, 'gateway': nic_gateway } # A temp hack for second network if index > 1: if index == 2: MAC_TEMP = '52:54:00:26:%02x:%02x' else: MAC_TEMP = '52:54:00:8%s' % (index % 10) + ':%02x:%02x' nic_config['mac'] = MAC_TEMP % (I.id / 256, I.id % 256) try: I.set_network( nic_config, index ) self.db2.commit() url = self.reverse_url('myun:instance:view', I.id) url += '?tab=network' return self.redirect( url ) except Exception, e: self.d['ERROR'].append( self.trans(_('save failed: %s')) % e ) self.render( 'myun/instance/edit_network.html', **self.d)