def test_create_from_string(self):
        '''

        '''
        sch = Schedule.from_string(DEFAULT_SCHEDULE_STRING)
        self.assertTrue(sch)
        self.assertEqual(sch.start_time, DEFAULT_START)
        self.assertEqual(sch.stop_time, DEFAULT_STOP)
        self.assertEqual(sch.time_zone, DEFAULT_ZONE)
        self.assertEqual(sch.days, DEFAULT_DAYS)
예제 #2
0
    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