首页 文章 精选 留言 我的

精选列表

搜索[Python支持],共10000篇文章
优秀的个人博客,低调大师

OpenStack nova-network 支持多vlan技术实现片段代码

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 #-*-encoding:utf-8-*- # #Copyright2012RedHat,Inc # #Author:EoghanGlynn<eglynn@redhat.com> # #LicensedundertheApacheLicense,Version2.0(the"License");youmay #notusethisfileexceptincompliancewiththeLicense.Youmayobtain #acopyoftheLicenseat # #http://www.apache.org/licenses/LICENSE-2.0 # #Unlessrequiredbyapplicablelaworagreedtoinwriting,software #distributedundertheLicenseisdistributedonan"ASIS"BASIS,WITHOUT #WARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.Seethe #Licenseforthespecificlanguagegoverningpermissionsandlimitations #undertheLicense. """ImplementationofInspectorabstractionforlibvirt.""" import libvirt import os,sys,time import traceback from lxml import etree from oslo.config import cfg from nova.openstack.common import logaslogging from nova import utils libvirt = None LOG = logging.getLogger(__name__) libvirt_opts = [ cfg.StrOpt( 'libvirt_type' , default = 'kvm' , help = 'Libvirtdomaintype(validoptionsare:' 'kvm,lxc,qemu,uml,xen)' ), cfg.StrOpt( 'libvirt_uri' , default = '', help = 'OverridethedefaultlibvirtURI' '(whichisdependentonlibvirt_type)' ), cfg.StrOpt( 'network_script_path' , default = '/etc/sysconfig/network-scripts/' , help = 'Centos6.6systemnetworkconfigpath' ), ] CONF = cfg.CONF CONF.register_opts(libvirt_opts) class LibvirtNetwork( object ): per_type_uris = dict (uml = 'uml:///system' ,xen = 'xen:///' ,lxc = 'lxc:///' ) def __init__( self ): self .uri = self ._get_uri() self .connection = None def _get_uri( self ): return CONF.libvirt_uri or self .per_type_uris.get(CONF.libvirt_type, 'qemu:///system' ) def _get_connection( self ): if not self .connection or not self ._test_connection(): global libvirt if libvirt is None : libvirt = __import__ ( 'libvirt' ) LOG.debug( 'Connectingtolibvirt:%s' , self .uri) self .connection = libvirt.openReadOnly( self .uri) return self .connection def _test_connection( self ): try : self .connection.getCapabilities() return True except libvirt.libvirtErrorase: print '*' * 100 if (e.get_error_code() = = libvirt.VIR_ERR_SYSTEM_ERROR and e.get_error_domain() in (libvirt.VIR_FROM_REMOTE, libvirt.VIR_FROM_RPC)): LOG.debug( 'Connectiontolibvirtbroke' ) return False raise def _lookup_by_name( self ,instance_name): try : return self ._get_connection().lookupByName(instance_name) except Exceptionasex: if not libvirt or not isinstance (ex,libvirt.libvirtError): LOG.error(ex) error_code = ex.get_error_code() msg = ( "Errorfromlibvirtwhilelookingup%(instance_name)s:" "[ErrorCode%(error_code)s]" "%(ex)s" % { 'instance_name' :instance_name, 'error_code' :error_code, 'ex' :ex}) LOG.error(msg) def get_vnic_name( self ,instance_name): domain = self ._lookup_by_name(instance_name) tree = etree.fromstring(domain.XMLDesc( 0 )) for iface in tree.findall( 'devices/interface' ): target = iface.find( 'target' ) if target is not None : vnic_name = target.get( 'dev' ) else : continue mac = iface.find( 'mac' ) if mac is not None : mac_address = mac.get( 'address' ) else : continue fref = iface.find( 'filterref' ) if fref is not None : fref = fref.get( 'filter' ) return vnic_name #Addwilliam,Bridgesupportsmultiplevlantechnology def bridge_multiple_vlan( self ,instance): network_info = dict () try : new_source_dev = instance[ 'hostname' ].split( '.' )[ - 1 ] if not new_source_dev: new_source_dev = 'br100' if 'vlan' not in new_source_dev: new_source_dev = 'br100' network_info[ 'vlan_name' ] = new_source_dev network_info[ 'instances_name' ] = instance[ 'name' ] #network_info['vnet_name']=self.get_vnic_name(instance['name']) network_info[ 'bond_name' ] = os.listdir( '/proc/net/bonding' )[ 0 ] network_info[ 'bond_dev' ] = '%s.%s' % (network_info[ 'bond_name' ],new_source_dev.split( 'vlan' )[ - 1 ]) network_info[ 'vlan_id' ] = new_source_dev.split( 'vlan' )[ - 1 ] check_result = self .check_bridge_vlan(network_info[ 'vlan_name' ],network_info[ 'bond_dev' ]) if not check_result: self .add_bridge_vlan(network_info) else : LOG.info( "Addwilliam,%sisexists,soisnotcreate" % network_info[ 'vlan_name' ]) except : LOG.error(traceback.format_exc()) def add_bridge_vlan( self ,network_info): utils.execute( "vconfig" , "add" ,network_info[ 'bond_name' ],network_info[ 'vlan_id' ],run_as_root = True ) LOG.info( "Addwilliam,utils.exeuctevconfigadd%s%s" % (network_info[ 'bond_name' ],network_info[ 'vlan_id' ])) utils.execute( "ifconfig" ,network_info[ 'bond_dev' ], "up" ,run_as_root = True ) LOG.info( "Addwilliam,utils.executeifconfig%sup" % (network_info[ 'bond_dev' ])) utils.execute( "brctl" , "addbr" ,network_info[ 'vlan_name' ],run_as_root = True ) LOG.info( "Addwilliam,utils.executebrctladdbr%s" % (network_info[ 'vlan_name' ])) utils.execute( "brctl" , "addif" ,network_info[ 'vlan_name' ],network_info[ 'bond_dev' ],run_as_root = True ) LOG.info( "Addwilliam,utils.executebrctladdif%s%s" % (network_info[ 'vlan_name' ],network_info[ 'bond_dev' ])) utils.execute( "ifconfig" ,network_info[ 'vlan_name' ], "up" ,run_as_root = True ) LOG.info("") #utils.execute("brctl","addif",network_info['vlan_name'],network_info['vnet_name'],run_as_root=True) bond_script = "%s/ifcfg-%s" % (CONF.network_script_path,network_info[ 'bond_dev' ]) vlan_script = "%s/ifcfg-%s" % (CONF.network_script_path,network_info[ 'vlan_name' ]) utils.execute( "chmod" , "777" ,CONF.network_script_path,run_as_root = True ) fp = open (bond_script, 'w' ) fp.write( "DEVICE=%s\n" % network_info[ 'bond_dev' ]) fp.write( "ONBOOT=yes\n" ) fp.write( "NM_CONTROLLED=no\n" ) fp.write( "BOOTPROTO=static\n" ) fp.write( "VLAN=yes\n" ) fp.close() fp = open (vlan_script, 'w' ) fp.write( "DEVICE=%s\n" % network_info[ 'vlan_name' ]) fp.write( "BOOTPROTO=static\n" ) fp.write( "ONBOOT=yes\n" ) fp.write( "TYPE=Bridge\n" ) fp.close() utils.execute( "chmod" , "755" ,CONF.network_script_path,run_as_root = True ) def check_bridge_vlan( self ,vlan_dev,bond_dev): vlan_list = bond_list = list () vlan = bond = False vlan_info = os.popen( "brctlshow|grepvlan|awk'{print$1}'" ).readlines() for i in vlan_info: vlan_list.append(i.strip()) bond_info = os.popen( "ifconfig|grepbond0|awk'{print$1}'" ).readlines() for i in bond_info: bond_list.append(i.strip()) if vlan_dev in vlan_list: vlan = True if bond_dev in bond_list: bond = True if vlan and bond: return True else : return False if __name__ = = "__main__" : instance = {u 'vm_state' :u 'building' ,u 'internal_id' : None ,u 'availability_zone' :u 'nova' ,u 'terminated_at' : None ,u 'ephemeral_gb' : 100 ,u 'instance_type_id' : 26 ,u 'user_data' : None ,u 'cleaned' : 0 ,u 'vm_mode' : None ,u 'deleted_at' : None ,u 'reservation_id' :u 'r-z2zsdcvl' ,u 'id' : 299 ,u 'security_groups' :[{u 'project_id' :u '50fd5d79ab184c399cdc22da78b8a7bc' ,u 'user_id' :u '373ddabe004846ebba268d7e88d13e16' ,u 'name' :u 'default' ,u 'deleted' : 0 ,u 'created_at' :u '2014-09-30T07:49:38.000000' ,u 'updated_at' : None ,u 'rules' :[],u 'deleted_at' : None ,u 'id' : 1 ,u 'description' :u 'default' }],u 'disable_terminate' : False ,u 'root_device_name' :u '/dev/vda' ,u 'user_id' :u '373ddabe004846ebba268d7e88d13e16' ,u 'uuid' :u 'c4cf7bc1-72bd-4523-bab0-fdd0f837fb46' ,u 'default_swap_device' : None ,u 'info_cache' :{u 'instance_uuid' :u 'c4cf7bc1-72bd-4523-bab0-fdd0f837fb46' ,u 'deleted' : 0 ,u 'created_at' :u '2015-04-16T03:36:51.000000' ,u 'updated_at' :u '2015-04-16T03:36:52.000000' ,u 'network_info' :u '[{"ovs_interfaceid":null,"network":{"bridge":"br100","subnets":[{"ips":[{"meta":{},"version":4,"type":"fixed","floating_ips":[],"address":"192.168.221.164"}],"version":4,"meta":{"dhcp_server":"192.168.221.11"},"dns":[{"meta":{},"version":4,"type":"dns","address":"192.168.252.25"},{"meta":{},"version":4,"type":"dns","address":"192.168.252.24"}],"routes":[],"cidr":"192.168.220.0/19","gateway":{"meta":{},"version":4,"type":"gateway","address":"192.168.192.1"}},{"ips":[],"version":null,"meta":{"dhcp_server":"192.168.221.11"},"dns":[],"routes":[],"cidr":null,"gateway":{"meta":{},"version":null,"type":"gateway","address":null}}],"meta":{"tenant_id":"50fd5d79ab184c399cdc22da78b8a7bc","multi_host":true,"should_create_bridge":true,"should_create_vlan":true,"bridge_interface":"bond0"},"id":"4a8546f2-3919-445a-9f87-b938491d41a5","label":"publics"},"devname":null,"qbh_params":null,"meta":{},"address":"fa:16:3e:c5:be:93","type":"bridge","id":"a7a69211-6aeb-4b54-9a20-803b96df73bf","qbg_params":null}]' ,u 'deleted_at' : None ,u 'id' : 299 },u 'hostname' :u 'gg-esc-web.vlan203' ,u 'launched_on' :u 'localhost' ,u 'display_description' :u 'gg-esc-web.vlan201' ,u 'key_data' : None ,u 'deleted' : 0 ,u 'scheduled_at' :u '2015-04-16T03:36:51.000000' ,u 'power_state' : 0 ,u 'default_ephemeral_device' : None ,u 'progress' : 0 ,u 'project_id' :u '50fd5d79ab184c399cdc22da78b8a7bc' ,u 'launched_at' : None ,u 'config_drive' :u' ',u' node ':u' localhost ',u' ramdisk_id ':u' ',u' access_ip_v6 ':None,u' access_ip_v4 ':None,u' kernel_id ':u' ',u' key_name ':None,u' updated_at ':u' 2015 - 04 - 16T03 : 36 : 52.727467 ',u' host ':u' localhost ',u' display_name ':u' gg - esc - web.vlan201 ',u' system_metadata ':[{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' windows ',u' key ':u' image_os_type ',u' deleted_at ':None,u' id ':4889},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' 8192 ',u' key ':u' instance_type_memory_mb ',u' deleted_at ':None,u' id ':4890},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' 0 ',u' key ':u' instance_type_swap ',u' deleted_at ':None,u' id ':4891},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':None,u' key ':u' instance_type_vcpu_weight ',u' deleted_at ':None,u' id ':4892},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' 60 ',u' key ':u' instance_type_root_gb ',u' deleted_at ':None,u' id ':4893},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' 26 ',u' key ':u' instance_type_id ',u' deleted_at ':None,u' id ':4894},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' windows - 16 - 8G - 100G ',u' key ':u' instance_type_name ',u' deleted_at ':None,u' id ':4895},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' 100 ',u' key ':u' instance_type_ephemeral_gb ',u' deleted_at ':None,u' id ':4896},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' 1 ',u' key ':u' instance_type_rxtx_factor ',u' deleted_at ':None,u' id ':4897},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' qcow2 ',u' key ':u' image_disk_format ',u' deleted_at ':None,u' id ':4898},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' 74c9f78a - 06d6 - 432d - 8051 - 4eeecf8ebff7 ',u' key ':u' instance_type_flavorid ',u' deleted_at ':None,u' id ':4899},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' ovf ',u' key ':u' image_container_format ',u' deleted_at ':None,u' id ':4900},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' 16 ',u' key ':u' instance_type_vcpus ',u' deleted_at ':None,u' id ':4901},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' 0 ',u' key ':u' image_min_ram ',u' deleted_at ':None,u' id ':4902},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' 60 ',u' key ':u' image_min_disk ',u' deleted_at ':None,u' id ':4903},{u' instance_uuid ':u' c4cf7bc1 - 72bd - 4523 - bab0 - fdd0f837fb46 ',u' deleted ':0,u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' updated_at ':None,u' value ':u' 406c0425 - b8d3 - 4607 - beff - 8fa529b46b7a ',u' key ':u' image_base_image_ref ',u' deleted_at ':None,u' id ':4904}],u' task_state ':u' spawning ',u' shutdown_terminate ':False,u' cell_name ':None,u' root_gb ':60,u' locked ':False,u' name ':u' instance - 0000012b ',u' created_at ':u' 2015 - 04 - 16T03 : 36 : 51.000000 ',u' locked_by ':None,u' launch_index ':0,u' memory_mb ':8192,u' vcpus ':16,u' image_ref ':u' 406c0425 - b8d3 - 4607 - beff - 8fa529b46b7a ',u' architecture ':None,u' auto_disk_config ':False,u' os_type ':u' windows ',u' metadata':[]} sc = LibvirtNetwork() sc.bridge_multiple_vlan(instance) 本文转自 swq499809608 51CTO博客,原文链接:http://blog.51cto.com/swq499809608/1633811

优秀的个人博客,低调大师

android 4.4 支持透明状态栏和透明导航栏

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } setContentView(R.layout.activity_main); } 本文转自 netcorner 博客园博客,原文链接:http://www.cnblogs.com/netcorner/p/4781253.html ,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

用户登录
用户注册
修改密码
忘记密码