...

Text file src/edge-infra.dev/hack/build/edge-golden-image/README.md

Documentation: edge-infra.dev/hack/build/edge-golden-image

     1# Edge Golden Image Guide
     2
     3This is a brief guide on how to run the following to generate a custom golden image
     4
     5## Authors
     6- Quan Huynh ([@qh185008](https://github.com/qh185008))
     7
     8## Prerequisites
     9
    10You will need to have these following installed:
    11- Packer: https://developer.hashicorp.com/packer/tutorials/docker-get-started/get-started-install-cli
    12
    13If running locally, your user account needs to be able to impersonate `golden-image-generation@ret-edge-pltf-infra.iam.gserviceaccount.com`, meaning you need the IAM Service Account Token Creator role
    14
    15## Overview
    16
    17- Build config is at `hack/build/edge-golden-image`
    18- `packer.pkr.hcl` is the main file to run any build
    19- `setup_script.sh` is to install packages and dependencies during packer provisioning
    20- `startup_script.sh` is executed when the VM starts. Used to connect a runner to GitHub
    21- `shutdown_script.sh` is executed when the VM stops. Disconnected the runner from GitHub
    22
    23## Quickstart
    24
    25To build with all defaults:
    26
    27```
    28packer build hack/build/edge-golden-image/packer.pkr.hcl
    29```
    30
    31- To add value for certain variables, you can replace within `source` block or customize the `variable` block above it. To add value, create `default` in the block you want and type in the value
    32```
    33variable "project_id" {
    34  type      = string
    35}
    36
    37variable "account_file" {
    38  type      = string
    39}
    40
    41variable "zone" {
    42  type      = string
    43  default   = "us-east1-b"
    44}
    45
    46variable "image_description" {
    47  type      = string
    48  default   = "Edge Golden Image, powered by GSRE golden image"
    49}
    50
    51variable "source_image" {
    52  type      = string
    53  default   = "gsre-base-ubuntu-2204-20230510"
    54  sensitive = true
    55}
    56
    57```
    58- Be mindful with these two lines inside `source` block:
    59
    60```
    61source_image        = "${var.source_image}"
    62
    63source_image_project_id      = ["gsre-compute-images-testing"]
    64```
    65
    66- GSRE team may update their image name, so checkup just in case. To check on image list, first you need to be on `golden-image-service-account`
    67```
    68# Run this to get list of account u can use
    69gcloud auth list
    70
    71
    72# Run this to set to the golden account
    73gcloud config set account `GOLDEN_ACCOUNT_NAME`
    74
    75# Run this to update the gcloud account.
    76# This command will help you speed up the switching account
    77gcloud container clusters get-credentials platform-infra --zone us-east1-b --project ret-edge-pltf-infra
    78
    79```
    80- If you don't see golden service account key, that means you haven't applied the key file, so go back and do so you silly goose
    81
    82```
    83build {
    84  sources = ["sources.googlecompute.golden-image-gsre-ubuntu-2204"]
    85
    86  provisioner "file" {
    87    source      = "/path/to/service/account/key.json"
    88    destination = "/tmp/serviceAccountKey.json"
    89  }
    90
    91  provisioner "file" {
    92    source      = "./startup_script.sh"
    93    destination = "/tmp/startup_script.sh"
    94  }
    95
    96  provisioner "file" {
    97    source      = "./shutdown_script.sh"
    98    destination = "/tmp/shutdown_script.sh"
    99  }
   100
   101  provisioner "shell" {
   102    script      = "./setup_script.sh"
   103    //expect_disconnect = true
   104}
   105```
   106
   107- This is to upload the service account key from your local folder to packer tmp folder during provisioning. We need this step to retrieve the secret and fetch `github-action-runner` key file
   108```
   109provisioner "file" {
   110    source      = "/path/to/service/account/key.json"
   111    destination = "/tmp/serviceAccountKey.json"
   112}
   113```
   114
   115- This is to upload both `startup_script` and `shutdown_script` to packer tmp folder. During the `setup_script` run, these 2 files will be moved inside `/opt/github/actions-runner` folder, later to be triggered for usage
   116```
   117provisioner "file" {
   118    source      = "./startup_script.sh"
   119    destination = "/tmp/startup_script.sh"
   120}
   121
   122provisioner "file" {
   123    source      = "./shutdown_script.sh"
   124    destination = "/tmp/shutdown_script.sh"
   125}
   126```
   127
   128- This is to start the setup process during provisioner
   129```
   130provisioner "shell" {
   131    script      = "./setup_script.sh"
   132}
   133
   134```
   135- After making changes to all these files, there is a way for you to save time to see if you runners are being registered correctly.
   136- Instead of waiting for the image to be built, create instances with startup and shutdown script, you can create a bash script and throw it inside this block and add it to `build` block. This simulates how it will look like when you run the actual build on the instance
   137```
   138provisioner "shell" {
   139    script      = "./script_you_want_to_run.sh"
   140}
   141```
   142- Example output:
   143```
   144==> googlecompute.golden-image-gsre-ubuntu-2204: Provisioning with shell script: ./start.sh
   145==> googlecompute.golden-image-gsre-ubuntu-2204:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
   146==> googlecompute.golden-image-gsre-ubuntu-2204:                                  Dload  Upload   Total   Spent    Left  Speed
   147==> googlecompute.golden-image-gsre-ubuntu-2204: 100    96  100    96    0     0    329      0 --:--:-- --:--:-- --:--:--   331
   148    googlecompute.golden-image-gsre-ubuntu-2204:
   149    googlecompute.golden-image-gsre-ubuntu-2204: --------------------------------------------------------------------------------
   150    googlecompute.golden-image-gsre-ubuntu-2204: |        ____ _ _   _   _       _          _        _   _                      |
   151    googlecompute.golden-image-gsre-ubuntu-2204: |       / ___(_) |_| | | |_   _| |__      / \   ___| |_(_) ___  _ __  ___      |
   152    googlecompute.golden-image-gsre-ubuntu-2204: |      | |  _| | __| |_| | | | | '_ \    / _ \ / __| __| |/ _ \| '_ \/ __|     |
   153    googlecompute.golden-image-gsre-ubuntu-2204: |      | |_| | | |_|  _  | |_| | |_) |  / ___ \ (__| |_| | (_) | | | \__ \     |
   154    googlecompute.golden-image-gsre-ubuntu-2204: |       \____|_|\__|_| |_|\__,_|_.__/  /_/   \_\___|\__|_|\___/|_| |_|___/     |
   155    googlecompute.golden-image-gsre-ubuntu-2204: |                                                                              |
   156    googlecompute.golden-image-gsre-ubuntu-2204: |                       Self-hosted runner registration                        |
   157    googlecompute.golden-image-gsre-ubuntu-2204: |                                                                              |
   158    googlecompute.golden-image-gsre-ubuntu-2204: --------------------------------------------------------------------------------
   159    googlecompute.golden-image-gsre-ubuntu-2204:
   160    googlecompute.golden-image-gsre-ubuntu-2204: # Authentication
   161    googlecompute.golden-image-gsre-ubuntu-2204:
   162    googlecompute.golden-image-gsre-ubuntu-2204:
   163    googlecompute.golden-image-gsre-ubuntu-2204: √ Connected to GitHub
   164    googlecompute.golden-image-gsre-ubuntu-2204:
   165    googlecompute.golden-image-gsre-ubuntu-2204: # Runner Registration
   166    googlecompute.golden-image-gsre-ubuntu-2204:
   167    googlecompute.golden-image-gsre-ubuntu-2204:
   168    googlecompute.golden-image-gsre-ubuntu-2204:
   169    googlecompute.golden-image-gsre-ubuntu-2204:
   170    googlecompute.golden-image-gsre-ubuntu-2204: √ Runner successfully added
   171    googlecompute.golden-image-gsre-ubuntu-2204: √ Runner connection is good
   172    googlecompute.golden-image-gsre-ubuntu-2204:
   173    googlecompute.golden-image-gsre-ubuntu-2204: # Runner settings
   174    googlecompute.golden-image-gsre-ubuntu-2204:
   175    googlecompute.golden-image-gsre-ubuntu-2204:
   176    googlecompute.golden-image-gsre-ubuntu-2204: √ Settings Saved.
   177```

View as plain text