While integrating an application with openstack we had to consume the nova REST api to create servers and utilize the cloud-init pacakge to set the user and password during deployment time.

Horizon custom script

During horizon that is possible by including a custom script
Screen Shot 2014-02-05 at 12.20.22 PM

However, when utilizing the nova REST api the flow is a bit different

python-nova-client user-data

Looking at the nova help docs it says the user-data attribute must be set with the custom script to be used when the server gets launched

 
root@controller01:~# nova help boot
usage: nova boot [--flavor ] [--image ]
                 [--image-with ] [--boot-volume ]
                 [--snapshot ] [--num-instances ]
                 [--meta ] [--file ]
                 [--key-name ] [--user-data ]
                 [--availability-zone ]
                 [--security-groups ]
                 [--block-device-mapping ]
                 [--block-device key1=value1[,key2=value2...]]
                 [--swap ]
                 [--ephemeral size=[,format=]]
                 [--hint ]
                 [--nic ]
                 [--config-drive ] [--poll]
                 

Boot a new server.

Positional arguments:
                  Name for the new server

Optional arguments:
….
  --user-data 
                        user data file to pass to be exposed by the metadata
                        server.
….

One thing that is not mentioned in the help docs is that the user-data needs to be a base64 string.
Below, the nova boot command is invoked in debug mode which lists all api calls involved in the process of creating a server

  1. Generate auth token
  2. Get flavor details
  3. Get image details
  4. Create server

The contents of the file /root/user-data are converted to a base64 string and added to the payload object as documented in the official api docs

 
root@controller01:~# cat user-data
#cloud-config
chpasswd:
  list: "root:password1234"
  expire: False

root@controller01:~# nova --debug boot test-cloud-init-5 --flavor 2 --image 636d07f8-e111-4d22-9e31-bba0fcd02f98 --security-groups default --nic net-id=b9449138-3bcd-44a4-a6d5-223922f15843 --user-data /root/user-data

REQ: curl -i 'http://controller01.qa.cloud:5000/v2.0/tokens' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "User-Agent: python-novaclient" -d '{"auth": {"tenantName": "admin", "passwordCredentials": {"username": "admin", "password": "openstack"}}}'

REQ: curl -i 'http://controller01.qa.cloud:8774/v2/2136a657f30c4f65895dd95164f4dda6/images/636d07f8-e111-4d22-9e31-bba0fcd02f98' -X GET -H "X-Auth-Project-Id: admin" -H "User-Agent: python-novaclient" -H "Accept: application/json" -H "X-Auth-Token: b0f8f689c74d4005af9d17f3cc1b5444"

REQ: curl -i 'http://controller01.qa.cloud:8774/v2/2136a657f30c4f65895dd95164f4dda6/flavors/2' -X GET -H "X-Auth-Project-Id: admin" -H "User-Agent: python-novaclient" -H "Accept: application/json" -H "X-Auth-Token: b0f8f689c74d4005af9d17f3cc1b5444"

REQ: curl -i 'http://controller01.qa.cloud:8774/v2/2136a657f30c4f65895dd95164f4dda6/servers' -X POST -H "X-Auth-Project-Id: admin" -H "User-Agent: python-novaclient" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: b0f8f689c74d4005af9d17f3cc1b5444" -d '{"server": {"name": "test-cloud-init-5", "imageRef": "636d07f8-e111-4d22-9e31-bba0fcd02f98", "flavorRef": "2", "user_data": "I2Nsb3VkLWNvbmZpZwpjaHBhc3N3ZDoKICBsaXN0OiAicm9vdDpwYXNzd29yZDEyMzQiCiAgZXhwaXJlOiBGYWxzZQo=", "max_count": 1, "min_count": 1, "networks": [{"uuid": "b9449138-3bcd-44a4-a6d5-223922f15843"}], "security_groups": [{"name": "default"}]}}'