OpenStack SDK -如何使用Kernel id和Ramdisk参数创建镜像



我一直在尝试创建一个OpenStack镜像通知内核Id和Ramdisk Id, 使用OpenStack统一SDK (https://github.com/openstack/python-openstacksdk),但没有成功。我知道这是可能的,因为OpenStack的CLI有这些参数,如这个页面(http://docs.openstack.org/cli-reference/glance.html#glance-image-create)所示,其中CLI有"——kernel-id"one_answers"——ramdisk-id"参数。我已经在终端中使用了这个参数,并确认它们可以工作,但我需要在python中使用它们。

我试图使用upload_method,如这里所述http://developer.openstack.org/sdks/python/openstacksdk/users/proxies/image.html#image-api-v2,但我不能得到attrs参数正确。文档只说它应该是一个字典。下面是我使用的代码

...
    atrib = {
        'properties': {
            'kernel_id': 'd84e1f2b-8d8c-4a4a-8858-77a8d5a93cb1',
            'ramdisk_id': 'cfef18e0-006e-477a-a098-593d43435a1e'
        } 
    } 
    with open(file) as fimage:
    image = image_service.upload_image(
        name=name,
        data=fimage,
        disk_format='qcow2',
        container_format='bare',
        **atrib)
....

这里是我得到的错误:

  File "builder.py", line 121, in main
    **atrib
  File "/usr/lib/python2.7/site-packages/openstack/image/v2/_proxy.py", line 51, in upload_image
    **attrs)
  File "/usr/lib/python2.7/site-packages/openstack/proxy2.py", line 193, in _create
    return res.create(self.session)
  File "/usr/lib/python2.7/site-packages/openstack/resource2.py", line 570, in create
    json=request.body, headers=request.headers)
  File "/usr/lib/python2.7/site-packages/keystoneauth1/session.py", line 675, in post
    return self.request(url, 'POST', **kwargs)
  File "/usr/lib/python2.7/site-packages/openstack/session.py", line 52, in map_exceptions_wrapper
    http_status=e.http_status, cause=e)
openstack.exceptions.HttpException: HttpException: Bad Request, 400 Bad Request
Provided object does not match schema 'image': {u'kernel_id': u'd84e1f2b-8d8c-4a4a-8858-77a8d5a93cb1', u'ramdisk_id': u'cfef18e0-006e-477a-a098-593d43435a1e'} is not of type 'string'  Failed validating 'type' in schema['additionalProperties']:     {'type': 'string'}  On instance[u'properties']:     {u'kernel_id': u'd84e1f2b-8d8c-4a4a-8858-77a8d5a93cb1',      u'ramdisk_id': u'cfef18e0-006e-477a-a098-593d43435a1e'}

已经尝试使用update_image方法,但没有成功,传递内核id和ramdisk id作为字符串创建实例,但它没有启动。有人知道怎么解吗?

您使用的glance API是什么版本?

我已经阅读了openstackclient/image/v1/images.py, openstackclient/v1/shell.py中的代码

## in shell.py
def do_image_create(gc, args):
    ...
    fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))
    raw_properties = fields.pop('property')
    fields['properties'] = {}
    for datum in raw_properties:
        key, value = datum.split('=', 1)
        fields['properties'][key] = value
    ...    
    image = gc.images.create(**fields)
## in images.py
def create(self, **kwargs):
    ...
    for field in kwargs:
        if field in CREATE_PARAMS:
            fields[field] = kwargs[field]
        elif field == 'return_req_id':
            continue
        else:
            msg = 'create() got an unexpected keyword argument '%s''
            raise TypeError(msg % field)
    hdrs = self._image_meta_to_headers(fields)
    ...
    resp, body = self.client.post('/v1/images',
                                  headers=hdrs,
                                  data=image_data)
    ...

和openstackclient/v2/shell.py,openstackclient/image/v2.images.py(我也调试了这个)

## in shell.py
def do_image_create(gc, args):
    ...
    raw_properties = fields.pop('property', [])
    for datum in raw_properties:
        key, value = datum.split('=', 1)
        fields[key] = value
    ...
    image = gc.images.create(**fields)
##in images.py
def create(self, **kwargs):
        """Create an image.""
        url = '/v2/images'
        image = self.model()
        for (key, value) in kwargs.items():
            try:
                setattr(image, key, value)
            except warlock.InvalidOperation as e:
                raise TypeError(utils.exception_to_str(e))
        resp, body = self.http_client.post(url, data=image)
        ...

似乎,您可以使用1.0版本中的方式创建映像,但在2.0版本中,您应该使用如下所示的kernel_id和ramdisk_id

atrib = {        
        'kernel_id': 'd84e1f2b-8d8c-4a4a-8858-77a8d5a93cb1',
        'ramdisk_id': 'cfef18e0-006e-477a-a098-593d43435a1e' 
} 

但是OpenStack SDK似乎无法将这两个参数转换为url(因为在OpenStack/image/v2/image.py中没有Body定义)。所以你应该修改OpenStack SDK来支持这个。

顺便说一句,OpenStack的代码和它的版本有点不同,但很多东西是一样的

相关内容

  • 没有找到相关文章

最新更新