def render(service, middleware): context = get_context(middleware) rcs = [] for i in ( services_config, asigra_config, collectd_config, geli_config, host_config, kbdmap_config, ldap_config, lldp_config, nfs_config, nis_config, nut_config, powerd_config, s3_config, smart_config, snmp_config, staticroute_config, tftp_config, truenas_config, tunable_config, vmware_config, watchdog_config, zfs_config, ): try: rcs += list(i(middleware, context)) except Exception: middleware.logger.error('Failed to generate %s', i.__name__, exc_info=True) write_if_changed('/etc/rc.conf.freenas', '\n'.join(rcs) + '\n')
def loader_config(middleware): config = generate_loader_config(middleware) path = "/boot/loader.conf.local" write_if_changed(path, "\n".join(config) + "\n") # write_if_changed creates the file with # the execute bit so remove it try: os.chmod(path, 0o644) except Exception: # dont crash here pass
async def generate(self, name): group = self.GROUPS.get(name) if group is None: raise ValueError('{0} group not found'.format(name)) for entry in group: renderer = self._renderers.get(entry['type']) if renderer is None: raise ValueError(f'Unknown type: {entry["type"]}') path = os.path.join(self.files_dir, entry['path']) try: rendered = await renderer.render(path) except Exception: self.logger.error( f'Failed to render {entry["type"]}:{entry["path"]}', exc_info=True) continue if rendered is None: continue outfile = '/etc/{0}'.format(entry['path']) changes = write_if_changed(outfile, rendered) # If ownership or permissions are specified, see if # they need to be changed. st = os.stat(outfile) if 'owner' in entry and entry['owner']: try: pw = await self.middleware.run_in_thread( pwd.getpwnam, entry['owner']) if st.st_uid != pw.pw_uid: os.chown(outfile, pw.pw_uid, -1) changes = True except Exception: pass if 'group' in entry and entry['group']: try: gr = await self.middleware.run_in_thread( grp.getgrnam, entry['group']) if st.st_gid != gr.gr_gid: os.chown(outfile, -1, gr.gr_gid) changes = True except Exception: pass if 'mode' in entry and entry['mode']: try: if (st.st_mode & 0x3FF) != entry['mode']: os.chmod(outfile, entry['mode']) changes = True except Exception: pass if not changes: self.logger.debug(f'No new changes for {outfile}')
async def generate(self, name): group = self.GROUPS.get(name) if group is None: raise ValueError('{0} group not found'.format(name)) for entry in group: renderer = self._renderers.get(entry['type']) if renderer is None: raise ValueError(f'Unknown type: {entry["type"]}') path = os.path.join(self.files_dir, entry['path']) try: rendered = await renderer.render(path) except Exception: self.logger.error(f'Failed to render {entry["type"]}:{entry["path"]}', exc_info=True) continue if rendered is None: continue outfile = '/etc/{0}'.format(entry['path']) changes = write_if_changed(outfile, rendered) # If ownership or permissions are specified, see if # they need to be changed. st = os.stat(outfile) if 'owner' in entry and entry['owner']: try: pw = await self.middleware.run_in_thread(pwd.getpwnam, entry['owner']) if st.st_uid != pw.pw_uid: os.chown(outfile, pw.pw_uid, -1) changes = True except Exception: pass if 'group' in entry and entry['group']: try: gr = await self.middleware.run_in_thread(grp.getgrnam, entry['group']) if st.st_gid != gr.gr_gid: os.chown(outfile, -1, gr.gr_gid) changes = True except Exception: pass if 'mode' in entry and entry['mode']: try: if (st.st_mode & 0x3FF) != entry['mode']: os.chmod(outfile, entry['mode']) changes = True except Exception: pass if not changes: self.logger.debug(f'No new changes for {outfile}')
def make_changes(self, full_path, entry, rendered): mode = entry.get('mode', DEFAULT_ETC_PERMS) def opener(path, flags): return os.open(path, os.O_CREAT | os.O_RDWR, mode=mode) outfile_dirname = os.path.dirname(full_path) if outfile_dirname != '/etc': os.makedirs(outfile_dirname, exist_ok=True) with open(full_path, "w", opener=opener) as f: perms_changed = self.set_etc_file_perms(f.fileno(), entry) contents_changed = write_if_changed(f.fileno(), rendered) return perms_changed or contents_changed
async def generate(self, name): group = self.GROUPS.get(name) if group is None: raise ValueError('{0} group not found'.format(name)) for entry in group: renderer = self._renderers.get(entry['type']) if renderer is None: raise ValueError(f'Unknown type: {entry["type"]}') if 'platform' in entry and entry['platform'].upper() != osc.SYSTEM: continue path = os.path.join(self.files_dir, entry.get('local_path') or entry['path']) entry_path = entry['path'] if osc.IS_LINUX: if entry_path.startswith('local/'): entry_path = entry_path[len('local/'):] outfile = f'/etc/{entry_path}' try: rendered = await renderer.render(path) except FileShouldNotExist: self.logger.debug(f'{entry["type"]}:{entry["path"]} file removed.') try: os.unlink(outfile) except FileNotFoundError: pass continue except Exception: self.logger.error(f'Failed to render {entry["type"]}:{entry["path"]}', exc_info=True) continue if rendered is None: continue outfile_dirname = os.path.dirname(outfile) if not os.path.exists(outfile_dirname): os.makedirs(outfile_dirname) changes = write_if_changed(outfile, rendered) # If ownership or permissions are specified, see if # they need to be changed. st = os.stat(outfile) if 'owner' in entry and entry['owner']: try: pw = await self.middleware.run_in_thread(pwd.getpwnam, entry['owner']) if st.st_uid != pw.pw_uid: os.chown(outfile, pw.pw_uid, -1) changes = True except Exception: pass if 'group' in entry and entry['group']: try: gr = await self.middleware.run_in_thread(grp.getgrnam, entry['group']) if st.st_gid != gr.gr_gid: os.chown(outfile, -1, gr.gr_gid) changes = True except Exception: pass if 'mode' in entry and entry['mode']: try: if (st.st_mode & 0x3FF) != entry['mode']: os.chmod(outfile, entry['mode']) changes = True except Exception: pass if not changes: self.logger.debug(f'No new changes for {outfile}')
def loader_config(middleware): config = generate_loader_config(middleware) write_if_changed("/boot/loader.conf.local", "\n".join(config) + "\n")
def render(service, middleware): os.makedirs('/run/truenas_libvirt', exist_ok=True) write_if_changed(LIBVIRTD_CONF_PATH, 'unix_sock_dir = "/run/truenas_libvirt"')