Ejemplo n.º 1
0
def update_ec2_alarms(verbose=0):
    alarm_keys = AlarmsManager.get_alarm_keys()

    for alarm_key in alarm_keys:
        alarm_group = AlarmsManager.get_alarm_group(alarm_key)

        tag_key = alarm_group.get('TagKey')
        tag_value = alarm_group.get('TagValue')

        AWSCredentialsManager.load_saved_environment_state()
        if alarm_group.get('AWSProfile'):
            AWSCredentialsManager.load_aws_profile(alarm_group['AWSProfile'])

        ec2_tags = get_all_ec2_tags(tag_key)

        for instance_id, key in ec2_tags.items():
            if key != alarm_key:
                continue

            if verbose:
                click.echo('\nUpdating instance {} tagged with {}: {}'.format(
                    instance_id, tag_key, tag_value))

            alarms = alarm_group.get('Alarms', [])
            for alarm in alarms:
                if verbose:
                    click.echo('Updating {} alarm on instance {}'.format(
                        alarm['MetricName'], instance_id))

                if alarm['MetricName'] in plugin_alarms:
                    alarm = plugin_alarms.get(
                        alarm['MetricName']).get_updated_alarm_kwargs(
                            identifier=instance_id, **alarm)

                set_cloudwatch_alarm(instance_id, **alarm)
    def test_clean_current_profile(self):
        os.environ['AWS_PROFILE'] = 'test_profile'
        os.environ['AWS_ACCESS_KEY_ID'] = 'test_id'
        os.environ['AWS_SECRET_ACCESS_KEY'] = 'test_secret'
        os.environ['AWS_DEFAULT_REGION'] = 'test_region'

        AWSCredentialsManager.clean_current_profile()

        self.assertEqual(os.environ.get('AWS_PROFILE'), None)
        self.assertEqual(os.environ.get('AWS_ACCESS_KEY_ID'), None)
        self.assertEqual(os.environ.get('AWS_SECRET_ACCESS_KEY'), None)
        self.assertEqual(os.environ.get('AWS_DEFAULT_REGION'), None)
    def test_clean_current_profile(self):
        os.environ['AWS_PROFILE'] = 'test_profile'
        os.environ['AWS_ACCESS_KEY_ID'] = 'test_id'
        os.environ['AWS_SECRET_ACCESS_KEY'] = 'test_secret'
        os.environ['AWS_DEFAULT_REGION'] = 'test_region'

        AWSCredentialsManager.clean_current_profile()

        self.assertEqual(os.environ.get('AWS_PROFILE'), None)
        self.assertEqual(os.environ.get('AWS_ACCESS_KEY_ID'), None)
        self.assertEqual(os.environ.get('AWS_SECRET_ACCESS_KEY'), None)
        self.assertEqual(os.environ.get('AWS_DEFAULT_REGION'), None)
Ejemplo n.º 4
0
def update_ec2_tags(verbose=0):
    if verbose:
        click.echo('Updating EC2 tags\n')

    for group in TagsManager.get_groups_by_type('EC2'):
        AWSCredentialsManager.load_saved_environment_state()
        if group.get('AWSProfile'):
            AWSCredentialsManager.load_aws_profile(group['AWSProfile'])

        set_ec2_tags(instance_ids=group.get('Resources'),
                     tag_key=group.get('TagKey'),
                     tag_value=group.get('TagValue'),
                     verbose=verbose)
    def test_save_environment_state(self):
        os.environ['AWS_PROFILE'] = 'test_profile'
        os.environ['AWS_ACCESS_KEY_ID'] = 'test_id'
        os.environ['AWS_SECRET_ACCESS_KEY'] = 'test_secret'
        os.environ['AWS_DEFAULT_REGION'] = 'test_region'

        AWSCredentialsManager.save_environment_state()

        self.assertDictEqual(AWSCredentialsManager.SAVED_ENVIRONMENT_STATE,
                             {
                                 'AWS_PROFILE': 'test_profile',
                                 'AWS_ACCESS_KEY_ID': 'test_id',
                                 'AWS_SECRET_ACCESS_KEY': 'test_secret',
                                 'AWS_DEFAULT_REGION': 'test_region',
                             })
    def test_save_environment_state(self):
        os.environ['AWS_PROFILE'] = 'test_profile'
        os.environ['AWS_ACCESS_KEY_ID'] = 'test_id'
        os.environ['AWS_SECRET_ACCESS_KEY'] = 'test_secret'
        os.environ['AWS_DEFAULT_REGION'] = 'test_region'

        AWSCredentialsManager.save_environment_state()

        self.assertDictEqual(
            AWSCredentialsManager.SAVED_ENVIRONMENT_STATE, {
                'AWS_PROFILE': 'test_profile',
                'AWS_ACCESS_KEY_ID': 'test_id',
                'AWS_SECRET_ACCESS_KEY': 'test_secret',
                'AWS_DEFAULT_REGION': 'test_region',
            })
    def test_set_current_profile(self):
        AWSCredentialsManager.clean_current_profile()

        profile = AWSProfile(
            AWS_PROFILE='test_profile',
            AWS_ACCESS_KEY_ID='test_id',
            AWS_SECRET_ACCESS_KEY='test_secret',
            AWS_DEFAULT_REGION='test_region',
        )

        AWSCredentialsManager.set_current_profile(profile)

        self.assertEqual(os.environ.get('AWS_PROFILE'), None)
        self.assertEqual(os.environ.get('AWS_ACCESS_KEY_ID'), 'test_id')
        self.assertEqual(os.environ.get('AWS_SECRET_ACCESS_KEY'), 'test_secret')
        self.assertEqual(os.environ.get('AWS_DEFAULT_REGION'), 'test_region')
    def test_set_current_profile(self):
        AWSCredentialsManager.clean_current_profile()

        profile = AWSProfile(
            AWS_PROFILE='test_profile',
            AWS_ACCESS_KEY_ID='test_id',
            AWS_SECRET_ACCESS_KEY='test_secret',
            AWS_DEFAULT_REGION='test_region',
        )

        AWSCredentialsManager.set_current_profile(profile)

        self.assertEqual(os.environ.get('AWS_PROFILE'), None)
        self.assertEqual(os.environ.get('AWS_ACCESS_KEY_ID'), 'test_id')
        self.assertEqual(os.environ.get('AWS_SECRET_ACCESS_KEY'),
                         'test_secret')
        self.assertEqual(os.environ.get('AWS_DEFAULT_REGION'), 'test_region')
Ejemplo n.º 9
0
def parse_data(data):
    for profile in data.get('AWSProfiles', []):
        AWSCredentialsManager.add_aws_profile(
            aws_profile=profile['AWS_PROFILE'],
            aws_access_key_id=profile['AWS_ACCESS_KEY_ID'],
            aws_secret_access_key=profile['AWS_SECRET_ACCESS_KEY'],
            aws_default_region=profile['AWS_DEFAULT_REGION'],
        )

    for alarm_group in data.get('AlarmGroups', []):
        AlarmsManager.add_alarm_group(**alarm_group)

    for tag_group in data.get('TagGroups', []):
        TagsManager.add_tag_group(**tag_group)

    for directory_path in data.get('Configuration').get('Plugins', []):
        ConfigurationManager.add_plugins_directory(directory_path)
Ejemplo n.º 10
0
def parse_data(data):
    for profile in data.get('AWSProfiles', []):
        AWSCredentialsManager.add_aws_profile(
            aws_profile=profile['AWS_PROFILE'],
            aws_access_key_id=profile['AWS_ACCESS_KEY_ID'],
            aws_secret_access_key=profile['AWS_SECRET_ACCESS_KEY'],
            aws_default_region=profile['AWS_DEFAULT_REGION'],
        )

    for alarm_group in data.get('AlarmGroups', []):
        AlarmsManager.add_alarm_group(**alarm_group)

    for tag_group in data.get('TagGroups', []):
        TagsManager.add_tag_group(**tag_group)

    for directory_path in data.get('Configuration').get('Plugins', []):
        ConfigurationManager.add_plugins_directory(directory_path)
Ejemplo n.º 11
0
    def test_add_aws_profile(self):
        AWSCredentialsManager.clean_current_profile()

        expected_profile = AWSProfile(
            AWS_PROFILE='test_profile',
            AWS_ACCESS_KEY_ID='test_id',
            AWS_SECRET_ACCESS_KEY='test_secret',
            AWS_DEFAULT_REGION='test_region',
        )

        AWSCredentialsManager.add_aws_profile(
            aws_profile='test_profile',
            aws_access_key_id='test_id',
            aws_secret_access_key='test_secret',
            aws_default_region='test_region',
        )

        self.assertEqual(AWSCredentialsManager.AWS_PROFILES.get('test_profile'),
                         expected_profile)
    def test_add_aws_profile(self):
        AWSCredentialsManager.clean_current_profile()

        expected_profile = AWSProfile(
            AWS_PROFILE='test_profile',
            AWS_ACCESS_KEY_ID='test_id',
            AWS_SECRET_ACCESS_KEY='test_secret',
            AWS_DEFAULT_REGION='test_region',
        )

        AWSCredentialsManager.add_aws_profile(
            aws_profile='test_profile',
            aws_access_key_id='test_id',
            aws_secret_access_key='test_secret',
            aws_default_region='test_region',
        )

        self.assertEqual(
            AWSCredentialsManager.AWS_PROFILES.get('test_profile'),
            expected_profile)
Ejemplo n.º 13
0
def update_ec2_alarms(verbose=0):
    plugin_alarms = load_plugins()
    alarm_keys = AlarmsManager.get_alarm_keys()

    for alarm_key in alarm_keys:
        alarm_group = AlarmsManager.get_alarm_group(alarm_key)

        tag_key = alarm_group.get('TagKey')
        tag_value = alarm_group.get('TagValue')

        AWSCredentialsManager.load_saved_environment_state()
        if alarm_group.get('AWSProfile'):
            AWSCredentialsManager.load_aws_profile(alarm_group['AWSProfile'])

        ec2_tags = get_all_ec2_tags(tag_key)

        for instance_id, key in ec2_tags.items():
            if key != alarm_key:
                continue

            if verbose:
                click.echo('\nUpdating instance {} tagged with {}: {}'
                           .format(instance_id, tag_key, tag_value))

            alarms = alarm_group.get('Alarms', [])
            for alarm in alarms:
                if verbose:
                    click.echo('Updating {} alarm on instance {}'
                               .format(alarm['MetricName'], instance_id))

                if alarm['MetricName'] in plugin_alarms:
                    alarm = plugin_alarms.get(alarm['MetricName']).get_updated_alarm_kwargs(
                        identifier=instance_id,
                        **alarm
                    )

                set_cloudwatch_alarm(instance_id, **alarm)
Ejemplo n.º 14
0
    def test_load_saved_environment_state(self):
        os.environ['AWS_PROFILE'] = 'test_profile'
        os.environ['AWS_ACCESS_KEY_ID'] = 'test_id'
        os.environ['AWS_SECRET_ACCESS_KEY'] = 'test_secret'
        os.environ['AWS_DEFAULT_REGION'] = 'test_region'

        AWSCredentialsManager.save_environment_state()
        AWSCredentialsManager.clean_current_profile()
        AWSCredentialsManager.load_saved_environment_state()

        self.assertEqual(os.environ.get('AWS_PROFILE'), 'test_profile')
        self.assertEqual(os.environ.get('AWS_ACCESS_KEY_ID'), 'test_id')
        self.assertEqual(os.environ.get('AWS_SECRET_ACCESS_KEY'), 'test_secret')
        self.assertEqual(os.environ.get('AWS_DEFAULT_REGION'), 'test_region')
    def test_load_saved_environment_state(self):
        os.environ['AWS_PROFILE'] = 'test_profile'
        os.environ['AWS_ACCESS_KEY_ID'] = 'test_id'
        os.environ['AWS_SECRET_ACCESS_KEY'] = 'test_secret'
        os.environ['AWS_DEFAULT_REGION'] = 'test_region'

        AWSCredentialsManager.save_environment_state()
        AWSCredentialsManager.clean_current_profile()
        AWSCredentialsManager.load_saved_environment_state()

        self.assertEqual(os.environ.get('AWS_PROFILE'), 'test_profile')
        self.assertEqual(os.environ.get('AWS_ACCESS_KEY_ID'), 'test_id')
        self.assertEqual(os.environ.get('AWS_SECRET_ACCESS_KEY'),
                         'test_secret')
        self.assertEqual(os.environ.get('AWS_DEFAULT_REGION'), 'test_region')
Ejemplo n.º 16
0
    def test_load_aws_profile(self):
        AWSCredentialsManager.clean_current_profile()

        AWSCredentialsManager.add_aws_profile(
            aws_profile='test_profile',
            aws_access_key_id='test_id',
            aws_secret_access_key='test_secret',
            aws_default_region='test_region',
        )

        AWSCredentialsManager.load_aws_profile('test_profile')

        self.assertEqual(os.environ.get('AWS_PROFILE'), None)
        self.assertEqual(os.environ.get('AWS_ACCESS_KEY_ID'), 'test_id')
        self.assertEqual(os.environ.get('AWS_SECRET_ACCESS_KEY'), 'test_secret')
        self.assertEqual(os.environ.get('AWS_DEFAULT_REGION'), 'test_region')

        AWSCredentialsManager.load_aws_profile('test_another_profile')

        self.assertEqual(os.environ.get('AWS_PROFILE'), 'test_another_profile')
        self.assertEqual(os.environ.get('AWS_ACCESS_KEY_ID'), None)
        self.assertEqual(os.environ.get('AWS_SECRET_ACCESS_KEY'), None)
        self.assertEqual(os.environ.get('AWS_DEFAULT_REGION'), None)
    def test_load_aws_profile(self):
        AWSCredentialsManager.clean_current_profile()

        AWSCredentialsManager.add_aws_profile(
            aws_profile='test_profile',
            aws_access_key_id='test_id',
            aws_secret_access_key='test_secret',
            aws_default_region='test_region',
        )

        AWSCredentialsManager.load_aws_profile('test_profile')

        self.assertEqual(os.environ.get('AWS_PROFILE'), None)
        self.assertEqual(os.environ.get('AWS_ACCESS_KEY_ID'), 'test_id')
        self.assertEqual(os.environ.get('AWS_SECRET_ACCESS_KEY'),
                         'test_secret')
        self.assertEqual(os.environ.get('AWS_DEFAULT_REGION'), 'test_region')

        AWSCredentialsManager.load_aws_profile('test_another_profile')

        self.assertEqual(os.environ.get('AWS_PROFILE'), 'test_another_profile')
        self.assertEqual(os.environ.get('AWS_ACCESS_KEY_ID'), None)
        self.assertEqual(os.environ.get('AWS_SECRET_ACCESS_KEY'), None)
        self.assertEqual(os.environ.get('AWS_DEFAULT_REGION'), None)
Ejemplo n.º 18
0
    def manage_credentials_decorator(*args, **kwargs):
        AWSCredentialsManager.save_environment_state()

        func(*args, **kwargs)

        AWSCredentialsManager.load_saved_environment_state()