[8654] | 1 | import os |
---|
[8655] | 2 | import socket |
---|
[8654] | 3 | import subprocess |
---|
| 4 | import sys |
---|
[8655] | 5 | import time |
---|
[8654] | 6 | import waeup.kofa |
---|
| 7 | |
---|
| 8 | AVAILABLE_KOFA_PROJECTS = ['waeup.kofa',] |
---|
| 9 | packages = dict() |
---|
| 10 | kofa_projects = dict() |
---|
| 11 | |
---|
| 12 | for pkg_name in AVAILABLE_KOFA_PROJECTS: |
---|
| 13 | try: |
---|
| 14 | name = pkg_name.split('.')[-1] |
---|
| 15 | mod = sys.modules[pkg_name].__file__ |
---|
| 16 | packages[name] = os.path.dirname(mod) |
---|
| 17 | kofa_projects[name] = os.path.dirname(os.path.dirname(os.path.dirname( |
---|
| 18 | packages[name]))) |
---|
| 19 | except: |
---|
| 20 | # not a package |
---|
| 21 | pass |
---|
| 22 | |
---|
| 23 | print "PACKAGES: ", packages |
---|
| 24 | print "KOFA PROJECTS: ", kofa_projects |
---|
| 25 | |
---|
| 26 | def do_foo(): |
---|
| 27 | mydir = os.path.dirname(os.path.dirname(__file__)) |
---|
| 28 | print "MYDIR", mydir |
---|
| 29 | #import pdb; pdb.set_trace() |
---|
| 30 | |
---|
| 31 | def enable_datafs(path, proj_name): |
---|
| 32 | """Copy data.fs in `path` for kofa project `proj_name`. |
---|
| 33 | """ |
---|
| 34 | pkg = packages[proj_name] |
---|
| 35 | #src_path = os.path.join( |
---|
| 36 | |
---|
| 37 | def bootstrap(): |
---|
| 38 | """Run bootstrap and buildout for each of the KOFA instances |
---|
| 39 | """ |
---|
| 40 | for name, path in kofa_projects.items(): |
---|
| 41 | bootstrap_instance(name, path) |
---|
| 42 | return |
---|
| 43 | |
---|
| 44 | def bootstrap_instance(name, path): |
---|
| 45 | """Run bootstrap.py and buildout for instance `name` at `path`. |
---|
| 46 | """ |
---|
| 47 | print "WAEUP.STRESS: bootstrapping %s" % name |
---|
| 48 | print "WAEUP.STRESS: path: %s" % path |
---|
| 49 | old_cwd_ = os.getcwd() |
---|
| 50 | os.chdir(path) |
---|
| 51 | print "WAEUP.STRESS: bootstrapping %s" % name |
---|
| 52 | subprocess.call(['python', 'bootstrap/bootstrap.py']) |
---|
| 53 | print "WAEUP.STRESS: running buildout for %s" % name |
---|
| 54 | subprocess.call(['./bin/buildout'], shell=True) |
---|
| 55 | os.chdir(old_cwd_) |
---|
| 56 | return |
---|
| 57 | |
---|
| 58 | def remove_zodb(name): |
---|
| 59 | path = kofa_projects[name] |
---|
| 60 | print "WAEUP.STRESS: removing ZODB of %s at %s" % (name, path) |
---|
| 61 | filestorage_dir = os.path.join(path, 'var', 'filestorage') |
---|
| 62 | for filename in os.listdir(filestorage_dir): |
---|
| 63 | if filename in ['Data.fs', 'Data.fs.tmp', 'Data.fs.index', |
---|
| 64 | 'Data.fs.lock']: |
---|
| 65 | filepath = os.path.join(filestorage_dir, filename) |
---|
| 66 | print " removing %s" % filepath |
---|
| 67 | os.unlink(filepath) |
---|
| 68 | return |
---|
| 69 | |
---|
[8681] | 70 | def start_instance(name, mode='paster'): |
---|
| 71 | """Start instance `name` in `mode`. |
---|
| 72 | |
---|
| 73 | The `name` should be someone like 'kofa', 'uniben', etc. where |
---|
| 74 | ``waeup.<NAME>`` is an existent instance in the `instances/` |
---|
| 75 | directory. |
---|
| 76 | |
---|
| 77 | `mode` can be one of `paster`|`kofactl` and tells how to start the |
---|
| 78 | instance. |
---|
| 79 | """ |
---|
[8654] | 80 | path = kofa_projects[name] |
---|
[8681] | 81 | print "WAEUP.STRESS: starting instance %s at %s using %s" % ( |
---|
| 82 | name, path, mode), |
---|
[8654] | 83 | old_cwd_ = os.getcwd() |
---|
| 84 | os.chdir(path) |
---|
| 85 | # yes, shell _is_ neccessary here |
---|
[8681] | 86 | if mode == 'kofactl': |
---|
| 87 | subprocess.call('./bin/kofactl start', shell=True) |
---|
[8683] | 88 | elif mode == 'zeo': |
---|
| 89 | os.listdir('./bin') |
---|
| 90 | subprocess.call('./bin/zeo_server start', shell=True) |
---|
| 91 | subprocess.call('./bin/zeo_client1 start', shell=True) |
---|
| 92 | subprocess.call('./bin/zeo_client2 start', shell=True) |
---|
[8681] | 93 | else: |
---|
| 94 | # by default we start paster as daemon |
---|
| 95 | subprocess.call( |
---|
| 96 | './bin/paster serve --daemon parts/etc/debug.ini', shell=True) |
---|
[8654] | 97 | os.chdir(old_cwd_) |
---|
| 98 | return |
---|
| 99 | |
---|
[8655] | 100 | def wait_for_startup(host, port): |
---|
| 101 | """Wait until a connection to host and port can be made. |
---|
| 102 | """ |
---|
| 103 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
| 104 | while True: |
---|
| 105 | try: |
---|
| 106 | s.connect((host, port)) |
---|
| 107 | break |
---|
| 108 | except: |
---|
| 109 | time.sleep(0.1) |
---|
| 110 | s.close() |
---|
| 111 | return |
---|
| 112 | |
---|
[8681] | 113 | def stop_instance(name, mode='paster'): |
---|
| 114 | """Stop instance `name` in `mode`. |
---|
| 115 | |
---|
| 116 | The `name` should be someone like 'kofa', 'uniben', etc. where |
---|
| 117 | ``waeup.<NAME>`` is an existent instance in the `instances/` |
---|
| 118 | directory. |
---|
| 119 | |
---|
| 120 | `mode` can be one of `paster`|`kofactl` and tells which tool to |
---|
| 121 | use to stop the instance. |
---|
| 122 | """ |
---|
[8654] | 123 | path = kofa_projects[name] |
---|
[8681] | 124 | print "WAEUP.STRESS: shutting down instance %s at %s using %s" % ( |
---|
| 125 | name, path, mode), |
---|
[8654] | 126 | old_cwd_ = os.getcwd() |
---|
| 127 | os.chdir(path) |
---|
[8681] | 128 | if mode == 'kofactl': |
---|
| 129 | subprocess.call('./bin/kofactl stop', shell=True) |
---|
[8683] | 130 | elif mode == 'zeo': |
---|
| 131 | subprocess.call('./bin/zeo_client2 stop', shell=True) |
---|
| 132 | subprocess.call('./bin/zeo_client1 stop', shell=True) |
---|
| 133 | subprocess.call('./bin/zeo_server stop', shell=True) |
---|
[8681] | 134 | else: |
---|
| 135 | # by default we use paster |
---|
| 136 | subprocess.call( |
---|
[8654] | 137 | './bin/paster serve --stop-daemon', shell=True) |
---|
| 138 | print "... done." |
---|
| 139 | os.chdir(old_cwd_) |
---|
| 140 | return |
---|
| 141 | |
---|
| 142 | def install_app(name): |
---|
| 143 | path = kofa_projects[name] |
---|
| 144 | old_cwd_ = os.getcwd() |
---|
| 145 | os.chdir(path) |
---|
| 146 | db_file = os.path.join(path, 'var', 'filestorage', 'Data.fs') |
---|
| 147 | config_file = os.path.join(path, 'parts', 'etc', 'site.zcml') |
---|
| 148 | #import waeup.kofa.meta |
---|
| 149 | #import grok.meta |
---|
| 150 | import grok |
---|
| 151 | #print "META: ", grok.meta |
---|
| 152 | import sys |
---|
| 153 | #print "PATHS: ", sys.path |
---|
| 154 | from zope.app.appsetup import database, config |
---|
| 155 | #config(config_file) |
---|
| 156 | #subprocess.call( |
---|
| 157 | # './bin/python-console -c "import grok.meta"', shell=True) |
---|
| 158 | script = os.path.join(os.path.dirname(__file__), 'scripts.py') |
---|
| 159 | subprocess.call( |
---|
| 160 | './bin/python-console %s' % script, shell=True) |
---|
| 161 | os.chdir(old_cwd_) |
---|
[8672] | 162 | |
---|
| 163 | def configure(): |
---|
| 164 | import ConfigParser |
---|
| 165 | from multimechanize.utilities.run import project_name, cmd_opts, args |
---|
| 166 | print "Configure" |
---|
| 167 | print "PROJECT NAME: ", project_name |
---|