1

I am automating instance creation using OpenstackSDK and passing bash script with commands as userdata. But the script does not excute even though the instance is crated. When I do this manually via GUI, the bash scripts executes fine to the newly created instance.

#Reading bash script
 with open('elk.sh', 'r') as f:
        init_script = f.read()

server = conn.compute.create_server(
        name=name,
        image_id=IMAGE_ID,
        flavor_id=FLAVOUR_ID,
        networks=[{"uuid": NETWORK_ID}],
        user_data=init_script,    # pass script to the instance
        key_name=KEY_PAIR
    ) 

Note: Also tried to encode as Base64 file butstill failed with is not JSON serializable.

Code snippet:

 with open(USER_DATA,'r') as file:
        f = file.read()
        bytes_content = bytes(f,encoding='utf-8')
        init_script = base64.b64encode(bytes_content)

Can anyone advice on this, please?

Thanks

4

1 回答 1

1

Python3 以不同的方式处理字符串和二进制文件。此外,要将bash/cloud-config文件传递给--user_datavia OpenstackSDK,它必须是 base46 编码的。

代码片段:

with open(USER_DATA,'r') as file:
        f = encodeutils.safe_encode(file.read().encode('utf-8'))
        init_script = base64.b64encode(f).decode('utf-8')
于 2017-06-16T17:34:28.287 回答