| 1 | #!/usr/bin/python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # Copyright © 2009 by Karl Ramm |
|---|
| 4 | # |
|---|
| 5 | # All rights reserved. |
|---|
| 6 | # |
|---|
| 7 | # Permission to use, copy, modify, and distribute this software and |
|---|
| 8 | # its documentation for any purpose and without fee is hereby granted, |
|---|
| 9 | # provided that the above copyright notice appear in all copies and |
|---|
| 10 | # that both that copyright notice and this permission notice appear in |
|---|
| 11 | # supporting documentation, and that the name of Karl Ramm not be used |
|---|
| 12 | # in advertising or publicity pertaining to distribution of the |
|---|
| 13 | # software without specific, written prior permission. |
|---|
| 14 | # |
|---|
| 15 | # KARL RAMM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
|---|
| 16 | # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN |
|---|
| 17 | # NO EVENT SHALL KARL RAMM BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
|---|
| 18 | # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
|---|
| 19 | # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
|---|
| 20 | # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
|---|
| 21 | # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|---|
| 22 | |
|---|
| 23 | import vm |
|---|
| 24 | from time import sleep, time |
|---|
| 25 | |
|---|
| 26 | class host(object): |
|---|
| 27 | def __init__(self, machine): |
|---|
| 28 | self.machine = machine |
|---|
| 29 | def setup(self): |
|---|
| 30 | pass |
|---|
| 31 | def teardown(self): |
|---|
| 32 | pass |
|---|
| 33 | |
|---|
| 34 | class cluster(object): |
|---|
| 35 | def __init__(self, count=None, spec=None, machine=vm.sshtestmachine): |
|---|
| 36 | if spec is None: |
|---|
| 37 | spec = count*[host] |
|---|
| 38 | self.machines = {} |
|---|
| 39 | self._all = [] |
|---|
| 40 | for proto in spec: |
|---|
| 41 | m = machine() |
|---|
| 42 | p = proto(m) |
|---|
| 43 | t = (m, p) |
|---|
| 44 | self._all.append(t) |
|---|
| 45 | self.machines.setdefault(proto, []).append(t) |
|---|
| 46 | def setup(self): |
|---|
| 47 | while any(not m.ready() for (m, p) in self._all): |
|---|
| 48 | sleep(7) |
|---|
| 49 | for (m, p) in self._all: |
|---|
| 50 | p.setup() |
|---|
| 51 | def teardown(self): |
|---|
| 52 | for (m, p) in self._all: |
|---|
| 53 | p.teardown() |
|---|
| 54 | m.shutdown() |
|---|
| 55 | start = time() |
|---|
| 56 | while True: |
|---|
| 57 | still = [m for (m, p) in self._all if m.alive()] |
|---|
| 58 | if not still: |
|---|
| 59 | break |
|---|
| 60 | sleep(7) |
|---|
| 61 | if time() > (start + 300): |
|---|
| 62 | for m in still: |
|---|
| 63 | m.shoot() |
|---|