Skip to content

Instantly share code, notes, and snippets.

@chmouel
Created November 23, 2010 12:35
Show Gist options
  • Save chmouel/711681 to your computer and use it in GitHub Desktop.
Save chmouel/711681 to your computer and use it in GitHub Desktop.
Create Cloud Servers via API and customize it via SSH
#!/usr/bin/python
# -*- encoding: utf-8 -*-
#
# Chmouel Boudjnah <chmouel@chmouel.com>
import os
import cloudservers
from subprocess import call
API_USER=""
API_KEY=""
IMAGE_NAME=""
IMAGE_FLAVOUR_ID=1
IMAGE_TYPE=69
def check_image(cnx, server_id):
while cnx.servers.get(server_id).status != "ACTIVE":
print ". ",
if cnx.servers.get(server_id).status == "ACTIVE":
return
cnx = cloudservers.CloudServers(os.environ['RCLOUD_API_USER'],
os.environ['RCLOUD_API_KEY'])
print "Creating Image.",
cstype = cnx.servers.create(image=IMAGE_TYPE,
flavor=IMAGE_FLAVOUR_ID,
name=IMAGE_NAME,
files={'/root/.ssh/authorized_keys' : open(os.path.expanduser("~/.ssh/id_rsa"), 'r')})
check_image(cnx, cstype.id)
print "done."
print "customizing it..."
public_ip=cstype.public_ip
ssh_options = ['-q', '-t', '-o StrictHostKeyChecking=no', '-o UserKnownHostsFile=/dev/null']
call('ssh %s root@%s "sudo apt-get -y install curl && curl http://www.chmouel.com/pub/bootstrap.sh|sh -"' % (" ".join(ssh_options), public_ip), shell=True)
print "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment