How to run user_data after droplet creation

November 23, 2014 1.6k views

Facing an error with user_data. After droplet creation nginx is not installed. I wanted the following coding to run on my server after droplet creation with the use user data

#!/bin/bash

apt-get -y update
apt-get -y install nginx
export HOSTNAME=$(curl -s http://169.254.169.254/metadata/v1/hostname)
export PUBLIC_IPV4=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
echo Droplet: $HOSTNAME, IP Address: $PUBLIC_IPV4 > /usr/share/nginx/html/index.html

so i used that code in

$userData = "#!/bin/bash
apt-get -y update
apt-get -y install nginx
export HOSTNAME=$(curl -s http://169.254.169.254/metadata/v1/hostname)
export PUBLIC_IPV4=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
echo Droplet: $HOSTNAME, IP Address: $PUBLIC_IPV4 > /usr/share/nginx/html/index.html";

For passing the data to the api i use this code below

$data = array("name" => "NewDroplet", "region" => "ams3", "size" => "512mb", "image" => "ubuntu-14-04-x64", "user_data" => $userData);

I am new to coding with api :-)
I dont know what i am doing wrong.

2 Answers

It's hard to know exactly where your error is without seeing the rest of the code or the error logs. In general, you seem to going in the right direction. Here's a working example:

<?php
$user_data = <<<EOD
#!/bin/bash
apt-get -y update
apt-get -y install nginx
export HOSTNAME=$(curl -s http://169.254.169.254/metadata/v1/hostname)
export PUBLIC_IPV4=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
echo Droplet: \$HOSTNAME, IP Address: \$PUBLIC_IPV4 > /usr/share/nginx/html/index.html
EOD;

$data = array(
    'name'               => 'nginx-droplet',
    'region'             => 'nyc3',
    'size'               => '512mb',
    'image'              => 'ubuntu-14-04-x64',
    'user_data'          => $user_data
);

$data_string = json_encode($data);

$ch = curl_init('https://api.digitalocean.com/v2/droplets');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer YOUR_TOKEN_API_HERE',
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);

$result = curl_exec($ch);
print_r($result);
?>

Note that the $HOSTNAME is escaped like \$HOSTNAME in order to prevent PHP from attempting to treat it as a variable.

For more information on the DigitalOcean metadata service see: An Introduction to Droplet Metadata

You might also want to check out the community contributed PHP bindings for DgitialOcean's APIv2.

Update CloudInit link and conclusion Metadata is a service provided to DigitalOcean droplets that allows a droplet to access data about itself, i.e. its metadata. Examples of available droplet metadata include user-provided user data, droplet ID, data center region,...

Thanks .
I got an error with this coding though as i dint get the hostname and ip to show on the html file
this was the coding used in v 1 api now i am using v2
export HOSTNAME=$(curl -s http://169.254.169.254/metadata/v1/hostname)
export PUBLICIPV4=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
echo Droplet: \$HOSTNAME, IP Address: \$PUBLIC
IPV4 > /usr/share/nginx/html/index.html

as i am using v2 api now ,How can i get the hostname with the new API . The new api is not easy to understand for me as i am beginner in coding.
Any help would be appreciated
Thanks.

Have another answer? Share your knowledge.