2929from oras .schemas import manifest as oras_manifest_schema
3030
3131from gardenlinux .features import Parser
32- from ..constants import OCI_ANNOTATION_SIGNATURE_KEY , OCI_ANNOTATION_SIGNED_STRING_KEY
32+ from ..constants import (
33+ OCI_ANNOTATION_SIGNATURE_KEY ,
34+ OCI_ANNOTATION_SIGNED_STRING_KEY ,
35+ GL_USER_AGENT_REGISTRY ,
36+ )
3337from .checksum import (
3438 calculate_sha256 ,
3539 verify_sha256 ,
@@ -653,40 +657,59 @@ def push_from_dir(
653657 cname : str ,
654658 directory : str ,
655659 manifest_file : str ,
656- commit : Optional [ str ] = None ,
660+ additional_tags : list = None ,
657661 ):
658- # Step 1 scan and extract nested artifacts:
659- for file in os .listdir (directory ):
660- try :
661- if file .endswith (".pxe.tar.gz" ):
662- logger .info (f"Found nested artifact { file } " )
663- nested_tar_obj = tarfile .open (f"{ directory } /{ file } " )
664- nested_tar_obj .extractall (filter = "data" , path = directory )
665- nested_tar_obj .close ()
666- except (OSError , tarfile .FilterError , tarfile .TarError ) as e :
667- print (f"Failed to extract nested artifact { file } " , e )
668- exit (1 )
662+ """
663+ Push artifacts from a directory to a registry
664+
665+ Args:
666+ architecture: Target architecture of the image
667+ version: Version tag for the image
668+ cname: Canonical name of the image
669+ directory: Directory containing the artifacts
670+ manifest_file: File to write the manifest index entry to
671+ additional_tags: Additional tags to push the manifest with
672+
673+ Returns:
674+ The digest of the pushed manifest
675+ """
676+ if additional_tags is None :
677+ additional_tags = []
669678
670679 try :
680+ # scan and extract nested artifacts
681+ for file in os .listdir (directory ):
682+ try :
683+ if file .endswith (".pxe.tar.gz" ):
684+ logger .info (f"Found nested artifact { file } " )
685+ nested_tar_obj = tarfile .open (f"{ directory } /{ file } " )
686+ nested_tar_obj .extractall (filter = "data" , path = directory )
687+ nested_tar_obj .close ()
688+ except (OSError , tarfile .FilterError , tarfile .TarError ) as e :
689+ print (f"Failed to extract nested artifact { file } " , e )
690+ exit (1 )
691+
692+ # Get metadata from files
671693 oci_metadata = get_oci_metadata_from_fileset (
672694 os .listdir (directory ), architecture
673695 )
674696
675697 features = ""
698+ commit = ""
676699 for artifact in oci_metadata :
677700 if artifact ["media_type" ] == "application/io.gardenlinux.release" :
678- file = open (f"{ directory } /{ artifact ["file_name" ]} " , "r" )
679- lines = file . readlines ()
680- for line in lines :
681- if line . strip () .startswith ("GARDENLINUX_FEATURES=" ):
682- features = line .strip (). removeprefix (
683- "GARDENLINUX_FEATURES="
684- )
685- break
686- file . close ()
687-
688- flavor = Parser . get_flavor_from_cname ( cname , get_arch = True )
689-
701+ with open (f"{ directory } /{ artifact ["file_name" ]} " , "r" ) as file :
702+ for line in file :
703+ line = line . strip ()
704+ if line .startswith ("GARDENLINUX_FEATURES=" ):
705+ features = line .removeprefix ("GARDENLINUX_FEATURES=" )
706+ elif line . startswith ( "GARDENLINUX_COMMIT_ID=" ):
707+ commit = line . removeprefix ( "GARDENLINUX_COMMIT_ID=" )
708+ if features and commit : # Break if both values are found
709+ break
710+ break # Break after processing the release file
711+
712+ # Push the image manifest
690713 digest = self .push_image_manifest (
691714 architecture ,
692715 cname ,
@@ -697,7 +720,103 @@ def push_from_dir(
697720 manifest_file ,
698721 commit = commit ,
699722 )
723+
724+ # Process additional tags if provided
725+ if additional_tags and len (additional_tags ) > 0 :
726+ print (f"DEBUG: Processing { len (additional_tags )} additional tags" )
727+ logger .info (f"Processing { len (additional_tags )} additional tags" )
728+
729+ self .push_additional_tags_manifest (
730+ architecture ,
731+ cname ,
732+ version ,
733+ additional_tags ,
734+ container = self .container ,
735+ )
736+
737+ return digest
700738 except Exception as e :
701739 print ("Error: " , e )
702740 exit (1 )
703- return digest
741+
742+ def push_additional_tags_manifest (
743+ self , architecture , cname , version , additional_tags , container
744+ ):
745+ """
746+ Push additional tags for an existing manifest using ORAS Registry methods
747+
748+ Args:
749+ architecture: Target architecture of the image
750+ cname: Canonical name of the image
751+ version: Version tag for the image
752+ additional_tags: List of additional tags to push
753+ container: Container object
754+ """
755+ try :
756+ # Source tag is the tag containing the version-cname-architecture combination
757+ source_tag = f"{ version } -{ cname } -{ architecture } "
758+ source_container = copy .deepcopy (container )
759+ source_container .tag = source_tag
760+
761+ # Authentication credentials from environment
762+ token = os .getenv ("GL_CLI_REGISTRY_TOKEN" )
763+ username = os .getenv ("GL_CLI_REGISTRY_USERNAME" )
764+ password = os .getenv ("GL_CLI_REGISTRY_PASSWORD" )
765+
766+ # Login to registry if credentials are provided
767+ if username and password :
768+ logger .debug (f"Logging in with username/password" )
769+ try :
770+ self .login (username , password )
771+ except Exception as login_error :
772+ logger .error (f"Login error: { str (login_error )} " )
773+ elif token :
774+ # If token is provided, set it directly on the Registry instance
775+ logger .debug (f"Using token authentication" )
776+ self .token = base64 .b64encode (token .encode ("utf-8" )).decode ("utf-8" )
777+ self .auth .set_token_auth (self .token )
778+
779+ # Get the manifest from the source container
780+ try :
781+ logger .debug (f"Getting manifest from { source_container } " )
782+ manifest = self .get_manifest (source_container )
783+ if not manifest :
784+ logger .error (f"Failed to get manifest for { source_container } " )
785+ return
786+ logger .info (
787+ f"Successfully retrieved manifest: { manifest ['mediaType' ] if 'mediaType' in manifest else 'unknown' } "
788+ )
789+ except Exception as get_error :
790+ logger .error (f"Error getting manifest: { str (get_error )} " )
791+ return
792+
793+ # For each additional tag, push the manifest using Registry.upload_manifest
794+ for tag in additional_tags :
795+ try :
796+ logger .debug (f"Pushing additional tag: { tag } " )
797+
798+ # Create a new container for this tag
799+ tag_container = copy .deepcopy (container )
800+ tag_container .tag = tag
801+
802+ logger .debug (f"Pushing to container: { tag_container } " )
803+
804+ # Upload the manifest to the new tag
805+ response = self .upload_manifest (manifest , tag_container )
806+
807+ if response and response .status_code in [200 , 201 ]:
808+ logger .info (f"Successfully pushed tag { tag } for manifest" )
809+ else :
810+ status_code = getattr (response , "status_code" , "unknown" )
811+ response_text = getattr (response , "text" , "No response text" )
812+ logger .error (
813+ f"Failed to push tag { tag } for manifest: { status_code } "
814+ )
815+
816+ except Exception as tag_error :
817+ logger .error (
818+ f"Error pushing tag { tag } for manifest: { str (tag_error )} "
819+ )
820+
821+ except Exception as e :
822+ logger .error (f"Error in push_additional_tags_manifest: { str (e )} " )
0 commit comments