Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build Your Infrastructure - with Python! (Ed L...

Build Your Infrastructure - with Python! (Ed Leafe)

PyCon Canada

August 13, 2013
Tweet

More Decks by PyCon Canada

Other Decks in Education

Transcript

  1. Authentication #!/usr/bin/env python # -*- coding: utf-8 -*- import pyrax

    import pyrax.utils as utils import pyrax.exceptions as exc # If you have keyring configured... pyrax.keyring_auth() # If you have a credential file... #pyrax.set_credential_file("/path/to/file") # Or just set directly #pyrax.set_credentials(username, password)
  2. Credential File For Keystone authentication: [keystone] username = myusername password

    = 1234567890abcdef tenant_id = abcdef1234567890 For Rackspace authentication: [rackspace_cloud] username = myusername api_key = 1234567890abcdef
  3. Configuration File [default] identity_type = rackspace keyring_username = leaferax region

    = DFW custom_user_agent = debug = False verify_ssl = False [devstack] identity_type = keystone region = RegionOne custom_user_agent = debug = True auth_endpoint = http://166.78.147.147:5000/v2.0/ tenant_name = demo tenant_id = d774d0b56b60464c80e3430062aae65f keyring_username = demo
  4. Shortcuts cs = pyrax.cloudservers cf = pyrax.cloudfiles clb = pyrax.cloud_loadbalancers

    cdb = pyrax.cloud_databases cnw = pyrax.cloud_networks dns = pyrax.cloud_dns cmn = pyrax.cloud_monitoring
  5. Create the Database # NOTE: The flavor ID and size

    are hardcoded # for demo purposes. db_instance = cdb.create("DemoDB", flavor=1, volume=2) utils.wait_for_build(db_instance, verbose=True) db = db_instance.create_database("demodb") db_user = db_instance.create_user("demouser", "topsecret", db)
  6. Create the Database # NOTE: The flavor ID and size

    are hardcoded # for demo purposes. db_instance = cdb.create("DemoDB", flavor=1, volume=2) utils.wait_for_build(db_instance, verbose=True) db = db_instance.create_database("demodb") db_user = db_instance.create_user("demouser", "topsecret", db)
  7. Create the Database # NOTE: The flavor ID and size

    are hardcoded # for demo purposes. db_instance = cdb.create("DemoDB", flavor=1, volume=2) utils.wait_for_build(db_instance, verbose=True) db = db_instance.create_database("demodb") db_user = db_instance.create_user("demouser", "topsecret", db)
  8. Create the Database # NOTE: The flavor ID and size

    are hardcoded # for demo purposes. db_instance = cdb.create("DemoDB", flavor=1, volume=2) utils.wait_for_build(db_instance, verbose=True) db = db_instance.create_database("demodb") db_user = db_instance.create_user("demouser", "topsecret", db)
  9. Create an isolated network new_network_name = "isolated" new_network_cidr = "192.168.0.0/24"

    new_net = cnw.create(new_network_name, cidr=new_network_cidr)
  10. Create an isolated network new_network_name = "isolated" new_network_cidr = "192.168.0.0/24"

    new_net = cnw.create(new_network_name, cidr=new_network_cidr)
  11. Create an isolated network new_network_name = "isolated" new_network_cidr = "192.168.0.0/24"

    new_net = cnw.create(new_network_name, cidr=new_network_cidr)
  12. Define a Public Key # Store the public key keyfile

    = os.expanduser("~/.ssh/id_rsa.pub") with open(keyfile, "r") as kf: pub = kf.read() key = cs.keypairs.create("macbook", pub)
  13. Define a Public Key # Store the public key keyfile

    = os.expanduser("~/.ssh/id_rsa.pub") with open(keyfile, "r") as kf: pub = kf.read() key = cs.keypairs.create("macbook", pub)
  14. Create the App Servers # This is hardcoded for demo

    purposes. app_image = "fb61c42c-b65c-45f0-b1b8-20bd5e47de32" # Create two servers with only ServiceNet networks = [{"net-id": cnw.SERVICE_NET_ID}] server1 = cs.servers.create("PyConCA_Srv1", image=app_image, flavor=2, key_name="macbook", nics=networks) server2 = cs.servers.create("PyConCA_Srv2", image=app_image, flavor=2, key_name="macbook", nics=networks) utils.wait_for_build(server1, verbose=True) utils.wait_for_build(server2, verbose=True)
  15. Create the App Servers # This is hardcoded for demo

    purposes. app_image = "fb61c42c-b65c-45f0-b1b8-20bd5e47de32" # Create two servers with only ServiceNet networks = [{"net-id": cnw.SERVICE_NET_ID}] server1 = cs.servers.create("PyConCA_Srv1", image=app_image, flavor=2, key_name="macbook", nics=networks) server2 = cs.servers.create("PyConCA_Srv2", image=app_image, flavor=2, key_name="macbook", nics=networks) utils.wait_for_build(server1, verbose=True) utils.wait_for_build(server2, verbose=True)
  16. Create the App Servers # This is hardcoded for demo

    purposes. app_image = "fb61c42c-b65c-45f0-b1b8-20bd5e47de32" # Create two servers with only ServiceNet networks = [{"net-id": cnw.SERVICE_NET_ID}] server1 = cs.servers.create("PyConCA_Srv1", image=app_image, flavor=2, key_name="macbook", nics=networks) server2 = cs.servers.create("PyConCA_Srv2", image=app_image, flavor=2, key_name="macbook", nics=networks) utils.wait_for_build(server1, verbose=True) utils.wait_for_build(server2, verbose=True)
  17. Define the LB Nodes & Virtual IP # Get the

    server IPs ip1 = server1.addresses["private"][0]["addr"] ip2 = server2.addresses["private"][0]["addr"] # Define the nodes node1 = clb.Node(address=ip1, port=80, weight=1, condition="ENABLED") node2 = clb.Node(address=ip2, port=80, weight=1, condition="ENABLED") # Create the Virtual IP vip = clb.VirtualIP(type="PUBLIC")
  18. Define the LB Nodes & Virtual IP # Get the

    server IPs ip1 = server1.addresses["private"][0]["addr"] ip2 = server2.addresses["private"][0]["addr"] # Define the nodes node1 = clb.Node(address=ip1, port=80, weight=1, condition="ENABLED") node2 = clb.Node(address=ip2, port=80, weight=1, condition="ENABLED") # Create the Virtual IP vip = clb.VirtualIP(type="PUBLIC")
  19. Define the LB Nodes & Virtual IP # Get the

    server IPs ip1 = server1.addresses["private"][0]["addr"] ip2 = server2.addresses["private"][0]["addr"] # Define the nodes node1 = clb.Node(address=ip1, port=80, weight=1, condition="ENABLED") node2 = clb.Node(address=ip2, port=80, weight=1, condition="ENABLED") # Create the Virtual IP vip = clb.VirtualIP(type="PUBLIC")
  20. Create the Load Balancer # Create the Load Balancer lb

    = clb.create("PyConCA_LB", port=80, protocol="HTTP", nodes=[node1, node2], virtual_ips=vip, algorithm="WEIGHTED_ROUND_ROBIN") utils.wait_for_build(lb, verbose=True) lb_ip = lb.virtual_ips[0].address
  21. Create the Load Balancer # Create the Load Balancer lb

    = clb.create("PyConCA_LB", port=80, protocol="HTTP", nodes=[node1, node2], virtual_ips=vip, algorithm="WEIGHTED_ROUND_ROBIN") utils.wait_for_build(lb, verbose=True) lb_ip = lb.virtual_ips[0].address
  22. Create the Load Balancer # Create the Load Balancer lb

    = clb.create("PyConCA_LB", port=80, protocol="HTTP", nodes=[node1, node2], virtual_ips=vip, algorithm="WEIGHTED_ROUND_ROBIN") utils.wait_for_build(lb, verbose=True) lb_ip = lb.virtual_ips[0].address
  23. Configure DNS domain_name = "pyraxdemo.com" dom = dns.create(name=domain_name, emailAddress="[email protected]", ttl=900,

    comment="Pyrax Demo") a_rec = {"type": "A", "name": domain_name, "data": lb_ip, "ttl": 900} recs = dom.add_record(a_rec)
  24. Configure DNS domain_name = "pyraxdemo.com" dom = dns.create(name=domain_name, emailAddress="[email protected]", ttl=900,

    comment="Pyrax Demo") a_rec = {"type": "A", "name": domain_name, "data": lb_ip, "ttl": 900} recs = dom.add_record(a_rec)