コード例 #1
0
ファイル: bk_api.py プロジェクト: waggle-sensor/beekeeper
    def get(self, node_id):
        try:

            bee_db = BeekeeperDB()
            node_state = bee_db.get_node_state(node_id)
            bee_db.close()
        except Exception as e:
            raise ErrorResponse(f"Unexpected error: {e}" , status_code=HTTPStatus.INTERNAL_SERVER_ERROR)

        if node_state == None:
            raise ErrorResponse(f"Error: node {node_id} not found")



        return { "data" : node_state }
コード例 #2
0
ファイル: bk_api.py プロジェクト: nconrad/beekeeper
    def get(self, node_id):
        try:

            bee_db = BeekeeperDB()
            node_state = bee_db.get_node_state(node_id)

        except Exception as e:
            raise ErrorResponse(f"Unexpected error: {e}",
                                status_code=HTTPStatus.INTERNAL_SERVER_ERROR)

        if "timestamp" in node_state:
            node_state["timestamp"] = node_state["timestamp"].isoformat()

        if "registration_event" in node_state:
            node_state["registration_event"] = node_state[
                "registration_event"].isoformat()

        return {"data": node_state}
コード例 #3
0
ファイル: bk_api.py プロジェクト: waggle-sensor/beekeeper
def set_node_beehive(node_id, beehive_id):

    # At this point we already know that beehive exists.
    # Now check if node is already assigned to that beehive
    node_state = None

    try:
        bee_db = BeekeeperDB()
        node_state = bee_db.get_node_state(node_id)
    #except bk_db.ObjectNotFound:
    #    node_state = None
        bee_db.close()
    except Exception as e:
        logger.debug(f"Getting node failed: {str(e)}")
        time.sleep(3)
        pass

    if not node_state:
        raise Exception(f"node {node_id} not found")


    if "beehive" in node_state:
        if node_state["beehive"] == beehive_id:
            logger.debug(f"Node {node_id} is already assigned to beehive {beehive_id}")
            return

    payload = {"node_id": node_id, "source": "beekeeper-register", "operation":"insert", "field_name": "beehive", "field_value": beehive_id}

    #url = f'{BEEKEEPER_DB_API}/log'
    try:
        insert_log(payload, lock_tables=True, force=True, lock_requested_by="set_node_beehive")
        #bk_api_response = requests.post(url,data=json.dumps(payload), timeout=3)
    except Exception as e:
        #raise Exception(f"Error: X Beekeeper DB API ({url}) cannot be reached: {str(e)}")
        raise Exception(f"insert_log returned: {str(e)}")


    return
コード例 #4
0
ファイル: bk_api.py プロジェクト: waggle-sensor/beekeeper
def deploy_wes(node_id, this_debug, force=False):

    logger.debug("(deploy_wes) determine correct beehive")

    assign_beehive = ""
    bee_db = None
    try:
        bee_db = BeekeeperDB()
        node_state = bee_db.get_node_state(node_id)
    except Exception as e:
        raise Exception(f"finding beehive for node failed: {str(e)}")

    if not node_state:
        raise Exception(f"node {node_id} not found")

    if not "beehive" in node_state:
        raise Exception(f"Node is not assigned to any beehive")

    assign_beehive=node_state["beehive"]
    if assign_beehive == "":
        raise Exception(f"Node is not assigned to any beehive")

    try:
        beehive_obj = bee_db.get_beehive(assign_beehive)
    except Exception as e:
        raise Exception(f"finding beehive for node failed: {str(e)}")

    if not beehive_obj:
        raise Exception(f"Beehive {assign_beehive} unknown" )

    if not isinstance(beehive_obj, dict):
        raise Exception(f"beehive_obj is not a dict")

    beehive_id = beehive_obj.get("id", "")
    if not beehive_id:
        raise Exception(f"beehive_id is missing")

    logger.debug(f"(deploy_wes) using beehive {beehive_id}")


    # first check if kubectl work on the node
    try:
        result_stdout_str ,result_stderr_str, exit_code = node_ssh(node_id, "kubectl get nodes")
    except Exception as e:
        raise Exception(f"node_ssh failed: {str(e)}")

    if exit_code != 0:
        raise Exception(f"ssh failed or kubectl is not yet ready ({result_stderr_str})")


    logger.debug("calling create_ssh_upload_cert")
    try:
        create_ssh_upload_cert(bee_db, node_id, beehive_obj, force=force )
    except Exception as e:
        raise Exception(f"create_ssh_upload_cert failed: {type(e).__name__} {str(e)}")

    logger.debug("calling create_tls_cert_for_node")
    try:
        create_tls_cert_for_node(bee_db, node_id, beehive_obj, force=force)
    except Exception as e:
        raise Exception(f"create_tls_cert_for_node failed: {type(e).__name__} {str(e)}")

    node_keypair = bee_db.get_node_keypair(node_id)

    if not "ssh_key_private" in node_keypair:
        raise Exception(f"ssh_key_private field missing in node_keypair")

    if not "ssh_key_public" in node_keypair:
        raise Exception(f"ssh_key_public field missing in node_keypair")

    ssh_key_private = node_keypair["ssh_key_private"]
    ssh_key_public = node_keypair["ssh_key_public"]

    logger.debug("calling get_node_credentials_all")
    try:
        node_creds = bee_db.get_node_credentials_all(node_id, beehive_id)
    except Exception as e:
        raise Exception(f"get_node_credentials_all failed: {type(e).__name__} {str(e)}")

    bee_db.close()

    if not "tls_cert" in node_creds:
        raise Exception(f"tls_cert field missing in node_creds")
    if not "tls_key" in node_creds:
        raise Exception(f"tls_key field missing in node_creds")

    if not "ssh_upload_cert" in node_creds:
        raise Exception(f"ssh_upload_cert field missing in node_creds")
    ssh_upload_cert = node_creds["ssh_upload_cert"]

    # create and push secret for upload-ssh-key
    upload_secret = kube_secret("wes-beehive-upload-ssh-key", {
        "ssh-key": ssh_key_private,
        "ssh-key.pub": ssh_key_public,
        "ssh-key-cert.pub": ssh_upload_cert,
    })
    kubectl_apply_with_logging(node_id, upload_secret)

    # create and upload tls secret
    tls_secret = kube_secret("wes-beehive-rabbitmq-tls", {
        "cert.pem": node_creds["tls_cert"],
        "key.pem": node_creds["tls_key"],
    })
    kubectl_apply_with_logging(node_id, tls_secret)

    #return {"result": "A"}
    ####################
    # waggle id / host / port config map

    for key in ["rmq_host", "rmq_port", "upload_host", "upload_port"]:
        if not key in beehive_obj:
            raise Exception(f"Beekeeper field {key} missing")

    rmq_host = beehive_obj["rmq_host"]
    rmq_port = beehive_obj["rmq_port"]
    upload_host = beehive_obj["upload_host"]
    upload_port = beehive_obj["upload_port"]

    waggle_ConfigMap = kube_configmap("waggle-config", {
        "WAGGLE_NODE_ID": node_id.lower(),
        "WAGGLE_BEEHIVE_RABBITMQ_HOST": rmq_host,
        "WAGGLE_BEEHIVE_RABBITMQ_PORT": str(rmq_port),
        "WAGGLE_BEEHIVE_UPLOAD_HOST": upload_host,
        "WAGGLE_BEEHIVE_UPLOAD_PORT": str(upload_port),
    })
    kubectl_apply_with_logging(node_id, waggle_ConfigMap)

    ###########################
    # beehive-ssh-ca configmap

    for key in ["ssh-pub", "ssh-cert", "tls-cert"]:
        if not key in beehive_obj:
            available_str = ",".join(list(beehive_obj))
            raise Exception(f"Beekeeper field {key} missing (got: {available_str})")

    ca_ssh_pub = beehive_obj["ssh-pub"]
    ca_ssh_cert = beehive_obj["ssh-cert"]
    ca_tls_cert = beehive_obj["tls-cert"]
    #return {"result": "B"}
    beehive_ssh_ca_ConfigMap = kube_configmap("beehive-ssh-ca", {
        "ca.pub": ca_ssh_pub,
        "ca-cert.pub": ca_ssh_cert,
    })
    kubectl_apply_with_logging(node_id, beehive_ssh_ca_ConfigMap)

    ###########################
    # beehive-tls-ca configmap

    beehive_tls_ca_ConfigMap = kube_configmap("beehive-ca-certificate", {
        "cacert.pem": ca_tls_cert,
    })
    kubectl_apply_with_logging(node_id, beehive_tls_ca_ConfigMap)

    node_private_git_repo_key = "/config/node-private-git-repo-key/node-private-git-repo-key"
    if os.path.exists(node_private_git_repo_key):
        result_stdout , result_stderr, exit_code = scp(node_private_git_repo_key, node_id, "/root/.ssh/")
        if exit_code != 0 :
            raise Exception(f"scp failed {result_stderr} and {result_stdout}")
    else:
        logger.info("/config/node-private-git-repo-key/node-private-git-repo-key not found, skipping")



    final_command = "./update-stack.sh"
    if FAKE_DEPLOYMENT:
        final_command = "echo \"This fake deployment was successful\""

    deploy_script= \
"""\
#!/bin/sh
### This script was generated by beekeeper ###
set -e
set -x

# check if k3s runs
kubectl get nodes

if [ ! -e "/opt/waggle-edge-stack" ] ; then
    cd /opt
    git clone https://github.com/waggle-sensor/waggle-edge-stack.git
fi

cd /opt/waggle-edge-stack
git pull origin main
#git checkout tags/v.1.0

cd /opt/waggle-edge-stack/kubernetes

""" + final_command


    #return {"result": "C"}
    try:
        node_ssh_with_logging(node_id, "cat > /tmp/deploy.sh", input_str=deploy_script)
        node_ssh_with_logging(node_id, "sh /tmp/deploy.sh")
    except Exception as e:
        raise Exception(f"node_ssh_with_logging failed: {str(e)}")

    try:
        register_wes_deployment_event(node_id, lock_tables=True, lock_requested_by="wes_deployment")
    except Exception as e:
        raise Exception(f"register_wes_deployment_event failed: {str(e)}")

    if this_debug:
        return {
            "waggle_ConfigMap": waggle_ConfigMap,
            "tls_secret":tls_secret,
            "upload_secret": upload_secret,
        }

        #node_creds["tls_result_stdout"] = result_stdout
        #node_creds["tls_result_stderr"] = result_stderr

    return {"success":True}