def __fstab_remove__(self): """ Removes the users mount by index or matching string. :return: The destination of the specified mount """ removed = False index = 0 with open("{}/jails/{}/fstab".format(self.iocroot, self.uuid), "r") as \ fstab: with open_atomic( "{}/jails/{}/fstab".format(self.iocroot, self.uuid), "w") as _fstab: for line in fstab.readlines(): if line.rsplit("#")[0].rstrip() == self.mount or index \ == self.index and not removed: removed = True dest = line.split()[1] continue else: _fstab.write(line) index += 1 if removed: self.lgr.info( "Successfully removed mount from {} ({})'s fstab".format( self.uuid, self.tag)) return dest # Needed for umounting, otherwise we lack context. else: self.lgr.info("No matching fstab entry.") exit()
def __fstab_remove__(self): removed = False index = 0 with open("{}/jails/{}/fstab".format(self.iocroot, self.uuid), "r") as \ fstab: with open_atomic( "{}/jails/{}/fstab".format(self.iocroot, self.uuid), "w") as _fstab: for line in fstab.readlines(): if line.rsplit("#")[0].rstrip() == self.mount or index \ == self.index and not removed: removed = True continue else: _fstab.write(line) index += 1 if removed: self.lgr.info( "Successfully removed mount from {} ({})'s fstab".format( self.uuid, self.tag)) else: self.lgr.info("No fstab entry matching: {}".format(self.mount))
def start_generate_resolv(self): resolver = self.get("resolver") # compat if resolver != "/etc/resolv.conf" and resolver != "none": with open_atomic("{}/root/etc/resolv.conf".format(self.path), "w") as resolv_conf: for line in resolver.split(";"): resolv_conf.write(line + "\n") elif resolver == "none": copy("/etc/resolv.conf", "{}/root/etc/resolv.conf".format(self.path)) else: copy(resolver, "{}/root/etc/resolv.conf".format(self.path))
def __fstab_add__(self): with open("{}/jails/{}/fstab".format(self.iocroot, self.uuid), "r") as \ fstab: with open_atomic( "{}/jails/{}/fstab".format(self.iocroot, self.uuid), "w") as _fstab: # open_atomic will empty the file, we need these still. for line in fstab.readlines(): _fstab.write(line) _fstab.write("{} # Added by iocage on {}\n".format( self.mount, datetime.utcnow().strftime("%F %T"))) self.lgr.info("Successfully added mount to {} ({})'s fstab".format( self.uuid, self.tag))
def json_write(self, data, _file="/config.json"): """Write a JSON file at the location given with supplied data.""" with open_atomic(self.location + _file, 'w') as out: json.dump(data, out, sort_keys=True, indent=4, ensure_ascii=False)