Example #1
0
def _grant(user: str,
           acl: str,
           description: str,
           action: str = "create") -> None:
    log.info(
        'Granting permission to {user} for {acl}/{action} ({description})'.
        format(user=user, acl=acl, action=action, description=description))

    # Create the ACL
    r = sdk_cmd.cluster_request('PUT',
                                '/acs/api/v1/acls/{acl}'.format(acl=acl),
                                raise_on_error=False,
                                json={'description': description})
    # 201=created, 409=already exists
    assert r.status_code == 201 or r.status_code == 409, '{} failed {}: {}'.format(
        create_endpoint, r.status_code, r.text)

    # Assign the user to the ACL
    r = sdk_cmd.cluster_request(
        'PUT',
        '/acs/api/v1/acls/{acl}/users/{user}/{action}'.format(acl=acl,
                                                              user=user,
                                                              action=action),
        raise_on_error=False)
    # 204=success, 409=already exists
    assert r.status_code == 204 or r.status_code == 409, '{} failed {}: {}'.format(
        create_endpoint, r.status_code, r.text)
Example #2
0
def dcos_ca_bundle():
    """
    Retrieve DC/OS CA bundle and returns the content.
    """
    resp = sdk_cmd.cluster_request('GET', '/ca/dcos-ca.crt')
    cert = resp.content.decode('ascii')
    assert cert is not None
    return cert
Example #3
0
def update_app(app_name,
               config,
               timeout=TIMEOUT_SECONDS,
               wait_for_completed_deployment=True):
    if "env" in config:
        log.info("Environment for marathon app {} ({} values):".format(
            app_name, len(config["env"])))
        for k in sorted(config["env"]):
            log.info("  {}={}".format(k, config["env"][k]))
    # throws on failure:
    sdk_cmd.cluster_request('PUT',
                            _api_url('apps/{}'.format(app_name)),
                            log_args=False,
                            json=config)

    if wait_for_completed_deployment:
        log.info("Waiting for Marathon deployment of {} to complete...".format(
            app_name))
        shakedown.deployment_wait(app_id=app_name, timeout=timeout)
Example #4
0
def dump_mesos_state(item: pytest.Item):
    for name in ['state.json', 'slaves']:
        r = sdk_cmd.cluster_request('GET',
                                    '/mesos/{}'.format(name),
                                    verify=False,
                                    raise_on_error=False)
        if r.ok:
            if name.endswith('.json'):
                name = name[:-len('.json')]  # avoid duplicate '.json'
            with open(setup_artifact_path(item, 'mesos_{}.json'.format(name)),
                      'w') as f:
                f.write(r.text)
Example #5
0
def _get_master_public_ip() -> str:
    """
    :return (str): The public IP of the master node in the DC/OS cluster.
    """
    response = sdk_cmd.cluster_request("GET", "/metadata", verify=False).json()
    if "PUBLIC_IPV4" not in response:
        raise KeyError("Cluster metadata does not include master's public ip: {response}".format(
            response=response))

    public_ip = response["PUBLIC_IPV4"]
    log.info("Master public ip is {public_ip}".format(public_ip=public_ip))
    return public_ip
Example #6
0
def _copy_file_to_localhost(host_id: str, keytab_absolute_path: str, output_filename: str):
    """
    Copies the keytab that was generated inside the container running the KDC server to the localhost
    so it can be uploaded to the secret store later.
    """
    log.info("Downloading keytab to %s", output_filename)

    keytab_response = sdk_cmd.cluster_request(
        'GET', "/slave/{}/files/download".format(host_id), params={"path": keytab_absolute_path})
    with open(output_filename, 'wb') as fd:
        for chunk in keytab_response.iter_content(chunk_size=128):
            fd.write(chunk)

    log.info("Downloaded %d bytes to %s", os.stat(output_filename).st_size, output_filename)
Example #7
0
def _get_config_once(app_name):
    return sdk_cmd.cluster_request('GET',
                                   _api_url('apps/{}'.format(app_name)),
                                   retry=False)
Example #8
0
def restart_app(app_name):
    log.info("Restarting {}...".format(app_name))
    # throws on failure:
    sdk_cmd.cluster_request('POST',
                            _api_url('apps/{}/restart'.format(app_name)))
    log.info("Restarted {}.".format(app_name))
Example #9
0
def get_metrics(package_name, service_name, task_name):
    """Return a list of DC/OS metrics datapoints.

    Keyword arguments:
    package_name -- the name of the package the service is using
    service_name -- the name of the service to get metrics for
    task_name -- the name of the task whose agent to run metrics commands from
    """
    tasks = shakedown.get_service_tasks(service_name)
    for task in tasks:
        if task['name'] == task_name:
            task_to_check = task

    if task_to_check is None:
        raise Exception("Could not find task")

    agent_id = task_to_check['slave_id']
    executor_id = task_to_check['executor_id']

    pod_name = '-'.join(task_name.split("-")[:2])
    pod_info = sdk_cmd.svc_cli(package_name,
                               service_name,
                               "pod info {}".format(pod_name),
                               json=True)
    task_info = None
    for task in pod_info:
        if task["info"]["name"] == task_name:
            task_info = task
            break

    print("get_metrics here")
    if not task_info:
        return []

    task_container_id = task_info["status"]["containerStatus"]["containerId"][
        "value"]

    # Not related to functionality but consuming this
    # endpoint to verify downstream integrity
    containers_response = sdk_cmd.cluster_request(
        "GET",
        "/system/v1/agent/{}/metrics/v0/containers".format(agent_id),
        retry=False)
    reported_container_ids = json.loads(containers_response.text)

    print("reported_container_ids = " + str(reported_container_ids))

    container_id_reported = False
    for container_id in reported_container_ids:
        if container_id == task_container_id:
            container_id_reported = True

    if not container_id_reported:
        raise ValueError(
            "The metrics /container endpoint returned {}, expecting {} to be returned as well"
            .format(reported_container_ids, task_container_id))

    app_response = sdk_cmd.cluster_request(
        "GET",
        "/system/v1/agent/{}/metrics/v0/containers/{}/app".format(
            agent_id, task_container_id),
        retry=False)
    app_json = json.loads(app_response.text)
    print("app_json = " + str(app_json))

    if app_json['dimensions']['executor_id'] == executor_id:
        print(" app_json['datapoints'] = " + str(app_json['datapoints']))
        return app_json['datapoints']

    raise Exception("No metrics found")