Exemplo n.º 1
0
def expand_application(app, dbsession):
    app.activator = (
        dbsession.query(Activator).filter(Activator.id == app.activatorId).one_or_none()
    )
    if app.activator:
        activator_extension.expand_activator(app.activator, dbsession)

    # TODO?: Expand app deployments if required
    # app.deployments = dbs.query(ApplicationDeployment).filter(
    #   ApplicationDeployment.applicationId == app.id
    # ).all()

    app_deps = (
        dbsession.query(ApplicationDeployment)
        .filter(ApplicationDeployment.applicationId == app.id)
        .all()
    )
    logger.debug("expand_application: %s", app_deps)
    if len(app_deps) > 0:
        if all(elem.deploymentState == DeploymentStatus.SUCCESS for elem in app_deps):
            app.deploymentState = DeploymentStatus.SUCCESS
        elif any(elem.deploymentState == DeploymentStatus.PENDING for elem in app_deps):
            app.deploymentState = DeploymentStatus.PENDING
        elif any(elem.deploymentState == DeploymentStatus.FAILURE for elem in app_deps):
            app.deploymentState = DeploymentStatus.FAILURE
        elif any(elem.deploymentState == DeploymentStatus.STARTED for elem in app_deps):
            app.deploymentState = DeploymentStatus.STARTED
    else:
        app.deploymentState = None
    logger.debug("expand_application::deploymentState: %s", app.deploymentState)
    return app
Exemplo n.º 2
0
def create(activatorDetails):
    """
    This function creates a new activator in the activator list
    based on the passed in activator data

    :param activator:  activator to create in activator list
    :return:        201 on success, 406 on activator exists
    """
    with db_session() as dbs:
        # Remove id as it's created automatically
        if "id" in activatorDetails:
            del activatorDetails["id"]

        extraFields = activator_extension.refine_activator_details(activatorDetails)

        schema = ActivatorSchema()
        new_activator = schema.load(activatorDetails, session=dbs)
        dbs.add(new_activator)
        dbs.flush()
        # Create entries into all tables where activator has association
        activator_extension.create_activator_associations(
            extraFields, new_activator, dbs
        )
        # Expand Activator
        new_activator = activator_extension.expand_activator(new_activator, dbs)
        schema = ExtendedActivatorSchema(many=False)
        data = schema.dump(new_activator)
        return data, 201
Exemplo n.º 3
0
def read_one(oid):
    """
    This function responds to a request for /api/activator/{key}
    with one matching activator from activatorss

    :param application:   key of activator to find
    :return:              activator matching key
    """
    with db_session() as dbs:
        act = (
            dbs.query(Activator)
            .filter(
                Activator.id == oid,
            )
            .one_or_none()
        )

        if act is not None:
            # Expand Activator
            act = activator_extension.expand_activator(act, dbs)
            schema = ExtendedActivatorSchema(many=False)
            data = schema.dump(act)
            return data, 200
        else:
            abort(404, f"Activator with id {oid} not found".format(id=oid))
Exemplo n.º 4
0
def create(activatorByURLDetails):
    """
    Args:
        url ([url]): [URL of the githib repo to get activator_metadata.yml]

        1. Connect to GitHIB repo using Github API and download yaml file
        2. Read  the contents of activator_metadata.yml file
        3. Create activator object to insert an entry into 'activator' table
        4. Create activatorMetadata object to insert an entry into 'activatorMetadata' table
        5. Read 'Platforms' field from yaml file and create 'activatorPlatform' object and insert into 'activatorPlatform' table
        6. Read 'mandatoryVariables'from yaml and insert into 'activatorVariables' table with 'isOptional'=False
        7. Read 'optionalVariables' from yaml and insert into 'activatorVariables' table with 'isOptional'=True

    """
    try:
        with db_session() as dbs:
            user = security.get_valid_user_from_token(dbsession=dbs)
            if not (user and user.isMCAdmin):
                return abort(
                    401,
                    "JWT not valid or user is not a Mission Control Admin")

            github_credentials = systemsettings.get_github_credentials(user.id)

            # get Yaml from gitgub and read the contents of the yaml file
            act_metadata_yml_dict = get_file_from_repo(
                activatorByURLDetails["url"], github_credentials)

            activator_id = create_activator(dbs, act_metadata_yml_dict,
                                            activatorByURLDetails["url"])

            activator_metadata = create_activator_metadata(
                dbs, act_metadata_yml_dict, activator_id,
                activatorByURLDetails["url"])

            create_activator_metadata_platforms(dbs, act_metadata_yml_dict,
                                                activator_metadata.id)

            mandatoryVariables = act_metadata_yml_dict["mandatoryVariables"]
            create_activator_metadata_variables(dbs, activator_metadata.id,
                                                mandatoryVariables, False)

            optionalVariables = act_metadata_yml_dict["optionalVariables"]
            create_activator_metadata_variables(dbs, activator_metadata.id,
                                                optionalVariables, True)

            # return the activator
            act = (dbs.query(Activator).filter(
                Activator.id == activator_id, ).one_or_none())

            if act is not None:
                # Expand Activator
                act = activator_extension.expand_activator(act, dbs)
                schema = ExtendedActivatorSchema(many=False)
                data = schema.dump(act)
                return data, 200

    except Exception as ex:
        logger.exception(ex)
        abort(500, "Internal Server Error")
Exemplo n.º 5
0
def deploy_application(app_deployment, dbsession):

    logger.debug("deploy_application:: %s", app_deployment)
    # expand fields for DaC application deployment
    app, act, actMetadata, lzenv = (dbsession.query(
        Application, Activator, ActivatorMetadata, LZEnvironment).filter(
            Activator.id == Application.activatorId,
            ActivatorMetadata.activatorId == Application.activatorId,
            Application.id == app_deployment.applicationId,
            ApplicationDeployment.applicationId == Application.id,
            ApplicationDeployment.lzEnvironmentId == LZEnvironment.id,
            LZEnvironment.id == app_deployment.lzEnvironmentId,
        ).one_or_none())

    act = activator_extension.expand_activator(act, dbsession)

    actMetadataVariable = (dbsession.query(ActivatorMetadataVariable).filter(
        ActivatorMetadataVariable.activatorMetadataId == actMetadata.id,
    ).all())

    if act:
        gitSnapshot = json.loads(act.gitSnapshotJson)
        if gitSnapshot and "git_clone_url" in gitSnapshot.keys():
            app_deployment.activatorGitUrl = gitSnapshot["git_clone_url"]
        else:
            raise Exception(
                "Error, could not retrieve git_clone_url from gitSnapshot Json"
            )

        optional_pairs, mandatory_pairs = get_variables_from_metadata(
            actMetadataVariable)
        app_deployment.optionalVariables = optional_pairs
        app_deployment.mandatoryVariables = mandatory_pairs

        app_deployment.workspaceProjectId = app_deployment.workspaceProjectId
        app_deployment.deploymentProjectId = app_deployment.deploymentProjectId

        app_deployment.id = app.id
        app_deployment.name = app.name
        app_deployment.description = app.description

        environment, lzlanvpc = (dbsession.query(
            LZEnvironment, LZLanVpc).filter(
                LZLanVpcEnvironment.lzlanvpcId == LZLanVpc.id,
                LZLanVpcEnvironment.environmentId == lzenv.id,
                LZLanVpcEnvironment.environmentId == LZEnvironment.id,
                LZLanVpcEnvironment.isActive,
                LZEnvironment.isActive,
            ).one_or_none())
        if lzlanvpc:
            environment.sharedVPCProjectId = lzlanvpc.sharedVPCProjectId
        else:
            environment.sharedVPCProjectId = ""
        app_deployment.deploymentEnvironment = environment

        return send_application_deployment_to_the_dac(app_deployment,
                                                      dbsession=dbsession)
    else:
        logger.error("deploy_application::activator not found, %s!",
                     app.activatorId)
Exemplo n.º 6
0
def update(oid, activatorDetails):
    """
    This function updates an existing activator in the activators list

    :param key:    key of the activator to update in the activators list
    :param activator:   activator to update
    :return:       updated activator
    """
    logger.debug("update")
    logger.debug("id")
    logger.debug(oid)
    logger.debug("activator")
    logger.debug(pformat(activatorDetails))

    if "id" in activatorDetails and activatorDetails["id"] != oid:
        abort(400, "Key mismatch in path and body")

    with db_session() as dbs:
        # Does the activators exist in activators list?
        existing_activator = (
            dbs.query(Activator).filter(Activator.id == oid).one_or_none()
        )

        # Does activator exist?

        if existing_activator is not None:
            # schema = ActivatorSchema()
            activatorDetails["id"] = oid
            logger.info("activatorDetails: %s", activatorDetails)

            extraFields = activator_extension.refine_activator_details(activatorDetails)

            schema = ActivatorSchema(many=False, session=dbs)
            updatedActivator = schema.load(activatorDetails)
            logger.info("updatedActivator: %s", updatedActivator)
            dbs.merge(updatedActivator)
            # Update CI list in activatorCI table
            # return the updated activator in the response
            dbs.flush()

            response = activator_extension.create_activator_associations(
                extraFields, updatedActivator, dbs
            )

            if response:
                abort(response["code"], response["message"])

                # Expand activator
            updatedActivator = activator_extension.expand_activator(
                updatedActivator, dbs
            )

            schema = ExtendedActivatorSchema(many=False)
            data = schema.dump(updatedActivator)
            return data, 200
        # otherwise, nope, deployment doesn't exist, so that's an error
        else:
            abort(404, f"Activator id {oid} not found")
Exemplo n.º 7
0
def setActivatorStatus(activatorDetails):
    """
    Update the activator status.
    : return:      The activator that was changed
    """
    logger.info(pformat(activatorDetails))

    with db_session() as dbs:
        # Does the activator to delete exist?
        existing_activator = (
            dbs.query(Activator)
            .filter(Activator.id == activatorDetails["id"], Activator.isActive)
            .one_or_none()
        )

        # if found?
        if existing_activator is not None:
            schema = ActivatorSchema()
            updated_activator = schema.load(activatorDetails, session=dbs)
            updated_activator.lastUpdated = ModelTools.get_utc_timestamp()
            dbs.merge(updated_activator)

            # Expand Activator
            updated_activator = activator_extension.expand_activator(
                updated_activator, dbs
            )

            activator_schema = ExtendedActivatorSchema()
            data = activator_schema.dump(updated_activator)

            # Create notifications
            if (
                updated_activator.status != "Available"
                and updated_activator.accessRequestedById
            ):
                full_name = (
                    (updated_activator.accessRequestedBy.firstName or "")
                    + " "
                    + (updated_activator.accessRequestedBy.lastName or "")
                )
                message = f"{full_name} has requested access to activator #{updated_activator.id}"
                notify_admins(
                    message=message,
                    activatorId=updated_activator.id,
                    fromUserId=updated_activator.accessRequestedById,
                )
            elif (
                updated_activator.status == "Available"
                and updated_activator.accessRequestedById
            ):
                activator_name = (
                    f"Activator {updated_activator.id} ({updated_activator.name})"
                )
                message = f"Access to {activator_name} has been granted."
                notify_user(
                    message,
                    activatorId=updated_activator.id,
                    toUserId=updated_activator.accessRequestedById,
                )

            return data, 200

        # Otherwise, nope, activator to update was not found
        else:
            actid = activatorDetails["id"]
            abort(404, f"Activator id {actid} not found")
Exemplo n.º 8
0
def read_all(
    isActive=None,
    isFavourite=None,
    category=None,
    status=None,
    environment=None,
    platform=None,
    type=None,
    source=None,
    sensitivity=None,
    page=None,
    page_size=None,
    sort=None,
):
    """
    This function responds to a request for /api/activators
    with the complete lists of activators

    :return:        json string of list of activators
    """
    # Create the list of activators from our data
    logger.debug(
        "Parameters: isActive: %s, isFavourite: %s, category: %s, status: %s, environment: %s, platform: %s, "
        "type: %s, source: %s, sensitivity: %s, page: %s, page_size: %s, sort: %s",
        isActive,
        isFavourite,
        category,
        status,
        environment,
        platform,
        type,
        source,
        sensitivity,
        page,
        page_size,
        sort,
    )

    with db_session() as dbs:
        # pre-process sort instructions
        if sort is None:
            activator_query = dbs.query(Activator).order_by(Activator.id)
        else:
            try:
                sort_inst = [si.split(":") for si in sort]
                orderby_arr = []
                for si in sort_inst:
                    si1 = si[0]
                    if len(si) > 1:
                        si2 = si[1]
                    else:
                        si2 = "asc"
                    orderby_arr.append(f"{si1} {si2}")
                # print("orderby: {}".format(orderby_arr))
                activator_query = dbs.query(Activator).order_by(
                    literal_column(", ".join(orderby_arr))
                )
            except SQLAlchemyError as e:
                logger.warning(e)
                activator_query = dbs.query(Activator).order_by(Activator.id)

    activator_metadatas = (
        dbs.query(ActivatorMetadata)
        .filter(
            (category is None or ActivatorMetadata.category == category),
            (type is None or ActivatorMetadata.typeId == type),
        )
        .all()
    )
    act_ids = None
    if activator_metadatas:
        act_ids = [am.activatorId for am in activator_metadatas]

    activator_query = activator_query.filter(
        (status is None or Activator.status == status),
        (environment is None or Activator.envs.like('%"{}"%'.format(environment))),
        (source is None or Activator.sourceControl.like('%"{}"%'.format(source))),
        (sensitivity is None or Activator.sensitivity == sensitivity),
        (isActive is None or Activator.isActive == isActive),
        (isFavourite is None or Activator.isFavourite == isFavourite),
        (act_ids is None or Activator.id.in_(act_ids)),
    )
    if act_ids is None:
        activators = None
    elif page is None or page_size is None:
        activators = activator_query.all()
    else:
        activators = activator_query.limit(page_size).offset(page * page_size).all()
    if activators:
        # Expand all Activators
        for act in activators:
            act = activator_extension.expand_activator(act, dbs)

        activator_schema = ExtendedActivatorSchema(many=True)
        data = activator_schema.dump(activators)

        logger.debug("read_all")
        logger.debug(pformat(data))
        return data, 200
    else:
        abort(404, "No Activators found with matching criteria")