import shelljs from "shelljs" ; import prompts from "prompts" ; import { assertExists , assertIsNonEmptyString , assertShellResult , isAppleM1 } from "../../utils" ; export type UploadImage = ( ) => Promise < string > ; export default function provideUploadImage ( shell , prompt , imageRepositoryUrl , imageRepositoryUsername , imageRepositoryPassword , agentName ) { assertExists ( shell , 'shell' ) ; assertExists ( prompt , 'prompt' ) ; assertIsNonEmptyString ( imageRepositoryUrl , 'imageRepositoryUrl' ) ; assertIsNonEmptyString ( agentName , 'agentName' ) ; return async function uploadImage ( ) { if ( imageRepositoryUsername && imageRepositoryPassword ) { const loginResult = shell . exec ( `docker login ${ imageRepositoryUrl } -u ${ imageRepositoryUsername } -p ${ imageRepositoryPassword }` ) ; assertShellResult ( loginResult , 'error authenticating with image repository' ) ; } console . log ( 'building agent image...' ) ; const containerTag = `${ agentName }-intermediate` ; let buildCommand = `docker build --tag ${ containerTag } .` ; if ( isAppleM1 ( ) ) { buildCommand = `docker buildx build --platform linux/amd64 --tag ${ containerTag } .` ; } const buildResult = shell . exec ( buildCommand ) ; assertShellResult ( buildResult , 'error building agent image' ) ; console . log ( 'pushing agent image to repository...' ) ; const tagResult = shell . exec ( `docker tag ${ containerTag } ${ imageRepositoryUrl }/${ containerTag }` ) ; assertShellResult ( tagResult , 'error tagging agent image' ) ; const pushResult = shell . exec ( `docker push ${ imageRepositoryUrl }/${ containerTag }` ) ; assertShellResult ( pushResult , 'error pushing agent image' ) ; const digestLine = pushResult . grep ( 'sha256' ) . toString ( ) ; const digestStartIndex = digestLine . indexOf ( 'sha256:' ) + 7 ; const imageDigest = digestLine . substring ( digestStartIndex , digestStartIndex + 64 ) ; const pullResult = shell . exec ( `docker pull -a ${ imageRepositoryUrl }/${ imageDigest }` ) ; assertShellResult ( pullResult , 'error pulling tagged agent images' ) ; const cidLine = pullResult . grep ( 'bafy' ) . toString ( ) ; const cidStartIndex = cidLine . indexOf ( 'bafy' ) ; const cidEndIndex = cidLine . indexOf ( ':' , cidStartIndex ) ; const imageIpfsCid = cidLine . substring ( cidStartIndex , cidEndIndex ) ; const imageReference = `${ imageIpfsCid }@sha256:${ imageDigest }` ; return imageReference ; } ; }