def get_machine_ids(cloud_name, machine_name): util.message("getting machine id's for " + str(machine_name)) try: driver = cloud.get_cloud_driver(cloud_name) if driver == None: return machine_ids = [] kount = 0 nds = driver.list_nodes() for n in nds: try: if machine_name == n.name: machine_ids.append(n.id) else: if re.search("^" + str(machine_name) + "$", n.name): machine_ids.append(n.id) except Exception: continue except: util.message("error thrown in get_machine_ids()", "error") return ([]) util.message('machine_ids = "' + str(machine_ids) + '"', 'info') return (machine_ids)
def action(cloud_name, machine_ids, action): try: driver = cloud.get_cloud_driver(cloud_name) if driver == None: return kount = 0 nds = driver.list_nodes() for n in nds: if n.id in machine_ids: kount = kount + 1 if action == "destroy": ret = n.destroy() node.delete(n.id) elif action == "start": ret = driver.ex_start_node(n) elif action == "stop": ret = driver.ex_stop_node(n) elif action == "reboot": ret = n.reboot() else: util.message( "Invalid action (" + str(action) + ") for NODE", "error") if kount == 0: util.message("Node not found", "error") return except Exception as e: util.message(str(e), "error") return return
def destroy(cloud_name, name): try: driver = cloud.get_cloud_driver(cloud_name) key = driver.get_key_pair(name) driver.delete_key_pair(key) except Exception as e: util.fatal_error(str(e))
def launch(cloud_name, size, name=None, key=None, location=None, security_group=None, \ network=None, data_gb=None, wait_for=True): provider, xxx, region, default_ssh_key, cloud_keys = cloud.read( cloud_name, True) if key == None: key = default_ssh_key if name == None: machine_name = "?" else: machine_name = name util.message("launching - " + str(cloud_name) + ", " + \ str(size) + ", " + str(key)) try: driver = cloud.get_cloud_driver(cloud_name) if driver == None: util.message("cloud driver not found", "error") return (None) size = get_size(provider, size) util.message("validating size - " + str(size), "info") sizes = driver.list_sizes() for s in sizes: if s.name == size: sz = s break else: util.message("Invalid Size - " + str(size), "error") return (None) im, typ = get_image(driver, cloud_name) if im == None: return (None) util.message("creating machine on " + str(provider), "info") if provider == 'aws': nd = driver.create_node (name=machine_name, size=sz, image=im, ex_keyname=key, \ ex_security_groups=["default"]) else: nd = driver.create_node (name=machine_name, size=sz, image=im, ex_keyname=key, \ ex_config_drive=True, ex_security_groups=driver.ex_list_security_groups(), \ networks=driver.ex_list_networks()) except Exception as e: util.message(str(e), "error") return (None) machine_id = nd.id util.message("machine_id - " + str(machine_id), "info") if wait_for == True: waitfor(cloud_name, machine_id, "running") return (machine_id)
def list_sizes(cloud_name): driver = cloud.get_cloud_driver(cloud_name) if driver == None: return try: sizes = driver.list_sizes() except Exception as e: util.message(str(e), 'error') return headers = [ 'Family', 'Size', 'RAM (MB)', 'Disk (GB)', 'Bandwidth', 'Price (USD/Mo)' ] keys = ['family', 'size', 'ram', 'disk', 'bandwidth', 'price'] jsonList = [] for size in sizes: if size.disk == 0: continue sz = size.name sz_split = sz.split(".") if len(sz_split) < 2: family = "" szz = size.name else: family = sz_split[0] szz = sz_split[1] sizeDict = {} sizeDict['family'] = family sizeDict['size'] = szz sizeDict['ram'] = str(size.ram) sizeDict['disk'] = str(size.disk) if size.price == 0.0: sizeDict['price'] = "" else: sizeDict['price'] = str(round(size.price * 720)) if size.bandwidth == None: sizeDict['bandwidth'] = "" else: sizeDict['bandwidth'] = str(size.bandwidth) jsonList.append(sizeDict) util.print_list(headers, keys, jsonList) return
def list_cloud_keys(cloud_name): try: driver = cloud.get_cloud_driver(cloud_name) kk = driver.list_key_pairs() except Exception as e: util.fatal_error(str(e)) headers = ['Name'] keys = ['name'] jsonList = [] for key in kk: dict = {} dict['name'] = key.name jsonList.append(dict) util.print_list(headers, keys, jsonList) return
def list(cloud_name): driver = cloud.get_cloud_driver(cloud_name) if driver == None: return try: nds = driver.list_nodes() except Exception as e: util.message(str(e), 'error') return headers = ['Name', 'ID', 'State', 'Public IP', 'Private IP'] keys = ['name', 'id', 'state', 'public_ip', 'private_ip'] jsonList = [] for nd in nds: ndDict = {} ndDict['id'] = str(nd.id) ndDict['name'] = str(nd.name) ndDict['state'] = str(nd.state) if len(nd.public_ips) >= 1: ndDict['public_ip'] = str(nd.public_ips[0]) else: ndDict['public_ip'] = "" if len(nd.private_ips) >= 1: ndDict['private_ip'] = str(nd.private_ips[0]) else: if len(nd.public_ips) >= 1: ndDict['private_ip'] = str(nd.public_ips[0]) else: ndDict['private_ip'] = "" jsonList.append(ndDict) util.print_list(headers, keys, jsonList) return
def import_from_file(cloud_name, name, pub_key_file): try: driver = cloud.get_cloud_driver(cloud_name) driver.import_key_pair_from_file(name, pub_key_file) except Exception as e: util.fatal_error(str(e))