Skip to content

Commit 1e88661

Browse files
committed
Improve deploy.sh/uninstall.sh, particularly when a role is deleted/undeleted
1 parent 876fd50 commit 1e88661

File tree

7 files changed

+37
-38
lines changed

7 files changed

+37
-38
lines changed

README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,9 @@ names start `_gcp_`. The part of the function name after `_gcp_` is used for the
132132
* If you use **both** `-c` and `-e` or **neither**, both types of labeling occur.
133133
* If you change from having Cloud Scheduler labeling to not having it, or vice versa, be sure to deploy both org-level and project-level elements , not just project elements, since this involves the org-level sink.
134134
* Organization-level and project-level elements
135-
* First, note that Iris is an organization-level application. Iris labels all projects in an org (unless you filter it down). Iris has architecture elements which are deployed to both the org and the project.
136-
* By default, deployment is of both organization-level elements (e.g., Log Sinks) and project-level elements (e.g., App Engine app). In general, you can just use this default every time you redeploy.
137-
* Alternatively, you can have a person without org-level permissions redeploy only the project-level elements (e.g. when you change configuration), after the first deployment. Do this with the `-p` switch on `deploy.sh`.
138-
* Likewise, org-level elements only are deployed when you use the `-o` switch on `deploy.sh`.
139-
* If you use **both** `-p` and `-o` or **neither**, both types of elements are deployed.
135+
* First, note that Iris is an organization-level application. A single instance of Iris labels all projects in an org (unless you limit it by configuration). Iris has architecture elements which are deployed to both the org and the project.
136+
* If you want a person with all permissions to deploy the org-level elements and a person without org-level permissions to redeploy only the project-level elements (e.g. when you change configuration), you can do this with flags on the script. Run `deploy.sh -h`.
137+
140138

141139
# Configuration
142140

main.py

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
gcp_utils.set_env()
6767

6868
logging.info("env is %s", sort_dict((os.environ.copy())))
69+
logging.info("Configuration is %s", json.dumps(config_utils.get_config()).replace("\n",""))
6970

7071
PluginHolder.init()
7172

scripts/_deploy-org.sh

+17-14
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,36 @@
44
# Usage
55
# - Called from deploy.sh
66

7-
#set -x
8-
9-
# The following line must come before set -u
7+
set -x
8+
# The following lines must come before set -u
109
if [[ -z "$IRIS_CUSTOM_ROLE" ]]; then IRIS_CUSTOM_ROLE=iris3; fi
11-
10+
if [[ -z "$SKIP_ADDING_IAM_BINDINGS" ]] ; then SKIP_ADDING_IAM_BINDINGS=""; fi
1211
set -u
1312
set -e
1413

15-
16-
if [[ -z "$IRIS_CUSTOM_ROLE" ]]; then IRIS_CUSTOM_ROLE=iris3; fi
17-
1814
LOG_SINK=iris_log
1915

2016
# Get organization id for this project
21-
ORGID=$(curl -X POST -H "Authorization: Bearer \"$(gcloud auth print-access-token)\"" \
22-
-H "Content-Type: application/json; charset=utf-8" \
23-
https://cloudresourcemanager.googleapis.com/v1/projects/"${PROJECT_ID}":getAncestry | grep -A 1 organization |
24-
tail -n 1 | tr -d ' ' | cut -d'"' -f4)
17+
ORGID=$(gcloud projects get-ancestors $PROJECT_ID --format='value(TYPE,ID)' | awk '/org/ {print $2}')
2518

2619
set +e
2720
# Create custom role to run iris
28-
if gcloud iam roles describe "$IRIS_CUSTOM_ROLE" --organization "$ORGID" > /dev/null; then
21+
existing_role=$(gcloud iam roles describe --organization "$ORGID" $IRIS_CUSTOM_ROLE --format='value(deleted,etag)')
22+
# existing_role variable as follows:
23+
# 1. For soft-deleted role, existing_role is like "True BwYSsZlhISU="
24+
# 2. For active role, without the string True.
25+
# 3. For non-existing role, empty-string
26+
if [ -n "$existing_role" ]; then
27+
if [[ "$existing_role" == *"True"* ]]; then # It's a soft-deleted role
28+
gcloud iam roles undelete -q "$IRIS_CUSTOM_ROLE" --organization "$ORGID" >/dev/null
29+
fi
30+
2931
gcloud iam roles update -q "$IRIS_CUSTOM_ROLE" --organization "$ORGID" --file iris-custom-role.yaml >/dev/null
3032
role_error=$?
33+
3134
else
32-
gcloud iam roles create -q "$IRIS_CUSTOM_ROLE" --organization "$ORGID" --file iris-custom-role.yaml >/dev/null
33-
role_error=$?
35+
gcloud iam roles create -q "$IRIS_CUSTOM_ROLE" --organization "$ORGID" --file iris-custom-role.yaml >/dev/null
36+
role_error=$?
3437
fi
3538

3639
set -e

scripts/_deploy-project.sh

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010

1111
#set -x
12+
# The following muyst come before set -u
13+
if [[ -z "$SKIP_ADDING_IAM_BINDINGS" ]] ; then SKIP_ADDING_IAM_BINDINGS=""; fi
1214
set -u
1315
set -e
1416

uninstall_scripts/_uninstall-for-org.sh

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
# This script deletes, on the org level (see `_deploy-org.sh`):
33
# * Iris Custom role along with the policy binding granting this role to the built-in App Engine service account `[email protected]`
44
# * Log sinks `iris_sink`
5-
6-
7-
#set -x
5+
set -x
86

97
# The following line must come before set -u
108
if [[ -z "$IRIS_CUSTOM_ROLE" ]]; then IRIS_CUSTOM_ROLE=iris3; fi
9+
if [[ -z "$SKIP_ADDING_IAM_BINDINGS" ]]; then SKIP_ADDING_IAM_BINDINGS=""; fi
1110

1211
set -u
1312
set -e
@@ -22,8 +21,8 @@ gcloud organizations remove-iam-policy-binding "$ORGID" --all \
2221
--member "serviceAccount:$PROJECT_ID@appspot.gserviceaccount.com" \
2322
--role "organizations/$ORGID/roles/$IRIS_CUSTOM_ROLE" >/dev/null|| true
2423

25-
# Just leave the role; it causes too much trouble in its "soft delete" state
26-
#gcloud iam roles delete -q "$IRIS_CUSTOM_ROLE" --organization "$ORGID" >/dev/null || true
24+
gcloud iam roles delete -q "$IRIS_CUSTOM_ROLE" --organization "$ORGID" >/dev/null || true
25+
2726

2827
if gcloud logging sinks describe --organization="$ORGID" "$LOG_SINK" >&/dev/null; then
2928
svcaccount=$(gcloud logging sinks describe --organization="$ORGID" "$LOG_SINK" |

util/config_utils.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,23 @@ def label_all_on_cron() -> bool:
6363

6464
@functools.lru_cache
6565
def get_config() -> typing.Dict:
66-
test_config = "config-test.yaml"
67-
prod_config = "config.yaml"
68-
if os.path.isfile(test_config):
69-
config_name = test_config
66+
test_config_file = "config-test.yaml"
67+
prod_config_file = "config.yaml"
68+
if os.path.isfile(test_config_file):
69+
config_file_to_use = test_config_file
7070

7171
else:
72-
config_name = prod_config
73-
74-
print("Using config file:", config_name, file=sys.stderr) # logging not yet enabled
72+
config_file_to_use = prod_config_file
7573

7674
try:
77-
with open(config_name) as config_file:
78-
config = yaml.full_load(config_file)
75+
with open(config_file_to_use) as f:
76+
config = yaml.full_load(f)
7977
except FileNotFoundError:
8078
raise FileNotFoundError(
81-
f"Could not find the config-*.yaml file, specifically {config_name}. You may want to create one, based perhaps on config.yaml.original"
79+
f"Could not find the needed config file {config_file_to_use}."
80+
f"You may want to create one, based perhaps on config.yaml.original"
8281
)
83-
config["config_file"] = config_name
82+
config["config_file"] = config_file_to_use
8483

8584
return config
8685

util/gcp/gcp_utils.py

-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
__invocation_count = Counter()
1919

20-
global_counter = 0
21-
22-
2320
def increment_invocation_count(path: str):
2421
global __invocation_count
2522
__invocation_count[path] += 1

0 commit comments

Comments
 (0)