Created
July 21, 2016 15:15
-
-
Save binarin/6e74fa82e8266e16701ac90fb8d9ba11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# minikube'd CCP rabbit | |
set -eu | |
set -o pipefail | |
main() { | |
case "$1" in | |
init) | |
step ensure-minikube-running | |
step ensure-registry | |
step honor-insecure-registry-in-minikube | |
step set-docker-credentials | |
step generate-ccp-config | |
step mark-minikube-as-openstack-controller | |
step init-virtualenv | |
;; | |
refresh) | |
activate-virtualenv | |
set-docker-credentials | |
ensure-deployed-etcd | |
destroy-existing-rabbit | |
build rabbitmq | |
deploy rabbitmq | |
kubectl scale --namespace=demo deployment/rabbitmq --replicas=3 | |
;; | |
*) | |
activate-virtualenv | |
set-docker-credentials | |
python -m microservices.cli --config-file=~/ccp.conf "$@" | |
;; | |
esac | |
} | |
step() { | |
echo "========= Running $1" | |
"$@" | |
} | |
ensure-minikube-running() { | |
case $(minikube status) in | |
Running) | |
return 0;; | |
*) | |
minikube start --cpus=2 --memory=4096 --vm-driver=kvm | |
;; | |
esac | |
} | |
honor-insecure-registry-in-minikube() { | |
minikube ssh -- sudo sed -e "s/EXTRA_ARGS=\\'.*/EXTRA_ARGS=\\'--insecure-registry=$(minikube ip):31500/" -i /var/lib/boot2docker/profile | |
minikube ssh -- sudo /etc/init.d/docker restart | |
} | |
ensure-registry() { | |
pod-exists default registry || kubectl create -f - <<EOF | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: registry | |
labels: | |
app: registry | |
spec: | |
containers: | |
- name: registry | |
image: registry:2 | |
env: | |
imagePullPolicy: Always | |
ports: | |
- containerPort: 5000 | |
hostPort: 5000 | |
EOF | |
service-exists default registry || kubectl create -f - <<EOF | |
kind: "Service" | |
apiVersion: "v1" | |
metadata: | |
name: "registry" | |
spec: | |
selector: | |
app: "registry" | |
ports: | |
- | |
protocol: "TCP" | |
port: 5000 | |
targetPort: 5000 | |
nodePort: 31500 | |
type: "NodePort" | |
EOF | |
} | |
pod-exists() { | |
local namespace=${1:?} | |
local pod=${2:?} | |
if kubectl --namespace=$namespace get pods $pod > /dev/null 2>&1 ; then | |
return 0 | |
fi | |
return 1 | |
} | |
service-exists() { | |
local namespace=${1:?} | |
local service=${2:?} | |
if kubectl --namespace=$namespace get services $service > /dev/null 2>&1 ; then | |
return 0 | |
fi | |
return 1 | |
} | |
object-exists() { | |
local namespace=${1:?} | |
local type=${2:?} | |
local name=${3:?} | |
if kubectl --namespace=$namespace get $type $name > /dev/null 2>&1 ; then | |
return 0 | |
fi | |
return 1 | |
} | |
set-docker-credentials() { | |
eval $(minikube docker-env) | |
} | |
generate-ccp-config() { | |
cat <<EOF > ~/ccp.conf | |
[DEFAULT] | |
deploy_config = ~/globals.yaml | |
[builder] | |
push = True | |
build-base-images-if-not-exist = True | |
[registry] | |
address = "$(minikube ip):31500" | |
[kubernetes] | |
namespace = "demo" | |
server = "https://$(minikube ip):8443" | |
key_file = "/home/binarin/.minikube/apiserver.key" | |
cert_file = "/home/binarin/.minikube/apiserver.crt" | |
ca_certs = "/home/binarin/.minikube/ca.crt" | |
[repositories] | |
skip_empty = True | |
EOF | |
} | |
mark-minikube-as-openstack-controller() { | |
kubectl label nodes minikubevm --overwrite openstack-controller=true > /dev/null 2>&1 | |
} | |
init-virtualenv() { | |
if [[ ! -d .venv ]]; then | |
virtualenv $(pwd)/.venv | |
fi | |
activate-virtualenv | |
pip install -U . | |
python setup.py develop | |
} | |
activate-virtualenv() { | |
set +u | |
. $(pwd)/.venv/bin/activate | |
set -u | |
} | |
ensure-deployed-etcd() { | |
if ! service-exists demo etcd; then | |
build etcd | |
deploy etcd | |
fi | |
} | |
destroy-existing-rabbit() { | |
local config_map | |
for config_map in $(kubectl --namespace=demo get configmap | grep rabbit | awk '{print $1}'); do | |
kubectl --namespace=demo delete "configmap/$config_map" | |
done | |
if object-exists demo deployment rabbitmq; then | |
kubectl --namespace=demo delete deployment/rabbitmq | |
fi | |
if service-exists demo rabbitmq ; then | |
kubectl --namespace=demo delete service/rabbitmq | |
fi | |
if pod-exists demo rabbitmq ; then | |
kubectl --namespace=demo delete pod/rabbitmq | |
fi | |
local try_no | |
for try_no in $(seq 1 20); do | |
if ! pod-exists demo rabbitmq; then | |
return 0 | |
fi | |
sleep 1 | |
done | |
echo "Rabbit pod not stopped" | |
return 1 | |
} | |
build() { | |
local component=${1:?} | |
python -m microservices.cli --config-file=~/ccp.conf build -c $component | |
} | |
deploy() { | |
local component=${1:?} | |
python -m microservices.cli --config-file=~/ccp.conf deploy -c fuel-ccp-$component | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment