packer with hcl configs - datocms-assets.com

48
Packer With HCL Configs Software Engineer at HashiCorp Adrien Delorme

Upload: others

Post on 22-Nov-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Packer With HCL Configs - datocms-assets.com

Packer With HCL Configs

Software Engineer at HashiCorp

Adrien Delorme

Page 2: Packer With HCL Configs - datocms-assets.com
Page 3: Packer With HCL Configs - datocms-assets.com

UBUNTU IMAGE

GENERIC PROVISIONING

SECURITY

TELEMETRY

BASE SERVER IMAGE

Page 4: Packer With HCL Configs - datocms-assets.com

CONSUL IMAGE VAULT IMAGENOMAD IMAGE

Kubernetes

APP IMAGE

App

BASE SERVER IMAGE

KUBERNETES IMAGE

Page 5: Packer With HCL Configs - datocms-assets.com

Packer in numbers

Page 6: Packer With HCL Configs - datocms-assets.com

34 Builders

Packer in numbers

Page 7: Packer With HCL Configs - datocms-assets.com

34 Builders

18 Provisioners

Packer in numbers

Page 8: Packer With HCL Configs - datocms-assets.com

34 Builders

22 Post Processors

18 Provisioners

Packer in numbers

Page 9: Packer With HCL Configs - datocms-assets.com

34 Builders

22 Post Processors

18 Provisioners 4

Maintainers

Packer in numbers

Page 10: Packer With HCL Configs - datocms-assets.com

34 Builders

22 Post Processors

1088 Contributors

18 Provisioners 4

Maintainers

Packer in numbers

Page 11: Packer With HCL Configs - datocms-assets.com

Why HCL2

Page 12: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

{ "variables": { "aws_access_key": null, "aws_secret_key": "", }, "builders": [{ "type": "amazon-ebs", "region": "{{ user `aws_region` }}", "access_key": "{{ user `aws_access_key` }}", "secret_key": "{{ user `aws_secret_key` }}", "ami_name": "ubuntu-16-04-test-{{ timestamp }}", "source_ami_filter": { "filters": { "virtualization-type": "hvm", "name": "ubuntu-xenial-16.04-amd64-server-*", "root-device-type": "ebs" }, "owners": [ "014720109477" ], "most_recent": true },

...

JSON works but…

Page 13: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

{ "variables": { "aws_access_key": null, "aws_secret_key": "", }, "builders": [{ "type": "amazon-ebs", "region": "{{ user `aws_region` }}", "access_key": "{{ user `aws_access_key` }}", "secret_key": "{{ user `aws_secret_key` }}", "ami_name": "ubuntu-16-04-test-{{ timestamp }}", "source_ami_filter": { "filters": { "virtualization-type": "hvm", "name": "ubuntu-xenial-16.04-amd64-server-*", "root-device-type": "ebs" }, "owners": [ "014720109477" ], "most_recent": true },

...

JSON works but… Quotes everywhere

Page 14: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

{ "variables": { "aws_access_key": null, "aws_secret_key": "", }, "builders": [{ "type": "amazon-ebs", "region": "{{ user `aws_region` }}", "access_key": "{{ user `aws_access_key` }}", "secret_key": "{{ user `aws_secret_key` }}", "ami_name": "ubuntu-16-04-test-{{ timestamp }}", "source_ami_filter": { "filters": { "virtualization-type": "hvm", "name": "ubuntu-xenial-16.04-amd64-server-*", "root-device-type": "ebs" }, "owners": [ "014720109477" ], "most_recent": true },

...

JSON works but… Variables are quirky

Page 15: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

{ "variables": { "aws_access_key": null, "aws_secret_key": "", }, "builders": [{ "type": "amazon-ebs", "region": "{{ user `aws_region` }}", "access_key": "{{ user `aws_access_key` }}", "secret_key": "{{ user `aws_secret_key` }}", "ami_name": "ubuntu-16-04-test-{{ timestamp }}", "source_ami_filter": { "filters": { "virtualization-type": "hvm", "name": "ubuntu-xenial-16.04-amd64-server-*", "root-device-type": "ebs" }, "owners": [ "014720109477" ], "most_recent": true },

...

JSON works but… Everything is parsed at

once

Page 16: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

{ "variables": { "aws_access_key": null, "aws_secret_key": "", }, "builders": [{ "type": "amazon-ebs", "region": "{{ user `aws_region` }}", "access_key": "{{ user `aws_access_key` }}", "secret_key": "{{ user `aws_secret_key` }}", "ami_name": "ubuntu-16-04-test-{{ timestamp }}", "source_ami_filter": { "filters": { "virtualization-type": "hvm", "name": "ubuntu-xenial-16.04-amd64-server-*", "root-device-type": "ebs" }, "owners": [ "014720109477" ], "most_recent": true },

...

JSON works but… The User Experience

could be better

Page 17: Packer With HCL Configs - datocms-assets.com

BASE SERVER IMAGE

Page 18: Packer With HCL Configs - datocms-assets.com

CONSUL IMAGE VAULT IMAGENOMAD IMAGE

Kubernetes

APP IMAGE

App

BASE SERVER IMAGE

KUBERNETES IMAGE

Page 19: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

{

"builders": [ { "type": "amazon-ebs" , ... }, { "type": "docker", ... }, { "type": "qemu", ... }, ... ],

"provisioners": [{ "type" : "shell", "scripts": [ "security.sh", "telemetry.sh" ] }],

"post-processors": [{ "type": "manifest", "output": "base-server-image-manifest.json", }]

Build chaining

Page 20: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

{ "builds": [ { "builder_type": "docker", "artifact_id": "container-id", "packer_run_uuid": "6d5d3185-fa95-44e1-8775-9e64fe2e2d8f" }, { "builder_type": "amazon-ebs", "artifact_id": "container-id", "packer_run_uuid": "6d5d3185-fa95-44e1-8775-9e64fe2e2d8f" }, { "builder_type": "qemu", "artifact_id": "container-id", "packer_run_uuid": "6d5d3185-fa95-44e1-8775-9e64fe2e2d8f" } ], "last_run_uuid": "6d5d3185-fa95-44e1-8775-9e64fe2e2d8f" }

Build chaining

Page 21: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

$ cat base-server-image-manifest.json | jq '.last_run_uuid as $last | .builds | map(select(.packer_run_uuid == $last) | {"base-image-artifact-\(.builder_type)": .artifact_id}) | add'

{ "base-image-artifact-docker": "container-id", "base-image-artifact-amazon-ebs": "ami-123456" }

Build chaining Could be easier

Page 22: Packer With HCL Configs - datocms-assets.com

YAML

Page 23: Packer With HCL Configs - datocms-assets.com

YAML was not an option

Page 24: Packer With HCL Configs - datocms-assets.com

YAML was not an option,

and neither was HCL1.

Page 25: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

HCL2 source "amazon-ebs" "my_ubuntu" {

# such comments ! Much wow !

access_key = "${var.aws_access_key}" ami_name = "ubuntu-16-04-test-${local.timestamp}" region = "${var.aws_region}"

source_ami_filter { filters = { name = "ubuntu-xenial-16.04-amd64-server-*" root-device-type = "ebs" virtualization-type = "hvm" }

most_recent = true owners = ["014720109477"] }

...

Page 26: Packer With HCL Configs - datocms-assets.com

What Changes

Page 27: Packer With HCL Configs - datocms-assets.com

IMAGE.JSON

{ "variables": { "foo": "bar" }, "builders": [{ "type": "vmware-iso", "name": "my_builder" }], "provisioners": [{ "type": "shell" }], "post-processors": [{ "type": "googlecompute-import" }] }

IMAGE.PKR.HCL

variable "foo" { default = "bar" }

Page 28: Packer With HCL Configs - datocms-assets.com

IMAGE.JSON

{ "variables": { "foo": "bar" }, "builders": [{ "type": "vmware-iso", "name": "my_builder" }], "provisioners": [{ "type": "shell" }], "post-processors": [{ "type": "googlecompute-import" }] }

IMAGE.PKR.HCL

variable "foo" { default = "bar" } source "vmware-iso" "my_build" { }

Page 29: Packer With HCL Configs - datocms-assets.com

IMAGE.JSON

{ "variables": { "foo": "bar" }, "builders": [{ "type": "vmware-iso", "name": "my_builder" }], "provisioners": [{ "type": "shell" }], "post-processors": [{ "type": "googlecompute-import" }] }

IMAGE.PKR.HCL

variable "foo" { default = "bar" } source "vmware-iso" "my_builder" { }

build { sources = ["source.vmware-iso.my_builder"] source "vmware-iso.my_builder" { output = "other_output" } }

Page 30: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

{ "variables": { "foo": "bar" }, "builders": [{ "type": "vmware-iso", "name": "my_builder" }], "provisioners": [{ "type": "shell" }], "post-processors": [{ "type": "googlecompute-import" }] }

CODE EDITOR

variable "foo" { default = "bar" } source "vmware-iso" "my_builder" { }

build { sources = ["source.vmware-iso.my_builder"] source "vmware-iso.my_builder" { output = "other_output" }

provisioner "shell" { }

post-processor "googlecompute-import" { }

}

Page 31: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

{ "variables": { "foo": "bar" }, "builders": [{ "type": "vmware-iso", "name": "my_builder" }], "provisioners": [{ "type": "shell" }], "post-processors": [{ "type": "googlecompute-import" }] }

CODE EDITOR

variable "foo" { default = "bar" } source "vmware-iso" "my_builder" { }

build { sources = ["source.vmware-iso.my_builder"] source "vmware-iso.my_builder" { output = "other_output" }

provisioner "shell" { }

post-processor "googlecompute-import" { }

}

Page 32: Packer With HCL Configs - datocms-assets.com

. ├── build.ubuntu.pkr.hcl ├── source.vmware-iso.pkr.hcl ├── variables.ubuntu.20.04.pkr.hcl ├── settings.auto.pkrvars.hcl └── etc └── scripts

Split HCL2 config files

Page 33: Packer With HCL Configs - datocms-assets.com

.pkr.hclSplit HCL2 config files

Page 34: Packer With HCL Configs - datocms-assets.com

.pkr.hcl

.pkrvars.hclSplit HCL2 config files

Page 35: Packer With HCL Configs - datocms-assets.com

.pkr.hcl

.pkrvars.hcl

.pkr.json

Split HCL2 config files

Page 36: Packer With HCL Configs - datocms-assets.com

HCL2 JSON != Classical JSONCODE EDITOR

{ "foo": "bar", "baz": 42 }

IMAGE.JSON

variable "foo" { default = "bar" }

source "vmware-iso" "my_builder" { }

build { sources = [ "source.vmware-iso.my_builder" ]

provisioner "shell" { }

post-processor "googlecompute-import" { } }

IMAGE.PKR.HCL

{ "variable": { "foo": { "default": "bar" } }, "source": { "vmware-iso": { "my_builder": {} } }, "build": { "sources": ["source.vmware-iso.my_builder"], "provisioner": [ { "shell": {} } ], "post-processor": [ { "googlecompute-import": {} } ] } }

Page 37: Packer With HCL Configs - datocms-assets.com

Caveats

Page 38: Packer With HCL Configs - datocms-assets.com

HCL2 support is in beta

Page 39: Packer With HCL Configs - datocms-assets.com

Some parts depend on Go interpolation

Page 40: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

locals { ubuntu_1804_boot_command = [ "<esc><wait>", "<enter><wait>", "/install/vmlinuz<wait>",

# ...

"preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/${var.preseed}", ] }

Go interpolation HCL Interpolation

Page 41: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

${upper("{{user `my_var`}}")}

> "{{USER `MY_VAR`}}" Avoid

Page 42: Packer With HCL Configs - datocms-assets.com

CODE EDITOR

${upper("{{user `my_var`}}")}

> "{{USER `MY_VAR`}}"

# this works: ${upper("${var.my_var}")}

Avoid

Page 43: Packer With HCL Configs - datocms-assets.com

Upcoming

Page 44: Packer With HCL Configs - datocms-assets.com

Chaining builds

Page 45: Packer With HCL Configs - datocms-assets.com

Terraform interoperability

Page 46: Packer With HCL Configs - datocms-assets.com

Remove Go templating

Page 47: Packer With HCL Configs - datocms-assets.com

Packer Plugin Repository

Page 48: Packer With HCL Configs - datocms-assets.com

Thanks!Don't hesitate to reach out on [email protected] or [email protected]

github.com/hashicorp/packer is the best place for issues

discuss.hashicorp.com is the best place for questions