def Run(self): downloader = download.Download() for arg in self._args: src = arg[0] dst = arg[1] full_url = download.Transform(src, self._build_info) # support legacy untagged short filenames if not (download.IsRemote(full_url) or download.IsLocal(full_url)): full_url = download.PathCompile(self._build_info, file_name=full_url) try: file_util.CreateDirectories(dst) except file_util.Error as e: raise ActionError('Could not create destination directory %s. %s' % (dst, e)) try: downloader.DownloadFile(full_url, dst, show_progress=True) except download.DownloadError as e: downloader.PrintDebugInfo() raise ActionError('Transfer error while downloading %s: %s' % (full_url, str(e))) if len(arg) > 2 and arg[2]: logging.info('Verifying SHA256 hash for %s.', dst) hash_ok = downloader.VerifyShaHash(dst, arg[2]) if not hash_ok: raise ActionError('SHA256 hash for %s was incorrect.' % dst)
def _ProcessTasks(self, tasks): """Process the pending tasks list. Args: tasks: The list of pending tasks. Raises: ConfigRunnerError: failure to confirm verify_urls """ if constants.FLAGS.verify_urls: dl = download.Download() for url in constants.FLAGS.verify_urls: if not dl.CheckUrl(url, [200], max_retries=-1): raise ConfigRunnerError('verify_urls failed for url %s' % url) while tasks: self._build_info.ActiveConfigPath(set_to=tasks[0]['path']) self._build_info.ConfigServer(set_to=tasks[0]['server']) entry = tasks[0]['data'] for element in entry: if element == 'policy': for line in entry['policy']: self._Policy(line) else: try: self._ProcessAction(element, entry[element]) except base.ConfigError as e: raise ConfigRunnerError(e) except base.actions.RestartEvent as e: if e.task_list_path: self._task_list_path = e.task_list_path if not e.retry_on_restart: self._PopTask(tasks) if e.pop_next: self._PopTask(tasks) power.Restart(e.timeout, str(e)) sys.exit(0) except base.actions.ShutdownEvent as e: if e.task_list_path: self._task_list_path = e.task_list_path if not e.retry_on_restart: self._PopTask(tasks) if e.pop_next: self._PopTask(tasks) power.Shutdown(e.timeout, str(e)) sys.exit(0) self._PopTask(tasks)
def Read(path: str): """Read a config file at path and return any data it contains. Will attempt to download files from remote repositories prior to reading. Args: path: The path (either local or remote) to read from. Returns: The parsed YAML content from the file. Raises: Error: Failure retrieving a remote file or parsing file content. """ if re.match('^http(s)?://', path): downloader = download.Download() try: path = downloader.DownloadFileTemp(path) except download.DownloadError as e: raise Error('Could not download yaml file %s: %s' % (path, str(e))) return _YamlReader(path)
def __init__(self): self._downloader = download.Download(show_progress=False)