Install the Python OpenStack Client
This article will assume you're using a recent Ubuntu release on the command line. You may need to install the openstack client via other means if you use another distribution
sudo apt install python3-openstackclient
Create an API user
From the Cloud Control Panel:
- Click Cloud > API users
- If you have not yet created one, click the + button in the bottom right.
- Download your openrc file by selecting the API user you have created and click the Get OpenRC file button under Actions. This will download a file we'll be using for authorization.
All API usernames will be prefixed with your client ID, so if your API username is "osuser", the username for login will be something like "RN-12345_osuser". This will be displayed in the Name field and in the openrc file.
NOTE: SSH Keys uploaded by an API user are separate from the ones displayed in the cloud control panel. You will need to upload your public key information using the API prior to being able to use your keys during new instance creation.
Test access
From your terminal:
- Type
source openrc
to load the OpenStack configuration information. - You will be prompted for your API User's password configured in the last step.
- Next, try to list the available flavors for your region using
openstack flavor list
. If you see a list of public flavors (i.e. 2GB SKVM, 8GB VDS, etc) then you have correctly configured your API user's access for this environment. - You can edit the
OS_REGION_NAME
in this file to point to any of our regions: NYC, ATL, LA, SEA, or NL.
This is all you need to do if you want to stick with command line usage of the cloud.
Python SDK usage
You can interact with our cloud using Python code as well. The openstack library will use environment variables loaded from your openrc file above, or a cloud config file (described below).
Import and connect
To connect using openrc environment variables:
import openstack
conn = openstack.connect()
To connect using a cloud config file:
import openstack
conn = openstack.connect(cloud="ramnode", region_name="NYC")
You can have multiple connections to different regions at the same time from the same application if you specify a new region_name
for each.
Upload your public key
You should upload a public key to allow access to your new instances. This example assumes your public key is located at ~/.ssh/id_openstack.pub
.
with open(os.path.expanduser("~/.ssh/id_openstack.pub")) as keyfile:
key_data = keyfile.read().strip()
conn.compute.create_keypair(name="mykey", public_key=key_data)
Find flavor, image, network, security group, and key
You will need a flavor, image, network, security group, and key object to create a server. Finding by name works across all regions.
flavor = conn.compute.find_flavor("2GB PKVM")
image = conn.compute.find_image("Ubuntu 20.04 Server Cloud")
network = conn.network.find_network("Public")
security_group = conn.network.find_security_group("firewall")
key = conn.compute.find_keypair("mykey")
Create a server
With a flavor, image, network, security group, and key, you have enough information to create a server.
server = conn.compute.create_server(name="mynewserver", flavor_id=flavor.id, image_id=image.id, networks=[{"uuid": network.id}], security_groups=[{'name': security_group.name}], key_name=key.name)
server = conn.compute.wait_for_server(server) # waiting is not necessary unless you intend to perform further operations on the server post-creation, or gather more information such as the IP assigned.
You should now be able to ssh to your new server.
Using a cloud config file instead of environment variables (optional)
It is sometimes favorable to configure environment variables at run time instead of prior to loading your script via openrc files. The Python SDK can read configurations from a clouds.yaml
file, read by default from ~/.config/openstack/clouds.yaml
. Below is an example file configured for our cloud using information gathered from the openrc file you downloaded above:
clouds:
ramnode:
auth:
username: RN-12345_myapiuser
password: myapiuserpassword
project_id: 0123456789abcdef0123456789abcdef
auth_url: https://nyc-controller.ramnode.com:5000/v3
user_domain_name: Default
regions:
- NYC
- SEA
- LA
- ATL
- NL
NOTE: All regions use nyc-controller for authorization - do NOT change the auth_url
.
Further reading
The following urls can provide additional information about CLI and SDK access:
- OpenStack CLI - https://docs.openstack.org/python-openstackclient/latest/
- OpenStack Python SDK - https://docs.openstack.org/openstacksdk/latest/user/index.html