1+ #!/bin/env node
2+
3+ const execSync = require ( 'child_process' ) . execSync ;
4+ const { readFileSync, writeFileSync, unlinkSync } = require ( 'fs' ) ;
5+ const POLICY = {
6+ "Version" : "2008-10-17" ,
7+ "Statement" : [
8+ {
9+ "Sid" : "LambdaECRImageRetrievalPolicy" ,
10+ "Effect" : "Allow" ,
11+ "Principal" : {
12+ "Service" : "lambda.amazonaws.com"
13+ } ,
14+ "Action" : [
15+ "ecr:BatchGetImage" ,
16+ "ecr:GetDownloadUrlForLayer"
17+ ]
18+ }
19+ ]
20+ } ;
21+
22+ const runtimeConfig = {
23+ REPO_NAME : process . argv [ 2 ] ,
24+ REPO_URI : null ,
25+ REPO_REGISTRY : null
26+ }
27+ try {
28+ const repos = execSync ( ` aws ecr describe-repositories --repository-names=${ runtimeConfig . REPO_NAME } ` , { stdio : 'pipe' } ) ;
29+ console . log ( ) ;
30+ runtimeConfig . REPO_URI = JSON . parse ( repos . toString ( ) ) . repositories [ 0 ] . repositoryUri ;
31+ } catch ( error ) {
32+ console . log ( error ) ;
33+ const newRepo = execSync ( `aws ecr create-repository --repository-name ${ runtimeConfig . REPO_NAME } ` , { stdio : 'pipe' } ) ;
34+ runtimeConfig . REPO_URI = JSON . parse ( newRepo . toString ( ) ) . repository . repositoryUri ;
35+ }
36+
37+ if ( runtimeConfig . REPO_URI ) {
38+ runtimeConfig . REPO_REGISTRY = runtimeConfig . REPO_URI . replace ( `/${ runtimeConfig . REPO_NAME } ` , "" ) ;
39+ console . log ( 'Setting Repo Policy' ) ;
40+ writeFileSync ( './lcs-policy.json' , JSON . stringify ( POLICY ) ) ;
41+ try {
42+ execSync ( `aws ecr set-repository-policy --repository-name=${ runtimeConfig . REPO_NAME } --policy-text=file://lcs-policy.json` , { stdio : 'pipe' } ) ;
43+ unlinkSync ( './lcs-policy.json' ) ;
44+ console . log ( `Repo policy set on ${ runtimeConfig . REPO_NAME } repo.` ) ;
45+ } catch ( error ) {
46+ console . log ( 'Error setting policy' , error . stderr . toString ( ) ) ;
47+ unlinkSync ( './lcs-policy.json' ) ;
48+ process . exit ( 1 ) ;
49+ }
50+ }
51+
52+ console . log ( '\nRuntime Config:' ) ;
53+ console . log ( '===============' ) ;
54+ console . log ( 'REPO_NAME:' , runtimeConfig . REPO_NAME ) ;
55+ console . log ( 'REPO_URI :' , runtimeConfig . REPO_URI ) ;
56+ console . log ( 'REPO_REGISTRY :' , runtimeConfig . REPO_REGISTRY ) ;
57+
58+ console . log ( "Updating package.json" ) ;
59+ let packageFile = JSON . parse ( readFileSync ( 'package.json' ) . toString ( ) ) ;
60+ packageFile . name = runtimeConfig . REPO_NAME ;
61+ writeFileSync ( 'package.json' , JSON . stringify ( packageFile , null , ' ' ) ) ;
62+
63+ console . log ( "Updating .github/workflows/release.yml" ) ;
64+ let releaseFile = readFileSync ( '.github/workflows/release.yml' ) . toString ( ) ;
65+ releaseFile = releaseFile . replace ( 'REPO_NAME: "lambda-container-service"' , `REPO_NAME: "${ runtimeConfig . REPO_NAME } "` ) ;
66+ releaseFile = releaseFile . replace ( 'REPO_URI: "045615149555.dkr.ecr.ap-southeast-2.amazonaws.com/lambda-container-service"' , `REPO_URI: "${ runtimeConfig . REPO_URI } "` ) ;
67+ releaseFile = releaseFile . replace ( 'REPO_REGISTRY: "045615149555.dkr.ecr.ap-southeast-2.amazonaws.com"' , `REPO_REGISTRY: "${ runtimeConfig . REPO_REGISTRY } "` ) ;
68+ writeFileSync ( '.github/workflows/release.yml' , releaseFile ) ;
69+ console . log ( 'Setup Successfull. Please delete this setup script.' ) ;
0 commit comments