-
Notifications
You must be signed in to change notification settings - Fork 53
/
local.yml.sh
executable file
·170 lines (139 loc) · 4.74 KB
/
local.yml.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
#!/usr/bin/env bash
#Exit script on first error
set -e
# Copies dev.yml to local.yml and substitutes some variables, e.g. STROOM_HOST
# Also copies proxy-dev.yml to proxy-local.yml in the same way
#Shell Colour constants for use in 'echo -e'
# shellcheck disable=SC2034
{
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
NC='\033[0m' # No Color
}
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
STROOM_SOURCE_CONF_FILE="${SCRIPT_DIR}/stroom-app/dev.yml"
PROXY_SOURCE_CONF_FILE="${SCRIPT_DIR}/stroom-proxy/stroom-proxy-app/proxy-dev.yml"
STROOM_LOCAL_CONF_FILE_NAME=local.yml
STROOM_LOCAL_2_CONF_FILE_NAME=local2.yml
STROOM_LOCAL_3_CONF_FILE_NAME=local3.yml
PROXY_LOCAL_CONF_FILE_NAME=proxy-local.yml
STROOM_LOCAL_CONF_FILE=${SCRIPT_DIR}/${STROOM_LOCAL_CONF_FILE_NAME}
STROOM_LOCAL_2_CONF_FILE=${SCRIPT_DIR}/${STROOM_LOCAL_2_CONF_FILE_NAME}
STROOM_LOCAL_3_CONF_FILE=${SCRIPT_DIR}/${STROOM_LOCAL_3_CONF_FILE_NAME}
PROXY_LOCAL_CONF_FILE=${SCRIPT_DIR}/${PROXY_LOCAL_CONF_FILE_NAME}
set_ip_address(){
if [ "$(uname)" == "Darwin" ]; then
# Code required to find IP address is different in MacOS
ip=$(ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk 'NR==1{print $2}')
else
local ip_binary
# If ip is not on the path (as seems to be the case with ansible) then
# try using /sbin instead.
if command -v ip > /dev/null; then
ip_binary="ip"
elif command -v /sbin/ip > /dev/null; then
ip_binary="/sbin/ip"
else
echo
echo -e "${RED}ERROR${NC} Unable to locate ${BLUE}ip${NC} command." >&2
exit 1
fi
ip=$( \
"${ip_binary}" route get 1 \
| awk 'match($0,"src [0-9\\.]+") {print substr($0,RSTART+4,RLENGTH-4)}')
fi
echo
echo -e "Using IP ${GREEN}${ip}${NC} as the IP, as determined from" \
"the operating system"
}
check_source_exists() {
local source="$1"; shift
if [ ! -f "${source}" ]; then
echo -e "${RED}Source file ${GREEN}${source}${RED}" \
"does not exist${NC}"
exit 1
fi
}
backup_current_local_file() {
local source="$1"; shift
local backup_file
backup_file="${source}.$(date +"%Y%m%dT%H%M")"
if [ -f "${source}" ]; then
backup_file="${source}.$(date +"%Y%m%dT%H%M")"
echo -e "Backing up ${GREEN}${source}${NC} to ${GREEN}${backup_file}${NC}"
cp "${source}" "${backup_file}"
fi
}
make_local2_file() {
echo -e "Overwriting ${GREEN}${STROOM_LOCAL_2_CONF_FILE}${NC} as a node 2" \
"variant of ${GREEN}${STROOM_LOCAL_CONF_FILE}${NC}"
sed \
-r \
-e 's/node1a/node2a/' \
-e 's/(port:.*[^\d])8080/\110080/' \
-e 's/(port:.*[^\d])8081/\110081/' \
< "${STROOM_LOCAL_CONF_FILE}" \
> "${STROOM_LOCAL_2_CONF_FILE}"
}
make_local3_file() {
echo -e "Overwriting ${GREEN}${STROOM_LOCAL_3_CONF_FILE}${NC} as a node 3" \
"variant of ${GREEN}${STROOM_LOCAL_CONF_FILE}${NC}"
sed \
-r \
-e 's/node1a/node3a/' \
-e 's/(port:.*[^\d])8080/\120080/' \
-e 's/(port:.*[^\d])8081/\120081/' \
< "${STROOM_LOCAL_CONF_FILE}" \
> "${STROOM_LOCAL_3_CONF_FILE}"
}
create_local_file() {
local source="$1"; shift
local dest="$1"; shift
echo -e "Overwriting ${GREEN}${dest}${NC} with a version" \
"templated from ${GREEN}${source}${NC}"
#Use '#' delimiter in HOME_DIR sed script as HOME contains '\'
sed \
-e "s/<<<IP_ADDRESS>>>/${ip}/g" \
-e "s#\${HOME_DIR[^}]*}#${HOME}#g" \
< "${source}" \
> "${dest}"
}
diff_against_backup() {
local file="$1"; shift
local backup_file
backup_file="${file}.$(date +"%Y%m%dT%H%M")"
if ! diff -q "${backup_file}" "${file}" > /dev/null; then
echo
echo -e "Run the following to see the changes made to your" \
"${file} file"
echo -e "${GREEN}vimdiff ${backup_file} ${file}${NC}"
else
echo -e "Backup file is identical to new local config file," \
"deleting backup file ${backup_file}"
rm "${backup_file}"
fi
}
main(){
set_ip_address
#Ensure various dirs exist
mkdir -p /tmp/stroom
check_source_exists "${STROOM_SOURCE_CONF_FILE}"
check_source_exists "${PROXY_SOURCE_CONF_FILE}"
backup_current_local_file "${STROOM_LOCAL_CONF_FILE}"
backup_current_local_file "${STROOM_LOCAL_2_CONF_FILE}"
backup_current_local_file "${STROOM_LOCAL_3_CONF_FILE}"
backup_current_local_file "${PROXY_LOCAL_CONF_FILE}"
create_local_file "${STROOM_SOURCE_CONF_FILE}" "${STROOM_LOCAL_CONF_FILE}"
# Make files for nodes 2 and 3 based on a copy of node 1
make_local2_file
make_local3_file
create_local_file "${PROXY_SOURCE_CONF_FILE}" "${PROXY_LOCAL_CONF_FILE}"
diff_against_backup "${STROOM_LOCAL_CONF_FILE}"
diff_against_backup "${STROOM_LOCAL_2_CONF_FILE}"
diff_against_backup "${STROOM_LOCAL_3_CONF_FILE}"
diff_against_backup "${PROXY_LOCAL_CONF_FILE}"
exit 0
}
main "$@"