bitovi / bitops

Automate the provisioning and configuration of cloud infrastructure with BitOps docker image
https://bitops.sh
Other
36 stars 9 forks source link

Plugin request - [Serverless] #182

Open PhillypHenning opened 2 years ago

PhillypHenning commented 2 years ago

Is your feature request related to a problem? Please describe. To broaden our support for IaC options I'd like to request that a serverless plugin is created for bitops:v2.0.1

Additional context This should be done sooner rather then later and will use the bitovi/battleship-api initiative as a "use-case"

shyamrayaprolu commented 2 years ago

Here is the slsdeploy script that I created in my previous job...

#!/bin/bash
# *************************************************************************************************** #
#
#   Bash script to recursively run sls deploy
#
#   The script requires the following *MANDATORY* configuration parameters to be supplied as command
#   line arguments:
#       -profile:       AWS Profile
#       -region:        AWS region
#       -subsystem      crmjs
#
#   The following configuration parameters are optional:
#       -stage: Stage to deploy to, default stage is 'default'
#
#
#   Ex: ./slsdeploy.sh -profile asurion-dev.xxxxxx -region us-east-1 -stage sqa
#
# *************************************************************************************************** #

declare AWS_REGION
declare AWS_PROFILE
declare SERVICE_STAGE
declare SUBSYSTEM
declare FUNCTIONS_LIST

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"

# *************************************************************************************************** #
# Recurse thru folders and run sls deploy.
#
# Expected folder structure
#   ../infrastructure               Common artifacts
#   ../infrastructure/functions     Common functions, like Custom Authorizer
#   ../functions                    Parent folder for all functions; To-Do: Dependency
#
# Arguments:
#   None
# Returns:
#   Success/Failure - 0/1
#
# *************************************************************************************************** #
#

prop () {
    grep "${1}" ./functions.properties|cut -d'=' -f2
}

sls_deploy () {

    if [ -d $1 ]; then
        cd $1
        echo "[`date`] INFO: Checking in $1"
        if [ ! -s $1/serverless.yml ]; then
            echo "[`date`] WARN: Not a valid serverless service folder..skipping"
            return 0
        else
            echo "[`date`] Deploying in $1"
            npm install
            [ -z "$SERVICE_STAGE" ] && sls deploy --aws-profile $AWS_PROFILE --region $AWS_REGION --subsystem $SUBSYSTEM --v || sls deploy --aws-profile $AWS_PROFILE --region $AWS_REGION --stage $SERVICE_STAGE --subsystem $SUBSYSTEM --svcname $SVCNAME --v
            # api_id =`aws apigateway get-rest-apis --region $AWS_REGION|jq '.items[]|select(.name=='retail-rtm-na-apigw-dev')'|grep "id"|cut -d':' -f2`
            # aws apigateway update-rest-api  --region $AWS_REGION --rest-api-id $api_id --policy "{\"jsonEscapedPolicyDocument\"}"
        fi

        if [ "$?" -ne 0 ]; then
            echo "[`date`] ERROR: Exiting Script - Deploy failed!"
            exit 1
        fi
    fi

    return 0
}

# *************************************************************************************************** #
# Validates the command line arguments and verifies if all the required arguments have been specified.
#
#
# Arguments:
#   All the command line arguments
# Returns:
#   Success/Failure - 0/1
#
# *************************************************************************************************** #
validate_parameters() {

    # Parse all the command line arguments into individual variables
    while [[ "$#" > 1 ]]; do case $1 in
        -profile) AWS_PROFILE="$2";;
        -region) AWS_REGION="$2";;
        -stage) SERVICE_STAGE="$2";;
        -subsystem) SUBSYSTEM="$2";;
        *)
        ;;
    esac; shift; shift
    done

    # Now parse all the specified values
    if [ -z $AWS_REGION ]; then
        echo "[`date`] ERROR: Please specify region to provision using the -region command line argument!"
        exit 1
    fi

    if [ -z $AWS_PROFILE ]; then
        echo "[`date`] ERROR: Please specify the AWS PROFILE name using the -profile command line argument!"
        exit 1
    fi
    if [ -z $SUBSYSTEM ]; then
        echo "[`date`] ERROR: Please specify the SUBSYSTEM name using the -subsystem command line argument!"
        exit 1
    fi

    echo "[`date`] INFO: Variables passed $AWS_PROFILE, $AWS_REGION, $SERVICE_STAGE, $SUBSYSTEM"

}

# *************************************************************************************************** #
# Main application entrypoint function.
#
# Arguments:
#   None
# Returns:
#   Success/Failure - 0/1
# *************************************************************************************************** #
main() {

    validate_parameters "$@"

    SUBSYSTEM=$(echo "$SUBSYSTEM" | tr '[:upper:]' '[:lower:]')

    if [ "$?" -ne 0 ]; then
        echo "[`date`] ERROR: Exiting Script - Parameter validation failed!"
        exit 1
    fi

    # Get Functions list from the property file
    FUNCTIONS_LIST=$(prop "$SUBSYSTEM.functions")
    echo "FUNCTIONS_LIST===>>"$FUNCTIONS_LIST

    sls_deploy $SCRIPTPATH/../infrastructure

    if [ "$?" -ne 0 ]; then
        echo "[`date`] ERROR: Exiting Script - Common artifacts deploy failed!"
        exit 1
    fi

    for SVCNAME in $(echo $FUNCTIONS_LIST | tr ',' '\n')
    do
       echo "SVCNAME Deployment Started===>>"$SVCNAME
       SLS_PROJ_WORKSPACE=${SCRIPTPATH%/*}
       SLS_SVCS_WORKSPACE=$(find "$SLS_PROJ_WORKSPACE" -maxdepth 1 -type d -name "*$SUBSYSTEM*" | rev | cut -f1 -d'/' - | rev)
       cat $SCRIPTPATH/../servicesslstemplate.yml $SCRIPTPATH/../$SLS_SVCS_WORKSPACE/*/*/$SVCNAME/deploy.yml > $SCRIPTPATH/../$SLS_SVCS_WORKSPACE/*/*/$SVCNAME/serverless.yml
       sls_deploy $SCRIPTPATH/../$SLS_SVCS_WORKSPACE/*/*/$SVCNAME
       echo "SVCNAME Deployment Completed===>>"$SVCNAME
    done
}

main "$@"