source: main/waeup.stress/trunk/src/waeup/stress/__init__.py @ 8749

Last change on this file since 8749 was 8748, checked in by uli, 12 years ago
  • Add support for running in profiling mode.
  • Make commands to be executed in instance context more modular.
File size: 5.6 KB
RevLine 
[8654]1import os
[8655]2import socket
[8654]3import subprocess
4import sys
[8655]5import time
[8654]6
7packages = dict()
8
[8689]9root = os.path.dirname(os.path.dirname(os.path.dirname(
10    os.path.dirname(__file__))))
11instances_root = os.path.join(root, 'instances')
12instances = dict()
13print "ROOT: ", root
14for 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]27print "WAEUP.STRESS.INIT: INSTANCES: ", instances
[8654]28
29def 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
35def 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
42def 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
56def 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]68def 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
[8748]75    `mode` can be one of `paster`|`kofactl`|`zeo`|`profile` and tells
76    how to start the instance.
[8681]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)
[8748]86    elif mode == 'profile':
87        subprocess.call(
88            './bin/paster serve --daemon parts/etc/profile.ini', shell=True)
[8683]89    elif mode == 'zeo':
90        os.listdir('./bin')
91        subprocess.call('./bin/zeo_server start', shell=True)
92        subprocess.call('./bin/zeo_client1 start', shell=True)
93        subprocess.call('./bin/zeo_client2 start', shell=True)
[8681]94    else:
95        # by default we start paster as daemon
96        subprocess.call(
[8687]97            './bin/paster serve --daemon parts/etc/deploy.ini', shell=True)
[8654]98    os.chdir(old_cwd_)
99    return
100
[8655]101def wait_for_startup(host, port):
102    """Wait until a connection to host and port can be made.
[8690]103
104    After a timeout of 10 secs we abort raising an IOError.
[8655]105    """
[8690]106    print "WAEUP.STRESS.INIT: waiting for instance(s) to come up on %s:%s" % (
107        host, port)
[8655]108    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
[8690]109    start = time.time()
[8655]110    while True:
[8690]111        if time.time() - 10 > start:
112            raise IOError('timeout while waiting for instance to come up')
[8655]113        try:
114            s.connect((host, port))
115            break
116        except:
117            time.sleep(0.1)
118    s.close()
[8690]119    print "WAEUP.STRESS.INIT: instance(s) up after %.2f secs" % (
120        time.time() - start,)
[8655]121    return
122
[8681]123def stop_instance(name, mode='paster'):
124    """Stop instance `name` in `mode`.
125
126    The `name` should be someone like 'kofa', 'uniben', etc. where
127    ``waeup.<NAME>`` is an existent instance in the `instances/`
128    directory.
129
130    `mode` can be one of `paster`|`kofactl` and tells which tool to
131    use to stop the instance.
132    """
[8689]133    path = instances[name]
[8681]134    print "WAEUP.STRESS: shutting down instance %s at %s using %s" % (
135        name, path, mode),
[8654]136    old_cwd_ = os.getcwd()
137    os.chdir(path)
[8681]138    if mode == 'kofactl':
139        subprocess.call('./bin/kofactl stop', shell=True)
[8683]140    elif mode == 'zeo':
141        subprocess.call('./bin/zeo_client2 stop', shell=True)
142        subprocess.call('./bin/zeo_client1 stop', shell=True)
143        subprocess.call('./bin/zeo_server stop', shell=True)
[8681]144    else:
145        # by default we use paster
146        subprocess.call(
[8654]147        './bin/paster serve --stop-daemon', shell=True)
148    print "... done."
149    os.chdir(old_cwd_)
150    return
151
152def install_app(name):
[8689]153    """Execute waeup.stress.script in instance context.
154    """
[8748]155    return exec_in_instance(name, 'install_app')
[8689]156    path = instances[name]
[8654]157    old_cwd_ = os.getcwd()
158    os.chdir(path)
159    script = os.path.join(os.path.dirname(__file__), 'scripts.py')
160    subprocess.call(
161        './bin/python-console %s' % script, shell=True)
162    os.chdir(old_cwd_)
[8748]163
164def import_csv(name, *paths):
165    return exec_in_instance(name, 'import_csv', *paths)
166
167def exec_in_instance(name, cmd, *args):
168    path = instances[name]
169    old_cwd_ = os.getcwd()
170    os.chdir(path)
171    script = os.path.join(os.path.dirname(__file__), 'scripts.py')
172    if args:
173        args = ' '.join(args)
174    else:
175        args = ''
176    subprocess.call(
177        './bin/python-console %s %s %s' % (
178            script, cmd, args), shell=True)
179    os.chdir(old_cwd_)
Note: See TracBrowser for help on using the repository browser.