Revision b81b3c96 lib/tools/ensure_dirs.py

b/lib/tools/ensure_dirs.py
22 22

  
23 23
"""
24 24

  
25
import errno
26 25
import os
27 26
import os.path
28 27
import optparse
29 28
import sys
30
import stat
31 29
import logging
32 30

  
33 31
from ganeti import constants
......
49 47
  ])
50 48

  
51 49

  
52
class EnsureError(errors.GenericError):
53
  """Top-level error class related to this script.
54

  
55
  """
56

  
57

  
58
def EnsurePermission(path, mode, uid=-1, gid=-1, must_exist=True,
59
                     _chmod_fn=os.chmod, _chown_fn=os.chown, _stat_fn=os.stat):
60
  """Ensures that given path has given mode.
61

  
62
  @param path: The path to the file
63
  @param mode: The mode of the file
64
  @param uid: The uid of the owner of this file
65
  @param gid: The gid of the owner of this file
66
  @param must_exist: Specifies if non-existance of path will be an error
67
  @param _chmod_fn: chmod function to use (unittest only)
68
  @param _chown_fn: chown function to use (unittest only)
69

  
70
  """
71
  logging.debug("Checking %s", path)
72
  try:
73
    st = _stat_fn(path)
74

  
75
    fmode = stat.S_IMODE(st[stat.ST_MODE])
76
    if fmode != mode:
77
      logging.debug("Changing mode of %s from %#o to %#o", path, fmode, mode)
78
      _chmod_fn(path, mode)
79

  
80
    if max(uid, gid) > -1:
81
      fuid = st[stat.ST_UID]
82
      fgid = st[stat.ST_GID]
83
      if fuid != uid or fgid != gid:
84
        logging.debug("Changing owner of %s from UID %s/GID %s to"
85
                      " UID %s/GID %s", path, fuid, fgid, uid, gid)
86
        _chown_fn(path, uid, gid)
87
  except EnvironmentError, err:
88
    if err.errno == errno.ENOENT:
89
      if must_exist:
90
        raise EnsureError("Path %s should exist, but does not" % path)
91
    else:
92
      raise EnsureError("Error while changing permissions on %s: %s" %
93
                        (path, err))
94

  
95

  
96
def EnsureDir(path, mode, uid, gid, _lstat_fn=os.lstat, _mkdir_fn=os.mkdir,
97
              _ensure_fn=EnsurePermission):
98
  """Ensures that given path is a dir and has given mode, uid and gid set.
99

  
100
  @param path: The path to the file
101
  @param mode: The mode of the file
102
  @param uid: The uid of the owner of this file
103
  @param gid: The gid of the owner of this file
104
  @param _lstat_fn: Stat function to use (unittest only)
105
  @param _mkdir_fn: mkdir function to use (unittest only)
106
  @param _ensure_fn: ensure function to use (unittest only)
107

  
108
  """
109
  logging.debug("Checking directory %s", path)
110
  try:
111
    # We don't want to follow symlinks
112
    st = _lstat_fn(path)
113
  except EnvironmentError, err:
114
    if err.errno != errno.ENOENT:
115
      raise EnsureError("stat(2) on %s failed: %s" % (path, err))
116
    _mkdir_fn(path)
117
  else:
118
    if not stat.S_ISDIR(st[stat.ST_MODE]):
119
      raise EnsureError("Path %s is expected to be a directory, but isn't" %
120
                        path)
121

  
122
  _ensure_fn(path, mode, uid=uid, gid=gid)
123

  
124

  
125 50
def RecursiveEnsure(path, uid, gid, dir_perm, file_perm):
126 51
  """Ensures permissions recursively down a directory.
127 52

  
......
141 66

  
142 67
  for root, dirs, files in os.walk(path):
143 68
    for subdir in dirs:
144
      EnsurePermission(os.path.join(root, subdir), dir_perm, uid=uid, gid=gid)
69
      utils.EnforcePermission(os.path.join(root, subdir), dir_perm, uid=uid,
70
                              gid=gid)
145 71

  
146 72
    for filename in files:
147
      EnsurePermission(os.path.join(root, filename), file_perm, uid=uid,
148
                       gid=gid)
73
      utils.EnforcePermission(os.path.join(root, filename), file_perm, uid=uid,
74
                              gid=gid)
149 75

  
150 76

  
151 77
def EnsureQueueDir(path, mode, uid, gid):
......
159 85
  """
160 86
  for filename in utils.ListVisibleFiles(path):
161 87
    if constants.JOB_FILE_RE.match(filename):
162
      EnsurePermission(utils.PathJoin(path, filename), mode, uid=uid, gid=gid)
88
      utils.EnforcePermission(utils.PathJoin(path, filename), mode, uid=uid,
89
                              gid=gid)
163 90

  
164 91

  
165 92
def ProcessPath(path):
......
176 103
    # No additional parameters
177 104
    assert len(path[5:]) == 0
178 105
    if pathtype == DIR:
179
      EnsureDir(pathname, mode, uid, gid)
106
      utils.MakeDirWithPerm(pathname, mode, uid, gid)
180 107
    elif pathtype == QUEUE_DIR:
181 108
      EnsureQueueDir(pathname, mode, uid, gid)
182 109
  elif pathtype == FILE:
183 110
    (must_exist, ) = path[5:]
184
    EnsurePermission(pathname, mode, uid=uid, gid=gid, must_exist=must_exist)
111
    utils.EnforcePermission(pathname, mode, uid=uid, gid=gid,
112
                            must_exist=must_exist)
185 113

  
186 114

  
187 115
def GetPaths():
......
323 251
    if opts.full_run:
324 252
      RecursiveEnsure(constants.JOB_QUEUE_ARCHIVE_DIR, getent.masterd_uid,
325 253
                      getent.masterd_gid, 0700, 0600)
326
  except EnsureError, err:
254
  except errors.GenericError, err:
327 255
    logging.error("An error occurred while setting permissions: %s", err)
328 256
    return constants.EXIT_FAILURE
329 257

  

Also available in: Unified diff