11import os
2- import sys
32import subprocess
3+ import sys
4+
45from lib .base_logger import logger
56from scripts .release .build .build_info import load_build_info
67
8+
79def helm_registry_login (helm_registry : str , region : str ):
810 logger .info (f"Attempting to log into ECR registry: { helm_registry } , using helm registry login." )
9-
10- aws_command = [
11- "aws" ,
12- "ecr" ,
13- "get-login-password" ,
14- "--region" ,
15- region
16- ]
17-
11+
12+ aws_command = ["aws" , "ecr" , "get-login-password" , "--region" , region ]
13+
1814 # as we can see the password is being provided by stdin, that would mean we will have to
1915 # pipe the aws_command (it figures out the password) into helm_command.
20- helm_command = [
21- "helm" ,
22- "registry" ,
23- "login" ,
24- "--username" ,
25- "AWS" ,
26- "--password-stdin" ,
27- helm_registry
28- ]
16+ helm_command = ["helm" , "registry" , "login" , "--username" , "AWS" , "--password-stdin" , helm_registry ]
2917
3018 try :
3119 logger .info ("Starting AWS ECR credential retrieval." )
3220 aws_proc = subprocess .Popen (
33- aws_command ,
34- stdout = subprocess .PIPE ,
35- stderr = subprocess .PIPE ,
36- text = True # Treat input/output as text strings
21+ aws_command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True # Treat input/output as text strings
3722 )
38-
23+
3924 logger .info ("Starting Helm registry login." )
4025 helm_proc = subprocess .Popen (
41- helm_command ,
42- stdin = aws_proc .stdout ,
43- stdout = subprocess .PIPE ,
44- stderr = subprocess .PIPE ,
45- text = True
26+ helm_command , stdin = aws_proc .stdout , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True
4627 )
47-
48- # Close the stdout stream of aws_proc in the parent process
28+
29+ # Close the stdout stream of aws_proc in the parent process
4930 # to prevent resource leakage (only needed if you plan to do more processing)
50- aws_proc .stdout .close ()
31+ aws_proc .stdout .close ()
5132
5233 # Wait for the Helm command (helm_proc) to finish and capture its output
5334 helm_stdout , helm_stderr = helm_proc .communicate ()
54-
35+
5536 # Wait for the AWS process to finish as well
56- aws_proc .wait ()
37+ aws_proc .wait ()
5738
5839 if aws_proc .returncode != 0 :
59- _ , aws_stderr = aws_proc .communicate ()
40+ _ , aws_stderr = aws_proc .communicate ()
6041 raise Exception (f"aws command to get password failed. Error: { aws_stderr } " )
61-
42+
6243 if helm_proc .returncode == 0 :
6344 logger .info ("Login to helm registry was successful." )
6445 logger .info (helm_stdout .strip ())
6546 else :
66- raise Exception (f"Login to helm registry failed, Exit code: { helm_proc .returncode } , Error: { helm_stderr .strip ()} " )
47+ raise Exception (
48+ f"Login to helm registry failed, Exit code: { helm_proc .returncode } , Error: { helm_stderr .strip ()} "
49+ )
6750
6851 except FileNotFoundError as e :
6952 # This catches errors if 'aws' or 'helm' are not in the PATH
7053 raise Exception (f"Command not found. Please ensure '{ e .filename } ' is installed and in your system's PATH." )
7154 except Exception as e :
7255 raise Exception (f"An unexpected error occurred: { e } ." )
73-
56+
7457
7558def main ():
7659 build_scenario = os .environ .get ("BUILD_SCENARIO" )
7760 build_info = load_build_info (build_scenario )
7861
79-
8062 registry = build_info .helm_charts ["mongodb-kubernetes" ].registry
8163 region = build_info .helm_charts ["mongodb-kubernetes" ].region
8264 return helm_registry_login (registry , region )
8365
66+
8467if __name__ == "__main__" :
8568 try :
8669 main ()
8770 except Exception as e :
8871 logger .error (f"Failed while logging in to the helm registry. Error: { e } " )
89- sys .exit (1 )
72+ sys .exit (1 )
0 commit comments