Manage BareMetal servers with Scaleway CLI

Post image

TERMINAL CLI LOGO

Scaleway CLI (Command Line Interface) goes live and is ready to help you manage your Scaleway cloud environment. The command line interface is an essential tool to operate in a cloud environment as it allows you to manage your resources faster. Scaleway CLI is pretty simple, you can log in, create servers, attach volumes, move IPs, pipe the result of a command to another one and much more!

Getting started

Requirements

To run Scaleway CLI, you need to have go. You can get it from the official Golang website.

Installation

To install it, simply run:

$ GO15VENDOREXPERIMENT=1 go get -u github.com/scaleway/scaleway-cli/cmd/scw
Configuration

Configure the CLI to use your token and organization ID. You can get them in the credentials section of the console (the organization ID is sometimes named access key).

$ scw login --token=<your_token_id> --organization=<your_organization_id>

Inspired from the Docker CLI

The Scaleway CLI is inspired from the Docker CLI, if you are familiar with it, you will recognize that many commands are similar. For instance, scw attach <server_id> attaches to the serial console of a running server, scw ps lists your servers and scw rm <server_id> removes one or more servers. The main difference is that instead of operating on containers, you operate on BareMetal SSD servers.

Showcase

As a showcase, we'll create a farm of nginx servers serving static content using Scaleway CLI and shell scripting. One front end server will act as a load balancer and proxify requests to nodes serving static content.

So we start by creating one server called my-static-farm-front and 20 nodes called my-static-farm-node-$i:

FRONTEND_ID=$(scw create --name my-static-farm-front Ubuntu_Trusty_14_04_LTS)  
for i in {1..20}  
do  
     scw create --name my-static-farm-node-$i Ubuntu_Trusty_14_04_LTS >> /tmp/farm-uuids.txt
done  

Like the Docker CLI, the scw create command outputs a unique identifier representing the created entity, we redirect its output to a temporary file, so that we can easily script around. Now, we can boot all servers:

# start all servers
for server in $(cat /tmp/farm-uuids.txt) $FRONTEND_ID  
do  
      scw start -s $server &
done  
wait $(jobs -p)  

The scw start -s argument waits for the SSH port of the server to be up, since it's not an immediate action, we run all starts in background. We then wait for all servers to be up using wait $(jobs -p).

The next step is to install nginx:

# install nginx
for server in $(cat /tmp/farm-uuids.txt) $FRONTEND_ID  
do  
      scw exec $server 'apt-get update && apt-get install -q -y nginx' < /dev/null &
done  
wait $(jobs -p)

# add a dummy static content on nodes
for server in $(cat /tmp/farm-uuids.txt) $FRONTEND_ID  
do  
      scw exec $server 'echo HelloWorld > /usr/share/nginx/html/index.html' < /dev/null &
done  
wait $(jobs -p)

At this stage, our farm is mostly ready. We still need to configure the frontend to act as a load balancer, so we need to fetch the IP addresses of our new servers:

(
    echo "upstream static-nodes {"
    for server in $(cat /tmp/farm-uuids.txt)
    do
        ip=$(scw inspect $server -f '.server.public_ip.address')
        echo "    server ${ip};"
    done
    echo "}"
    echo "server {"
    echo "        listen 80;"
    echo "        location / {"
    echo "                proxy_pass http://static-nodes;"
    echo "        }"
    echo "}"
) | scw exec $FRONTEND_ID 'cat > /etc/nginx/sites-enabled/default'

scw exec $FRONTEND_ID 'service nginx reload'  

Et voilà! Our farm is now up and running, the frontend is available at:

echo "http://$(scw inspect $FRONTEND_ID -f '.server.public_ip.address')"  

More

The source code is available online on GitHub. Comments, feature requests and contributions are always welcome!

Sign up now to try the Scaleway CLI!

Author image

Sébastien Rannou