-
Notifications
You must be signed in to change notification settings - Fork 500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cleanup of bash scripts #802
Merged
+67
−32
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dc91ecd
Style, safety, and mechanical improvements to bash scripts
kolbe d21ff13
Added better handling for existing credentials
kolbe c2977b8
Merge branch 'master' of github.com:pingcap/tidb-operator into kolbe-…
kolbe 7ed8dce
Merge branch 'master' of github.com:pingcap/tidb-operator into kolbe-…
kolbe 7e8c14d
Merge branch 'master' of github.com:pingcap/tidb-operator into kolbe-…
kolbe d0d8543
Merge branch 'master' of github.com:pingcap/tidb-operator into kolbe-…
kolbe fbab0dd
Merge branch 'master' of github.com:pingcap/tidb-operator into kolbe-…
kolbe bc54083
Added further checks for existing files and state
kolbe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,45 +7,72 @@ | |
# so it cannot just be added to the existing terraform. | ||
|
||
set -euo pipefail | ||
cd "$(dirname "$0")" | ||
PROJECT="${TF_VAR_GCP_PROJECT:-$(cat terraform.tfvars | awk -F '=' '/GCP_PROJECT/ {print $2}' | cut -d '"' -f 2)}" | ||
echo "using project: $PROJECT" | ||
|
||
cred_file=credentials.auto.tfvars | ||
if test -f "$cred_file" ; then | ||
if cat "$cred_file" | awk -F'=' '/GCP_CREDENTIALS/ {print $2}' >/dev/null ; then | ||
echo "GCP_CREDENTAILS_PATH already set in $cred_file" | ||
if ! cd "$(dirname "$0")"; then | ||
printf "Could not change to base directory of script." >&2 | ||
exit 1 | ||
fi | ||
fi | ||
|
||
GCLOUD="gcloud --project $PROJECT" | ||
if ! [[ -d .terraform ]]; then | ||
echo "no .terraform directory, perhaps you need to run ''terraform init''?" >&2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the quoting looked a little odd to me, but no big deal. |
||
exit 1 | ||
fi | ||
|
||
if ! project=${TF_VAR_GCP_PROJECT:-$( echo var.GCP_PROJECT | terraform console 2>/dev/null )} || [[ -z $project ]]; then | ||
echo "could not identify current project; set GCP_PROJECT in a .tfvars file or set the TF_VAR_GCP_PROJECT environment variable" >&2 | ||
exit 1 | ||
fi | ||
echo "using project: $project" | ||
|
||
cred_file=credentials.auto.tfvars | ||
|
||
if cred_path=$( echo var.GCP_CREDENTIALS_PATH | terraform console 2>/dev/null ) && [[ $cred_path ]]; then | ||
if ! command -v jq >/dev/null; then | ||
echo "GCP_CREDENTAILS_PATH already set to $cred_path and jq(1) is not installed to ensure it is for project $project" >&2 | ||
exit 1 | ||
elif cred_project=$(jq -r .project_id "$cred_path" 2>/dev/null) && [[ $cred_project != "$project" ]]; then | ||
echo "GCP_CREDENTAILS_PATH already set to $cred_path but credentials project $cred_project does not match current project $project" >&2 | ||
exit 1 | ||
elif ! [[ -f $cred_path ]]; then | ||
echo "GCP_CREDENTAILS_PATH already set, but $cred_path doesn't exist" >&2 | ||
else | ||
echo "GCP_CREDENTAILS_PATH already set to $cred_path for project $project" >&2 | ||
exit | ||
fi | ||
fi | ||
|
||
gcloud=( gcloud --project "$project" ) | ||
|
||
mkdir -p credentials | ||
key_file=credentials/terraform-key.json | ||
email="terraform@${PROJECT}.iam.gserviceaccount.com" | ||
email="terraform@${project}.iam.gserviceaccount.com" | ||
|
||
sas=$($GCLOUD iam service-accounts list) | ||
if echo "$sas" | grep terraform >/dev/null ; then | ||
if test -f $key_file && grep "$PROJECT" $key_file >/dev/null ; then | ||
echo "service account terraform already exists along with the key file. Will set terraform variables" | ||
if [[ $("${gcloud[@]}" --format='value(name)' iam service-accounts list --filter='displayName~^terraform$') ]]; then | ||
if grep -sq "$project" "$key_file"; then | ||
echo "service account terraform already exists along with the key file, will set terraform variables" | ||
else | ||
echo "service account terraform already exists, will get a key for it" | ||
$GCLOUD iam service-accounts keys create $key_file --iam-account "$email" | ||
"${gcloud[@]}" iam service-accounts keys create "$key_file" --iam-account "$email" | ||
fi | ||
else | ||
echo "creating a new service account terraform" | ||
$GCLOUD iam service-accounts create --display-name terraform terraform | ||
$GCLOUD iam service-accounts keys create $key_file --iam-account "$email" | ||
"${gcloud[@]}" iam service-accounts create --display-name terraform terraform | ||
"${gcloud[@]}" iam service-accounts keys create "$key_file" --iam-account "$email" | ||
fi | ||
|
||
chmod 0600 $key_file | ||
chmod 0600 "$key_file" | ||
|
||
$GCLOUD projects add-iam-policy-binding "$PROJECT" --member "serviceAccount:$email" --role roles/container.clusterAdmin | ||
$GCLOUD projects add-iam-policy-binding "$PROJECT" --member "serviceAccount:$email" --role roles/compute.networkAdmin | ||
$GCLOUD projects add-iam-policy-binding "$PROJECT" --member "serviceAccount:$email" --role roles/compute.viewer | ||
$GCLOUD projects add-iam-policy-binding "$PROJECT" --member "serviceAccount:$email" --role roles/compute.securityAdmin | ||
$GCLOUD projects add-iam-policy-binding "$PROJECT" --member "serviceAccount:$email" --role roles/iam.serviceAccountUser | ||
$GCLOUD projects add-iam-policy-binding "$PROJECT" --member "serviceAccount:$email" --role roles/compute.instanceAdmin.v1 | ||
roles=( | ||
roles/container.clusterAdmin | ||
roles/compute.networkAdmin | ||
roles/compute.viewer | ||
roles/compute.securityAdmin | ||
roles/iam.serviceAccountUser | ||
roles/compute.instanceAdmin.v1 | ||
) | ||
for role in "${roles[@]}"; do | ||
"${gcloud[@]}" projects add-iam-policy-binding "$project" --member "serviceAccount:$email" --role "$role" | ||
done | ||
|
||
echo GCP_CREDENTIALS_PATH="\"$(pwd)/$key_file\"" > "$cred_file" | ||
printf 'GCP_CREDENTIALS_PATH = "%s/%s"\n' "$PWD" "$key_file" > "$cred_file" | ||
echo "Successfully wrote credentials to $key_file and configuration to $cred_file" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way the docs are written now, someone would run this before running
terraform init
. https://pingcap.com/docs/v3.0/tidb-in-kubernetes/deploy/gcp-gke/There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new approach uses
terraform console
which is much more reliable. We can update the code snippet in the docs to haveterraform init
before running this.