Ejemplo n.º 1
0
def service_unavailable_predicate(service_name):
    url = dcos_service_url(service_name)
    try:
        http.get(url)
    except DCOSHTTPException as e:
        if e.response.status_code == 500:
            return True
    else:
        return False
Ejemplo n.º 2
0
def get_resource(resource):
    """:param resource: optional filename or http(s) url for the application or group resource
       :type resource: str
       :returns: resource
       :rtype: dict
    """

    if resource is None:
        return None

    if os.path.isfile(resource):
        with util.open_file(resource) as resource_file:
            return util.load_json(resource_file)
    else:
        try:
            http.silence_requests_warnings()
            req = http.get(resource)
            if req.status_code == 200:
                data = b''
                for chunk in req.iter_content(1024):
                    data += chunk
                return util.load_jsons(data.decode('utf-8'))
            else:
                raise Exception
        except Exception:
            raise DCOSException(
                "Can't read from resource: {0}. Please check that it exists.".
                format(resource))
Ejemplo n.º 3
0
def test_ui_available(marathon_service_name):
    """Simply verifies that a request to the UI endpoint is successful if Marathon is launched."""

    response = http.get("{}/ui/".format(
        shakedown.dcos_service_url(marathon_service_name)))
    assert response.status_code == 200, "HTTP status code is {}, but 200 was expected".format(
        response.status_code)
Ejemplo n.º 4
0
def mesos_available_predicate():
    url = master_url()
    try:
        response = http.get(url)
        return response.status_code == 200
    except Exception as e:
        return False
Ejemplo n.º 5
0
def service_available_predicate(service_name):
    url = dcos_service_url(service_name)
    try:
        response = http.get(url)
        return response.status_code == 200
    except Exception as e:
        return False
Ejemplo n.º 6
0
def dcos_agents_state():
    response = http.get(agents_url())

    if response.status_code == 200:
        return response.json()
    else:
        return None
Ejemplo n.º 7
0
    def browse(self, slave, path):
        """ GET /files/browse.json

        Request
        path:...  # path to run ls on

        Response
        [
          {
            path:  # full path to file
            nlink:
            size:
            mtime:
            mode:
            uid:
            gid:
          }
        ]

        :param slave: slave to issue the request on
        :type slave: Slave
        :returns: /files/browse.json response
        :rtype: dict
        """

        url = self.slave_url(slave['id'],
                             slave.http_url(),
                             'files/browse.json')
        return http.get(url, params={'path': path}).json()
Ejemplo n.º 8
0
def http_get_marathon_path(name, marathon_name='marathon'):
    """Invokes HTTP GET for marathon url with name.
       For example, name='ping': http GET {dcos_url}/service/marathon/ping
    """
    url = get_marathon_endpoint(name, marathon_name)
    headers = {'Accept': '*/*'}
    return http.get(url, headers=headers)
Ejemplo n.º 9
0
    def slave_file_read(self, slave_id, private_url, path, offset, length):
        """See the master_file_read() docs

        :param slave_id: slave ID
        :type slave_id: str
        :param path: absolute path to read
        :type path: str
        :param private_url: The slave's private URL derived from its
                            pid.  Used when we're accessing mesos
                            directly, rather than through DC/OS.
        :type private_url: str
        :param offset: start byte location, or -1.  -1 means read no data, and
                       is used to fetch the size of the file in the response's
                       'offset' parameter.
        :type offset: int
        :param length: number of bytes to read, or -1.  -1 means read the whole
                       file
        :type length: int
        :returns: files/read.json response
        :rtype: dict

        """

        url = self.slave_url(slave_id,
                             private_url,
                             'files/read.json')
        params = {'path': path,
                  'length': length,
                  'offset': offset}
        return http.get(url, params=params, timeout=self._timeout).json()
Ejemplo n.º 10
0
    def metadata(self):
        """ GET /metadata

        :returns: /metadata content
        :rtype: dict
        """
        url = self.get_dcos_url('metadata')
        return http.get(url, timeout=self._timeout).json()
Ejemplo n.º 11
0
    def get_state_summary(self):
        """Get the Mesos master state summary json object

        :returns: Mesos' master state summary json object
        :rtype: dict
        """

        url = self.master_url('master/state-summary')
        return http.get(url, timeout=self._timeout).json()
Ejemplo n.º 12
0
    def get_master_state(self):
        """Get the Mesos master state json object

        :returns: Mesos' master state json object
        :rtype: dict
        """

        url = self.master_url('master/state.json')
        return http.get(url, timeout=self._timeout).json()
Ejemplo n.º 13
0
    def hosts(self, host):
        """ GET v1/hosts/<host>

        :param host: host
        :type host: str
        :returns: {'ip', 'host'} dictionary
        :rtype: dict(str, str)
        """
        url = self._path('v1/hosts/{}'.format(host))
        return http.get(url, headers={}).json()
def ensure_permissions():
    common.set_service_account_permissions(MOM_EE_SERVICE_ACCOUNT)

    url = urljoin(
        shakedown.dcos_url(), 'acs/api/v1/acls/dcos:superuser/users/{}'.format(
            MOM_EE_SERVICE_ACCOUNT))
    req = http.get(url)
    expected = '/acs/api/v1/acls/dcos:superuser/users/{}/full'.format(
        MOM_EE_SERVICE_ACCOUNT)
    assert req.json()['array'][0][
        'url'] == expected, "Service account permissions couldn't be set"
Ejemplo n.º 15
0
    def fetch(self, path, **kwargs):
        """GET the resource located at `path`

        :param path: the URL path
        :type path: str
        :param **kwargs: http.get kwargs
        :type **kwargs: dict
        :returns: the response object
        :rtype: Response
        """

        url = urllib.parse.urljoin(self._base_url(), path)
        return http.get(url, **kwargs)
Ejemplo n.º 16
0
def _download_and_store(url, location):
    """Download given url and store in location on disk

    :param url: url to download
    :type url: str
    :param location: path to file to store url
    :type location: str
    :rtype: None
    """

    with open(location, 'wb') as f:
        r = http.get(url, stream=True)
        for chunk in r.iter_content(1024):
            f.write(chunk)
Ejemplo n.º 17
0
def test_metrics_endpoint(marathon_service_name):
    service_url = dcos_service_url(marathon_service_name)
    response = http.get("{}metrics".format(service_url))
    assert response.status_code == 200, "HTTP status code {} is NOT 200".format(response.status_code)

    if marathon_version_less_than('1.7'):
        metric_name = 'service.mesosphere.marathon.app.count'
    else:
        metric_name = 'marathon.apps.active.gauge'

    response_json = response.json()
    logger.info('Found metric gauges: '.format(response_json['gauges']))
    assert response_json['gauges'][metric_name] is not None, \
        "{} is absent".format(metric_name)
Ejemplo n.º 18
0
def get_group(id):
    """ Returns a group from the DCOS Enterprise.  It returns None if none exists.

        :param id: group id
        :type id: str
        :return: Group
        :rtype: dict
    """
    acl_url = urljoin(_acl_url(), 'groups/{}'.format(id))
    try:
        r = http.get(acl_url)
        return r.json()
    except DCOSHTTPException as e:
        if e.response.status_code != 400:
            raise
Ejemplo n.º 19
0
    def get_slave_state(self, slave_id, private_url):
        """Get the Mesos slave state json object

        :param slave_id: slave ID
        :type slave_id: str
        :param private_url: The slave's private URL derived from its
                            pid.  Used when we're accessing mesos
                            directly, rather than through DC/OS.
        :type private_url: str
        :returns: Mesos' master state json object
        :rtype: dict

        """

        url = self.slave_url(slave_id, private_url, 'state.json')
        return http.get(url, timeout=self._timeout).json()
Ejemplo n.º 20
0
def get_user(uid):
    """ Returns a user from the DCOS Enterprise.  It returns None if none exists.

        :param uid: user id
        :type uid: str
        :return: User
        :rtype: dict
    """
    try:
        acl_url = urljoin(_acl_url(), 'users/{}'.format(uid))
        r = http.get(acl_url)
        return r.json()
        # assert r.status_code == 201
    except DCOSHTTPException as e:
        if e.response.status_code == 400:
            return None
        else:
            raise
Ejemplo n.º 21
0
    def master_file_read(self, path, length, offset):
        """This endpoint isn't well documented anywhere, so here is the spec
        derived from the mesos source code:

        request format:
        {
            path: absolute path to read
            offset: start byte location, or -1.  -1 means read no data, and
                    is used to fetch the size of the file in the response's
                    'offset' parameter.
            length: number of bytes to read, or -1.  -1 means read the whole
                    file.
        }

        response format:
        {
            data: file data.  Empty if a request.offset=-1.  Could be
                  smaller than request.length if EOF was reached, or if (I
                  believe) request.length is larger than the length
                  supported by the server (16 pages I believe).

            offset: the offset value from the request, or the size of the
                    file if the request offset was -1 or >= the file size.
        }

        :param path: absolute path to read
        :type path: str
        :param offset: start byte location, or -1.  -1 means read no data, and
                       is used to fetch the size of the file in the response's
                       'offset' parameter.
        :type offset: int
        :param length: number of bytes to read, or -1.  -1 means read the whole
                       file
        :type length: int
        :returns: files/read.json response
        :rtype: dict
        """

        url = self.master_url('files/read.json')
        params = {'path': path,
                  'length': length,
                  'offset': offset}
        return http.get(url, params=params, timeout=self._timeout).json()
Ejemplo n.º 22
0
def get_providers():
    """
    Returns dict of providers configured on cluster

    :returns: configured providers
    :rtype: {}
    """

    dcos_url = config.get_config_val("core.dcos_url").rstrip('/')
    endpoint = '/acs/api/v1/auth/providers'
    url = urllib.parse.urljoin(dcos_url, endpoint)
    try:
        providers = http.get(url)
        return providers.json()
    # this endpoint should only have authentication in DC/OS 1.8
    except DCOSAuthenticationException:
        msg = "This command is not supported for your cluster"
        raise DCOSException(msg)
    except DCOSHTTPException as e:
        if e.response.status_code == 404:
            msg = "This command is not supported for your cluster"
            raise DCOSException(msg)

    return {}
Ejemplo n.º 23
0
 def cluster_available_predicate(url):
     try:
         response = http.get(url, verify=False)
         return response.status_code == 200
     except Exception as e:
         return False
Ejemplo n.º 24
0
def wait_until_fail(endpoint):
    try:
        http.get(endpoint)
        return True
    except DCOSHTTPException:
        return False
Ejemplo n.º 25
0
def get_pod_status(pod_id):
    url = urljoin(DCOS_SERVICE_URL, get_pod_status_url(pod_id))
    return http.get(url).json()
Ejemplo n.º 26
0
def http_get_marathon_path(name, marathon_name='marathon'):
    """ Invokes HTTP GET for marathon url with name
        ex.  name='ping'  http GET {dcos_url}/service/marathon/ping
    """
    url = get_marathon_endpoint(name, marathon_name)
    return http.get(url)
Ejemplo n.º 27
0
def get_pod_version(pod_id, version_id):
    url = urljoin(DCOS_SERVICE_URL, get_pod_versions_url(pod_id, version_id))
    return http.get(url).json()
Ejemplo n.º 28
0
def test_non_authorized_user():
    with shakedown.new_dcos_user('kenny', 'kenny'):
        with pytest.raises(errors.DCOSAuthorizationException) as exec_info:
            http.get(urljoin(shakedown.dcos_url(), 'service/marathon/v2/apps'))
            error = exec_info.value
            assert str(error) == "You are not authorized to perform this operation"
Ejemplo n.º 29
0
def http_get_marathon_path(name, marathon_name='marathon'):
    """ Invokes HTTP GET for marathon url with name
        ex.  name='ping'  http GET {dcos_url}/service/marathon/ping
    """
    url = get_marathon_endpoint(name, marathon_name)
    return http.get(url)
Ejemplo n.º 30
0
def test_non_authenticated_user():
    with shakedown.no_user():
        with pytest.raises(errors.DCOSAuthenticationException) as exec_info:
            http.get(urljoin(shakedown.dcos_url(), 'service/marathon/v2/apps'))
            error = exec_info.value
            assert str(error) == "Authentication failed. Please run `dcos auth login`"