Esempio n. 1
0
def _restore_collection(in_zip, mission, file_type):
    collection, created = models.Collection.objects.get_or_create(
        mission=mission, file_type=file_type
    )

    location_descs = json.loads(in_zip.read(
        "config/collections/%s/locations.json" % collection)
    )
    for location_desc in location_descs:
        models.Location.objects.get_or_create(
            collection=collection, **location_desc
        )

    zip_config_path = "config/collections/%s/collection.conf" % collection
    errors = check_collection_configuration(
        CollectionConfigurationReader.from_fileobject(
            in_zip.open(zip_config_path)
        )
    )
    if not errors:
        backup_config(collection.config_path)
        _restore_file(in_zip, zip_config_path, collection.config_path)
        logger.info("Restored configuration for collection %s" % collection)
    else:
        logger.warn(
            "Could not restore collection configuration due to errors:\n%s"
            % ("\n".join(errors))
        )
Esempio n. 2
0
def restore(in_path):
    """ Restore a previously backupped ZIP.
    """
    with closing(ZipFile(in_path)) as in_zip:
        manifest = json.loads(in_zip.read("manifest.json"))
        names = in_zip.namelist()

        if manifest["logs"]:
            # restore logs
            for name in names:
                extract = False
                # extract the file if it does not exist in the filesystem, or the
                # filesystem version is older
                if name.startswith("logs/"):
                    if exists(join("/var/log/minv", basename(name))):
                        ts = timestamp(datetime(*in_zip.getinfo(name).date_time))
                        if ts > getmtime(join("/var/log/minv", basename(name))):
                            extract = True
                    else:
                        extract = True

                if extract:
                    _restore_file(
                        in_zip, name, join("/var/log/minv", basename(name))
                    )

        if manifest["config"]:
            # restore the global configuration if it is stored in the backup
            if "config/minv.conf" in names:
                errors = check_global_configuration(
                    GlobalReader.from_fileobject(in_zip.open("config/minv.conf"))
                )
                if not errors:
                    backup_config("/etc/minv/minv.conf")
                    _restore_file(
                        in_zip, "config/minv.conf", "/etc/minv/minv.conf"
                    )
                    logger.info("Restored global configuration.")
                else:
                    logger.warn(
                        "Could not restore global configuration due to errors:"
                        "\n%s" % ("\n".join(errors))
                    )

            # restore all the collections included
            for name in names:
                if name.startswith("config/collections/") \
                        and name.endswith("collection.conf"):
                    try:
                        mission, file_type = name.split("/")[2:4]
                    except:
                        logger.error(
                            "Invalid collection configuration path '%s'" % name
                        )
                    else:
                        _restore_collection(in_zip, mission, file_type)
Esempio n. 3
0
def restore(in_path):
    """ Restore a previously backupped ZIP.
    """
    with closing(ZipFile(in_path)) as in_zip:
        manifest = json.loads(in_zip.read("manifest.json"))
        names = in_zip.namelist()

        if manifest["logs"]:
            # restore logs
            for name in names:
                extract = False
                # extract the file if it does not exist in the filesystem, or the
                # filesystem version is older
                if name.startswith("logs/"):
                    if exists(join("/var/log/minv", basename(name))):
                        ts = timestamp(
                            datetime(*in_zip.getinfo(name).date_time))
                        if ts > getmtime(join("/var/log/minv",
                                              basename(name))):
                            extract = True
                    else:
                        extract = True

                if extract:
                    _restore_file(in_zip, name,
                                  join("/var/log/minv", basename(name)))

        if manifest["config"]:
            # restore the global configuration if it is stored in the backup
            if "config/minv.conf" in names:
                errors = check_global_configuration(
                    GlobalReader.from_fileobject(
                        in_zip.open("config/minv.conf")))
                if not errors:
                    backup_config("/etc/minv/minv.conf")
                    _restore_file(in_zip, "config/minv.conf",
                                  "/etc/minv/minv.conf")
                    logger.info("Restored global configuration.")
                else:
                    logger.warn(
                        "Could not restore global configuration due to errors:"
                        "\n%s" % ("\n".join(errors)))

            # restore all the collections included
            for name in names:
                if name.startswith("config/collections/") \
                        and name.endswith("collection.conf"):
                    try:
                        mission, file_type = name.split("/")[2:4]
                    except:
                        logger.error(
                            "Invalid collection configuration path '%s'" %
                            name)
                    else:
                        _restore_collection(in_zip, mission, file_type)
Esempio n. 4
0
def _restore_collection(in_zip, mission, file_type):
    collection, created = models.Collection.objects.get_or_create(
        mission=mission, file_type=file_type)

    location_descs = json.loads(
        in_zip.read("config/collections/%s/locations.json" % collection))
    for location_desc in location_descs:
        models.Location.objects.get_or_create(collection=collection,
                                              **location_desc)

    zip_config_path = "config/collections/%s/collection.conf" % collection
    errors = check_collection_configuration(
        CollectionConfigurationReader.from_fileobject(
            in_zip.open(zip_config_path)))
    if not errors:
        backup_config(collection.config_path)
        _restore_file(in_zip, zip_config_path, collection.config_path)
        logger.info("Restored configuration for collection %s" % collection)
    else:
        logger.warn(
            "Could not restore collection configuration due to errors:\n%s" %
            ("\n".join(errors)))
Esempio n. 5
0
def configuration_view(request, mission, file_type):
    """ View function to provide a change form for the collections configuration.
    For the metadata mapping a seperate formset is used.
    """
    collection = models.Collection.objects.get(mission=mission,
                                               file_type=file_type)

    config = collection.configuration

    if request.method == "POST":
        configuration_form = forms.CollectionConfigurationForm(request.POST)
        mapping_formsets = _generate_mapping_formsets(collection,
                                                      POST=request.POST)

        mapping_formsets_valid = all(
            [f[1].is_valid() for f in mapping_formsets])

        if configuration_form.is_valid() and mapping_formsets_valid:
            for key, value in configuration_form.cleaned_data.items():
                setattr(config, key, value)

            for location, mapping_formset in mapping_formsets:
                mapping = SortedDict()
                for form in mapping_formset:
                    if form.is_valid():
                        data = form.cleaned_data
                        if data.get(
                                "DELETE") or not data.get("index_file_key"):
                            continue
                        mapping[data["search_key"]] = data["index_file_key"]

                if location:
                    config.set_section_dict(
                        "metadata_mapping.%s" % location.url, mapping)
                else:
                    config.default_metadata_mapping = mapping

            errors = check_collection_configuration(config)

            if errors:
                for error in errors:
                    messages.error(request, error)
            else:
                backup_config(config._config_path)
                config.write()
                messages.info(
                    request,
                    "Saved configuration for collection %s." % collection)

            # re-read the forms here with the updated configuration
            config.read(reset=True)

            configuration_form = forms.CollectionConfigurationForm(
                initial={
                    "export_interval":
                    timedelta_to_duration(config.export_interval),
                    "harvest_interval":
                    timedelta_to_duration(config.harvest_interval),
                    "available_result_list_fields":
                    config.available_result_list_fields,
                    "available_alignment_fields":
                    config.available_alignment_fields
                })
            mapping_formsets = _generate_mapping_formsets(collection,
                                                          config=config)

    else:
        configuration_form = forms.CollectionConfigurationForm(
            initial={
                "export_interval": timedelta_to_duration(
                    config.export_interval),
                "harvest_interval": timedelta_to_duration(
                    config.harvest_interval),
                "available_result_list_fields":
                config.available_result_list_fields,
                "available_alignment_fields": config.available_alignment_fields
            })
        mapping_formsets = _generate_mapping_formsets(collection,
                                                      config=config)

    return render(
        request, "inventory/collection/configuration.html", {
            "collections": models.Collection.objects.all(),
            "collection": collection,
            "configuration_form": configuration_form,
            "mapping_formsets": mapping_formsets
        })
Esempio n. 6
0
def configuration_view(request, mission, file_type):
    """ View function to provide a change form for the collections configuration.
    For the metadata mapping a seperate formset is used.
    """
    collection = models.Collection.objects.get(
        mission=mission, file_type=file_type
    )

    config = collection.configuration

    if request.method == "POST":
        configuration_form = forms.CollectionConfigurationForm(request.POST)
        mapping_formsets = _generate_mapping_formsets(
            collection, POST=request.POST
        )

        mapping_formsets_valid = all([
            f[1].is_valid() for f in mapping_formsets
        ])

        if configuration_form.is_valid() and mapping_formsets_valid:
            for key, value in configuration_form.cleaned_data.items():
                setattr(config, key, value)

            for location, mapping_formset in mapping_formsets:
                mapping = SortedDict()
                for form in mapping_formset:
                    if form.is_valid():
                        data = form.cleaned_data
                        if data.get("DELETE") or not data.get("index_file_key"):
                            continue
                        mapping[data["search_key"]] = data["index_file_key"]

                if location:
                    config.set_section_dict(
                        "metadata_mapping.%s" % location.url, mapping
                    )
                else:
                    config.default_metadata_mapping = mapping

            errors = check_collection_configuration(config)

            if errors:
                for error in errors:
                    messages.error(request, error)
            else:
                backup_config(config._config_path)
                config.write()
                messages.info(request,
                    "Saved configuration for collection %s." % collection
                )

            # re-read the forms here with the updated configuration
            config.read(reset=True)

            configuration_form = forms.CollectionConfigurationForm(
                initial={
                    "export_interval": timedelta_to_duration(
                        config.export_interval
                    ),
                    "harvest_interval": timedelta_to_duration(
                        config.harvest_interval
                    ),
                    "available_result_list_fields":
                        config.available_result_list_fields,
                    "available_alignment_fields":
                        config.available_alignment_fields
                }
            )
            mapping_formsets = _generate_mapping_formsets(
                collection, config=config
            )

    else:
        configuration_form = forms.CollectionConfigurationForm(
            initial={
                "export_interval": timedelta_to_duration(
                    config.export_interval
                ),
                "harvest_interval": timedelta_to_duration(
                    config.harvest_interval
                ),
                "available_result_list_fields":
                    config.available_result_list_fields,
                "available_alignment_fields":
                    config.available_alignment_fields
            }
        )
        mapping_formsets = _generate_mapping_formsets(
            collection, config=config
        )

    return render(
        request, "inventory/collection/configuration.html", {
            "collections": models.Collection.objects.all(),
            "collection": collection, "configuration_form": configuration_form,
            "mapping_formsets": mapping_formsets
        }
    )
Esempio n. 7
0
 def _backup_config(self, path):
     backup_path = backup_config(path)
     self.info("Backed up old configuration at '%s'" % backup_path)