Skip to content

Instantly share code, notes, and snippets.

@map0logo
Created August 25, 2014 03:50
Show Gist options
  • Save map0logo/62165e7ed01861683312 to your computer and use it in GitHub Desktop.
Save map0logo/62165e7ed01861683312 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python3.3
"""
This script creates and installs a Mezzanine CMS database and application.
"""
import sys
import xmlrpclib
cmsName = "my_cms"
databaseName = "cms_db"
databaseType = "postgresql"
databasePassword = "cms_pwd"
def executeCommands(server, sessionId, cmdList):
cmd = "(" + "&& ".join(cmdList) + " ) >> ~/mezzanineAppInstall.txt 2>&1"
print cmd
server.system(sessionId, cmd)
def substituteTuple(param, orig, new):
search = '"%s": "%s"' % (param, orig)
replace = '"%s": "%s"' % (param, new)
return (search, replace)
def createApp(server, sessionId, appName, autostart, extraInfo):
# Create the database
server.create_db(sessionId, databaseName, databaseType, databasePassword)
# Install pip and log the progress
commands = []
commands.append("echo")
commands.append("date")
commands.append("mkdir -p $HOME/lib/python3.3")
commands.append("easy_install-3.3 pip")
executeCommands(server, sessionId, commands)
# Create the application
appInfo = server.create_app(sessionId, appName, "django165_mw34_33", False, "", "")
# (some?) subsequent commands run in the newly created app directory
appDir = "%s/%s/webapps/%s" % (account["home"], account["username"], appName)
cmsDir = "%s/%s" % (appDir, cmsName)
commands = []
commands.append("export PYTHONPATH=%s/lib/python3.3" % appDir)
commands.append("cd %s" % appDir)
commands.append('$HOME/bin/pip-3.3 -v install -U --install-option="--install-scripts=%s/bin" --install-option="--install-lib=%s/lib/python3.3" mezzanine' % (appDir, appDir))
commands.append("./bin/mezzanine-project %s" % cmsName)
executeCommands(server, sessionId, commands)
# Fix up the local_settings.py file
substitutions = []
substitutions.append( substituteTuple("ENGINE", "django.db.backends.sqlite3", "django.db.backends.postgresql_psycopg2") )
substitutions.append( substituteTuple("NAME", "dev.db", databaseName) )
substitutions.append( substituteTuple("USER", "", databaseName) )
substitutions.append( substituteTuple("PASSWORD", "", databasePassword) )
substitutions.append( ("DEBUG = True", "DEBUG = False"))
settingsFileName = "%s/local_settings.py" % cmsDir
server.replace_in_file(sessionId, settingsFileName, *substitutions)
# Now use mezzanine tools to populate the database, and gather
# all the static content into one place
commands = []
commands.append("export PYTHONPATH=$HOME/lib/python3.3")
commands.append("cd %s" % cmsDir)
commands.append("python3.3 manage.py createdb --noinput")
commands.append("python3.3 manage.py collectstatic --noinput")
commands.append("cp ../myproject/myproject/wsgi.py deploy/")
executeCommands(server, sessionId, commands)
# Fix up the wsgi.py file
fname = "%s/deploy/wsgi.py" % cmsDir
substitutions = []
substitutions.append( ("myproject.settings", "%s.settings" % cmsName) )
server.replace_in_file(sessionId, fname, *substitutions)
# Fix up the apache conf file
fname = "%s/apache2/conf/httpd.conf" % appDir
substitutions = []
substitutions.append( ("myproject/myproject/wsgi.py", "%s/deploy/wsgi.py" % cmsName) )
server.replace_in_file(sessionId, fname, *substitutions)
# Restart apache
commands = []
commands.append("%s/apache2/bin/restart" % appDir )
commands.append("echo app id %d" % appInfo['id'] )
executeCommands(server, sessionId, commands)
# Create an application to serve the static files
fname = "%s/static" % cmsDir
appInfo = server.create_app(sessionId, appName+"_static", "symlink_static_only", False, fname, "")
print appInfo['id']
def deleteApp(server, sessionId, appName, autostart, extraInfo):
# delete database and applications
server.delete_app(sessionId, appName)
server.delete_app(sessionId, appName+"_static")
server.delete_db(sessionId, databaseName, databaseType)
server.delete_db_user(sessionId, databaseName, databaseType)
if __name__ == '__main__':
mode, accountName, accountPassword, machine, appName, autostart, extraInfo = sys.argv[1:]
# connect & log in
server = xmlrpclib.ServerProxy("https://api.webfaction.com/", verbose=False)
sessionId, account = server.login(accountName, accountPassword, machine)
if mode == "create":
createApp(server, sessionId, appName, autostart, extraInfo)
elif mode == "delete":
deleteApp(server, sessionId, appName, autostart, extraInfo)
else:
print "Invalid argument. Must be 'create' or 'delete'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment