-
Notifications
You must be signed in to change notification settings - Fork 38
/
runExampleApp.sh
executable file
·208 lines (181 loc) · 4.54 KB
/
runExampleApp.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/bash
set -e
PREREQUISITES=("yarn" "node")
SCRIPT_DIR=$(
cd "$(dirname "$0")"
pwd
)
USE_FORMAT=$(
type tput >/dev/null 2>&1
if [ $? -eq 0 ]; then echo "true"; else echo "false"; fi
)
CREDENTIALS_FILE="${SCRIPT_DIR}/examples/discovery-search-app/ibm-credentials.env"
ENV_LOCAL_FILE="${SCRIPT_DIR}/examples/discovery-search-app/.env.local"
function boldMessage() {
if [ "$USE_FORMAT" == "true" ]; then
echo "$(tput bold)$1$(tput sgr 0)"
else
echo "$1"
fi
}
function colorMessage() {
if [ "$USE_FORMAT" == "true" ]; then
echo "$(tput setaf $2)$1$(tput sgr 0)"
else
echo "$1"
fi
}
function paddedMessage() {
echo
echo
boldMessage "$1"
}
function checkForDependencies() {
boldMessage "Checking for prerequisites..."
missing=0
for i in "${PREREQUISITES[@]}"; do
echo -n " $i: "
set +e
type $i >/dev/null 2>&1
exists=$?
set -e
if [ $exists -eq 0 ]; then
colorMessage "Y" 2
else
colorMessage "N" 1
fi
if [ $exists -ne 0 ]; then
((missing += 1))
fi
done
if [[ $missing -gt 0 ]]; then
echo
echo "Not all prerequistes are installed." >&2
exit 1
fi
}
function updateEnvFile() {
local authType
local url
local projectId
local username
local password
local apikey
local authTypeValid=false
paddedMessage "What kind of authentication are you using?"
echo "iam (Cloud)"
echo "cp4d (CP4D)"
echo
while ! $authTypeValid; do
read -p " authType: " authType
if [[ "$authType" == "iam" || "$authType" == "cp4d" ]]; then
authTypeValid=true
else
paddedMessage "Please provide a valid value (iam/cp4d)"
fi
done
if [[ "$authType" == "cp4d" ]]; then
paddedMessage "CP4D:"
echo " Provide the base URL (and port, if necessary) for your CP4D deployment. Example:"
colorMessage " https://zen-25-cpd-zen-25.apps.my-cluster-name.com" 3
echo
while [[ "$url" == "" ]]; do
read -p " url: " url
done
echo " Provide the credentials used to log into the CP4D dashboard."
while [[ "$username" == "" ]]; do
read -p " username: " username
done
while [[ "$password" == "" ]]; do
read -s -p " password: " password
done
cat >"$CREDENTIALS_FILE" <<EOL
DISCOVERY_AUTH_TYPE=${authType}
DISCOVERY_AUTH_URL="${url}/icp4d-api"
DISCOVERY_AUTH_DISABLE_SSL=true
DISCOVERY_USERNAME=${username}
DISCOVERY_PASSWORD=${password}
DISCOVERY_DISABLE_SSL=true
EOL
elif [[ "$authType" == "iam" ]]; then
paddedMessage "Cloud:"
echo " Provide the API URL and key for your Cloud instance. Example URL:"
colorMessage " https://api.us-south.discovery.cloud.ibm.com/instances/1234" 3
echo
while [[ "$url" == "" ]]; do
read -p " url: " url
done
while [[ "$apikey" == "" ]]; do
read -s -p " apikey: " apikey
done
cat >"$CREDENTIALS_FILE" <<EOL
DISCOVERY_AUTH_TYPE=${authType}
DISCOVERY_URL=${url}
DISCOVERY_APIKEY=${apikey}
EOL
else
echo "Unsupported authentication type: ${authType}"
exit 1
fi
paddedMessage "Project ID:"
echo " Copy one of available project IDs:"
node "${SCRIPT_DIR}/examples/discovery-search-app/scripts/listProjects.js"
while [[ "$projectId" == "" ]]; do
read -p " project_id: " projectId
done
cat >"$ENV_LOCAL_FILE" <<EOL
REACT_APP_PROJECT_ID=${projectId}
EOL
if [ $OSTYPE == 'msys' ]; then
echo "SASS_PATH=\"../../node_modules;src\"" >>"$ENV_LOCAL_FILE"
fi
echo
}
if [ "$USE_FORMAT" == "true" ]; then tput clear; fi
#
# check for missing prerequistes
#
checkForDependencies
#
# install dependencies
#
paddedMessage "Installing project dependencies..."
yarn --silent
colorMessage "done" 2
#
# collection discovery instance information
#
if [ -f "$CREDENTIALS_FILE" ]; then
paddedMessage "File already exists:"
colorMessage " $CREDENTIALS_FILE" 3
read -p "$(paddedMessage 'Update file before configuring server (y/n)? ')" updateEnvValues
if [[ "${updateEnvValues}" =~ ^(Y|y)$ ]]; then
updateEnvFile
fi
else
updateEnvFile
fi
#
# run server setup script
#
paddedMessage "Setting up server..."
yarn workspace discovery-search-app run server:setup
colorMessage "done" 2
#
# build discovery-react-components
#
paddedMessage "Building components..."
yarn run build:pkgs 2>/tmp/component_build
if [ $? -ne 0 ]; then
echo
cat /tmp/component_build
echo
echo
echo "Build failed with the above errors" >&2
exit 1
fi
colorMessage "done" 2
read -p "$(paddedMessage 'Start the example app now (y/n)? ')" startServer
if [[ "${startServer}" =~ ^(Y|y)$ ]]; then
yarn workspace discovery-search-app run start
fi