Skip to content

Commit 27458ef

Browse files
gursewak1997jlebon
authored andcommitted
src/cloud-prune: Minor fixes to handle edge cases
Removed gcp-project switch since we can access that from meta.json. Handle AWS ami/snapshot pruning to take either of the resources to be deleted than needing both of them. Also, exit gracefully when we have no policy defined for the respective stream. Additionally, added handling of some edge cases in handle_upload_builds_json
1 parent 5ed3049 commit 27458ef

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/cmd-cloud-prune

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def parse_args():
6060
parser.add_argument("--upload-builds-json", help="Push builds.json", action='store_true')
6161
parser.add_argument("--stream", type=str, help="CoreOS stream", required=True)
6262
parser.add_argument("--gcp-json-key", help="GCP Service Account JSON Auth", default=os.environ.get("GCP_JSON_AUTH"))
63-
parser.add_argument("--gcp-project", help="GCP Project name", default=os.environ.get("GCP_PROJECT_NAME"))
6463
parser.add_argument("--acl", help="ACL for objects", action='store', default='private')
6564
parser.add_argument("--aws-config-file", default=os.environ.get("AWS_CONFIG_FILE"), help="Path to AWS config file")
6665
return parser.parse_args()
@@ -98,7 +97,11 @@ def main():
9897

9998
with open(args.policy, "r") as f:
10099
policy = yaml.safe_load(f)
101-
validate_policy(stream, policy)
100+
if stream in policy:
101+
validate_policy(stream, policy)
102+
else:
103+
print(f"There is no policy defined in gc-policy.yaml for {stream}")
104+
return
102105

103106
with open(BUILDFILES['list'], "r") as f:
104107
builds_json_data = json.load(f)
@@ -159,7 +162,6 @@ def get_cloud_config(args):
159162
return {
160163
"gcp": {
161164
"json-key": args.gcp_json_key,
162-
"project": args.gcp_project
163165
},
164166
"aws": {
165167
"credentials": args.aws_config_file
@@ -207,15 +209,24 @@ def save_builds_json(builds_json_data):
207209

208210
def handle_upload_builds_json(s3_client, bucket, prefix, dry_run, acl):
209211
remote_builds_json = get_json_from_s3(s3_client, bucket, os.path.join(prefix, "builds.json"))
212+
# This is the copy of builds.json from what we last downloaded from the source
210213
with open(BUILDFILES['sourcedata'], "r") as f:
211214
builds_json_source_data = json.load(f)
215+
# This is the current list of builds at builds/builds.json
216+
with open(BUILDFILES['list'], "r") as f:
217+
current_builds_json = json.load(f)
218+
219+
# If there are no changes to the local builds/builds.json we won't need to upload
220+
# anything to the s3 bucket. Will return in this scenario.
221+
if builds_json_source_data == current_builds_json:
222+
print("There are no changes to the local builds/builds.json. No upload needed")
223+
return
224+
212225
# Check if there are any changes that were made to remote(s3 version) builds.json
213226
# while the pruning was in progress
214227
if remote_builds_json != builds_json_source_data:
215228
print("Detected remote updates to builds.json. Merging it to the local builds.json file")
216-
with open(BUILDFILES['list'], "r") as f:
217-
current_builds_json = json.load(f)
218-
update_policy_cleanup(current_builds_json, remote_builds_json)
229+
remote_builds_json = update_policy_cleanup(current_builds_json, remote_builds_json)
219230
if not dry_run:
220231
# Make sure we have the merged json as local builds/builds.json
221232
save_builds_json(remote_builds_json)
@@ -232,6 +243,7 @@ def update_policy_cleanup(current_builds, remote_builds):
232243
current_build = current_builds_dict[build_id]
233244
if 'policy-cleanup' in current_build:
234245
remote_build['policy-cleanup'] = current_build['policy-cleanup']
246+
return remote_builds
235247

236248

237249
def prune_cloud_uploads(build, cloud_config, dry_run):
@@ -257,7 +269,7 @@ def deregister_aws_amis(build, cloud_config, dry_run):
257269
if dry_run:
258270
print(f"Would delete {ami_id} and {snapshot_id} for {build.id}")
259271
continue
260-
if ami_id and snapshot_id and region_name:
272+
if (ami_id or snapshot_id) and region_name:
261273
try:
262274
deregister_aws_resource(ami_id, snapshot_id, region=region_name, credentials_file=aws_credentials)
263275
except Exception as e:
@@ -274,8 +286,8 @@ def delete_gcp_image(build, cloud_config, dry_run):
274286
print(f"No GCP image for {build.id} for {build.arch}")
275287
return errors
276288
gcp_image = gcp.get("image")
289+
project = gcp.get("project")
277290
json_key = cloud_config.get("gcp", {}).get("json-key")
278-
project = cloud_config.get("gcp", {}).get("project")
279291
if dry_run:
280292
print(f"Would delete {gcp_image} GCP image for {build.id}")
281293
elif gcp_image and json_key and project:

0 commit comments

Comments
 (0)