简体   繁体   English

在OpenStack中创建实例时指定安装后脚本[python-novaclient]

[英]Specify post-installation script when creating an instance in OpenStack [python-novaclient]

I have a working python program which is able to create instances on OpenStack thanks to the python-novaclient library. 我有一个工作的python程序,由于python-novaclient库,它能够在OpenStack上创建实例。

Now I'd like to give a post-installation script at the creation time. 现在我想在创建时给出一个安装后脚本。 I looked at the documentation of the Servers .create() method but it doesn't seem to be implemented. 我查看了Servers .create()方法的文档,但似乎没有实现。

Did anyone faced this problem? 有人遇到过这个问题吗?


EDIT 编辑

In Horizon, when we create an instance, there is this information next to the textarea for the post-installation script: 在Horizo​​n中,当我们创建实例时,textarea旁边有用于安装后脚本的信息:

The "Customisation Script" field is analogous to "User Data" in other systems. “自定义脚本”字段类似于其他系统中的“用户数据”。

Does it mean userdata is the parameter I need? 这是否意味着userdata是我需要的参数?

userdata – user data to pass to be exposed by the metadata server this can be a file type object as well or a string. userdata - 要传递给元数据服务器的用户数据,也可以是文件类型对象或字符串。

Indeed the solution is on userdata 事实上,解决的办法是对userdata

Here's the Python code I wrote to solve my problem: 这是我为解决问题而编写的Python代码:

## Return the new created instance
# @param name Name of the instance to create in a String format
# @param image OpenStack image to deploy on the virtual machine
# @param flavor OpenStack flavor to use for the virtual machine
# @param keypair Name of the keypair to copy on the instance
# @param sec_groups List of security groups to link to the instance
def create_instance(self,name,image,flavor,keypair=None,sec_groups=None):
  instance = self.client.servers.create(
    name=name,
    image=image,
    flavor=flavor,
    key_name=keypair,
    security_groups=sec_groups,
    userdata="#!/bin/bash \n echo 'AMAZING TEST' > /root/test"
  )
  return instance

Try enabling config drive. 尝试启用配置驱动器。 The user data can be sent to VM via config drive. 用户数据可以通过配置驱动器发送到VM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM