Skip to content
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

Revert to old tacos unlock #197

Open
wants to merge 54 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
83fe509
reverting back to old version of tacos unlock
kneeyo1 Mar 22, 2024
c0f3df7
auto-commit: GHA deps, for test
Mar 22, 2024
14e7b62
add back terragrunt-slices
kneeyo1 Mar 22, 2024
cb5ef3a
this might look cleaner
kneeyo1 Mar 22, 2024
0342f31
revert a bit more, no more fan in summary
kneeyo1 Mar 22, 2024
fc71f09
chmod
kneeyo1 Mar 22, 2024
c2b327f
triyng this script
kneeyo1 Mar 25, 2024
f8c4f63
correctly run tf-lock-release
kneeyo1 Mar 25, 2024
6437617
this gets all slices and not just changed files
kneeyo1 Mar 25, 2024
30c2058
fully remove the matrix workflow and do ALL slices
kneeyo1 Mar 25, 2024
9df1217
remove unused var
kneeyo1 Mar 25, 2024
a97bdc7
needs the matrix for setup
kneeyo1 Mar 25, 2024
300934c
lets get this bread
kneeyo1 Mar 26, 2024
5f12a1e
remove the matrix stuff, add conditional for set-workload-identity-pr…
kneeyo1 Mar 26, 2024
978a602
clean code is happy code
kneeyo1 Mar 26, 2024
6fd1fef
add some checks for tf root module
kneeyo1 Mar 26, 2024
0a7d974
bash tricks
kneeyo1 Mar 26, 2024
d4e8925
workload-identity-provider gets passed quite a bit of stuff
kneeyo1 Mar 26, 2024
0b92ac5
trying this one more time, it should work
kneeyo1 Mar 26, 2024
2420197
new setup
kneeyo1 Mar 27, 2024
b4a2c39
update pr comment
kneeyo1 Mar 27, 2024
dca9a10
chmod
kneeyo1 Mar 28, 2024
8e8411f
more chmod
kneeyo1 Mar 28, 2024
9b9ca00
i think this is supposed to be a pipe
kneeyo1 Mar 28, 2024
3884c3e
added terragrunt and terraform setup
kneeyo1 Mar 28, 2024
86b0919
clean up ocid_provider call
kneeyo1 Mar 28, 2024
2b46ef7
json dumps needs list
kneeyo1 Mar 28, 2024
d3194b1
trying this out to make json happy
kneeyo1 Mar 29, 2024
877789e
properly set the output
kneeyo1 Mar 29, 2024
bde3cf1
this should unlock all slices, matrix style
kneeyo1 Mar 29, 2024
2951c83
if statement might be broken. fixed
kneeyo1 Mar 29, 2024
a80ae1a
add debugging step
kneeyo1 Mar 29, 2024
d07cc00
im missing a pipe
kneeyo1 Mar 29, 2024
d9b64d2
this might be why
kneeyo1 Mar 29, 2024
c613126
stealing all the setup tasks
kneeyo1 Mar 29, 2024
0d790b3
custom setup probably..
kneeyo1 Mar 29, 2024
cf9ba9c
# This is a combination of 7 commits.
kneeyo1 Mar 29, 2024
e7debd9
direct add in the editted files using slices.py
kneeyo1 Apr 2, 2024
a7b3100
simplified tf_lock_release
kneeyo1 Apr 4, 2024
5769d78
correct details for failure
kneeyo1 Apr 4, 2024
48a5276
refactored to create basic setup derived from setup
kneeyo1 Apr 4, 2024
c24d29d
removing one more step
kneeyo1 Apr 4, 2024
4901cbc
add gcp auth
kneeyo1 Apr 4, 2024
bffda88
add the inputs
kneeyo1 Apr 4, 2024
3e27545
give it the SA as input
kneeyo1 Apr 4, 2024
8344b9e
need to pass in the ssh-private-key
kneeyo1 Apr 4, 2024
182cd52
trying this first
kneeyo1 Apr 4, 2024
337b475
write out debug msg for UserError
kneeyo1 Apr 5, 2024
11d45bd
tf lock acquire exit is not consistent, tthis fixes it
kneeyo1 Apr 5, 2024
ae17c2d
make tf-lock use terragrunt-noninteractive
bukzor Apr 5, 2024
de52de6
fixed working dir
kneeyo1 Apr 5, 2024
4d981d1
Merge branch 'main' into fix/unlock-all-on-release
kneeyo1 Apr 11, 2024
4cf8660
Merge branch 'main' into fix/unlock-all-on-release
kneeyo1 Apr 30, 2024
5cc2569
Update TODO.md
kneeyo1 May 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions .github/actions/basic-setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Basic-Setup
# This is derived from the Setup github action.
# It omits several steps, and is mainly used by tacos_unlock.

inputs:
ssh-private-key:
description: "Private SSH key to use for git clone"
type: string
default: ""
user:
description: the username that will be used for following steps
required: false
default: ${{github.triggering_actor}}
shell:
description: "private -- do not use"
default: env ./tacos-gha/lib/ci/default-shell {0}

runs:
using: composite

steps:
- uses: ./tacos-gha/.github/actions/just-the-basics

- name: tell TF username and PR
uses: ./tacos-gha/.github/actions/set-username-and-hostname
with:
user: ${{inputs.user}}

- name: Set up SSH agent
if: inputs.ssh-private-key != ''
uses: webfactory/ssh-agent@v0.8.0
with:
ssh-private-key: ${{ inputs.ssh-private-key }}

# These fix most ownership, permission issues, but the .ssh config files
# still get the wrong ownership, fixed in the next step.
ssh-agent-cmd: |-
./tacos-gha/lib/ci/bin/sudo-ssh-agent
ssh-add-cmd: |-
./tacos-gha/lib/ci/bin/sudo-ssh-add

- name: Fix .ssh permissions
shell: ${{inputs.shell}}
if: inputs.ssh-private-key != ''
run: |
: fix ssh config ownership
sudo chown -v -R "$(id -un):$(id -gn)" ~/.ssh

: Show SSH agent pubkeys
ssh-add -L

: ... hashes too
ssh-add -l

# this should really be default behavior:
- shell: ${{inputs.shell}}
run: |
gha-set-env 'TF_VERSION' < "$(nearest-config-file .terraform-version)"
gha-set-env 'TERRAGRUNT_VERSION' < "$(nearest-config-file .terragrunt-version)"

- name: Setup Terragrunt
uses: autero1/action-terragrunt@v1.3.2
with:
terragrunt_version: ${{env.TERRAGRUNT_VERSION}}

- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_wrapper: false
terraform_version: ${{ env.TF_VERSION }}
96 changes: 36 additions & 60 deletions .github/workflows/tacos_unlock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,18 @@ env:
GETSENTRY_SAC_VERB: state-admin

jobs:
determine-tf-root-modules:
name: List Slices
determine-terraformers:
name: list terraformers
if: |
false
|| github.event.action != 'labeled'
|| github.event.label.name == ':taco::unlock'
outputs:
slices: ${{ steps.list-slices.outputs.slices }}

terraformers: ${{ steps.list-terraformers.outputs.terraformers }}
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write

steps:
- name: Checkout IAC
uses: actions/checkout@v4
Expand All @@ -55,30 +53,31 @@ jobs:
repository: ${{inputs.tacos_gha_repo}}
ref: ${{inputs.tacos_gha_ref}}
path: tacos-gha

- name: List Slices
id: list-slices
uses: ./tacos-gha/.github/actions/list-slices

- name: basic-setup
uses: ./tacos-gha/.github/actions/basic-setup
- name: List Terraformers
id: list-terraformers
run: |
"$TACOS_GHA_HOME/"lib/ci/list-terraformers
tacos_unlock:
name: TACOS Unlock
needs: [determine-tf-root-modules]
needs: [determine-terraformers]
if: |
needs.determine-tf-root-modules.outputs.slices != '[]'
needs.determine-terraformers.outputs.terraformers != '[]'
strategy:
fail-fast: false
matrix:
tf-root-module:
${{ fromJSON(needs.determine-tf-root-modules.outputs.slices) }}

terraformer:
${{ fromJSON(needs.determine-terraformers.outputs.terraformers) }}
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
id-token: write

env:
TF_ROOT_MODULE: ${{matrix.tf-root-module}}
SUDO_GCP_SERVICE_ACCOUNT: ${{fromJSON(matrix.terraformer).SUDO_GCP_SERVICE_ACCOUNT}}
GETSENTRY_SAC_OIDC: ${{fromJSON(matrix.terraformer).GETSENTRY_SAC_OIDC}}
SLICES: ${{toJSON(fromJSON(matrix.terraformer).slices)}}
steps:
- name: Checkout IAC
uses: actions/checkout@v4
Expand All @@ -88,8 +87,8 @@ jobs:
repository: ${{inputs.tacos_gha_repo}}
ref: ${{inputs.tacos_gha_ref}}
path: tacos-gha
- name: Setup
uses: ./tacos-gha/.github/actions/setup
- name: basic-setup
uses: ./tacos-gha/.github/actions/basic-setup
with:
ssh-private-key: ${{ secrets.ssh-private-key }}
# We explicitly list the low-concern actions, during which users will
Expand All @@ -104,63 +103,40 @@ jobs:
&& github.event_name == 'pull_request'
&& (
false
|| github.event.action == 'closed'
|| github.event.action == 'converted_to_draft'
|| (
true
&& github.event.action == 'labeled'
&& github.event.label.name == ':taco::plan'
)
|| github.event.action == 'ready_for_review'
|| github.event.action == 'synchronize'
|| github.event.action == 'reopened'
)

&& github.event.pull_request.user.login

|| github.triggering_actor
}}

- name: gcp auth
id: auth
uses: google-github-actions/auth@v2.1.1
with:
workload_identity_provider: ${{env.GETSENTRY_SAC_OIDC}}
service_account: ${{env.SUDO_GCP_SERVICE_ACCOUNT}}
- name: Unlock
id: main
run: |
tf-step-summary "TACOS Unlock" "$TACOS_GHA_HOME/"lib/tacos/unlock

- name: Save matrix result
# we need to show any errors to end-users
if: always()
uses: ./tacos-gha/.github/actions/matrix-fan-out

summary:
needs: tacos_unlock
# we need to report failures, too
if: always() && needs.tacos_unlock.result != 'skipped'

runs-on: ubuntu-latest

steps:
- name: Checkout tacos-gha
uses: actions/checkout@v4
with:
repository: ${{inputs.tacos_gha_repo}}
ref: ${{inputs.tacos_gha_ref}}
path: tacos-gha

- name: Setup
uses: ./tacos-gha/.github/actions/just-the-basics
- name: Run matrix-fan-in
uses: ./tacos-gha/.github/actions/matrix-fan-in
- name: Summarize
id: summary
run: |
./tacos-gha/lib/ci/tacos-unlock-summary |
gha-step-summary

- name: Update PR
# we want to report failures, too
# release all tfstate locks currently held
jq <<< "$SLICES" -r '.[]' | ./tacos-gha/lib/ci/unlock
- name: update PR
if: always()

uses: thollander/actions-comment-pull-request@v2.4.3
with:
message: ${{ fromJSON(steps.summary.outputs.summary) }}
message: ${{ fromJSON(steps.main.outputs.summary) }}
comment_tag: unlock
mode: recreate

reset-label:
name: Reset Label

uses: ./.github/workflows/reset-label.yml
with:
label: ":taco::unlock"
Expand Down
1 change: 1 addition & 0 deletions bin/terragrunt-slices
10 changes: 10 additions & 0 deletions lib/ci/list-terraformers
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -euo pipefail

python3 -m lib.tacos.terraformers |
jq -R |
jq -cs |
gha-set-output terraformers |
# prettify
jq . \
;
kneeyo1 marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 6 additions & 4 deletions lib/ci/set-terraformer
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/bin/bash
set -euo pipefail

with-user-env "$TF_ROOT_MODULE" \
sudo-gcp-service-account |
gha-set-env SUDO_GCP_SERVICE_ACCOUNT \
;
if ! [[ "${SUDO_GCP_SERVICE_ACCOUNT:-}" ]]; then
with-user-env "$TF_ROOT_MODULE" \
sudo-gcp-service-account |
gha-set-env SUDO_GCP_SERVICE_ACCOUNT \
;
fi
36 changes: 36 additions & 0 deletions lib/ci/unlock
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
set -euo pipefail
set -ex
exec 1>&2 # stdout is reserved for tf plan/apply results
(
echo "### TACOS Unlock"
echo
if results=$(xargs -r -P10 -n10 tf-lock-release 2>&1); then
cat <<EOF
<details>
<summary>
Success! all slices have been unlocked.
</summary>

\`\`\`console
$results
\`\`\`
</details>
EOF
else
cat <<EOF

<details>
<summary>
Some slices failed to unlock.
</summary>

\`\`\`console
$results
\`\`\`

</details>
EOF
fi
) | gha-step-summary;

82 changes: 82 additions & 0 deletions lib/tacos/terraformers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python3.12
from __future__ import annotations

from dataclasses import dataclass
from typing import Dict
from typing import List
from typing import Union

from lib.constants import TACOS_GHA_HOME
from lib.sh import sh
from lib.types import Generator

from .dependent_slices import TFCategorized
from .dependent_slices import TopLevelTFModule


@dataclass(frozen=True)
class TerraformerResult:
GETSENTRY_SAC_OIDC: str
SUDO_GCP_SERVICE_ACCOUNT: str
slices: set[TopLevelTFModule]


def list_terraformers() -> Generator[TerraformerResult]:
"""List all slices and the oidc provider and terraformer of that slice"""
for slice in sorted(TFCategorized.from_git().slices):
with sh.cd(slice):
oidc_provider = sh.stdout(
(TACOS_GHA_HOME / "lib/getsentry-sac/oidc-provider",)
)
terraformer = sh.stdout(("sudo-gcp-service-account",))

yield TerraformerResult(oidc_provider, terraformer, set([slice]))


def terraformers() -> Generator[TerraformerResult]:
"""Which slices need to be unlocked?"""
from collections import defaultdict

by_terraformer: defaultdict[tuple[str, str], set[TopLevelTFModule]] = (
defaultdict(set)
)

for tf_result in list_terraformers():
key = (
tf_result.GETSENTRY_SAC_OIDC,
tf_result.SUDO_GCP_SERVICE_ACCOUNT,
)
for slice in tf_result.slices:
by_terraformer[key].add(slice)

for key in by_terraformer:
oidc_provider, terraformer = key
yield TerraformerResult(
oidc_provider, terraformer, by_terraformer[key]
)


def convert_terraform_result(
result: TerraformerResult,
) -> Dict[str, Union[str, List[str]]]:
"""Convert TerraformerResult to a JSON-serializable dictionary"""
return {
"GETSENTRY_SAC_OIDC": result.GETSENTRY_SAC_OIDC,
"SUDO_GCP_SERVICE_ACCOUNT": result.SUDO_GCP_SERVICE_ACCOUNT,
# Convert each TopLevelTFModule in the set to a string, then convert the set to a list
"slices": [str(path) for path in result.slices],
}


def main() -> int:
import json

for result in terraformers():
# use custom conversion here, because json doesn't like sets or OSPaths
print(json.dumps(convert_terraform_result(result)))

return 0


if __name__ == "__main__":
exit(main())
4 changes: 3 additions & 1 deletion lib/terragrunt/slices
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh
#!/bin/bash
# print a list of all known terragrunt slices
set -euo pipefail

terragrunt output-module-groups |
jq '.[] | .[]' -r |
sed -r 's@^'"$PWD"'/@@' \
Expand Down
4 changes: 2 additions & 2 deletions lib/tf_lock/TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ FIXME: automated testing for lib/tf_lock

```console
cd slice-0-project
alias sudo-plan='sudo-gcp -u tacos-gha-tf-plan@sac-dev-sa.iam.gserviceaccount.com'
alias sudo-apply='sudo-gcp -u tacos-gha-tf-apply@sac-dev-sa.iam.gserviceaccount.com'
alias sudo-plan='env GETSENTRY_SAC_VERB=plan sudo-gcp'
alias sudo-apply='env GETSENTRY_SAC_VERB=apply sudo-gcp'

sudo-plan tf-lock-info
> {"lock": false}
Expand Down
Loading