Skip to content

Commit

Permalink
feat(middleware): common method write_if_changed
Browse files Browse the repository at this point in the history
  • Loading branch information
william-gr committed Mar 11, 2019
1 parent ff04ac8 commit e19e7c3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 41 deletions.
13 changes: 3 additions & 10 deletions src/middlewared/middlewared/etc_files/loader.py
@@ -1,22 +1,15 @@
import logging
import os
import subprocess
import sysctl

from middlewared.utils.io import write_if_changed

logger = logging.getLogger(__name__)


def loader_config(middleware):
config = generate_loader_config(middleware)

with open(os.open("/boot/loader.conf.local", os.O_CREAT | os.O_RDWR), "w+") as f:
f.seek(0)
data = f.read()
new = "\n".join(config) + "\n"
if data != new:
f.seek(0)
f.write(new)
f.truncate()
write_if_changed("/boot/loader.conf.local", "\n".join(config) + "\n")


def generate_loader_config(middleware):
Expand Down
15 changes: 3 additions & 12 deletions src/middlewared/middlewared/etc_files/rc.conf.py
Expand Up @@ -6,6 +6,8 @@
import subprocess
import sysctl

from middlewared.utils.io import write_if_changed

NFS_BINDIP_NOTFOUND = '/tmp/.nfsbindip_notfound'
RE_FIRMWARE_VERSION = re.compile(r'Firmware Revision\s*:\s*(\S+)', re.M)

Expand Down Expand Up @@ -444,16 +446,5 @@ def render(service, middleware):
except Exception:
middleware.logger.error('Failed to generate %s', i.__name__, exc_info=True)

with open(os.open('/etc/rc.conf.freenas', os.O_CREAT | os.O_RDWR), 'w+') as f:
f.seek(0)
current = f.read()
new = '\n'.join(rcs) + '\n'
if current != new:
f.seek(0)
f.write('\n'.join(rcs) + '\n')
f.truncate()
os.fsync(f)

# Signal init later to make sure the file is synced to filesystem
if current != new:
if write_if_changed('/etc/rc.conf.freenas', '\n'.join(rcs) + '\n'):
os.kill(1, signal.SIGALRM)
21 changes: 2 additions & 19 deletions src/middlewared/middlewared/plugins/etc.py
@@ -1,10 +1,10 @@
from mako import exceptions
from mako.lookup import TemplateLookup
from middlewared.service import Service
from middlewared.utils.io import write_if_changed

import asyncio
import grp
import hashlib
import imp
import os
import pwd
Expand Down Expand Up @@ -249,24 +249,7 @@ async def generate(self, name):
continue

outfile = '/etc/{0}'.format(entry['path'])
changes = False

# Check hash of generated and existing file
# Do not rewrite if they are the same
if os.path.exists(outfile):
with open(outfile, 'rb') as f:
existing_hash = hashlib.sha256(f.read()).hexdigest()

new_hash = hashlib.sha256(rendered.encode('utf-8')).hexdigest()
if existing_hash != new_hash:
with open(outfile, 'w') as f:
f.write(rendered)
changes = True

if not os.path.exists(outfile):
with open(outfile, 'w') as f:
f.write(rendered)
changes = True
changes = write_if_changed(outfile, rendered)

# If ownership or permissions are specified, see if
# they need to be changed.
Expand Down
21 changes: 21 additions & 0 deletions src/middlewared/middlewared/utils/io.py
@@ -0,0 +1,21 @@
import os


def write_if_changed(path, data):

if isinstance(data, str):
data = data.encode()

changed = False

with open(os.open(path, os.O_CREAT | os.O_RDWR), 'wb+') as f:
f.seek(0)
current = f.read()
if current != data:
changed = True
f.seek(0)
f.write(data)
f.truncate()
os.fsync(f)

return changed

0 comments on commit e19e7c3

Please sign in to comment.