utkarsharma2 commented on code in PR #35759: URL: https://github.com/apache/airflow/pull/35759#discussion_r1399605295
########## scripts/in_container/run_fix_ownership.py: ########## @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import os +import pwd +import sys +from pathlib import Path + +HOST_OS = os.environ.get("HOST_OS", "") +DOCKER_IS_ROOTLESS = os.environ.get("DOCKER_IS_ROOTLESS", "false") == "true" + + +def change_ownership_of_files(path: Path) -> None: + host_user_id = os.environ.get("HOST_USER_ID", "") + host_group_id = os.environ.get("HOST_GROUP_ID", "") + if host_user_id == "" or host_group_id == "": + print( + f"ERROR: HOST_USER_ID or HOST_GROUP_ID environment variables " + f"are not set: {host_user_id}:{host_group_id}" + ) + sys.exit(1) + if not host_user_id.isnumeric() or not host_group_id.isnumeric(): + print( + f"ERROR: HOST_USER_ID or HOST_GROUP_ID environment variables " + f"should be numeric: {host_user_id}:{host_group_id}" + ) + sys.exit(1) + count_files = 0 + root_uid = pwd.getpwnam("root").pw_uid + print("Attempting to see if there are files that need to be changed to host user ownership") + for file in path.rglob("*"): + try: + if file.is_symlink() and file.lstat().st_uid == root_uid: + # Also change ownership of symlink itself (by default it will only change + # ownership of the file it points to) + os.chown(file, int(host_user_id), int(host_group_id), follow_symlinks=False) + count_files += 1 + if file.stat().st_uid == root_uid: + os.chown(file, int(host_user_id), int(host_group_id)) + count_files += 1 + if os.environ.get("VERBOSE_COMMANDS", "false") == "true": + print(f"Changed ownership of {file}") Review Comment: ```suggestion follow_symlinks = True if file.is_symlink() and file.lstat().st_uid == root_uid: follow_symlinks = False os.chown(file, int(host_user_id), int(host_group_id), follow_symlinks=follow_symlinks) count_files += 1 if os.environ.get("VERBOSE_COMMANDS", "false") == "true": print(f"Changed ownership of {file}") ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org