Install script for SimpleLog-2.0.2

-----BEGIN WEBFACTION INSTALL SCRIPT-----
#!/usr/local/bin/python2.5

"""
SimpleLog 2.0.2

Notes:
 - After SimpleLog has been installed you need specify the URL that it
   will be available at and create an administrative account. For more
   information see:

   http://wiki.simplelog.net/Installing/NextSteps

Requirements:
 - 1 Long Running Application
 - 1 MySQL Database

"extra info": "leave empty"

"""

import sys
import xmlrpclib

APP_CREATED = False
DB_CREATED = False

def create(app_name, autostart, db_name, password, server, session_id):
    app = server.create_app(session_id, app_name, 'rails_123', autostart, '')
    globals()['APP_CREATED'] = True

    server.create_db(session_id, db_name, 'mysql', password)
    globals()['DB_CREATED'] = True

    # Download and organize.
    cmd = (
        'rm -fr $(ls | grep -v autostart.cgi);'
        'wget -q http://wf.davidsissitka.com/api/scripts/simplelog/2.0.2/simplelog_2.\\'
        '0.2.tgz;'
        'tar fxz simplelog_2.0.2.tgz;'
        'mv --target-directory=. simplelog_2.0.2/* simplelog_2.0.2/.DS_Store;'
    )
    server.system(session_id, cmd)

    # Populate the database.
    cmd = (
        'sed -i "3s/simplelog/%(db_name)s/" config/database.yml;'
        'sed -i "4s/simplelog/%(db_name)s/" config/database.yml;'
        'sed -i "5s/\'\!simplePASS\'/%(password)s/" config/database.yml;'
        'rake simplelog:install;'
    )
    server.system(session_id, cmd % locals())

    # Clean Up
    cmd = (
        'rm simplelog_2.0.2.tgz;'
        'rmdir simplelog_2.0.2/;'
    )
    server.system(session_id, cmd)

    print app['id']

def delete(app_name, autostart, db_name, password, server, session_id):
    # Stop Mongrel
    cmd = (
        'cd webapps/%(app_name)s/;'
        'mongrel_rails stop;'
    )
    server.system(session_id, cmd % locals())

    server.delete_app(session_id, app_name)
    server.delete_db(session_id, db_name, 'mysql')

def main(action, username, password, machine, app_name, autostart, extra_info):
    db_name = (username + '_' + app_name)[:16]
    server = xmlrpclib.Server('https://api.webfaction.com/')
    session_id, account = server.login(username, password, machine)

    try:
        # Create/Delete Application
        globals()[action](app_name, autostart, db_name, password, server, session_id)

    except xmlrpclib.Fault, e:
        # Something has gone wrong so anything that was created needs to be
        # deleted.
        if APP_CREATED:
            server.delete_app(session_id, app_name)
        if DB_CREATED:
            server.delete_db(session_id, db_name, 'mysql')

        print e.faultString

if __name__ == '__main__':
    main(*sys.argv[1:])
-----END WEBFACTION INSTALL SCRIPT-----