How do detect that cloud-init completed initialization
Asked Answered
F

5

19

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?

Frugivorous answered 8/10, 2015 at 14:46 Comment(0)
R
25

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.

Refute answered 27/8, 2021 at 13:20 Comment(0)
F
7

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
Frugivorous answered 19/4, 2016 at 12:24 Comment(2)
Sounds nice, link is dead. Prime example of why you need to include your answer here on SO.Dieter
Good point, I updated the answer.Conglobate
F
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"
Frugivorous answered 20/2, 2016 at 7:2 Comment(2)
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.Currey
Apparently, there is now a dedicated command for that - cloud-init status. More information is available here.Hyunhz
S
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.

Shovel answered 15/7, 2017 at 2:15 Comment(2)
Right, though it require cloud-init assistance and the machine able to talk to cloud provider.Conglobate
This will have to be run from the userdata (as the last line), which does not guarantee that cloud-init has completed (it may do more after the userdata has been run, such as printing ec2 instance fingerprints, etc)Ammoniac
B
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.

Bellini answered 18/2, 2016 at 8:58 Comment(2)
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.Conglobate
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.Bellini

© 2022 - 2024 — McMap. All rights reserved.