Skip to content

Commit 046aaf5

Browse files
committed
use node.js instead of ruby
1 parent 8b21072 commit 046aaf5

14 files changed

+300
-117
lines changed

.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
insert_final_newline = false
13+
trim_trailing_whitespace = false

.travis.yml

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
---
22
sudo: required
33
dist: trusty
4-
language: ruby
5-
rvm:
6-
- 2.3.0
4+
language: node_js
75
cache:
86
directories:
9-
- vendor/bundle
7+
- $HOME/.npm
8+
- $HOME/.yarn-cache
9+
- node_modules
10+
node_js:
11+
- '6.9.1'
1012

1113
install:
12-
- gem install travis -v 1.8.2 --no-rdoc --no-ri
13-
- bundle install
14+
- npm install -g yarn@">=0.16.0" --cache-min 999999999
15+
- yarn install
1416
# install aws cli
1517
- sudo apt-get -y install python-pip
1618
- sudo pip install awscli
@@ -31,7 +33,6 @@ script:
3133
./terraform-env.sh ${ENV} plan
3234
fi
3335
34-
# TODO I want to make it deploy. But my ruby and travis's ruby make conflicts...
3536
after_success:
3637
# If branch name matched with deploy/xxx, then continue.
3738
- if [ -z "${ENV}" ]; then echo "${TRAVIS_BRANCH} is not a branch to deploy."; exit 0; fi
@@ -41,10 +42,10 @@ after_success:
4142
- ./terraform-env.sh ${ENV} get
4243
- ./terraform-env.sh ${ENV} apply
4344
# Remove unused amis
44-
- ruby scripts/remove_unused_and_rotated_amis.rb
45+
- node scripts/remove_unused_and_rotated_amis.js
4546
# Replace instances if web_ami was updated.
4647
- ./terraform-env.sh ${ENV} output | tee out_after
4748
- lines=$(cat out_before out_after | grep 'web_ami' | uniq | wc -l)
4849
- asg_name=$(grep "web_asg_name" out_after | awk -F ' = ' '{print $2}')
49-
- test ${lines} -eq 2 && ruby scripts/replace_old_instances.rb ${asg_name}
50+
- test ${lines} -eq 2 && node scripts/replace_old_instances.js -n ${asg_name}
5051

Gemfile

-5
This file was deleted.

Gemfile.lock

-24
This file was deleted.

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
## Dependencies
44

55
* Terraform
6-
* direnv
7-
* Ruby
6+
* Node.js
87
* AWS CLI
8+
* direnv
99

1010
## Getting started
1111

@@ -26,6 +26,7 @@ $ bundle install
2626
Plan and Apply
2727

2828
```
29+
$ ./terraform-env.sh stg get
2930
$ ./terraform-env.sh stg plan
3031
$ ./terraform-env.sh stg apply
3132
```

package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "micropost-formation",
3+
"version": "1.0.0",
4+
"engines": {
5+
"npm": "~6.7.0"
6+
},
7+
"repository": {
8+
"url": "ssh://[email protected]/springboot-angular2-tutorial/micropost-formation.git",
9+
"type": "git"
10+
},
11+
"author": "Akira Sosa <[email protected]>",
12+
"license": "MIT",
13+
"dependencies": {
14+
"argparse": "^1.0.9",
15+
"aws-sdk": "^2.6.10",
16+
"bluebird": "^3.4.6",
17+
"lodash": "^4.16.4"
18+
}
19+
}

scripts/get_certificate_arn.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const AWS = require('aws-sdk');
2+
const ArgumentParser = require('argparse').ArgumentParser;
3+
4+
AWS.config.region = process.env.AWS_DEFAULT_REGION;
5+
6+
function parseArguments() {
7+
const parser = new ArgumentParser({
8+
description: 'This script find an ACM arn by domain name',
9+
});
10+
parser.addArgument(['-d', '--domain'], {
11+
help: 'domain name of certificate to search',
12+
required: true,
13+
});
14+
return parser.parseArgs();
15+
}
16+
17+
const args = parseArguments();
18+
const ACM = new AWS.ACM();
19+
20+
ACM.listCertificates((err, data) => {
21+
const arn = data.CertificateSummaryList
22+
.filter(s => s.DomainName === args.domain)
23+
.map(s => s.CertificateArn);
24+
console.log(arn[0]);
25+
});
26+

scripts/get_certificate_arn.rb

-12
This file was deleted.

scripts/pre.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if [ "${ENVIRONMENT}" = "prod" ]; then
88
fi
99

1010
export TF_VAR_aws_region=${AWS_DEFAULT_REGION}
11-
export TF_VAR_alb_certificate_arn=$(bundle exec ruby scripts/get_certificate_arn.rb --domain "*.hana053.com" --region ${AWS_DEFAULT_REGION})
11+
export TF_VAR_alb_certificate_arn=$(node scripts/get_certificate_arn.js -d "*.hana053.com")
1212

1313
asg_name=$(terraform output web_asg_name)
1414
if [ -n "${asg_name}" ]; then
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const AWS = require('aws-sdk');
2+
const _ = require('lodash');
3+
4+
AWS.config.setPromisesDependency(require('bluebird'));
5+
AWS.config.region = process.env.AWS_DEFAULT_REGION;
6+
7+
const EC2 = new AWS.EC2();
8+
const AutoScaling = new AWS.AutoScaling();
9+
10+
function rotatedImages() {
11+
return EC2.describeImages({
12+
Filters: [
13+
{
14+
Name: 'tag:Rotated',
15+
Values: ['true'],
16+
},
17+
],
18+
}).promise()
19+
.then(data => data.Images);
20+
}
21+
22+
function unusedImageIds() {
23+
return AutoScaling.describeLaunchConfigurations({}).promise()
24+
.then(data => {
25+
return data.LaunchConfigurations.map(lc => lc.ImageId);
26+
});
27+
}
28+
29+
function deregisterImage(image) {
30+
return EC2.deregisterImage({
31+
ImageId: image.ImageId,
32+
}).promise()
33+
.then(() => {
34+
console.log(`deregistered ${image.ImageId}`);
35+
return image;
36+
})
37+
// ignore error and continue
38+
.catch(() => image)
39+
}
40+
41+
function deleteSnapShot(image) {
42+
const snapShotId = _.chain(image.BlockDeviceMappings)
43+
.flatten()
44+
.map(m => m.Ebs)
45+
.compact()
46+
.map(m => m.SnapshotId)
47+
.first()
48+
.value();
49+
return EC2.deleteSnapshot({
50+
SnapshotId: snapShotId,
51+
}).promise()
52+
.then(() => {
53+
console.log(`deleted ${snapShotId}`);
54+
return image;
55+
})
56+
.catch(() => image)
57+
}
58+
59+
Promise.all([rotatedImages(), unusedImageIds()])
60+
.then(results => {
61+
const [rotatedImages, unusedImageIds] = results;
62+
return rotatedImages.filter(image => {
63+
return !unusedImageIds.includes(image.ImageId)
64+
})
65+
})
66+
.then(images => {
67+
const promises = images.map(image => {
68+
return deregisterImage(image).then(deleteSnapShot)
69+
});
70+
return Promise.all(promises);
71+
})
72+
;

scripts/remove_unused_and_rotated_amis.rb

-40
This file was deleted.

scripts/replace_old_instances.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const AWS = require('aws-sdk');
2+
const ArgumentParser = require('argparse').ArgumentParser;
3+
4+
AWS.config.setPromisesDependency(require('bluebird'));
5+
AWS.config.region = process.env.AWS_DEFAULT_REGION;
6+
7+
const AutoScaling = new AWS.AutoScaling();
8+
9+
function parseArguments() {
10+
const parser = new ArgumentParser({
11+
description: 'This script scale out specified auto scaling group to flip out old instances.',
12+
});
13+
parser.addArgument(['-n', '--name'], {
14+
help: 'name of a auto scaling group',
15+
required: true,
16+
});
17+
return parser.parseArgs();
18+
}
19+
20+
function validateAsgCapacity(asg) {
21+
if (!asg) {
22+
console.error('This autoscaling group does not exist...');
23+
process.exit(1);
24+
}
25+
if (asg.DesiredCapacity * 2 > asg.MaxSize) {
26+
console.error(`Can not scale out to replace instances. Consider increasing the max capacity.`);
27+
process.exit(1);
28+
}
29+
console.log(`Current desired capacity is ${asg.DesiredCapacity}`);
30+
31+
return asg;
32+
}
33+
34+
function multipleDesiredCapacity(asg) {
35+
console.log(`Scale out to ${asg.DesiredCapacity * 2}.`);
36+
37+
return AutoScaling.setDesiredCapacity({
38+
AutoScalingGroupName: asg.AutoScalingGroupName,
39+
DesiredCapacity: asg.DesiredCapacity * 2,
40+
HonorCooldown: true,
41+
}).promise();
42+
}
43+
44+
const asgName = parseArguments().name;
45+
46+
AutoScaling.describeAutoScalingGroups({
47+
AutoScalingGroupNames: [asgName],
48+
}).promise()
49+
.then(data => data.AutoScalingGroups[0])
50+
.then(validateAsgCapacity)
51+
.then(multipleDesiredCapacity);

scripts/replace_old_instances.rb

-24
This file was deleted.

0 commit comments

Comments
 (0)