Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import with_statement
- from fabric.api import env, local, sudo, settings, hide, run, prompt
- from fabric.utils import warn
- def vagrant():
- env.user = 'vagrant'
- env.hosts = ['127.0.0.1:2222']
- result = local('vagrant global-status | grep running', capture=True)
- machineId = result.split()[0]
- result = local('vagrant ssh-config | grep IdentityFile'.format(machineId), capture=True)
- env.key_filename = result.split()[1]
- def install_postgresql():
- # First ensure that MySQL has not already been installed
- if package_installed('postgresql') and package_installed('postgresql-contrib'):
- warn('PostgreSQL has already been installed')
- return
- # Prompt the user for a Postgres username and password
- pg_username = prompt('Please input your postgres username:')
- if (pg_username == "user"):
- warn('Cannot name your user "user"')
- return
- pg_password = prompt('Please input your postgres password:')
- pg_password_again = prompt('Please input your postgres password again:')
- if (pg_password != pg_password_again):
- warn('Passwords must match')
- return
- pg_db = prompt('Please input your postgres database name:')
- if not package_installed('postgresql'):
- apt_get('postgresql')
- if not package_installed('postgresql-contrib'):
- apt_get('postgresql-contrib')
- # Update config files
- postgresqlconf_path = sudo('find / -name "postgresql.conf"')
- sudo('sed -i "/^#listen_addresses.*/ s/^#//" %s' % postgresqlconf_path)
- sudo('sed -i "/^listen_addresses.*/ s/\'localhost\'/\'\*\'/" %s' % postgresqlconf_path)
- pg_hba_conf_path = sudo('find / -name "pg_hba.conf"')
- lines = "host all all 0.0.0.0/0 md5\nhost all all ::/0 md5"
- sudo("echo '%s' >> %s" % (lines, pg_hba_conf_path))
- sudo('service postgresql restart')
- # Create postgres user and password
- if not is_pg_user(pg_username):
- run_pg_command('''psql -t -A -c "CREATE USER %s WITH PASSWORD '%s';"''' % (pg_username, pg_password))
- # Create database
- if not is_pg_db(pg_db):
- run_pg_command('createdb %s -O %s' % (pg_db, pg_username))
- # Utils
- def run_pg_command(command):
- return sudo('sudo -u postgres %s' % command)
- def is_pg_user(username):
- with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):
- return run_pg_command('''psql -t -A -c "SELECT COUNT(*) FROM pg_user WHERE usename = '%s';"''' % username) == "1"
- def is_pg_db(db):
- with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):
- return run_pg_command('''psql -t -A -c "SELECT COUNT(*) FROM pg_database WHERE datname = '%s';"''' % db) == "1"
- def package_installed(pkg_name):
- cmd_f = 'dpkg-query -l "%s" | grep -q ^.i'
- cmd = cmd_f % (pkg_name)
- with settings(hide('warnings', 'stderr'), warn_only=True):
- result = run(cmd)
- return result.succeeded
- def apt_get(*packages):
- sudo('apt-get -y --no-upgrade install %s' % ' '.join(packages), shell=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement