def dotenv_get(filepath, key=None): out = sudo(dotenv.get_cli_string(filepath, 'get', key), quiet=True) print out if "UserWarning" in out: return False else: return out[out.index('=') + 1:].strip("\"'")
def config(c, list=True, set=False, get=False, unset=False, key=None, val=None): """Manage project configuration via .env e.g: fab config --set -k <key> -v <value> fab config --get -k <key> fab config --unset --keyval <key> fab config [--list] """ if set: action = "set" if not key and not val: return logger.error("Key and value expected") elif get: action = "get" if not key: return logger.error("Key expected") elif unset: action = "unset" if not key: return logger.error("Key expected") elif list: action = "list" with Connection(prod_server, user=prod_user) as r: with r.prefix( "source {0}/bin/activate".format(remote_prod_virtualenv)): with r.cd(remote_prod_dir): command = dotenv.get_cli_string('.env', action, key, val) r.run(command)
def aws_config(action=None, key=None, value=None): aws_add_env() if value is not None: value = shlex.quote(value) cmd = dotenv.get_cli_string('/root/app.env', action, key, value) cmd = cmd.split() cmd = cmd[0] + ' -q never ' + ' '.join(cmd[1:]) res = run_cfg(cmd, remote=True, capture=(action == 'get')) return res
def config(action=None, key=None, value=None): '''Read/write to .env file on local and remote machines. Usages: fab [prod] config:set,<key>,<value> fab [prod] config:get,<key> fab [prod] config:unset,<key> fab [prod] config:list ''' import dotenv command = dotenv.get_cli_string(env.dotenv_path, action, key, value) env.config_setter('touch %(dotenv_path)s' % env) if env.config_setter == local: with virtualenv(): env.config_setter(command) else: env.config_setter(command) restart_servers()
def config(action=None, key=None, value=None): """Read/write to .env file on local and remote machines. Usages: fab [prod] config:set,<key>,<value> fab [prod] config:get,<key> fab [prod] config:unset,<key> fab [prod] config:list """ import dotenv command = dotenv.get_cli_string(env.dotenv_path, action, key, value) env.config_setter('touch %(dotenv_path)s' % env) if env.config_setter == local: with virtualenv(): env.config_setter(command) else: env.config_setter(command) restart_servers()
def dotenv_set(filepath, key, value): sudo(dotenv.get_cli_string(filepath, 'set', key, value), quiet=True)
def getdata(c): """ Get remote data from the production sever """ with Connection(prod_server, user=prod_user) as r: with r.cd(remote_prod_dir): with Connection(prod_server, user=prod_user) as r: with r.prefix("source {0}/bin/activate".format( remote_prod_virtualenv)): with r.cd(remote_prod_dir): command = dotenv.get_cli_string( '.env', 'get', 'STATIC_ROOT', ) ret = r.run(command) remote_static_root = ret.stdout.split('=')[1].strip() command = dotenv.get_cli_string( '.env', 'get', 'MEDIA_ROOT', ) ret = r.run(command) remote_media_root = ret.stdout.split('=')[1].strip() command = dotenv.get_cli_string( '.env', 'get', 'DATABASE', ) ret = r.run(command) remote_database = ret.stdout.split('=')[1].strip() command = dotenv.get_cli_string( '.env', 'get', 'USERNAME', ) ret = r.run(command) remote_database_username = ret.stdout.split( '=')[1].strip() logger.info("Backing up the database") r.run("pg_dump -U {} {} > {}/data/dump.sql".format( remote_database, remote_database_username, remote_prod_dir)) logger.info("Getting remote data dump file") run("rsync -vzh --info=progress2 {}@{}:{}/data/dump.sql data/dump.sql" .format( prod_user, prod_server, remote_prod_dir, )) #r.get(remote_prod_dir + '/data/dump.sql', local=os.getcwd() + "/data/dump.sql") logger.info("Recreating local database") run("dropdb {}".format(database_local)) run("createdb {}".format(database_local)) run("psql -U {} {} < data/dump.sql".format(database_local_user, database_local), warn=True) logger.info("Syncing static and media files") run("rsync -avzh --info=progress2 --delete {}@{}:{}/ {}/".format( prod_user, prod_server, remote_static_root, static_root)) run("rsync -avzh --info=progress2 --delete --exclude='applications/*' {}@{}:{}/ {}/" .format(prod_user, prod_server, remote_media_root, media_root))