def view_interfaces(request): return_dict = {} try: template = 'logged_in_error.html' nics, err = networking.get_interfaces() if err: raise Exception(err) bonds, err = networking.get_bonding_info_all() if err: raise Exception(err) if "action" in request.GET: if request.GET["action"] == "saved": conf = "Network interface information successfully updated" if request.GET["action"] == "removed_bond": conf = "Network bond successfully removed" if request.GET["action"] == "state_down": conf = "Network interface successfully disabled. The state change may take a couple of seconds to reflect on this page so please refresh it to check the updated status." if request.GET["action"] == "state_up": conf = "Network interface successfully enabled. The state change may take a couple of seconds to reflect on this page so please refresh it to check the updated status." if request.GET["action"] == "created_bond": conf = "Network bond successfully created. Please edit the address information for the bond in order to use it." return_dict["conf"] = conf return_dict["nics"] = nics return_dict["bonds"] = bonds template = "view_interfaces.html" return django.shortcuts.render_to_response(template, return_dict, context_instance = django.template.context.RequestContext(request)) except Exception, e: return_dict['base_template'] = "networking_base.html" return_dict["page_title"] = 'View network interfaces' return_dict['tab'] = 'view_interfaces_tab' return_dict["error"] = 'Error loading interfaces' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def create_bond(request): return_dict = {} try: interfaces, err = networking.get_interfaces() if err: raise Exception(err) if not interfaces: raise Exception("Error loading network interface information : No interfaces found") bonds, err = networking.get_bonding_info_all() if err: raise Exception(err) return_dict['interfaces'] = interfaces if_list = [] existing_bonds = [] for if_name, iface in interfaces.items(): if 'bonding_master' in iface and iface['bonding_master']: existing_bonds.append(if_name) continue if 'slave_to' in iface and iface['slave_to']: continue if 'eth' not in if_name: continue if_list.append(if_name) if request.method == "GET": form = networking_forms.CreateBondForm(interfaces = if_list, existing_bonds = existing_bonds) return_dict['form'] = form return django.shortcuts.render_to_response("create_bond.html", return_dict, context_instance = django.template.context.RequestContext(request)) else: form = networking_forms.CreateBondForm(request.POST, interfaces = if_list, existing_bonds = existing_bonds) return_dict['form'] = form if not form.is_valid(): return django.shortcuts.render_to_response("create_bond.html", return_dict, context_instance = django.template.context.RequestContext(request)) cd = form.cleaned_data result, err = networking.create_bond(cd['name'], cd['slaves'], int(cd['mode'])) if not err: raise Exception(err) audit_str = "Created a network bond named %s with slaves %s"%(cd['name'], ','.join(cd['slaves'])) audit.audit("create_bond", audit_str, request.META["REMOTE_ADDR"]) return django.http.HttpResponseRedirect('/view_interfaces?action=created_bond') except Exception, e: return_dict['base_template'] = "networking_base.html" return_dict["page_title"] = 'Create a network interface bond' return_dict['tab'] = 'view_interfaces_tab' return_dict["error"] = 'Error creating a network interface bond' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def view_interfaces(request): return_dict = {} try: template = 'logged_in_error.html' nics, err = networking.get_interfaces() if err: raise Exception(err) bonds, err = networking.get_bonding_info_all() if err: raise Exception(err) if "ack" in request.GET: if request.GET["ack"] == "saved": return_dict[ 'ack_message'] = "Network interface information successfully updated" if request.GET["ack"] == "removed_bond": return_dict[ 'ack_message'] = "Network bond successfully removed" if request.GET["ack"] == "removed_vlan": return_dict['ack_message'] = "VLAN successfully removed" if request.GET["ack"] == "created_vlan": return_dict['ack_message'] = "VLAN successfully created" if request.GET["ack"] == "state_down": return_dict[ 'ack_message'] = "Network interface successfully disabled. The state change may take a couple of seconds to reflect on this page so please refresh it to check the updated status." if request.GET["ack"] == "state_up": return_dict[ 'ack_message'] = "Network interface successfully enabled. The state change may take a couple of seconds to reflect on this page so please refresh it to check the updated status." if request.GET["ack"] == "created_bond": return_dict[ 'ack_message'] = "Network bond successfully created. Please edit the address information for the bond in order to use it." return_dict["nics"] = nics return_dict["bonds"] = bonds template = "view_interfaces.html" return django.shortcuts.render_to_response( template, return_dict, context_instance=django.template.context.RequestContext(request)) except Exception, e: return_dict['base_template'] = "networking_base.html" return_dict["page_title"] = 'View network interfaces' return_dict['tab'] = 'view_interfaces_tab' return_dict["error"] = 'Error loading interfaces' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response( "logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def create_bond(request): return_dict = {} try: interfaces, err = networking.get_interfaces() if err: raise Exception(err) if not interfaces: raise Exception( "Error loading network interface information : No interfaces found" ) bm, err = networking.get_bonding_masters() if err: raise Exception(err) bid, err = networking.get_bonding_info_all() if err: raise Exception(err) return_dict['interfaces'] = interfaces if_list = [] existing_bonds = [] for if_name, iface in interfaces.items(): ret, err = networking.get_ip_info(if_name) if ret: continue if if_name.startswith('lo') or if_name in bid['by_slave']: continue if if_name in bm: existing_bonds.append(if_name) continue if_list.append(if_name) if request.method == "GET": form = networking_forms.CreateBondForm( interfaces=if_list, existing_bonds=existing_bonds) return_dict['form'] = form return django.shortcuts.render_to_response( "create_bond.html", return_dict, context_instance=django.template.context.RequestContext( request)) else: form = networking_forms.CreateBondForm( request.POST, interfaces=if_list, existing_bonds=existing_bonds) return_dict['form'] = form if not form.is_valid(): return django.shortcuts.render_to_response( "create_bond.html", return_dict, context_instance=django.template.context.RequestContext( request)) cd = form.cleaned_data print cd result, err = networking.create_bond(cd['name'], cd['slaves'], int(cd['mode'])) if not result: if err: raise Exception(err) else: raise Exception('Bond creation failed!') python_scripts_path, err = common.get_python_scripts_path() if err: raise Exception(err) common_python_scripts_path, err = common.get_common_python_scripts_path( ) if err: raise Exception(err) status_path, err = common.get_system_status_path() if err: raise Exception(err) ret, err = command.get_command_output( "python %s/generate_manifest.py %s" % (common_python_scripts_path, status_path)) if err: raise Exception(err) ret, err = command.get_command_output( "python %s/generate_status.py %s" % (common_python_scripts_path, status_path)) if err: raise Exception(err) audit_str = "Created a network bond named %s with slaves %s" % ( cd['name'], ','.join(cd['slaves'])) audit.audit("create_bond", audit_str, request.META) return django.http.HttpResponseRedirect( '/view_interfaces?ack=created_bond') except Exception, e: return_dict['base_template'] = "networking_base.html" return_dict["page_title"] = 'Create a network interface bond' return_dict['tab'] = 'view_interfaces_tab' return_dict["error"] = 'Error creating a network interface bond' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response( "logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def create_bond(): try: os.system('clear') interfaces, err = networking.get_interfaces() if err: raise Exception('Error retrieving interface information : %s' % err) if not interfaces: raise Exception('No interfaces detected') print '\n\nIntegralstor Unicell NIC Bonding' print '---------------------------------\n\n' print 'Available interfaces: \n' bm, err = networking.get_bonding_masters() if err: raise Exception(err) bid, err = networking.get_bonding_info_all() if err: raise Exception(err) avail_if = [] for if_name, iface in interfaces.items(): ret, err = networking.get_ip_info(if_name) if ret: continue if if_name.startswith( 'lo') or if_name in bm or if_name in bid['by_slave']: continue print '\t- %s' % if_name avail_if.append(if_name) print "\n" bond_name = None is_name = False while is_name is False: bond_name = raw_input('Provide bond name: ') if bond_name in interfaces or bond_name.startswith('lo'): print "\t- Can't assign %s, it's been taken already. Please provide another one.\n" % bond_name else: is_name = True print "\n" slaves = [] is_ok = False while is_ok is False: s = raw_input( "\nEnter slaves from the shown interface list, separated by comma. - " ).split(",") slaves = [inner.strip() for inner in s] for slave in slaves: if slave not in avail_if: break else: is_ok = True print("\nSelecetd slaves: %s") % slaves confirm = raw_input("\t- Confirm selected slaves (y/n)? ") if confirm.lower() in ["n"]: is_ok = False print "\n" is_ok = False while is_ok is False: mode = raw_input( "Available modes [4]802.3ad, [6]balance-alb] - 4 or 6?: ") if mode in ["4", "6"]: is_ok = True ret, err = networking.create_bond(bond_name, slaves, int(mode)) if not ret: if err: raise Exception('Error creating bond: %s' % err) else: raise Exception("Couldn't create bond") if ret: print "\nBond created!\n" except Exception, e: print "Error: %s" % e return -1