aboutsummaryrefslogtreecommitdiffstats
path: root/tools/moon_kubernetes/init_k8s_moon.sh
blob: 00c1b4434fcd1a5e76b217391000305e6e9b36cc (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
#number of pods type that should be running or be stopped
declare -i pods_to_check=0
 #global variable on current namespace to check
current_namespace=""
#if set to 1 we check that the pods are running, otherwise we chack that the pods are stopped
declare -i check_running=1
#name of the pod to check
match_pattern=""
#postfix used to recognize pods name
OS="unknown_os"

#this function checks if a pod with name starting with $1 is in the Running / Stopped state depending on $heck_running
#   $1 : the name the pods starts with (without the random string added by kubernate to the pod name)
#   $2 : either the number of identical pods that shall be run or #
#   $3 : if $2 is #, the number of lines of the pods name appear on which the pod appears
function check_pod() {
   declare -i nb_arguments=$#
   match_pattern="$1"; shift
   if [ $nb_arguments -gt 2 ]; then
         shift; declare -i nb_pods_pattern="$1"
         if [ $check_running -eq 1 ]; then #check if pods are running
             declare -i result=$(sudo kubectl get po --namespace=${current_namespace} | grep  $match_pattern | grep "1/1" | grep -c "Running")
             if [ $result -eq $nb_pods_pattern ]; then
                pods_to_check=$pods_to_check+1
             fi
         else #check if pods are stopped
             declare -i result=$(sudo kubectl get po --namespace=${current_namespace} | grep  $match_pattern | grep -c "Running\|Terminating")
             if [ $result -eq 0 ]; then
                pods_to_check=$pods_to_check+1
             fi
         fi
   else
         declare -i nb=$1
         if [ $check_running -eq 1 ]; then #check if pods are running
             declare -i result=$(sudo kubectl get po --namespace=${current_namespace} |  grep  $match_pattern | grep "$nb/$nb" | grep -c "Running")
             if [ $result -eq 1 ]; then
                pods_to_check=$pods_to_check+1
             fi
         else #check if pods are stopped
             declare -i result=$(sudo kubectl get po --namespace=${current_namespace} |  grep  $match_pattern | grep -c "Running\|Terminating")
             if [ $result -eq 0 ]; then
                pods_to_check=$pods_to_check+1
             fi
         fi
   fi
}

#this function tests a list of pods
function check_pods() {
    current_namespace="${1}"; shift
    pods=("${@}")
    declare -i pods_nb=${#pods[@]}
    sleep 2
    while [ $pods_to_check -lt $pods_nb ]
    do
        pods_to_check=0
        for node in "${pods[@]}"
        do
            check_pod $node
        done

        if [ $check_running -eq 1 ]; then
            echo -ne "$pods_to_check node types on $pods_nb are running...\033[0K\r"
        else
            declare -i running_pods=$pods_nb-$pods_to_check
            echo -ne "$running_pods node types on $pods_nb are still running...\033[0K\r"
        fi
        sleep 2
    done
}

#this function checks if a list of pods ($2) in a specific namspace ($1) are in the Running state
function check_pods_running() {
   check_running=1
   check_pods "${@}"
   pods_to_check=0
}

#this function checks if a list of pods ($2) are not in a specific namspace ($1)
function check_pods_not_running() {
   check_running=0
   check_pods "${@}"
   pods_to_check=0
}

function wait_for_kubernate_calico() {
  echo -ne "Waiting for kubernate... "
  kube_namespace="kube-system"
  declare -a kube_pods=("calico-etcd 1" "calico-node 2" "calico-policy-controller 1" "etcd-${OS} 1" "kube-apiserver-${OS} 1" "kube-controller-manager-${OS} 1" "kube-dns 3" "kube-proxy 1" "kube-scheduler-${OS} 1")
  check_pods_running "$kube_namespace" "${kube_pods[@]}"
}

function wait_for_moon_init() {
 echo "Waiting for moon (consul, db, keystone) ..."
 kube_namespace="moon"
 declare -a kube_pods=("consul 1" "db 1" "keystone 1")
 check_pods_running "$kube_namespace" "${kube_pods[@]}"
}

function wait_for_moon_forming() {
 echo "Waiting for moon (forming) ..."
 kube_namespace="moon"
 declare -a kube_pods=("forming 1")
 check_pods_running "$kube_namespace" "${kube_pods[@]}"
}

function wait_for_moon_manager() {
 echo "Waiting for moon (manager) ..."
 kube_namespace="moon"
 declare -a kube_pods=("manager # 3")
 check_pods_running "$kube_namespace" "${kube_pods[@]}"
}

function wait_for_moon_end() {
 echo "Waiting for moon (orchestrator, gui) ..."
 kube_namespace="moon"
 declare -a kube_pods=("gui 1" "orchestrator 1")
 check_pods_running "$kube_namespace" "${kube_pods[@]}"
}

function wait_for_moon_forming_to_end() {
 echo "Waiting for moon forming to finish initialization. This can take few minutes..."
 kube_namespace="moon"
 declare -a kube_pods=("forming 1")
 check_pods_not_running "$kube_namespace" "${kube_pods[@]}"
}

function wait_for_moon_delete_to_end(){
  echo "Waiting for moon to terminate..."
  kube_namespace="moon"
  declare -a kube_pods=("consul 1" "db 1" "keystone 1" "manager # 3" "gui 1" "orchestrator 1")
  check_pods_not_running "$kube_namespace" "${kube_pods[@]}"
}

function check_os(){
 if [ -f /etc/os-release ]; then
    # freedesktop.org and systemd
    . /etc/os-release
    OS=${ID}
 elif type lsb_release >/dev/null 2>&1; then
    # linuxbase.org
    OS=$(lsb_release -si)
    declare -i result=$(grep -i "debian" $OS)
    if [ $result -eq 1 ]; then
        OS="debian"
    fi
    declare -i result=$(grep -i "ubuntu" $OS)
    if [ $result -eq 1 ]; then
        OS="ubuntu"
    fi
 elif [ -f /etc/lsb-release ]; then
    # For some versions of Debian/Ubuntu without lsb_release command
    . /etc/lsb-release
    OS=$DISTRIB_ID
    declare -i result=$(grep -i "debian" $OS)
    if [ $result -eq 1 ]; then
        OS="debian"
    fi
    declare -i result=$(grep -i "ubuntu" $OS)
    if [ $result -eq 1 ]; then
        OS="ubuntu"
    fi
 elif [ -f /etc/debian_version ]; then
    # Older Debian/Ubuntu/etc.
    declare -i result=$(grep -i "debian" $OS)
    if [ $result -eq 1 ]; then
        OS="debian"
    fi
    declare -i result=$(grep -i "ubuntu" $OS)
    if [ $result -eq 1 ]; then
        OS="ubuntu"
    fi
 elif [ -f /etc/SuSe-release ]; then
    # Older SuSE/etc.
    echo "TO DO : get the name of the OS at the end of the pods name"
 elif [ -f /etc/redhat-release ]; then
    # Older Red Hat, CentOS, etc.
    echo "TO DO : get the name of the OS at the end of the pods name"
 else
    # Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
    OS=$(uname -s)
    echo "TO DO : get the name of the OS at the end of the pods name"
 fi
 echo "postfix used to detect pods name : ${OS}"
}

declare -i nb_arguments=$#
declare -i init_kubernate=1

if [ $# -eq 1 ]; then
    if [ $1 == "moon" ]; then
       init_kubernate=0
    fi

    if [ $1 == "-h" ]; then
       echo "Usage : "
       echo " - 'bash tools/moon_kubernetes/init_k8s_moon.sh'        launches the kubernates platform and the moon platform."
       echo " - 'bash tools/moon_kubernetes/init_k8s_moon.sh moon'   launches the moon platform only. If the moon platform is already launched, it deletes and recreates it."
       echo " "
    fi
fi

if [ $init_kubernate -eq 1 ]; then
    check_os
    echo "=============================="
    echo "Launching kubernate  "
    echo "=============================="
    sudo kubeadm reset
    sudo swapoff -a
    sudo kubeadm init --pod-network-cidr=192.168.0.0/16 # network for Calico
    #sudo kubeadm init --pod-network-cidr=10.244.0.0/16 # network for Canal

    mkdir -p $HOME/.kube
    sudo cp -f /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config

    kubectl apply -f http://docs.projectcalico.org/v2.4/getting-started/kubernetes/installation/hosted/kubeadm/1.6/calico.yaml
    #kubectl apply -f https://raw.githubusercontent.com/projectcalico/canal/master/k8s-install/1.6/rbac.yaml
    #kubectl apply -f https://raw.githubusercontent.com/projectcalico/canal/master/k8s-install/1.6/canal.yaml
    #kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

    kubectl delete deployment kube-dns --namespace=kube-system
    kubectl apply -f tools/moon_kubernetes/templates/kube-dns.yaml
    kubectl taint nodes --all node-role.kubernetes.io/master- # malke the master also as a node

    kubectl proxy&

    wait_for_kubernate_calico

    echo "=============================="
    echo "Kubernate platform is ready ! "
    echo "=============================="
fi

echo "============================"
echo "Launching moon "
echo "============================"
#check if the moon platform is running, if so we terminate it
declare -i moon_is_running=$(sudo kubectl get namespace | grep -c moon)
if [ $moon_is_running -eq 1 ]; then
    sudo kubectl delete namespace moon
    wait_for_moon_delete_to_end
    sleep 2
fi

#launching moon
kubectl create namespace moon
kubectl create configmap moon-config --from-file tools/moon_kubernetes/conf/moon.conf -n moon
kubectl create configmap config --from-file ~/.kube/config -n moon
kubectl create configmap moon-policy-templates --from-file tests/functional/scenario_tests -n moon
kubectl create secret generic mysql-root-pass --from-file=tools/moon_kubernetes/conf/password_root.txt -n moon
kubectl create secret generic mysql-pass --from-file=tools/moon_kubernetes/conf/password_moon.txt -n moon

kubectl create -n moon -f tools/moon_kubernetes/templates/consul.yaml
kubectl create -n moon -f tools/moon_kubernetes/templates/db.yaml
kubectl create -n moon -f tools/moon_kubernetes/templates/keystone.yaml
wait_for_moon_init


kubectl create -n moon -f tools/moon_kubernetes/templates/moon_forming.yaml
wait_for_moon_forming


kubectl create -n moon -f tools/moon_kubernetes/templates/moon_manager.yaml
wait_for_moon_manager


kubectl create -n moon -f tools/moon_kubernetes/templates/moon_orchestrator.yaml
kubectl create -n moon -f tools/moon_kubernetes/templates/moon_gui.yaml
wait_for_moon_end

#wait the end of pods initialization performed by moon forming
wait_for_moon_forming_to_end

echo "==========================                   "
echo "Moon platform is ready !"
echo "=========================="