diff --git a/common/docs/DeployFunctions.md b/common/docs/DeployFunctions.md new file mode 100644 index 000000000..b3531d790 --- /dev/null +++ b/common/docs/DeployFunctions.md @@ -0,0 +1,47 @@ +_Copyright (c) 2019, 2020, 2021 Oracle and/or its affiliates The Universal Permissive License (UPL), Version 1.0_ + +# Deployment Functions + +The deployment helper functions are defined in common/utils/deploy-functions.env and sourced by common/source.env. They are used to streamline the deployment of microservices. + +## k8s_deploy + +Deploy a set of yaml files. The function accepts a single parameter that represents a space separate list of yaml files to be deployed, for example: + +``` +k8s-deploy 'inventory-helidon-deployment.yaml inventory-service.yaml' +``` + +### Internals + +kubectl apply is used to deploy the files and so the k8s_deploy function is idempotent. + +The yaml files are expected to be in the same folder as the script that invokes this function. + +The yaml files are processed to substitute environment variables before they are deployed. The processed yaml files are place in the .deployed folder in the home directory of the microservice. + +The syntax available to define substitution variable is as follows: + +- **${VAR}** Fails if VAR is undefined, else is substitutes the value of VAR +- **${VAR-default}** If VAR is not defined substitute 'default', else substitutes the value of VAR +- **${VAR-}** If VAR is not defined substitute '', else it substitutes the value of VAR +- **${VAR?MSG}** Fails if VAR is undefined with the error 'MSG', else it substitutes value of VAR +- **${VAR:-default}** If VAR is not defined or empty, substitute 'default', else it substitutes the value of VAR +- **${VAR:?MSG}** Fails if VAR is undefined or empty with the error 'MSG', else it substitutes the value of VAR + +Here is an example: + +``` +- name: OCI_REGION + value: "${OCI_REGION-}" +``` + +## k8s_undeploy + +Undeploy the yaml files what were deployed by k82_deploy. The files are undeployed in the reverse order. + +### Internals + +The yaml files of the deployed resources are found in the .deployed folder. The files are deleted once thay have been undeployed. + +kubectl delete is used to undeploy the resources. diff --git a/grabdish/config/requirements.env b/grabdish/config/requirements.env index cd41fae43..fb1fabde6 100644 --- a/grabdish/config/requirements.env +++ b/grabdish/config/requirements.env @@ -3,7 +3,7 @@ # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. # Variables to be set in input.env: -# DB_DEPLOYMENT: 2db +# DB_DEPLOYMENT: 1DB 2DB # DB_TYPE: atp # QUEUE_TYPE: classicq # DB_PASSWORD_SECRET: Name of the secret holding the database password access by function get_secret @@ -18,4 +18,4 @@ REQ_UTILS="touch cat rm sqlplus grep" REQ_INPUT_PARAMS="DB_DEPLOYMENT DB_TYPE QUEUE_TYPE DB_PASSWORD_SECRET UI_PASSWORD_SECRET DB1_NAME DB1_TNS_ADMIN DB1_ALIAS DB2_NAME DB2_TNS_ADMIN DB2_ALIAS CWALLET_OS_BUCKET OCI_REGION GRABDISH_LOG" -REQ_OUTPUT_PARAMS="ORDER_DB_NAME ORDER_DB_ALIAS ORDER_DB_TNS_ADMIN INVENTORY_DB_NAME INVENTORY_DB_ALIAS INVENTORY_DB_TNS_ADMIN" \ No newline at end of file +REQ_OUTPUT_PARAMS="ORDER_DB_NAME ORDER_DB_ALIAS ORDER_DB_TNS_ADMIN INVENTORY_DB_NAME INVENTORY_DB_ALIAS INVENTORY_DB_TNS_ADMIN" diff --git a/grabdish/docs/Build.md b/grabdish/docs/Build.md new file mode 100644 index 000000000..0624de6a4 --- /dev/null +++ b/grabdish/docs/Build.md @@ -0,0 +1,36 @@ +_Copyright (c) 2019, 2020, 2021 Oracle and/or its affiliates The Universal Permissive License (UPL), Version 1.0_ + +# Grabdish Build Process + +It is necessary to build the grabdish microservices before they can be deployed. Each microservice has its own build script. A build script compiles the code, constructs a docker image, and pushes the image to the repository. The image is used when deploying the microservice. It is possible to run the builds and provisioning in parallel to save time, however, building more than one java based microservice in parallel has been found to be unreliable and so is not recommended. + +## Prerequisites + +The following are required before building a Grabdish microservice: + +1. Java installed +2. Docker installed +3. Docker image repository prepared +4. Software cloned + +## Environment Variables + +To build grabdish services the following environment variables must be exported: + +DOCKER_REGISTRY +JAVA_HOME +PATH=${JAVA_HOME}/bin:$PATH + +## Steps + +1. Change to a service's home folder, for example: + +``` +cd inventory-helidon +``` + +2. Execute the build script: + +``` +./build.sh +``` diff --git a/grabdish/docs/Deploy.md b/grabdish/docs/Deploy.md new file mode 100644 index 000000000..974343fdc --- /dev/null +++ b/grabdish/docs/Deploy.md @@ -0,0 +1,56 @@ +_Copyright (c) 2019, 2020, 2021 Oracle and/or its affiliates The Universal Permissive License (UPL), Version 1.0_ + +# Grabdish Deployment + +## Environment Variables + +To deploy grabdish services the following environment variables must be exported. Note, most of these are outputs from the provisioning process: + +- ORDER_DB_NAME +- ORDER_DB_TNS_ADMIN +- ORDER_DB_ALIAS +- INVENTORY_DB_NAME +- INVENTORY_DB_TNS_ADMIN +- INVENTORY_DB_ALIAS +- OCI_REGION: OCI vault only +- VAULT_SECRET_OCID: OCI vault only + +## Deploy Steps + +1. Source the common environment: + + source microservices-datadriven/common/source.env + +2. Change to a microservice's home folder, for example: + +``` +cd inventory-helidon +``` + +3. Execute the deploy script: + +``` +./deploy.sh +``` + +## Undeploy Steps + +1. Source the common environment: + + source microservices-datadriven/common/source.env + +2. Change to a microservice's home folder, for example: + +``` +cd inventory-helidon +``` + +3. Execute the undeploy script: + +``` +./undeploy.sh +``` + +## Internals + +The deploy.sh and undeploy.sh scripts use the k8s_deploy andf k8s_undeploy helper functions respectively, defined in common/utils/deploy-functions.env and sourced by common/source.env. See common/docs/DeployFunctions.md for details. diff --git a/grabdish/docs/ProvisionBuildDeploy.md b/grabdish/docs/ProvisionBuildDeploy.md deleted file mode 100644 index 472b9b195..000000000 --- a/grabdish/docs/ProvisionBuildDeploy.md +++ /dev/null @@ -1,152 +0,0 @@ -_Copyright (c) 2019, 2020, 2021 Oracle and/or its affiliates The Universal Permissive License (UPL), Version 1.0_ - -# Grabdish Application Provisioning, Build and Deployment - -These notes assume that you are running on a supported shell (Cloud shell, Linux or macOS) and have cloned the code. - -## Provisioning - -The code that provisions the Grabdish application is located in the grabdish/config folder. The grabdish application is designed to work on any Kubernetes cluster and database. This provisioning is performed automatically when provisioning the dcms-oci workshop. - -### Prerequisites - -The following are required before provisoning grabdish: -1. Kubernetes cluster with kuebctl configured -2. One or two databases -3. An OCI object store bucket (ATP only) -4. get_secret bash function (available in infra/vault/folder) -5. sqlplus installed - -### Input Parameters - -To provision grabdish the following parameters are required: - -DB_DEPLOYMENT: 1db (future) or 2db -DB_TYPE: sa (stand alone, future) or atp -QUEUE_TYPE: classicq or teq (future) -DB_PASSWORD_SECRET: Name of the secret holding the database password accessed by function get_secret (see below) -UI_PASSWORD_SECRET: Name of the secret holding the UI password accessed by function get_secret (see below) -DB1_TNS_ADMIN: Folder containing DB1's TNS_ADMIN folder -DB1_ALIAS: TNS alias of DB1 -DB2_TNS_ADMIN: Folder containing DB2's TNS_ADMIN folder (null for 1db only) -DB2_ALIAS: TNS alias of DB2 (null for 1db only) -CWALLET_OS_BUCKET: Bucket where the wallet token can be installed (null if not ATP) -OCI_REGION: The name of the OCI_REGION. Used to construct a URL for wallet tokens (null if not ATP) -GRABDISH_LOG: Location to create log files - -### Secret management - -The bash get_secret function must be exported and return the DB and UI passwords when called, for example: - - DB_PASSWORD=$(get_secret DB_PASSWORD_SECRET). - -### Outputs - -The output of provisioning provides the following environment variables: - -ORDER_DB_NAME -ORDER_DB_ALIAS -ORDER_DB_TNS_ADMIN -INVENTORY_DB_NAME: -INVENTORY_DB_ALIAS: -INVENTORY_DB_TNS_ADMIN: - -### Provisioning Steps - -1. Source the common environment: - - source microservices-datadriven/common/source.env - -2. Create a folder to store the provisioning state and make this the current directory, for example: - - STATE='grabdish' - mkdir -p $STATE - cd $STATE - -3. Create an input.env file containing the parameters in bash source file format, for axample: - -cat >$STATE/input.env <$STATE/input.env </dev/null cat >$OUTPUT_FILE </dev/null 2>&1; then + return 1 + fi } # Test if the secret exists function is_secret_set() { local SECRET_NAME="$1" local SECRET_VALUE - if ! SECRET=$(oci os object get --bucket-name $VAULT_BUCKET --name "$SECRET_NAME" --file -); then + if ! SECRET=$(oci os object get --bucket-name $VAULT_BUCKET --name "$SECRET_NAME" --file - >/dev/null 2>&1); then return 1 fi } @@ -22,12 +24,12 @@ function is_secret_set() { function get_secret() { local SECRET_NAME="$1" local SECRET_VALUE - if SECRET_VALUE=$(oci os object get --bucket-name $VAULT_BUCKET --name "$SECRET_NAME" --file -); then + if SECRET_VALUE=$(oci os object get --bucket-name $VAULT_BUCKET --name "$SECRET_NAME" --file - 2>/dev/null); then echo "$SECRET_VALUE" else return 1 fi -) +} # Export the functions so that they are available to subshells export -f set_secret diff --git a/workshops/dcms-oci/config/threads/db/apply.sh b/workshops/dcms-oci/config/threads/db/apply.sh index c58fa7e5e..4bfcfd224 100755 --- a/workshops/dcms-oci/config/threads/db/apply.sh +++ b/workshops/dcms-oci/config/threads/db/apply.sh @@ -118,7 +118,7 @@ VCN_OCID=$(state_get VCN_OCID) state_set DB1_ALIAS "$DB_ALIAS" state_set DB2_OCID '' state_set DB2_TNS_ADMIN '' - state_set DB1_ALIAS '' + state_set DB2_ALIAS '' ) ;; 2DB) diff --git a/workshops/dcms-oci/config/threads/db/destroy.sh b/workshops/dcms-oci/config/threads/db/destroy.sh index 9f3906576..a8311c980 100755 --- a/workshops/dcms-oci/config/threads/db/destroy.sh +++ b/workshops/dcms-oci/config/threads/db/destroy.sh @@ -30,6 +30,9 @@ done DBS="db1 db2" for db in $DBS; do db_upper=`echo $db | tr '[:lower:]' '[:upper:]'` + if ! state_set ${db_upper}_OCID || test -z "$(state_get ${db_upper}_OCID)"; then + continue + fi if ! test $(state_get RUN_TYPE) == "LL"; then DB_STATE=$DCMS_INFRA_STATE/db/$db cd $DB_STATE diff --git a/workshops/dcms-oci/setup.sh b/workshops/dcms-oci/setup.sh index fc9c6159b..bcba6890d 100755 --- a/workshops/dcms-oci/setup.sh +++ b/workshops/dcms-oci/setup.sh @@ -313,11 +313,11 @@ case "$DCMS_VAULT_STATUS" in applying-failed | new) # Start or restart the vault setup cd $DCMS_VAULT - cat input.env <input.env <