Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do detect that cloud-init completed initialization

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?

like image 217
Oliver Gondža Avatar asked Oct 08 '15 14:10

Oliver Gondža


People also ask

Where can I find cloud-init logs?

Boot Time Analysis - cloud-init analyze log file into formatted and sorted events. It allows for detailed analysis of the most costly cloud-init operations are to determine the long-pole in cloud-init configuration and setup. These subcommands default to reading /var/log/cloud-init. log.

How does cloud-init know first boot?

By default, cloud-init attempts to determine which case it is running in by checking the instance ID in the cache against the instance ID it determines at runtime. If they do not match, then this is an instance's first boot; otherwise, it's a subsequent boot. Internally, cloud-init refers to this behavior as check .

Where are cloud-init files stored?

Cloud-init config is provided in two places: /etc/cloud/cloud. cfg. /etc/cloud/cloud.


2 Answers

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.

like image 174
Maximilian Hils Avatar answered Oct 16 '22 19:10

Maximilian Hils


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"
like image 4
Oliver Gondža Avatar answered Oct 16 '22 17:10

Oliver Gondža