SHARE
TWEET

Untitled

a guest Oct 4th, 2017 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from __future__ import with_statement
  2. from fabric.api import env, local, sudo, settings, hide, run, prompt
  3. from fabric.utils import warn
  4.  
  5. def vagrant():
  6.     env.user = 'vagrant'
  7.     env.hosts = ['127.0.0.1:2222']
  8.  
  9.     result = local('vagrant global-status | grep running', capture=True)
  10.     machineId = result.split()[0]
  11.    
  12.     result = local('vagrant ssh-config | grep IdentityFile'.format(machineId), capture=True)
  13.     env.key_filename = result.split()[1]
  14.  
  15. def install_mysql():
  16.     # First ensure that MySQL has not already been installed
  17.     if package_installed('mysql-server'):
  18.         warn('MySQL has already been installed')
  19.         return
  20.  
  21.     # Prompt the user for a MySQL root password
  22.     mysql_root_password = prompt('Please input your MySQL root password:')
  23.     mysql_root_password_again = prompt('Please input your MySQL root password again:')
  24.     if (mysql_root_password != mysql_root_password_again):
  25.         warn('Passwords must match')
  26.         return
  27.  
  28.     # Prompt the user for a MySQL user and password
  29.     mysql_user = prompt('Please input your MySQL username:')
  30.     mysql_password = prompt('Please input your MySQL password:')
  31.     mysql_password_again = prompt('Please input your MySQL password again:')
  32.     if (mysql_password != mysql_password_again):
  33.         warn('Passwords must match')
  34.         return
  35.  
  36.    
  37.     sudo('echo "mysql-server-5.0 mysql-server/root_password password %s" | debconf-set-selections' % mysql_root_password)
  38.     sudo('echo "mysql-server-5.0 mysql-server/root_password_again password %s" | debconf-set-selections' % mysql_root_password_again)
  39.     apt_get('mysql-server')
  40.  
  41.     # Update my.cnf
  42.     sudo('sed -i "/^bind-address.*/ s/^#*/#/" /etc/mysql/my.cnf')
  43.     sudo('sed -i "/^skip-external-locking/ s/^#*/#/" /etc/mysql/my.cnf')
  44.     sudo('service mysql restart')
  45.  
  46.     # Create user and grant priveleges
  47.     run('mysql -uroot -p%s -e "create user \'%s\'@\'%%\' identified by \'%s\'"' % (mysql_root_password, mysql_user, mysql_password))
  48.     run('mysql -uroot -p%s -e "grant all on *.* to \'%s\'@\'%%\' identified by \'%s\'"' % (mysql_root_password, mysql_user, mysql_password))
  49.  
  50. # Utils
  51. def package_installed(pkg_name):
  52.     cmd_f = 'dpkg-query -l "%s" | grep -q ^.i'
  53.     cmd = cmd_f % (pkg_name)
  54.     with settings(hide('warnings', 'stderr'), warn_only=True):
  55.         result = run(cmd)
  56.     return result.succeeded
  57.  
  58. def apt_get(*packages):
  59.     sudo('apt-get -y --no-upgrade install %s' % ' '.join(packages), shell=False)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top