# Edge Golden Image Guide This is a brief guide on how to run the following to generate a custom golden image ## Authors - Quan Huynh ([@qh185008](https://github.com/qh185008)) ## Prerequisites You will need to have these following installed: - Packer: https://developer.hashicorp.com/packer/tutorials/docker-get-started/get-started-install-cli If 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 ## Overview - Build config is at `hack/build/edge-golden-image` - `packer.pkr.hcl` is the main file to run any build - `setup_script.sh` is to install packages and dependencies during packer provisioning - `startup_script.sh` is executed when the VM starts. Used to connect a runner to GitHub - `shutdown_script.sh` is executed when the VM stops. Disconnected the runner from GitHub ## Quickstart To build with all defaults: ``` packer build hack/build/edge-golden-image/packer.pkr.hcl ``` - 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 ``` variable "project_id" { type = string } variable "account_file" { type = string } variable "zone" { type = string default = "us-east1-b" } variable "image_description" { type = string default = "Edge Golden Image, powered by GSRE golden image" } variable "source_image" { type = string default = "gsre-base-ubuntu-2204-20230510" sensitive = true } ``` - Be mindful with these two lines inside `source` block: ``` source_image = "${var.source_image}" source_image_project_id = ["gsre-compute-images-testing"] ``` - 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` ``` # Run this to get list of account u can use gcloud auth list # Run this to set to the golden account gcloud config set account `GOLDEN_ACCOUNT_NAME` # Run this to update the gcloud account. # This command will help you speed up the switching account gcloud container clusters get-credentials platform-infra --zone us-east1-b --project ret-edge-pltf-infra ``` - 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 ``` build { sources = ["sources.googlecompute.golden-image-gsre-ubuntu-2204"] provisioner "file" { source = "/path/to/service/account/key.json" destination = "/tmp/serviceAccountKey.json" } provisioner "file" { source = "./startup_script.sh" destination = "/tmp/startup_script.sh" } provisioner "file" { source = "./shutdown_script.sh" destination = "/tmp/shutdown_script.sh" } provisioner "shell" { script = "./setup_script.sh" //expect_disconnect = true } ``` - 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 ``` provisioner "file" { source = "/path/to/service/account/key.json" destination = "/tmp/serviceAccountKey.json" } ``` - 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 ``` provisioner "file" { source = "./startup_script.sh" destination = "/tmp/startup_script.sh" } provisioner "file" { source = "./shutdown_script.sh" destination = "/tmp/shutdown_script.sh" } ``` - This is to start the setup process during provisioner ``` provisioner "shell" { script = "./setup_script.sh" } ``` - 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. - 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 ``` provisioner "shell" { script = "./script_you_want_to_run.sh" } ``` - Example output: ``` ==> googlecompute.golden-image-gsre-ubuntu-2204: Provisioning with shell script: ./start.sh ==> googlecompute.golden-image-gsre-ubuntu-2204: % Total % Received % Xferd Average Speed Time Time Time Current ==> googlecompute.golden-image-gsre-ubuntu-2204: Dload Upload Total Spent Left Speed ==> googlecompute.golden-image-gsre-ubuntu-2204: 100 96 100 96 0 0 329 0 --:--:-- --:--:-- --:--:-- 331 googlecompute.golden-image-gsre-ubuntu-2204: googlecompute.golden-image-gsre-ubuntu-2204: -------------------------------------------------------------------------------- googlecompute.golden-image-gsre-ubuntu-2204: | ____ _ _ _ _ _ _ _ _ | googlecompute.golden-image-gsre-ubuntu-2204: | / ___(_) |_| | | |_ _| |__ / \ ___| |_(_) ___ _ __ ___ | googlecompute.golden-image-gsre-ubuntu-2204: | | | _| | __| |_| | | | | '_ \ / _ \ / __| __| |/ _ \| '_ \/ __| | googlecompute.golden-image-gsre-ubuntu-2204: | | |_| | | |_| _ | |_| | |_) | / ___ \ (__| |_| | (_) | | | \__ \ | googlecompute.golden-image-gsre-ubuntu-2204: | \____|_|\__|_| |_|\__,_|_.__/ /_/ \_\___|\__|_|\___/|_| |_|___/ | googlecompute.golden-image-gsre-ubuntu-2204: | | googlecompute.golden-image-gsre-ubuntu-2204: | Self-hosted runner registration | googlecompute.golden-image-gsre-ubuntu-2204: | | googlecompute.golden-image-gsre-ubuntu-2204: -------------------------------------------------------------------------------- googlecompute.golden-image-gsre-ubuntu-2204: googlecompute.golden-image-gsre-ubuntu-2204: # Authentication googlecompute.golden-image-gsre-ubuntu-2204: googlecompute.golden-image-gsre-ubuntu-2204: googlecompute.golden-image-gsre-ubuntu-2204: √ Connected to GitHub googlecompute.golden-image-gsre-ubuntu-2204: googlecompute.golden-image-gsre-ubuntu-2204: # Runner Registration googlecompute.golden-image-gsre-ubuntu-2204: googlecompute.golden-image-gsre-ubuntu-2204: googlecompute.golden-image-gsre-ubuntu-2204: googlecompute.golden-image-gsre-ubuntu-2204: googlecompute.golden-image-gsre-ubuntu-2204: √ Runner successfully added googlecompute.golden-image-gsre-ubuntu-2204: √ Runner connection is good googlecompute.golden-image-gsre-ubuntu-2204: googlecompute.golden-image-gsre-ubuntu-2204: # Runner settings googlecompute.golden-image-gsre-ubuntu-2204: googlecompute.golden-image-gsre-ubuntu-2204: googlecompute.golden-image-gsre-ubuntu-2204: √ Settings Saved. ```