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

Last change on this file since 13100 was 9875, checked in by uli, 12 years ago

Enable new modes and consider async ZODBs when removing old instance data.

File size: 5.9 KB
Line 
1import os
2import socket
3import subprocess
4import sys
5import time
6
7packages = dict()
8
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):
15    try:
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
21        name = pkg_name.split('.')[-1]
22        instances[name] = path_
23    except:
24        # not a package
25        pass
26
27print "WAEUP.STRESS.INIT: INSTANCES: ", instances
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    """
38    for name, path in instances.items():
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):
57    path = instances[name]
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                        'Data.async.fs', 'Data.async.fs.index',
64                        'Data.async.fs.tmp', 'Data.async.fs.lock']:
65            filepath = os.path.join(filestorage_dir,  filename)
66            print "  removing %s" % filepath
67            os.unlink(filepath)
68    return
69
70def 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`|`zeo`|`profile` and tells
78    how to start the instance.
79    """
80    path = instances[name]
81    print "WAEUP.STRESS: starting instance %s at %s using %s" % (
82        name, path, mode)
83    old_cwd_ = os.getcwd()
84    os.chdir(path)
85    # yes, shell _is_ neccessary here
86    if mode == 'kofactl':
87        subprocess.call('./bin/kofactl start', shell=True)
88    elif mode == 'profile':
89        subprocess.call(
90            './bin/paster serve --daemon parts/etc/profile.ini', shell=True)
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)
95    elif mode == 'profile':
96        subprocess.call(
97            './bin/paster serve --daemon parts/etc/profile.ini', shell=True)
98    else:
99        # by default we start paster as daemon
100        subprocess.call(
101            './bin/paster serve --daemon parts/etc/deploy.ini', shell=True)
102    os.chdir(old_cwd_)
103    return
104
105def wait_for_startup(host, port):
106    """Wait until a connection to host and port can be made.
107
108    After a timeout of 10 secs we abort raising an IOError.
109    """
110    print "WAEUP.STRESS.INIT: waiting for instance(s) to come up on %s:%s" % (
111        host, port)
112    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
113    start = time.time()
114    while True:
115        if time.time() - 10 > start:
116            raise IOError('timeout while waiting for instance to come up')
117        try:
118            s.connect((host, port))
119            break
120        except:
121            time.sleep(0.1)
122    s.close()
123    print "WAEUP.STRESS.INIT: instance(s) up after %.2f secs" % (
124        time.time() - start,)
125    return
126
127def 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
134    `mode` can be one of `paster`|`kofactl`|`zeo`|`profile` and tells
135    which tool to use to stop the instance.
136    """
137    path = instances[name]
138    print "WAEUP.STRESS: shutting down instance %s at %s using %s" % (
139        name, path, mode),
140    old_cwd_ = os.getcwd()
141    os.chdir(path)
142    if mode == 'kofactl':
143        subprocess.call('./bin/kofactl stop', shell=True)
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)
148    else:
149        # by default we use paster
150        subprocess.call(
151        './bin/paster serve --stop-daemon', shell=True)
152    print "... done."
153    os.chdir(old_cwd_)
154    return
155
156def install_app(name):
157    """Execute waeup.stress.script in instance context.
158    """
159    return exec_in_instance(name, 'install_app')
160    path = instances[name]
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_)
167
168def import_csv(name, *paths):
169    return exec_in_instance(name, 'import_csv', *paths)
170
171def 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_)
Note: See TracBrowser for help on using the repository browser.