/
autosnap.py
360 lines (306 loc) · 13 KB
/
autosnap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python
#
# Copyright (c) 2011, 2012 iXsystems, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
import cPickle as pickle
import logging
import os
import re
import sys
import uuid
sys.path.append('/usr/local/www')
sys.path.append('/usr/local/www/freenasUI')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'freenasUI.settings')
# Make sure to load all modules
from django.db.models.loading import cache
cache.get_apps()
from freenasUI.freeadmin.apppool import appPool
from freenasUI.storage.models import Task, Replication
from datetime import datetime, time, timedelta
from freenasUI.common.locks import mntlock
from freenasUI.common.pipesubr import pipeopen
from freenasUI.common.system import send_mail
from freenasUI.common.timesubr import isTimeBetween
from freenasUI.storage.models import VMWarePlugin
from lockfile import LockFile
log = logging.getLogger('tools.autosnap')
# NOTE
#
# In this script there is no asynchnous programming so ALL locks are obtained
# in the blocking way.
#
# With this assumption, the mntlock SHOULD only be instansized once during the
# whole lifetime of this script.
#
MNTLOCK = mntlock()
VMWARE_FAILS = '/var/tmp/.vmwaresnap_fails'
# Set to True if verbose log desired
debug = False
def snapinfodict2datetime(snapinfo):
year = int(snapinfo['year'])
month = int(snapinfo['month'])
day = int(snapinfo['day'])
hour = int(snapinfo['hour'])
minute = int(snapinfo['minute'])
return datetime(year, month, day, hour, minute)
def snap_expired(snapinfo, snaptime):
snapinfo_expirationtime = snapinfodict2datetime(snapinfo)
snap_ttl_value = int(snapinfo['retcount'])
snap_ttl_unit = snapinfo['retunit']
if snap_ttl_unit == 'h':
snapinfo_expirationtime = snapinfo_expirationtime + timedelta(hours=snap_ttl_value)
elif snap_ttl_unit == 'd':
snapinfo_expirationtime = snapinfo_expirationtime + timedelta(days=snap_ttl_value)
elif snap_ttl_unit == 'w':
snapinfo_expirationtime = snapinfo_expirationtime + timedelta(days=7 * snap_ttl_value)
elif snap_ttl_unit == 'm':
snapinfo_expirationtime = snapinfo_expirationtime + timedelta(days=int(30.436875 * snap_ttl_value))
elif snap_ttl_unit == 'y':
snapinfo_expirationtime = snapinfo_expirationtime + timedelta(days=int(365.2425 * snap_ttl_value))
return snapinfo_expirationtime <= snaptime
def isMatchingTime(task, snaptime):
curtime = time(snaptime.hour, snaptime.minute)
repeat_type = task.task_repeat_unit
if not isTimeBetween(curtime, task.task_begin, task.task_end):
return False
if repeat_type == 'daily':
return True
if repeat_type == 'weekly':
cur_weekday = snaptime.weekday() + 1
if ('%d' % cur_weekday) in task.task_byweekday.split(','):
return True
return False
# Detect if another instance is running
def exit_if_running(pid):
log.debug("Checking if process %d is still alive", pid)
try:
os.kill(pid, 0)
# If we reached here, there is another process in progress
log.debug("Process %d still working, quitting", pid)
sys.exit(0)
except OSError:
log.debug("Process %d gone", pid)
def autorepl_running():
if not os.path.exists('/var/run/autorepl.pid'):
return False
with open('/var/run/autorepl.pid', 'r') as f:
pid = f.read().strip('\n')
if not pid.isdigit():
return False
try:
os.kill(int(pid), 0)
return True
except OSError:
return False
appPool.hook_tool_run('autosnap')
mypid = os.getpid()
# (mis)use MNTLOCK as PIDFILE lock.
locked = True
try:
MNTLOCK.lock_try()
except IOError:
locked = False
if not locked:
sys.exit(0)
AUTOSNAP_PID = -1
try:
with open('/var/run/autosnap.pid') as pidfile:
AUTOSNAP_PID = int(pidfile.read())
except:
pass
if AUTOSNAP_PID != -1:
exit_if_running(AUTOSNAP_PID)
with open('/var/run/autosnap.pid', 'w') as pidfile:
pidfile.write('%d' % mypid)
MNTLOCK.unlock()
now = datetime.now().replace(microsecond=0)
if now.second < 30 or now.minute == 59:
snaptime = now.replace(second=0)
else:
snaptime = now.replace(minute=now.minute + 1, second=0)
mp_to_task_map = {}
# Grab all matching tasks into a tree.
# Since the snapshot we make have the name 'foo@auto-%Y%m%d.%H%M-{expire time}'
# format, we just keep one task.
TaskObjects = Task.objects.filter(task_enabled=True)
taskpath = {'recursive': [], 'nonrecursive': []}
for task in TaskObjects:
if isMatchingTime(task, snaptime):
if task.task_recursive:
taskpath['recursive'].append(task.task_filesystem)
else:
taskpath['nonrecursive'].append(task.task_filesystem)
fs = task.task_filesystem
expire_time = ('%s%s' % (task.task_ret_count, task.task_ret_unit[0])).__str__()
tasklist = []
if (fs, expire_time) in mp_to_task_map:
tasklist = mp_to_task_map[(fs, expire_time)]
tasklist.append(task)
else:
tasklist = [task]
mp_to_task_map[(fs, expire_time)] = tasklist
re_path = re.compile("^((" + '|'.join(taskpath['nonrecursive']) +
")@|(" + '|'.join(taskpath['recursive']) + ")[@/])")
# Only proceed further if we are going to generate any snapshots for this run
if len(mp_to_task_map) > 0:
# Grab all existing snapshot and filter out the expiring ones
snapshots = {}
snapshots_pending_delete = set()
previous_prefix = '/'
zfsproc = pipeopen("/sbin/zfs list -t snapshot -H -o name", debug, logger=log)
lines = zfsproc.communicate()[0].split('\n')
reg_autosnap = re.compile('^auto-(?P<year>\d{4})(?P<month>\d{2})(?P<day>\d{2}).(?P<hour>\d{2})(?P<minute>\d{2})-(?P<retcount>\d+)(?P<retunit>[hdwmy])$')
for snapshot_name in lines:
if snapshot_name != '':
fs, snapname = snapshot_name.split('@')
snapname_match = reg_autosnap.match(snapname)
if snapname_match is not None:
snap_infodict = snapname_match.groupdict()
snap_ret_policy = '%s%s' % (snap_infodict['retcount'], snap_infodict['retunit'])
if snap_expired(snap_infodict, snaptime):
# Only delete the snapshot if there's a snapshot task enabled that created it.
if re_path:
if re_path.match(snapshot_name):
# Destroy of expired snapshots is recursive, so only request so on the
# toplevel.
if fs.startswith(previous_prefix):
if ('%s@%s' % (previous_prefix[:-1], snapname)) in snapshots_pending_delete:
continue
else:
previous_prefix = '%s/' % (fs)
snapshots_pending_delete.add(snapshot_name)
else:
if (fs, snap_ret_policy) in mp_to_task_map:
if (fs, snap_ret_policy) in snapshots:
last_snapinfo = snapshots[(fs, snap_ret_policy)]
if snapinfodict2datetime(last_snapinfo) < snapinfodict2datetime(snap_infodict):
snapshots[(fs, snap_ret_policy)] = snap_infodict
else:
snapshots[(fs, snap_ret_policy)] = snap_infodict
list_mp = mp_to_task_map.keys()
for mpkey in list_mp:
tasklist = mp_to_task_map[mpkey]
if mpkey in snapshots:
snapshot_time = snapinfodict2datetime(snapshots[mpkey])
for taskindex in range(len(tasklist) - 1, -1, -1):
task = tasklist[taskindex]
if snapshot_time + timedelta(minutes=task.task_interval) > snaptime:
del tasklist[taskindex]
if len(tasklist) == 0:
del mp_to_task_map[mpkey]
snaptime_str = snaptime.strftime('%Y%m%d.%H%M')
for mpkey, tasklist in mp_to_task_map.items():
fs, expire = mpkey
recursive = False
for task in tasklist:
if task.task_recursive is True:
recursive = True
if recursive is True:
rflag = ' -r'
else:
rflag = ''
snapname = '%s@auto-%s-%s' % (fs, snaptime_str, expire)
# If there's a VMWare Plugin object for this filesystem
# snapshot the VMs before taking the ZFS snapshot
from pysphere import VIServer
server = VIServer()
qs = VMWarePlugin.objects.filter(filesystem=fs)
vmsnapname = str(uuid.uuid4())
vmsnapdescription = str(datetime.now()).split('.')[0] + " FreeNAS Created Snapshot"
snapvms = []
snapvmfails = []
for obj in qs:
try:
server.connect(obj.hostname, obj.username, obj.get_password())
except:
log.warn("VMware login failed to %s", obj.hostname)
continue
vmlist = server.get_registered_vms(status='poweredOn')
for vm in vmlist:
if vm.startswith("[%s]" % obj.datastore):
vm1 = server.get_vm_by_path(vm)
try:
vm1.create_snapshot(vmsnapname, description=vmsnapdescription, memory=False)
except:
log.warn("Snapshot of VM %s failed", vm1)
snapvmfails.append(vm1)
snapvms.append(vm1)
if snapvmfails:
try:
with LockFile(VMWARE_FAILS) as lock:
with open(VMWARE_FAILS, 'rb') as f:
fails = pickle.load(f)
except:
fails = {}
fails[snapname] = [vm.get_property('path') for vm in snapvmfails]
with LockFile(VMWARE_FAILS) as lock:
with open(VMWARE_FAILS, 'wb') as f:
pickle.dump(fails, f)
send_mail(
subject="VMware Snapshot failed! (%s)" % snapname,
text="""
Hello,
The following VM failed to snapshot %s:
%s
""" % (snapname, ' \n'.join([vm.get_property('path') for vm in snapvmfails])),
channel='snapvmware'
)
if len(snapvms) > 0 and len(snapvmfails) == 0:
vmflag = '-o freenas:vmsynced=Y '
else:
vmflag = ''
# If there is associated replication task, mark the snapshots as 'NEW'.
if Replication.objects.filter(repl_filesystem=fs, repl_enabled=True).count() > 0:
MNTLOCK.lock()
snapcmd = '/sbin/zfs snapshot%s %s"%s"' % (rflag, vmflag, snapname)
proc = pipeopen(snapcmd, logger=log)
err = proc.communicate()[1]
if proc.returncode != 0:
log.error("Failed to create snapshot '%s': %s", snapname, err)
MNTLOCK.unlock()
else:
snapcmd = '/sbin/zfs snapshot%s %s"%s"' % (rflag, vmflag, snapname)
proc = pipeopen(snapcmd, logger=log)
err = proc.communicate()[1]
if proc.returncode != 0:
log.error("Failed to create snapshot '%s': %s", snapname, err)
for vm in snapvms:
if vm not in snapvmfails:
vm.delete_named_snapshot(vmsnapname)
MNTLOCK.lock()
if not autorepl_running():
for snapshot in snapshots_pending_delete:
snapcmd = '/sbin/zfs destroy -r -d "%s"' % (snapshot) # snapshots with clones will have destruction deferred
proc = pipeopen(snapcmd, logger=log)
err = proc.communicate()[1]
if proc.returncode != 0:
log.error("Failed to destroy snapshot '%s': %s", snapshot, err)
else:
log.debug("Autorepl running, skip destroying snapshots")
MNTLOCK.unlock()
os.unlink('/var/run/autosnap.pid')
os.execl('/usr/local/bin/python',
'python',
'/usr/local/www/freenasUI/tools/autorepl.py')