Advertisement
Guest User

Untitled

a guest
Nov 13th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. from __future__ import with_statement
  2. from fabric.api import env, local, run, sudo, cd, hosts, get
  3. from fabric.context_managers import prefix
  4. import datetime
  5.  
  6. env.use_ssh_config = True
  7.  
  8.  
  9. # Hosts
  10. # env.hosts list references aliases in ~/.ssh/config or IP address. When using .ssh/config,
  11. # fab will use the ssh keyfile referenced by the host alias, otherwise need to do what is
  12. # being done in dev to assign env a key_filename
  13. def dev():
  14. env.hosts = ['xxx.xxx.xx.xx']
  15. env.user = 'vagrant'
  16. result = local('vagrant ssh-config 271h39z | grep IdentityFile', capture=True)
  17. env.key_filename=result.split()[1]
  18.  
  19. def beta():
  20. env.hosts = ['beta']
  21.  
  22. def production():
  23. env.hosts = ['prod']
  24.  
  25.  
  26. # Deployment Functions
  27. def develop():
  28. code_dir = '/home/vagrant/theme'
  29. with cd(code_dir), prefix('source /usr/lib/ckan/default/bin/activate'):
  30. run('python setup.py develop')
  31. run('sudo service nginx restart')
  32.  
  33. def deploy(migrate=None):
  34. code_dir = '/home/ubuntu/theme'
  35. with cd(code_dir), prefix('source /usr/lib/ckan/default/bin/activate'):
  36. run("git pull")
  37. if migrate is not None:
  38. migrate_command = "python ckanext/theme/migration/manage.py upgrade {}".format(migrate)
  39. run(migrate_command)
  40. run('sudo python setup.py develop')
  41. run('sudo service nginx restart')
  42.  
  43.  
  44. # Data dump functions
  45. def get_timestamp(ts=None):
  46. if ts is None:
  47. return datetime.datetime.now().__str__().split(' ')[0]
  48. else:
  49. return ts
  50.  
  51. def dump_database(passwd, ts=None, dirStr=None):
  52. timestamp = get_timestamp(ts)
  53. if dirStr is None:
  54. database_dump_filename = "ckan-{}.dump".format(timestamp)
  55. database_tar_filename = "ckan-dump-{}.tgz".format(timestamp)
  56. else:
  57. database_dump_filename = "{}/ckan.dump".format(dirStr, timestamp)
  58. database_tar_filename = "{}/ckan-dump.tgz".format(dirStr, timestamp)
  59. dump_command = "PGPASSWORD='{}' pg_dump ckan_default -U ckan_default -h localhost > {}".format(passwd, database_dump_filename)
  60. tar_command = "tar -cvpzf {} {}".format(database_tar_filename,database_dump_filename)
  61. run(dump_command)
  62. run(tar_command)
  63.  
  64. def dump_datastore(passwd, ts=None, dirStr=None):
  65. timestamp = get_timestamp(ts)
  66. if dirStr is None:
  67. datastore_dump_filename = "datastore-{}.dump".format(timestamp)
  68. datastore_tar_filename = "datastore-dump-{}.tgz".format(timestamp)
  69. else:
  70. datastore_dump_filename = "{}/datastore-ctdata.dump".format(dirStr, timestamp)
  71. datastore_tar_filename = "{}/datastore-dump.tgz".format(dirStr, timestamp)
  72. datastore_dump_command = "PGPASSWORD='{}' pg_dump datastore_default -U ckan_default -h localhost > {}".format(passwd, datastore_dump_filename)
  73. tar_command = "tar cvpzf {} {}".format(datastore_tar_filename, datastore_dump_filename)
  74. run(datastore_dump_command)
  75. run(tar_command)
  76.  
  77. def gather_needed_files(ts=None, dirStr=None):
  78. timestamp = get_timestamp(ts)
  79. if dirStr is None:
  80. tar_file = "needed-files.tgz".format(timestamp)
  81. else:
  82. tar_file = "{}/needed-files.tgz".format(dirStr, timestamp)
  83. tar_command = "tar cvpzf {} /var/lib/ckan/default".format(tar_file)
  84. run(tar_command)
  85.  
  86. def dump_ckan(passwd):
  87. timestamp = datetime.datetime.now().__str__().split(' ')[0]
  88. directory_name = 'dump-{}'.format(timestamp)
  89. create_directory_python_command = "import os; os.mkdir('{}')".format(directory_name)
  90. command = '''python -c "{}"'''.format(create_directory_python_command)
  91. run(command)
  92. dump_database(passwd, timestamp, directory_name)
  93. dump_datastore(passwd, timestamp, directory_name)
  94. gather_needed_files(timestamp, directory_name)
  95.  
  96. def download_dumps(localpath, date=None):
  97. if date is None:
  98. date = get_timestamp(date)
  99. remotepath = 'dump-{}/*.tgz'.format(date)
  100. get(remotepath, localpath)
  101.  
  102. def backup(passwd, localpath):
  103. dump_ckan(passwd)
  104. download_dumps(localpath)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement