3 # Copyright 2012 GRNET S.A. All rights reserved.
5 # Redistribution and use in source and binary forms, with or
6 # without modification, are permitted provided that the following
9 # 1. Redistributions of source code must retain the above
10 # copyright notice, this list of conditions and the following
13 # 2. Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials
16 # provided with the distribution.
18 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
31 # The views and conclusions contained in the software and
32 # documentation are those of the authors and should not be
33 # interpreted as representing official policies, either expressed
34 # or implied, of GRNET S.A.
42 from subprocess import check_call
45 from .vlmc import showmapped as vlmc_showmapped
46 from .vlmc import get_mapped as vlmc_get_mapped
49 def start_peer(peer, cli=False):
51 raise Error("Cannot start peer %s. Peer already running" % peer.role)
53 s = "Starting %s " % peer.role
54 sys.stdout.write(s.ljust(FIRST_COLUMN_WIDTH))
59 sys.stdout.write(red("FAILED".ljust(SECOND_COLUMN_WIDTH)))
60 sys.stdout.write("\n")
62 except Exception as e:
65 sys.stdout.write(red("FAILED".ljust(SECOND_COLUMN_WIDTH)))
66 sys.stdout.write("\n")
67 raise Error("Cannot start %s" % peer.role)
71 while not peer.is_running():
76 sys.stdout.write(red("FAILED".ljust(SECOND_COLUMN_WIDTH)))
77 sys.stdout.write("\n")
78 raise Error("Couldn't start %s" % peer.role)
81 sys.stdout.write(green("OK".ljust(SECOND_COLUMN_WIDTH)))
82 sys.stdout.write("\n")
85 def stop_peer(peer, cli=False):
90 pretty_print(peer.role, yellow("Not running"))
93 s = "Stopping %s " % peer.role
94 sys.stdout.write(s.ljust(FIRST_COLUMN_WIDTH))
102 sys.stdout.write(red("FAILED".ljust(SECOND_COLUMN_WIDTH)))
103 sys.stdout.write("\n")
104 raise Error("Failed to stop peer %s." % peer.role)
106 sys.stdout.write(green("OK".ljust(SECOND_COLUMN_WIDTH)))
107 sys.stdout.write("\n")
110 def peer_running(peer, cli):
112 if peer.is_running():
114 pretty_print(peer.role, green('running'))
118 pretty_print(peer.role, red('not running'))
122 pretty_print(peer.role, yellow("Has valid pidfile but does not "
123 "seem to be active"))
127 def start_peers(peers, cli=False):
129 if not loaded_module(m):
130 raise Error("Cannot start userspace peers. " + m +
131 " module not loaded")
132 for r, _ in config['roles']:
137 def stop_peers(peers, cli=False):
138 for r, _ in reversed(config['roles']):
143 def start(user=False, role=None, cli=False, **kwargs):
148 raise Error("Invalid peer %s" % role)
149 return start_peer(p, cli)
152 return start_peers(peers, cli)
155 raise Error("Cannot start. Try stopping first")
158 print "===================="
159 print "Starting archipelago"
160 print "===================="
163 get_segment().create()
165 start_peers(peers, cli)
166 load_module("blktap", None)
167 except Exception as e:
170 stop(user, role, cli)
173 def stop(user=False, role=None, cli=False, **kwargs):
178 raise Error("Invalid peer %s" % role)
179 return stop_peer(p, cli)
181 return stop_peers(peers, cli)
184 print "===================="
185 print "Stoping archipelago"
186 print "===================="
189 if vlmc_showmapped() > 0:
190 raise Error("Cannot stop archipelago. Mapped volumes exist")
192 mapped = vlmc_get_mapped()
193 if mapped and len(mapped) > 0:
194 raise Error("Cannot stop archipelago. Mapped volumes exist")
195 stop_peers(peers, cli)
198 def status(cli=False, **kwargs):
201 if vlmc_showmapped() > 0:
204 mapped = vlmc_get_mapped()
205 if mapped and len(mapped) > 0:
207 if loaded_module("blktap"):
209 pretty_print("blktap", green('Loaded'))
213 pretty_print("blktap", red('Not loaded'))
214 for role, _ in reversed(config['roles']):
216 if peer_running(p, cli):
221 def restart(**kwargs):