Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. from fabric.context_managers import prefix
  2. from fabric.contrib.console import confirm
  3. from fabric.operations import prompt, sudo
  4. from fabric.api import run, task
  5. from fabric.state import env
  6. import os
  7.  
  8. env.user = 'root'
  9.  
  10.  
  11. @task
  12. def create_user(username, python='/usr/local/bin/python3.5'):
  13. run('useradd -m -s /bin/bash {}'.format(username))
  14. run_user = lambda x: sudo(x, user=username)
  15.  
  16. run_user('mkdir ~/logs')
  17. run_user('mkdir ~/www')
  18.  
  19. if confirm('Create virtualenv?'):
  20. run_user('{} -m venv --copies ~/venv'.format(python))
  21.  
  22. with prefix('source ~/venv/bin/activate'):
  23. run_user('pip install --upgrade pip setuptools wheel')
  24.  
  25. if confirm('Add ssh key?'):
  26. run_user('mkdir ~/.ssh')
  27. run_user('chmod 700 ~/.ssh')
  28.  
  29. with open(os.path.expanduser('~/.ssh/id_rsa.pub'), 'r') as fp:
  30. ssh_key = fp.read().strip()
  31. run_user('echo "{}" >> ~/.ssh/authorized_keys'.format(ssh_key))
  32.  
  33. run_user('chmod 600 ~/.ssh/authorized_keys')
  34.  
  35. if confirm('Install project now?', default=False):
  36. configs = prompt('Configs to install: ').split('')
  37.  
  38. for conf in configs:
  39. script_path = '/home/{user}/www/{conf}/system/install.sh'\
  40. .format(user=username, conf=conf)
  41.  
  42. run('chmod +x {}'.format(script_path))
  43. run('{} {}'.format(script_path, conf))
  44.  
  45.  
  46. @task
  47. def create_database():
  48. sql = lambda x: sudo('cd ~ && psql -c "{}"'.format(x), user='postgres')
  49.  
  50. username = prompt('Username: ')
  51. password = prompt('Password: ')
  52.  
  53. sql("CREATE USER {} WITH PASSWORD '{}'".format(username, password))
  54. dbnames = prompt('DB names: ').split(',')
  55.  
  56. for dbname in dbnames:
  57. sql("CREATE DATABASE {};".format(dbname))
  58. sql("GRANT ALL PRIVILEGES ON DATABASE {} TO {}".format(dbname, username))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement