Buggy B
Buggy B

Reputation: 765

Openstack API - Creating instances does not accept user-data = <bash script>

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

Upvotes: 1

Views: 951

Answers (1)

Buggy B
Buggy B

Reputation: 765

Python3 handles string and binary differently. Also, to pass bash/cloud-config file to --user_data via OpenstackSDK, it has to be base46 encoded.

Code snippet:

with open(USER_DATA,'r') as file:
        f = encodeutils.safe_encode(file.read().encode('utf-8'))
        init_script = base64.b64encode(f).decode('utf-8')

Upvotes: 1

Related Questions