[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', |
---|
[9875] | 62 | 'Data.fs.lock', |
---|
| 63 | 'Data.async.fs', 'Data.async.fs.index', |
---|
| 64 | 'Data.async.fs.tmp', 'Data.async.fs.lock']: |
---|
[8654] | 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 | |
---|
[8748] | 77 | `mode` can be one of `paster`|`kofactl`|`zeo`|`profile` and tells |
---|
| 78 | how to start the instance. |
---|
[8681] | 79 | """ |
---|
[8689] | 80 | path = instances[name] |
---|
[8681] | 81 | print "WAEUP.STRESS: starting instance %s at %s using %s" % ( |
---|
[8690] | 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) |
---|
[8748] | 88 | elif mode == 'profile': |
---|
| 89 | subprocess.call( |
---|
| 90 | './bin/paster serve --daemon parts/etc/profile.ini', shell=True) |
---|
[8683] | 91 | elif mode == 'zeo': |
---|
| 92 | subprocess.call('./bin/zeo_server start', shell=True) |
---|
| 93 | subprocess.call('./bin/zeo_client1 start', shell=True) |
---|
| 94 | subprocess.call('./bin/zeo_client2 start', shell=True) |
---|
[9875] | 95 | elif mode == 'profile': |
---|
| 96 | subprocess.call( |
---|
| 97 | './bin/paster serve --daemon parts/etc/profile.ini', shell=True) |
---|
[8681] | 98 | else: |
---|
| 99 | # by default we start paster as daemon |
---|
| 100 | subprocess.call( |
---|
[8687] | 101 | './bin/paster serve --daemon parts/etc/deploy.ini', shell=True) |
---|
[8654] | 102 | os.chdir(old_cwd_) |
---|
| 103 | return |
---|
| 104 | |
---|
[8655] | 105 | def wait_for_startup(host, port): |
---|
| 106 | """Wait until a connection to host and port can be made. |
---|
[8690] | 107 | |
---|
| 108 | After a timeout of 10 secs we abort raising an IOError. |
---|
[8655] | 109 | """ |
---|
[8690] | 110 | print "WAEUP.STRESS.INIT: waiting for instance(s) to come up on %s:%s" % ( |
---|
| 111 | host, port) |
---|
[8655] | 112 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
[8690] | 113 | start = time.time() |
---|
[8655] | 114 | while True: |
---|
[8690] | 115 | if time.time() - 10 > start: |
---|
| 116 | raise IOError('timeout while waiting for instance to come up') |
---|
[8655] | 117 | try: |
---|
| 118 | s.connect((host, port)) |
---|
| 119 | break |
---|
| 120 | except: |
---|
| 121 | time.sleep(0.1) |
---|
| 122 | s.close() |
---|
[8690] | 123 | print "WAEUP.STRESS.INIT: instance(s) up after %.2f secs" % ( |
---|
| 124 | time.time() - start,) |
---|
[8655] | 125 | return |
---|
| 126 | |
---|
[8681] | 127 | def stop_instance(name, mode='paster'): |
---|
| 128 | """Stop instance `name` in `mode`. |
---|
| 129 | |
---|
| 130 | The `name` should be someone like 'kofa', 'uniben', etc. where |
---|
| 131 | ``waeup.<NAME>`` is an existent instance in the `instances/` |
---|
| 132 | directory. |
---|
| 133 | |
---|
[9875] | 134 | `mode` can be one of `paster`|`kofactl`|`zeo`|`profile` and tells |
---|
| 135 | which tool to use to stop the instance. |
---|
[8681] | 136 | """ |
---|
[8689] | 137 | path = instances[name] |
---|
[8681] | 138 | print "WAEUP.STRESS: shutting down instance %s at %s using %s" % ( |
---|
| 139 | name, path, mode), |
---|
[8654] | 140 | old_cwd_ = os.getcwd() |
---|
| 141 | os.chdir(path) |
---|
[8681] | 142 | if mode == 'kofactl': |
---|
| 143 | subprocess.call('./bin/kofactl stop', shell=True) |
---|
[8683] | 144 | elif mode == 'zeo': |
---|
| 145 | subprocess.call('./bin/zeo_client2 stop', shell=True) |
---|
| 146 | subprocess.call('./bin/zeo_client1 stop', shell=True) |
---|
| 147 | subprocess.call('./bin/zeo_server stop', shell=True) |
---|
[8681] | 148 | else: |
---|
| 149 | # by default we use paster |
---|
| 150 | subprocess.call( |
---|
[8654] | 151 | './bin/paster serve --stop-daemon', shell=True) |
---|
| 152 | print "... done." |
---|
| 153 | os.chdir(old_cwd_) |
---|
| 154 | return |
---|
| 155 | |
---|
| 156 | def install_app(name): |
---|
[8689] | 157 | """Execute waeup.stress.script in instance context. |
---|
| 158 | """ |
---|
[8748] | 159 | return exec_in_instance(name, 'install_app') |
---|
[8689] | 160 | path = instances[name] |
---|
[8654] | 161 | old_cwd_ = os.getcwd() |
---|
| 162 | os.chdir(path) |
---|
| 163 | script = os.path.join(os.path.dirname(__file__), 'scripts.py') |
---|
| 164 | subprocess.call( |
---|
| 165 | './bin/python-console %s' % script, shell=True) |
---|
| 166 | os.chdir(old_cwd_) |
---|
[8748] | 167 | |
---|
| 168 | def import_csv(name, *paths): |
---|
| 169 | return exec_in_instance(name, 'import_csv', *paths) |
---|
| 170 | |
---|
| 171 | def exec_in_instance(name, cmd, *args): |
---|
| 172 | path = instances[name] |
---|
| 173 | old_cwd_ = os.getcwd() |
---|
| 174 | os.chdir(path) |
---|
| 175 | script = os.path.join(os.path.dirname(__file__), 'scripts.py') |
---|
| 176 | if args: |
---|
| 177 | args = ' '.join(args) |
---|
| 178 | else: |
---|
| 179 | args = '' |
---|
| 180 | subprocess.call( |
---|
| 181 | './bin/python-console %s %s %s' % ( |
---|
| 182 | script, cmd, args), shell=True) |
---|
| 183 | os.chdir(old_cwd_) |
---|