Files
userspace/createprojectspace.sh
2021-01-25 19:56:08 -05:00

45 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
export PROJECT=$1
export USER=$2
export SERVER=$3
export SERVER_PROJECT_DIR="~/.kube/projects/$PROJECT"
echo "Checking if project namespace exists"
exists=$(ssh $SERVER "kubectl get namespace --output=jsonpath=\"{.items[?(@.metadata.name=='$PROJECT')].metadata.name}\"")
if [ -z $exists ]; then
echo "Namespace not found, creating"
else
echo "Namespace exists, not overwriting"
exit 1
fi
echo "Creating namespace dir on server"
ssh $SERVER "mkdir -p $SERVER_PROJECT_DIR"
if [ $? -ne 0 ]; then
echo "Failed to create server project dir"
exit 1
fi
echo "Templating namespace with helm and copying to server"
helm template $PROJECT ./namespace --set user=$USER | ssh $SERVER "cat - > $SERVER_PROJECT_DIR/namespace.yaml"
if [ $? -ne 0 ]; then
echo "Failed to template namespace. Is helm installed?"
exit 1
fi
echo "Creating namespace from template"
ssh $SERVER "kubectl apply -f $SERVER_PROJECT_DIR/namespace.yaml"
if [ $? -ne 0 ]; then
echo "Failed to create namespace"
exit 1
fi
echo "adding server to config with new context $SERVER-$USER"
kubectl config set-context $SERVER-$USER-$PROJECT --cluster=$SERVER --namespace=$PROJECT --user=$USER-$SERVER
kubectl config set current-context $SERVER-$USER-$PROJECT
echo "done"