Openstack使用自定义密码创建虚拟机

nova.api.openstack.compute.servers.py中的create函数写道虚拟机的adminPass来自一个名为password的变量。这个变量又是通过_get_server_admin_password函数产生的:

create函数:

1
password = self._get_server_admin_password(server_dict)

_get_server_admin_password函数:

1
2
3
4
5
6
7
8
9
10
11
def _get_server_admin_password(self, server):
"""Determine the admin password for a server on creation."""
try:
password = server['adminPass']
self._validate_admin_password(password)
except KeyError:
password = utils.generate_password()
except ValueError:
raise exc.HTTPBadRequest(explanation=_("Invalid adminPass"))

return password

由此可见,_get_server_admin_password函数已经具备判断并选择自定义密码的能力,于是只要传入一个adminPassserver_dict里即可。

修改后的create函数:

1
2
3
4
5
6
7
meta_dict = body['server'].get('metadata', None)
if meta_dict:
if meta_dict.get('password', None):
server_dict['adminPass'] = meta_dict['password']

del server_dict['metadata']
password = self._get_server_admin_password(server_dict)

调用方法:

1
nova boot ******* --meta password=mypass *****