def test_running_time_after_stop(self): ''' Verify a RUNNING instance is stopped when timestamp is after the schedule stop time ''' running = True inst = Instance(DEFAULT_ID, running, DEFAULT_SCHEDULE) timestamp = datetime.datetime(2018, 4, 23, 23, 0) inst.evaluate_schedule(timestamp) self.assertEqual(inst.running, False)
def test_stopped_time_before_start(self): ''' Verify a STOPPED instance stays stopped when timestamp is before the schedule start time ''' running = False inst = Instance(DEFAULT_ID, running, DEFAULT_SCHEDULE) timestamp = datetime.datetime(2018, 4, 23, 0, 0) inst.evaluate_schedule(timestamp) self.assertEqual(inst.running, False)
def test_stopped_no_stop_time_after_start(self): ''' Verify a STOPPED instance is started when timestamp is after the schedule start time ''' running = False inst = Instance(DEFAULT_ID, running, NO_STOP_SCHEDULE) timestamp = datetime.datetime(2018, 4, 23, 12, 0) inst.evaluate_schedule(timestamp) self.assertEqual(inst.running, True)
def test_running_time_between_start_stop(self): ''' Verify a RUNNING instance continues to run when timestamp is between the schedule start and stop times ''' running = True inst = Instance(DEFAULT_ID, running, DEFAULT_SCHEDULE) timestamp = datetime.datetime(2018, 4, 23, 12, 0) inst.evaluate_schedule(timestamp) self.assertEqual(inst.running, True)
def test_running_no_stop_time_after_start(self): ''' Verify a RUNNING instance continues to run when timestamp is after the schedule start time ''' running = True inst = Instance(DEFAULT_ID, running, NO_STOP_SCHEDULE) timestamp = datetime.datetime(2018, 4, 23, 23, 0) inst.evaluate_schedule(timestamp) self.assertEqual(inst.running, True)
def test_stopped_time_between_start_stop(self): ''' Verify a STOPPED instance is started when timestamp is between the schedule start and stop times ''' running = False inst = Instance(DEFAULT_ID, running, DEFAULT_SCHEDULE) timestamp = datetime.datetime(2018, 4, 23, 12, 0) inst.evaluate_schedule(timestamp) self.assertEqual(inst.running, True)
def test_create_stopped(self): ''' Create a STOPPED instance ''' running = False inst = Instance(DEFAULT_ID, running, DEFAULT_SCHEDULE) self.assertTrue(inst) self.assertEqual(inst.id, DEFAULT_ID) self.assertEqual(inst.running, running) self.assertEqual(inst.schedule, DEFAULT_SCHEDULE)
def test_create_running(self): ''' Create a RUNNING instance ''' running = True inst = Instance(DEFAULT_ID, running, DEFAULT_SCHEDULE) self.assertTrue(inst) self.assertEqual(inst.id, DEFAULT_ID) self.assertEqual(inst.running, running) self.assertEqual(inst.schedule, DEFAULT_SCHEDULE)
def get_scheduled_instances(self): instances = [] ec2 = boto3.client('ec2') regions = [ region['RegionName'] for region in ec2.describe_regions()['Regions'] ] for region in regions: ec2 = boto3.client('ec2', region_name=region) # TODO: Add filters to ignore the following instances # Instance State = shutting-down or terminated # Instances in auto-scaling groups filters = [{'Name': 'tag-key', 'Values': [EC2.SCHEDULE_TAG]}] result = ec2.describe_instances(Filters=filters) # TODO: Get rid of this double 'for' loop for reservation in result['Reservations']: for ec2_instance in reservation['Instances']: id = region + ':' + ec2_instance['InstanceId'] running = self._get_state(ec2_instance['State']) schedule = self._get_schedule(ec2_instance['Tags']) logger.info( 'Instance [{}]: Running= {} Schedule= {}'.format( id, running, schedule)) # Ignore instances that are not running or stopped if running != None: try: instances.append( Instance(id, running, Schedule.from_string(schedule), provider.aws.EC2(id))) except Exception as e: logger.error('Instance [{}]: {}'.format(id, e)) return instances