6
votes

I am configuring an OpenStack box using cloud-init/cloud-config. I intend to wait until it is fully configured before I start using it.

This is not all that hard to do using some marker file or detecting if the cloud-init process is still running, though it seems quite cumbersome to do that in every cloud-init script. Is there some recommended way? Natively supported by cloud-init, ideally?

5

5 Answers

2
votes

As @flyxiao pointed out, cloud-init put status information into a dedicated directory on a filesystem: /run/cloud-init/ (preferred over /var/lib/cloud/data/ as it is guaranteed to describe last init process). status.json contains detailed about all init phases and result.json denotes the whole init is completed. The project documentation suggest a python script to detect cloud-init completion:

 fin = "/run/cloud-init/result.json"
 if os.path.exists(fin):

   ret = json.load(open(fin, "r"))

   if len(ret['v1']['errors']):

     print "Finished with errors:" + "\n".join(ret['v1']['errors'])

   else:

     print "Finished no errors"

 else:

   print "Not Finished"
2
votes

The simplest answer is to set a tag on the instance so you can poll for its existence.

If you have a Linux host, do this last:

aws ec2 create-tags --resources `ec2metadata --instance-id` --tags Key=BootstrapStatus,Value=complete

This avoids needing to set up a network endpoint, creating a point of failure, or SSHing in, creating a need to secure credentials.

1
votes

You can check the /var/lib/cloud/data/status.json for cloud-init status. Or if the host is using upstart, add one init process in /etc/init/newprocess.conf and newprocess.conf should be started after cloud-final.

1
votes

Another alternative is to let the cloud-init phone home once it finishes.

1
votes

The following command does the trick:

cloud-init status --wait

From https://ubuntu.com/blog/cloud-init-v-18-2-cli-subcommands:

cloud-init status gives simple human-readable or programmatic output for what cloud-init is doing and whether it has finished successfully. It can be used as a sanity check on a machine or in scripts to block until cloud-init has completed successfully.