15

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?

Oliver Gondža
  • 3,386
  • 4
  • 29
  • 49

5 Answers5

18

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.

Maximilian Hils
  • 6,309
  • 3
  • 27
  • 46
6

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

phone_home:
    url: https://example.com/$INSTANCE_ID/
    post:
        - pub_key_dsa
        - instance_id
        - fqdn
    tries: 10
Oliver Gondža
  • 3,386
  • 4
  • 29
  • 49
3

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"
Oliver Gondža
  • 3,386
  • 4
  • 29
  • 49
  • Thank you, very useful. Also needs to be checked that cloud-init is installed and enabled IMO :) .. in case somebody is using a mix of images with and without cloud-init. – akostadinov Mar 03 '16 at 13:11
  • Apparently, there is now a dedicated command for that - `cloud-init status`. More information is available [here](https://ubuntu.com/blog/cloud-init-v-18-2-cli-subcommands). – Alexander Kucheryuk Jun 16 '20 at 10:42
3

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.

Michael Labbé
  • 12,017
  • 4
  • 27
  • 36
2

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.

Oliver Gondža
  • 3,386
  • 4
  • 29
  • 49
flyxiao
  • 29
  • 4
  • I am using cloud-init 0.7.6 on RHEL and there is no `newprocess.conf` file. Can you provide documentation link? I will have a look at `/var/lib/cloud/data/*.json`. – Oliver Gondža Feb 18 '16 at 11:43
  • Maybe /var/lib/cloud/data/*.json matches you, i need clear newprocess.conf solution. If your RHEL uses the upstart as the OS init, you may change /etc/init/cloud-init-local.conf (remove one temp file), /etc/init/cloud-final.conf (generate one temp file). if your RHEL uses the systemd as the OS init. Change cloud-init-local.service and cloud-final.service. – flyxiao Feb 19 '16 at 06:47