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