Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build sacrificial VM image with Packer #7

Merged
merged 8 commits into from
May 19, 2022
Merged

Conversation

paseaf
Copy link
Owner

@paseaf paseaf commented May 18, 2022

This PR should close #6
Refer to /packer/README.md for details

@paseaf paseaf linked an issue May 18, 2022 that may be closed by this pull request
@paseaf
Copy link
Owner Author

paseaf commented May 18, 2022

Task notes

  • Create a script to automate Terraform & Packer build
    Do it later when have time.
  • Adapt Terraform to use Packer image
  • update system packages and install Docker for Packer image
    Note: use update.sh to achieve unattended update.
  • fix packer build error

    ubuntu-2204-with-docker.googlecompute.ubuntu-2204: Error waiting for SSH: Packer experienced an authentication error when trying to connect via SSH. This can happen if your username/password are wrong. You may want to double-check your credentials as part of your debugging process. original error: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
    Solution: Follow Packer - Running outside of Google Cloud to set up GOOGLE_APPLICATION_CRENDENTIALS env variable.

  • packer build seems to already starts a new vm instance?
    No. It just creates a vm and use it to create the image. The VM is deleted once the image is created.
  • Does machine type matter in Packer setup?
    Not sure. Check it out when have to.

Takeaways

  • Packer prints script commands in red!
    image
    Answer: It's an expected behavior caused by set -x!! See here
  • Bug: packer build fails randomly with Could not get lock /var/lib/apt/lists/lock. It is held by process 1712 (apt-get)
    Solution:
    Wait for locked file with modified apt-get:
    #  https://github.com/geerlingguy/packer-boxes/issues/7#issuecomment-425641793
    function apt-get() {
      while fuser -s /var/lib/apt/lists/lock;
      do echo 'apt-get is waiting for the lock release ...';
        sleep 1;
      done;
      /usr/bin/apt-get "$@";
    }
  • How to create meaningful image names automatically with packer?
    Use HCL Functions!! ({{timestamp}})
    example: https://github.com/vancluever/packer-terraform-example/blob/master/packer/ami.json
    For simplicity: use the same image name, and overwrite old once when build again!

@paseaf paseaf self-assigned this May 19, 2022
packer/ubuntu-2204.pkr.hcl Outdated Show resolved Hide resolved
packer/scripts/install_docker.sh Outdated Show resolved Hide resolved
packer/scripts/install_docker.sh Outdated Show resolved Hide resolved
packer/scripts/update.sh Outdated Show resolved Hide resolved
terraform/README.md Outdated Show resolved Hide resolved
@paseaf paseaf changed the title [WIP] Automate image build process for sacrificial VM Automate image build process for sacrificial VM May 19, 2022
@paseaf paseaf marked this pull request as ready for review May 19, 2022 18:45
@paseaf paseaf force-pushed the zl/build-vm-with-packer branch from 4fc366a to 9c18239 Compare May 19, 2022 19:01
@paseaf
Copy link
Owner Author

paseaf commented May 19, 2022

LGTM!

@paseaf paseaf changed the title Automate image build process for sacrificial VM Build sacrificial VM image with Packer May 19, 2022
@paseaf paseaf added the documentation Improvements or additions to documentation label May 19, 2022
@paseaf paseaf merged commit efea665 into main May 19, 2022
@paseaf paseaf deleted the zl/build-vm-with-packer branch May 19, 2022 19:07
@paseaf paseaf added the packer label Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deployment documentation Improvements or additions to documentation packer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automate image build process for sacrificial VM
1 participant