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

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

Consider instance mode when starting/stopping instances.

File size: 4.7 KB
Line 
1import os
2import socket
3import subprocess
4import sys
5import time
6import waeup.kofa
7
8AVAILABLE_KOFA_PROJECTS = ['waeup.kofa',]
9packages = dict()
10kofa_projects = dict()
11
12for 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
23print "PACKAGES: ", packages
24print "KOFA PROJECTS: ", kofa_projects
25
26def do_foo():
27    mydir = os.path.dirname(os.path.dirname(__file__))
28    print "MYDIR", mydir
29    #import pdb; pdb.set_trace()
30
31def 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
37def 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
44def 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
58def 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
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` and tells how to start the
78    instance.
79    """
80    path = kofa_projects[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    else:
89        # by default we start paster as daemon
90        subprocess.call(
91            './bin/paster serve --daemon parts/etc/debug.ini', shell=True)
92    os.chdir(old_cwd_)
93    return
94
95def wait_for_startup(host, port):
96    """Wait until a connection to host and port can be made.
97    """
98    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
99    while True:
100        try:
101            s.connect((host, port))
102            break
103        except:
104            time.sleep(0.1)
105    s.close()
106    return
107
108def stop_instance(name, mode='paster'):
109    """Stop instance `name` in `mode`.
110
111    The `name` should be someone like 'kofa', 'uniben', etc. where
112    ``waeup.<NAME>`` is an existent instance in the `instances/`
113    directory.
114
115    `mode` can be one of `paster`|`kofactl` and tells which tool to
116    use to stop the instance.
117    """
118    path = kofa_projects[name]
119    print "WAEUP.STRESS: shutting down instance %s at %s using %s" % (
120        name, path, mode),
121    old_cwd_ = os.getcwd()
122    os.chdir(path)
123    if mode == 'kofactl':
124        subprocess.call('./bin/kofactl stop', shell=True)
125    else:
126        # by default we use paster
127        subprocess.call(
128        './bin/paster serve --stop-daemon', shell=True)
129    print "... done."
130    os.chdir(old_cwd_)
131    return
132
133def install_app(name):
134    path = kofa_projects[name]
135    old_cwd_ = os.getcwd()
136    os.chdir(path)
137    db_file = os.path.join(path, 'var', 'filestorage', 'Data.fs')
138    config_file = os.path.join(path, 'parts', 'etc', 'site.zcml')
139    #import waeup.kofa.meta
140    #import grok.meta
141    import grok
142    #print "META: ", grok.meta
143    import sys
144    #print "PATHS: ", sys.path
145    from zope.app.appsetup import database, config
146    #config(config_file)
147    #subprocess.call(
148    #    './bin/python-console -c "import grok.meta"', shell=True)
149    script = os.path.join(os.path.dirname(__file__), 'scripts.py')
150    subprocess.call(
151        './bin/python-console %s' % script, shell=True)
152    os.chdir(old_cwd_)
153
154def configure():
155    import ConfigParser
156    from multimechanize.utilities.run import project_name, cmd_opts, args
157    print "Configure"
158    print "PROJECT NAME: ", project_name
Note: See TracBrowser for help on using the repository browser.