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, run, sudo, cd, hosts, get
- from fabric.context_managers import prefix
- import datetime
- env.use_ssh_config = True
- # Hosts
- # env.hosts list references aliases in ~/.ssh/config or IP address. When using .ssh/config,
- # fab will use the ssh keyfile referenced by the host alias, otherwise need to do what is
- # being done in dev to assign env a key_filename
- def dev():
- env.hosts = ['xxx.xxx.xx.xx']
- env.user = 'vagrant'
- result = local('vagrant ssh-config 271h39z | grep IdentityFile', capture=True)
- env.key_filename=result.split()[1]
- def beta():
- env.hosts = ['beta']
- def production():
- env.hosts = ['prod']
- # Deployment Functions
- def develop():
- code_dir = '/home/vagrant/theme'
- with cd(code_dir), prefix('source /usr/lib/ckan/default/bin/activate'):
- run('python setup.py develop')
- run('sudo service nginx restart')
- def deploy(migrate=None):
- code_dir = '/home/ubuntu/theme'
- with cd(code_dir), prefix('source /usr/lib/ckan/default/bin/activate'):
- run("git pull")
- if migrate is not None:
- migrate_command = "python ckanext/theme/migration/manage.py upgrade {}".format(migrate)
- run(migrate_command)
- run('sudo python setup.py develop')
- run('sudo service nginx restart')
- # Data dump functions
- def get_timestamp(ts=None):
- if ts is None:
- return datetime.datetime.now().__str__().split(' ')[0]
- else:
- return ts
- def dump_database(passwd, ts=None, dirStr=None):
- timestamp = get_timestamp(ts)
- if dirStr is None:
- database_dump_filename = "ckan-{}.dump".format(timestamp)
- database_tar_filename = "ckan-dump-{}.tgz".format(timestamp)
- else:
- database_dump_filename = "{}/ckan.dump".format(dirStr, timestamp)
- database_tar_filename = "{}/ckan-dump.tgz".format(dirStr, timestamp)
- dump_command = "PGPASSWORD='{}' pg_dump ckan_default -U ckan_default -h localhost > {}".format(passwd, database_dump_filename)
- tar_command = "tar -cvpzf {} {}".format(database_tar_filename,database_dump_filename)
- run(dump_command)
- run(tar_command)
- def dump_datastore(passwd, ts=None, dirStr=None):
- timestamp = get_timestamp(ts)
- if dirStr is None:
- datastore_dump_filename = "datastore-{}.dump".format(timestamp)
- datastore_tar_filename = "datastore-dump-{}.tgz".format(timestamp)
- else:
- datastore_dump_filename = "{}/datastore-ctdata.dump".format(dirStr, timestamp)
- datastore_tar_filename = "{}/datastore-dump.tgz".format(dirStr, timestamp)
- datastore_dump_command = "PGPASSWORD='{}' pg_dump datastore_default -U ckan_default -h localhost > {}".format(passwd, datastore_dump_filename)
- tar_command = "tar cvpzf {} {}".format(datastore_tar_filename, datastore_dump_filename)
- run(datastore_dump_command)
- run(tar_command)
- def gather_needed_files(ts=None, dirStr=None):
- timestamp = get_timestamp(ts)
- if dirStr is None:
- tar_file = "needed-files.tgz".format(timestamp)
- else:
- tar_file = "{}/needed-files.tgz".format(dirStr, timestamp)
- tar_command = "tar cvpzf {} /var/lib/ckan/default".format(tar_file)
- run(tar_command)
- def dump_ckan(passwd):
- timestamp = datetime.datetime.now().__str__().split(' ')[0]
- directory_name = 'dump-{}'.format(timestamp)
- create_directory_python_command = "import os; os.mkdir('{}')".format(directory_name)
- command = '''python -c "{}"'''.format(create_directory_python_command)
- run(command)
- dump_database(passwd, timestamp, directory_name)
- dump_datastore(passwd, timestamp, directory_name)
- gather_needed_files(timestamp, directory_name)
- def download_dumps(localpath, date=None):
- if date is None:
- date = get_timestamp(date)
- remotepath = 'dump-{}/*.tgz'.format(date)
- get(remotepath, localpath)
- def backup(passwd, localpath):
- dump_ckan(passwd)
- download_dumps(localpath)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement