Question

How do I wait for a new droplet to be ready for ssh using the API (DropletKit)?

I’m currently using the V2 API to spin up new droplets based on my own custom snapshot. When I run the droplets.create call via DropletKit it returns relatively quickly with the public ip.

The next step is for me to ssh into the machine and kick off a bash script. Unfortunately, I’m seeing ssh timeouts because the droplet is still powering on, which is causing my scripts to fail.

If I need to wait for a droplet to become accessible via SSH, what’s the recommended way of doing this?

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I’ve been running across this problem with all sorts of other actions. For example, deleting a droplet returns as “OK, DigitalOcean will delete this soonish”, but there is no “Actions->Wait()” function to block until the actions queue is empty. This leads to lots of polling, slow scripts, and having to explain to management why DigitalOcean takes so much longer to configure than AWS.

@wiq

The API doesn’t provide exact information on whether a specific service is up, running, or accessible though you can check the status a few different ways instead of simply trying to connect until it’s up.

If you use nmap, you can check for return status using something such as:

nmap DROPLET_IP -PN -p ssh | egrep 'open|closed|filtered' | awk '{print $2}'

When SSH isn’t open/available it’ll return either closed or filtered and, when open and able to accept connection, it’ll return open.

You would then run your while loop until open is returned. Using this method, you shouldn’t need to worry about SSH Keys or passwords as it’s simply checking the port itself, not actually connecting to it.

@Woet yes that works (I’m currently using net-ssh with a timeout that does that). I suppose my question is more… does the API give any information about it’s connectivity status? Or is polling the go to strategy here?

Sleep for 60 seconds, then use a while loop to check whether SSH responds every 5 seconds or so.