Example #1
0
File: k8s.py Project: Dpk28/nephos
def secret_from_file(secret,
                     namespace,
                     key=None,
                     filename=None,
                     verbose=False):
    """Convert a file into a K8S Secret.

    Args:
        secret (str): Name of Secret where to save the file.
        namespace (str): Name of namespace.
        key (str): Key to which to assign the file in the K8S t. If not specified, the filename is used.
        filename (str): If not provided, we ask the user for input.
        verbose (bool): Verbosity. False by default.
    """
    try:
        secret_read(secret, namespace, verbose=verbose)
    except ApiException:
        # Get relevant variables
        if not filename:
            secret_data = input_files((key, ), secret, clean_key=True)
        else:
            with open(filename, "rb") as f:
                data = f.read()
                secret_data = {key: data}
        secret_create(secret_data, secret, namespace, verbose=verbose)
Example #2
0
 def test_input_files_cleankey(self, mock_print, mock_get_response, mock_isfile, mock_open):
     mock_isfile.side_effect = [True]
     mock_get_response.side_effect = [self.files[0]]
     data = input_files((None,), clean_key=True)
     mock_print.assert_called_once_with('Replaced some_file&.txt with some_file_.txt')
     mock_get_response.assert_called_with('Input None')
     mock_isfile.assert_called_with(self.files[0])
     mock_open.assert_called_with(self.files[0], 'rb')
     assert data.keys() == {'some_file_.txt'}
Example #3
0
 def test_input_files_mistake(self, mock_print, mock_get_response, mock_isfile, mock_open):
     mock_isfile.side_effect = [False, True]
     mock_get_response.side_effect = [self.files[0] + 'OOPS', self.files[0]]
     data = input_files(('hello',))
     mock_print.assert_called_once_with('{} is not a file'.format(self.files[0] + 'OOPS'))
     mock_get_response.assert_has_calls([call('Input hello'), call('Input hello')])
     mock_isfile.assert_has_calls([call(self.files[0] + 'OOPS'), call(self.files[0])])
     mock_open.assert_called_with(self.files[0], 'rb')
     assert data.keys() == {'hello'}
Example #4
0
 def test_input_files_suffix(self, mock_print, mock_get_response, mock_isfile, mock_open):
     mock_isfile.side_effect = [True]
     mock_get_response.side_effect = [self.files[0]]
     data = input_files(('hello',), text_append='big')
     mock_print.assert_not_called()
     mock_get_response.assert_called_with('Input hello big')
     mock_isfile.assert_called_with(self.files[0])
     mock_open.assert_called_with(self.files[0], 'rb')
     assert data.keys() == {'hello'}
Example #5
0
 def test_input_files(self, mock_print, mock_get_response, mock_isfile,
                      mock_open):
     mock_isfile.side_effect = [True]
     mock_get_response.side_effect = [self.files[0]]
     data = input_files(("hello", ))
     mock_print.assert_not_called()
     mock_get_response.assert_called_with("Input hello")
     mock_isfile.assert_called_with(self.files[0])
     mock_open.assert_called_with(self.files[0], "rb")
     assert data.keys() == {"hello"}
Example #6
0
 def test_input_files_multiple(self, mock_print, mock_get_response, mock_isfile, mock_open):
     mock_isfile.side_effect = [True, True]
     mock_get_response.side_effect = self.files
     data = input_files(('hello', 'goodbye'))
     mock_print.assert_not_called()
     mock_get_response.assert_has_calls([call('Input hello'), call('Input goodbye')])
     mock_isfile.assert_has_calls([call(self.files[0]), call(self.files[1])])
     mock_open.assert_any_call(self.files[0], 'rb')
     mock_open.assert_any_call(self.files[1], 'rb')
     assert data.keys() == {'hello', 'goodbye'}
Example #7
0
 def test_input_files_cleankey(self, mock_log, mock_get_response,
                               mock_isfile, mock_open):
     mock_isfile.side_effect = [True]
     mock_get_response.side_effect = [self.files[0]]
     data = input_files((None, ), clean_key=True)
     mock_log.warning.assert_called_once_with(
         "Replaced some_file&.txt with some_file_.txt")
     mock_get_response.assert_called_with("Input None")
     mock_isfile.assert_called_with(self.files[0])
     mock_open.assert_called_with(self.files[0], "rb")
     assert data.keys() == {"some_file_.txt"}
Example #8
0
def deploy_composer(opts, upgrade=False, verbose=False):
    """Deploy Hyperledger Composer on K8S.

    We use the hl-composer Helm chart as a basis to deploying Composer
    on K8S. Please note that Composer is unmaintained and may eventually
    be deprecated from this repository as we migrate to raw Fabric.

    Args:
        opts (dict): Nephos options dict.
        upgrade (bool): Do we upgrade the deployment? False by default.
        verbose (bool): Verbosity. False by default.
    """
    peer_namespace = get_namespace(opts, opts["peers"]["msp"])
    # Ensure BNA exists
    secret_data = input_files((None, ), clean_key=True)
    secret_create(secret_data, opts["composer"]["secret_bna"], peer_namespace)
    composer_connection(opts, verbose=verbose)

    # Start Composer
    version = get_version(opts, "hl-composer")
    config_yaml = (
        f'{opts["core"]["dir_values"]}/hl-composer/{opts["composer"]["name"]}.yaml'
    )
    if not upgrade:
        extra_vars = helm_extra_vars(version=version, config_yaml=config_yaml)
        helm_install(
            opts["core"]["chart_repo"],
            "hl-composer",
            opts["composer"]["name"],
            peer_namespace,
            extra_vars=extra_vars,
        )
    else:
        preserve = (HelmPreserve(
            peer_namespace,
            f"{opts['composer']['name']}-hl-composer-rest",
            "COMPOSER_APIKEY",
            "rest.config.apiKey",
        ), )
        extra_vars = helm_extra_vars(version=version,
                                     config_yaml=config_yaml,
                                     preserve=preserve)
        helm_upgrade(
            opts["core"]["chart_repo"],
            "hl-composer",
            opts["composer"]["name"],
            extra_vars=extra_vars,
        )
    helm_check("hl-composer",
               opts["composer"]["name"],
               peer_namespace,
               pod_num=3)
Example #9
0
 def test_input_files_multiple(self, mock_print, mock_get_response,
                               mock_isfile, mock_open):
     mock_isfile.side_effect = [True, True]
     mock_get_response.side_effect = self.files
     data = input_files(("hello", "goodbye"))
     mock_print.assert_not_called()
     mock_get_response.assert_has_calls(
         [call("Input hello"), call("Input goodbye")])
     mock_isfile.assert_has_calls(
         [call(self.files[0]), call(self.files[1])])
     mock_open.assert_any_call(self.files[0], "rb")
     mock_open.assert_any_call(self.files[1], "rb")
     assert data.keys() == {"hello", "goodbye"}
Example #10
0
 def test_input_files_mistake(self, mock_print, mock_get_response,
                              mock_isfile, mock_open):
     mock_isfile.side_effect = [False, True]
     mock_get_response.side_effect = [self.files[0] + "OOPS", self.files[0]]
     data = input_files(("hello", ))
     mock_print.assert_called_once_with(
         "{} is not a file".format(self.files[0] + "OOPS"))
     mock_get_response.assert_has_calls(
         [call("Input hello"), call("Input hello")])
     mock_isfile.assert_has_calls(
         [call(self.files[0] + "OOPS"),
          call(self.files[0])])
     mock_open.assert_called_with(self.files[0], "rb")
     assert data.keys() == {"hello"}
Example #11
0
def secret_from_file(secret,
                     namespace,
                     key=None,
                     filename=None,
                     verbose=False):
    try:
        secret_read(secret, namespace, verbose=verbose)
    except ApiException:
        # Get relevant variables
        if not filename:
            secret_data = input_files([key], secret, clean_key=True)
        else:
            with open(filename, 'rb') as f:
                data = f.read()
                secret_data = {key: data}
        secret_create(secret_data, secret, namespace, verbose=verbose)
Example #12
0
def upgrade_network(opts, verbose=False):
    """Upgrade Hyperledger Composer network.

    Args:
        opts (dict): Nephos options dict.
        verbose (bool): Verbosity. False by default.
    """
    peer_namespace = get_namespace(opts, opts["peers"]["msp"])
    secret_data = input_files((None, ), clean_key=True)
    secret_create(secret_data, opts["composer"]["secret_bna"], peer_namespace)

    # Set up the PeerAdmin card
    hlc_cli_ex = get_helm_pod(peer_namespace,
                              "hlc",
                              "hl-composer",
                              verbose=verbose)

    bna, _ = hlc_cli_ex.execute("ls /hl_config/blockchain_network")
    bna_name, bna_rem = bna.split("_")
    bna_version, _ = bna_rem.split(".bna")
    peer_msp = opts["peers"]["msp"]
    bna_admin = opts["msps"][peer_msp]["org_admin"]

    res, _ = hlc_cli_ex.execute(
        f"composer network ping --card {bna_admin}@{bna_name}")

    curr_version = (res.split("Business network version: ")[1]).split()[0]
    logging.info(curr_version)

    if curr_version != bna_version:
        hlc_cli_ex.execute(
            ("composer network install --card PeerAdmin@hlfv1 " +
             f"--archiveFile /hl_config/blockchain_network/{bna}"))
        hlc_cli_ex.execute(
            ("composer network upgrade " + "--card PeerAdmin@hlfv1 " +
             f"--networkName {bna_name} --networkVersion {bna_version}"))
        res, _ = hlc_cli_ex.execute(
            f"composer network ping --card {bna_admin}@{bna_name}")
        curr_version = (res.split("Business network version: ")[1]).split()[0]
        logging.info(f"Upgraded to {curr_version}")