Statistics
| Branch: | Revision:

root / shared-filer / create @ bd924ffd

History | View | Annotate | Download (3 kB)

1 bd924ffd Dimitris Aragiorgis
#!/usr/bin/env python
2 bd924ffd Dimitris Aragiorgis
#
3 bd924ffd Dimitris Aragiorgis
# Copyright (C) 2012 Greek Research and Technology Network
4 bd924ffd Dimitris Aragiorgis
#
5 bd924ffd Dimitris Aragiorgis
# This program is free software; you can redistribute it and/or modify
6 bd924ffd Dimitris Aragiorgis
# it under the terms of the GNU General Public License as published by
7 bd924ffd Dimitris Aragiorgis
# the Free Software Foundation; either version 2 of the License, or
8 bd924ffd Dimitris Aragiorgis
# (at your option) any later version.
9 bd924ffd Dimitris Aragiorgis
#
10 bd924ffd Dimitris Aragiorgis
# This program is distributed in the hope that it will be useful, but
11 bd924ffd Dimitris Aragiorgis
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 bd924ffd Dimitris Aragiorgis
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 bd924ffd Dimitris Aragiorgis
# General Public License for more details.
14 bd924ffd Dimitris Aragiorgis
#
15 bd924ffd Dimitris Aragiorgis
# You should have received a copy of the GNU General Public License
16 bd924ffd Dimitris Aragiorgis
# along with this program; if not, write to the Free Software
17 bd924ffd Dimitris Aragiorgis
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 bd924ffd Dimitris Aragiorgis
# 02110-1301, USA.
19 bd924ffd Dimitris Aragiorgis
20 bd924ffd Dimitris Aragiorgis
"""Create a new file under a shared directory
21 bd924ffd Dimitris Aragiorgis
22 bd924ffd Dimitris Aragiorgis
This program creates a new file inside a shared directory. It will be
23 bd924ffd Dimitris Aragiorgis
used to host a disk Image. It takes it's input from environment variables.
24 bd924ffd Dimitris Aragiorgis
Specifically the following variables should be present:
25 bd924ffd Dimitris Aragiorgis
26 bd924ffd Dimitris Aragiorgis
 - VOL_NAME: The name of the new Image file
27 bd924ffd Dimitris Aragiorgis
 - VOL_SIZE: The size of the new Image (in mebibytes)
28 bd924ffd Dimitris Aragiorgis
29 bd924ffd Dimitris Aragiorgis
Returns O after successfull creation, 1 on failure
30 bd924ffd Dimitris Aragiorgis
31 bd924ffd Dimitris Aragiorgis
"""
32 bd924ffd Dimitris Aragiorgis
33 bd924ffd Dimitris Aragiorgis
import os
34 bd924ffd Dimitris Aragiorgis
import sys
35 bd924ffd Dimitris Aragiorgis
import errno
36 bd924ffd Dimitris Aragiorgis
37 bd924ffd Dimitris Aragiorgis
sys.path.append("/usr/share/ganeti")
38 bd924ffd Dimitris Aragiorgis
39 bd924ffd Dimitris Aragiorgis
from ganeti import utils
40 bd924ffd Dimitris Aragiorgis
41 bd924ffd Dimitris Aragiorgis
# Default shared directory
42 bd924ffd Dimitris Aragiorgis
SHARED_DIR = "/srv/ganeti/shared-file-storage"
43 bd924ffd Dimitris Aragiorgis
44 bd924ffd Dimitris Aragiorgis
45 bd924ffd Dimitris Aragiorgis
def ReadEnv():
46 bd924ffd Dimitris Aragiorgis
  """Read the mandatory enviromental variables
47 bd924ffd Dimitris Aragiorgis
48 bd924ffd Dimitris Aragiorgis
  """
49 bd924ffd Dimitris Aragiorgis
  name = os.getenv("VOL_NAME")
50 bd924ffd Dimitris Aragiorgis
  if name is None:
51 bd924ffd Dimitris Aragiorgis
    sys.stderr.write('The environment variable VOL_NAME is missing.\n')
52 bd924ffd Dimitris Aragiorgis
    return None
53 bd924ffd Dimitris Aragiorgis
  size = os.getenv("VOL_SIZE")
54 bd924ffd Dimitris Aragiorgis
  if size is None:
55 bd924ffd Dimitris Aragiorgis
    sys.stderr.write('The environment variable VOL_SIZE is missing.\n')
56 bd924ffd Dimitris Aragiorgis
    return None
57 bd924ffd Dimitris Aragiorgis
  shared_dir_param = os.getenv("EXTP_SHARED_DIR")
58 bd924ffd Dimitris Aragiorgis
  if shared_dir_param is not None:
59 bd924ffd Dimitris Aragiorgis
    sys.stderr.write('Found shared dir param, value: %s\n' % shared_dir_param)
60 bd924ffd Dimitris Aragiorgis
61 bd924ffd Dimitris Aragiorgis
  return (name, size, shared_dir_param)
62 bd924ffd Dimitris Aragiorgis
63 bd924ffd Dimitris Aragiorgis
64 bd924ffd Dimitris Aragiorgis
def main():
65 bd924ffd Dimitris Aragiorgis
  sys.stderr.write('Creation started: reading environment...\n')
66 bd924ffd Dimitris Aragiorgis
67 bd924ffd Dimitris Aragiorgis
  # Read environment
68 bd924ffd Dimitris Aragiorgis
  env = ReadEnv()
69 bd924ffd Dimitris Aragiorgis
  if env is None:
70 bd924ffd Dimitris Aragiorgis
    sys.stderr.write('Wrong environment. Aborting\n')
71 bd924ffd Dimitris Aragiorgis
    return 1
72 bd924ffd Dimitris Aragiorgis
73 bd924ffd Dimitris Aragiorgis
  file_name, size, shared_dir_param = env
74 bd924ffd Dimitris Aragiorgis
75 bd924ffd Dimitris Aragiorgis
  if shared_dir_param is None:
76 bd924ffd Dimitris Aragiorgis
    shared_dir_param = SHARED_DIR
77 bd924ffd Dimitris Aragiorgis
78 bd924ffd Dimitris Aragiorgis
  file_path = utils.PathJoin(shared_dir_param, file_name)
79 bd924ffd Dimitris Aragiorgis
80 bd924ffd Dimitris Aragiorgis
  sys.stderr.write('Read environment successfully: file name: %s, '
81 bd924ffd Dimitris Aragiorgis
                   'size: %sMB\n' % (file_path, size))
82 bd924ffd Dimitris Aragiorgis
83 bd924ffd Dimitris Aragiorgis
  # Check if directory exists
84 bd924ffd Dimitris Aragiorgis
  if not os.path.isdir(shared_dir_param):
85 bd924ffd Dimitris Aragiorgis
    sys.stderr.write('%s is not a valid directory. Aborting' % shared_dir_param)
86 bd924ffd Dimitris Aragiorgis
    return 1
87 bd924ffd Dimitris Aragiorgis
88 bd924ffd Dimitris Aragiorgis
  # Create file
89 bd924ffd Dimitris Aragiorgis
  try:
90 bd924ffd Dimitris Aragiorgis
    fd = os.open(file_path, os.O_RDWR | os.O_CREAT | os.O_EXCL)
91 bd924ffd Dimitris Aragiorgis
    f = os.fdopen(fd, "w")
92 bd924ffd Dimitris Aragiorgis
    f.truncate(int(size) * 1024 * 1024)
93 bd924ffd Dimitris Aragiorgis
    f.close()
94 bd924ffd Dimitris Aragiorgis
  except EnvironmentError, err:
95 bd924ffd Dimitris Aragiorgis
    if err.errno == errno.EEXIST:
96 bd924ffd Dimitris Aragiorgis
      sys.stderr.write('File already existing: %s\n' % file_path)
97 bd924ffd Dimitris Aragiorgis
      return 1
98 bd924ffd Dimitris Aragiorgis
    sys.stderr.write('Error in file creation: %\n' % str(err))
99 bd924ffd Dimitris Aragiorgis
    return 1
100 bd924ffd Dimitris Aragiorgis
101 bd924ffd Dimitris Aragiorgis
  sys.stderr.write('Image file created successfully\n')
102 bd924ffd Dimitris Aragiorgis
  return 0
103 bd924ffd Dimitris Aragiorgis
104 bd924ffd Dimitris Aragiorgis
105 bd924ffd Dimitris Aragiorgis
if __name__ == "__main__":
106 bd924ffd Dimitris Aragiorgis
    sys.exit(main())