Revision 54ca6e4b lib/watcher/state.py

b/lib/watcher/state.py
28 28
import logging
29 29

  
30 30
from ganeti import utils
31
from ganeti import constants
32 31
from ganeti import serializer
33 32
from ganeti import errors
34 33

  
......
100 99

  
101 100
    self._orig_data = serializer.Dump(self._data)
102 101

  
103
  def Save(self):
102
  def Save(self, filename):
104 103
    """Save state to file, then unlock and close it.
105 104

  
106 105
    """
......
109 108
    serialized_form = serializer.Dump(self._data)
110 109
    if self._orig_data == serialized_form:
111 110
      logging.debug("Data didn't change, just touching status file")
112
      os.utime(constants.WATCHER_STATEFILE, None)
111
      os.utime(filename, None)
113 112
      return
114 113

  
115 114
    # We need to make sure the file is locked before renaming it, otherwise
116 115
    # starting ganeti-watcher again at the same time will create a conflict.
117
    fd = utils.WriteFile(constants.WATCHER_STATEFILE,
116
    fd = utils.WriteFile(filename,
118 117
                         data=serialized_form,
119 118
                         prewrite=utils.LockFile, close=False)
120 119
    self.statefile = os.fdopen(fd, 'w+')
......
147 146

  
148 147
    ndata = self._data["node"]
149 148

  
150
    if name not in ndata:
151
      ndata[name] = {}
149
    ndata.setdefault(name, {})[KEY_BOOT_ID] = bootid
152 150

  
153
    ndata[name][KEY_BOOT_ID] = bootid
154

  
155
  def NumberOfRestartAttempts(self, instance):
151
  def NumberOfRestartAttempts(self, instance_name):
156 152
    """Returns number of previous restart attempts.
157 153

  
158 154
    @type instance: L{Instance}
......
161 157
    """
162 158
    idata = self._data["instance"]
163 159

  
164
    if instance.name in idata:
165
      return idata[instance.name][KEY_RESTART_COUNT]
160
    if instance_name in idata:
161
      return idata[instance_name][KEY_RESTART_COUNT]
166 162

  
167 163
    return 0
168 164

  
......
174 170

  
175 171
    """
176 172
    idict = self._data["instance"]
173

  
177 174
    # First, delete obsolete instances
178 175
    obsolete_instances = set(idict).difference(instances)
179 176
    for inst in obsolete_instances:
180 177
      logging.debug("Forgetting obsolete instance %s", inst)
181
      del idict[inst]
178
      idict.pop(inst, None)
182 179

  
183 180
    # Second, delete expired records
184 181
    earliest = time.time() - RETRY_EXPIRATION
......
186 183
                         if idict[i][KEY_RESTART_WHEN] < earliest]
187 184
    for inst in expired_instances:
188 185
      logging.debug("Expiring record for instance %s", inst)
189
      del idict[inst]
186
      idict.pop(inst, None)
190 187

  
191
  def RecordRestartAttempt(self, instance):
188
  def RecordRestartAttempt(self, instance_name):
192 189
    """Record a restart attempt.
193 190

  
194 191
    @type instance: L{Instance}
......
197 194
    """
198 195
    idata = self._data["instance"]
199 196

  
200
    if instance.name not in idata:
201
      inst = idata[instance.name] = {}
202
    else:
203
      inst = idata[instance.name]
204

  
197
    inst = idata.setdefault(instance_name, {})
205 198
    inst[KEY_RESTART_WHEN] = time.time()
206 199
    inst[KEY_RESTART_COUNT] = inst.get(KEY_RESTART_COUNT, 0) + 1
207 200

  
208
  def RemoveInstance(self, instance):
201
  def RemoveInstance(self, instance_name):
209 202
    """Update state to reflect that a machine is running.
210 203

  
211 204
    This method removes the record for a named instance (as we only
......
217 210
    """
218 211
    idata = self._data["instance"]
219 212

  
220
    if instance.name in idata:
221
      del idata[instance.name]
213
    idata.pop(instance_name, None)

Also available in: Unified diff