def update(key, secret, service, location, node_uuid=None, ssh_key=None, image=None, size=None, security_group=None): """Update or create node""" if node_uuid is not None: driver = getDriverInstance(service, key, secret) found_node_list = [node for node in driver.list_nodes() if node.uuid==node_uuid] if len(found_node_list) == 0: # node_uuid relates to not available one, recreate node_uuid = None elif len(found_node_list) == 1: node = found_node_list[0] if node.state not in [NodeState.RUNNING, NodeState.REBOOTING, NodeState.PENDING]: # node_uuid relates to destroyed one, recreate node_uuid = None if not node_uuid: if not image or not size or not location: raise Exception("Node can not be created because of lacking informations") # XXX-Cedric : what exception? # Creates node return install(key, secret, service, image, size, location, security_group) else: return dict( ssh_key = ssh_key, node_uuid = node_uuid )
def install(key, secret, service, image_id, size_id, location_id, security_group): driver = getDriverInstance(service, key, secret) # Defines a dict that will be used to create our node(s) argument_list = dict() argument_list['image'] = [image for image in driver.list_images() if image_id in image.id][0] argument_list['size'] = [size for size in driver.list_sizes() if size_id in size.id][0] # We can create uour own images and sizes #image = NodeImage(id=self.options['image_id'], # name=self.options['image_name'], # driver=driver) #size = NodeSize(self.options['size_id'], self.options['size_name'], None, # None, None, None, driver=driver) argument_list['location'] = [location for location in driver.list_locations() if location_id in location.id][0] # If we are in ec2 : adding ssh key manually if 'EC2' in service: try: unique_keyname = str(uuid.uuid1()) keypair = driver.ex_create_keypair(unique_keyname) ssh_key = keypair['keyMaterial'] argument_list['ex_keyname'] = unique_keyname except Exception, e: # XX-Cedric : what to do here? if e.args[0].find("InvalidKeyPair.Duplicate") == -1: # XXX-Cedric : our unique key was not so unique...Do something raise e else: raise e
def destroy(key, secret, service, node_uuid): driver = getDriverInstance(service, key, secret) node = getNode(driver, node_uuid) if node is None: return False if node.state in [NodeState.RUNNING, NodeState.REBOOTING, NodeState.PENDING]: return node.destroy() else: return False
def start(key, secret, service, node_uuid): """Starts node""" driver = getDriverInstance(service, key, secret) node = getNode(driver, node_uuid) if node is None: return False if node.state in [0, 1]: return True elif node.state == 3: return node.reboot() else: return False return True
def getpubliciplist(key, secret, service, node_uuid): """Gets public ip(s), can wait for the first IP to appear""" public_ip_list = [] for i in range(100): driver = getDriverInstance(service, key, secret) node = getNode(driver, node_uuid) if node.state not in [NodeState.RUNNING, NodeState.REBOOTING, NodeState.PENDING]: break if node.public_ip[0]: public_ip_list = node.public_ip break else: time.sleep(5) return dict( public_ip_list = public_ip_list )
def stop(key, secret, service, node_uuid, ssh_key=None): """Stops node""" driver = getDriverInstance(service, key, secret) node = getNode(driver, node_uuid) # Checks state if node.state not in [NodeState.RUNNING, NodeState.REBOOTING, NodeState.PENDING]: return False # Good state : connects if ssh_key is None: ssh_key = getprivatekey(key, secret, service, node_uuid) public_ip = node.public_ip[0] if not public_ip: raise Exception('Node is started but has no IP.') try: ssh = getSSHConnection(driver, public_ip, ssh_key) print('Stopping instance...') stdin, stdout, stderr = ssh.exec_command('halt') except SSHException, e: print('unable to stop') raise e