UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

3 lines (2 loc) 4.19 kB
"use strict";var C=Object.create,c=Object.defineProperty,w=Object.getOwnPropertyDescriptor,S=Object.getOwnPropertyNames,A=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty,L=(e,o)=>{for(var t in o)c(e,t,{get:o[t],enumerable:!0})},d=(e,o,t,s)=>{if(o&&typeof o=="object"||typeof o=="function")for(let r of S(o))!P.call(e,r)&&r!==t&&c(e,r,{get:()=>o[r],enumerable:!(s=w(o,r))||s.enumerable});return e},l=(e,o,t)=>(t=e!=null?C(A(e)):{},d(o||!e||!e.__esModule?c(t,"default",{value:e,enumerable:!0}):t,e)),T=e=>d(c({},"__esModule",{value:!0}),e),W={};L(W,{autoDeleteHandler:()=>I,handler:()=>k}),module.exports=T(W);var h=require("@aws-sdk/client-ecr"),m=l(require("https")),R=l(require("url")),n={sendHttpRequest:x,log:N,includeStackTraces:!0,userHandlerIndex:"./index"},p="AWSCDK::CustomResourceProviderFramework::CREATE_FAILED",D="AWSCDK::CustomResourceProviderFramework::MISSING_PHYSICAL_ID";function y(e){return async(o,t)=>{let s={...o,ResponseURL:"..."};if(n.log(JSON.stringify(s,void 0,2)),o.RequestType==="Delete"&&o.PhysicalResourceId===p){n.log("ignoring DELETE event caused by a failed CREATE event"),await u("SUCCESS",o);return}try{let r=await e(s,t),a=b(o,r);await u("SUCCESS",a)}catch(r){let a={...o,Reason:n.includeStackTraces?r.stack:r.message};a.PhysicalResourceId||(o.RequestType==="Create"?(n.log("CREATE failed, responding with a marker physical resource id so that the subsequent DELETE will be ignored"),a.PhysicalResourceId=p):n.log(`ERROR: Malformed event. "PhysicalResourceId" is required: ${JSON.stringify(o)}`)),await u("FAILED",a)}}}function b(e,o={}){let t=o.PhysicalResourceId??e.PhysicalResourceId??e.RequestId;if(e.RequestType==="Delete"&&t!==e.PhysicalResourceId)throw new Error(`DELETE: cannot change the physical resource ID from "${e.PhysicalResourceId}" to "${o.PhysicalResourceId}" during deletion`);return{...e,...o,PhysicalResourceId:t}}async function u(e,o){let t={Status:e,Reason:o.Reason??e,StackId:o.StackId,RequestId:o.RequestId,PhysicalResourceId:o.PhysicalResourceId||D,LogicalResourceId:o.LogicalResourceId,NoEcho:o.NoEcho,Data:o.Data},s=R.parse(o.ResponseURL),r=`${s.protocol}//${s.hostname}/${s.pathname}?***`;n.log("submit response to cloudformation",r,t);let a=JSON.stringify(t),f={hostname:s.hostname,path:s.path,method:"PUT",headers:{"content-type":"","content-length":Buffer.byteLength(a,"utf8")}};await F({attempts:5,sleep:1e3},n.sendHttpRequest)(f,a)}async function x(e,o){return new Promise((t,s)=>{try{let r=m.request(e,a=>{a.resume(),!a.statusCode||a.statusCode>=400?s(new Error(`Unsuccessful HTTP response: ${a.statusCode}`)):t()});r.on("error",s),r.write(o),r.end()}catch(r){s(r)}})}function N(e,...o){console.log(e,...o)}function F(e,o){return async(...t)=>{let s=e.attempts,r=e.sleep;for(;;)try{return await o(...t)}catch(a){if(s--<=0)throw a;await H(Math.floor(Math.random()*r)),r*=2}}}async function H(e){return new Promise(o=>setTimeout(o,e))}var g="aws-cdk:auto-delete-images",i=new h.ECR({}),k=y(I);async function I(e){switch(e.RequestType){case"Create":break;case"Update":return{PhysicalResourceId:(await q(e)).PhysicalResourceId};case"Delete":return U(e.ResourceProperties?.RepositoryName)}}async function q(e){let o=e,t=o.OldResourceProperties?.RepositoryName;return{PhysicalResourceId:o.ResourceProperties?.RepositoryName??t}}async function E(e){let o=await i.listImages(e),t=[],s=[];(o.imageIds??[]).forEach(a=>{"imageTag"in a?s.push(a):t.push(a)});let r=o.nextToken??null;t.length===0&&s.length===0||(s.length!==0&&await i.batchDeleteImage({repositoryName:e.repositoryName,imageIds:s}),t.length!==0&&await i.batchDeleteImage({repositoryName:e.repositoryName,imageIds:t}),r&&await E({...e,nextToken:r}))}async function U(e){if(!e)throw new Error("No RepositoryName was provided.");let t=(await i.describeRepositories({repositoryNames:[e]})).repositories?.find(s=>s.repositoryName===e);if(!await _(t?.repositoryArn)){process.stdout.write(`Repository does not have '${g}' tag, skipping cleaning. `);return}try{await E({repositoryName:e})}catch(s){if(s.name!=="RepositoryNotFoundException")throw s}}async function _(e){return(await i.listTagsForResource({resourceArn:e})).tags?.some(t=>t.Key===g&&t.Value==="true")}