database Package

database Package

Roles in this namespace are meant to enable database management for database servers as MySQL, MongoDB, Redis and such, in Debian distributions.

mongodb Module

Roles in this namespace are meant to provide MongoDB database management utilities for Debian distributions.

class provy.more.debian.database.mongodb.MongoDBRole(prov, context)[source]

Bases: provy.core.roles.Role

This role provides MongoDB database management utilities for Debian distributions.

Example:

from provy.core import Role
from provy.more.debian import MongoDBRole

class MySampleRole(Role):
    def provision(self):
        with self.using(MongoDBRole) as role:
            role.restart()
configure(configuration)[source]

Configures the MongoDB database according to a dictionary.

Note

Some important details about this method:

  • It will leave configuration items untouched if they’re not changed;
  • It will create a new configuration item if it doesn’t exist yet;
  • It will overwrite the configuration items defined in the original configuration by the ones defined in the configuration argument, if they have the same name;
  • It will convert boolean items to lowercase (like True to “true”), when writing, to follow the mongodb.conf conventions;
  • It will leave file comments untouched, to avoid losing potentially important information;
Parameters:configuration (dict) – The intended configuration items.

Example:

from provy.core import Role
from provy.more.debian import MongoDBRole

class MySampleRole(Role):
    def provision(self):
        with self.using(MongoDBRole) as mongo:
            mongo.configure({
                'port': 9876,
                'replSet': 'my_replica_set',
            })
provision()[source]

Installs MongoDB and its dependencies. This method should be called upon if overriden in base classes, or MongoDB won’t work properly in the remote server.

Example:

from provy.core import Role
from provy.more.debian import MongoDBRole

class MySampleRole(Role):
    def provision(self):
        self.provision_role(MongoDBRole) # no need to call this if using with block.
provision_to_debian()[source]

Installs MongoDB and its dependencies via Debian-specific repository. It’s not recommended that you use this method directly; Instead, provision this role directly and it will find out the best way to provision.

Example:

from provy.core import Role
from provy.more.debian import MongoDBRole

class MySampleRole(Role):
    def provision(self):
        with self.using(MongoDBRole) as mongo:
            mongo.provision_to_debian()
provision_to_ubuntu()[source]

Installs MongoDB and its dependencies via Ubuntu-specific repository. It’s not recommended that you use this method directly; Instead, provision this role directly and it will find out the best way to provision.

Example:

from provy.core import Role
from provy.more.debian import MongoDBRole

class MySampleRole(Role):
    def provision(self):
        with self.using(MongoDBRole) as mongo:
            mongo.provision_to_ubuntu()
restart()[source]

Restarts the MongoDB database.

Example:

from provy.core import Role
from provy.more.debian import MongoDBRole

class MySampleRole(Role):
    def provision(self):
        with self.using(MongoDBRole) as mongo:
            mongo.restart()

mysql Module

Roles in this namespace are meant to provide MySQL database management utilities for Debian distributions.

class provy.more.debian.database.mysql.MySQLRole(prov, context)[source]

Bases: provy.core.roles.Role

This role provides MySQL database management utilities for Debian distributions.

This role uses two context keys: mysql_root_user and mysql_root_pass. If none are found, it uses ‘root’ and empty password.

Example:

from provy.core import Role
from provy.more.debian import MySQLRole

class MySampleRole(Role):
    def provision(self):
        with self.using(MySQLRole) as role:
            role.ensure_user(username=self.context['mysql_user'], identified_by=self.context['mysql_password'])
            role.ensure_database(self.context['mysql_database'], owner=self.context['mysql_user'])
ensure_database(database_name)[source]

Creates the database if it does not exist.

Parameters:database_name (str) – Database to create.
Returns:Whether the database had to be created or not.
Return type:bool

Example:

class MySampleRole(Role):
    def provision(self):
        with self.using(MySQLRole) as role:
            role.ensure_database('database')
ensure_grant(privileges, on, username, login_from='%', with_grant_option=False)[source]

Ensures that the given user has the given privileges on the specified location.

Parameters:
  • privileges (str) – Privileges to assign to user (e.g.: “ALL PRIVILEGES”).
  • on (str) – Object to assign privileges to. If only the name is supplied, ‘.*’ will be appended to the name. If you want all databases pass ‘.‘.
  • username (str) – User to grant the privileges to.
  • login_from (str) – Location where the user gets the grants. Defaults to ‘%’ (anywhere).
  • with_grant_option (bool) – If True, indicates that this user may grant other users the same privileges. Defaults to False.
Returns:

Whether the grant had to be added or not.

Return type:

bool

Example:

class MySampleRole(Role):
    def provision(self):
        with self.using(MySQLRole) as role:
            role.ensure_grant('ALL PRIVILEGES',
                              on='database',
                              username='backend',
                              login_from='%',
                              with_grant_option=True)
ensure_user(username, identified_by, login_from='%')[source]

Ensure the given user is created in the database and can login from the specified location.

Parameters:
  • username (str) – Name of the user to be created.
  • identified_by (str) – Password that the user will use to login to mysql server.
  • login_from (str) – Locations that this user can login from. Defaults to ‘%’ (anywhere).
Returns:

Whether the user had to be created or not.

Return type:

bool

Example:

class MySampleRole(Role):
    def provision(self):
        with self.using(MySQLRole) as role:
            role.ensure_user('someuser', 'somepass', 'localhost')
get_user_grants(username, login_from='%')[source]

Returns all grants for the given user at the given location.

Parameters:
  • username (str) – Name of the user to be verify.
  • login_from (str) – Locations that this user can login from. Defaults to ‘%’ (anywhere).
Returns:

The user grants.

Return type:

list

Example:

class MySampleRole(Role):
    def provision(self):
        with self.using(MySQLRole) as role:
            if role.get_user_grants('user', login_from='%'):
                pass
get_user_hosts(username)[source]

Returns all the available hosts that this user can login from.

Parameters:username (str) – Name of the user to be verified.
Returns:The user hosts.
Return type:list

Example:

class MySampleRole(Role):
    def provision(self):
        with self.using(MySQLRole) as role:
            if not '%' in role.get_user_hosts('someuser'):
                pass
has_grant(privileges, on, username, login_from, with_grant_option)[source]

Returns True if the user has the specified privileges on the specified object in the given location.

Parameters:
  • privileges (str) – Privileges that are being verified.
  • on (str) – Database object that the user holds privileges on.
  • username (str) – Name of the user to be verify.
  • login_from (str) – Locations that this user can login from. Defaults to ‘%’ (anywhere).
  • with_grant_option (bool) – Indicates if we are verifying against grant option.
Returns:

Whether the user has the privileges or not.

Return type:

bool

Example:

class MySampleRole(Role):
    def provision(self):
        with self.using(MySQLRole) as role:
            if role.has_grant('ALL PRIVILEGES',
                              'database',
                              'user',
                              login_from='%',
                              with_grant_option=True):
                pass
is_database_present(database_name)[source]

Returns True if the database is already created.

Parameters:database_name (str) – Database to verify.
Returns:Whether the database is present or not.
Return type:bool

Example:

class MySampleRole(Role):
    def provision(self):
        with self.using(MySQLRole) as role:
            if role.is_database_present('database'):
                pass
provision()[source]

Installs MySQL Server and its dependencies. This method should be called upon if overriden in base classes, or MySQL won’t work properly in the remote server.

Example:

class MySampleRole(Role):
    def provision(self):
        self.provision_role(MySQLRole) # no need to call this if using with block.
user_exists(username, login_from='%')[source]

Returns True if the given user exists for the given location in mysql server.

Parameters:
  • username (str) – Name of the user to be verified.
  • login_from (str) – Locations that this user can login from. Defaults to ‘%’ (anywhere).
Returns:

Whether the user exists.

Return type:

bool

Example:

class MySampleRole(Role):
    def provision(self):
        with self.using(MySQLRole) as role:
            if not role.user_exists('someuser'):
                pass

postgresql Module

Roles in this namespace are meant to provide PostgreSQL database management utilities for Debian distributions.

class provy.more.debian.database.postgresql.PostgreSQLRole(prov, context)[source]

Bases: provy.more.base.database.postgresql.BasePostgreSQLRole

This role provides PostgreSQL database management utilities for Debian distributions.

Take a look at provy.more.base.database.postgresql.BasePostgreSQLRole for more available methods.

Example:

from provy.core import Role
from provy.more.debian import PostgreSQLRole

class MySampleRole(Role):
    def provision(self):
        with self.using(PostgreSQLRole) as role:
            role.ensure_user("john")
            role.ensure_database("foo", owner="john")
provision()[source]

Installs PostgreSQL and its dependencies. This method should be called upon if overriden in base classes, or PostgreSQL won’t work properly in the remote server.

Example:

class MySampleRole(Role):
    def provision(self):
        self.provision_role(PostgreSQLRole) # no need to call this if using with block.

redis Module

Roles in this namespace are meant to provide Redis key-value store management utilities for Debian distributions.

class provy.more.debian.database.redis.RedisRole(prov, context)[source]

Bases: provy.core.roles.Role

This role provides Redis key-value store management utilities for Debian distributions.

Example:

from provy.core import Role
from provy.more.debian import RedisRole

class MySampleRole(Role):
    def provision(self):
        self.provision_role(RedisRole)
provision()[source]

Installs Redis and its dependencies. This method should be called upon if overriden in base classes, or Redis won’t work properly in the remote server.

Example:

class MySampleRole(Role):
    def provision(self):
        self.provision_role(RedisRole) # no need to call this if using with block.