分类 python 下的文章

openstack tenant指定availability-zone启动虚拟机报错

在admin租户下使用nova boot  --availability-zone在指定的节点上启动虚拟机正常

可是当在非admin租户下指定 --availability-zone 启动虚拟机报错

#nova boot --flavor m1.tiny --image  cirros --nic net-id=65758d11-4027-4b33-9a8f-a5a215bb89c0 --availability-zone nova:vgw test-vgw
ERROR: Policy doesn't allow compute:create:forced_host to be performed. (HTTP 403) (Request-ID: req-42f48090-e0eb-4ed0-8493-99b06d1ce02d)

加--debug选项,看到如下报错信息

INFO (connectionpool:203) Starting new HTTP connection (1): 172.16.85.129
DEBUG (connectionpool:295) "POST /v1.1/bdd28cc0c15245adae5455a67118bb17/servers HTTP/1.1" 403 107
RESP: [403] {'date': 'Fri, 19 Jun 2015 04:45:54 GMT', 'content-length': '107', 'content-type': 'application/json; charset=UTF-8', 'x-compute-request-id': 'req-ed4a06fc-512e-4c5a-9f99-0b7304f817d0'}
RESP BODY: {"forbidden": {"message": "Policy doesn't allow compute:create:forced_host to be performed.", "code": 403}}

DEBUG (shell:783) Policy doesn't allow compute:create:forced_host to be performed. (HTTP 403) (Request-ID: req-ed4a06fc-512e-4c5a-9f99-0b7304f817d0)
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/novaclient/shell.py", line 780, in main
    OpenStackComputeShell().main(map(strutils.safe_decode, sys.argv[1:]))
  File "/usr/lib/python2.6/site-packages/novaclient/shell.py", line 716, in main
    args.func(self.cs, args)
  File "/usr/lib/python2.6/site-packages/novaclient/v1_1/shell.py", line 433, in do_boot
    server = cs.servers.create(*boot_args, **boot_kwargs)
  File "/usr/lib/python2.6/site-packages/novaclient/v1_1/servers.py", line 871, in create
    **boot_kwargs)
  File "/usr/lib/python2.6/site-packages/novaclient/v1_1/servers.py", line 534, in _boot
    return_raw=return_raw, **kwargs)
  File "/usr/lib/python2.6/site-packages/novaclient/base.py", line 152, in _create
    _resp, body = self.api.client.post(url, body=body)
  File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 312, in post
    return self._cs_request(url, 'POST', **kwargs)
  File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 286, in _cs_request
    **kwargs)
  File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 268, in _time_request
    resp, body = self.request(url, method, **kwargs)
  File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 262, in request
    raise exceptions.from_response(resp, body, url, method)
Forbidden: Policy doesn't allow compute:create:forced_host to be performed. (HTTP 403) (Request-ID: req-ed4a06fc-512e-4c5a-9f99-0b7304f817d0)
ERROR: Policy doesn't allow compute:create:forced_host to be performed. (HTTP 403) (Request-ID: req-ed4a06fc-512e-4c5a-9f99-0b7304f817d0)

 

解决方法如下

vim /etc/nova/policy.json

#change
"compute:create:forced_host": "is_admin:True",
#to
"compute:create:forced_host": "",

重启nova-compute服务即可

nova boot vm with '--nic net-id=xxxx, v4-fixed-ip=xxx' failed

在juno上指定ip启动虚拟机会出错,查询日志,在/var/log/nova/nova-compute.log
里面有如下出错信息(拖动滚动条看最右边的):

2015-06-09 05:53:41.966 19951 ERROR nova.compute.manager [-] [instance: d9058791-9971-4962-8c18-5fb3188355ab] Instance failed to spawn
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab] Traceback (most recent call last):
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/compute/manager.py", line 2246, in _build_resources
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     yield resources
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/compute/manager.py", line 2116, in _build_and_run_instance
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     block_device_info=block_device_info)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/virt/libvirt/driver.py", line 2616, in spawn
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     admin_pass=admin_password)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/virt/libvirt/driver.py", line 3097, in _create_image
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     instance, network_info, admin_pass, files, suffix)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/virt/libvirt/driver.py", line 2894, in _inject_data
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     network_info, libvirt_virt_type=CONF.libvirt.virt_type)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/virt/netutils.py", line 87, in get_injected_network_template
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     if not (network_info and template):
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/network/model.py", line 463, in __len__
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     return self._sync_wrapper(fn, *args, **kwargs)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/network/model.py", line 450, in _sync_wrapper
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     self.wait()
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/network/model.py", line 482, in wait
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     self[:] = self._gt.wait()
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/eventlet/greenthread.py", line 173, in wait
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     return self._exit_event.wait()
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/eventlet/event.py", line 125, in wait
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     current.throw(*self._exc)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/eventlet/greenthread.py", line 212, in main
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     result = function(*args, **kwargs)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/compute/manager.py", line 1646, in _allocate_network_async
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     dhcp_options=dhcp_options)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/network/neutronv2/api.py", line 443, in allocate_for_instance
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     self._delete_ports(neutron, instance, created_port_ids)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/openstack/common/excutils.py", line 82, in __exit__
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     six.reraise(self.type_, self.value, self.tb)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/network/neutronv2/api.py", line 423, in allocate_for_instance
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     security_group_ids, available_macs, dhcp_opts)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/network/neutronv2/api.py", line 214, in _create_port
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     port_id = port_client.create_port(port_req_body)['port']['id']
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/network/neutronv2/__init__.py", line 84, in wrapper
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     ret = obj(*args, **kwargs)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/neutronclient/v2_0/client.py", line 98, in with_params
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     ret = self.function(instance, *args, **kwargs)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/neutronclient/v2_0/client.py", line 322, in create_port
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     return self.post(self.ports_path, body=body)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/network/neutronv2/__init__.py", line 84, in wrapper
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     ret = obj(*args, **kwargs)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/neutronclient/v2_0/client.py", line 1325, in post
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     headers=headers, params=params)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/network/neutronv2/__init__.py", line 84, in wrapper
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     ret = obj(*args, **kwargs)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/neutronclient/v2_0/client.py", line 1236, in do_request
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     body = self.serialize(body)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/nova/network/neutronv2/__init__.py", line 84, in wrapper
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     ret = obj(*args, **kwargs)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/neutronclient/v2_0/client.py", line 1266, in serialize
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     self.get_attr_metadata()).serialize(data, self.content_type())
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/neutronclient/common/serializer.py", line 390, in serialize
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     return self._get_serialize_handler(content_type).serialize(data)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/neutronclient/common/serializer.py", line 54, in serialize
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     return self.dispatch(data, action=action)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/neutronclient/common/serializer.py", line 44, in dispatch
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     return action_method(*args, **kwargs)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/neutronclient/common/serializer.py", line 66, in default
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     return jsonutils.dumps(data, default=sanitizer)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/neutronclient/openstack/common/jsonutils.py", line 168, in dumps
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     return json.dumps(value, default=default, **kwargs)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib64/python2.7/json/__init__.py", line 250, in dumps
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     sort_keys=sort_keys, **kw).encode(obj)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     chunks = self.iterencode(o, _one_shot=True)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     return _iterencode(o, 0)
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]   File "/usr/lib/python2.7/site-packages/neutronclient/common/serializer.py", line 65, in sanitizer
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab]     return six.text_type(obj, 'utf8')
2015-06-09 05:53:41.966 19951 TRACE nova.compute.manager [instance: d9058791-9971-4962-8c18-5fb3188355ab] TypeError: coercing to Unicode: need string or buffer, IPAddress found

以关键字 “TypeError: coercing to Unicode: need string or buffer, IPAddress found” ,搜索到此bug网页:https://bugs.launchpad.net/nova/+bug/1408529
文中给出了bug原因:If ip address is provided when running nova boot, nova compute will invoke neutron client to create a port. However, the ip address parameter is an IPAddress object so neutron client will fail to send the request to neutron server. Transform IPAddress object to string to address this issue.
只需要把发送给neutronclient的参数改为str就可以了
bug修复:https://git.openstack.org/cgit/openstack/nova/commit/?id=aae858a246e20b1bf55004517b5d9ab28968190a

编辑/usr/lib/python2.7/site-packages/nova/network/neutronv2/api.py

 198   try:
 199       if fixed_ip:
 200           port_req_body['port']['fixed_ips'] = [{'ip_address': fixed_ip}]
 201           port_req_body['port']['network_id'] = network_id

修改200行为:

200            port_req_body['port']['fixed_ips'] = [{'ip_address': str(fixed_ip)}]

重启 nova-compute后成功指定ip启动虚拟机

 

python从日志文件中提取出现的ip

#coding:utf-8
'''
Created on 2015年4月13日

@author: Administrator
'''
fp = open('rootaccess.07log','r')
total_count = 0
ip_array = {}

for eachLine in fp:
    ip =  eachLine.split(" ")[0]
    if ip_array.has_key(ip):
        ip_array[ip] = ip_array[ip] + 1
    else:
        ip_array.setdefault(ip,1)
    total_count = total_count + 1

R=ip_array.items()
num = 0
for i in R:
    if i[1]>0:
        print i
        num+=1
print total_count
fp.close()

日志文件如下所示(160W+行),需要提取首行ip,并统计全部ip数,不包括重复的

116.233.77.238 - - [06/Apr/2015:23:59:59 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
61.170.251.177 - - [06/Apr/2015:23:59:59 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
101.86.33.55 - - [06/Apr/2015:23:59:59 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
114.86.53.187 - - [06/Apr/2015:23:59:59 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
180.155.104.206 - - [06/Apr/2015:23:59:59 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
114.88.209.25 - - [06/Apr/2015:23:59:59 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
180.164.251.133 - - [06/Apr/2015:23:59:59 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
116.230.55.212 - - [07/Apr/2015:00:00:00 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
114.95.223.230 - - [07/Apr/2015:00:00:00 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
101.229.143.32 - - [07/Apr/2015:00:00:00 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
114.92.56.189 - - [07/Apr/2015:00:00:00 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
116.230.118.3 - - [07/Apr/2015:00:00:00 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
180.157.88.191 - - [07/Apr/2015:00:00:00 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
124.77.20.187 - - [07/Apr/2015:00:00:00 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"
180.155.152.186 - - [07/Apr/2015:00:00:00 +0800] "POST /api/magicboxauth HTTP/1.1" 200 203 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" "-"

 

 

如果需要提取全部的ip地址,linux一条命令可以达到要求,但是命令不是排除日志请求中的ip

# grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}" xxx.log|sort|uniq -c