def __check_datasets__(self): """ Loops through the required datasets and if there is root privilege will then create them. """ datasets = ("iocage", "iocage/download", "iocage/images", "iocage/jails", "iocage/log", "iocage/releases", "iocage/templates") for dataset in datasets: zfs_dataset_name = f"{self.pool}/{dataset}" try: ds = Dataset(zfs_dataset_name) if not ds.exists: raise ZFSException(-1, 'Dataset does not exist') elif not ds.path: iocage_lib.ioc_common.logit( { "level": "EXCEPTION", "message": f'Please set a mountpoint on {ds.name}' }, _callback=self.callback) except ZFSException: # Doesn't exist if os.geteuid() != 0: raise RuntimeError("Run as root to create missing" " datasets!") iocage_lib.ioc_common.logit( { "level": "INFO", "message": f"Creating {self.pool}/{dataset}" }, _callback=self.callback, silent=self.silent) dataset_options = { "compression": "lz4", "aclmode": "passthrough", "aclinherit": "passthrough" } ds = Dataset(zfs_dataset_name) ds.create({'properties': dataset_options}) prop = ds.properties.get("exec") if prop != "on": iocage_lib.ioc_common.logit( { "level": "EXCEPTION", "message": f"Dataset \"{dataset}\" has " f"exec={prop} (should be on)" }, _callback=self.callback)
def fetch_download(self, _list, missing=False): """Creates the download dataset and then downloads the RELEASE.""" dataset = f"{self.iocroot}/download/{self.release}" fresh = False if not os.path.isdir(dataset): fresh = True dataset = f"{self.pool}/iocage/download/{self.release}" ds = Dataset(dataset) if not ds.exists: ds.create({'properties': {'compression': 'lz4'}}) if not ds.mounted: ds.mount() if missing or fresh: release_download_path = os.path.join(self.iocroot, 'download', self.release) for f in _list: if self.hardened: _file = f"{self.server}/{self.root_dir}/{f}" if f == "lib32.txz": continue else: _file = f"{self.server}/{self.root_dir}/" \ f"{self.release}/{f}" if self.auth == "basic": r = requests.get(_file, auth=(self.user, self.password), verify=self.verify, stream=True) elif self.auth == "digest": r = requests.get(_file, auth=requests.auth.HTTPDigestAuth( self.user, self.password), verify=self.verify, stream=True) else: r = requests.get(_file, verify=self.verify, stream=True) status = r.status_code == requests.codes.ok if not status: r.raise_for_status() with open(os.path.join(release_download_path, f), 'wb') as txz: file_size = int(r.headers['Content-Length']) chunk_size = 1024 * 1024 total = file_size / chunk_size start = time.time() dl_progress = 0 last_progress = 0 for i, chunk in enumerate( r.iter_content(chunk_size=chunk_size), 1): if chunk: elapsed = time.time() - start dl_progress += len(chunk) txz.write(chunk) progress = float(i) / float(total) if progress >= 1.: progress = 1 progress = round(progress * 100, 0) if progress != last_progress: text = self.update_progress( progress, f'Downloading: {f}', elapsed, chunk_size) if progress % 10 == 0: # Not for user output, but for callback # heartbeats iocage_lib.ioc_common.logit( { 'level': 'INFO', 'message': text.rstrip() }, _callback=self.callback, silent=True) last_progress = progress start = time.time()