Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Converted readiness.py to bash script. #256

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 0 additions & 104 deletions mariadb/templates/bin/_peer-finder.py.tpl

This file was deleted.

142 changes: 142 additions & 0 deletions mariadb/templates/bin/_peer-finder.sh.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/bin/sh
# Copyright 2017 The Openstack-Helm Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#set -ex


TOKEN_FILE='/var/run/secrets/kubernetes.io/serviceaccount/token'

# check for 3 arguments

# ./myscript.sh -e conf -s /etc -l /usr/lib /etc/hosts
# Use -gt 1 to consume two arguments per pass in the loop (e.g. each
# argument has a corresponding value to go with it).
# Use -gt 0 to consume one or more arguments per pass in the loop (e.g.
# some arguments don't have a corresponding value to go with it such
# as in the --default example).
# note: if this is set to -gt 0 the /etc/hosts part is not recognized ( may be a bug )


# ensure correct command line arguments are passed
if [[ $# -eq 2 ]] ; then
echo "peer-finder: service $1 found and value of $2 for force cluster"
SERVICE_NAME=$1
FORCE_ONLY_MEMBERS=$2
else
echo 'peer-finder: You need to pass argument <service name> <1|0 for force cluster members>'
exit 1
fi

# print galera cluster address
while [ 1 ]
do

# get service endpoint
URL="https://kubernetes.default.svc.cluster.local/api/v1/namespaces/$NAMESPACE/endpoints/$SERVICE_NAME"
echo $URL

# read token file
TOKEN=$(<$TOKEN_FILE)

STATUS=$?
if [ $STATUS -eq 0 ]; then
echo "token read"
echo $TOKEN
else
echo 'Unable to open a file with token.'
# not sure if service should be up or keep retrying
#exit 1
fi


# get json output string
DATA = curl -k -H "Authorization: Bearer $TOKEN" $URL

startSearching=false # ignore lines in json output until "subsets" found. start searching
hasAddresses=false # if found subsets, make sure there is 'addresses' section
seedFound=false # found seed job, get last IP as IP comes before name with seed in it
lastIP=""
ips=()
names=()

IP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')

while read -r line1; do

# remove double quotes from line
line=$(echo $line1 | sed 's/\"//g')

if [[ $line == *"subsets"* ]]; then
# found subsets section, start looking for ip addresses
startSearching=true
fi

if [[ $line == *seed* && $FORCE_ONLY_MEMBERS == 0 ]]; then
# found seed running, so return lastIP only
echo "--wsrep_cluster_address=gcomm://${lastIP%?}"
exit 0 #enable production
fi

if [[ $line == *ports* ]]; then
# the line with port has ip address also,
# so want to skip processing below
break
fi

if [[ $startSearching == true ]]; then
# reached subsets, starting parsing IP's
if [[ $line == *addresses* ]]; then
# making sure we have addresses with IP's
hasAddresses=true
fi

if [[ $line == *ip* ]]; then
# seems like ip key and actual IP on different line
# ignore ip key and add actual ip to array

do
if [[ ! $word =~ ^.ip.:.* ]]; then

if [[ $word == IP ]]; then
# skip adding current IP to ips list
break
fi

# add ip address to ips list
# remove "ip:" from line if it exists
ips=("${ips[@]}" $(echo $word | sed 's/ip\://g') )
lastIP=$word
fi
done
fi
fi

done <<< "$DATA"

if [[ $hasAddresses == true ]]; then
# loop through IP and add to result string
RESULT=""
for i in "${ips[@]}"; do
RESULT="$RESULT$i"
done

# return IP in format = 192.168.120.65,192.168.120.66
# remove trailing comma
echo "--wsrep_cluster_address=gcomm://${RESULT%?}"
exit 0
fi

done

41 changes: 0 additions & 41 deletions mariadb/templates/bin/_readiness.py.tpl

This file was deleted.

42 changes: 42 additions & 0 deletions mariadb/templates/bin/_readiness.sh.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/sh
# Copyright 2017 The Openstack-Helm Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -ex

SLEEP_TIMEOUT=3

# loop forever
while [ 1 ]
do

mysql --host=localhost --port="{{ .Values.network.port.mariadb }}" --user=root --password="{{ .Values.database.root_password }}" -e'show databases;' > /dev/null 2> /dev/null


STATUS=$?
if [ $STATUS -eq 0 ]; then

# mysql is fine and return success
/bin/echo "Service OK"
exit 0
else
# mysql service is unavailable and keep looping
/bin/echo "Service Unavailable"

fi


# check x amount of seconds
sleep $SLEEP_TIMEOUT
done
2 changes: 1 addition & 1 deletion mariadb/templates/bin/_seed.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function wait_for_cluster {
continue
fi
# Count number of endpoint separators.
ENDPOINTS_CNT=`python /tmp/peer-finder.py mariadb 1 | grep -o ',' | wc -l`
ENDPOINTS_CNT=`bash /tmp/peer-finder.sh mariadb 1 | grep -o ',' | wc -l`
# TODO(tomasz.paszkowski): Fix a corner case when only one endpoint is on the list.
# Add +1 for seed node and +1 as first item does not have a separator
ENDPOINTS_CNT=$(($ENDPOINTS_CNT+2))
Expand Down
2 changes: 1 addition & 1 deletion mariadb/templates/bin/_start.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ else
# to look for
sleep 30

export WSREP_OPTIONS=`python /tmp/peer-finder.py mariadb 0`
export WSREP_OPTIONS=`bash /tmp/peer-finder.sh mariadb 0`
exec mysqld --defaults-file=/etc/my.cnf \
--console \
--bind-address="0.0.0.0" \
Expand Down
8 changes: 4 additions & 4 deletions mariadb/templates/configmap-bin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ metadata:
data:
start.sh: |
{{ tuple "bin/_start.sh.tpl" . | include "helm-toolkit.template" | indent 4 }}
peer-finder.py: |
{{ tuple "bin/_peer-finder.py.tpl" . | include "helm-toolkit.template" | indent 4 }}
readiness.py: |
{{ tuple "bin/_readiness.py.tpl" . | include "helm-toolkit.template" | indent 4 }}
peer-finder.sh: |
{{ tuple "bin/_peer-finder.sh.tpl" . | include "helm-toolkit.template" | indent 4 }}
readiness.sh: |
{{ tuple "bin/_readiness.sh.tpl" . | include "helm-toolkit.template" | indent 4 }}
bootstrap-db.sh: |
{{ tuple "bin/_bootstrap-db.sh.tpl" . | include "helm-toolkit.template" | indent 4 }}
seed.sh: |
Expand Down
12 changes: 6 additions & 6 deletions mariadb/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ spec:
readinessProbe:
exec:
command:
- python
- /mariadb-readiness.py
- bash
- /tmp/readiness.sh
initialDelaySeconds: 60
volumeMounts:
- name: mycnfd
Expand All @@ -108,11 +108,11 @@ spec:
mountPath: /tmp/bootstrap-db.sh
subPath: bootstrap-db.sh
- name: peerfinder
mountPath: /tmp/peer-finder.py
subPath: peer-finder.py
mountPath: /tmp/peer-finder.sh
subPath: peer-finder.sh
- name: readiness
mountPath: /mariadb-readiness.py
subPath: readiness.py
mountPath: /tmp/readiness.sh
subPath: readiness.sh
- name: charsets
mountPath: /etc/my.cnf.d/charsets.cnf
subPath: charsets.cnf
Expand Down
Loading