-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathk8s-generate
More file actions
executable file
·137 lines (108 loc) · 4.28 KB
/
Copy pathk8s-generate
File metadata and controls
executable file
·137 lines (108 loc) · 4.28 KB
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
#!/bin/bash
set -ex
APP_ROOT=$(git rev-parse --show-toplevel)
cd $APP_ROOT
function usage() {
cat << EOF
usage: $0 <config-dir> <output-dir>
examples:
Use containers and map-data version tags as specified in bin/_headway_version.sh
$0 builds/planet planet
Use containers from a tag other than what's in bin/_headway_version.sh
HEADWAY_DATA_TAG=dev $0 builds/planet planet-dev
Use containers and map-data version tags other than whats specified in bin/_headway_version.sh
HEADWAY_DATA_TAG=dev HEADWAY_CONTAINER_TAG=dev $0 builds/Seattle seattle-dev
EOF
}
if [ ! $# -eq 2 ]; then
usage
exit 1
fi
CONFIG_DIR="$1"
OUTPUT_DIR="$2"
if [ -z "$HEADWAY_DATA_TAG" ]; then
source "bin/_headway_version.sh"
fi
export HEADWAY_DATA_TAG
export HEADWAY_CONTAINER_TAG="${HEADWAY_CONTAINER_TAG:-latest}"
source bin/_source-env.sh "$CONFIG_DIR"
set -a
source ".bin-env"
set +a
EXISTING_DEPLOYMENT_CONFIG="${OUTPUT_DIR}/deployment-config.yaml"
PELIAS_CONFIG_FILE="data/${HEADWAY_AREA}/${HEADWAY_AREA}.pelias.json"
if [ -f "$PELIAS_CONFIG_FILE" ]; then
PELIAS_CONFIG_JSON=$(cat $PELIAS_CONFIG_FILE)
elif [ -f "$EXISTING_DEPLOYMENT_CONFIG" ]; then
echo "Warning: ${PELIAS_CONFIG_FILE} not found; extracting pelias config from existing ${EXISTING_DEPLOYMENT_CONFIG}"
PELIAS_CONFIG_JSON=$(python3 -c "import yaml, sys; d=yaml.safe_load(sys.stdin); print(d['data']['pelias-config-json'], end='')" < "$EXISTING_DEPLOYMENT_CONFIG")
else
echo "Missing pelias config file: ${PELIAS_CONFIG_FILE} and no existing rendered output to fall back on"
exit 1
fi
indent4() { while IFS= read -r line; do printf ' %s\n' "$line"; done; }
export PELIAS_CONFIG_JSON_YAML="|
$(echo "$PELIAS_CONFIG_JSON" | indent4)"
function render_template() {
local template_file=$1
local output="${2:-$(basename $template_file | sed 's/\.tpl$//')}"
envsubst < "$template_file" > "${OUTPUT_DIR}/${output}"
}
set -o nounset
mkdir -p "$OUTPUT_DIR"
rm -f "${OUTPUT_DIR}"/*
function generate_transit() {
local otp_endpoints=""
local separator=", "
for headway_transit_area in $HEADWAY_TRANSIT_AREAS; do
local otp_endpoint_name=$(generate_otp $headway_transit_area);
otp_endpoints+="${separator}\"http://${otp_endpoint_name}:8000/otp/routers\""
done
export OTP_ENDPOINTS_JSON="[\"http://valhalla:8002\"${otp_endpoints}]"
render_template "k8s/_template/travelmux-deployment.yaml.tpl"
}
function log() {
echo $@ 1>&2
}
function generate_otp() {
local headway_transit_area=$1
if [ -z "$headway_transit_area" ]; then
echo "must specify headway_transit_area to $0"
exit 1
fi
# Lowercase transit area for idiomatic k8s names
export TRANSIT_AREA=$(echo $headway_transit_area | tr '[:upper:]' '[:lower:]')
export OTP_GRAPH_URL="${HEADWAY_K8S_TEMPLATE_ARTIFACT_ROOT}/${HEADWAY_DATA_TAG}/${HEADWAY_AREA_TAG}/${headway_transit_area}.graph.obj.zst"
export OTP_ENDPOINT_NAME="opentripplanner-${TRANSIT_AREA}"
local router_config_file="${CONFIG_DIR}/transit/${headway_transit_area}-router-config.json"
if [ -f "$router_config_file" ]; then
export OTP_ROUTER_CONFIG_JSON_YAML="|
$(indent4 < "$router_config_file")"
else
# blank keys or "null" keys don't appear in the config map
# which causes an error in pod creation
export OTP_ROUTER_CONFIG_JSON_YAML='""'
fi
export OTP_CONFIG_JSON_YAML="|
$(indent4 < "services/otp/otp-config.json")"
for file in k8s/_template/opentripplanner*.tpl; do
output=$(basename "$file" | sed 's/\.tpl$//' | sed "s/AREA/${TRANSIT_AREA}/")
render_template "$file" "$output"
done
echo "$OTP_ENDPOINT_NAME"
}
if [[ "$HEADWAY_ENABLE_TRANSIT_ROUTING" != 0 ]]; then
generate_transit
fi
# NOTE: we *do* want to enable the travelmux *service*, regardless
# of if transit routing is enabled - otherwise our nginx frontend will fail
# to start up due to the inability to resolve DNS. A better solution might
# be to remove the travelmux section from nginx altogether when transit
# routing is disabled, but that's beyond the capabilities of the simple
# envsubst we're using. For now then, we copy in the service defintion.
render_template "k8s/_template/travelmux-service.yaml.tpl"
shopt -s extglob
for file in k8s/_template/!(opentripplanner*|travelmux*).tpl
do
render_template $file
done