Page MenuHomePhabricator
Paste P9946

k8s_webservice.sh
ActivePublic

Authored by Bstorm on Dec 19 2019, 12:34 AM.
#!/bin/bash
# This is intended to be run as a tool user
set -Eeuo pipefail
function usage {
echo -e "Usage (must be toolforge user account):\n"
echo "k8s_webservice.sh <start|stop>"
echo ""
echo "Example: k8s_webservice.sh start"
}
function startsvc {
echo "starting..."
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
name: $1
toolforge: tool
tool.toolforge.org/service: "true"
name: $1
spec:
replicas: 1
selector:
matchLabels:
name: $1
toolforge: tool
tool.toolforge.org/service: "true"
template:
metadata:
labels:
name: $1
toolforge: tool
tool.toolforge.org/service: "true"
spec:
serviceAccountName: ${1}-obs
containers:
- command:
- /usr/bin/webservice-runner
- --type
- uwsgi-python
- --port
- "8000"
image: docker-registry.tools.wmflabs.org/toolforge-python37-sssd-web:latest
imagePullPolicy: Always
name: webservice
ports:
- containerPort: 8000
name: http
protocol: TCP
workingDir: /data/project/${1}/
EOF
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
labels:
name: $1
toolforge: tool
tool.toolforge.org/service: "true"
name: $1
spec:
ports:
- name: http
port: 8000
protocol: TCP
targetPort: 8000
selector:
name: $1
type: ClusterIP
EOF
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1/\$2
labels:
name: $1
toolforge: tool
tool.toolforge.org/service: "true"
name: $1
spec:
rules:
- host: tools.wmflabs.org
http:
paths:
- backend:
serviceName: $1
servicePort: 8000
path: /$1(/|$)(.*)
EOF
}
function stopsvc {
echo "stopping..."
echo "First the ingress"
kubectl delete ingress $1
echo "Now the service"
kubectl delete svc $1
echo "And the actual app"
kubectl delete deployment $1
}
wmcsproject=$(</etc/wmcs-project)
if ! [[ $USER == "${wmcsproject}."* ]]; then
printf >&2 '%s: user name does not start with "%s": %s\n' "$0" "$wmcsproject" "$USER"
usage
exit 1
fi
prefix=$(($(echo -n $wmcsproject | wc -c)+1))
tool="${USER:prefix}"
case $1 in
start)
startsvc "$tool"
;;
stop)
stopsvc "$tool"
;;
restart)
echo "For a restart, this seems kind of heavy"
echo "Why don't you just run \"kubectl get pods\" and the \"kubectl delete pods <podname>\"?"
;;
*)
echo "There must be an argument of start or stop"
usage
exit 1
;;
esac

Event Timeline

Bstorm updated the paste's language from autodetect to bash.Dec 19 2019, 3:46 PM
Bstorm edited the content of this paste. (Show Details)
bd808 edited the content of this paste. (Show Details)Dec 19 2019, 8:17 PM
bd808 added projects: Toolforge, Kubernetes.