a412d2ff2b2184ccfee486607a53a4ecd16d85d7
[archipelago] / xseg / tools / ext_scripts / vlmc_wrapper.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2012 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     """
49
50     name = os.getenv("VOL_NAME")
51     if name is None:
52         sys.stderr.write('The environment variable VOL_NAME is missing.\n')
53         return None
54     size = os.getenv("VOL_SIZE")
55     origin = os.getenv("EXTP_ORIGIN")
56     return (name, size, origin)
57
58
59 def create(env):
60     """Create a new vlmc Image
61     """
62     name, size, origin = env
63     vlmc.create(name=name, size=int(size), snap=origin)
64     return 0
65
66
67 def attach(env):
68     """Map an existing vlmc Image to a block device
69
70     This function maps an existing vlmc Image to a block device
71     e.g. /dev/xsegbd{X} and returns the device path. If the mapping
72     already exists, it returns the corresponding device path.
73     """
74
75     name, _, _ = env
76
77     # Check if the mapping already exists
78     d_id = vlmc.is_mapped(name)
79     if d_id:
80       # The mapping exists. Return it.
81         sys.stdout.write("%s" % str(DEVICE_PREFIX + str(d_id)))
82         return 0
83     # The mapping doesn't exist. Create it.
84     d_id = vlmc.map_volume(name=name)
85     # The device was successfully mapped. Return it.
86     #maybe assert (d_id == vlmc.is_mapped(name)
87     sys.stdout.write("%s" % str(DEVICE_PREFIX + str(d_id)))
88     return 0
89
90
91 def detach(env):
92     """Unmap a vlmc device from the Image it is mapped to
93
94     This function unmaps an vlmc device from the Image it is mapped to.
95     It is idempotent if the mapping doesn't exist at all.
96     """
97     name, _, _ = env
98
99     #try:
100     # Check if the mapping already exists
101     d_id = vlmc.is_mapped(name)
102     if d_id:
103         # The mapping exists. Unmap the vlmc device.
104         vlmc.unmap_volume(name=str(DEVICE_PREFIX + str(d_id)))
105     #assert(vlmc.is_mapped(name) == None)
106     return 0
107     #except Error as e:
108     #  sys.stderr.write(str(e)+'\n')
109     #  return -1
110
111
112 def grow(env):
113     """Grow an existing vlmc Image
114     """
115     name, size, _ = env
116
117     vlmc.resize(name=name, size=int(size))
118     return 0
119
120
121 def remove(env):
122     """ Delete a vlmc Image
123     """
124
125     name, _, _ = env
126
127     vlmc.remove(name=name)
128     return 0
129
130
131 def verify(env):
132     return 0
133
134
135 def main():
136     env = ReadEnv()
137     if env is None:
138         sys.stderr.write("Wrong environment. Aborting...\n")
139         return 1
140
141     loadrc(None)
142
143     try:
144         action = {
145             'attach': attach,
146             'create': create,
147             'detach': detach,
148             'grow': grow,
149             'remove': remove,
150             'verify': verify
151         }[os.path.basename(sys.argv[0])]
152     except:
153         sys.stderr.write("Op not supported\n")
154         return 1
155
156     try:
157         return action(env)
158     except Error as e:
159         sys.stderr.write(str(e) + '\n')
160         return 1
161
162 if __name__ == "__main__":
163     sys.exit(main())