def update(): # Set the local repository directory containing the metadata files. tuf.conf.repository_directory = 'C:\Users\Sarah\Documents\GitHub\TUFeverywhere\Project' repository_mirrors = {'mirror1': {'url_prefix': 'http://172.16.42.2:8001', 'metadata_path': 'metadata', 'targets_path': 'targets', 'confined_target_dirs': ['']}} # Create the Upater object using the updater name 'tuf-example' # and the repository mirrors defined above. updater = tuf.client.updater.Updater('tuf-example', repository_mirrors) # Set the local destination directory to save the target files. destination_directory = './targets' # Refresh the repository's top-level roles, store the target information for # all the targets tracked, and determine which of these targets have been # updated. updater.refresh() all_targets = updater.all_targets() updated_targets = updater.updated_targets(all_targets, destination_directory) # Download each of these updated targets and save them locally. for target in updated_targets: try: updater.download_target(target, destination_directory) except tuf.DownloadError, e: pass
def update_conf(filename, host): """ <Purpose> Downloads the FILENAME file from the localhost http server <Arguments> <Exceptions> <Side Effects> None. <Returns> None. """ # conf = cc.load_conf(os.path.join(os.getcwd(),'Configuration/conf.json')) # Set the local repositories directory containing all of the metadata files. tuf.settings.repositories_directory = 'tufclient' # Set the repository mirrors. This dictionary is needed by the Updater # class of updater.py.getupdates.pygetupdates.py repository_mirrors = {'mirror': {'url_prefix': host, 'metadata_path': 'metadata', 'targets_path': 'targets', 'confined_target_dirs': ['']}} # Create the repository object using the repository name 'repository' # and the repository mirrors defined above. updater = tuf.client.updater.Updater('tufrepo', repository_mirrors) # The local destination directory to save the target files. destination_directory = './tufclient/tuftargets' logging.info("TUF is updating...") # Refresh the repository's top-level roles... updater.refresh(unsafely_update_root_if_necessary=False) # ... and store the target information for the target file specified on the # command line, and determine which of these targets have been updated. target_fileinfo = [] target_fileinfo.append(updater.get_one_valid_targetinfo( filename)) updated_targets = updater.updated_targets(target_fileinfo, destination_directory) # Retrieve each of these updated targets and save them to the destination # directory. for target in updated_targets: try: updater.download_target(target, destination_directory) logging.info("TUF updating %s", target) except tuf.exceptions.DownloadError: logging.error("TUF update failed %s", target) # Remove any files from the destination directory that are no longer being # tracked. updater.remove_obsolete_targets(destination_directory) if len(updated_targets) > 0: return True return False
def _get_digests(self, target, sizes=False): import tuf.client.updater updater = tuf.client.updater.Updater('repository', self._repository_mirrors) tgt = updater.get_one_valid_targetinfo(target) updater.download_target(tgt, self._copy_targets_dir) with open(path.join(self._copy_targets_dir, target), 'rb') as f: manifest = f.read().decode('utf-8') return self._dxf.get_alias(manifest=manifest, sizes=sizes)
def _get_digests(self, target, sizes=False): import tuf.client.updater updater = tuf.client.updater.Updater('updater', self._repository_mirrors) tgt = updater.target(target) updater.download_target(tgt, self._copy_targets_dir) with open(path.join(self._copy_targets_dir, target), 'rb') as f: manifest = f.read().decode('utf-8') return self._dxf.get_alias(manifest=manifest, sizes=sizes)
def fetch(component): directory = 'updater-tmp' + "/" try: os.mkdir(directory) except: pass # likely the directory already exists targets = updater.targets_of_role('targets/' + component) updated_targets = updater.updated_targets(targets, directory) for target in updated_targets: updater.download_target(target, directory) updater.remove_obsolete_targets(directory)
def run(self): """ Check for updates """ if not self.mirrors: return while True: try: tuf.conf.repository_directory = os.path.join(self.bundle_path, 'repo') updater = tuf.client.updater.Updater('leap-updater', self.mirrors) updater.refresh() targets = updater.all_targets() updated_targets = updater.updated_targets(targets, self.source_path) if updated_targets: logger.info("There is updates needed. Start downloading " "updates.") for target in updated_targets: updater.download_target(target, self.dest_path) self._set_permissions(target) if os.path.isdir(self.dest_path): if os.path.isdir(self.update_path): shutil.rmtree(self.update_path) shutil.move(self.dest_path, self.update_path) filepath = sorted([f['filepath'] for f in updated_targets]) emit(catalog.UPDATER_NEW_UPDATES, ", ".join(filepath)) logger.info("Updates ready: %s" % (filepath,)) return except NotImplemented as e: logger.error("NotImplemented: %s" % (e,)) return except Exception as e: logger.error("An unexpected error has occurred while " "updating: %s" % (e,)) finally: time.sleep(self.delay)
def run(self): """ Check for updates periodically """ if not self.mirrors: return while True: try: tuf.conf.repository_directory = os.path.join( self.bundle_path, 'repo') updater = tuf.client.updater.Updater('leap-updater', self.mirrors) updater.refresh() targets = updater.all_targets() updated_targets = updater.updated_targets( targets, self.source_path) if updated_targets: print "There is updates needed. Start downloading updates." for target in updated_targets: updater.download_target(target, self.dest_path) self._set_permissions(target) if os.path.isdir(self.dest_path): if os.path.isdir(self.update_path): shutil.rmtree(self.update_path) shutil.move(self.dest_path, self.update_path) filepath = sorted([f['filepath'] for f in updated_targets]) signal(proto.UPDATER_NEW_UPDATES, content=", ".join(filepath)) print "Updates ready: ", filepath return except NotImplemented as e: print "NotImplemented: ", e return except Exception as e: print "ERROR:", e finally: time.sleep(self.delay)
def update_client(repository_mirror): """ <Purpose> Perform an update of the metadata and target files located at 'repository_mirror'. Target files are saved to the 'targets' directory in the current working directory. The current directory must already include a 'metadata' directory, which in turn must contain the 'current' and 'previous' directories. At a minimum, these two directories require the 'root.json' metadata file. <Arguments> repository_mirror: The URL to the repository mirror hosting the metadata and target files. E.g., 'http://localhost:8001' <Exceptions> tuf.RepositoryError, if 'repository_mirror' is improperly formatted. <Side Effects> Connects to a repository mirror and updates the metadata files and any target files. Obsolete targets are also removed locally. <Returns> None. """ # Does 'repository_mirror' have the correct format? try: tuf.formats.URL_SCHEMA.check_match(repository_mirror) except tuf.FormatError as e: message = 'The repository mirror supplied is invalid.' raise tuf.RepositoryError(message) # Set the local repository directory containing all of the metadata files. tuf.conf.repository_directory = '.' # Set the repository mirrors. This dictionary is needed by the Updater # class of updater.py. repository_mirrors = {'mirror': {'url_prefix': repository_mirror, 'metadata_path': 'metadata', 'targets_path': 'targets', 'confined_target_dirs': ['']}} # Create the repository object using the repository name 'repository' # and the repository mirrors defined above. updater = tuf.client.updater.Updater('repository', repository_mirrors) # The local destination directory to save the target files. destination_directory = './targets' # Refresh the repository's top-level roles, store the target information for # all the targets tracked, and determine which of these targets have been # updated. updater.refresh() all_targets = updater.all_targets() updated_targets = updater.updated_targets(all_targets, destination_directory) # Download each of these updated targets and save them locally. for target in updated_targets: try: updater.download_target(target, destination_directory) except tuf.DownloadError as e: pass # Remove any files from the destination directory that are no longer being # tracked. updater.remove_obsolete_targets(destination_directory)
updater = tuf.client.updater.Updater('tuf-example', repository_mirrors) # Set the local destination directory to save the target files. destination_directory = './targets' # Refresh the repository's top-level roles, store the target information for # all the targets tracked, and determine which of these targets have been # updated. updater.refresh() all_targets = updater.all_targets() updated_targets = updater.updated_targets(all_targets, destination_directory) # Download each of these updated targets and save them locally. for target in updated_targets: try: updater.download_target(target, destination_directory) except tuf.DownloadError, e: pass # Remove any files from the destination directory that are no longer being # tracked. updater.remove_obsolete_targets(destination_directory) """ # Example demonstrating an update that only downloads the targets of # a specific role (i.e., 'targets/role1') updater.refresh() targets_of_role1 = updater.targets_of_role('targets/role1') updated_targets = updater.updated_targets(targets_of_role1, destination_directory)
def update_client(parsed_arguments): """ <Purpose> Perform an update of the metadata and target files located at 'repository_mirror'. Target files are saved to the 'targets' directory in the current working directory. The current directory must already include a 'metadata' directory, which in turn must contain the 'current' and 'previous' directories. At a minimum, these two directories require the 'root.json' metadata file. <Arguments> parsed_arguments: An argparse Namespace object, containing the parsed arguments. <Exceptions> tuf.exceptions.Error, if 'parsed_arguments' is not a Namespace object. <Side Effects> Connects to a repository mirror and updates the local metadata files and any target files. Obsolete, local targets are also removed. <Returns> None. """ if not isinstance(parsed_arguments, argparse.Namespace): raise tuf.exceptions.Error('Invalid namespace object.') else: logger.debug('We have a valid argparse Namespace object.') # Set the local repositories directory containing all of the metadata files. tuf.settings.repositories_directory = '.' # Set the repository mirrors. This dictionary is needed by the Updater # class of updater.py. repository_mirrors = { 'mirror': { 'url_prefix': parsed_arguments.repo, 'metadata_path': 'metadata', 'targets_path': 'targets' } } # Create the repository object using the repository name 'repository' # and the repository mirrors defined above. updater = tuf.client.updater.Updater('tufrepo', repository_mirrors) # The local destination directory to save the target files. destination_directory = './tuftargets' # Refresh the repository's top-level roles... updater.refresh(unsafely_update_root_if_necessary=False) # ... and store the target information for the target file specified on the # command line, and determine which of these targets have been updated. target_fileinfo = [] for target in parsed_arguments.targets: target_fileinfo.append(updater.get_one_valid_targetinfo(target)) updated_targets = updater.updated_targets(target_fileinfo, destination_directory) # Retrieve each of these updated targets and save them to the destination # directory. for target in updated_targets: try: updater.download_target(target, destination_directory) except tuf.exceptions.DownloadError: pass # Remove any files from the destination directory that are no longer being # tracked. updater.remove_obsolete_targets(destination_directory)
# The local destination directory to save the target files. destination_directory = './targets' # Refresh the repository's top-level roles, store the target information for # all the targets tracked, and determine which of these targets have been # updated. updater.refresh() all_targets = updater.all_targets() updated_targets = updater.updated_targets(all_targets, destination_directory) # Download each of these updated targets and save them locally. for target in updated_targets: try: updater.download_target(target, destination_directory) except tuf.DownloadError, e: pass # Remove any files from the destination directory that are no longer being # tracked. updater.remove_obsolete_targets(destination_directory) def parse_options(): """ <Purpose> Parse the command-line options and set the logging level as specified by the user through the --verbose option. 'basic_client' expects the '--repo' to be set by the user.
def update_client(repository_mirror): """ <Purpose> Perform an update of the metadata and target files located at 'repository_mirror'. Target files are saved to the 'targets' directory in the current working directory. The current directory must already include a 'metadata' directory, which in turn must contain the 'current' and 'previous' directories. At a minimum, these two directories require the 'root.json' metadata file. <Arguments> repository_mirror: The URL to the repository mirror hosting the metadata and target files. E.g., 'http://localhost:8001' <Exceptions> tuf.RepositoryError, if 'repository_mirror' is improperly formatted. <Side Effects> Connects to a repository mirror and updates the metadata files and any target files. Obsolete targets are also removed locally. <Returns> None. """ # Does 'repository_mirror' have the correct format? try: tuf.formats.URL_SCHEMA.check_match(repository_mirror) except tuf.FormatError as e: message = 'The repository mirror supplied is invalid.' raise tuf.RepositoryError(message) # Set the local repository directory containing all of the metadata files. tuf.conf.repository_directory = '.' # Set the repository mirrors. This dictionary is needed by the Updater # class of updater.py. repository_mirrors = { 'mirror': { 'url_prefix': repository_mirror, 'metadata_path': 'repository', 'targets_path': 'repository/targets', 'confined_target_dirs': [''] } } # Create the repository object using the repository name 'repository' # and the repository mirrors defined above. updater = tuf.client.updater.Updater('repository', repository_mirrors) # The local destination directory to save the target files. destination_directory = './targets' # Refresh the repository's top-level roles, store the target information for # all the targets tracked, and determine which of these targets have been # updated. updater.refresh() all_targets = updater.all_targets() updated_targets = updater.updated_targets(all_targets, destination_directory) # Download each of these updated targets and save them locally. for target in updated_targets: try: updater.download_target(target, destination_directory) except tuf.DownloadError as e: pass # Remove any files from the destination directory that are no longer being # tracked. updater.remove_obsolete_targets(destination_directory)