Skip to content

Commit 8af468e

Browse files
authored
Release 6.3.1 - Update Quickstart Demo to use custom Service Account
1 parent cff59c8 commit 8af468e

11 files changed

Lines changed: 227 additions & 115 deletions

File tree

deploy.sh

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,61 @@
1717
# Exit on error.
1818
set -e
1919

20-
log_bucket=$1
20+
build_account=""
21+
log_bucket=""
22+
23+
while [[ $# -gt 0 ]]; do
24+
case "$1" in
25+
--build-account)
26+
build_account="$2"
27+
shift 2
28+
;;
29+
--gcs-logs-bucket)
30+
log_bucket="$2"
31+
shift 2
32+
;;
33+
*)
34+
# Handle other arguments or flags if needed
35+
echo "Unknown option: $1" >&2
36+
shift
37+
;;
38+
esac
39+
done
40+
2141
echo "Deploying Cortex Data Foundation."
2242

2343
cloud_build_project=$(cat "config/config.json" | python3 -c "import json,sys; print(str(json.load(sys.stdin)['projectIdSource']))" 2>/dev/null || echo "")
24-
if [[ "${cloud_build_project}" == "" ]]
25-
then
26-
echo "ERROR: Cortex Data Foundation is not configured."
27-
echo "Please read https://github.com/GoogleCloudPlatform/cortex-data-foundation/blob/main/README.md"
44+
if [[ "${cloud_build_project}" == "" ]]; then
45+
echo "ERROR: Cortex Data Foundation source project is not configured."
46+
echo "Please read https://cloud.google.com/cortex/docs/deployment-prerequisites"
2847
exit 1
2948
fi
49+
3050
echo "Using Cloud Build in project '${cloud_build_project}'"
3151

32-
if [[ "${log_bucket}" == "" ]]
33-
then
52+
if [[ "${log_bucket}" == "" ]]; then
3453
_GCS_BUCKET="${cloud_build_project}_cloudbuild"
3554
else
3655
_GCS_BUCKET="${log_bucket}"
3756
fi
57+
3858
echo "Using logs bucket ${_GCS_BUCKET}"
3959

60+
# Check if bucket already created and exist e.g. if Cloud Build has been used before in the project.
61+
# If it does not exist Cloud Build might not have been called before and the bucket will be created
62+
# with correct permissions
63+
if gcloud storage buckets describe "${_GCS_BUCKET}" --project="$cloud_build_project" >/dev/null 2>&1; then
64+
# Bucket exists we can go ahead and set policy binding
65+
echo "Ensuring ${build_account} has access to bucket ${_GCS_BUCKET}"
66+
gcloud storage buckets add-iam-policy-binding gs://"${_GCS_BUCKET}" --member=serviceAccount:"${build_account}" --role=roles/storage.objectUser
67+
fi
68+
4069
set +e
4170
echo -e "\n\033[0;32m\033[1mPlease wait while Data Foundation is being deployed...\033[0m\n"
4271
gcloud builds submit --config=cloudbuild.yaml --suppress-logs \
4372
--project "${cloud_build_project}" \
44-
--substitutions=_GCS_BUCKET="${_GCS_BUCKET}" . \
45-
&& _SUCCESS="true"
73+
--substitutions=_GCS_BUCKET="${_GCS_BUCKET}",_BUILD_ACCOUNT="projects/${cloud_build_project}/serviceAccounts/${build_account}" . &&
74+
_SUCCESS="true"
4675
if [[ "${_SUCCESS}" != "true" ]]; then
4776
echo -e "\n🛑 Data Foundation deployment has failed. 🛑"
4877
exit 1

src/SAP/SAP_CDC/deploy_cdc.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# limitations under the License.
1616

1717
log_bucket=$1
18+
build_account="$2"
19+
1820
echo "Deploying CDC and extra data."
1921

2022
if [[ "${log_bucket}" == "" ]]
@@ -27,4 +29,5 @@ else
2729
_GCS_BUCKET="${log_bucket}"
2830
fi
2931

30-
gcloud builds submit --config=cloudbuild.cdc.yaml --substitutions=_GCS_BUCKET="${_GCS_BUCKET}" .
32+
gcloud builds submit --config=cloudbuild.cdc.yaml --substitutions=_GCS_BUCKET="${_GCS_BUCKET}",_BUILD_ACCOUNT="${build_account}" .
33+

src/SFDC/deploy.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# assuming the bucket exists.
2121

2222
GCS_LOGS_BUCKET="$1"
23+
BUILD_ACCOUNT="$2"
2324

2425
echo -e "🦄🦄🦄 Running Cortex Data Foundation modules for SalesForce.com 🔪🔪🔪\n"
2526

@@ -33,4 +34,4 @@ fi
3334

3435
gcloud builds submit . \
3536
--config=cloudbuild.sfdc.yaml \
36-
--substitutions=_GCS_LOGS_BUCKET="${GCS_LOGS_BUCKET}"
37+
--substitutions=_GCS_LOGS_BUCKET="${GCS_LOGS_BUCKET}",_BUILD_ACCOUNT="${BUILD_ACCOUNT}"

src/k9/src/meridian/deploy_meridian.sh

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -111,55 +111,53 @@ done
111111

112112
validate_args
113113

114-
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
114+
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
115115

116116
export SOURCE_PROJECT=$(cat ${CONFIG_FILE} | python3 -c "import json,sys; print(str(json.load(sys.stdin)['projectIdSource']))" 2>/dev/null || echo "")
117117
export TARGET_BUCKET=$(cat ${CONFIG_FILE} | python3 -c "import json,sys; print(str(json.load(sys.stdin)['targetBucket']))" 2>/dev/null || echo "")
118-
if [[ "${SOURCE_PROJECT}" == "" ]]
119-
then
120-
echo "ERROR: projectIdSource value in config.json is empty."
121-
exit 1
118+
if [[ "${SOURCE_PROJECT}" == "" ]]; then
119+
echo "ERROR: projectIdSource value in config.json is empty."
120+
exit 1
122121
fi
123-
if [[ "${TARGET_BUCKET}" == "" ]]
124-
then
125-
echo "ERROR: targetBucket value in config.json is empty."
126-
exit 1
122+
if [[ "${TARGET_BUCKET}" == "" ]]; then
123+
echo "ERROR: targetBucket value in config.json is empty."
124+
exit 1
127125
fi
128126

129-
declare -a _WORKER_POOL_OPTIONS
130-
declare -a _REGION_PARAMETER
131-
132-
if [[ -n "${_WORKER_POOL_NAME}" ]]; then
133-
_WORKER_POOL_OPTIONS+=(",_WORKER_POOL_NAME=\"${_WORKER_POOL_NAME}\"")
134-
fi
127+
declare -a _WORKER_POOL_OPTIONS
128+
declare -a _REGION_PARAMETER
135129

136-
if [[ -n "${_CLOUD_BUILD_REGION}" ]]; then
137-
_WORKER_POOL_OPTIONS+=(",_CLOUD_BUILD_REGION=\"${_CLOUD_BUILD_REGION}\"")
138-
_REGION_PARAMETER=(--region "${_CLOUD_BUILD_REGION}")
139-
fi
130+
if [[ -n "${_WORKER_POOL_NAME}" ]]; then
131+
_WORKER_POOL_OPTIONS+=(",_WORKER_POOL_NAME=\"${_WORKER_POOL_NAME}\"")
132+
fi
140133

141-
if [[ -n "${_BUILD_ACCOUNT}" ]]; then
142-
_WORKER_POOL_OPTIONS+=(",_BUILD_ACCOUNT=\"${_BUILD_ACCOUNT}\"")
143-
fi
134+
if [[ -n "${_CLOUD_BUILD_REGION}" ]]; then
135+
_WORKER_POOL_OPTIONS+=(",_CLOUD_BUILD_REGION=\"${_CLOUD_BUILD_REGION}\"")
136+
_REGION_PARAMETER=(--region "${_CLOUD_BUILD_REGION}")
137+
fi
144138

139+
if [[ -n "${_BUILD_ACCOUNT}" ]]; then
140+
_WORKER_POOL_OPTIONS+=(",_BUILD_ACCOUNT=\"${_BUILD_ACCOUNT}\"")
141+
fi
145142

146143
echo "Deploying Meridian"
147144

148145
cp -f "${CONFIG_FILE}" "${SCRIPT_DIR}/config/config.json"
149146
set +e
147+
150148
gcloud builds submit --project="${SOURCE_PROJECT}" \
151-
--config="${SCRIPT_DIR}/cloudbuild.meridian.yaml" \
152-
--substitutions \
153-
_TGT_BUCKET="${TARGET_BUCKET}",_GCS_BUCKET="${GCS_LOGS_BUCKET}" "${_WORKER_POOL_OPTIONS[@]}" \
154-
"${_REGION_PARAMETER[@]}" \
155-
"${SCRIPT_DIR}"
149+
--config="${SCRIPT_DIR}/cloudbuild.meridian.yaml" \
150+
--substitutions \
151+
_TGT_BUCKET="${TARGET_BUCKET}",_GCS_BUCKET="${GCS_LOGS_BUCKET}",_BUILD_ACCOUNT="${_BUILD_ACCOUNT}" \
152+
"${_REGION_PARAMETER[@]}" \
153+
"${SCRIPT_DIR}"
154+
156155
_err=$?
157156
rm -f "${SCRIPT_DIR}/config/config.json"
158157

159-
if [ $_err -ne 0 ]
160-
then
161-
echo "Meridian deployment failed."
162-
exit 1
158+
if [ $_err -ne 0 ]; then
159+
echo "Meridian deployment failed."
160+
exit 1
163161
fi
164162

165-
echo "Meridian has been deployed."
163+
echo "Meridian has been deployed."

src/utils/automatic/deploy_cortex_meridian_1_click.sh

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,31 +174,61 @@ fi
174174

175175
fancy_echo_done "Done creating Service Account"
176176

177+
# Create Cloud Build SA
178+
179+
fancy_echo_start "Creating Cortex Deployer Service Account for Cloud Build"
180+
181+
# Define the service account ID
182+
CLOUD_BUILD_SERVICE_ACCOUNT_ID="cortex-deployer"
183+
184+
# Define the full service account email
185+
CLOUD_BUILD_SERVICE_ACCOUNT_EMAIL="${CLOUD_BUILD_SERVICE_ACCOUNT_ID}@${PROJECT_ID}.iam.gserviceaccount.com"
186+
187+
# Check if the service account exists
188+
if gcloud iam service-accounts list \
189+
--project="$PROJECT_ID" \
190+
--filter="email:${CLOUD_BUILD_SERVICE_ACCOUNT_EMAIL}" \
191+
--format="value(email)" |
192+
grep -q "${CLOUD_BUILD_SERVICE_ACCOUNT_EMAIL}"; then
193+
fancy_sub_echo "Service account '${CLOUD_BUILD_SERVICE_ACCOUNT_EMAIL}' already exists skipping create 🔐"
194+
else
195+
fancy_sub_echo "Creating service account '${CLOUD_BUILD_SERVICE_ACCOUNT_ID}'..."
196+
gcloud iam service-accounts create "${CLOUD_BUILD_SERVICE_ACCOUNT_ID}" --project="$PROJECT_ID" \
197+
--description="Cortex Deployer Service Account" \
198+
--display-name="Cortex Deployer"
199+
if [ $? -eq 0 ]; then
200+
fancy_sub_echo "Service account '${CLOUD_BUILD_SERVICE_ACCOUNT_EMAIL}' created successfully 🔐"
201+
else
202+
fancy_error_echo "Error creating service account '${CLOUD_BUILD_SERVICE_ACCOUNT_ID}' 🔐"
203+
exit 1
204+
fi
205+
fi
206+
207+
fancy_echo_done "Done creating Service Account"
208+
177209
# IAM roles assignments
178210

179-
fancy_echo_start "Assigning IAM roles to Cloud Build Default Service Account & Meridian Colab Runner Service Account"
211+
fancy_echo_start "Assigning IAM roles to Cloud Build Service Account & Meridian Colab Runner Service Account"
180212

181213
CLOUD_BUILD_ROLES=('roles/aiplatform.colabEnterpriseAdmin'
182214
'roles/storage.objectUser' 'roles/workflows.editor' 'roles/bigquery.jobUser'
183-
'roles/bigquery.dataEditor' 'roles/iam.serviceAccountUser')
184-
185-
# Get cloud build service account email
186-
CLOUD_BUILD_SA=$(gcloud builds get-default-service-account --project="$PROJECT_ID" --format='value(serviceAccountEmail)' | sed 's|.*/||')
187-
215+
'roles/bigquery.dataEditor' 'roles/iam.serviceAccountUser' 'roles/logging.logWriter'
216+
'roles/cloudbuild.builds.builder')
217+
188218
for role in "${CLOUD_BUILD_ROLES[@]}"; do
189-
fancy_sub_echo "Assigning role: $role to $CLOUD_BUILD_SA 🔑"
219+
fancy_sub_echo "Assigning role: $role to $CLOUD_BUILD_SERVICE_ACCOUNT_EMAIL 🔑"
190220
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
191-
--member="serviceAccount:$CLOUD_BUILD_SA" \
221+
--member="serviceAccount:$CLOUD_BUILD_SERVICE_ACCOUNT_EMAIL" \
192222
--role="$role" \
193223
--condition=None \
194224
--no-user-output-enabled
195225
if [ $? -ne 0 ]; then
196-
fancy_error_echo "Error assigning role '$role' to service account '$CLOUD_BUILD_SA'"
226+
fancy_error_echo "Error assigning role '$role' to service account '$CLOUD_BUILD_SERVICE_ACCOUNT_EMAIL'"
197227
exit 1
198228
fi
199229
done
200230

201-
fancy_sub_echo "Done assigning roles to service account '$CLOUD_BUILD_SA'"
231+
fancy_sub_echo "Done assigning roles to service account '$CLOUD_BUILD_SERVICE_ACCOUNT_EMAIL'"
202232

203233
MERIDIAN_RUNNER_ROLES=('roles/bigquery.dataViewer' 'roles/bigquery.jobUser'
204234
'roles/bigquery.readSessionUser' 'roles/cloudbuild.builds.editor'
@@ -317,7 +347,7 @@ fancy_sub_echo "Deploying Cortex Data Foundation via Cloud Build👷"
317347

318348
source_project=$(cat "config/config.json" | python3 -c "import json,sys; print(str(json.load(sys.stdin)['projectIdSource']))" 2>/dev/null || echo "")
319349
gcloud config set project "${source_project}"
320-
./deploy.sh
350+
./deploy.sh --build-account "$CLOUD_BUILD_SERVICE_ACCOUNT_EMAIL"
321351
popd 1>/dev/null
322352

323353
fancy_echo_done "Done Cortex Data Foundation was deployed via Cloud Build"

0 commit comments

Comments
 (0)