forked from ASAP-CRN/wf-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcloud_ops.py
More file actions
234 lines (204 loc) · 5.87 KB
/
Copy pathgcloud_ops.py
File metadata and controls
234 lines (204 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env python3
"""Elementary gcloud / Cloud Storage CLI wrappers and bucket IAM/label helpers.
Thin wrappers around `gcloud storage ...` subprocess calls (copy/move/remove/
rsync/list) plus the bucket permission and label operations used during data
promotion. Also includes the small bucket/dataset name-parsing helpers.
"""
import json
import logging
import subprocess
import re
def get_team_name(bucket: str) -> str:
return bucket.split("-team-", 1)[1].split("-")[0]
def strip_team_prefix(entity_id: str) -> str:
"""Strip 'team' prefix if present: dataset directories do not have 'team' prefix"""
norm_id = entity_id.strip().lower()
norm_id = re.sub(r'^team[-_ ]*', '', norm_id)
if not norm_id:
raise ValueError(f"ID: [{entity_id}] is empty after stripping 'team' prefix: [{norm_id}]")
return norm_id
def run_command(command):
try:
result = subprocess.run(command, check=True, capture_output=True, text=True)
return result.stdout
except subprocess.CalledProcessError as e:
if "No policy binding found" in e.stderr:
print(f"[INFO] No existing storage.admin binding to remove (command: {' '.join(command)})")
else:
print(f"[ERROR] Command failed:\n{e.stderr}")
raise
def remove_internal_qc_label(bucket_name):
command = [
"gcloud",
"storage",
"buckets",
"update",
bucket_name,
"--remove-labels=internal-qc-data"
]
result = subprocess.run(command, check=True, capture_output=True, text=True)
return result.stdout
def check_admin_binding(bucket_name):
team_name = get_team_name(bucket_name)
role_admin = "roles/storage.admin"
team_gg = "asap-team-" + team_name + "@dnastack.com"
member = f"group:{team_gg}"
policy_json = run_command([
"gcloud",
"storage",
"buckets",
"get-iam-policy",
bucket_name,
"--format=json"
])
policy = json.loads(policy_json)
has_admin_binding = any(
binding["role"] == role_admin and member in binding.get("members", [])
for binding in policy.get("bindings", [])
)
return member, role_admin, has_admin_binding
def change_gg_storage_admin_to_read_write(bucket_name):
member, role_admin, has_admin_binding = check_admin_binding(bucket_name)
if has_admin_binding:
print(f"[INFO] Removing Storage Admin access and granting Storage Object Creator and Viewer to CRN Teams for [{bucket_name}] on Google Group")
run_command([
"gcloud",
"storage",
"buckets",
"remove-iam-policy-binding",
bucket_name,
f"--member={member}",
f"--role={role_admin}"
])
run_command([
"gcloud",
"storage",
"buckets",
"add-iam-policy-binding",
bucket_name,
f"--member={member}",
"--role=roles/storage.objectViewer"
])
run_command([
"gcloud",
"storage",
"buckets",
"add-iam-policy-binding",
bucket_name,
f"--member={member}",
"--role=roles/storage.objectCreator"
])
else:
print(f"[INFO] Storage Object Creator and Viewer already granted to CRN Teams' permissions for [{bucket_name}] on Google Group")
def list_dirs(bucket_name):
command = [
"gcloud",
"storage",
"ls",
bucket_name
]
result = subprocess.run(command, check=True, capture_output=True, text=True)
return result.stdout
def gcopy(source_path, destination_path, recursive=False):
command = [
"gcloud",
"storage",
"cp",
source_path,
destination_path
]
if recursive:
command.insert(3, "--recursive")
result = subprocess.run(command, check=True, capture_output=True, text=True)
# These if's are because gcloud returns important info in stderr (e.g. "Copying /path/to/file1 to gs://bucket/file1...")
# even if the command is successful. Since ERROR may be misleading, it's better to logging.info both stdout and stderr
if result.stdout:
logging.info(result.stdout)
if result.stderr:
logging.info(result.stderr)
def gmove(source_path, destination_path):
command = [
"gcloud",
"storage",
"mv",
source_path,
destination_path
]
result = subprocess.run(command, check=True, capture_output=True, text=True)
if result.stdout:
logging.info(result.stdout)
if result.stderr:
logging.info(result.stderr)
def gremove(destination_path):
command = [
"gcloud",
"storage",
"rm",
destination_path
]
try:
result = subprocess.run(command, check=True, capture_output=True, text=True)
except subprocess.CalledProcessError:
logging.info(f"No files found at {destination_path}; skipping deletion.")
return
if result.stdout:
logging.info(result.stdout)
if result.stderr:
logging.info(result.stderr)
def gsync(source_path, destination_path, dry_run):
command = [
"gcloud",
"storage",
"rsync",
"-r",
source_path,
destination_path
]
if dry_run:
command.insert(4, "--dry-run")
result = subprocess.run(command, check=True, capture_output=True, text=True)
if result.stdout:
logging.info(result.stdout)
if result.stderr:
logging.info(result.stderr)
def gsync_del(source_path, destination_path, dry_run):
command = [
"gcloud",
"storage",
"rsync",
"--delete-unmatched-destination-objects",
"-r",
source_path,
destination_path
]
if dry_run:
command.insert(4, "--dry-run")
result = subprocess.run(command, check=True, capture_output=True, text=True)
if result.stdout:
logging.info(result.stdout)
if result.stderr:
logging.info(result.stderr)
def add_verily_read_access(bucket_name):
command = [
"gcloud",
"storage",
"buckets",
"add-iam-policy-binding",
bucket_name,
"--member=group:asap-cloud-readers@verily-bvdp.com",
"--role=roles/storage.objectViewer",
"--project",
"dnastack-asap-parkinsons"
]
result = subprocess.run(command, check=True, capture_output=True, text=True)
if result.stdout:
logging.info(result.stdout)
if result.stderr:
logging.info(result.stderr)
__all__ = [
"get_team_name", "strip_team_prefix", "run_command",
"remove_internal_qc_label", "check_admin_binding",
"change_gg_storage_admin_to_read_write", "list_dirs",
"gcopy", "gmove", "gremove", "gsync", "gsync_del",
"add_verily_read_access",
]