Introducing Scaleway Cloud-Init Support

Post image

Today we are glad to introduce cloud-init support on Scaleway. Cloud-init lets you configure your server during the first boot in an automatic and repeatable way. In this blog post we will showcase how to get started with Scaleway user data and cloud-init.

Scaleway Metadata and User Data

Scaleway Metadata is a service that allows a server to retrieve information about itself. With server metadata, you can retrieve your server physical position, server id, ssh public keys, tags, etc..

Important: you can only access server metadata from the server itself.

You can fetch your server metadata running the following commands:

  • scw-metadata returns server metadata in plain text:
NAME=scw-f5bccf  
TAGS=0  
STATE_DETAIL=booted  
HOSTNAME=scw-f5bccf  
...
  • scw-metadata-json returns server metadata in JSON format:
{
   "name" : "scw-f5bccf",
   "tags" : [],
   "state_detail" : "booted",
   "hostname" : "scw-f5bccf"
   ...
}

These data are generated dynamically using your server and account information. You can not modify them, they are read-only.

However, in addition to server metadata, you can provide extra data using user data your server will be able to fetch. For instance, you can use user data to dynamically configure your server with cloud-init.

Create User Data

To create a new user data, you have two solutions:

1. From your server

You can create user data on your server running the following command:

$ scw-userdata hello world

The command above creates a new User Data with:

  • key: hello
  • value: world

To retrieve the previously created User Data run:

$ scw-userdata hello
world  

You can list all the User Data keys running:

$ scw-userdata

USER_DATA=1  
USER_DATA_0=hello  

2. With Scaleway CLI from anywhere

You can create User Data on your server running the following command:

$ scw _userdata scw-73cf7d hello=world

To retrieve the previously created User Data run:

$ scw _userdata scw-73cf7d hello
world  

You can list all the User Data keys running:

$  scw _userdata scw-73cf7d
hello  

Cloud-Init

Cloud-init is a multi-distribution package that provides boot time customization for cloud servers. You can use cloud-init with Scaleway User Data and perform actions and customizations on your server during the boot time.

Scaleway has worked with the upstream developers of cloud-init to natively enable cloud-init on Scaleway's platform. It is now possible to use cloud-init natively on most of Scaleway's x86_64 images. Debian Jessie lacks some of cloud-init dependancies to be enabled natively. Fedora28 is currently suffering a bug that keeps us from being able to build the image but that should be fixed shortly.

Cloud-init supports multiple user data formats:

  • Scripts (bash, python, perl, etc.)
#!/bin/bash
apt-get update -y -qq  
apt-get upgrade -y -qq  
...
  • Cloud config files
#cloud-config
apt_update: true  
apt_upgrade: true  

Cloud-init comes with a large number of modules for handling command execution, users and groups creation, writing files, configure disks and more. All modules are written in Python. You can find more information on the official documentation website to write your own module.

Cloud-Init on Scaleway

You can provide cloud-init's user_data directly from the console's Advanced settings in the freeform field provided for that purpose.

Following are some examples of what you can do with User Data and cloud-init

Update and Upgrade packages

Execute an apt-get update and apt-get upgrade on startup

#cloud-config
apt_update: true  
apt_upgrade: true  

Install packages

Install nginx, mysql-server, php5-fpm, php5-mysql on the server at bootime

#cloud-config
packages:  
   - nginx
   - mysql-server
   - php5-fpm
   - php5-mysql

Run commands

Execute multiple commands that will be executed in order at rc.local like level with output to the console.

#cloud-config
runcmd:  
   - [ sh, -c, echo "=========hello world=========" ]
   - [ wget, "http://www.scaleway.com", -O, /tmp/index.html ]

Write contents in file

You can also write contents in file specifying a path and a content.

#cloud-config
write_files:  
  - path: /tmp/hello.txt
    content: |
      Hello
      World
      \o/

Test and debug your cloud configuration via the command line

To test your cloud configuration directly via the command line on your server:

  • Initialize cloud-init data
cloud-init -d init  
  • Run the cloud-init final module:
cloud-init -d modules --mode final  
  • See what cloud-init did during the previous boots
cloud-init analyze show  

This post and its examples show you the possibilities that are offered by Scaleway Metadata service. As explained above, cloud-init combined with User Data let you run configuration dynamically at boot time. Of course you can use User Data without cloud-init. User Data is really new and we'd really love to get your feedback about it via our community platform.

You can go deeper with cloud-init checking out the official documentation. If you have any issues with the Scaleway DataSource, please let us know.

Happy Cloud Riding

Author image

Edouard Bonlieu

Strategy and marketing at Online.net & Scaleway