diff --git a/basis/bin/auto_env.sh b/basis/bin/auto_env.sh index 3d518d4b..f2699323 100755 --- a/basis/bin/auto_env.sh +++ b/basis/bin/auto_env.sh @@ -40,6 +40,9 @@ if [ "$TF_VAR_db_password" == "__TO_FILL__" ]; then echo "> TF_VAR_db_password=$TF_VAR_db_password" fi +# Livelabs Green Button (Autodetect compartment/vcn/subnet) +livelabs_green_button + # -- env.sh # Do not stop if __TO_FILL__ are not replaced if TF_VAR_group_name exist in env variable # XXX -> It would be safer to check also for TF_VAR_xxx containing __TO_FILL__ too diff --git a/basis/bin/shared_bash_function.sh b/basis/bin/shared_bash_function.sh index 32d2e1ab..eed8fb0f 100755 --- a/basis/bin/shared_bash_function.sh +++ b/basis/bin/shared_bash_function.sh @@ -235,6 +235,70 @@ get_ui_url() { fi } +livelabs_green_button() { + # Lot of tests to be sure we are in a empty Green Button LiveLabs + # compartment_ocid still undefined ? + if grep -q '# export TF_VAR_compartment_ocid=ocid1.compartment.xxxxx' $PROJECT_DIR/env.sh; then + # vnc_ocid still undefined ? + if [ "$TF_VAR_vcn_ocid" != "__TO_FILL__" ]; then + # Variables already set + return + fi + # In cloud shell ? + if [ -z $OCI_CLI_CLOUD_SHELL ]; then + return + fi + # Whoami user format ? + if [[ `whoami` =~ ^ll.*_u.* ]]; then + echo "LiveLabs - Green Button - User name in llxxx_uxx format" + else + return + fi + get_user_details + # OCI User name format ? + if [[ $TF_VAR_username =~ ^LL.*-USER$ ]]; then + echo "LiveLabs - Green Button" + else + return + fi + + export USER_BASE=`echo "${TF_VAR_username/-USER/}"` + echo USER_BASE=$USER_BASE + + export TF_VAR_compartment_ocid=`oci iam compartment list --compartment-id-in-subtree true --all | jq -c -r '.data[] | select(.name | contains("'$USER_BASE'")) | .id'` + echo TF_VAR_compartment_ocid=$TF_VAR_compartment_ocid + + if [ "$TF_VAR_compartment_ocid" != "" ]; then + sed -i "s&# export TF_VAR_compartment_ocid=ocid1.compartment.xxxxx&export TF_VAR_compartment_ocid=\"$TF_VAR_compartment_ocid\"&" $PROJECT_DIR/env.sh + echo "TF_VAR_compartment_ocid stored in env.sh" + fi + + export TF_VAR_vcn_ocid=`oci network vcn list --compartment-id $TF_VAR_compartment_ocid | jq -c -r '.data[].id'` + echo TF_VAR_vcn_ocid=$TF_VAR_vcn_ocid + if [ "$TF_VAR_vcn_ocid" != "" ]; then + sed -i "s&TF_VAR_vcn_ocid=\"__TO_FILL__\"&TF_VAR_vcn_ocid=\"$TF_VAR_vcn_ocid\"&" $PROJECT_DIR/env.sh + echo "TF_VAR_vcn_ocid stored in env.sh" + fi + + export TF_VAR_subnet_ocid=`oci network subnet list --compartment-id $TF_VAR_compartment_ocid | jq -c -r '.data[].id'` + echo TF_VAR_subnet_ocid=$TF_VAR_subnet_ocid + if [ "$TF_VAR_subnet_ocid" != "" ]; then + sed -i "s&TF_VAR_public_subnet_ocid=\"__TO_FILL__\"&TF_VAR_public_subnet_ocid=\"$TF_VAR_subnet_ocid\"&" $PROJECT_DIR/env.sh + sed -i "s&TF_VAR_private_subnet_ocid=\"__TO_FILL__\"&TF_VAR_private_subnet_ocid=\"$TF_VAR_subnet_ocid\"&" $PROJECT_DIR/env.sh + echo "TF_VAR_subnet_ocid stored in env.sh" + # Set the real variables such that the first ./build.sh works too. + export TF_VAR_public_subnet_ocid=$TF_VAR_subnet_ocid + export TF_VAR_private_subnet_ocid=$TF_VAR_subnet_ocid + fi + + # LiveLabs support only E4 Shapes + if grep -q '# export TF_VAR_instance_shape=VM.Standard.E4.Flex' $PROJECT_DIR/env.sh; then + sed -i "s&# export TF_VAR_instance_shape=&export TF_VAR_instance_shape=&" $PROJECT_DIR/env.sh + fi + + fi +} + create_deployment_in_apigw() { # Publish the API with an API Deployment to API Gateway if [ "$APIGW_DEPLOYMENT_OCID" != "" ]; then diff --git a/py_oci_starter.py b/py_oci_starter.py index 09fd575f..cdc686f7 100755 --- a/py_oci_starter.py +++ b/py_oci_starter.py @@ -503,6 +503,12 @@ def env_sh_contents(): contents.append(' # API Management') contents.append(' # export APIM_HOST=xxxx-xxx.adb.region.oraclecloudapps.com') contents.append('') + + if params.get('instance_shape') == None: + contents.append(' # Compute Shape') + contents.append(' # export TF_VAR_instance_shape=VM.Standard.E4.Flex') + contents.append('') + contents.append(' # Landing Zone') contents.append(' # export TF_VAR_lz_appdev_cmp_ocid=$TF_VAR_compartment_ocid') contents.append(' # export TF_VAR_lz_database_cmp_ocid=$TF_VAR_compartment_ocid')