Hi, I getting started with DigitalOcean V2 API. When I tried create droplet with user data via API, server is created without executed my code. The same code when I did pasted in the browser and created droplet working properly.
This is my code:
$droplet->create('the-name',
'nyc1',
'512mb',
'ubuntu-16-04-x64',
false,
false,
false,
array($key),
'
#!/bin/bash
sudo apt-get update
debconf-set-selections <<< "postfix postfix/mailname string domain.com"
debconf-set-selections <<< "postfix postfix/main_mailer_type string \'Internet Site\'"
apt-get install -y postfix
sudo postconf -e \'home_mailbox= Maildir/\'
sudo postconf -e \'virtual_alias_maps= hash:/etc/postfix/virtual\'
printf "kontakt@e-cleaner.net.pl root" > /etc/postfix/virtual
sudo postmap /etc/postfix/virtual
sudo systemctl restart postfix
sudo apt-get update
sudo ufw allow Postfix
echo \'export MAIL=~/Maildir\' | sudo tee -a /etc/bash.bashrc | sudo tee -a /etc/profile.d/mail.sh
sudo apt-get install s-nail
echo \'init\' | mail -s \'init\' -Snorecord
What is the reason of my problem?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
I don’t see any closing ');, but I guess you just forgot to copy that.
I would guess it has something to do with the escaping of the '. Try changing all the \' to " and \'Internet Site\' to \"Internet Site\".
But from what I can remember, you need to #!/bin/bash on the first line - right now it’s on the second line.
Try running this command on the server to see what the user-data actually is:
curl -w "\n" http://169.254.169.254/metadata/v1/user-data
https://www.digitalocean.com/community/tutorials/an-introduction-to-droplet-metadata
Instead of passing the data directly, I’d recommend passing it as a variable and using built-in functions such as file_get_contents.
For example:
<?php
$data = file_get_contents( '/path/to/userdata.sh' );
$droplet->create(
'the-name',
'nyc1',
'512mb',
'ubuntu-16-04-x64',
false,
false,
false,
[ $key ],
$data
);
You’d then create a new file called userdata.sh and pass the path to file_get_contents.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.