A sample script used as a driving example for the Getting Started tutorial for CloudBridge library. Complete documentation is available in the docs: http://cloudbridge.readthedocs.io/en/latest/getting_started.html
Created
August 2, 2016 19:18
-
-
Save afgane/a7d17d383d573990d0e28717cbf8b100 to your computer and use it in GitHub Desktop.
A complete CloudBridge intro script
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
from cloudbridge.cloud.factory import CloudProviderFactory, ProviderList | |
# Add your cloud provider info, for example: | |
# config = {'aws_access_key': 'access key', | |
# 'aws_secret_key': 'secret key'} | |
# provider = CloudProviderFactory().create_provider(ProviderList.AWS, config) | |
# image_id = 'ami-2d39803a' # Ubuntu 14.04 (HVM) | |
# Create a key pair | |
kp = provider.security.key_pairs.create('CB-intro') | |
with open('CB-intro.pem', 'w') as f: | |
f.write(kp.material) | |
import os | |
os.chmod('CB-intro.pem', 0400) | |
# Create and configure a private network | |
n = provider.network.create('CB-intro') | |
sn = n.create_subnet('10.0.0.1/28', 'CB-intro') | |
# Create a security group | |
sg = provider.security.security_groups.create('CB-intro', 'SG created by CB', n.id) | |
sg.add_rule('tcp', 22, 22, '0.0.0.0/0') | |
# Launch an instance | |
img = provider.compute.images.get('ami-2d39803a') # Ubuntu 14.04 (HVM) in AWS us-east-1 | |
inst_type = sorted([t for t in provider.compute.instance_types.list() if t.vcpus >= 2 and t.ram >= 4], key=lambda x: x.vcpus*x.ram)[0] | |
lc = provider.compute.instances.create_launch_config() | |
lc.add_network_interface(n.id) | |
inst = provider.compute.instances.create('CB-intro', img, inst_type, key_pair=kp, security_groups=[sg], launch_config=lc) | |
inst.wait_till_ready() | |
inst.state |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment