# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing permissions and limitations under the
# License.

steps:
- id: 'clone_community_builders'
  name: 'gcr.io/cloud-builders/git'
  args: ['clone', 'https://github.com/GoogleCloudPlatform/cloud-builders-community']
- id: 'add_packer_builder'
  waitFor: ['clone_community_builders']
  name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: "bash"
  args:
    - '-c'
    - |
       PACKER_BUILDER_VERSION_STRING=$(gcloud container images list-tags gcr.io/$PROJECT_ID/packer --format='value(tags)' | grep latest )
       PACKER_BUILDER_VERSION=$(echo "$$PACKER_BUILDER_VERSION_STRING" | awk -F',' '{print $1}')
       NESTED_VIRT_IMAGE=$(gcloud compute images list --no-standard-images --filter 'family:nested-virt' --format 'value(name)')
       NESTED_PACKER_IMAGE=$(gcloud compute images list --no-standard-images --filter 'family:nested-packer' --format 'value(name)')

       if [ -z $$PACKER_BUILDER_VERSION ]; then
         cd cloud-builders-community/packer
         gcloud builds submit .
       else
          echo "Using packer version : $$PACKER_BUILDER_VERSION"
       fi

       if [ -z $$NESTED_VIRT_IMAGE ]; then
         echo "No preexisting images with nested-virtualization enabled exist"
       else
          echo "Found GCE Image(s) with Nested Virtualzation Enabled : $$NESTED_VIRT_IMAGE"
          echo $$NESTED_VIRT_IMAGE > /workspace/nested-images-versions.txt
       fi

       if [ -z $$NESTED_PACKER_IMAGE ]; then
         echo "No preexisting images with nested-virtualization enabled and packer installed exist"
       else
          echo "Found GCE Image(s) with Nested Virtualzation Enabled and packer installed : $$NESTED_PACKER_IMAGE"
          echo $$NESTED_PACKER_IMAGE > /workspace/nested-packer-images-versions.txt
       fi
- id: 'create_nested_virtualization_image'
  name: 'gcr.io/$PROJECT_ID/packer'
  entrypoint: bash
  waitFor: ['add_packer_builder']
  args:
    - '-c'
    - |
       function create_nested_gce_image(){
         cat <<NESTED_VIRT_SPEC>nested-virt-spec.json
       {
         "builders": [
        {
          "image_name": "nested-virtualization-centos-7",
          "type": "googlecompute",
          "project_id": "$PROJECT_ID",
          "source_image_family": "centos-7",
          "image_family": "nested-virt",
          "ssh_username": "packer",
          "zone": "${_IMAGE_ZONE}",
          "image_licenses": ["projects/vm-options/global/licenses/enable-vmx"]
        }
        ]
       }
       NESTED_VIRT_SPEC
        packer validate nested-virt-spec.json
        packer build -force nested-virt-spec.json
       }
       if [ ${_BUILD_NESTED_VIRT_IMAGE} == 'true' ]; then
        create_nested_gce_image
       else
        if [ -f /workspace/nested-images-versions.txt ]; then
          NESTED_VIRT_IMAGE_VERSION=$(cat /workspace/nested-images-versions.txt)
          echo "Skipping the nested-virt Image Build b/c existing version(s) found: $$NESTED_VIRT_IMAGE_VERSION"
        else
          create_nested_gce_image
        fi
       fi
       
- id: create_packer_agent
  waitFor: 'create_nested_virtualization_image'
  name: 'gcr.io/$PROJECT_ID/packer'
  entrypoint: "bash"
  args:
    - '-c'
    - |
       function create_packer_agent_image(){
        echo '#!/bin/bash' > packer-install-script.sh
        cat <<PACKER_INSTALL>>packer-install-script.sh
       yum update -y && yum install git unzip wget qemu-kvm -y
       echo "export PATH=$_PATH:/usr/libexec" > /etc/profile.d/libexec-path.sh
       source /etc/profile.d/libexec-path.sh
       curl -LO \
       https://releases.hashicorp.com/packer/1.3.0/packer_1.3.0_linux_amd64.zip
       unzip packer_1.3.0_linux_amd64.zip
       cp packer /usr/bin/packerio
       PACKER_INSTALL
         cat <<AGENT_SPEC>agent-spec.json
       {
          "builders": [
           {
             "type": "googlecompute",
             "image_name": "nested-packer-image-${_AGENT_VERSION}",
             "image_family": "nested-packer",
             "project_id": "$PROJECT_ID",
             "source_image": "nested-virtualization-centos-7",
             "ssh_username": "packer",
             "zone": "${_IMAGE_ZONE}",
             "min_cpu_platform": "Intel Haswell",
             "startup_script_file": "packer-install-script.sh"
           }
         ]
       }
       AGENT_SPEC
        packer validate agent-spec.json
        packer build -force agent-spec.json
       }
       if [ ${_BUILD_PACKER_AGENT_IMAGE} == 'true' ]; then
        create_packer_agent_image
       else
         if [ -f /workspace/nested-packer-images-versions.txt ]; then
          NESTED_PACKER_IMAGE_VERSION=$(cat /workspace/nested-packer-images-versions.txt)
          echo "Skipping the nested-virt packer Image Build b/c existing version(s) found: $$NESTED_PACKER_IMAGE_VERSION"
        else
          create_packer_agent_image
        fi
       fi
- id: 'build_base-image-builder_builder'
  name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/base-image-builder:latest', '.' ]
- id: 'push_base-image-builder_builder'
  name: 'gcr.io/cloud-builders/docker'
  waitFor: ['build_base-image-builder_builder']
  args: ['push', 'gcr.io/$PROJECT_ID/base-image-builder:latest']
images: ['gcr.io/$PROJECT_ID/base-image-builder:latest']
timeout: 1200s
substitutions:
  _PATH: '\$PATH'
  _IMAGE_ZONE: 'us-central1-f'
  _AGENT_VERSION: 'v1'
  _BUILD_NESTED_VIRT_IMAGE: 'true'
  _BUILD_PACKER_AGENT_IMAGE: 'true'
tags: ['cloud-builders-community']
