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
Esempio n. 2
0
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)
Esempio n. 3
0
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)
Esempio n. 4
0
# 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)

for target in updated_targets:
  updater.download_target(target, destination_directory)
"""

def remove_obsolete():
    tuf.conf.repository_directory = REPO_DIR
    updater = tuf.client.updater.Updater('leap-updater', {})
    updater.remove_obsolete_targets(".")
Esempio n. 6
0
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)
    # 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.

    Example:
      $ python basic_client.py --repo http://localhost:8001

    If the required option is unset, a parser error is printed
    and the scripts exits.
Esempio n. 8
0
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)
Esempio n. 9
0
def remove_obsolete():
    tuf.conf.repository_directory = REPO_DIR
    updater = tuf.client.updater.Updater('leap-updater', {})
    updater.remove_obsolete_targets(".")