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

Add initial support for packer #242

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
8 changes: 4 additions & 4 deletions ansible/apply
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import pathlib
import shutil
import tempfile
import argparse
import ansible
import ansibleutils

def run_playbook(args):
tempdir = pathlib.Path(tempfile.mkdtemp())
ansible.create_workspace(tempdir, args.env, args.playbook)
ansibleutils.create_workspace(tempdir, args.env, args.playbook)
try:
# Invoke the ansible binary installed in the virtualenv
ansible_args = [
str(ansible.VENV_PATH / "bin" / "ansible-playbook"),
str(ansibleutils.VENV_PATH / "bin" / "ansible-playbook"),
"-i", str(tempdir / "env" / "hosts"),
str(tempdir / "play" / "playbook.yml"),
]
Expand All @@ -39,5 +39,5 @@ if __name__ == "__main__":
)
args = parser.parse_args()

ansible.install_ansible()
ansibleutils.install_ansible()
run_playbook(args)
2 changes: 1 addition & 1 deletion ansible/roles/common/tasks/apt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- name: install apt packages
apt:
name:
- aptitude # needed by ansible itself
# - aptitude # needed by ansible itself
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an unrelated change. Was this necessary to build the AMI?

- ca-certificates
- htop
- iptables
Expand Down
26 changes: 24 additions & 2 deletions packer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,36 @@

This directory contains configuration for running packer to build AMIs.

## Dependencies

Running these packer scripts requires the following software:

- python3
- [packer](https://developer.hashicorp.com/packer/downloads)
- [aws-cli](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)

## The `packer` wrapper

This directory contains a python script named `packer` which wraps the system `packer`. The script creates an ansible virtual environment and moves the correct ansible configuration in place.

### Running `packer`

To run the wrapper pass the environment and playbook:
Before running the packer script, you will need to initialize packer:

```bash
packer init ./docs-rs-builder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be run from within the packer directory, correct? If so, do you want to add a cd packer or so to the snippet?

```

You will also need to make sure that you are logged into the correct AWS account in the AWS cli. First, ensure you have the configuration needed to log into the appropriate AWS account in your "~/.aws/config" file (TODO: link to detailed instructions).

For example, to log into the docs-rs staging account, run:

```bash
aws sso login --profile docs-rs-staging
```

To run the wrapper pass the environment and playbook along with the profile name of the aws account you just logged into:

```bash
$ ./packer staging docs-rs-build
$ AWS_PROFILE=docs-rs-staging ./packer staging docs-rs-builder
```
5 changes: 4 additions & 1 deletion packer/docs-rs-builder/builder.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ packer {

data "amazon-parameterstore" "revision" {
name = "/docs-rs/builder/sha"
region = "us-east-1"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting is broken here. We run terraform fmt in CI, do we want to do the same for Packer?


locals {
Expand All @@ -23,7 +24,7 @@ source "amazon-ebs" "ubuntu" {
region = "us-east-1"
source_ami_filter {
filters = {
name = "ubuntu/images/*ubuntu-focal-20.04-amd64-server-*"
name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
Expand Down Expand Up @@ -51,5 +52,7 @@ build {
# The default is the user running packer
user = "ubuntu"
extra_arguments = ["--extra-vars", "vars_repository_sha=${local.revision}"]
# Work around for https://github.com/hashicorp/packer-plugin-ansible/issues/69
ansible_ssh_extra_args = ["-oHostKeyAlgorithms=+ssh-rsa -oPubkeyAcceptedKeyTypes=+ssh-rsa"]
}
}
8 changes: 5 additions & 3 deletions packer/packer
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import pathlib
import shutil
import argparse
import sys
# This adds the ansible folder to the python package search path
# so we can reuse functionality from there.
sys.path.append("../ansible")
rylev marked this conversation as resolved.
Show resolved Hide resolved
import ansible
import ansibleutils

# Utility for removing everything in a directory except one thing
def remove_all_except_one(directory, exception):
Expand Down Expand Up @@ -37,8 +39,8 @@ def create_workspace_dir():
# Create the workspace environment
def create_workspace(env, playbook):
workspace = create_workspace_dir()
ansible.install_ansible(workspace / ".venv")
ansible.create_workspace(workspace, env, playbook)
ansibleutils.install_ansible(workspace / ".venv")
ansibleutils.create_workspace(workspace, env, playbook)

# Link the template into the workspace
template_path = pathlib.Path(playbook).resolve()
Expand Down