1+ #! /bin/bash
2+ # Copyright (c) 2021 Oracle and/or its affiliates.
3+ # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
4+
5+
6+ if ! (return 0 2> /dev/null); then
7+ echo " ERROR: Usage 'source setup_functions.env'"
8+ return 1
9+ fi
10+
11+ # Request compartment details and create or validate
12+ # Parameters:
13+ # $1: Run Type: LL or OT (required)
14+ # $2: The tenancy OCID (required)
15+ # $3: Home region (required)
16+ # $4: The description text of the compartment if it is created (required)
17+ #
18+ # Input Environment variables:
19+ # TEST_COMPARTMENT: Compartment name or OCID to use (otional)
20+ # TEST_PARENT_COMPARTMENT_OCID: Parent compartment OCID (optional)
21+ #
22+ # Output Environment variables:
23+ # COMPARTMENT_OCID: Returned compartment OCID
24+ #
25+ function compartment-dialog() {
26+ set -u
27+ COMPARTMENT_OCID=' ' # Updated in calling shell, not local
28+ local COMP
29+ local PARENT_COMP
30+ local RUN_TYPE=" $1 "
31+ local TENANCY_OCID=" $2 "
32+ local HOME_REGION=" $3 "
33+ local COMP_DESC=" $4 "
34+
35+ while test -z " $COMPARTMENT_OCID " ; do
36+ if test " $RUN_TYPE " == ' LL' ; then
37+ # The compartment is already created. Ask for the OCID
38+ read -p " Please enter your OCI compartment's OCID: " COMP
39+ if ! oci iam compartment get --compartment-id " $COMP " 2>&1 > ~/comp_ocid_err; then
40+ echo " ERROR: The compartment $COMP does not exist. Please retry."
41+ cat ~ /comp_ocid_err
42+ rm ~ /comp_ocid_err
43+ continue
44+ else
45+ rm ~ /comp_ocid_err
46+ COMPARTMENT_OCID=" $COMP "
47+ break
48+ fi
49+ fi
50+
51+ if ! test -z " ${TEST_COMPARTMENT-} " ; then
52+ COMP=" $TEST_COMPARTMENT "
53+ else
54+ echo ' Please enter the OCI compartment where you would like the workshop resources to be created.'
55+ echo ' For an existing compartment, enter the OCID. For a new compartment, enter the name.'
56+ read -p " Please specify the compartment: " COMP
57+ fi
58+
59+ if test -z " $COMP " ; then
60+ echo " ERROR: No compartment specified"
61+ continue
62+ fi
63+
64+ if [[ " $COMP " =~ ocid1.* ]]; then
65+ # An existing compartment
66+ if ! oci iam compartment get --compartment-id " $COMP " 2>&1 > ~/comp_ocid_err; then
67+ echo " ERROR: The compartment $COMP does not exist. Please retry."
68+ cat ~ /comp_ocid_err
69+ rm ~ /comp_ocid_err
70+ if ! test -z " ${TEST_COMPARTMENT-} " ; then
71+ return 1
72+ fi
73+ continue
74+ else
75+ rm ~ /comp_ocid_err
76+ COMPARTMENT_OCID=" $COMP "
77+ break
78+ fi
79+ fi
80+
81+ # New compartment
82+ if ! test -z " ${TEST_PARENT_COMPARTMENT_OCID-} " ; then
83+ PARENT_COMP=" $TEST_PARENT_COMPARTMENT_OCID "
84+ else
85+ echo ' Please enter the OCID of the compartment in which you would like the new compartment to be created.'
86+ read -p " Please specify the parent compartment OCID (hit return for the root compartment): " PARENT_COMP
87+ if [[ ! " $PARENT_COMP " =~ ocid1.* ]]; then
88+ PARENT_COMP=" $TENANCY_OCID "
89+ fi
90+ fi
91+
92+ COMPARTMENT_OCID=` oci iam compartment create --region " $HOME_REGION " --compartment-id " $PARENT_COMP " --name " $COMP " --description " $COMP_DESC " --query ' data.id' --raw-output`
93+ done
94+
95+ # Wait for the compartment to become active
96+ while ! test ` oci iam compartment get --compartment-id " $COMPARTMENT_OCID " --query ' data."lifecycle-state"' --raw-output 2> /dev/null` " " == ' ACTIVE' ; do
97+ echo " Waiting for the compartment to become ACTIVE"
98+ sleep 5
99+ done
100+ return 0
101+ }
102+ export -f compartment-dialog
103+
104+
105+ # Setup the state store
106+ # Parameters:
107+ # $1: Full hierarchical name of the state store folder (required)
108+ # $2: Log file (required)
109+ #
110+ # Output:
111+ # Exported state functions
112+ #
113+ function state-store-setup () {
114+ set -u
115+ local STATE_STORE_FOLDER=" $1 "
116+ local STATE_STORE_LOG=" $2 "
117+ local STATE_STORE_STATUS
118+
119+ if ! STATE_STORE_STATUS=$( provisioning-get-status $STATE_STORE_FOLDER ) ; then
120+ echo " ERROR: Unable to get workshop state store status"
121+ return 1
122+ fi
123+
124+ case " $STATE_STORE_STATUS " in
125+
126+ applied | byo)
127+ # Nothing to do
128+ ;;
129+
130+ applying)
131+ # Setup already running so exit
132+ echo " ERROR: Setup is already running and so cannot run setup"
133+ return 1
134+ ;;
135+
136+ destroying-failed | destroying | destroyed)
137+ # Cannot setup during destroy phase
138+ echo " ERROR: Destroy is running and so cannot run setup"
139+ return 1
140+ ;;
141+
142+ applying-failed | new)
143+ # Start or restart the state_store setup
144+ cd $STATE_STORE_FOLDER
145+ echo " STATE_LOG='$STATE_STORE_LOG '" > $STATE_STORE_FOLDER /input.env
146+ if ! provisioning-apply $MSDD_INFRA_CODE /state_store; then
147+ echo " ERROR: Failed to create state_store in $STATE_STORE_FOLDER "
148+ return 1
149+ fi
150+ ;;
151+
152+ esac
153+ source $STATE_STORE_FOLDER /output.env
154+ return 0
155+ }
156+ export -f state-store-setup
157+
158+
159+ # Setup folder based vault
160+ # Parameters:
161+ # $1: Full hierarchical name of the vault state folder (required)
162+ # Output:
163+ # Exported vault functions
164+ #
165+ function folder-vault-setup () {
166+ set -u
167+ local VAULT_STATE_FOLDER=" $1 "
168+ local VAULT_STATUS
169+
170+ if ! VAULT_STATUS=$( provisioning-get-status " $VAULT_STATE_FOLDER " ) ; then
171+ echo " ERROR: Unable to get workshop vault status"
172+ return 1
173+ fi
174+
175+ case " $VAULT_STATUS " in
176+
177+ applied | byo)
178+ # No setup to do
179+ ;;
180+
181+ applying)
182+ # Setup already running so exit
183+ echo " ERROR: Setup is already running and so cannot run setup"
184+ return 1
185+ ;;
186+
187+ destroying-failed | destroying | destroyed)
188+ # Cannot setup during destroy phase
189+ echo " ERROR: Destroy is running and so cannot run setup"
190+ return 1
191+ ;;
192+
193+ applying-failed | new)
194+ # Start or restart the vault setup
195+ cd $VAULT_STATE_FOLDER
196+ if ! provisioning-apply $MSDD_INFRA_CODE /vault/folder; then
197+ echo " ERROR: Failed to create vault in $VAULT_STATE_FOLDER "
198+ return 1
199+ fi
200+ ;;
201+
202+ esac
203+ source $VAULT_STATE_FOLDER /output.env
204+ }
205+ export -f folder-vault-setup
0 commit comments