def update_spot_requests(username=None): ec2 = get_root_ec2_connection() use_filter = None if username: use_filter = {'launch.key_name': "%s*" % (username)} requests = ec2.get_all_spot_instance_requests(filters=use_filter) dbh.execute("BEGIN IMMEDIATE TRANSACTION") if username: dbh.execute( """ DELETE FROM pending_spot_requests WHERE username = ? """, [username]) else: dbh.execute(""" DELETE FROM pending_spot_requests """) for request in requests: username = user_from_key(request.launch_specification.key_name) instance_type = request.launch_specification.instance_type request_time = request.create_time if request.state == 'open': dbh.execute( """ INSERT OR REPLACE INTO pending_spot_requests ( request_id, instance_type, request_time, username ) VALUES (?, ?, julianday(?), ?) """, [request.id, instance_type, request_time, username]) dbh.execute("COMMIT")
def update_spot_requests(username=None): ec2 = get_root_ec2_connection() use_filter = None if username: use_filter = {'launch.key_name': "%s*" % (username)} requests = ec2.get_all_spot_instance_requests(filters=use_filter) dbh.execute("BEGIN IMMEDIATE TRANSACTION") if username: dbh.execute(""" DELETE FROM pending_spot_requests WHERE username = ? """, [username]) else: dbh.execute(""" DELETE FROM pending_spot_requests """) for request in requests: username = user_from_key(request.launch_specification.key_name) instance_type = request.launch_specification.instance_type request_time = request.create_time if request.state == 'open': dbh.execute(""" INSERT OR REPLACE INTO pending_spot_requests ( request_id, instance_type, request_time, username ) VALUES (?, ?, julianday(?), ?) """, [request.id, instance_type, request_time, username]) dbh.execute("COMMIT")
def update_instances(username = None): ec2 = get_root_ec2_connection() use_filter = None if username: use_filter = { 'key-name': "%s*" % username } all_instances = ec2.get_all_instances(filters=use_filter) now = datetime.datetime.utcnow().isoformat() for reservation in all_instances: for instance in reservation.instances: # We can safely import each instance as a transaction. dbh.execute("BEGIN IMMEDIATE TRANSACTION") username = user_from_key(instance.key_name) start_time = instance.launch_time is_spot = instance.spot_instance_request_id != None if is_spot: is_spot = 1 else: is_spot = 0 old_end_time = None for row in dbh.execute(""" SELECT end_time FROM instances WHERE instance_id = ? """, [instance.id]): old_end_time = row[0] old_stop_time = None for row in dbh.execute(""" SELECT stopped_time FROM instance_stopped WHERE instance_id = ? AND running_time IS NULL """, [instance.id]): old_stop_time = row[0] if instance.state == 'terminated' and old_end_time == None: dbh.execute(""" INSERT OR REPLACE INTO instances ( instance_id, instance_type, start_time, end_time, last_seen, is_spot, username ) VALUES (?, ?, julianday(?), julianday(?), julianday(?), ?, ?) """, [instance.id, instance.instance_type, start_time, now, now, is_spot, username]) if instance.state != 'stopped' and instance.state != 'terminated' and old_stop_time != None: dbh.execute(""" UPDATE instance_stopped WHERE instance_id = ? AND stopped_time = ? SET running_time = ? """, [instance.id, old_stop_time, now]) if instance.state != 'terminated': dbh.execute(""" INSERT OR REPLACE INTO instances ( instance_id, instance_type, start_time, last_seen, is_spot, username ) VALUES (?, ?, julianday(?), julianday(?), ?, ?) """, [instance.id, instance.instance_type, start_time, now, is_spot, username]) if instance.state == 'stopped': dbh.execute(""" INSERT INTO instance_stopped (instance_id, stopped_time) VALUES (?, ?) """, [instance.id, now]) dbh.execute("COMMIT")
def delete_ssh_key(self, key_name): if key_name in self.get_ssh_keys(): ec2 = get_root_ec2_connection() ec2.get_key_pair(key_name).delete audit_log("Delete SSH key %s" % (key_name)) dbh.execute("BEGIN EXCLUSIVE") dbh.execute(""" DELETE FROM ssh_keys WHERE key_name=? AND user_name=? """, [key_name, self.user_name]) dbh.execute("COMMIT")
def delete_ssh_key(self, key_name): if key_name in self.get_ssh_keys(): ec2 = get_root_ec2_connection() ec2.get_key_pair(key_name).delete audit_log("Delete SSH key %s" % (key_name)) dbh.execute("BEGIN EXCLUSIVE") dbh.execute( """ DELETE FROM ssh_keys WHERE key_name=? AND user_name=? """, [key_name, self.user_name]) dbh.execute("COMMIT")
def run_instances(self, instance_info): info_without_ud = instance_info.copy() if 'user_data' in info_without_ud: del info_without_ud['user_data'] old_cost = self.cost_instances() extra_cost = self.cost_proposal(instance_info['instance_type'], instance_info['count']) if not instance_info['key_name'].startswith(self.user_name): audit_log("Rejecting instance request %s (for %s) because of key" % (self.user_name, info_without_ud)) raise Exception("Needs to be associated with SSH key") if old_cost + extra_cost > SPEND_LIMIT: audit_log( "Rejecting instance request %s (for %s) because of cost" % (self.user_name, info_without_ud)) raise Exception("Excessive instance cost") ec2 = get_root_ec2_connection() audit_log("Making instance requset %s for %s" % (info_without_ud, self.user_name)) if instance_info['use_spot']: spot_price = INSTANCE_COST[ instance_info['instance_type']] * SPOT_BASE spot_requests = ec2.request_spot_instances( price=spot_price, image_id=instance_info['image_id'], count=instance_info['count'], key_name=instance_info['key_name'], security_groups=instance_info['security_groups'], user_data=instance_info.get('user_data'), instance_type=instance_info['instance_type'], placement=instance_info.get('availability_zone', None) #, availability_zone_group=instance_info.get('placement_group', None) ) return spot_requests else: reservation = ec2.run_instances( image_id=instance_info['image_id'], min_count=instance_info['count'], max_count=instance_info['count'], key_name=instance_info['key_name'], security_groups=instance_info['security_groups'], user_data=instance_info['user_data'], instance_type=instance_info['instance_type'], placement=instance_info.get('availability_zone', None) #, placement_group=instance_info.get('placement_group', None) ) return reservation
def run_instances(self, instance_info): info_without_ud = instance_info.copy() if 'user_data' in info_without_ud: del info_without_ud['user_data'] old_cost = self.cost_instances() extra_cost = self.cost_proposal( instance_info['instance_type'], instance_info['count'] ) if not instance_info['key_name'].startswith(self.user_name): audit_log("Rejecting instance request %s (for %s) because of key" % (self.user_name, info_without_ud)) raise Exception("Needs to be associated with SSH key") if old_cost + extra_cost > SPEND_LIMIT: audit_log("Rejecting instance request %s (for %s) because of cost" % (self.user_name, info_without_ud)) raise Exception("Excessive instance cost") ec2 = get_root_ec2_connection() audit_log("Making instance requset %s for %s" % ( info_without_ud, self.user_name )) if instance_info['use_spot']: spot_price = INSTANCE_COST[instance_info['instance_type']] * SPOT_BASE spot_requests = ec2.request_spot_instances( price=spot_price, image_id=instance_info['image_id'], count=instance_info['count'], key_name=instance_info['key_name'], security_groups=instance_info['security_groups'], user_data=instance_info.get('user_data'), instance_type=instance_info['instance_type'], placement=instance_info.get('availability_zone', None) #, availability_zone_group=instance_info.get('placement_group', None) ) return spot_requests else: reservation = ec2.run_instances( image_id=instance_info['image_id'], min_count=instance_info['count'], max_count=instance_info['count'], key_name=instance_info['key_name'], security_groups=instance_info['security_groups'], user_data=instance_info['user_data'], instance_type=instance_info['instance_type'], placement=instance_info.get('availability_zone', None) #, placement_group=instance_info.get('placement_group', None) ) return reservation
def create_ssh_key(self, key_name): key_name = re.sub(r'[^-a-zA-Z0-9_]', '', key_name) if not key_name.startswith("%s-" % (self.user_name)): full_name = "%s-%s" % (self.user_name, key_name) else: full_name = key_name ec2 = get_root_ec2_connection() keypair = ec2.create_key_pair(key_name=full_name) audit_log("Creating SSH keypair %s for %s" % (full_name, self.user_name)) dbh.execute("BEGIN EXCLUSIVE") dbh.execute( """ INSERT OR REPLACE INTO ssh_keys (user_name, key_name, private_key, fingerprint) VALUES (?, ?, ?, ?) """, [self.user_name, full_name, keypair.material, keypair.fingerprint]) dbh.execute("COMMIT")
def create_ssh_key(self, key_name): key_name = re.sub(r'[^-a-zA-Z0-9_]', '', key_name) if not key_name.startswith("%s-" % (self.user_name)): full_name = "%s-%s" % (self.user_name, key_name) else: full_name = key_name ec2 = get_root_ec2_connection() keypair = ec2.create_key_pair( key_name = full_name ) audit_log("Creating SSH keypair %s for %s" % ( full_name, self.user_name )) dbh.execute("BEGIN EXCLUSIVE") dbh.execute(""" INSERT OR REPLACE INTO ssh_keys (user_name, key_name, private_key, fingerprint) VALUES (?, ?, ?, ?) """, [self.user_name, full_name, keypair.material, keypair.fingerprint]) dbh.execute("COMMIT")
def update_instances(username=None): ec2 = get_root_ec2_connection() use_filter = None if username: use_filter = {'key-name': "%s*" % username} all_instances = ec2.get_all_instances(filters=use_filter) now = datetime.datetime.utcnow().isoformat() for reservation in all_instances: for instance in reservation.instances: # We can safely import each instance as a transaction. dbh.execute("BEGIN IMMEDIATE TRANSACTION") username = user_from_key(instance.key_name) start_time = instance.launch_time is_spot = instance.spot_instance_request_id != None if is_spot: is_spot = 1 else: is_spot = 0 old_end_time = None for row in dbh.execute( """ SELECT end_time FROM instances WHERE instance_id = ? """, [instance.id]): old_end_time = row[0] old_stop_time = None for row in dbh.execute( """ SELECT stopped_time FROM instance_stopped WHERE instance_id = ? AND running_time IS NULL """, [instance.id]): old_stop_time = row[0] if instance.state == 'terminated' and old_end_time == None: dbh.execute( """ INSERT OR REPLACE INTO instances ( instance_id, instance_type, start_time, end_time, last_seen, is_spot, username ) VALUES (?, ?, julianday(?), julianday(?), julianday(?), ?, ?) """, [ instance.id, instance.instance_type, start_time, now, now, is_spot, username ]) if instance.state != 'stopped' and instance.state != 'terminated' and old_stop_time != None: dbh.execute( """ UPDATE instance_stopped WHERE instance_id = ? AND stopped_time = ? SET running_time = ? """, [instance.id, old_stop_time, now]) if instance.state != 'terminated': dbh.execute( """ INSERT OR REPLACE INTO instances ( instance_id, instance_type, start_time, last_seen, is_spot, username ) VALUES (?, ?, julianday(?), julianday(?), ?, ?) """, [ instance.id, instance.instance_type, start_time, now, is_spot, username ]) if instance.state == 'stopped': dbh.execute( """ INSERT INTO instance_stopped (instance_id, stopped_time) VALUES (?, ?) """, [instance.id, now]) dbh.execute("COMMIT")