Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Last active December 26, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miraculixx/7063521 to your computer and use it in GitHub Desktop.
Save miraculixx/7063521 to your computer and use it in GitHub Desktop.
This is manage.py for an appfog hosted django application. It works by connecting to the specified database instance. Note that all django commands that do not target the database will run locally.
mysql:
rmanage: ${name} ${username} ${password} ${port}

Using rmanage you can e.g. apply the following operations directly to your appfog database:

  • syncdb
  • south migrations
  • dump and load fixtures

Usage:

af tunnel myservice rmanage

where myservice is the service name on appfog (e.g. a mysql service)

This will launch a prompt e.g.

manage.py@af> 

Enter help to display the commands Enter exit to leave the prompt

Examples:

# start the console
af tunnel myservice rmanage
# show all migrations 
manage.py@af> migrate --list
# migrate a particular applications
manage.py@af> migrate myapp
# load a fixture
manage.py@af> loaddata myfixture.json
# run the django shell (using local code, remote database)
manage.py@af> shell

or run commands directly e.g. following af update
# one command at a time
echo migrate --list | af tunnel myservice rmanage
echo loaddata fixture.json | af tunnel myservice rmanage
# multiple commands from a file, one command per line
cat commands.rmanage | af tunnel myservice rmanage

Installation

create a file .af_clients in your home directory with the following content:

mysql:
  rmanage: ${name} ${username} ${password} ${port} 

Create the file settings_rmanage.py in your project's directory:

from settings import *

#
# settings are passed from af tunnel, order of parameters
# see .af_cliens in user home
#
DATABASES['default'] = {
                'ENGINE': 'django.db.backends.mysql',
                'NAME': sys.argv[1],
                'USER': sys.argv[2],
                'PASSWORD': sys.argv[3],
                'HOST': '127.0.0.1', # default = localhost 
                'PORT': sys.argv[4] # default
}
#!/usr/bin/env python
"""
appfog remote django console
Copyright (c) 2013 Patrick Senti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This is manage.py for an appfog hosted django application.
It works by connecting to the specified database instance.
Note that all django commands that do not target the database
will run locally.
"""
from django.core.management import execute_manager
import os
import settings_rmanage as settings
import sys
if __name__ == "__main__":
argv = []
print "*** appfog remote django console ***"
print "*** running with settings *** "
print settings.DATABASES['default']
def do_prompt():
try:
return raw_input("manage.py@af> ")
except EOFError:
return "exit"
prompt = do_prompt()
while prompt != "exit":
argv = []
argv.append("")
for command in prompt.split():
argv.append(command)
argv.append('--settings=settings_rmanage')
try:
execute_manager(settings, argv)
except:
print "*** ERROR. Try again."
prompt = do_prompt()
exit()
from settings import *
#
# settings are passed from af tunnel, order of parameters
# see .af_cliens in user home
#
DATABASES['default'] = {
'ENGINE': 'django.db.backends.mysql',
'NAME': sys.argv[1],
'USER': sys.argv[2],
'PASSWORD': sys.argv[3],
'HOST': '127.0.0.1', # default = localhost
'PORT': sys.argv[4] # default
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment