Guest User

Untitled

a guest
Jan 9th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.52 KB | None | 0 0
  1. from fabric.api import *
  2.  
  3. """
  4. Base configuration
  5. """
  6. env.project_name = '$(project)'
  7. env.database_password = '$(db_password)'
  8. env.site_media_prefix = "site_media"
  9. env.admin_media_prefix = "admin_media"
  10. env.newsapps_media_prefix = "na_media"
  11. env.path = '/home/newsapps/sites/%(project_name)s' % env
  12. env.log_path = '/home/newsapps/logs/%(project_name)s' % env
  13. env.env_path = '%(path)s/env' % env
  14. env.repo_path = '%(path)s/repository' % env
  15. env.apache_config_path = '/home/newsapps/sites/apache/%(project_name)s' % env
  16. env.python = 'python2.6'
  17.  
  18. """
  19. Environments
  20. """
  21. def production():
  22. """
  23. Work on production environment
  24. """
  25. env.settings = 'production'
  26. env.hosts = ['$(production_domain)']
  27. env.user = '$(production_user)'
  28. env.s3_bucket = '$(production_s3)'
  29.  
  30. def staging():
  31. """
  32. Work on staging environment
  33. """
  34. env.settings = 'staging'
  35. env.hosts = ['$(staging_domain)']
  36. env.user = '$(staging_user)'
  37. env.s3_bucket = '$(staging_s3)'
  38.  
  39. """
  40. Branches
  41. """
  42. def stable():
  43. """
  44. Work on stable branch.
  45. """
  46. env.branch = 'stable'
  47.  
  48. def master():
  49. """
  50. Work on development branch.
  51. """
  52. env.branch = 'master'
  53.  
  54. def branch(branch_name):
  55. """
  56. Work on any specified branch.
  57. """
  58. env.branch = branch_name
  59.  
  60. """
  61. Commands - setup
  62. """
  63. def setup():
  64. """
  65. Setup a fresh virtualenv, install everything we need, and fire up the database.
  66.  
  67. Does NOT perform the functions of deploy().
  68. """
  69. require('settings', provided_by=[production, staging])
  70. require('branch', provided_by=[stable, master, branch])
  71.  
  72. setup_directories()
  73. setup_virtualenv()
  74. clone_repo()
  75. checkout_latest()
  76. destroy_database()
  77. create_database()
  78. load_data()
  79. install_requirements()
  80. install_apache_conf()
  81. deploy_requirements_to_s3()
  82.  
  83. def setup_directories():
  84. """
  85. Create directories necessary for deployment.
  86. """
  87. run('mkdir -p %(path)s' % env)
  88. run('mkdir -p %(env_path)s' % env)
  89. run ('mkdir -p %(log_path)s;' % env)
  90. sudo('chgrp -R www-data %(log_path)s; chmod -R g+w %(log_path)s;' % env)
  91. run('ln -s %(log_path)s %(path)s/logs' % env)
  92.  
  93. def setup_virtualenv():
  94. """
  95. Setup a fresh virtualenv.
  96. """
  97. run('virtualenv -p %(python)s --no-site-packages %(env_path)s;' % env)
  98. run('source %(env_path)s/bin/activate; easy_install -U setuptools; easy_install pip;' % env)
  99.  
  100. def clone_repo():
  101. """
  102. Do initial clone of the git repository.
  103. """
  104. run('git clone git@tribune.unfuddle.com:tribune/%(project_name)s.git %(repo_path)s' % env)
  105.  
  106. def checkout_latest():
  107. """
  108. Pull the latest code on the specified branch.
  109. """
  110. run('cd %(repo_path)s; git checkout %(branch)s; git pull origin %(branch)s' % env)
  111.  
  112. def install_requirements():
  113. """
  114. Install the required packages using pip.
  115. """
  116. run('source %(env_path)s/bin/activate; pip install -E %(env_path)s -r %(repo_path)s/requirements.txt' % env)
  117.  
  118. def install_apache_conf():
  119. """
  120. Install the apache site config file.
  121. """
  122. sudo('cp %(repo_path)s/%(project_name)s/configs/%(settings)s/%(project_name)s %(apache_config_path)s' % env)
  123.  
  124. def deploy_requirements_to_s3():
  125. """
  126. Deploy the latest newsapps and admin media to s3.
  127. """
  128. run('s3cmd del --recursive s3://%(s3_bucket)s/%(project_name)s/%(admin_media_prefix)s/' % env)
  129. run('s3cmd -P --guess-mime-type sync %(env_path)s/src/django/django/contrib/admin/media/ s3://%(s3_bucket)s/%(project_name)s/%(site_media_prefix)s/' % env)
  130. run('s3cmd del --recursive s3://%(s3_bucket)s/%(project_name)s/%(newsapps_media_prefix)s/' % env)
  131. run('s3cmd -P --guess-mime-type sync %(env_path)s/src/newsapps/newsapps/na_media/ s3://%(s3_bucket)s/%(project_name)s/%(newsapps_media_prefix)s/' % env)
  132.  
  133. """
  134. Commands - deployment
  135. """
  136. def deploy():
  137. """
  138. Deploy the latest version of the site to the server and restart Apache2.
  139.  
  140. Does not perform the functions of load_new_data().
  141. """
  142. require('settings', provided_by=[production, staging])
  143. require('branch', provided_by=[stable, master, branch])
  144.  
  145. with settings(warn_only=True):
  146. maintenance_up()
  147.  
  148. checkout_latest()
  149. gzip_assets()
  150. deploy_to_s3()
  151. refresh_widgets()
  152. maintenance_down()
  153.  
  154. def maintenance_up():
  155. """
  156. Install the Apache maintenance configuration.
  157. """
  158. sudo('cp %(repo_path)s/%(project_name)s/configs/%(settings)s/%(project_name)s_maintenance %(apache_config_path)s' % env)
  159. reboot()
  160.  
  161. def gzip_assets():
  162. """
  163. GZips every file in the assets directory and places the new file
  164. in the gzip directory with the same filename.
  165. """
  166. run('cd %(repo_path)s; python gzip_assets.py' % env)
  167.  
  168. def deploy_to_s3():
  169. """
  170. Deploy the latest project site media to S3.
  171. """
  172. env.gzip_path = '%(path)s/repository/%(project_name)s/gzip/assets/' % env
  173. run(('s3cmd -P --add-header=Content-encoding:gzip --guess-mime-type --rexclude-from=%(path)s/repository/s3exclude sync %(gzip_path)s s3://%(s3_bucket)s/%(project_name)s/%(site_media_prefix)s/') % env)
  174.  
  175. def refresh_widgets():
  176. """
  177. Redeploy the widgets to S3.
  178. """
  179. run('source %(env_path)s/bin/activate; cd %(repo_path)s; ./manage refreshwidgets' % env)
  180.  
  181. def reboot():
  182. """
  183. Restart the Apache2 server.
  184. """
  185. sudo('/mnt/apps/bin/restart-all-apache.sh')
  186.  
  187. def maintenance_down():
  188. """
  189. Reinstall the normal site configuration.
  190. """
  191. install_apache_conf()
  192. reboot()
  193.  
  194. """
  195. Commands - rollback
  196. """
  197. def rollback(commit_id):
  198. """
  199. Rolls back to specified git commit hash or tag.
  200.  
  201. There is NO guarantee we have committed a valid dataset for an arbitrary
  202. commit hash.
  203. """
  204. require('settings', provided_by=[production, staging])
  205. require('branch', provided_by=[stable, master, branch])
  206.  
  207. maintenance_up()
  208. checkout_latest()
  209. git_reset(commit_id)
  210. gzip_assets()
  211. deploy_to_s3()
  212. refresh_widgets()
  213. maintenance_down()
  214.  
  215. def git_reset(commit_id):
  216. """
  217. Reset the git repository to an arbitrary commit hash or tag.
  218. """
  219. env.commit_id = commit_id
  220. run("cd %(repo_path)s; git reset --hard %(commit_id)s" % env)
  221.  
  222. """
  223. Commands - data
  224. """
  225. def load_new_data():
  226. """
  227. Erase the current database and load new data from the SQL dump file.
  228. """
  229. require('settings', provided_by=[production, staging])
  230.  
  231. maintenance_up()
  232. pgpool_down()
  233. destroy_database()
  234. create_database()
  235. load_data()
  236. pgpool_up()
  237. maintenance_down()
  238.  
  239. def create_database():
  240. """
  241. Creates the user and database for this project.
  242. """
  243. run('echo "CREATE USER %(project_name)s WITH PASSWORD \'%(database_password)s\';" | psql postgres' % env)
  244. run('createdb -O %(project_name)s %(project_name)s -T template_postgis' % env)
  245.  
  246. def destroy_database():
  247. """
  248. Destroys the user and database for this project.
  249.  
  250. Will not cause the fab to fail if they do not exist.
  251. """
  252. with settings(warn_only=True):
  253. run('dropdb %(project_name)s' % env)
  254. run('dropuser %(project_name)s' % env)
  255.  
  256. def load_data():
  257. """
  258. Loads data from the repository into PostgreSQL.
  259. """
  260. run('psql -q %(project_name)s < %(path)s/repository/data/psql/dump.sql' % env)
  261. run('psql -q %(project_name)s < %(path)s/repository/data/psql/finish_init.sql' % env)
  262.  
  263. def pgpool_down():
  264. """
  265. Stop pgpool so that it won't prevent the database from being rebuilt.
  266. """
  267. sudo('/etc/init.d/pgpool stop')
  268.  
  269. def pgpool_up():
  270. """
  271. Start pgpool.
  272. """
  273. sudo('/etc/init.d/pgpool start')
  274.  
  275. """
  276. Commands - miscellaneous
  277. """
  278.  
  279. def clear_cache():
  280. """
  281. Restart memcache, wiping the current cache.
  282. """
  283. sudo('/mnt/apps/bin/restart-memcache.sh')
  284.  
  285. def echo_host():
  286. """
  287. Echo the current host to the command line.
  288. """
  289. run('echo %(settings)s; echo %(hosts)s' % env)
  290.  
  291. """
  292. Deaths, destroyers of worlds
  293. """
  294. def shiva_the_destroyer():
  295. """
  296. Remove all directories, databases, etc. associated with the application.
  297. """
  298. with settings(warn_only=True):
  299. run('rm -Rf %(path)s' % env)
  300. run('rm -Rf %(log_path)s' % env)
  301. run('dropdb %(project_name)s' % env)
  302. run('dropuser %(project_name)s' % env)
  303. sudo('rm %(apache_config_path)s' % env)
  304. reboot()
  305. run('s3cmd del --recursive s3://%(s3_bucket)s/%(project_name)s' % env)
  306.  
  307. """
  308. Utility functions (not to be called directly)
  309. """
  310. def _execute_psql(query):
  311. """
  312. Executes a PostgreSQL command using the command line interface.
  313. """
  314. env.query = query
  315. run(('cd %(path)s/repository; psql -q %(project_name)s -c "%(query)s"') % env)
Add Comment
Please, Sign In to add comment