Skip to content

Commit 9ef3b7d

Browse files
authored
Merge pull request #28 from sql-machine-learning/last_day_pr
polish start.bash and make it more user-friendly
2 parents 67f1699 + a63c070 commit 9ef3b7d

File tree

3 files changed

+72
-34
lines changed

3 files changed

+72
-34
lines changed

provision.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ apt-get update
1313

1414
echo "Installing Docker ..."
1515
# c.f. https://dockr.ly/3cExcay
16-
if ! which docker > /dev/null; then
16+
if which docker > /dev/null; then
1717
echo "Docker had been installed. Skip."
1818
else
1919
best_install_url=$(find_fastest_docker_url)

start.bash

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,27 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
echo "Docker pull SQLFlow images ..."
15+
cat <<EOF
16+
This script is safe to re-run, feel free to retry when it exits abnormally.
17+
Especially when we are waiting for a pod in Kubernetes cluster, it may
18+
pull image from registry and take a lot of time to startup.
19+
20+
EOF
21+
22+
if [[ "$(whoami)" != "root" ]]; then
23+
echo "Please change to root user and retry."
24+
exit 1
25+
fi
26+
27+
echo "Docker pull dependency images, you can comment this if already have them ..."
1628
# c.f. https://github.com/sql-machine-learning/sqlflow/blob/develop/.travis.yml
1729
docker pull sqlflow/sqlflow:jupyter
1830
docker pull sqlflow/sqlflow:mysql
1931
docker pull sqlflow/sqlflow:server
2032
docker pull sqlflow/sqlflow:step
33+
docker pull argoproj/argoexec:v2.7.7
34+
docker pull argoproj/argocli:v2.7.7
35+
docker pull argoproj/workflow-controller:v2.7.7
2136
echo "Done."
2237

2338
# NOTE: According to https://stackoverflow.com/a/16619261/724872,
@@ -29,6 +44,26 @@ source $(dirname $0)/sqlflow/docker/dev/find_fastest_resources.sh
2944
# Find a way that we do not need to use 'set -e'
3045
set +e
3146

47+
# Execute cmd until given output is present
48+
# or exit when timeout (50*3s)
49+
# "$1" is user message
50+
# "$2" is cmd
51+
# "$3" is expected output
52+
function wait_or_exit() {
53+
echo -n "Waiting for $1 "
54+
for i in {1..50}; do
55+
$2 | grep -o -q "$3"
56+
if [[ $? -eq 0 ]]; then
57+
echo "Done"
58+
return
59+
fi
60+
echo -n "."
61+
sleep 3
62+
done
63+
echo "Fail"
64+
exit
65+
}
66+
3267
# Use a faster kube image and docker registry
3368
echo "Start minikube cluster ..."
3469
minikube_status=$(minikube status | grep "apiserver: Running")
@@ -49,68 +84,64 @@ else
4984
fi
5085
fi
5186

52-
# Test if a Kubernetes resource is alive
87+
wait_or_exit "minikube" "minikube status" "apiserver: Running"
88+
89+
# Test if a Kubernetes pod is ready
5390
# "$1" shoulde be namespace id e.g. argo
54-
# "$2" should be resource id e.g. pod/argo-server
55-
function is_resource_alive() {
56-
local type=$(echo "$2" | cut -d / -f1)
57-
local name=$(echo "$2" | cut -d / -f2)
58-
if kubectl get -n "$1" "$2" | grep -q -o "$name" >/dev/null; then
59-
# make sure relative pod is alive
60-
if kubectl get pod -n "$1" | grep "$name" | grep "Running" >/dev/null; then
61-
echo "yes"
62-
else
63-
echo "no"
64-
fi
91+
# "$2" should be pod selector e.g. k8s-app=kubernetes-dashboard
92+
function is_pod_ready() {
93+
pod=$(kubectl get pod -n "$1" -l "$2" -o name | tail -1)
94+
if [[ -z "$pod" ]]; then
95+
echo "no"
96+
return
97+
fi
98+
ready=$(kubectl get -n "$1" "$pod" -o jsonpath='{.status.containerStatuses[0].ready}')
99+
if [[ "$ready" == "true" ]]; then
100+
echo "yes"
65101
else
66102
echo "no"
67103
fi
68104
}
69105

70106
echo "Start argo ..."
71-
argo_server_alive=$(is_resource_alive "argo" "service/argo-server")
107+
argo_server_alive=$(is_pod_ready "argo" "app=argo-server")
72108
if [[ "$argo_server_alive" == "yes" ]]; then
73109
echo "Already in running."
74110
else
75111
$(dirname $0)/sqlflow/scripts/travis/start_argo.sh
76112
fi
113+
wait_or_exit "argo" "is_pod_ready argo app=argo-server" "yes"
77114

78-
echo "Strat Kubernetes Dashboard..."
79-
dashboard_alive=$(is_resource_alive "kubernetes-dashboard" "service/kubernetes-dashboard")
115+
echo "Strat Kubernetes Dashboard ..."
116+
dashboard_alive=$(is_pod_ready "kubernetes-dashboard" "k8s-app=kubernetes-dashboard")
80117
if [[ "$dashboard_alive" == "yes" ]]; then
81118
echo "Already in running."
82119
else
83-
nohup minikube dashboard &
120+
nohup minikube dashboard >/dev/null 2>&1 &
84121
fi
122+
wait_or_exit "Kubernetes Dashboard" "is_pod_ready kubernetes-dashboard k8s-app=kubernetes-dashboard" "yes"
85123

86124
echo "Strat SQLFlow ..."
87-
sqlflow_alive=$(is_resource_alive "default" "pod/sqlflow-server")
125+
sqlflow_alive=$(is_pod_ready "default" "app=sqlflow-server")
88126
if [[ "$sqlflow_alive" == "yes" ]]; then
89127
echo "Already in running."
90128
else
91129
kubectl apply -f sqlflow/doc/run/k8s/install-sqlflow.yaml
92130
fi
131+
wait_or_exit "SQLFlow" "is_pod_ready default app=sqlflow-server" "yes"
93132

94133
# Kill port exposing if it already exist
95134
function stop_expose() {
96-
ps -elf | grep "kubectl port-forward" | grep "$1" | grep "$2" | awk '{print $4}' | xargs kill >/dev/null
135+
ps -elf | grep "kubectl port-forward" | grep "$1" | grep "$2" | awk '{print $4}' | xargs kill >/dev/null 2>&1
97136
}
98137

99138
# Kubernetes port-forwarding
100139
# "$1" should be namespace
101140
# "$2" should be resource, e.g. service/argo-server
102141
# "$3" should be port mapping, e.g. 8000:80
103142
function expose() {
104-
echo "Stop exposing $3..."
105143
stop_expose "$2" "$3"
106-
echo "Detecting service and exposing port ..."
107-
while [[ true ]]; do
108-
local alive=$(is_resource_alive "$1" "$2")
109-
if [[ "$alive" == "yes" ]]; then
110-
break
111-
fi
112-
sleep 1
113-
done
144+
echo "Exposing port for $2 at $3 ..."
114145
nohup kubectl port-forward -n $1 --address='0.0.0.0' $2 $3 >>port-forward-log 2>&1 &
115146
}
116147

@@ -119,18 +150,25 @@ expose kubernetes-dashboard service/kubernetes-dashboard 9000:80
119150
expose argo service/argo-server 9001:2746
120151
expose default pod/sqlflow-server 8888:8888
121152
expose default pod/sqlflow-server 3306:3306
153+
expose default pod/sqlflow-server 50051:50051
122154

123155
jupyter_addr=$(kubectl logs pod/sqlflow-server notebook | grep -o -E "http://127.0.0.1[^?]+\?token=.*" | head -1)
156+
mysql_addr="mysql://root:root@tcp($(kubectl get -o jsonpath='{.status.podIP}' pod/sqlflow-server))/?maxAllowedPacket=0"
124157

125-
cat <<EOF
158+
echo -e "
159+
\033[32m
126160
Congratulations, SQLFlow playground is up!
127161
128162
Access Jupyter Notebook at: $jupyter_addr
129163
Access Kubernetes Dashboard at: http://localhost:9000
130164
Access Argo Dashboard at: http://localhost:9001
131-
Access SQLFlow with cli: refer to https://github.com/sql-machine-learning/sqlflow/blob/develop/doc/run/cli.md
165+
Access SQLFlow with cli: ./sqlflow --datasource="\"$mysql_addr\""
132166
133167
Stop minikube with: minikube stop
134-
Stop vagrant with: vagrant halt
135-
EOF
168+
Stop vagrant vm with: vagrant halt
136169
170+
[Dangerous]
171+
Destroy minikube with: minikube delete && rm -rf ~/.minikube
172+
Destroy vagrant vm with: vagrant destroy
173+
\033[0m
174+
"

0 commit comments

Comments
 (0)