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

Buggy B
  • 623
  • 7
  • 18
  • Found the solution after R & D with open(USER_DATA,'r') as file: f = encodeutils.safe_encode(file.read().encode('utf-8')) init_script = base64.b64encode(f).decode('utf-8') – Buggy B Jun 16 '17 at 17:31

1 Answers1

1

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')
Luca Di Liello
  • 1,486
  • 2
  • 17
  • 34
Buggy B
  • 623
  • 7
  • 18