# Playbook to set up Kubernetes access

- hosts: localhost
  connection: local
  vars:
    helm_version: v2.12.1
    kubectl_url: https://amazon-eks.s3-us-west-2.amazonaws.com/1.14.6/2019-08-22/bin/{{ ansible_system | lower }}/amd64/kubectl
    helm_checksum:
      Darwin: sha256:51b503202af374fb1e7e37f6d3baf179cbee8ec4435a5c7df19ef633dd64c96b
      Linux: sha256:2dc50b3d409b931849b4c915f4e027638d552af11bda42d3a622655971651506
    helm3_checksum:
      Darwin: sha256:60fc01cd9036ee90f23d54b6d8054f4cd5bbf1427f9fd6fdaebf4d789800cbcc
      Linux: sha256:32fc626233a0f6e599fec6b37625f0e730e91d463d5d2833c0a3426492f68a3b
    argo_checksum:
      Darwin: sha256:f74402daa95168bbb9f6c62117a28b8260da5e2357d71681d9f5b458fb497542
      Linux: sha256:b23603fd70d8181b88ae950c6435ad4fd64442dc446efc0a5485174d0e371b73
    kubectl_checksum:
      Darwin: 8dd54f7283955b1bd85540b75f374575ea25c57481cdccf1e4547548133acf31
      Linux: 387caa16fb715db9304bbbef444a94db5b9b1e67a75c478aefe56e9c253fc5c9
    # aws --region us-west-2  eks describe-cluster --name prod1 --output json
    eks_cluster:
      name: prod1
      endpoint: https://BF8D34B5B9291B9F91411CEF9770FF90.sk1.us-west-2.eks.amazonaws.com
      certificateAuthority:
        data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRFNE1EY3hOekl6TVRFME1Gb1hEVEk0TURjeE5ESXpNVEUwTUZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTHZOCkVUOE9ZRkprM2hnWm1JbW9vdUdDK29pQVlSVDR5TFNxNlF1SnpjYmg2bHZ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_env.HOME }}/.local/bin"
        state: directory

    - name: Download tools
      get_url:
        url: "{{ item.url }}"
        dest: "{{ ansible_env.HOME }}/.local/bin/{{ item.filename }}"
        mode: 0755
        checksum: "sha256:{{ item.checksum }}"
      with_items:
        - url: "{{ kubectl_url }}"
          filename: kubectl
          checksum: "{{ kubectl_checksum[ansible_system] }}"

    - name: Install gettoken.py
      copy:
        src: ../k8s/gettoken/gettoken.py
        dest: "{{ ansible_env.HOME }}/.local/bin/gettoken.py"
        mode: 0755

    - block:
    # install homebrew python for darwin is because the system python locks a
    # version of six that is incompatible with some of the deps needed for gettoken.py authenticator
      - name: install homebrew deps
        homebrew:
          name:
            - 'python@2'
            - gnu-tar # for unarchive module in ansible on darwin

      - name: install python deps
        pip:
          executable: /usr/local/bin/pip2
          name:
          - httplib2
          - requests
          - oauth2client

      - name: patch gettoken.py for mac
        lineinfile:
          dest: "{{ ansible_env.HOME }}/.local/bin/gettoken.py"
          regexp: '#!/usr/bin/python2.7'
          line: '#!/usr/local/bin/python2.7'
      when: ansible_distribution == 'MacOSX'


    - name: Install kubens and kubectx
      copy:
        src: kubectx/{{ item }}
        dest: "{{ ansible_env.HOME }}/.local/bin/{{ item }}"
        mode: 0755
      with_items:
        - kubens
        - kubectx

    - block:
      - name: get helm version
        shell: helm version -c
        register: helm_version_output
        changed_when: false
        failed_when: false

      - name: Install helm
        get_url:
          url: "https://artifactory.zooxlabs.com/artifactory/infra-bin/helm-{{ helm_version }}-{{ ansible_system | lower }}-amd64"
          dest: "{{ ansible_env.HOME }}/.local/bin/helm"
          mode: 0777
          checksum: "{{ helm_checksum[ansible_system] }}"

      - name: Run helm init
        shell: '{{ ansible_env.HOME }}/.local/bin/helm init --client-only'

      - name: Add helm repos
        shell: '{{ ansible_env.HOME }}/.local/bin/helm repo add zoox-stable https://artifactory.zooxlabs.com/artifactory/helm-stable'

      - name: Create .helm directory if it doesn't exist
        file:
          path: "{{ ansible_env.HOME }}/.helm"
          state: directory
          mode: '0755'

      - name: Install helm starters
        copy:
          src: ./files/helm-starters/
          dest: "{{ ansible_env.HOME }}/.helm/starters"

    - block:
      - name: Install helm3
        get_url:
          url: "https://artifactory.zooxlabs.com/artifactory/infra-bin/helm3-{{ ansible_system | lower }}-amd64"
          dest: "{{ ansible_env.HOME }}/.local/bin/helm3"
          mode: 0777
          checksum: "{{ helm3_checksum[ansible_system] }}"

      - name: Create .helm3 directory if it doesn't exist
        file:
          path: "{{ ansible_env.HOME }}/.helm3"
          state: directory
          mode: '0755'

    - name: Install Argo cli
      get_url:
        url: https://github.com/argoproj/argo/releases/download/v2.3.0/argo-{{ ansible_system | lower }}-amd64
        dest: "{{ ansible_env.HOME }}/.local/bin/argo"
        mode: 0777
        checksum: "{{ argo_checksum[ansible_system] }}"

    - name: Ensure $HOME/.kube exists
      file:
        path: "{{ ansible_env.HOME }}/.kube"
        state: directory

    - name: Write kubeconfig
      template:
        dest: "{{ ansible_env.HOME }}/.kube/config"
        src: kubeconfig.j2
        backup: yes
      register: write_kubeconfig

    - debug:
        msg: "Old kubeconfig saved as {{ write_kubeconfig.backup_file }}"
      when:
        - write_kubeconfig.changed
        - write_kubeconfig.backup_file is defined
