使用 openstacksdk 启动实例时执行自定义脚本
Execute customized script when launching instance using openstacksdk
我是 Openstack 的新手,我正在尝试创建一个工具,以便我可以在 Openstack 云中启动任意数量的实例。这可以使用 openstacksdk 的 nova-client 模块轻松完成。
现在的问题是我想让实例执行 bash 脚本,因为它们是通过将其添加为用户数据文件创建的,但它不执行。这很令人困惑,因为我没有任何错误或警告消息。有谁知道它会是什么?
代码的重要部分
Python 程序最重要的部分是获取云信息的函数、创建实例的函数和主要函数,.正如@Corey 所说,我会 post 他们在这里。
"""
Function that allow us to log at cloud with all the credentials needed.
Username and password are not read from env.
"""
def get_nova_credentials_v2():
d = {}
user = ""
password = ""
print("Logging in...")
user = input("Username: ")
password = getpass.getpass(prompt="Password: ", stream=None)
while (user == "" or password == ""):
print("User or password field is empty")
user = input("Username: ")
password = getpass.getpass(prompt="Password: ", stream=None)
d['version'] = '2.65'
d['username'] = user
d['password'] = password
d['project_id'] = os.environ['OS_PROJECT_ID']
d['auth_url'] = os.environ['OS_AUTH_URL']
d['user_domain_name'] = os.environ['OS_USER_DOMAIN_NAME']
return d
然后我们有create_server函数:
"""
This function creates a server using the info we got from JSON file
"""
def create_server(server):
s = {}
print("Creating "+server['compulsory']['name']+"...")
s['name'] = server['compulsory']['name']
s['image'] = server['compulsory']['os']
s['flavor'] = server['compulsory']['flavor']
s['min_count'] = server['compulsory']['copyNumber']
s['max_count'] = server['compulsory']['copyNumber']
s['userdata'] = server['file']
s['key_name'] = server['compulsory']['keyName']
s['availability_zone'] = server['compulsory']['availabilityZone']
s['nics'] = server['compulsory']['network']
print(s['userdata'])
if(exists("instalacion_k8s_docker.sh")):
print("Exists")
s['userdata'] = server['file']
nova.servers.create(**s)
现在主要功能:
"""
Main process: First we create a connection to Openstack using our credentials.
Once connected we cal get_serverdata function to get all instance objects we want to be created.
We check that it is not empty and that we are not trying to create more instances than we are allowed.
Lastly we create the instances and the program finishes.
"""
credentials = get_nova_credentials_v2()
nova = client.Client(**credentials)
instances = get_serverdata()
current_instances = len(nova.servers.list())
if not instances:
print("No instance was writen. Check instances.json file.")
exit(3)
num = 0
for i in instances:
create_server(i)
exit(0)
对于其余代码,您可以在 github 上访问此 public 存储库。
非常感谢!
问题已解决
正如@Corey 所说,问题出在 server['file']
的内容上。它不能是您写入数据的文件的路径,而是它的内容或文件类型对象。在 OpenstackSDK 的情况下,它必须是 base64 编码,但在 Novaclient 中不是这样。
非常感谢@Corey 的所有帮助! :)
我是 Openstack 的新手,我正在尝试创建一个工具,以便我可以在 Openstack 云中启动任意数量的实例。这可以使用 openstacksdk 的 nova-client 模块轻松完成。 现在的问题是我想让实例执行 bash 脚本,因为它们是通过将其添加为用户数据文件创建的,但它不执行。这很令人困惑,因为我没有任何错误或警告消息。有谁知道它会是什么?
代码的重要部分
Python 程序最重要的部分是获取云信息的函数、创建实例的函数和主要函数,.正如@Corey 所说,我会 post 他们在这里。
"""
Function that allow us to log at cloud with all the credentials needed.
Username and password are not read from env.
"""
def get_nova_credentials_v2():
d = {}
user = ""
password = ""
print("Logging in...")
user = input("Username: ")
password = getpass.getpass(prompt="Password: ", stream=None)
while (user == "" or password == ""):
print("User or password field is empty")
user = input("Username: ")
password = getpass.getpass(prompt="Password: ", stream=None)
d['version'] = '2.65'
d['username'] = user
d['password'] = password
d['project_id'] = os.environ['OS_PROJECT_ID']
d['auth_url'] = os.environ['OS_AUTH_URL']
d['user_domain_name'] = os.environ['OS_USER_DOMAIN_NAME']
return d
然后我们有create_server函数:
"""
This function creates a server using the info we got from JSON file
"""
def create_server(server):
s = {}
print("Creating "+server['compulsory']['name']+"...")
s['name'] = server['compulsory']['name']
s['image'] = server['compulsory']['os']
s['flavor'] = server['compulsory']['flavor']
s['min_count'] = server['compulsory']['copyNumber']
s['max_count'] = server['compulsory']['copyNumber']
s['userdata'] = server['file']
s['key_name'] = server['compulsory']['keyName']
s['availability_zone'] = server['compulsory']['availabilityZone']
s['nics'] = server['compulsory']['network']
print(s['userdata'])
if(exists("instalacion_k8s_docker.sh")):
print("Exists")
s['userdata'] = server['file']
nova.servers.create(**s)
现在主要功能:
"""
Main process: First we create a connection to Openstack using our credentials.
Once connected we cal get_serverdata function to get all instance objects we want to be created.
We check that it is not empty and that we are not trying to create more instances than we are allowed.
Lastly we create the instances and the program finishes.
"""
credentials = get_nova_credentials_v2()
nova = client.Client(**credentials)
instances = get_serverdata()
current_instances = len(nova.servers.list())
if not instances:
print("No instance was writen. Check instances.json file.")
exit(3)
num = 0
for i in instances:
create_server(i)
exit(0)
对于其余代码,您可以在 github 上访问此 public 存储库。 非常感谢!
问题已解决
正如@Corey 所说,问题出在 server['file']
的内容上。它不能是您写入数据的文件的路径,而是它的内容或文件类型对象。在 OpenstackSDK 的情况下,它必须是 base64 编码,但在 Novaclient 中不是这样。
非常感谢@Corey 的所有帮助! :)