-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy_stacks.sh
executable file
·111 lines (94 loc) · 4.26 KB
/
deploy_stacks.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/sh
set -e
export DEPLOYMENT_PROFILE=your-aws-peofile
export DEPLOYMENT_REGION=ap-northeast-2
export DEPLOYMENT_NAMESPACE=devproto
STS=$(aws sts get-caller-identity --profile $DEPLOYMENT_PROFILE --region $DEPLOYMENT_REGION --output json)
printf "Based on your DEPLOYMENT_PROFILE and DEPLOYMENT_REGION, this is your caller identity:\n"
printf "$(echo $STS | jq)\n"
read -p "Is this correct (YyNn)? " -n 1 -r
[[ $REPLY =~ ^[Yy]$ ]] || exit 0
printf "\n"
export ACCOUNT_ID=$(echo $STS | jq .Account --raw-output)
# .npmrc in HOME
if [ -f "$HOME/.npmrc" ]; then
printf "\nDetected $HOME/.npmrc file that may interfere with the installation\n"
printf "Make sure that you have all settings properly setup (or delete it from $HOME/.npmrc)"
read -p "My .npmrc is correct (YyNn)?\n " -n 1 -r
[[ $REPLY =~ ^[Yy]$ ]] || exit 0
printf "\n"
fi
# DOCKER check
if (! docker stats --no-stream &> /dev/null); then
printf "Docker daemon is not running. Docker is mandatory to run for this deployment\n"
read -p "Start docker (YyNn)? " -n 1 -r
[[ $REPLY =~ ^[Yy]$ ]] || exit 0
printf "\n"
# Mac OS launch Docker
open /Applications/Docker.app
# Wait until Docker daemon is running and has completed initialisation
while (! docker stats --no-stream &> /dev/null); do
# Docker takes a few seconds to initialize
echo "Waiting for Docker to launch..."
sleep 5
done
fi
# Deploy project
printf "=====================================================================\n"
printf "Deployment for Schedule Optimization Application\n"
printf "=====================================================================\n"
printf "The following deployment steps will be performed:\n"
printf "\t1. Build the whole project\n"
printf "\t2. Build docker image for processing step and push it to ECR\n"
printf "\t3. CDK deploy the whole infrastructure\n"
printf "\n"
read -p "Ready (YyNn)? " -n 1 -r
[[ $REPLY =~ ^[Yy]$ ]] || exit 0
printf "\n"
printf "\n=====================================================================\n"
printf "Application Build\n"
printf "=====================================================================\n"
pushd opt_engine
sh build_schedule_optimization_app.sh
popd
printf "\n=====================================================================\n"
printf "CDK Application Build\n"
printf "=====================================================================\n"
yarn init-project
yarn build:infra
printf "\n=====================================================================\n"
printf "Create ECR \n"
printf "=====================================================================\n"
pushd infra
# Create Repository
cdk deploy EcrStack --profile "${DEPLOYMENT_PROFILE}" --region "${DEPLOYMENT_REGION}" --outputs-file ./ecr-stack.json
# Tag container image
export ECR_NAME_KEY=.[\""${DEPLOYMENT_NAMESPACE}-EcrStack"\"].EcrRepository
export DEPLOYMENT_ECR=$(cat ecr-stack.json | jq -r $ECR_NAME_KEY)
export DEPLOYMENT_CONTAINER_IMAGE=$ACCOUNT_ID.dkr.ecr.$DEPLOYMENT_REGION.amazonaws.com/$DEPLOYMENT_ECR:latest
rm ecr-stack.json
popd
printf "\n=====================================================================\n"
printf "Build Container image\n"
printf "=====================================================================\n"
pushd opt_engine/dist/schedule-optimization-app
# Docker Build
docker build --platform linux/amd64 -t "${DEPLOYMENT_CONTAINER_IMAGE}" .
printf "\n=====================================================================\n"
printf "Upload Container image\n"
printf "=====================================================================\n"
# ECR login
aws ecr get-login-password --profile "${DEPLOYMENT_PROFILE}" --region "${DEPLOYMENT_REGION}" | docker login --username AWS --password-stdin "${ACCOUNT_ID}.dkr.ecr.${DEPLOYMENT_REGION}.amazonaws.com"
# Push image
docker push "${DEPLOYMENT_CONTAINER_IMAGE}"
popd
printf "\n=====================================================================\n"
printf "Deploy infra structures\n"
printf "=====================================================================\n"
pushd infra
yarn cdk:bootstrap --profile "${DEPLOYMENT_PROFILE}" --region "${DEPLOYMENT_REGION}"
yarn dev:deploy --profile "${DEPLOYMENT_PROFILE}" --region "${DEPLOYMENT_REGION}"
popd
printf "\n"
printf "\nCDK deployment done."
printf "\n"