def setup_mysql(): sudo('apt-get install -y mysql-server python-mysqldb') with virtualenv(Settings.DIR_VENV): run('pip install mysqlclient') if not mysql.user_exists(Settings.DB_USER_NAME): mysql.create_user(Settings.DB_USER_NAME, password=Settings.DB_PASSWORD) if not mysql.database_exists(Settings.DB_NAME): mysql.create_database(Settings.DB_NAME, Settings.DB_USER_NAME)
def test_create_user(mysql_server): from fabtools.mysql import create_user, query, user_exists with settings(mysql_user='******', mysql_password=MYSQL_ROOT_PASSWORD): try: create_user('bob', 'password', host='host1') create_user('bob', 'password', host='host2') assert user_exists('bob', host='host1') assert user_exists('bob', host='host2') assert not user_exists('bob', host='localhost') finally: query('DROP USER bob@host1;') query('DROP USER bob@host2;')
def user(name, password, **kwargs): """ Require a MySQL user. Extra arguments will be passed to :py:func:`fabtools.mysql.create_user`. Example:: from fabtools import require require.mysql.user('dbuser', 'somerandomstring') """ if not user_exists(name, **kwargs): create_user(name, password, **kwargs)
def user(name, password, **kwargs): """ Require a MySQL user. Extra arguments will be passed to :py:func:`fabtools.mysql.create_user`. Example:: from fabric.api import settings from fabtools import require with settings(mysql_user='******', mysql_password='******'): require.mysql.user('dbuser', 'somerandomstring') """ if not user_exists(name, **kwargs): create_user(name, password, **kwargs)
def db_init(): if not mysql.user_exists('featuring'): mysql.create_user('featuring', '123123') if not mysql.database_exists('featuring'): mysql.create_database('featuring', owner='featuring')