system.models.Configuration.get_by_name_as_int

Here are the examples of the python api system.models.Configuration.get_by_name_as_int taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

19 Examples 7

Example 1

Project: database-as-a-service
Source File: models.py
View license
def user_m2m_changed(sender, **kwargs):
    team = kwargs.get('instance')
    action = kwargs.get('action')
    if action == 'post_add':
        if Configuration.get_by_name_as_int('laas_integration') == 1:
            register_team_laas_task.delay(team)

Example 2

Project: database-as-a-service
Source File: snapshot.py
View license
    def changelist_view(self, request, extra_context=None):
        extra_context = extra_context or {}

        backup_avaliable = Configuration.get_by_name_as_int(
            'backup_avaliable')

        extra_context['backup_avaliable'] = False
        if backup_avaliable:
            extra_context['backup_avaliable'] = True

        return super(SnapshotAdmin, self).changelist_view(request, extra_context=extra_context)

Example 3

Project: database-as-a-service
Source File: mysqldb.py
View license
    def __mysql_client__(self, instance, database='mysql'):
        connection_address, connection_port = self.__get_admin_connection(
            instance)
        try:
            LOG.debug(
                'Connecting to mysql databaseinfra %s', self.databaseinfra)
            # mysql uses timeout in seconds
            connection_timeout_in_seconds = Configuration.get_by_name_as_int(
                'mysql_connect_timeout', default=MYSQL_CONNECTION_DEFAULT_TIMEOUT)

            client = mysqldb.connect(host=connection_address, port=int(connection_port),
                                     user=self.databaseinfra.user, passwd=self.databaseinfra.password,
                                     db=database, connect_timeout=connection_timeout_in_seconds)
            LOG.debug(
                'Successfully connected to mysql databaseinfra %s' % (self.databaseinfra))
            return client
        except Exception as e:
            raise e

Example 4

Project: database-as-a-service
Source File: redis.py
View license
    def get_sentinel_client(self, instance=None):
        connection_timeout_in_seconds = Configuration.get_by_name_as_int(
            'redis_connect_timeout', default=REDIS_CONNECTION_DEFAULT_TIMEOUT)
        sentinels = self.__get_admin_sentinel_connection(instance)
        sentinel = Sentinel(
            sentinels, socket_timeout=connection_timeout_in_seconds)
        return sentinel

Example 5

Project: database-as-a-service
Source File: models.py
View license
    def get_log_url(self):
        if Configuration.get_by_name_as_int('laas_integration') != 1:
            return ""

        if self.databaseinfra.plan.is_pre_provisioned:
            return ""

        from util import get_credentials_for
        from util.laas import get_group_name
        from dbaas_credentials.models import CredentialType

        credential = get_credentials_for(
            environment=self.environment, credential_type=CredentialType.LOGNIT
        )
        return credential.endpoint + get_group_name(self)

Example 6

Project: database-as-a-service
Source File: models.py
View license
    def get_dex_url(self):
        if Configuration.get_by_name_as_int('dex_analyze') != 1:
            return ""

        if self.databaseinfra.plan.is_pre_provisioned:
            return ""

        if self.engine_type != 'mongodb':
            return ""

        return 1

Example 7

Project: database-as-a-service
Source File: models.py
View license
    @classmethod
    def purge_quarantine(self):
        quarantine_time = Configuration.get_by_name_as_int(
            'quarantine_retention_days')
        quarantine_time_dt = date.today() - timedelta(days=quarantine_time)
        databases = Database.objects.filter(
            is_in_quarantine=True, quarantine_dt__lte=quarantine_time_dt
        )
        for database in databases:
            database.delete()
            LOG.info("The database %s was deleted, because it was set to quarentine %d days ago" % (
                database.name, quarantine_time)
            )

Example 8

Project: database-as-a-service
Source File: errors.py
View license
    def __init__(self):
        auto_resize_gb = Configuration.get_by_name_as_int(
            name='auto_resize_max_size_in_gb', default=100
        )
        msg = 'Disk auto resize can not be greater than {}GB'.format(
            auto_resize_gb
        )
        super(OverflowError, self).__init__(msg)

Example 9

Project: database-as-a-service
Source File: models.py
View license
    @classmethod
    def last_offering_available_for_auto_resize(cls):
        parameter_in_kb = cls.converter_gb_to_kb(
            Configuration.get_by_name_as_int(
                name='auto_resize_max_size_in_gb', default=100
            )
        )

        disks = DiskOffering.objects.filter(
            size_kb__lte=parameter_in_kb
        ).order_by('-size_kb')

        if not disks:
            raise NoDiskOfferingLesserError(parameter_in_kb)
        return disks[0]

Example 10

Project: database-as-a-service
Source File: create_log.py
View license
    def do(self, workflow_dict):
        try:
            if Configuration.get_by_name_as_int('laas_integration') == 1:
                register_database_laas(workflow_dict['database'])

            return True

        except Exception:
            traceback = full_stack()

            workflow_dict['exceptions']['error_codes'].append(DBAAS_0018)
            workflow_dict['exceptions']['traceback'].append(traceback)

            return False

Example 11

Project: database-as-a-service
Source File: tasks.py
View license
@app.task(bind=True)
@only_one(key="removedatabaseoldbackupkey", timeout=1200)
def remove_database_old_backups(self):

    worker_name = get_worker_name()
    task_history = TaskHistory.register(request=self.request,
                                        worker_name=worker_name, user=None)

    backup_retention_days = Configuration.get_by_name_as_int(
        'backup_retention_days')

    LOG.info("Removing backups older than %s days" % (backup_retention_days))

    backup_time_dt = date.today() - timedelta(days=backup_retention_days)
    snapshots = Snapshot.objects.filter(start_at__lte=backup_time_dt,
                                        purge_at__isnull=True,
                                        instance__isnull=False,
                                        snapshopt_id__isnull=False)
    msgs = []
    status = TaskHistory.STATUS_SUCCESS
    if len(snapshots) == 0:
        msgs.append("There is no snapshot to purge")
    for snapshot in snapshots:
        try:
            remove_snapshot_backup(snapshot=snapshot)
            msg = "Backup %s removed" % (snapshot)
            LOG.info(msg)
        except Exception as e:
            msg = "Error removing backup %s. Error: %s" % (snapshot, str(e))
            status = TaskHistory.STATUS_ERROR
            LOG.error(msg)
        msgs.append(msg)

    task_history.update_status_for(status, details="\n".join(msgs))

    return

Example 12

Project: database-as-a-service
Source File: mongodb.py
View license
    def __mongo_client__(self, instance):
        connection_address = self.__get_admin_connection(instance)
        if not self.databaseinfra and instance:
            self.databaseinfra = instance.databaseinfra
        try:
            # mongo uses timeout in mili seconds
            connection_timeout_in_miliseconds = Configuration.get_by_name_as_int(
                'mongo_connect_timeout', default=MONGO_CONNECTION_DEFAULT_TIMEOUT) * 1000

            client = pymongo.MongoClient(
                connection_address, connectTimeoutMS=connection_timeout_in_miliseconds)
            if self.databaseinfra.user and self.databaseinfra.password:
                LOG.debug(
                    'Authenticating databaseinfra %s', self.databaseinfra)
                client.admin.authenticate(
                    self.databaseinfra.user, self.databaseinfra.password)
            return client
        except TypeError:
            raise AuthenticationError(
                message='Invalid address: ' % connection_address)

Example 13

Project: database-as-a-service
Source File: redis.py
View license
    def __redis_client__(self, instance):

        try:
            LOG.debug(
                'Connecting to redis databaseinfra %s', self.databaseinfra)
            # redis uses timeout in seconds
            connection_timeout_in_seconds = Configuration.get_by_name_as_int(
                'redis_connect_timeout', default=REDIS_CONNECTION_DEFAULT_TIMEOUT)

            if (instance and instance.instance_type == Instance.REDIS) or (not self.databaseinfra.plan.is_ha and not instance):
                connection_address, connection_port = self.__get_admin_single_connection(
                    instance)
                client = redis.Redis(host=connection_address,
                                     port=int(connection_port),
                                     password=self.databaseinfra.password,
                                     socket_connect_timeout=connection_timeout_in_seconds)

            else:
                sentinel = self.get_sentinel_client(instance)
                client = sentinel.master_for(self.databaseinfra.name,
                                             socket_timeout=connection_timeout_in_seconds,
                                             password=self.databaseinfra.password)

            LOG.debug(
                'Successfully connected to redis databaseinfra %s' % (self.databaseinfra))
            return client
        except Exception as e:
            raise e

Example 14

Project: database-as-a-service
Source File: models.py
View license
    def restore_allowed(self):
        if Configuration.get_by_name_as_int('restore_allowed') == 1:
            return True

        return False

Example 15

Project: database-as-a-service
Source File: tasks.py
View license
@app.task(bind=True)
@only_one(key="purgequarantinekey", timeout=1000)
def purge_quarantine(self,):
    user = AccountUser.objects.get(username='admin')
    AuditRequest.new_request("purge_quarantine", user, "localhost")
    try:

        task_history = TaskHistory.register(request=self.request, user=user)

        LOG.info("id: %s | task: %s | kwargs: %s | args: %s" % (
            self.request.id, self.request.task, self.request.kwargs, str(self.request.args)))
        quarantine_time = Configuration.get_by_name_as_int(
            'quarantine_retention_days')
        quarantine_time_dt = date.today() - timedelta(days=quarantine_time)

        databases = Database.objects.filter(is_in_quarantine=True,
                                            quarantine_dt__lte=quarantine_time_dt)

        for database in databases:
            if database.plan.provider == database.plan.CLOUDSTACK:
                databaseinfra = database.databaseinfra

                destroy_infra(databaseinfra=databaseinfra, task=task_history)
            else:
                database.delete()

            LOG.info("The database %s was deleted, because it was set to quarentine %d days ago" % (
                database.name, quarantine_time))

        task_history.update_status_for(
            TaskHistory.STATUS_SUCCESS, details='Databases destroyed successfully')
        return

    except Exception:
        task_history.update_status_for(
            TaskHistory.STATUS_ERROR, details="Error")
        return
    finally:
        AuditRequest.cleanup_request()

Example 16

Project: database-as-a-service
Source File: tasks.py
View license
@app.task
@only_one(key="db_infra_notification_key", timeout=20)
def databaseinfra_notification(self, user=None):
    worker_name = get_worker_name()
    task_history = TaskHistory.register(
        request=self.request, user=user, worker_name=worker_name)
    threshold_infra_notification = Configuration.get_by_name_as_int(
        "threshold_infra_notification", default=0)
    if threshold_infra_notification <= 0:
        LOG.warning("database infra notification is disabled")
        return

    # Sum capacity per databseinfra with parameter plan, environment and engine
    infras = DatabaseInfra.objects.values(
        'plan__name', 'environment__name', 'engine__engine_type__name',
        'plan__provider'
    ).annotate(capacity=Sum('capacity'))
    for infra in infras:
        try:
            database = infra.databases.get()
        except Database.MultipleObjectsReturned:
            pass
        else:
            if database.is_in_quarantine:
                continue
            if not database.subscribe_to_email_events:
                continue

        used = DatabaseInfra.objects.filter(
            plan__name=infra['plan__name'], environment__name=infra['environment__name'],
            engine__engine_type__name=infra['engine__engine_type__name']
        ).aggregate(used=Count('databases'))
        # calculate the percentage

        percent = int(used['used'] * 100 / infra['capacity'])
        if percent >= threshold_infra_notification and infra['plan__provider'] != Plan.CLOUDSTACK:
            LOG.info('Plan %s in environment %s with %s%% occupied' % (
                infra['plan__name'], infra['environment__name'], percent))
            LOG.info("Sending database infra notification...")
            context = {}
            context['plan'] = infra['plan__name']
            context['environment'] = infra['environment__name']
            context['used'] = used['used']
            context['capacity'] = infra['capacity']
            context['percent'] = percent
            email_notifications.databaseinfra_ending(context=context)

        task_history.update_status_for(
            TaskHistory.STATUS_SUCCESS,
            details='Databaseinfra Notification successfully sent to dbaas admins!'
        )
    return

Example 17

Project: database-as-a-service
Source File: tasks.py
View license
def database_notification_for_team(team=None):
    """
    Notifies teams of database usage.
    if threshold_database_notification <= 0, the notification is disabled.
    """
    LOG.info("sending database notification for team %s" % team)
    threshold_database_notification = Configuration.get_by_name_as_int(
        "threshold_database_notification", default=0)
    # if threshold_database_notification
    if threshold_database_notification <= 0:
        LOG.warning("database notification is disabled")
        return

    databases = Database.objects.filter(
        team=team, is_in_quarantine=False, subscribe_to_email_events=True
    )
    msgs = []
    for database in databases:
        used = database.used_size_in_mb
        capacity = database.total_size_in_mb
        try:
            percent_usage = (used / capacity) * 100
        except ZeroDivisionError:
            # database has no total size
            percent_usage = 0.0
        msg = "database %s => usage: %.2f | threshold: %.2f" % (
            database, percent_usage, threshold_database_notification)
        LOG.info(msg)
        msgs.append(msg)

        if not team.email:
            msgs.append(
                "team %s has no email set and therefore no database usage notification will been sent" % team)
        else:
            if percent_usage >= threshold_database_notification:
                LOG.info("Sending database notification...")
                context = {}
                context['database'] = database.name
                context['team'] = team
                context['measure_unity'] = "MB"
                context['used'] = used
                context['capacity'] = capacity
                context['percent'] = "%.2f" % percent_usage
                context['environment'] = database.environment.name
                email_notifications.database_usage(context=context)

    return msgs

Example 18

Project: database-as-a-service
Source File: tasks.py
View license
@app.task(bind=True)
@only_one(key="purge_task_history", timeout=600)
def purge_task_history(self):
    try:
        worker_name = get_worker_name()
        task_history = TaskHistory.register(
            request=self.request, user=None, worker_name=worker_name)

        now = datetime.datetime.now()
        retention_days = Configuration.get_by_name_as_int(
            'task_history_retention_days')

        n_days_before = now - datetime.timedelta(days=retention_days)

        tasks_to_purge = TaskHistory.objects.filter(
            task_name__in=[
                'notification.tasks.database_notification',
                'notification.tasks.database_notification_for_team',
                'notification.tasks.update_database_used_size',
                'notification.tasks.update_disk_used_size',
                'notification.tasks.update_database_status',
                'notification.tasks.update_instances_status',
                'system.tasks.set_celery_healthcheck_last_update'
            ],
            ended_at__lt=n_days_before,
            task_status__in=["SUCCESS", "ERROR", "WARNING"])

        tasks_to_purge.delete()

        task_history.update_status_for(TaskHistory.STATUS_SUCCESS,
                                       details='Purge succesfully done!')
    except Exception as e:
        task_history.update_status_for(TaskHistory.STATUS_ERROR, details=e)

Example 19

View license
def zabbix_collect_used_disk(task):
    status = TaskHistory.STATUS_SUCCESS
    threshold_disk_resize = Configuration.get_by_name_as_int(
        "threshold_disk_resize", default=80.0
    )
    collected = 0
    resizes = 0
    problems = 0

    integration = CredentialType.objects.get(type=CredentialType.ZABBIX)
    for environment in Environment.objects.all():
        task.add_detail(
            'Execution for environment: {}'.format(environment.name)
        )
        credentials = Credential.get_credentials(
            environment=environment, integration=integration
        )

        for database in Database.objects.filter(environment=environment):
            database_resized = False
            task.add_detail(
                message='Database: {}'.format(database.name), level=1
            )

            zabbix_provider = factory_for(
                databaseinfra=database.databaseinfra, credentials=credentials
            )
            metrics = ZabbixMetrics(
                zabbix_provider.api, zabbix_provider.main_clientgroup
            )

            driver = database.databaseinfra.get_driver()
            non_database_instances = driver.get_non_database_instances()

            for host in zabbix_provider.hosts:
                instance = database.databaseinfra.instances.filter(address=host.address).first()
                if instance in non_database_instances:
                    continue

                collected += 1
                task.add_detail(
                    message='Host: {} ({})'.format(
                        host.hostname, host.address
                    ), level=2
                )

                try:
                    zabbix_size = metrics.get_current_disk_data_size(host)
                    zabbix_used = metrics.get_current_disk_data_used(host)
                    zabbix_percentage = (zabbix_used * 100)/zabbix_size
                except ZabbixMetricsError as error:
                    problems += 1
                    task.add_detail(message='Error: {}'.format(error), level=3)
                    status = TaskHistory.STATUS_WARNING
                    continue

                task.add_detail(
                    message='Zabbix /data: {}% ({}kb/{}kb)'.format(
                        zabbix_percentage, zabbix_used, zabbix_size
                    ), level=3
                )

                current_percentage = zabbix_percentage
                current_used = zabbix_used
                current_size = zabbix_size
                if zabbix_percentage >= threshold_disk_resize:
                    current_percentage, current_used, current_size = host_mount_data_percentage(
                        address=host.address, task=task
                    )
                    if zabbix_percentage > current_percentage:
                        problems += 1
                        status = TaskHistory.STATUS_WARNING
                        task.add_detail(
                            message='Zabbix metrics not updated', level=4
                        )

                if current_percentage >= threshold_disk_resize and \
                        database.disk_auto_resize and \
                        not database_resized:
                    try:
                        task_resize = disk_auto_resize(
                            database=database,
                            current_size=current_size,
                            usage_percentage=current_percentage
                        )
                        database_resized = True
                    except Exception as e:
                        problems += 1
                        status = TaskHistory.STATUS_WARNING
                        task.add_detail(
                            message='Could not do resize. {}'.format(e),
                            level=4
                        )
                    else:
                        resizes += 1
                        task.add_detail(
                            message='Executing Resize... Task: {}'.format(
                                task_resize.id
                            ), level=4
                        )

                if not update_disk(
                    database=database, address=host.address, task=task,
                    total_size=current_size, used_size=current_used
                ):
                    problems += 1
                    status = TaskHistory.STATUS_WARNING

            zabbix_provider.logout()

    details = 'Collected: {} | Resize: {} | Problems: {}'.format(
        collected, resizes, problems
    )
    task.update_status_for(status=status, details=details)