Created
November 23, 2010 12:35
-
-
Save chmouel/711681 to your computer and use it in GitHub Desktop.
Create Cloud Servers via API and customize it via SSH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- encoding: utf-8 -*- | |
# | |
# Chmouel Boudjnah <[email protected]> | |
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