ssh-keygen -y -f ~/Downloads/ssh-key-2024-08-20.key > ~/Downloads/ssh-key-2024-08-20.pub
Known issue with Python 3.12 in Ubuntu 24.04
# as root
# ModuleNotFoundError: No module named 'distutils'
apt install -y python3-venv python3-setuptools python3-pip python3-distutils-extra python3-setuptools-whl
curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh -o install.sh
bash ./install.sh
oci iam compartment list --include-root
https://docs.oracle.com/en-us/iaas/Content/ResourceManager/Tasks/create-csp-github.htm#top
To give access to a private repository, you need to generate a personal access token and use it in your commands
The scope repo (which includes repo:status, repo_deployment, and public_repo) is required for use with Resource Manager. See https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token
https://docs.oracle.com/en-us/iaas/Content/ResourceManager/Tasks/create-stack-git.htm#top
echo Enter Github token
read GITHUBTOKEN
STACKNAME=mail
GITHUBUSER=dcox761
GITHUBREPO=oci-mail
VARFILE=variables.json
KEYFILE=public_key.pem
GITHUBBASE=https://github.com
GITHUBURL=$GITHUBBASE/$GITHUBUSER/$GITHUBREPO.git
# pick the root compartment instead
COMPARTMENT_ID=$OCI_TENANCY
#COMPARTMENT_ID=$(oci iam compartment list \
# --all --query 'data[0]."compartment-id"' --raw-output)
echo "Compartment OCID: $COMPARTMENT_ID"
oci resource-manager configuration-source-provider create-github-access-token-provider \
--compartment-id $COMPARTMENT_ID \
--access-token $GITHUBTOKEN \
--api-endpoint $GITHUBBASE \
--display-name $GITHUBUSER
CONFIG_SOURCE_PROVIDER_JSON=$(oci resource-manager configuration-source-provider list \
--compartment-id $COMPARTMENT_ID)
CONFIG_SOURCE_PROVIDER_OCID=$(echo $CONFIG_SOURCE_PROVIDER_JSON | jq -r '.data.items[] | select(.["display-name"] == "'$GITHUBUSER'") | .id')
echo "Configuration Source Provider OCID: $CONFIG_SOURCE_PROVIDER_OCID"
oci resource-manager stack create-from-git-provider \
--compartment-id $COMPARTMENT_ID \
--display-name $STACKNAME \
--config-source-configuration-source-provider-id $CONFIG_SOURCE_PROVIDER_OCID \
--config-source-repository-url $GITHUBURL \
--config-source-branch-name main \
--terraform-version "1.5.x"
After creating the stack, you can set variables using the following commands:
STACK_ID=$(oci resource-manager stack list \
--compartment-id $COMPARTMENT_ID \
--display-name $STACKNAME \
--query 'data[0].id' --raw-output)
echo "Stack OCID: $STACK_ID"
To set the stack_id
variable in the stack along with variables from a file, you can use the following commands:
CURRENT_VARIABLES_JSON=$(oci resource-manager stack get --stack-id $STACK_ID --query 'data.variables' --raw-output)
#MERGED_VARIABLES_JSON=$(jq -s '.[0] * .[1]' <(echo $CURRENT_VARIABLES_JSON) $VARFILE)
# merge with stack_id
#MERGED_VARIABLES_JSON=$(jq -s --arg stack_id "$STACK_ID" '.[0] * .[1] | .stack_id = $stack_id' <(echo "$CURRENT_VARIABLES_JSON") $VARFILE)
# merge with tenancy_ocid
MERGED_VARIABLES_JSON=$(jq -s --arg tenancy_ocid "$OCI_TENANCY" '.[0] * .[1] | .tenancy_ocid = $tenancy_ocid' <(echo "$CURRENT_VARIABLES_JSON") $VARFILE)
# merge with SSH public key
SSH_PUBLIC_KEY=$(cat $KEYFILE)
MERGED_VARIABLES_JSON=$(echo "$MERGED_VARIABLES_JSON" | jq --arg ssh_public_key "$SSH_PUBLIC_KEY" '. + {ssh_public_key: $ssh_public_key}')
oci resource-manager stack update --force \
--stack-id $STACK_ID \
--variables "$MERGED_VARIABLES_JSON"
# Create a plan job to review the changes
PLAN_JOB_ID=$(oci resource-manager job create --stack-id $STACK_ID --operation PLAN --query 'data.id' --raw-output)
echo "Plan job created with ID: $PLAN_JOB_ID"
# Wait for the plan job to complete
while true; do
JOB_STATUS=$(oci resource-manager job get --job-id $PLAN_JOB_ID --query 'data."lifecycle-state"' --raw-output)
if [[ "$JOB_STATUS" == "SUCCEEDED" || "$JOB_STATUS" == "FAILED" ]]; then
break
fi
echo "Waiting for plan job to complete..."
sleep 10
done
echo "Plan job completed with status: $JOB_STATUS"
# Retrieve the plan job details
oci resource-manager job get --job-id $PLAN_JOB_ID
# Retrieve and display the plan job logs
oci resource-manager job get-job-logs-content --job-id $PLAN_JOB_ID > /tmp/plan_job_logs.txt
echo -e "$(cat /tmp/plan_job_logs.txt)" | less
# Prompt the user to apply the changes
read -p "Do you want to apply these changes? (yes/no): " APPLY_CHANGES
if [ "$APPLY_CHANGES" == "yes" ]; then
# Apply the stack
# Option to wait here --wait-for-state SUCCEEDED --wait-for-state FAILED
APPLY_JOB_ID=$(oci resource-manager job create-apply-job --stack-id $STACK_ID --execution-plan-strategy FROM_PLAN_JOB_ID --execution-plan-job-id "$PLAN_JOB_ID" --wait-for-state SUCCEEDED --query 'data.id' --raw-output)
echo "Apply job created with ID: $APPLY_JOB_ID"
else
echo "Changes not applied."
fi