Advertisement
Guest User

Untitled

a guest
Jan 26th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. from __future__ import with_statement
  2. from fabric.api import local, settings, abort, run, cd, env, sudo
  3. from fabric.colors import green as _green
  4. from fabric.colors import yellow as _yellow
  5. from fabric.colors import red as _red
  6. from fabric.contrib.console import confirm
  7. from fabric.contrib.project import rsync_project
  8. from fabric.contrib.files import upload_template, exists
  9. from fabric.operations import require
  10. from fabric.context_managers import prefix
  11. import os.path
  12. import inspect
  13. import sys
  14.  
  15. # -----------------------------------------------------------------------------
  16. # Globals
  17. # -----------------------------------------------------------------------------
  18.  
  19. env.project_name = 'feedback'
  20.  
  21. def _set_vagrant_env():
  22. env.python = 'python2.6'
  23.  
  24. env.path = '/home/vagrant/www'
  25. env.repo_path = '/vagrant'
  26. env.env_path = os.path.join(env.path, 'env')
  27. env.code_path = os.path.join(env.repo_path, env.project_name)
  28.  
  29. env.cmd_apache_start = 'sudo /usr/sbin/apache2ctl start'
  30. env.cmd_apache_stop = 'sudo /usr/sbin/apache2ctl stop'
  31. env.cmd_apache_restart = 'sudo /usr/sbin/apache2ctl restart'
  32.  
  33. env.mysql_admin_user = 'root'
  34. env.mysql_admin_pass = 'root'
  35.  
  36. try:
  37. sys.path.insert(0, env.project_name)
  38. from config.vagrant import DATABASES
  39. env.mysql_dbname = DATABASES['default']['NAME']
  40. env.mysql_user = DATABASES['default']['USER']
  41. env.mysql_pass = DATABASES['default']['PASSWORD']
  42.  
  43. except ImportError:
  44. print(_red('... Unable to get database configuration from Django project, falling back to defaults'))
  45. env.mysql_dbname = env.project_name
  46. env.mysql_user = env.project_name
  47. env.mysql_pass = env.project_name
  48.  
  49.  
  50. # -----------------------------------------------------------------------------
  51. # Environments
  52. # -----------------------------------------------------------------------------
  53.  
  54. def vagrant():
  55. """
  56. Select vagrant-managed VM for commands
  57. """
  58. env.settings = 'vagrant'
  59.  
  60. # get vagrant ssh setup
  61. vagrant_config = _get_vagrant_config()
  62. env.key_filename = vagrant_config['IdentityFile']
  63. env.hosts = ['%s:%s' % (vagrant_config['HostName'], vagrant_config['Port'])]
  64. env.user = vagrant_config['User']
  65.  
  66. _set_vagrant_env()
  67.  
  68. # -----------------------------------------------------------------------------
  69. # Main commands
  70. # -----------------------------------------------------------------------------
  71.  
  72. def setup():
  73. """
  74. Setup a fresh virtualenv, install everything we need, and fire up the database.
  75. """
  76. require('settings', provided_by=[vagrant])
  77.  
  78. mkdirs()
  79. setup_virtualenv()
  80. install_requirements()
  81. drop_database();
  82. create_database()
  83. populate_database()
  84. install_apache_conf()
  85. apache_restart()
  86.  
  87.  
  88. def destroy_world():
  89. """
  90. Removes everything
  91. """
  92. require('settings', provided_by=[vagrant])
  93.  
  94. mkdirs() # drop_database files are copied here
  95. remove_apache_conf()
  96. apache_restart()
  97. drop_database()
  98. rmdirs()
  99.  
  100.  
  101.  
  102. # -----------------------------------------------------------------------------
  103. # Database operations
  104. # -----------------------------------------------------------------------------
  105.  
  106. def create_database():
  107. """
  108. Create database and db user
  109. """
  110. print(_yellow('>>> starting %s()' % _fn()))
  111. require('settings', provided_by=[vagrant])
  112. if env.settings != 'vagrant':
  113. print('... skipping this action for non-vagrant environment')
  114. return
  115.  
  116. upload_template('sql/createdb.sql.template', '%(path)s/temp.sql' % env, context=env, use_jinja=True)
  117. run('mysql -u %(mysql_admin_user)s -p%(mysql_admin_pass)s --batch < %(path)s/temp.sql' % env)
  118. run('rm -f %(path)s/temp.sql' % env)
  119.  
  120.  
  121. def drop_database():
  122. """
  123. Create database and db user
  124. """
  125. print(_yellow('>>> starting %s()' % _fn()))
  126. require('settings', provided_by=[vagrant])
  127. if env.settings != 'vagrant':
  128. print('... skipping this action for non-vagrant environment')
  129. return
  130.  
  131. upload_template('sql/dropdb.sql.template', '%(path)s/temp.sql' % env, context=env, use_jinja=True)
  132. run('mysql -u %(mysql_admin_user)s -p%(mysql_admin_pass)s --batch < %(path)s/temp.sql' % env)
  133. run('rm -f %(path)s/temp.sql' % env)
  134.  
  135.  
  136. def populate_database():
  137. """
  138. Create initial database scheme and load initial data
  139. """
  140. print(_yellow('>>> starting %s()' % _fn()))
  141. require('settings', provided_by=[vagrant])
  142.  
  143. virtualenv('FLAVOR=%(settings)s %(python)s %(code_path)s/manage.py syncdb --noinput --migrate' % env)
  144. virtualenv('FLAVOR=%(settings)s %(python)s %(code_path)s/manage.py loaddata %(repo_path)s/sql/init.json' % env)
  145.  
  146.  
  147. def migrate():
  148. """
  149. Migrate database scheme and data
  150. """
  151. print(_yellow('>>> starting %s()' % _fn()))
  152. require('settings', provided_by=[vagrant])
  153.  
  154. virtualenv('FLAVOR=%(settings)s %(python)s %(code_path)s/manage.py migrate common' % env)
  155.  
  156. # -----------------------------------------------------------------------------
  157. # Helpers
  158. # -----------------------------------------------------------------------------
  159.  
  160. def dummy():
  161. """
  162. Dummy task for testing
  163. """
  164. print(_yellow('>>> starting %s()' % _fn()))
  165. require('settings', provided_by=[vagrant])
  166. run('uname -a && hostname && pwd')
  167.  
  168.  
  169. def wsgi_restart():
  170. """
  171. Gently restarts mod_wsgi by touching it's config
  172. """
  173. print(_yellow('>>> starting %s()' % _fn()))
  174. require('settings', provided_by=[vagrant])
  175.  
  176. if env.settings == 'vagrant':
  177. run('touch /etc/apache2/sites-available/%(project_name)s.wsgi' % env)
  178.  
  179. def apache_restart():
  180. """
  181. Restarts apache
  182. """
  183. print(_yellow('>>> starting %s()' % _fn()))
  184. require('settings', provided_by=[vagrant])
  185.  
  186. run(env.cmd_apache_restart)
  187.  
  188.  
  189. def apache_start():
  190. """
  191. Starts apache
  192. """
  193. print(_yellow('>>> starting %s()' % _fn()))
  194. require('settings', provided_by=[vagrant])
  195.  
  196. run(env.cmd_apache_start)
  197.  
  198.  
  199. def apache_stop():
  200. """
  201. Stops apache
  202. """
  203. print(_yellow('>>> starting %s()' % _fn()))
  204. require('settings', provided_by=[vagrant])
  205.  
  206. run(env.cmd_apache_start)
  207.  
  208.  
  209. def setup_virtualenv():
  210. """
  211. Setup a fresh virtualenv.
  212. """
  213. print(_yellow('>>> starting %s()' % _fn()))
  214. run('virtualenv -p %(python)s --no-site-packages %(env_path)s' % env)
  215. virtualenv('easy_install -U setuptools')
  216. virtualenv('easy_install pip')
  217.  
  218.  
  219. def virtualenv(command):
  220. """
  221. Run command in virtualenv.
  222. """
  223. with prefix('source %(env_path)s/bin/activate' % env):
  224. run(command)
  225.  
  226.  
  227. def install_requirements():
  228. """
  229. Install the required packages using pip.
  230. """
  231. print(_yellow('>>> starting %s()' % _fn()))
  232. virtualenv('pip install -q -E %(env_path)s -r %(repo_path)s/requirements.txt' % env)
  233.  
  234.  
  235. def install_apache_conf():
  236. """
  237. Backup old httpd.conf and install new one.
  238. """
  239. print(_yellow('>>> starting %s()' % _fn()))
  240. require('settings', provided_by=[vagrant])
  241.  
  242. if env.settings == 'vagrant':
  243. upload_template('apache/vagrant.conf.template', '/etc/apache2/sites-available/%(project_name)s' % env,
  244. context=env, use_jinja=True, use_sudo=True)
  245.  
  246. upload_template('apache/config.wsgi.template', '/etc/apache2/sites-available/%(project_name)s.wsgi' % env,
  247. context=env, use_jinja=True, use_sudo=True)
  248.  
  249. sudo('a2ensite %(project_name)s' % env)
  250.  
  251. else:
  252. # write your own code for other environments,
  253. # like shared hosting
  254. pass
  255.  
  256.  
  257. def remove_apache_conf():
  258. """
  259. Removes apache conf. Vagrant-only version for now
  260. """
  261. print(_yellow('>>> starting %s()' % _fn()))
  262. require('settings', provided_by=[vagrant])
  263. if env.settings != 'vagrant':
  264. return
  265.  
  266. sudo('a2dissite %(project_name)s' % env)
  267.  
  268.  
  269. def maintenance_on():
  270. """
  271. Turn maintenance mode on.
  272. """
  273. print(_yellow('>>> starting %s()' % _fn()))
  274. require('settings', provided_by=[vagrant])
  275. run('touch %(repo_path)s/.upgrading' % env)
  276.  
  277.  
  278. def maintenance_off():
  279. """
  280. Turn maintenance mode off.
  281. """
  282. print(_yellow('>>> starting %s()' % _fn()))
  283. require('settings', provided_by=[vagrant])
  284. run('rm -f %(repo_path)s/.upgrading' % env)
  285.  
  286.  
  287. def mkdirs():
  288. """
  289. Create all directories required for project
  290. """
  291. print(_yellow('>>> starting %s()' % _fn()))
  292. require('settings', provided_by=[vagrant])
  293.  
  294. run('mkdir -p %(path)s' % env)
  295.  
  296.  
  297. def rmdirs():
  298. """
  299. Removes all directories. Vagrant-only version for now
  300. """
  301. print(_yellow('>>> starting %s()' % _fn()))
  302. require('settings', provided_by=[vagrant])
  303. if env.settings != 'vagrant':
  304. return
  305.  
  306. run('rm -rf %(path)s' % env)
  307.  
  308. # -----------------------------------------------------------------------------
  309. # Functions not to be run directly
  310. # -----------------------------------------------------------------------------
  311. def _fn():
  312. """
  313. Returns current function name
  314. """
  315. return inspect.stack()[1][3]
  316.  
  317. def _get_vagrant_config():
  318. """
  319. Parses vagrant configuration and returns it as dict of ssh parameters
  320. and their values
  321. """
  322. result = local('vagrant ssh_config', capture=True)
  323. conf = {}
  324. for line in iter(result.splitlines()):
  325. parts = line.split()
  326. conf[parts[0]] = ' '.join(parts[1:])
  327.  
  328. return conf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement