---
# ***
# WARNING: Do not EDIT or MERGE this file, it is generated by packagespec.
# ***

version: 2.1

executors:
  builder:
    docker:
      - image: circleci/buildpack-deps
    environment:
      PRODUCT_REVISION: ""
      AUTO_INSTALL_TOOLS: 'YES'
      BUILDKIT_PROGRESS: plain
    shell: /usr/bin/env bash -euo pipefail -c
  builder-machine:
    machine:
      image: ubuntu-1604:202007-01
    resource_class: xlarge
    environment:
      PRODUCT_REVISION: ""
      AUTO_INSTALL_TOOLS: 'YES'
      BUILDKIT_PROGRESS: plain
    shell: /usr/bin/env bash -euo pipefail -c

workflows:
  version: 2
  build-standalone:
    jobs:
      - build-common-layers
      - darwin_amd64_package: { requires: [ build-common-layers ] }
      - linux_amd64_package: { requires: [ build-common-layers ] }
      - windows_amd64_package: { requires: [ build-common-layers ] }
      - bundle-releases:
          requires:
            - darwin_amd64_package
            - linux_amd64_package
            - windows_amd64_package
jobs:
  build-common-layers:
    executor: builder-machine
    steps:
      
      - add_ssh_keys:
          fingerprints:
            # hashicorp-ci github user.
            - "c6:96:98:82:dc:04:6c:39:dd:ac:83:05:e3:15:1c:98"

      - checkout
      - write-build-layer-cache-keys
      - restore_cache:
          name: Restore build image cache
          keys:
            - cache006-copy-source_dde4_{{checksum ".buildcache/cache-keys/copy-source-dde49480ad145a6d7ce0f55c0d23e6f50c04d8d3"}}
            - cache006-go-modules_694e_{{checksum ".buildcache/cache-keys/go-modules-694e8500194e3b187b35bd9a40229f3bd215710e"}}
            - cache006-ui_5f82_{{checksum ".buildcache/cache-keys/ui-5f82d1f0cbf979ee051aa1ba0f7105483fa6947d"}}
            - cache006-base_aac1_{{checksum ".buildcache/cache-keys/base-aac1d959b4b3af1495941602b5e51fb6181baded"}}
      - run:
          name: Finish early if loaded exact match from cache.
          command: |
            if [ -f .buildcache/archives/03-copy-source-dde49480ad145a6d7ce0f55c0d23e6f50c04d8d3.tar.gz ]; then
              echo "Exact match found in cache, skipping build."
              circleci-agent step halt
            else
              echo "No exact match found, proceeding with build."
            fi
      - run:
          name: Load whatever builder cache we have (if any) into the Docker daemon
          no_output_timeout: 30m
          command: |
            LAYER_SPEC_ID=03-copy-source-dde49480ad145a6d7ce0f55c0d23e6f50c04d8d3 make -C packages*.lock load-builder-cache
      - run:
          name: Build base layer
          command: |
            
            KEYFILE="$HOME/.ssh/id_rsa_c6969882dc046c39ddac8305e3151c98"
            eval "$(ssh-agent -s)"
            if [ -f "$KEYFILE" ]; then
              ssh-add "$KEYFILE"
            else
              echo "==> INFO: SSH key for github.com not found"
              echo "    Attempts to access private repositories from within"
              echo "    the build will fail, e.g. for private go modules, or"
              echo "    attempts to directly clone private repositories."
            fi

            make -f packages*.lock/layer.mk 00-base-aac1d959b4b3af1495941602b5e51fb6181baded-image
      - run:
          name: Build ui layer
          command: |
            
            KEYFILE="$HOME/.ssh/id_rsa_c6969882dc046c39ddac8305e3151c98"
            eval "$(ssh-agent -s)"
            if [ -f "$KEYFILE" ]; then
              ssh-add "$KEYFILE"
            else
              echo "==> INFO: SSH key for github.com not found"
              echo "    Attempts to access private repositories from within"
              echo "    the build will fail, e.g. for private go modules, or"
              echo "    attempts to directly clone private repositories."
            fi

            make -f packages*.lock/layer.mk 01-ui-5f82d1f0cbf979ee051aa1ba0f7105483fa6947d-image
      - run:
          name: Build go-modules layer
          command: |
            
            KEYFILE="$HOME/.ssh/id_rsa_c6969882dc046c39ddac8305e3151c98"
            eval "$(ssh-agent -s)"
            if [ -f "$KEYFILE" ]; then
              ssh-add "$KEYFILE"
            else
              echo "==> INFO: SSH key for github.com not found"
              echo "    Attempts to access private repositories from within"
              echo "    the build will fail, e.g. for private go modules, or"
              echo "    attempts to directly clone private repositories."
            fi

            make -f packages*.lock/layer.mk 02-go-modules-694e8500194e3b187b35bd9a40229f3bd215710e-image
      - run:
          name: Build copy-source layer
          command: |
            
            KEYFILE="$HOME/.ssh/id_rsa_c6969882dc046c39ddac8305e3151c98"
            eval "$(ssh-agent -s)"
            if [ -f "$KEYFILE" ]; then
              ssh-add "$KEYFILE"
            else
              echo "==> INFO: SSH key for github.com not found"
              echo "    Attempts to access private repositories from within"
              echo "    the build will fail, e.g. for private go modules, or"
              echo "    attempts to directly clone private repositories."
            fi

            make -f packages*.lock/layer.mk 03-copy-source-dde49480ad145a6d7ce0f55c0d23e6f50c04d8d3-image

      - run:
          name: Export builder image
          command: make -f packages*.lock/layer.mk 03-copy-source-dde49480ad145a6d7ce0f55c0d23e6f50c04d8d3-save
      - save_cache:
          name: Save layer copy-source to cache.
          key: cache006-copy-source_dde4_{{checksum ".buildcache/cache-keys/copy-source-dde49480ad145a6d7ce0f55c0d23e6f50c04d8d3"}}
          paths:
            - .buildcache/archives/03-copy-source-dde49480ad145a6d7ce0f55c0d23e6f50c04d8d3.tar.gz
      - run:
          name: Prepare layer go-modules for caching
          command: mv .buildcache/archives/03-copy-source-dde49480ad145a6d7ce0f55c0d23e6f50c04d8d3.tar.gz .buildcache/archives/02-go-modules-694e8500194e3b187b35bd9a40229f3bd215710e.tar.gz
      - save_cache:
          name: Save layer go-modules to cache.
          key: cache006-go-modules_694e_{{checksum ".buildcache/cache-keys/go-modules-694e8500194e3b187b35bd9a40229f3bd215710e"}}
          paths:
            - .buildcache/archives/02-go-modules-694e8500194e3b187b35bd9a40229f3bd215710e.tar.gz
      - run:
          name: Prepare layer ui for caching
          command: mv .buildcache/archives/02-go-modules-694e8500194e3b187b35bd9a40229f3bd215710e.tar.gz .buildcache/archives/01-ui-5f82d1f0cbf979ee051aa1ba0f7105483fa6947d.tar.gz
      - save_cache:
          name: Save layer ui to cache.
          key: cache006-ui_5f82_{{checksum ".buildcache/cache-keys/ui-5f82d1f0cbf979ee051aa1ba0f7105483fa6947d"}}
          paths:
            - .buildcache/archives/01-ui-5f82d1f0cbf979ee051aa1ba0f7105483fa6947d.tar.gz
      - run:
          name: Prepare layer base for caching
          command: mv .buildcache/archives/01-ui-5f82d1f0cbf979ee051aa1ba0f7105483fa6947d.tar.gz .buildcache/archives/00-base-aac1d959b4b3af1495941602b5e51fb6181baded.tar.gz
      - save_cache:
          name: Save layer base to cache.
          key: cache006-base_aac1_{{checksum ".buildcache/cache-keys/base-aac1d959b4b3af1495941602b5e51fb6181baded"}}
          paths:
            - .buildcache/archives/00-base-aac1d959b4b3af1495941602b5e51fb6181baded.tar.gz
  darwin_amd64_package:
    executor: builder
    environment:
      - PACKAGE_SPEC_ID: 3f81e804b31cb70e63a9c834931d9a0a4c2e2bb3
    steps:
      
      - setup_remote_docker:
          version: 19.03.12
      
      - add_ssh_keys:
          fingerprints:
            # hashicorp-ci github user.
            - "c6:96:98:82:dc:04:6c:39:dd:ac:83:05:e3:15:1c:98"


      - checkout

      # Restore the package cache first, we might not need to rebuild.
      - write-package-cache-key
      - restore_cache:
          name: Restore package cache
          key: 'cache006-package-3f81e804b31cb70e63a9c834931d9a0a4c2e2bb3-{{checksum ".buildcache/cache-keys/package-3f81e804b31cb70e63a9c834931d9a0a4c2e2bb3"}}'
      - run:
          name: Check the cache status.
          command: |
            if ! { PKG=$(find .buildcache/packages/store -maxdepth 1 -mindepth 1 -name '*.zip' 2> /dev/null) && [ -n "$PKG" ]; }; then
              echo "No package found, continuing with build."
              exit 0
            fi
            echo "Package already cached, skipping build."
            circleci-agent step halt

      # We need to rebuild, so load the builder cache.
      - write-build-layer-cache-keys
      - restore_cache:
          name: Restore build layer cache
          keys:
          - cache006-copy-source_dde4_{{checksum ".buildcache/cache-keys/copy-source-dde49480ad145a6d7ce0f55c0d23e6f50c04d8d3"}}
          - cache006-go-modules_694e_{{checksum ".buildcache/cache-keys/go-modules-694e8500194e3b187b35bd9a40229f3bd215710e"}}
          - cache006-ui_5f82_{{checksum ".buildcache/cache-keys/ui-5f82d1f0cbf979ee051aa1ba0f7105483fa6947d"}}
          - cache006-base_aac1_{{checksum ".buildcache/cache-keys/base-aac1d959b4b3af1495941602b5e51fb6181baded"}}
      - run:
          name: Load whatever builder cache we have (if any) into the Docker daemon
          no_output_timeout: 30m
          command: |
            make -C packages*.lock load-builder-cache

      - run:
          name: Compile Package
          command: |
            
            KEYFILE="$HOME/.ssh/id_rsa_c6969882dc046c39ddac8305e3151c98"
            eval "$(ssh-agent -s)"
            if [ -f "$KEYFILE" ]; then
              ssh-add "$KEYFILE"
            else
              echo "==> INFO: SSH key for github.com not found"
              echo "    Attempts to access private repositories from within"
              echo "    the build will fail, e.g. for private go modules, or"
              echo "    attempts to directly clone private repositories."
            fi

            make -C packages*.lock package

      - run:
          name: List packages
          command: ls -lahR .buildcache/packages
      - save_cache:
          name: Saving package cache
          key: 'cache006-package-3f81e804b31cb70e63a9c834931d9a0a4c2e2bb3-{{checksum ".buildcache/cache-keys/package-3f81e804b31cb70e63a9c834931d9a0a4c2e2bb3"}}'
          paths:
            - .buildcache/packages/store
      # Save builder image cache if necessary.
      # The range should only iterate over a single layer.

  linux_amd64_package:
    executor: builder
    environment:
      - PACKAGE_SPEC_ID: 26ad460ac09efc9c56bdbb7a962ff92616c06c51
    steps:
      
      - setup_remote_docker:
          version: 19.03.12
      
      - add_ssh_keys:
          fingerprints:
            # hashicorp-ci github user.
            - "c6:96:98:82:dc:04:6c:39:dd:ac:83:05:e3:15:1c:98"


      - checkout

      # Restore the package cache first, we might not need to rebuild.
      - write-package-cache-key
      - restore_cache:
          name: Restore package cache
          key: 'cache006-package-26ad460ac09efc9c56bdbb7a962ff92616c06c51-{{checksum ".buildcache/cache-keys/package-26ad460ac09efc9c56bdbb7a962ff92616c06c51"}}'
      - run:
          name: Check the cache status.
          command: |
            if ! { PKG=$(find .buildcache/packages/store -maxdepth 1 -mindepth 1 -name '*.zip' 2> /dev/null) && [ -n "$PKG" ]; }; then
              echo "No package found, continuing with build."
              exit 0
            fi
            echo "Package already cached, skipping build."
            circleci-agent step halt

      # We need to rebuild, so load the builder cache.
      - write-build-layer-cache-keys
      - restore_cache:
          name: Restore build layer cache
          keys:
          - cache006-copy-source_dde4_{{checksum ".buildcache/cache-keys/copy-source-dde49480ad145a6d7ce0f55c0d23e6f50c04d8d3"}}
          - cache006-go-modules_694e_{{checksum ".buildcache/cache-keys/go-modules-694e8500194e3b187b35bd9a40229f3bd215710e"}}
          - cache006-ui_5f82_{{checksum ".buildcache/cache-keys/ui-5f82d1f0cbf979ee051aa1ba0f7105483fa6947d"}}
          - cache006-base_aac1_{{checksum ".buildcache/cache-keys/base-aac1d959b4b3af1495941602b5e51fb6181baded"}}
      - run:
          name: Load whatever builder cache we have (if any) into the Docker daemon
          no_output_timeout: 30m
          command: |
            make -C packages*.lock load-builder-cache

      - run:
          name: Compile Package
          command: |
            
            KEYFILE="$HOME/.ssh/id_rsa_c6969882dc046c39ddac8305e3151c98"
            eval "$(ssh-agent -s)"
            if [ -f "$KEYFILE" ]; then
              ssh-add "$KEYFILE"
            else
              echo "==> INFO: SSH key for github.com not found"
              echo "    Attempts to access private repositories from within"
              echo "    the build will fail, e.g. for private go modules, or"
              echo "    attempts to directly clone private repositories."
            fi

            make -C packages*.lock package

      - run:
          name: List packages
          command: ls -lahR .buildcache/packages
      - save_cache:
          name: Saving package cache
          key: 'cache006-package-26ad460ac09efc9c56bdbb7a962ff92616c06c51-{{checksum ".buildcache/cache-keys/package-26ad460ac09efc9c56bdbb7a962ff92616c06c51"}}'
          paths:
            - .buildcache/packages/store
      # Save builder image cache if necessary.
      # The range should only iterate over a single layer.

  windows_amd64_package:
    executor: builder
    environment:
      - PACKAGE_SPEC_ID: 6b5bf7794d67cf76b1b4772807af52deb1f6a808
    steps:
      
      - setup_remote_docker:
          version: 19.03.12
      
      - add_ssh_keys:
          fingerprints:
            # hashicorp-ci github user.
            - "c6:96:98:82:dc:04:6c:39:dd:ac:83:05:e3:15:1c:98"


      - checkout

      # Restore the package cache first, we might not need to rebuild.
      - write-package-cache-key
      - restore_cache:
          name: Restore package cache
          key: 'cache006-package-6b5bf7794d67cf76b1b4772807af52deb1f6a808-{{checksum ".buildcache/cache-keys/package-6b5bf7794d67cf76b1b4772807af52deb1f6a808"}}'
      - run:
          name: Check the cache status.
          command: |
            if ! { PKG=$(find .buildcache/packages/store -maxdepth 1 -mindepth 1 -name '*.zip' 2> /dev/null) && [ -n "$PKG" ]; }; then
              echo "No package found, continuing with build."
              exit 0
            fi
            echo "Package already cached, skipping build."
            circleci-agent step halt

      # We need to rebuild, so load the builder cache.
      - write-build-layer-cache-keys
      - restore_cache:
          name: Restore build layer cache
          keys:
          - cache006-copy-source_dde4_{{checksum ".buildcache/cache-keys/copy-source-dde49480ad145a6d7ce0f55c0d23e6f50c04d8d3"}}
          - cache006-go-modules_694e_{{checksum ".buildcache/cache-keys/go-modules-694e8500194e3b187b35bd9a40229f3bd215710e"}}
          - cache006-ui_5f82_{{checksum ".buildcache/cache-keys/ui-5f82d1f0cbf979ee051aa1ba0f7105483fa6947d"}}
          - cache006-base_aac1_{{checksum ".buildcache/cache-keys/base-aac1d959b4b3af1495941602b5e51fb6181baded"}}
      - run:
          name: Load whatever builder cache we have (if any) into the Docker daemon
          no_output_timeout: 30m
          command: |
            make -C packages*.lock load-builder-cache

      - run:
          name: Compile Package
          command: |
            
            KEYFILE="$HOME/.ssh/id_rsa_c6969882dc046c39ddac8305e3151c98"
            eval "$(ssh-agent -s)"
            if [ -f "$KEYFILE" ]; then
              ssh-add "$KEYFILE"
            else
              echo "==> INFO: SSH key for github.com not found"
              echo "    Attempts to access private repositories from within"
              echo "    the build will fail, e.g. for private go modules, or"
              echo "    attempts to directly clone private repositories."
            fi

            make -C packages*.lock package

      - run:
          name: List packages
          command: ls -lahR .buildcache/packages
      - save_cache:
          name: Saving package cache
          key: 'cache006-package-6b5bf7794d67cf76b1b4772807af52deb1f6a808-{{checksum ".buildcache/cache-keys/package-6b5bf7794d67cf76b1b4772807af52deb1f6a808"}}'
          paths:
            - .buildcache/packages/store
      # Save builder image cache if necessary.
      # The range should only iterate over a single layer.


  bundle-releases:
    executor: builder
    steps:
      - checkout
      - write-all-package-cache-keys
      - load-darwin_amd64_package
      - run:
          environment:
            PACKAGE_SPEC_ID: 3f81e804b31cb70e63a9c834931d9a0a4c2e2bb3
          name: Write package metadata for darwin_amd64_package
          command: |
            make package-meta
      - load-linux_amd64_package
      - run:
          environment:
            PACKAGE_SPEC_ID: 26ad460ac09efc9c56bdbb7a962ff92616c06c51
          name: Write package metadata for linux_amd64_package
          command: |
            make package-meta
      - load-windows_amd64_package
      - run:
          environment:
            PACKAGE_SPEC_ID: 6b5bf7794d67cf76b1b4772807af52deb1f6a808
          name: Write package metadata for windows_amd64_package
          command: |
            make package-meta
      - run:
          name: Write package aliases
          command:
            make aliases
      - run:
          name: List Build Cache
          command: ls -lahR .buildcache

      # Give the lockfile a name store_artifacts can work with.
      - run: cp packages*.lock/pkgs.yml lockfile-standalone.yml

      # Surface the lock file as an artifact.
      - store_artifacts:
          path: lockfile-standalone.yml
          destination: lockfile-standalone.yml

      # Surface the package store directory as an artifact.
      # This makes each zipped package separately downloadable.
      - store_artifacts:
          path: .buildcache/packages
          destination: packages-standalone

      # Surface a tarball of the whole package store plus lockfile as an artifact.
      - run: tar -czf packages-standalone.tar.gz .buildcache/packages lockfile-standalone.yml
      - store_artifacts:
          path: packages-standalone.tar.gz
          destination: packages-standalone.tar.gz

      # Surface a tarball of just the metadata files.
      - run: tar -czf meta-standalone.tar.gz .buildcache/packages/store/*.json lockfile-standalone.yml
      - store_artifacts:
          path: meta-standalone.tar.gz
          destination: meta-standalone.tar.gz

commands:
  load-darwin_amd64_package:
    steps:
      - restore_cache:
          key: 'cache006-package-3f81e804b31cb70e63a9c834931d9a0a4c2e2bb3-{{checksum ".buildcache/cache-keys/package-3f81e804b31cb70e63a9c834931d9a0a4c2e2bb3"}}'
  
  load-linux_amd64_package:
    steps:
      - restore_cache:
          key: 'cache006-package-26ad460ac09efc9c56bdbb7a962ff92616c06c51-{{checksum ".buildcache/cache-keys/package-26ad460ac09efc9c56bdbb7a962ff92616c06c51"}}'
  
  load-windows_amd64_package:
    steps:
      - restore_cache:
          key: 'cache006-package-6b5bf7794d67cf76b1b4772807af52deb1f6a808-{{checksum ".buildcache/cache-keys/package-6b5bf7794d67cf76b1b4772807af52deb1f6a808"}}'
  

  write-build-layer-cache-keys:
    steps:
      - run:
          name: Write builder layer cache keys
          command: make -C packages*.lock write-builder-cache-keys

  write-package-cache-key:
    steps:
      - run:
          name: Write package cache key
          command: make -C packages*.lock write-package-cache-key

  write-all-package-cache-keys:
    steps:
      - run:
          name: Write all package cache keys
          command: make -C packages*.lock write-all-package-cache-keys
