Merge branch 'hotfix-0.3.5'
[archipelago] / xseg / tools / ext_scripts / vlmc_wrapper.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2012-2013 Greek Research and Technology Network
4 #
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.
9 #
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.
14 #
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
18 # 02110-1301, USA.
19
20 """ vlmc provider wrapper-script for ganeti extstorage disk template
21
22 The script takes it's input from environment variables. Specifically the
23 following variables should be present:
24
25  - VOL_NAME: The name of the new Image file
26  - VOL_SIZE: The size of the new Image (in megabytes)
27
28 The following variables are optional:
29
30  - EXTP_ORIGIN: The name of the Image file to snapshot
31
32 The code branches to the correct function, depending on the name (sys.argv[0])
33 of the executed script (attach, create, etc).
34
35 Returns O after successfull completion, 1 on failure
36
37 """
38
39 import os
40 import sys
41
42 from archipelago.common import Error, DEVICE_PREFIX, loadrc
43 from archipelago import vlmc as vlmc
44
45
46 def ReadEnv():
47     """Read the enviromental variables"""
48     name = os.getenv("VOL_NAME")
49     if name is None:
50         sys.stderr.write('The environment variable VOL_NAME is missing.\n')
51         return None
52
53     return {"name": name,
54             "size": os.getenv("VOL_SIZE"),
55             "origin": os.getenv("EXTP_ORIGIN"),
56             "snapshot_name": os.getenv("VOL_SNAPSHOT_NAME"),
57             }
58
59
60 def create(env):
61     """Create a new vlmc Image."""
62     name = env.get("name")
63     size = env.get("size")
64     origin = env.get("origin")
65
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)
69     return 0
70
71
72 def snapshot(env):
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)
79     return 0
80
81
82 def attach(env):
83     """Map an existing vlmc Image to a block device
84
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.
88
89     """
90
91     name = env.get("name")
92
93     # Check if the mapping already exists
94     d_id = vlmc.is_mapped(name)
95     if d_id:
96       # The mapping exists. Return it.
97         sys.stdout.write("%s" % str(DEVICE_PREFIX + str(d_id)))
98         return 0
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)))
104     return 0
105
106
107 def detach(env):
108     """Unmap a vlmc device from the Image it is mapped to
109
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.
112
113     """
114     name = env.get("name")
115
116     #try:
117     # Check if the mapping already exists
118     d_id = vlmc.is_mapped(name)
119     if d_id:
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)
123     return 0
124     #except Error as e:
125     #  sys.stderr.write(str(e)+'\n')
126     #  return -1
127
128
129 def grow(env):
130     """Grow an existing vlmc Image"""
131     name = env.get("name")
132     size = env.get("size")
133
134     sys.stderr.write("Resizing '%s'. New size '%s'\n" % (name, size))
135     vlmc.resize(name=name, size=int(size))
136     return 0
137
138
139 def remove(env):
140     """Delete a vlmc Image"""
141     name = env.get("name")
142
143     sys.stderr.write("Deleting '%s'\n" % name)
144     vlmc.remove(name=name)
145     return 0
146
147
148 def verify(env):
149     return 0
150
151
152 def setinfo(env):
153     return 0
154
155
156 def main():
157     env = ReadEnv()
158     if env is None:
159         sys.stderr.write("Wrong environment. Aborting...\n")
160         return 1
161
162     loadrc(None)
163
164     actions = {
165         'create': create,
166         'snapshot': snapshot,
167         'attach': attach,
168         'detach': detach,
169         'grow': grow,
170         'remove': remove,
171         'verify': verify,
172         'setinfo': setinfo,
173     }
174
175     try:
176         action_name = os.path.basename(sys.argv[0])
177         action = actions[action_name]
178         return action(env)
179     except KeyError:
180         sys.stderr.write("Action '%s' not supported\n" % action_name)
181         return 1
182     except Error as e:
183         sys.stderr.write("Archipelago error: %s\n" % e)
184         return 1
185
186 if __name__ == "__main__":
187     sys.exit(main())