User Data

When provisioning a server on the Hivelocity Bare Metal Cloud, you have the option to include User Data, which can be used to perform various automation tasks after the server is ready, such as executing scripts or bootstrapping your applications.

User data files can be bash scripts, PowerShell scripts, or cloud-init files. Cloud-init is a simple but powerful technique for carrying out actions on a cloud instance at boot time. It takes care of defining each machine’s hostname, adds SSH keys, and bootstrap your applications.

Usage

Userdata takes two different forms:

User Data Script

  • Typically used by those who just want to execute a shell script.
  • Begins with: #! for Linux shell or #ps1 for Windows PowerShell.

Cloud Config Data

  • Uses valid yaml syntax
  • Begins with: #cloud-config

When provisioning a new server from the myVelocity Portal, you will find the option to add userdata at the bottom of the deploy page.

22102210

If you want to provision a server via our API, you can add userdata by simply using the script key in the body of the call.

curl -X 'POST' \
  'https://core.hivelocity.net/api/v2/bare-metal-devices/' \
  -H 'accept: application/json' \
  -H 'X-API-KEY: $API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
  "osName": "CentOS 7.x",
  "productId": 576,
  "hostname": "your.hostname.com",
  "period": "hourly",
  "locationName": "NYC1",
  "script": "YOUR SCRIPT HERE AS A STRING"
}'

Examples

Linux

#cloud-config
package_upgrade: true
packages:
    -  nginx
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
apt-get update && apt-get upgrade -y
apt-get install nginx -y
#ps1
New-Item C:/Users/Admin test.txt
Set-Content C:/Users/Admin/test.txt "Hello world"

$password = ConvertTo-SecureString "mysecurestringpass" -AsPlainText -Force
New-LocalUser -Name "hvAccount1" -Password $password

Caveats

Windows can accept cloud-config scripts, but has some limitations compared to Linux. See Cloudbase-init's documentation on user data for a more information on cloud-config scripts.

Troubleshooting Tips

Please note that the length of time it takes to execute could vary depending on the number of commands in your script. Sometimes the script will need more time to execute after first seeing the server available in the portal.

Any form of user data will only run on the initial provision. If you wish to rerun the script again on a reload of the same server, you will have to pass in the script again.

It is recommend you develop and test your userdata files in a local environment such as Vagrant or Docker to speed up the development cycle.


What’s Next
Did this page help you?