-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.sh
executable file
·170 lines (146 loc) · 4.67 KB
/
deploy.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
repo="$( cd "$(dirname "$0")" && pwd )"
source "${repo}/scripts/lib/common-functions.sh"
node_version=$( cat "$repo/.nvmrc" )
function main {
local -r env=$(parse_env_from_script_name)
case "${env}" in
"hahtuva" | "dev" | "qa" | "prod" | "util")
deploy "${env}"
;;
*)
fatal "Unknown env $env"
;;
esac
}
function deploy {
local -r env="$1"
require_docker
init_nodejs
cd "$repo/infra"
npm_ci_if_needed
if [ "${env}" == "util" ]; then
deploy_util
else
deploy_env "${env}"
fi
}
function deploy_util {
bootstrap_cdk
export_aws_credentials "util"
npx cdk --app "npx ts-node ${repo}/infra/src/cdk-app-util.ts" deploy --require-approval never --all
}
function deploy_env {
local -r env="$1"
if ! is_running_on_codebuild; then
export_aws_credentials "util"
local -r accountId=$(get_aws_account_id_of_env "${env}")
local -r region=$(get_aws_region_of_env "${env}")
export CDK_DEPLOY_TARGET_ACCOUNT=${accountId}
export CDK_DEPLOY_TARGET_REGION=${region}
fi
login_to_docker_if_possible
ENV=${env} npx cdk --app "npx ts-node ${repo}/infra/src/cdk-app.ts" deploy --require-approval never --all
}
function login_to_docker_if_possible {
if [ -n "${DOCKER_USERNAME:-}" ] && [ -n "${DOCKER_PASSWORD:-}" ]; then
info "Logging in to dockerhub"
echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin
else
info "Not logging into dockerhub"
fi
}
function bootstrap_cdk {
util_account_id=$( get_aws_account_id_of_env "util" )
local env
for env in util hahtuva dev qa prod; do
info "Bootstrapping $env"
bootstrap_cdk_for_env "${util_account_id}" "${env}"
done
}
function bootstrap_cdk_for_env {
local -r util_account_id=$1
local -r env=$2
local -r account_id=$(get_aws_account_id_of_env "${env}")
local -r region=$(get_aws_region_of_env "${env}")
export_aws_credentials "${env}"
info "Bootstrapping CDK for env ${env} at ${account_id}/${region}"
if [ "${env}" == "util" ]; then
npx cdk bootstrap "aws://${account_id}/${region}" \
--qualifier kayttooike --toolkit-stack-name KayttooikeusCDKToolkit
else
setup_cdk_deployment_target_policy
npx cdk bootstrap "aws://${account_id}/${region}" \
--qualifier kayttooike --toolkit-stack-name KayttooikeusCDKToolkit \
--trust "${util_account_id}" \
--trust-for-lookup "${util_account_id}" \
--cloudformation-execution-policies "arn:aws:iam::${account_id}:policy/KayttooikeusCDKDeploymentTargetPermissions"
info "Setting up CDK deployment target policy for env ${env}"
fi
}
function setup_cdk_deployment_target_policy {
npx ts-node "${repo}/infra/src/setup-cdk-deployment-target-policy.ts"
}
function is_running_on_codebuild {
[ -n "${CODEBUILD_BUILD_ID:-}" ]
}
function export_aws_credentials {
local -r env=$1
export AWS_PROFILE="oph-yleiskayttoiset-${env}"
if ! aws sts get-caller-identity >/dev/null; then
fatal "AWS credentials are not configured env $env. Aborting."
fi
}
function get_aws_region_of_env {
local -r env=$1
get_env_specific_param "${env}" region
}
function get_aws_account_id_of_env {
local -r env=$1
get_env_specific_param "${env}" account_id
}
function get_env_specific_param {
local -r env=$1
local -r param=$2
if ! is_running_on_codebuild; then
export_aws_credentials "util"
fi
aws ssm get-parameter --name "/env/${env}/${param}" --query Parameter.Value --output text
}
function get_aws_account_id {
aws sts get-caller-identity --query Account --output text
}
function npm_ci_if_needed {
require_command shasum
if [ ! -f "package.json" ]; then
fatal "package.json is missing"
elif [ ! -f "package-lock.json" ]; then
info "package-lock.json is missing"
npm install
elif [ ! -f "$( npm root )/package.json.checksum" ]; then
info "package.json checksum missing"
npm ci
elif [ ! -f "$( npm root )/package-lock.json.checksum" ]; then
info "package-lock.json checksum missing"
npm ci
elif ! shasum --check "$( npm root )/package.json.checksum"; then
info "package.json changed"
npm install
elif ! shasum --check "$( npm root )/package-lock.json.checksum"; then
info "package-lock.json changed"
npm ci
else
info "No changes in package.json or package-lock.json"
fi
shasum package-lock.json > "$( npm root )/package-lock.json.checksum"
shasum package.json > "$( npm root )/package.json.checksum"
}
function init_nodejs {
export NVM_DIR="${NVM_DIR:-$HOME/.cache/nvm}"
set +o errexit
source "$repo/scripts/lib/nvm.sh"
nvm use "${node_version}" || nvm install "${node_version}"
set -o errexit
}
main "$@"