SHARE
TWEET

Untitled

a guest May 30th, 2017 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env bash
  2.  
  3. set -euf -o pipefail
  4.  
  5. group="habitat-k8s"
  6. location='westus'
  7. vm_name="habitat-dev"
  8. cluster_name="k8s-cluster"
  9. name_prefix="habk8s"
  10. new_name=$(echo $(mktemp -u ${name_prefix}XXXX) | tr '[:upper:]' '[:lower:]')
  11. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  12.  
  13. if [[ -z $(az account list -o tsv 2>/dev/null ) ]]; then
  14.     az login -o table
  15. fi
  16. echo ""
  17.  
  18. if [[ -z $(az group show -n ${group}) ]]; then
  19.     echo "Creating Resource Group named ${group}"
  20.     az group create -n ${group} -l ${location} 1>/dev/null
  21. else
  22.     echo "Using Resource Group name ${group}"
  23. fi
  24.  
  25. if [[ ! -f ${DIR}/.ssh/id_rsa ]]; then
  26.     mkdir -p ${DIR}/.ssh
  27.     echo "Generating ssh keys to use for setting up the Kubernetes cluster"
  28.     ssh-keygen -f ${DIR}/.ssh/id_rsa -t rsa -N '' 1>/dev/null
  29. else
  30.     echo "Using ${DIR}/.ssh/id_rsa to authenticate with the Kubernetes cluster"
  31. fi
  32.  
  33. cosmosdb_name=$(az cosmosdb list -g ${group} --query "[?starts_with(name, '${name_prefix}')] | [0].name" -o tsv)
  34. if [[ -z "${cosmosdb_name}" ]]; then
  35.     echo "Create CosmosDB instance named ${new_name} with MongoDB wire format to be used by Rails Mongoid"
  36.     az cosmosdb create -g ${group} -n "${new_name}" --kind MongoDB 1>/dev/null
  37. else
  38.     echo "Using the already provisioned CosmosDB named ${cosmosdb_name}"
  39. fi
  40.  
  41. if [[ -z $(az acs show -g ${group} -n ${cluster_name} -o tsv) ]]; then
  42.     echo "Creating Azure Kubernetes cluster named ${cluster_name} in group ${group}"
  43.     az acs create -g ${group} -n ${cluster_name} --orchestrator-type Kubernetes \
  44.         --agent-vm-size Standard_DS2_v2 --agent-count 2 \
  45.         --ssh-key-value ${DIR}/.ssh/id_rsa.pub 1>/dev/null
  46. else
  47.     echo "Using Azure Kubernetes cluster named ${cluster_name} in group ${group}"
  48. fi
  49.  
  50. registry=$(az acr list -g ${group} --query "[?starts_with(name, '${name_prefix}')] | [0].loginServer" -o tsv)
  51. if [[ -z ${registry} ]]; then
  52.     echo "Creating Azure Container Registry named ${new_name} in group ${group}"
  53.     registry=$(az acr create -g ${group} -n ${new_name} -l ${location} \
  54.                 --admin-enabled true --sku Basic --query "loginServer" -o tsv)
  55. else
  56.     echo "Using Azure Container Registry ${registry} in group ${group}"
  57. fi
  58. echo ""
  59.  
  60. registry_name=$(az acr list -g ${group} --query "[?starts_with(name, '${name_prefix}')] | [0].name" -o tsv)
  61. creds=$(az acr credential show -g ${group} -n ${registry_name})
  62. username=$(echo $creds | jq ".username" -r)
  63. password=$(echo $creds | jq ".passwords[0].value" -r)
  64. echo "Logging Docker into ${registry} with user: ${username}"
  65. docker login ${registry} -u ${username} -p ${password}
  66. echo "To push to your docker registry run 'docker push ${registry}/myImage:version'"
  67. echo ""
  68.  
  69. if [[ ! -d ${HOME}.kube/config ]]; then
  70.     echo "Creating ${HOME}.kube/config w/ credentials for managing ${cluster_name}"
  71.     az acs kubernetes get-credentials -g ${group} -n ${cluster_name} \
  72.         --ssh-key-file ${DIR}/.ssh/id_rsa 1>/dev/null
  73. else
  74.     echo "Using ${HOME}.kube/config w/ credentials for managing ${cluster_name}"
  75. fi
  76.  
  77. if [[ -z $(which kubectl) ]]; then
  78.     echo "We didn't find kubectl installed, so installing..."
  79.     az acs kubernetes install-cli
  80. fi
  81.  
  82. echo "Your Kubernetes cluster has been deployed and you are ready to connect."
  83. echo "To connect to the cluster run 'kubectl cluster-info'"
  84. echo ""
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top