Skip to content

Commit

Permalink
Implement read only support. Not finished..
Browse files Browse the repository at this point in the history
  • Loading branch information
hkbakke committed Feb 3, 2017
1 parent ff8b2bb commit c1067b7
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/zfssnap.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ def get_policy(self, policy):
'destination': {
'host': None,
'ssh_user': None,
'read_only': False,
'cmds': {
'zfs': self.global_defaults['cmds']['zfs'],
}
Expand All @@ -291,7 +292,10 @@ def get_policy(self, policy):
'zfs': self.global_defaults['cmds']['zfs'],
'cat': self.global_defaults['cmds']['cat']
},
'file_prefix': 'zfssnap'
'file_prefix': 'zfssnap',
'destination': {
'read_only': False
}
})

self._validate_keep(user_config.get('keep', {}))
Expand Down Expand Up @@ -368,6 +372,14 @@ def label(self):
def label(self, value):
self.set_property(ZFSSNAP_LABEL, value)

@property
def read_only(self):
return self.get_property('readonly')

@read_only.setter
def read_only(self, value):
self.set_property('readonly', value)

def _refresh_properties(self):
self.logger.debug('Refreshing zfs properties cache for %s', self.name)
self._properties = {}
Expand Down Expand Up @@ -577,7 +589,7 @@ def _run_replication_cmd(self, in_cmd, out_cmd):

return lines

def receive_from_file(self, label, src_dir, metadata):
def receive_from_file(self, label, src_dir, metadata, read_only=False):
self.logger.info('Selecting %s', metadata.path)

# Make sure the cache is refreshed as the snapshot count might have
Expand Down Expand Up @@ -610,6 +622,10 @@ def receive_from_file(self, label, src_dir, metadata):
#dst_snapshot = self.get_snapshot(metadata.snapshot)
dst_snapshot.repl_status = 'success'

if read_only:
self.logger.info('Marking %s read only' % dst_snapshot.name)
dst_snapshot.read_only = 'on'

# Cleanup files after marking the sync as success as we don't
# really care if this goes well for the sake of sync integrity
self.cleanup_sync_files(metadata, src_dir)
Expand Down Expand Up @@ -647,7 +663,7 @@ def send_to_file(self, label, dst_dir, file_prefix='zfssnap', suffix_length=None
# See comment in replicate()
snapshot.repl_status = 'success'

def replicate(self, dst_dataset, label, base_snapshot):
def replicate(self, dst_dataset, label, base_snapshot, read_only=False):
_base_snapshot = self._get_base_snapshot(label, base_snapshot)
snapshot = self.snapshot(label, recursive=True)

Expand All @@ -672,6 +688,10 @@ def replicate(self, dst_dataset, label, base_snapshot):
# snapshot with repl_status success exists at all times.
snapshot.repl_status = 'success'

if read_only:
self.logger.info('Marking %s read only' % dst_snapshot.name)
dst_snapshot.read_only = 'on'

# For completeness also set repl_status to success on destination.
dst_snapshot = dst_dataset.get_snapshot(snapshot.snapshot_name)
dst_snapshot.repl_status = snapshot.repl_status
Expand Down Expand Up @@ -1102,6 +1122,7 @@ def _run_replicate_policy(self, policy, reset=False, base_snapshot=None):
cmds=policy_config['destination']['cmds'])
dst_dataset = Dataset(dst_host, policy_config['destination']['dataset'])
keep = policy_config['keep']
read_only = policy_config['destination']['read_only']

self._aquire_lock()

Expand All @@ -1112,7 +1133,7 @@ def _run_replicate_policy(self, policy, reset=False, base_snapshot=None):
self.logger.warning('Destroying destination dataset')
dst_dataset.destroy(recursive=True)
else:
src_dataset.replicate(dst_dataset, label, base_snapshot)
src_dataset.replicate(dst_dataset, label, base_snapshot, read_only)

src_dataset.enforce_retention(keep, label, recursive=True, reset=reset,
replication=True)
Expand All @@ -1125,6 +1146,7 @@ def _run_receive_from_file_policy(self, policy, reset=False):
dst_dataset = Dataset(dst_host, policy_config['destination']['dataset'])
src_dir = policy_config['source']['dir']
file_prefix = policy_config.get('file_prefix', None)
read_only = policy_config['destination']['read_only']

self._aquire_lock()

Expand All @@ -1139,7 +1161,8 @@ def _run_receive_from_file_policy(self, policy, reset=False):
metadata_files = self._get_metadata_files(src_dir, label, file_prefix)

for metadata in sorted(metadata_files, key=attrgetter('datetime')):
dst_dataset.receive_from_file(label, src_dir, metadata)
dst_dataset.receive_from_file(label, src_dir, metadata,
read_only)
except SegmentMissingException as e:
self.logger.info(e)

Expand Down

0 comments on commit c1067b7

Please sign in to comment.