We’re excited to announce the support for server contextualization. This means that you can post various meta data to either your server or drive definition (via the API), and then read it back from within the actual server.
This has been a popular feature request, since it is applicable to a wide array of tasks. Two of the most common use cases for this would be to either push SSH keys or software licenses to a virtual machine, but it can be used for a lot of other things too.
There are really two distinct elements to server contextualization: reading and writing. The writing part is done via the API (and will be implemented in the web console later).
The reading part on the other hand is performed on the actual server via a simulated serial device (and hence does not require any network interface).
Writing meta data
In order to write meta data to a given virtual machine, you need to either use the Server or Drive API call. To simplify the above operations, you can use our Python library. An example on how to write the data can be found there too.
Please note that currently your server needs to be stopped before you can write meta data to it; we are working on being able to update meta data to running servers so stayed tuned.
Reading meta data
In order to read back the actual meta data, you need to read from the serial device on the server. Since it’s a standardized serial device, more or less every language has support for reading from it. All you need to do is to send a trigger, and you will get the data formatted as JSON back.
Again, using the Python library, this task can be significantly simplified.
A real world example
As we alluded to above, one common use case for this is to push SSH keys to a server in order to streamline the installation of a new virtual machine. We’re going to use the Python library and assume that you have that set up and working.
With the library installed, let’s create a new server and push an SSH key to it.
[python light=”true”] import cloudsigmadrive = cloudsigma.resource.Drive()
server = cloudsigma.resource.Server()
test_disk = {
‘name’: ‘Test Drive’,
‘size’: 1073741824 * 5,
‘media’: ‘disk’
}
my_test_disk = drive.create(test_disk)
test_server = {
‘name’: ‘Test Server’,
‘cpu’: 1000,
‘mem’: 512 * 1024 ** 2,
‘vnc_password’: ‘test_server’
}
test_server[‘drives’] = [ {
‘boot_order’: 1,
‘dev_channel’: ‘0:0’,
‘device’: ‘virtio’,
‘drive’: my_test_disk[‘uuid’]
} ]
test_server[‘nics’] = [ {
‘ip_v4_conf’: {
‘conf’: ‘dhcp’,
‘ip’: None
},
‘model’: ‘virtio’,
‘vlan’: None
} ]
test_server[‘meta’] = {
‘ssh_key’: ‘<insert your SSH key>’
}
my_test_server = server.create(test_server)
[/python]
This will create a server for you with a (1GHz, 0.5GB RAM, 5GB disk). However, you will need to install your operating system of choice onto the disk.
Once you have installed your operating system (or pulled a ready system from our marketplace), you can login to your newly created server and run our sample script that extracts and installs your SSH key. In order to run the script below, you need to have Curl and our Python library installed on the server, the setup instruction for which will differ depending on your distribution.
With everything installed, this script will fetch the SSH key from the meta data and properly install it on the server.
[python light=”true”] import cloudsigmaimport os
import stat
metadata = cloudsigma.metadata.GetServerMetadata().get()
ssh_key = metadata[‘meta’][‘ssh_key’]
# Define paths
home = os.path.expanduser(‘~’)
ssh_path = os.path.join(home, ‘.ssh’)
authorized_keys = os.path.join(ssh_path, ‘authorized_keys’)
def get_permission(path):
return oct(os.stat(ssh_path)[stat.ST_MODE])[-4:]
if not os.path.isdir(ssh_path):
print ‘Creating folder %s’ % ssh_path
os.makedirs(ssh_path)
if get_permission(ssh_path) != 0700:
print ‘Setting permission for %s’ % ssh_path
os.chmod(ssh_path, 0700)
# We’ll have to assume that there might be other keys installed.
# We could do something fancy, like checking if the key is installed already,
# but in order to keep things simple, we’ll simply append the key.
with open(authorized_keys, ‘a’) as auth_file:
auth_file.write(ssh_key + ‘n’)
if get_permission(authorized_keys) != 0600:
print ‘Setting permission for %s’ % authorized_keys
os.chmod(authorized_keys, 0600)
[/python]
Alternatively, you can download the above script from here.
If you’re having trouble with any of the above, please take a look at the detailed instruction here.
If you are using this feature please comment and let us know. Likewise if you are doing something particularly creative with the meta data feature it would be great to share this with other customers.
Share this Post
- Manage Docker resources with Cgroups - May 12, 2015
- Docker, Cgroups & More from ApacheCon 2015 - April 30, 2015
- How to setup & optimise MongoDB on public cloud servers - March 24, 2015
- Presentation deck from CloudExpo Europe - March 17, 2015
- CoreOS is now available on CloudSigma! - March 10, 2015