Revision cc7f5bfc lib/ssconf.py

b/lib/ssconf.py
103 103
    - keys are restricted to predefined values
104 104

  
105 105
  """
106
  def __init__(self, cfg_location=None):
106
  def __init__(self, cfg_location=None, _lockfile=pathutils.SSCONF_LOCK_FILE):
107 107
    if cfg_location is None:
108 108
      self._cfg_dir = pathutils.DATA_DIR
109 109
    else:
110 110
      self._cfg_dir = cfg_location
111 111

  
112
    self._lockfile = _lockfile
113

  
112 114
  def KeyToFilename(self, key):
113 115
    """Convert a given key into filename.
114 116

  
......
136 138
      raise errors.ConfigurationError("Can't read ssconf file %s: %s" %
137 139
                                      (filename, str(err)))
138 140

  
139
  def WriteFiles(self, values):
141
  def WriteFiles(self, values, dry_run=False):
140 142
    """Writes ssconf files used by external scripts.
141 143

  
142 144
    @type values: dict
143 145
    @param values: Dictionary of (name, value)
146
    @type dry_run boolean
147
    @param dry_run: Whether to perform a dry run
144 148

  
145 149
    """
146
    ssconf_lock = utils.FileLock.Open(pathutils.SSCONF_LOCK_FILE)
150
    ssconf_lock = utils.FileLock.Open(self._lockfile)
147 151

  
148 152
    # Get lock while writing files
149 153
    ssconf_lock.Exclusive(blocking=True, timeout=SSCONF_LOCK_TIMEOUT)
......
151 155
      for name, value in values.iteritems():
152 156
        if value and not value.endswith("\n"):
153 157
          value += "\n"
158

  
154 159
        if len(value) > _MAX_SIZE:
155
          raise errors.ConfigurationError("ssconf file %s above maximum size" %
156
                                          name)
160
          msg = ("Value '%s' has a length of %s bytes, but only up to %s are"
161
                 " allowed" % (name, len(value), _MAX_SIZE))
162
          raise errors.ConfigurationError(msg)
163

  
157 164
        utils.WriteFile(self.KeyToFilename(name), data=value,
158
                        mode=constants.SS_FILE_PERMS)
165
                        mode=constants.SS_FILE_PERMS,
166
                        dry_run=dry_run)
159 167
    finally:
160 168
      ssconf_lock.Unlock()
161 169

  
......
320 328
                                      " family: %s" % err)
321 329

  
322 330

  
323
def WriteSsconfFiles(values):
331
def WriteSsconfFiles(values, dry_run=False):
324 332
  """Update all ssconf files.
325 333

  
326 334
  Wrapper around L{SimpleStore.WriteFiles}.
327 335

  
328 336
  """
329
  SimpleStore().WriteFiles(values)
337
  SimpleStore().WriteFiles(values, dry_run=dry_run)
330 338

  
331 339

  
332 340
def GetMasterAndMyself(ss=None):

Also available in: Unified diff