root / shared-filer / create @ bd924ffd
History | View | Annotate | Download (3 kB)
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 |
"""Create a new file under a shared directory |
21 |
|
22 |
This program creates a new file inside a shared directory. It will be |
23 |
used to host a disk Image. It takes it's input from environment variables. |
24 |
Specifically the following variables should be present: |
25 |
|
26 |
- VOL_NAME: The name of the new Image file |
27 |
- VOL_SIZE: The size of the new Image (in mebibytes) |
28 |
|
29 |
Returns O after successfull creation, 1 on failure |
30 |
|
31 |
""" |
32 |
|
33 |
import os |
34 |
import sys |
35 |
import errno |
36 |
|
37 |
sys.path.append("/usr/share/ganeti") |
38 |
|
39 |
from ganeti import utils |
40 |
|
41 |
# Default shared directory |
42 |
SHARED_DIR = "/srv/ganeti/shared-file-storage" |
43 |
|
44 |
|
45 |
def ReadEnv(): |
46 |
"""Read the mandatory enviromental variables |
47 |
|
48 |
""" |
49 |
name = os.getenv("VOL_NAME") |
50 |
if name is None: |
51 |
sys.stderr.write('The environment variable VOL_NAME is missing.\n') |
52 |
return None |
53 |
size = os.getenv("VOL_SIZE") |
54 |
if size is None: |
55 |
sys.stderr.write('The environment variable VOL_SIZE is missing.\n') |
56 |
return None |
57 |
shared_dir_param = os.getenv("EXTP_SHARED_DIR") |
58 |
if shared_dir_param is not None: |
59 |
sys.stderr.write('Found shared dir param, value: %s\n' % shared_dir_param) |
60 |
|
61 |
return (name, size, shared_dir_param) |
62 |
|
63 |
|
64 |
def main(): |
65 |
sys.stderr.write('Creation started: reading environment...\n') |
66 |
|
67 |
# Read environment |
68 |
env = ReadEnv() |
69 |
if env is None: |
70 |
sys.stderr.write('Wrong environment. Aborting\n') |
71 |
return 1 |
72 |
|
73 |
file_name, size, shared_dir_param = env |
74 |
|
75 |
if shared_dir_param is None: |
76 |
shared_dir_param = SHARED_DIR |
77 |
|
78 |
file_path = utils.PathJoin(shared_dir_param, file_name) |
79 |
|
80 |
sys.stderr.write('Read environment successfully: file name: %s, ' |
81 |
'size: %sMB\n' % (file_path, size)) |
82 |
|
83 |
# Check if directory exists |
84 |
if not os.path.isdir(shared_dir_param): |
85 |
sys.stderr.write('%s is not a valid directory. Aborting' % shared_dir_param) |
86 |
return 1 |
87 |
|
88 |
# Create file |
89 |
try: |
90 |
fd = os.open(file_path, os.O_RDWR | os.O_CREAT | os.O_EXCL) |
91 |
f = os.fdopen(fd, "w") |
92 |
f.truncate(int(size) * 1024 * 1024) |
93 |
f.close() |
94 |
except EnvironmentError, err: |
95 |
if err.errno == errno.EEXIST: |
96 |
sys.stderr.write('File already existing: %s\n' % file_path) |
97 |
return 1 |
98 |
sys.stderr.write('Error in file creation: %\n' % str(err)) |
99 |
return 1 |
100 |
|
101 |
sys.stderr.write('Image file created successfully\n') |
102 |
return 0 |
103 |
|
104 |
|
105 |
if __name__ == "__main__": |
106 |
sys.exit(main()) |