Python base64.standard_b64encode() Examples

The following are 30 code examples of base64.standard_b64encode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module base64 , or try the search function .
Example #1
Source File: release_builder.py    From dcos-kafka-service with Apache License 2.0 6 votes vote down vote up
def _update_marathon_json(self, package_json):
        """Updates the marathon.json definition to contain the desired name and version strings.
        """
        # note: the file isn't valid JSON, so we edit the raw content instead
        marathon_encoded = package_json.get("marathon", {}).get("v2AppMustacheTemplate")
        orig_marathon_lines = base64.standard_b64decode(marathon_encoded).decode().split("\n")

        marathon_lines = []
        for line in orig_marathon_lines:
            name_match = re.match(r'^ *"PACKAGE_NAME": ?"(.*)",?$', line.rstrip("\n"))
            version_match = re.match(r'^ *"PACKAGE_VERSION": ?"(.*)",?$', line.rstrip("\n"))
            if name_match:
                line = line.replace(name_match.group(1), self._pkg_name)
            elif version_match:
                line = line.replace(version_match.group(1), self._pkg_version)
            marathon_lines.append(line)

        log.info("Updated marathon.json.mustache:")
        log.info("\n".join(difflib.unified_diff(orig_marathon_lines, marathon_lines, lineterm="")))

        # Update parent package object with changes:
        package_json["marathon"]["v2AppMustacheTemplate"] = base64.standard_b64encode(
            "\n".join(marathon_lines).encode("utf-8")
        ).decode() 
Example #2
Source File: pem.py    From aqua-monitor with GNU Lesser General Public License v3.0 6 votes vote down vote up
def save_pem(contents, pem_marker):
    """Saves a PEM file.

    :param contents: the contents to encode in PEM format
    :param pem_marker: the marker of the PEM content, such as 'RSA PRIVATE KEY'
        when your file has '-----BEGIN RSA PRIVATE KEY-----' and
        '-----END RSA PRIVATE KEY-----' markers.

    :return: the base64-encoded content between the start and end markers.

    """

    (pem_start, pem_end) = _markers(pem_marker)

    b64 = base64.standard_b64encode(contents).replace(b('\n'), b(''))
    pem_lines = [pem_start]

    for block_start in range(0, len(b64), 64):
        block = b64[block_start:block_start + 64]
        pem_lines.append(block)

    pem_lines.append(pem_end)
    pem_lines.append(b(''))

    return b('\n').join(pem_lines) 
Example #3
Source File: container_test.py    From django-cloud-deploy with Apache License 2.0 6 votes vote down vote up
def get(self, projectId, zone, clusterId):
        ca = base64.standard_b64encode(FAKE_CA).decode('utf-8')
        if 'invalid_response' in clusterId:
            return http_fake.HttpRequestFake(
                json.loads(CLUSTER_GET_RESPONSE_INVALID))
        if clusterId not in self.clusters_to_get_count:
            status = 'ERROR'
        else:
            self.clusters_to_get_count[clusterId][0] += 1
            get_count, total_get_count = self.clusters_to_get_count[clusterId]
            if get_count < total_get_count:
                status = 'PROVISIONING'
            else:
                status = 'RUNNING'
        response = CLUSTER_GET_RESPONSE_TEMPLATE.format(clusterId, ca, status)
        return http_fake.HttpRequestFake(json.loads(response)) 
Example #4
Source File: lightstep_binary_propagator.py    From lightstep-tracer-python with MIT License 6 votes vote down vote up
def inject(self, span_context, carrier):
        if type(carrier) is not bytearray:
            raise InvalidCarrierException()

        state = BinaryCarrier()
        basic_ctx = state.basic_ctx

        basic_ctx.trace_id = span_context.trace_id
        basic_ctx.span_id = span_context.span_id
        basic_ctx.sampled = span_context.sampled
        if span_context.baggage is not None:
            for key in span_context.baggage:
                basic_ctx.baggage_items[key] = span_context.baggage[key]


        serializedProto = state.SerializeToString()
        encoded = standard_b64encode(serializedProto)
        carrier.extend(encoded) 
Example #5
Source File: handlers.py    From jupyter-notebook-gist with Mozilla Public License 2.0 6 votes vote down vote up
def get(self):

        # Extract access code
        access_code = extract_code_from_args(self.request.arguments)

        # Request access token from github
        access_token = self.request_access_token(access_code)

        github_headers = {"Accept": "application/json",
                          "Authorization": "token " + access_token}

        response = request_session.get("https://api.github.com/gists",
                                       headers=github_headers)
        response_to_send = bytearray(response.text, 'utf-8')
        self.write("<script>var gists = '")
        self.write(base64.standard_b64encode(response_to_send))
        self.write("';")
        self.write("window.opener.postMessage(gists, window.opener.location)")
        self.finish(";</script>") 
Example #6
Source File: android_crypto.py    From plugin.video.netflix with MIT License 6 votes vote down vote up
def key_request_data(self):
        """Return a key request dict"""
        # No key update supported -> remove existing keys
        self.crypto_session.RemoveKeys()
        key_request = self.crypto_session.GetKeyRequest(  # pylint: disable=assignment-from-none
            bytearray([10, 122, 0, 108, 56, 43]), 'application/xml', True, dict())

        if not key_request:
            raise MSLError('Widevine CryptoSession getKeyRequest failed!')

        common.debug('Widevine CryptoSession getKeyRequest successful. Size: {}', len(key_request))

        # Save the key request (challenge data) required for manifest requests
        # Todo: to be implemented if/when it becomes mandatory
        key_request = base64.standard_b64encode(key_request).decode('utf-8')
        # g.LOCAL_DB.set_value('drm_session_challenge', key_request, TABLE_SESSION)

        return [{
            'scheme': 'WIDEVINE',
            'keydata': {
                'keyrequest': key_request
            }
        }] 
Example #7
Source File: pem.py    From bash-lambda-layer with MIT License 6 votes vote down vote up
def save_pem(contents, pem_marker):
    """Saves a PEM file.

    :param contents: the contents to encode in PEM format
    :param pem_marker: the marker of the PEM content, such as 'RSA PRIVATE KEY'
        when your file has '-----BEGIN RSA PRIVATE KEY-----' and
        '-----END RSA PRIVATE KEY-----' markers.

    :return: the base64-encoded content between the start and end markers.

    """

    (pem_start, pem_end) = _markers(pem_marker)

    b64 = base64.standard_b64encode(contents).replace(b('\n'), b(''))
    pem_lines = [pem_start]

    for block_start in range(0, len(b64), 64):
        block = b64[block_start:block_start + 64]
        pem_lines.append(block)

    pem_lines.append(pem_end)
    pem_lines.append(b(''))

    return b('\n').join(pem_lines) 
Example #8
Source File: bitfinex.py    From bitex with MIT License 6 votes vote down vote up
def sign(self, url, endpoint, endpoint_path, method_verb, *args, **kwargs):
        try:
            req = kwargs['params']
        except KeyError:
            req = {}
        if self.version == 'v1':
            req['request'] = endpoint_path
            req['nonce'] = self.nonce()

            js = json.dumps(req)
            data = base64.standard_b64encode(js.encode('utf8'))
        else:
            data = '/api/' + endpoint_path + self.nonce() + json.dumps(req)
        h = hmac.new(self.secret.encode('utf8'), data, hashlib.sha384)
        signature = h.hexdigest()
        headers = {"X-BFX-APIKEY": self.key,
                   "X-BFX-SIGNATURE": signature,
                   "X-BFX-PAYLOAD": data}
        if self.version == 'v2':
            headers['content-type'] = 'application/json'

        return url, {'headers': headers} 
Example #9
Source File: gemini.py    From bitex with MIT License 6 votes vote down vote up
def sign(self, uri, endpoint, endpoint_path, method_verb, *args, **kwargs):
        nonce = self.nonce()
        try:
            params = kwargs['params']
        except KeyError:
            params = {}
        payload = params
        payload['nonce'] = nonce
        payload['request'] = endpoint_path

        js = json.dumps(payload)
        data = base64.standard_b64encode(js.encode('utf8'))
        h = hmac.new(self.secret.encode('utf8'), data, hashlib.sha384)
        signature = h.hexdigest()
        headers = {'X-GEMINI-APIKEY': self.key,
                   'X-GEMINI-PAYLOAD': data,
                   'X-GEMINI-SIGNATURE': signature}
        return uri, {'headers': headers} 
Example #10
Source File: cluster.py    From MLOpsDatabricks with MIT License 6 votes vote down vote up
def get_state(self, databricks_token):
        base_url = self.get_base_url()

        response = requests.post(
            base_url + 'clusters/get',
            headers={
                'Authorization': b"Basic " + base64.standard_b64encode(
                    b"token:" + str.encode(databricks_token))},
            json={
                "cluster_id": self.id
            })

        if response.status_code == 200:
            self.state = response.json()['state']
        else:
            raise ClusterManagementException(
                "Error getting cluster state: %s: %s" %
                (response.json()["error_code"], response.json()["message"])
            ) 
Example #11
Source File: style_sheet_unittest.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def testImages(self):
    fs = fake_fs.FakeFS()
    fs.AddFile('/src/foo/x.css', """
.x .y {
    background-image: url(../images/bar.jpeg);
}
""")
    fs.AddFile('/src/images/bar.jpeg', 'hello world')
    with fs:
      project = project_module.Project([os.path.normpath('/src/')])
      loader = resource_loader.ResourceLoader(project)

      foo_x = loader.LoadStyleSheet('foo.x')
      self.assertEquals(1, len(foo_x.images))

      r0 = foo_x.images[0]
      self.assertEquals(os.path.normpath('/src/images/bar.jpeg'),
                        r0.absolute_path)

      inlined = foo_x.contents_with_inlined_images
      self.assertEquals("""
.x .y {
    background-image: url(data:image/jpeg;base64,%s);
}
""" % base64.standard_b64encode('hello world'), inlined) 
Example #12
Source File: message_conversion.py    From Cloudroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _from_inst(inst, rostype):
    # Special case for uint8[], we base64 encode the string
    if rostype in ros_binary_types:
        return standard_b64encode(inst)

    # Check for time or duration
    if rostype in ros_time_types:
        return {"secs": inst.secs, "nsecs": inst.nsecs}

    # Check for primitive types
    if rostype in ros_primitive_types:
        return inst

    # Check if it's a list or tuple
    if type(inst) in list_types:
        return _from_list_inst(inst, rostype)

    # Assume it's otherwise a full ros msg object
    return _from_object_inst(inst, rostype) 
Example #13
Source File: style_sheet.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def contents_with_inlined_images(self):
    images_by_url = {}
    for i in self.images:
      for a in i.aliases:
        images_by_url[a] = i

    def InlineUrl(m):
      url = m.group('url')
      image = images_by_url[url]

      ext = os.path.splitext(image.absolute_path)[1]
      data = base64.standard_b64encode(image.contents)

      return 'url(data:image/%s;base64,%s)' % (ext[1:], data)

    # I'm assuming we only have url()'s associated with images
    return re.sub('url\((?P<quote>"|\'|)(?P<url>[^"\'()]*)(?P=quote)\)',
                  InlineUrl, self.contents) 
Example #14
Source File: style_sheet_unittest.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def testImages(self):
    fs = fake_fs.FakeFS()
    fs.AddFile('/src/foo/x.css', """
.x .y {
    background-image: url(../images/bar.jpeg);
}
""")
    fs.AddFile('/src/images/bar.jpeg', 'hello world')
    with fs:
      project = project_module.Project([os.path.normpath('/src/')])
      loader = resource_loader.ResourceLoader(project)

      foo_x = loader.LoadStyleSheet('foo.x')
      self.assertEquals(1, len(foo_x.images))

      r0 = foo_x.images[0]
      self.assertEquals(os.path.normpath('/src/images/bar.jpeg'),
                        r0.absolute_path)

      inlined = foo_x.contents_with_inlined_images
      self.assertEquals("""
.x .y {
    background-image: url(data:image/jpeg;base64,%s);
}
""" % base64.standard_b64encode('hello world'), inlined) 
Example #15
Source File: style_sheet.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def contents_with_inlined_images(self):
    images_by_url = {}
    for i in self.images:
      for a in i.aliases:
        images_by_url[a] = i

    def InlineUrl(m):
      url = m.group('url')
      image = images_by_url[url]

      ext = os.path.splitext(image.absolute_path)[1]
      data = base64.standard_b64encode(image.contents)

      return 'url(data:image/%s;base64,%s)' % (ext[1:], data)

    # I'm assuming we only have url()'s associated with images
    return re.sub('url\((?P<quote>"|\'|)(?P<url>[^"\'()]*)(?P=quote)\)',
                  InlineUrl, self.contents) 
Example #16
Source File: aws_srp.py    From warrant with Apache License 2.0 6 votes vote down vote up
def process_challenge(self, challenge_parameters):
        user_id_for_srp = challenge_parameters['USER_ID_FOR_SRP']
        salt_hex = challenge_parameters['SALT']
        srp_b_hex = challenge_parameters['SRP_B']
        secret_block_b64 = challenge_parameters['SECRET_BLOCK']
        # re strips leading zero from a day number (required by AWS Cognito)
        timestamp = re.sub(r" 0(\d) ", r" \1 ",
                           datetime.datetime.utcnow().strftime("%a %b %d %H:%M:%S UTC %Y"))
        hkdf = self.get_password_authentication_key(user_id_for_srp,
                                                    self.password, hex_to_long(srp_b_hex), salt_hex)
        secret_block_bytes = base64.standard_b64decode(secret_block_b64)
        msg = bytearray(self.pool_id.split('_')[1], 'utf-8') + bytearray(user_id_for_srp, 'utf-8') + \
              bytearray(secret_block_bytes) + bytearray(timestamp, 'utf-8')
        hmac_obj = hmac.new(hkdf, msg, digestmod=hashlib.sha256)
        signature_string = base64.standard_b64encode(hmac_obj.digest())
        response = {'TIMESTAMP': timestamp,
                    'USERNAME': user_id_for_srp,
                    'PASSWORD_CLAIM_SECRET_BLOCK': secret_block_b64,
                    'PASSWORD_CLAIM_SIGNATURE': signature_string.decode('utf-8')}
        if self.client_secret is not None:
            response.update({
                "SECRET_HASH":
                self.get_secret_hash(self.username, self.client_id, self.client_secret)})
        return response 
Example #17
Source File: cos_comm.py    From cos-python-sdk-v5 with MIT License 6 votes vote down vote up
def get_content_md5(body):
    """计算任何输入流的md5值"""
    if isinstance(body, text_type) or isinstance(body, binary_type):
        return get_md5(body)
    elif hasattr(body, 'tell') and hasattr(body, 'seek') and hasattr(body, 'read'):
        file_position = body.tell()  # 记录文件当前位置
        # avoid OOM
        md5 = hashlib.md5()
        chunk = body.read(DEFAULT_CHUNK_SIZE)
        while chunk:
            md5.update(to_bytes(chunk))
            chunk = body.read(DEFAULT_CHUNK_SIZE)
        md5_str = base64.standard_b64encode(md5.digest())
        try:
            body.seek(file_position)  # 恢复初始的文件位置
        except Exception as e:
            raise CosClientError('seek unsupported to calculate md5!')
        return md5_str
    else:
        raise CosClientError('unsupported body type to calculate md5!')
    return None 
Example #18
Source File: style_sheet_unittest.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def testImages(self):
    fs = fake_fs.FakeFS()
    fs.AddFile('/src/foo/x.css', """
.x .y {
    background-image: url(../images/bar.jpeg);
}
""")
    fs.AddFile('/src/images/bar.jpeg', 'hello world')
    with fs:
      project = project_module.Project([os.path.normpath('/src/')])
      loader = resource_loader.ResourceLoader(project)

      foo_x = loader.LoadStyleSheet('foo.x')
      self.assertEquals(1, len(foo_x.images))

      r0 = foo_x.images[0]
      self.assertEquals(os.path.normpath('/src/images/bar.jpeg'),
                        r0.absolute_path)

      inlined = foo_x.contents_with_inlined_images
      self.assertEquals("""
.x .y {
    background-image: url(data:image/jpeg;base64,%s);
}
""" % base64.standard_b64encode('hello world'), inlined) 
Example #19
Source File: cluster.py    From MLOpsDatabricks with MIT License 5 votes vote down vote up
def install_libraries(self, databricks_token, library_config_file_path):
        if self.state is None:
            self.get_state(databricks_token)

        if self.state != 'RUNNING':
            raise ClusterManagementException(
                "The cluster %s is not RUNNING (%s)" %
                (self.id, self.state)
            )

        with open(library_config_file_path, "r") as content:
            libraries = json.load(content)

        response = requests.post(
            self.get_base_url() + 'libraries/install',
            headers={
                'Authorization': b"Basic " + base64.standard_b64encode(
                    b"token:" + str.encode(databricks_token))
            },
            json={
                "cluster_id": self.id,
                "libraries": libraries
            })

        if response.status_code != 200:
            raise ClusterManagementException(
                "Error installing libraries: %s: %s" %
                (response.json()["error_code"], response.json()["message"])
            ) 
Example #20
Source File: images.py    From kitty with GNU General Public License v3.0 5 votes vote down vote up
def transmit_image(self, image_data: ImageData, image_id: int, rgba_path: str, width: int, height: int) -> int:
        self.transmission_status[image_id] = 0
        gc = GraphicsCommand()
        gc.a = 't'
        gc.f = image_data.transmit_fmt
        gc.s = width
        gc.v = height
        gc.i = image_id
        if self.filesystem_ok:
            gc.t = 'f'
            self.handler.cmd.gr_command(
                gc, standard_b64encode(rgba_path.encode(fsenc)))
        else:
            import zlib
            with open(rgba_path, 'rb') as f:
                data = f.read()
            gc.S = len(data)
            data = zlib.compress(data)
            gc.o = 'z'
            data = standard_b64encode(data)
            while data:
                chunk, data = data[:4096], data[4096:]
                gc.m = 1 if data else 0
                self.handler.cmd.gr_command(gc, chunk)
                gc.clear()
        return image_id 
Example #21
Source File: set_background_image.py    From kitty with GNU General Public License v3.0 5 votes vote down vote up
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
        if not args:
            self.fatal('Must specify path to PNG image')
        path = args[0]
        ret = {
            'match': opts.match,
            'configured': opts.configured,
            'layout': opts.layout,
            'all': opts.all,
            'img_id': str(uuid4())
        }
        if path.lower() == 'none':
            ret['data'] = '-'
            return ret
        if imghdr.what(path) != 'png':
            self.fatal('{} is not a PNG image'.format(path))

        def file_pipe(path: str) -> Generator[Dict, None, None]:
            with open(path, 'rb') as f:
                while True:
                    data = f.read(512)
                    if not data:
                        break
                    ret['data'] = standard_b64encode(data).decode('ascii')
                    yield ret
            ret['data'] = ''
            yield ret
        return file_pipe(path) 
Example #22
Source File: gr.py    From kitty with GNU General Public License v3.0 5 votes vote down vote up
def display_png_file(path):
    cmd = {'a': 'T', 't': 'f', 'f': '100'}
    path = os.path.abspath(path)
    if not isinstance(path, bytes):
        path = path.encode(sys.getfilesystemencoding() or 'utf-8')
    data = standard_b64encode(path)
    write_gr_cmd(cmd, data) 
Example #23
Source File: gr.py    From kitty with GNU General Public License v3.0 5 votes vote down vote up
def display(data, width, height, x, y, z, ncols=0, nrows=0):
    move_cursor(x, y)
    cmd = {'a': 'T', 's': width, 'v': height, 'c': ncols, 'r': nrows, 'S': len(data), 'z': z}
    data = zlib.compress(data)
    cmd['o'] = 'z'
    data = standard_b64encode(data)
    while data:
        chunk, data = data[:4096], data[4096:]
        m = 1 if data else 0
        cmd['m'] = m
        write_gr_cmd(cmd, chunk)
        cmd.clear() 
Example #24
Source File: loop.py    From kitty with GNU General Public License v3.0 5 votes vote down vote up
def __call__(self, *a: Any, **kw: Any) -> None:
        from base64 import standard_b64encode
        buf = io.StringIO()
        kw['file'] = buf
        print(*a, **kw)
        stext = buf.getvalue()
        text = b'\x1bP@kitty-print|' + standard_b64encode(stext.encode('utf-8')) + b'\x1b\\'
        fobj = self.fobj or sys.stdout.buffer
        fobj.write(text)
        fobj.flush() 
Example #25
Source File: widget.py    From pySINDy with MIT License 5 votes vote down vote up
def _get_embed_state(self, drop_defaults=False):
        state = {
            'model_name': self._model_name,
            'model_module': self._model_module,
            'model_module_version': self._model_module_version
        }
        model_state, buffer_paths, buffers = _remove_buffers(self.get_state(drop_defaults=drop_defaults))
        state['state'] = model_state
        if len(buffers) > 0:
            state['buffers'] = [{'encoding': 'base64',
                                 'path': p,
                                 'data': standard_b64encode(d).decode('ascii')}
                                for p, d in zip(buffer_paths, buffers)]
        return state 
Example #26
Source File: default.py    From kodi with GNU General Public License v3.0 5 votes vote down vote up
def tivixDecode(self, x):
        a = x[2:]
        file3_separator = '//'
        # bk0, bk1...bk4
        bk = ['3d4788f5-ef50-4329-afb6-c400ae0897fa', '44d1e467-f246-4669-92e1-8ee6b6b3b314', '970e632e-2d80-47c9-85e3-2910c42cb8df',
              '33f3b87a-1c7c-4076-a689-55c56a6d09d7', 'ce2173f7-f004-4699-afbd-c10747362fd4']
        for k in reversed(bk):
            a = a.replace(file3_separator + base64.standard_b64encode(urllib.quote(k, safe='~()*!.\'')), '')
        try:
            template = base64.standard_b64decode(a)
        except:
            template = ''
        return template 
Example #27
Source File: cluster.py    From MLOpsDatabricks with MIT License 5 votes vote down vote up
def terminate(
        self,
        databricks_token,
        library_config_file_path,
        permanent=False
    ):
        if permanent is True:
            api_url = 'clusters/permanent-delete'
        else:
            api_url = 'clusters/delete'
            self.uninstall_libraries(
                databricks_token,
                library_config_file_path
            )
            print(
                "Requested to uninstall libraries from %s" %
                (self.id)
            )

        response = requests.post(
            self.get_base_url() + api_url,
            headers={
                'Authorization': b"Basic " + base64.standard_b64encode(
                    b"token:" + str.encode(databricks_token))
            },
            json={
                "cluster_id": self.id
            }),

        if response[0].status_code != 200:
            raise ClusterManagementException(
                "Error terminating cluster: %s: %s" %
                (
                    response[0].json()["error_code"],
                    response[0].json()["message"]
                )
            ) 
Example #28
Source File: cluster.py    From MLOpsDatabricks with MIT License 5 votes vote down vote up
def uninstall_libraries(self, databricks_token, library_config_file_path):
        if self.state is None:
            self.get_state(databricks_token)

        if self.state == 'TERMINATED':
            return

        with open(library_config_file_path, "r") as content:
            libraries = json.load(content)

        response = requests.post(
            self.get_base_url() + 'libraries/uninstall',
            headers={
                'Authorization': b"Basic " + base64.standard_b64encode(
                    b"token:" + str.encode(databricks_token))
            },
            json={
                "cluster_id": self.id,
                "libraries": libraries
            })

        if response.status_code != 200:
            raise ClusterManagementException(
                "Error uninstalling libraries: %s: %s" %
                (response.json()["error_code"], response.json()["message"])
            ) 
Example #29
Source File: cluster.py    From MLOpsDatabricks with MIT License 5 votes vote down vote up
def start(self, databricks_token):
        if not self.id:
            raise ClusterManagementException(
                "The Cluster ID is undefined"
            )

        if self.state is None:
            self.get_state(databricks_token)

        if self.state == 'TERMINATED':
            response = requests.post(
                self.get_base_url() + 'clusters/start',
                headers={
                    'Authorization': b"Basic " + base64.standard_b64encode(
                        b"token:" + str.encode(databricks_token))
                },
                json={
                    "cluster_id": self.id
                })

            if response.status_code == 200:
                self.get_state(databricks_token)
            else:
                raise ClusterManagementException(
                    "Error starting cluster: %s: %s" %
                    (response.json()["error_code"], response.json()["message"])
                ) 
Example #30
Source File: cluster.py    From MLOpsDatabricks with MIT License 5 votes vote down vote up
def create(
        self,
        databricks_token,
        vm_type="Standard_D3_v2"
    ):
        response = requests.post(
            self.get_base_url() + 'clusters/create',
            headers={
                'Authorization': b"Basic " + base64.standard_b64encode(
                    b"token:" + str.encode(databricks_token)
                )
            },
            json={
                "cluster_name": self.name,
                "spark_version": "5.4.x-cpu-ml-scala2.11",
                "node_type_id": vm_type,
                "driver_node_type_id": vm_type,
                "spark_env_vars": {
                    "PYSPARK_PYTHON": "/databricks/python3/bin/python3"
                },
                "autoscale": {
                    "min_workers": 1,
                    "max_workers": 2
                },
                "autotermination_minutes": 60
            })

        if response.status_code == 200:
            self.id = response.json()['cluster_id']

            self.get_state(databricks_token)
        else:
            raise ClusterManagementException(
                "Error creating the cluster: %s:%s" % (
                    response.json()["error_code"],
                    response.json()["message"]
                )
            )