Advertisement
Guest User

Untitled

a guest
Oct 9th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  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_postgresql():
  16. # First ensure that MySQL has not already been installed
  17. if package_installed('postgresql') and package_installed('postgresql-contrib'):
  18. warn('PostgreSQL has already been installed')
  19. return
  20.  
  21. # Prompt the user for a Postgres username and password
  22. pg_username = prompt('Please input your postgres username:')
  23. if (pg_username == "user"):
  24. warn('Cannot name your user "user"')
  25. return
  26. pg_password = prompt('Please input your postgres password:')
  27. pg_password_again = prompt('Please input your postgres password again:')
  28. if (pg_password != pg_password_again):
  29. warn('Passwords must match')
  30. return
  31. pg_db = prompt('Please input your postgres database name:')
  32.  
  33. if not package_installed('postgresql'):
  34. apt_get('postgresql')
  35.  
  36. if not package_installed('postgresql-contrib'):
  37. apt_get('postgresql-contrib')
  38.  
  39. # Update config files
  40. postgresqlconf_path = sudo('find / -name "postgresql.conf"')
  41.  
  42. sudo('sed -i "/^#listen_addresses.*/ s/^#//" %s' % postgresqlconf_path)
  43. sudo('sed -i "/^listen_addresses.*/ s/\'localhost\'/\'\*\'/" %s' % postgresqlconf_path)
  44.  
  45. pg_hba_conf_path = sudo('find / -name "pg_hba.conf"')
  46. lines = "host all all 0.0.0.0/0 md5\nhost all all ::/0 md5"
  47. sudo("echo '%s' >> %s" % (lines, pg_hba_conf_path))
  48. sudo('service postgresql restart')
  49.  
  50. # Create postgres user and password
  51. if not is_pg_user(pg_username):
  52. run_pg_command('''psql -t -A -c "CREATE USER %s WITH PASSWORD '%s';"''' % (pg_username, pg_password))
  53.  
  54. # Create database
  55. if not is_pg_db(pg_db):
  56. run_pg_command('createdb %s -O %s' % (pg_db, pg_username))
  57.  
  58. # Utils
  59. def run_pg_command(command):
  60. return sudo('sudo -u postgres %s' % command)
  61.  
  62. def is_pg_user(username):
  63. with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):
  64. return run_pg_command('''psql -t -A -c "SELECT COUNT(*) FROM pg_user WHERE usename = '%s';"''' % username) == "1"
  65.  
  66. def is_pg_db(db):
  67. with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):
  68. return run_pg_command('''psql -t -A -c "SELECT COUNT(*) FROM pg_database WHERE datname = '%s';"''' % db) == "1"
  69.  
  70. def package_installed(pkg_name):
  71. cmd_f = 'dpkg-query -l "%s" | grep -q ^.i'
  72. cmd = cmd_f % (pkg_name)
  73. with settings(hide('warnings', 'stderr'), warn_only=True):
  74. result = run(cmd)
  75. return result.succeeded
  76.  
  77. def apt_get(*packages):
  78. sudo('apt-get -y --no-upgrade install %s' % ' '.join(packages), shell=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement