File tree 6 files changed +66
-2
lines changed
6 files changed +66
-2
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,6 @@ sls invoke -f notifyToSlack -l
36
36
* [ Angular2 app] ( https://github.com/springboot-angular2-tutorial/angular2-app )
37
37
* [ Spring Boot app] ( https://github.com/springboot-angular2-tutorial/boot-app )
38
38
* [ Android app] ( https://github.com/springboot-angular2-tutorial/android-app )
39
- * [ Server provisioning by Ansible and Packer] ( https://github.com/springboot-angular2-tutorial/micropost-provisionings )
40
39
* [ Infrastructure by Terraform] ( https://github.com/springboot-angular2-tutorial/micropost-formation )
41
40
42
41
## License
Original file line number Diff line number Diff line change
1
+ const cleanEcrImages = require ( './clean_ecr_images/index' ) ;
2
+
3
+ module . exports . handle = ( event , context , callback ) => {
4
+
5
+ cleanEcrImages ( event ) . then ( ( ) => {
6
+ callback ( null , { } ) ;
7
+ } ) . catch ( e => {
8
+ console . error ( e ) ;
9
+ } ) ;
10
+
11
+ } ;
Original file line number Diff line number Diff line change
1
+ const AWS = require ( 'aws-sdk' ) ;
2
+ const _ = require ( 'lodash' ) ;
3
+
4
+ const ecr = new AWS . ECR ( ) ;
5
+
6
+ module . exports = function ( params ) {
7
+ const repositoryName = params . repositoryName ;
8
+ const age = params . age || 2 ;
9
+
10
+ return ecr . describeImages ( { repositoryName : repositoryName } )
11
+ . promise ( )
12
+ . then ( data => findImagesToClear ( data . imageDetails , age ) )
13
+ . then ( imagesToClear => deleteImages ( imagesToClear , repositoryName ) )
14
+ . then ( data => console . log ( data ) )
15
+ ;
16
+ } ;
17
+
18
+ function findImagesToClear ( images , age ) {
19
+ return _ . chain ( images )
20
+ . sortBy ( 'imagePushedAt' )
21
+ . take ( images . length - age )
22
+ . value ( ) ;
23
+ }
24
+
25
+ function deleteImages ( images , repositoryName ) {
26
+ const imageIds = images . map ( i => _ . pick ( i , 'imageDigest' ) ) ;
27
+
28
+ return ecr . batchDeleteImage ( {
29
+ imageIds : imageIds ,
30
+ repositoryName : repositoryName ,
31
+ } ) . promise ( ) ;
32
+ }
Original file line number Diff line number Diff line change 2
2
"dependencies" : {
3
3
"@slack/client" : " ^3.6.0" ,
4
4
"dotenv" : " ^2.0.0" ,
5
+ "lodash" : " ^4.17.2" ,
5
6
"serverless-plugin-write-env-vars" : " ^1.0.1"
6
7
},
7
8
"devDependencies" : {
Original file line number Diff line number Diff line change @@ -5,12 +5,33 @@ provider:
5
5
runtime : nodejs4.3
6
6
region : ap-northeast-1
7
7
stage : ${opt:stage, self:custom.defaultStage}
8
+ iamRoleStatements :
9
+ - Effect : " Allow"
10
+ Action :
11
+ - " ecr:DescribeImages"
12
+ - " ecr:BatchDeleteImage"
13
+ Resource : " *"
8
14
9
15
functions :
10
16
notifyToSlack :
11
17
handler : functions/notify_to_slack.handle
12
18
events :
13
19
- sns : web_autoscaled
20
+ cleanEcrImages :
21
+ handler : functions/clean_ecr_images.handle
22
+ events :
23
+ - schedule :
24
+ rate : rate(24 hours)
25
+ enabled : true
26
+ input :
27
+ age : 2
28
+ repositoryName : ' micropost/frontend'
29
+ - schedule :
30
+ rate : rate(1 minute)
31
+ enabled : true
32
+ input :
33
+ age : 2
34
+ repositoryName : ' micropost/backend'
14
35
15
36
custom :
16
37
defaultStage : stg
Original file line number Diff line number Diff line change @@ -344,7 +344,7 @@ jsprim@^1.2.2:
344
344
json-schema "0.2.3"
345
345
verror "1.3.6"
346
346
347
- lodash@^4.13.1 :
347
+ lodash@^4.13.1, lodash@^4.17.2 :
348
348
version "4.17.2"
349
349
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.2.tgz#34a3055babe04ce42467b607d700072c7ff6bf42"
350
350
You can’t perform that action at this time.
0 commit comments