def create_node(self, name): """Create a compute node with a given name and configuration information. Return the Node object.""" deployment = self.deployment_script() # Find the size and image we want to use when creating our node. # The following iterates through lists of sizes and images and finds # the match for our configured strings. size_name = self.config["size"] size = filter(lambda size: size.id == size_name, self.compute.list_sizes())[0] img_name = self.config["image"] image = filter(lambda image: image.name == img_name, self.compute.list_images())[0] log.info("Deploying node with size=%s, image=%s", size, image) # Deploy our node. This calls create_node but waits for the creation to # complete, and then it uses paramiko to SSH into the node and run # the commands specified by the `deploy` argument. In order to do this, # the paramiko SSH client must know the private key, specified in # `ssh_key`. `ex_keyname` is the public key we paired up above. key_name = self.pair_ssh_key() self.compute.region = self.config["region"] node = self.compute.deploy_node(name=name, image=image, size=size, deploy=deployment, ssh_key=self.config["private_key"], ssh_username=self.config["ssh_user"], ex_keyname=key_name, ssh_timeout=6400, timeout=6400) log.info("Node deployed: %s", node) return node
def deployment_script(self): # Once the node is built, it'll be a bare image. Run the configured # bootstrap script using libcloud's ScriptDeployment to run the system # updates and install Flask. if self.deploy_name: deployment = ScriptFileDeployment(self.config[self.deploy_name]) log.info("Created ScriptFileDeployment with %s file.", self.deploy_name) return deployment
def get_driver(self): # Get the compute driver we want to connect to, then pass in credentials. compute_driver = self.config["compute_driver"] ComputeDriver = get_compute_driver(compute_driver) identity = self.config["identity"] credential = self.config["credential"] rgn = self.config["region"] compute = ComputeDriver(identity, credential, region=rgn) log.info("Created a %s compute driver in the %s region.", compute_driver, rgn) return compute
def pair_ssh_key(self): # Pair our SSH public key with the provider so we can communicate # with our deployed compute nodes. public_key = self.config["public_key"] private_key = self.config["private_key"] pub_key = open(public_key, "r").read() key_name = os.path.split(private_key)[-1] keys = [key.name for key in self.compute.list_key_pairs()] if key_name not in keys: # If this key isn't already paired, import the key by choosing a name # and passing in the contents of the public key. key = self.compute.import_key_pair_from_string(key_name, pub_key) log.info("Paired %s key with provider.", key) else: log.info("Already had %s key paired.", key_name) return key_name
# CASE 1: 2 WEB SERVERS, 1 DATABASE SERVER, 1 LOAD BALANCER WITH HA PROXY from config import get_config from oslogger import log from db_node import DBServer from ha_node import HAProxyServer from web_node import WebServer import pdb if __name__ == "__main__": config = get_config() log.info("Creating web nodes.") nodes = [] pdb.set_trace() web_server = WebServer(config) for name in ("sabeen-server-1", "sabeen-server-2"): nodes.append(web_server.create_node(name)) log.info("Creating database node.") db_server = DBServer(config) db_server.create_node("sabeen-db") log.info("Creating haproxy load balancer node.") haproxy_server = HAProxyServer(config, nodes) lb_node = haproxy_server.create_node("sabeen-haproxy") log.info("Access the load balancer at %s", lb_node.public_ips)
# CASE 2: 2 WEB SERVERS, 2 DATABASE SERVER2, 1 LOAD BALANCER FOR DBS AND ONE FOR SERVER # WITH HA PROXY from config import get_config from oslogger import log from db_node import DBServer from ha_node import HAProxyServer from web_node import WebServer import libcloud.security libcloud.security.VERIFY_SSL_CERT = False if __name__ == "__main__": config = get_config() log.info("Creating web nodes.") server_nodes = [] web_server = WebServer(config) for name in ("server-1-stella", "server-2-stella"): server_nodes.append(web_server.create_node(name)) log.info("Creating database nodes.") db_nodes = [] db_server = DBServer(config) for name in ("db-1-stella", "db-2-stella"): db_nodes.append(db_server.create_node(name)) log.info("Creating haproxy load balancer node for servers.") haproxy_server = HAProxyServer(config, server_nodes) log.info("Creating haproxy load balancer node for dbs.") haproxy_db = HAProxyServer(config, db_nodes)
from db_node import DBServer from ha_node import HAProxyServer from web_node import WebServer # storage from libcloud.storage.types import Provider from libcloud.storage.providers import get_driver from io import BytesIO import libcloud.security libcloud.security.VERIFY_SSL_CERT = False if __name__ == "__main__": config = get_config() log.info("Creating web node.") web_server = WebServer(config) server_node = web_server.create_node("server-stella") log.info("Creating database node.") db_server = DBServer(config) db_node = db_server.create_node("db-stella") # drives and storing files Driver = get_driver(Provider.CLOUDFILES) storage = Driver(config["identity"], config["credential"], config["region"]) container = storage.create_container("my_container_stella") obj = container.upload_object_via_stream( BytesIO("some_data"), object_name="my_uploaded_data_stella")