Registering a DreamCompute server with ServerPilot using Shade

This article assumes you have

Authentication

Start by setting the environment variables that tell Shade how to authenticate to Dreamcompute, do this by running the following and typing in your password when it asks for it:

[user@localhost]$ source openrc.sh

Getting a server ID and server API key from ServerPilot

The first step in this process is to request a new server ID and server API key from ServerPilot. You also set the name of the server you want to create here.

import requests
import shade
import json

client_id = 'CLIENT ID GOES HERE'
api_key = 'API KEY GOES HERE'
server_name = 'serverpilot'

Next you make the request to the ServerPilot API to create new a new server ID and API key.

server_info = json.loads('{"name": "' + server_name + '"}')
server_endpoint = 'https://api.serverpilot.io/v1/servers'

session = requests.Session()
session.auth = (client_id, api_key)
session.headers = {'Content-Type': 'application/json'}
response_raw = session.post(server_endpoint, json.dumps(server_info))
print(response_raw.content)
response_json = json.loads(response_raw.content)

Now you have a server ID and server API key stored in the response_json dictionary that you can use with the server you want to register with ServerPilot.

Launching a server and registering it with ServerPilot

Pass the ServerPilot installer as a task to be executed as soon as the new server is created, using cloud-init:

cloud_init='''#!/bin/bash
sudo apt-get update && sudo apt-get -y install wget ca-certificates && \
sudo wget -nv -O serverpilot-installer \
https://download.serverpilot.io/serverpilot-installer && \
sudo sh serverpilot-installer \
--server-id={serverid} \
--server-apikey={serverapikey}
'''.format(serverid=response_json['data']['id'], serverapikey=response_json['data']['apikey'])

Then set variables for the image, flavor, and key pair to launch the server with.

image_name = 'Ubuntu-16.04'
flavor_id = '100'
key_name = 'KEY NAME GOES HERE'

Change the key_name file to be the name of your key pair on DreamCompute so that you can SSH into the server. The image and flavor_id variables can also be modified to deploy a different image or a different size server.

Finally, connect to DreamCompute with Shade and request the building of the server. For more information about Shade, read our documentation on how to use Shade with DreamCompute.

conn = shade.OpenStackCloud()

image = conn.get_image(image_name)
conn.create_server(image=image, flavor=flavor_id,
        name=server_info['name'], network='public', userdata=cloud_init,
        key_name=key_name)

Once the script runs and finishes, go to ServerPilot.io, click on servers and you should see your new server (it may take up to a couple minutes for the installation script to finish). You can now use ServerPilot to manage your server and deploy applications on it.

Full script

# step-1
import requests
import shade
import json

client_id = 'CLIENT ID GOES HERE'
api_key = 'API KEY GOES HERE'
server_name = 'serverpilot'

# step-2
server_info = json.loads('{"name": "' + server_name + '"}')
server_endpoint = 'https://api.serverpilot.io/v1/servers'

session = requests.Session()
session.auth = (client_id, api_key)
session.headers = {'Content-Type': 'application/json'}
response_raw = session.post(server_endpoint, json.dumps(server_info))
print(response_raw.content)
response_json = json.loads(response_raw.content)

# step-3
cloud_init='''#!/bin/bash
sudo apt-get update && sudo apt-get -y install wget ca-certificates && \
sudo wget -nv -O serverpilot-installer \
https://download.serverpilot.io/serverpilot-installer && \
sudo sh serverpilot-installer \
--server-id={serverid} \
--server-apikey={serverapikey}
'''.format(serverid=response_json['data']['id'], serverapikey=response_json['data']['apikey'])

# step-4
image_name = 'Ubuntu-16.04'
flavor_id = '100'
key_name = 'KEY NAME GOES HERE'

# step-5
conn = shade.OpenStackCloud()

image = conn.get_image(image_name)
conn.create_server(image=image, flavor=flavor_id,
        name=server_info['name'], network='public', userdata=cloud_init,
        key_name=key_name)

Did this article answer your questions?

Article last updated .