Esempio n. 1
0
def _legacy_push_user_profile_to_site(site, user_id, profile):
    url = site["multisiteurl"] + "automation.py?" + urlencode_vars([
        ("command", "push-profile"),
        ("secret", site["secret"]),
        ("siteid", site['id']),
        ("debug", config.debug and "1" or ""),
    ])

    response = get_url(url,
                       site.get('insecure', False),
                       data={
                           'user_id': user_id,
                           'profile': mk_repr(profile),
                       },
                       timeout=60)

    if not response:
        raise MKAutomationException(_("Empty output from remote site."))

    try:
        response = mk_eval(response)
    except Exception:
        # The remote site will send non-Python data in case of an error.
        raise MKAutomationException("%s: <pre>%s</pre>" %
                                    (_("Got invalid data"), response))
    return response
Esempio n. 2
0
def _get_check_table_from_remote(api_request):
    """Gathers the check table from a remote site

    Cares about pre 1.6 sites that does not support the new service-discovery-job API call.
    Falling back to the previously existing try-inventry and inventory automation calls.
    """
    try:
        sync_changes_before_remote_automation(api_request.host.site_id())

        return _deserialize_remote_result(
            watolib.do_remote_automation(
                get_site_config(api_request.host.site_id()),
                "service-discovery-job",
                [
                    ("host_name", api_request.host.name()),
                    ("options", json.dumps(api_request.options._asdict())),
                ],
            ))
    except watolib.MKAutomationException as e:
        if "Invalid automation command: service-discovery-job" not in "%s" % e:
            raise

        # Compatibility for pre 1.6 remote sites.
        if api_request.options.action == DiscoveryAction.TABULA_RASA:
            raise MKAutomationException(
                _("Tabula rasa not supported any more"))

        if api_request.options.action == DiscoveryAction.REFRESH:
            options = ["@scan"]
        else:
            options = ["@noscan"]

        if not api_request.options.ignore_errors:
            options.append("@raiseerrors")

        return DiscoveryResult(
            job_status={
                "is_active": False,
                "state": JobStatusStates.INITIALIZED,
            },
            check_table=try_discovery(
                api_request.host.site_id(),
                options,
                api_request.host.name(),
            ).check_table,
            check_table_created=int(time.time()),
            host_labels={},
            new_labels={},
            vanished_labels={},
            changed_labels={},
        )
Esempio n. 3
0
def _automation_failure(
    response: AutomationResponse,
    exception: SyntaxError,
) -> MKGeneralException:
    if response.local:
        return local_automation_failure(
            command=response.command,
            cmdline=response.cmdline,
            out=response.serialized_result,
            exc=exception,
        )
    return MKAutomationException("%s: <pre>%s</pre>" % (
        _("Got invalid data"),
        response.serialized_result,
    ))
Esempio n. 4
0
    def get_request(self):
        site_id = SiteId(request.get_ascii_input_mandatory("site_id"))
        activate_changes.verify_remote_site_config(site_id)

        try:
            serialized_domain_requests = ast.literal_eval(
                request.get_ascii_input_mandatory("domains"))
            if serialized_domain_requests and isinstance(
                    serialized_domain_requests[0], str):
                serialized_domain_requests = [
                    asdict(DomainRequest(x))
                    for x in serialized_domain_requests
                ]
        except SyntaxError:
            raise MKAutomationException(
                _("Invalid request: %r") %
                request.get_ascii_input_mandatory("domains"))

        return ActivateChangesRequest(site_id=site_id,
                                      domains=serialized_domain_requests)