golang.org/x/build@v0.0.0-20240506185731-218518f32b70/env/windows/build.bash (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2017 The Go Authors. All rights reserved.
     4  # Use of this source code is governed by a BSD-style
     5  # license that can be found in the LICENSE file.
     6  
     7  #
     8  # Creates a new windows VM running the buildlet
     9  #
    10  
    11  # SPINNING UP A TEST INSTANCE
    12  # Create a windows instance and bakes the buildlet into the startup.
    13  #
    14  # PROJECT_ID=[your GCP project] BASE_IMAGE=windows-server-2012-r2-dc-v20171010 IMAGE_PROJECT=windows-cloud ./build.bash
    15  # PROJECT_ID=[your GCP project] ./rdp.bash
    16  
    17  # CREATED AN IMAGE FOR BUILDER DASHBOARD
    18  # Creates an image and validates it for use in the dashboard.
    19  #
    20  # Steps:
    21  #  - Create new VM
    22  #  - Run startup.ps1, restart
    23  #  - Wait till buildlet process is up
    24  #  - Stop VM
    25  #  - Capture image
    26  #  - Create a new VM from the image
    27  #  - Wait till buildlet process is up
    28  #  - Run ./test_buildlet.bash
    29  #
    30  # PROJECT_ID=[your GCP project] BASE_IMAGE=windows-server-2012-r2-dc-v20171010 IMAGE_PROJECT=windows-cloud CAPTURE_IMAGE=true ./build.bash
    31  
    32  set -eu
    33  
    34  ZONE="us-central1-f"
    35  BUILDER_PREFIX="${1-golang}"
    36  IMAGE_NAME="${1-${BASE_IMAGE}}"
    37  INSTANCE_NAME="${BUILDER_PREFIX}-buildlet"
    38  TEST_INSTANCE_NAME="${BUILDER_PREFIX}-buildlet-test"
    39  MACHINE_TYPE="e2-standard-4"
    40  BUILDLET_IMAGE="windows-amd64-${IMAGE_NAME}"
    41  IMAGE_PROJECT=$IMAGE_PROJECT
    42  BASE_IMAGE=$BASE_IMAGE
    43  CAPTURE_IMAGE="${CAPTURE_IMAGE-false}"
    44  
    45  function wait_for_buildlet() {
    46    ip=$1
    47    seconds=5
    48  
    49    echo "Waiting for buildlet at ${ip} to become responsive"
    50    until curl "http://${ip}" 2>/dev/null; do
    51      echo "retrying ${ip} in ${seconds} seconds"
    52      sleep "${seconds}"
    53    done
    54  }
    55  
    56  #
    57  # 0. Cleanup images/instances from prior runs
    58  #
    59  echo "Destroying existing instances (if exists)"
    60  yes "Y" | gcloud compute instances delete "$INSTANCE_NAME" --project="$PROJECT_ID" --zone="$ZONE" || true
    61  yes "Y" | gcloud compute instances delete "$TEST_INSTANCE_NAME" --project="$PROJECT_ID" --zone="$ZONE" || true
    62  echo "Destroying existing image (if exists)"
    63  yes "Y" | gcloud compute images delete "$BUILDLET_IMAGE" --project="$PROJECT_ID" || true
    64  
    65  
    66  #
    67  # 1. Create base instance
    68  #
    69  echo "Creating target instance"
    70  gcloud compute instances create --machine-type="$MACHINE_TYPE" "$INSTANCE_NAME" \
    71          --image "$BASE_IMAGE" --image-project "$IMAGE_PROJECT" \
    72          --project="$PROJECT_ID" --zone="$ZONE" \
    73          --network=default-vpc \
    74          --metadata="buildlet-binary-url=https://storage.googleapis.com/go-builder-data/buildlet.windows-amd64" \
    75          --metadata-from-file=windows-startup-script-ps1=startup.ps1
    76  
    77  echo ""
    78  echo "Follow logs with:"
    79  echo ""
    80  echo gcloud compute instances tail-serial-port-output "$INSTANCE_NAME" --zone="$ZONE" --project="$PROJECT_ID"
    81  echo ""
    82  ip=$(gcloud compute instances describe "$INSTANCE_NAME" --project="$PROJECT_ID" --zone="$ZONE" --format="value(networkInterfaces[0].networkIP)")
    83  
    84  wait_for_buildlet "$ip"
    85  
    86  if [ ! "$CAPTURE_IMAGE" = "true" ]; then
    87    exit 0
    88  fi
    89  
    90  #
    91  # 2. Image base instance
    92  #
    93  
    94  echo "Shutting down instance"
    95  gcloud compute instances stop "$INSTANCE_NAME" \
    96          --project="$PROJECT_ID" --zone="$ZONE"
    97  
    98  echo "Capturing image"
    99  gcloud compute images create "$BUILDLET_IMAGE" --source-disk "$INSTANCE_NAME" --source-disk-zone "$ZONE" --project="$PROJECT_ID"
   100  
   101  echo "Removing base machine"
   102  yes "Y" | gcloud compute instances delete "$INSTANCE_NAME" --project="$PROJECT_ID" --zone="$ZONE" || true
   103  
   104  #
   105  # 3. Verify image is valid
   106  #
   107  
   108  echo "Creating new machine with image"
   109  gcloud compute instances create --machine-type="$MACHINE_TYPE" --image "$BUILDLET_IMAGE" "$TEST_INSTANCE_NAME" \
   110         --network=default-vpc \
   111         --project="$PROJECT_ID" --metadata="buildlet-binary-url=https://storage.googleapis.com/go-builder-data/buildlet.windows-amd64" \
   112         --zone="$ZONE"
   113  
   114  test_image_ip=$(gcloud compute instances describe "$TEST_INSTANCE_NAME" --project="$PROJECT_ID" --zone="$ZONE" --format="value(networkInterfaces[0].networkIP)")
   115  wait_for_buildlet "$test_image_ip"
   116  
   117  echo "Performing test build"
   118  ./test_buildlet.bash "$test_image_ip"
   119  
   120  echo "Removing test instance"
   121  yes "Y" | gcloud compute instances delete "$TEST_INSTANCE_NAME" --project="$PROJECT_ID" --zone="$ZONE" || true
   122  
   123  echo "Success! A new buildlet can be created with the following command"
   124  echo "gcloud compute instances create --machine-type='$MACHINE_TYPE' '$INSTANCE_NAME' \
   125  --metadata='buildlet-binary-url=https://storage.googleapis.com/go-builder-data/buildlet.windows-amd64' \
   126  --image '$BUILDLET_IMAGE' --image-project '$PROJECT_ID'"