Created
May 10, 2011 15:12
-
-
Save Kami/964654 to your computer and use it in GitHub Desktop.
This file contains 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
import os | |
import sys | |
import time | |
from os.path import join as pjoin | |
import warnings | |
warnings.filterwarnings('ignore') | |
sys.path.insert(0, '/Users/kami/Projects/Programming/Python/libcloud-trunk') | |
import libcloud.security | |
libcloud.security.VERIFY_SSL_CERT = True | |
from libcloud.compute.types import Provider as ProviderCompute | |
from libcloud.compute.providers import get_driver as get_driver_compute | |
from libcloud.storage.types import Provider as ProviderStorage | |
from libcloud.storage.providers import get_driver as get_driver_storage | |
from libcloud.resource.lb.types import Provider as ProviderLb | |
from libcloud.resource.lb.providers import get_driver as get_driver_lb | |
from libcloud.compute.types import NodeState | |
from libcloud.resource.lb.base import LBNode | |
from libcloud.storage.types import ContainerAlreadyExistsError | |
import secrets | |
SLEEP = 5 | |
SIZE_ID = 't1.micro' | |
IMAGE_ID = 'ami-f5bfefb0' | |
NODE_NAME_PREFIX = 'libcloud-demo' | |
CONTAINER_NAME = 'demo_app' | |
APP_FILE = 'hello.js' | |
BALANCER_NAME = 'demo_lb' | |
conn_ec2 = get_driver_compute(ProviderCompute.EC2_US_WEST)(secrets.EC2_ACCESS_ID, | |
secrets.EC2_SECRET_KEY) | |
conn_cf = get_driver_storage(ProviderStorage.CLOUDFILES_US)(secrets.CF_USERNAME, | |
secrets.CF_API_KEY) | |
conn_rs = get_driver_lb(ProviderLb.RACKSPACE)(secrets.CF_USERNAME, | |
secrets.CF_API_KEY) | |
def upload_app(app_path): | |
print '-> Uploading app to CloudFiles' | |
try: | |
container = conn_cf.create_container(container_name=CONTAINER_NAME) | |
container.enable_cdn() | |
except ContainerAlreadyExistsError: | |
container = conn_cf.get_container(container_name=CONTAINER_NAME) | |
obj = container.upload_object(file_path=app_path, object_name=APP_FILE) | |
return obj | |
def boot_and_setup_nodes(number, app_url): | |
print '-> Booting %d nodes on EC2' % (number) | |
nodes = [] | |
size = [size for size in conn_ec2.list_sizes() if | |
size.id == SIZE_ID][0] | |
image = [image for image in conn_ec2.list_images() if | |
image.id == IMAGE_ID][0] | |
script_content = open('script.sh').read() | |
for index in range(0, number): | |
name = '%s-%d' % (NODE_NAME_PREFIX, index + 1) | |
script = script_content % {'app_url': app_url, 'server': name, | |
'app_file': APP_FILE} | |
node = conn_ec2.create_node(name=name, size=size, image=image, | |
ex_userdata=script, ex_keyname='libcloud_demo') | |
nodes.append(node) | |
print '-> %d nodes created' % (number) | |
return nodes | |
def setup_load_balancers(nodes): | |
print '-> Creating load balancer at Rackspace' | |
lb_nodes = [] | |
for node in nodes: | |
lb_nodes.append(LBNode(id=node.id, ip=node.public_ip[0], port=80)) | |
print lb_nodes | |
balancer = conn_rs.create_balancer(name=BALANCER_NAME, port=80, nodes=lb_nodes) | |
print '-> Balancer created and all the nodes attached' | |
print '-> Balancer is listening on http://%s:%s' % (balancer.ip, balancer.port) | |
return balancer | |
def wait_until_running(expected): | |
print '-> Waiting for all nodes to become active' | |
running = [] | |
nodes = conn_ec2.list_nodes() | |
while len(running) != expected: | |
for node in nodes: | |
if node.state == NodeState.RUNNING and node not in running: | |
running.append(node) | |
if len(running) == expected: | |
return running | |
nodes = conn_ec2.list_nodes() | |
print 'Not all nodes yet active, sleeping %d seconds...' % (SLEEP) | |
time.sleep(SLEEP) | |
return running | |
def main(): | |
obj = upload_app(pjoin(os.getcwd(), APP_FILE)) | |
obj_cdn_url = obj.get_cdn_url() | |
boot_and_setup_nodes(number=2, app_url=obj_cdn_url) | |
nodes = wait_until_running(expected=2) | |
balancer = setup_load_balancers(nodes) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment