Statistics
| Branch: | Revision:

root / shared-filer / remove @ 68abdf0d

History | View | Annotate | Download (2.5 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
"""Remove an existing file under a shared directory
21

    
22
This program removes an existing file from a shared directory. It takes
23
it's input from environment variables. Specifically the following variables
24
should be present:
25

    
26
 - VOL_NAME: The name of the new Image file
27

    
28
Returns O after successfull creation, 1 on failure
29

    
30
"""
31

    
32
import os
33
import sys
34

    
35
sys.path.insert(0, "/etc/ganeti/share")
36

    
37
from ganeti import utils
38

    
39

    
40
# Default shared directory
41
SHARED_DIR = "/srv/ganeti/shared-file-storage"
42

    
43

    
44
def ReadEnv():
45
  """Read the mandatory enviromental variables
46

    
47
  """
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
  shared_dir_param = os.getenv("EXTP_SHARED_DIR")
53
  if shared_dir_param is not None:
54
    sys.stderr.write('Found shared dir param, value: %s\n' % shared_dir_param)
55

    
56
  return (name, shared_dir_param)
57

    
58

    
59
def main():
60
  sys.stderr.write('Removal started: reading environment...\n')
61

    
62
  # Read environment
63
  env = ReadEnv()
64
  if env is None:
65
    sys.stderr.write('Wrong environment. Aborting\n')
66
    return 1
67

    
68
  file_name, shared_dir_param = env
69

    
70
  if shared_dir_param is None:
71
    shared_dir_param = SHARED_DIR
72

    
73
  file_path = utils.PathJoin(shared_dir_param, file_name)
74

    
75
  sys.stderr.write('Read environment successfully: file name: %s\n' % file_path)
76

    
77
  # Remove file
78
  cmd = ["rm", "-f", "%s" % file_path]
79
  result = utils.RunCmd(cmd)
80
  if result.failed:
81
    sys.stderr.write('Cannot remove Image file %s with `rm -f`: %s - %s\n' %
82
                     (file_path, result.fail_reason, result.output))
83
    return 1
84

    
85
  sys.stderr.write('Image file %s removed successfully\n' % file_path)
86
  return 0
87

    
88

    
89
if __name__ == "__main__":
90
    sys.exit(main())