3 # Copyright (C) 2012-2013 Greek Research and Technology Network
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 """ vlmc provider wrapper-script for ganeti extstorage disk template
22 The script takes it's input from environment variables. Specifically the
23 following variables should be present:
25 - VOL_NAME: The name of the new Image file
26 - VOL_SIZE: The size of the new Image (in megabytes)
28 The following variables are optional:
30 - EXTP_ORIGIN: The name of the Image file to snapshot
32 The code branches to the correct function, depending on the name (sys.argv[0])
33 of the executed script (attach, create, etc).
35 Returns O after successfull completion, 1 on failure
42 from archipelago.common import Error, DEVICE_PREFIX, loadrc
43 from archipelago import vlmc as vlmc
47 """Read the enviromental variables"""
48 name = os.getenv("VOL_NAME")
50 sys.stderr.write('The environment variable VOL_NAME is missing.\n')
54 "size": os.getenv("VOL_SIZE"),
55 "origin": os.getenv("EXTP_ORIGIN"),
56 "snapshot_name": os.getenv("VOL_SNAPSHOT_NAME"),
61 """Create a new vlmc Image."""
62 name = env.get("name")
63 size = env.get("size")
64 origin = env.get("origin")
66 sys.stderr.write("Creating volume '%s' of size '%s' from '%s'\n"
67 % (name, size, origin))
68 vlmc.create(name=name, size=int(size), snap=origin)
73 """Create a snapshot of an existing vlmc Image."""
74 name = env.get("name")
75 snapshot_name = env.get("snapshot_name")
76 sys.stderr.write("Creating snapshot '%s' from '%s'\n" %
77 (snapshot_name, name))
78 vlmc.snapshot(name=name, snap_name=snapshot_name)
83 """Map an existing vlmc Image to a block device
85 This function maps an existing vlmc Image to a block device
86 e.g. /dev/xsegbd{X} and returns the device path. If the mapping
87 already exists, it returns the corresponding device path.
91 name = env.get("name")
93 # Check if the mapping already exists
94 d_id = vlmc.is_mapped(name)
96 # The mapping exists. Return it.
97 sys.stdout.write("%s" % str(DEVICE_PREFIX + str(d_id)))
99 # The mapping doesn't exist. Create it.
100 d_id = vlmc.map_volume(name=name)
101 # The device was successfully mapped. Return it.
102 #maybe assert (d_id == vlmc.is_mapped(name)
103 sys.stdout.write("%s" % str(DEVICE_PREFIX + str(d_id)))
108 """Unmap a vlmc device from the Image it is mapped to
110 This function unmaps an vlmc device from the Image it is mapped to.
111 It is idempotent if the mapping doesn't exist at all.
114 name = env.get("name")
117 # Check if the mapping already exists
118 d_id = vlmc.is_mapped(name)
120 # The mapping exists. Unmap the vlmc device.
121 vlmc.unmap_volume(name=str(DEVICE_PREFIX + str(d_id)))
122 #assert(vlmc.is_mapped(name) == None)
125 # sys.stderr.write(str(e)+'\n')
130 """Grow an existing vlmc Image"""
131 name = env.get("name")
132 size = env.get("size")
134 sys.stderr.write("Resizing '%s'. New size '%s'\n" % (name, size))
135 vlmc.resize(name=name, size=int(size))
140 """Delete a vlmc Image"""
141 name = env.get("name")
143 sys.stderr.write("Deleting '%s'\n" % name)
144 vlmc.remove(name=name)
159 sys.stderr.write("Wrong environment. Aborting...\n")
166 'snapshot': snapshot,
176 action_name = os.path.basename(sys.argv[0])
177 action = actions[action_name]
180 sys.stderr.write("Action '%s' not supported\n" % action_name)
183 sys.stderr.write("Archipelago error: %s\n" % e)
186 if __name__ == "__main__":