require 'sct/command_interface' module Sct class ClusterCommand IS_PUBLIC_COMMAND = true SYNTAX = 'sct cluster up | sct cluster down | sct cluster reset | sct cluster update-config' SUMMARY = 'Starts/stops/resets or updates the config of the minikube cluster.' EXAMPLE = 'sct cluster up | sct cluster down | sct cluster reset | sct cluster update-config' EXAMPLE_DESCRIPTION = 'Starts/stops/resets or updates the config of the minikube cluster.' DESCRIPTION = "sct cluster allows you to start, stop, reset or update the config of the Spend Cloud minikube cluster." OPTIONS = [] def execute(args, options) return puts "SCT has not been initialized. Run 'sct init' first.".red unless Sct::Config.exists case args[0] when "up" up when "down" down when "update-config" update_config when "reset" reset else puts "Unknown or missing argument. Please run 'sct cluster up','sct cluster down' or 'sct cluster update-config'".red end end def up start system "kubectl delete pod -n kube-system $(kubectl get pods -n kube-system | grep registry-creds | awk '{print $1}')" system "kubectl rollout status -n kube-system deployment/registry-creds" post_start end def down system "#{minikube} stop" end def update_config if Sct::Helpers.operatingSystem == Sct::Helpers::WINDOWS windows_home_path = Sct::Helpers.windowsHomePath kube_file_path = windows_home_path+"/.kube/config" if !File.exists?(kube_file_path) return puts "#{kube_file_path} doesn't exist".red end system("sed -e 's~\\\\~/~g' -e 's~C:~/mnt/c~g' < #{kube_file_path} > ~/.kube/minikube-config") puts "#{kube_file_path} copied to ~/.kube/minikube-config".green end system "kubectl config use-context minikube" end def reset system "#{minikube} delete" start system "kubectl create secret generic gcloud-credentials --from-file=\"$(echo ~)/.config/gcloud/application_default_credentials.json\"" system "kubectl create secret generic -n kube-system registry-creds-dpr --from-literal DOCKER_PRIVATE_REGISTRY_PASSWORD=changeme --from-literal DOCKER_PRIVATE_REGISTRY_SERVER=changeme --from-literal DOCKER_PRIVATE_REGISTRY_USER=changeme" system "kubectl patch secret -n kube-system registry-creds-dpr -p='{\"metadata\": {\"labels\": { \"app\": \"registry-creds\", \"cloud\": \"dpr\", \"kubernetes.io/minikube-addons\": \"registry-creds\"}}}'" system "kubectl create secret generic -n kube-system registry-creds-ecr --from-literal AWS_ACCESS_KEY_ID=changeme --from-literal AWS_SECRET_ACCESS_KEY=changeme --from-literal AWS_SESSION_TOKEN=\"\" --from-literal aws-account=changeme --from-literal aws-assume-role=changeme --from-literal aws-region=changeme" system "kubectl patch secret -n kube-system registry-creds-ecr -p='{\"metadata\": {\"labels\": { \"app\": \"registry-creds\", \"cloud\": \"ecr\", \"kubernetes.io/minikube-addons\": \"registry-creds\"}}}'" system "kubectl create secret generic -n kube-system registry-creds-gcr --from-file=\"$(echo ~)/.config/gcloud/application_default_credentials.json\" --from-literal=gcrurl=\"https://eu.gcr.io\"" system "kubectl patch secret -n kube-system registry-creds-gcr -p='{\"metadata\": {\"labels\": { \"app\": \"registry-creds\", \"cloud\": \"gcr\", \"kubernetes.io/minikube-addons\": \"registry-creds\"}}}'" system "kubectl create secret generic -n kube-system registry-creds-acr --from-literal ACR_PASSWORD=changeme --from-literal ACR_CLIENT_ID=changeme --from-literal ACR_URL=changeme" system "kubectl patch secret -n kube-system registry-creds-acr -p='{\"metadata\": {\"labels\": { \"app\": \"registry-creds\", \"cloud\": \"acr\", \"kubernetes.io/minikube-addons\": \"registry-creds\"}}}'" system "#{minikube} addons enable registry-creds" system "#{minikube} addons enable ingress" system "kubectl rollout status -n kube-system deployment/registry-creds" system "kubectl rollout status -n kube-system deployment/nginx-ingress-controller" wait_for_gcr_secret system "kubectl apply -f ~/development/spend-cloud/k8s/" post_start end def start system "#{minikube} start --cpus=$(cat /proc/cpuinfo | grep processor | wc -l) --memory=16G" system "#{minikube} ssh -- 'sudo su -c \"echo 10048576 > /proc/sys/fs/inotify/max_user_watches\"'" update_config end def post_start system "kubectl rollout status deployment/spend-cloud-api" system "sudo sct hostfile" puts "\nāœ”ļø You can visit your environment at šŸ‘‰ https://spend-cloud.spend.cloud.local šŸ‘Œ (might still take a few minutes)" end def wait_for_gcr_secret puts "Waiting for Google Cloud Registry secret to become available...".yellow while true secrets = `kubectl get secrets` if secrets.include? "gcr-secret" puts "Google Cloud Registry secret is now available.".green break else sleep 1 end end end def minikube if Sct::Helpers.operatingSystem == Sct::Helpers::WINDOWS return "minikube.exe" else return "minikube" end end implements CommandInterface end end