2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type MachineImageFilter
|
2019-05-31 08:27:41 -04:00
|
|
|
|
2016-04-14 20:29:27 -04:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/config"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2016-04-14 20:29:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// SourceMachineConfig represents the configuration to run a machine using
|
|
|
|
// the SDC API in order for provisioning to take place.
|
|
|
|
type SourceMachineConfig struct {
|
2019-05-28 11:50:58 -04:00
|
|
|
// Name of the VM used for building the
|
2019-06-06 10:29:25 -04:00
|
|
|
// image. Does not affect (and does not have to be the same) as the name for a
|
|
|
|
// VM instance running this image. Maximum 512 characters but should in
|
|
|
|
// practice be much shorter (think between 5 and 20 characters). For example
|
|
|
|
// mysql-64-server-image-builder. When omitted defaults to
|
|
|
|
// packer-builder-[image_name].
|
|
|
|
MachineName string `mapstructure:"source_machine_name" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The Triton package to use while
|
2019-06-06 10:29:25 -04:00
|
|
|
// building the image. Does not affect (and does not have to be the same) as
|
|
|
|
// the package which will be used for a VM instance running this image. On the
|
|
|
|
// Joyent public cloud this could for example be g3-standard-0.5-smartos.
|
|
|
|
MachinePackage string `mapstructure:"source_machine_package" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The UUID of the image to base the new
|
2019-06-06 10:29:25 -04:00
|
|
|
// image on. Triton supports multiple types of images, called 'brands' in
|
|
|
|
// Triton / Joyent lingo, for contains and VM's. See the chapter Containers
|
|
|
|
// and virtual machines in
|
|
|
|
// the Joyent Triton documentation for detailed information. The following
|
|
|
|
// brands are currently supported by this builder:joyent andkvm. The
|
|
|
|
// choice of base image automatically decides the brand. On the Joyent public
|
|
|
|
// cloud a valid source_machine_image could for example be
|
|
|
|
// 70e3ae72-96b6-11e6-9056-9737fd4d0764 for version 16.3.1 of the 64bit
|
|
|
|
// SmartOS base image (a 'joyent' brand image). source_machine_image_filter
|
|
|
|
// can be used to populate this UUID.
|
|
|
|
MachineImage string `mapstructure:"source_machine_image" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The UUID's of Triton
|
2019-06-06 10:29:25 -04:00
|
|
|
// networks added to the source machine used for creating the image. For
|
|
|
|
// example if any of the provisioners which are run need Internet access you
|
|
|
|
// will need to add the UUID's of the appropriate networks here. If this is
|
|
|
|
// not specified, instances will be placed into the default Triton public and
|
|
|
|
// internal networks.
|
|
|
|
MachineNetworks []string `mapstructure:"source_machine_networks" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Triton metadata
|
2019-06-06 10:29:25 -04:00
|
|
|
// applied to the VM used to create the image. Metadata can be used to pass
|
|
|
|
// configuration information to the VM without the need for networking. See
|
|
|
|
// Using the metadata
|
|
|
|
// API in the
|
|
|
|
// Joyent documentation for more information. This can for example be used to
|
|
|
|
// set the user-script metadata key to have Triton start a user supplied
|
|
|
|
// script after the VM has booted.
|
|
|
|
MachineMetadata map[string]string `mapstructure:"source_machine_metadata" required:"false"`
|
2020-04-16 09:37:22 -04:00
|
|
|
// Key/value pair tags applied to the VM used to create the image.
|
2019-06-06 10:29:25 -04:00
|
|
|
MachineTags map[string]string `mapstructure:"source_machine_tags" required:"false"`
|
2020-03-13 13:04:48 -04:00
|
|
|
// Same as [`source_machine_tags`](#source_machine_tags) but defined as a
|
2020-04-16 09:37:22 -04:00
|
|
|
// singular block containing a `key` and a `value` field. In HCL2 mode the
|
2020-04-01 18:54:21 -04:00
|
|
|
// [`dynamic_block`](/docs/configuration/from-1.5/expressions#dynamic-blocks)
|
2020-03-13 13:04:48 -04:00
|
|
|
// will allow you to create those programatically.
|
2020-11-18 18:46:42 -05:00
|
|
|
MachineTag config.KeyValues `mapstructure:"source_machine_tag" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Whether or not the firewall
|
2019-06-06 10:29:25 -04:00
|
|
|
// of the VM used to create an image of is enabled. The Triton firewall only
|
|
|
|
// filters inbound traffic to the VM. All outbound traffic is always allowed.
|
|
|
|
// Currently this builder does not provide an interface to add specific
|
|
|
|
// firewall rules. Unless you have a global rule defined in Triton which
|
|
|
|
// allows SSH traffic enabling the firewall will interfere with the SSH
|
|
|
|
// provisioner. The default is false.
|
|
|
|
MachineFirewallEnabled bool `mapstructure:"source_machine_firewall_enabled" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Filters used to populate the
|
2019-06-06 10:29:25 -04:00
|
|
|
// source_machine_image field. Example:
|
|
|
|
MachineImageFilters MachineImageFilter `mapstructure:"source_machine_image_filter" required:"false"`
|
builder/triton: Add a data source for source_machine_image
fixes: #5476
Based on this new template addition:
```
{
"variables": {
"image_version": "",
"triton_account": "",
"triton_key_id": "",
"triton_key_material": ""
},
"builders": [{
"type": "triton",
"triton_account": "{{user `triton_account`}}",
"triton_key_id": "{{user `triton_key_id`}}",
"triton_key_material": "{{user `triton_key_material`}}",
"source_machine_package": "g4-highcpu-128M",
"source_machine_image_filter": {
"name": "ubuntu-16.04",
"most_recent": "true"
},
"ssh_username": "root",
"image_version": "{{user `image_version`}}",
"image_name": "teamcity-server"
}],
"provisioners": [
{
"type": "shell",
"start_retry_timeout": "10m",
"inline": [
"sudo apt-get update -y",
"sudo apt-get install -y nginx"
]
}
]
}
```
I got the following output from packer:
```
packer-testing % make image
packer build \
-var "triton_account=stack72_joyent" \
-var "triton_key_id=40:9d:d3:f9:0b:86:62:48:f4:2e:a5:8e:43:00:2a:9b" \
-var "triton_key_material=""" \
-var "image_version=1.0.0" \
new-template.json
triton output will be in this color.
==> triton: Selecting an image based on search criteria
==> triton: Based, on given search criteria, Machine ID is: "7b5981c4-1889-11e7-b4c5-3f3bdfc9b88b"
==> triton: Waiting for source machine to become available...
==> triton: Waiting for SSH to become available...
==> triton: Connected to SSH!
==> triton: Provisioning with shell script: /var/folders/_p/2_zj9lqn4n11fx20qy787p7c0000gn/T/packer-shell797317310
triton: Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
triton: Hit:2 http://archive.ubuntu.com/ubuntu xenial InRelease
```
I can verify from the triton cli tools that the id `7b5981c4` (from the packer output) is indeed the correct ID
```
terraform [master●] % triton images name=~ubuntu-16.04
SHORTID NAME VERSION FLAGS OS TYPE PUBDATE
49b22aec ubuntu-16.04 20160427 P linux lx-dataset 2016-04-27
675834a0 ubuntu-16.04 20160505 P linux lx-dataset 2016-05-05
4edaa46a ubuntu-16.04 20160516 P linux lx-dataset 2016-05-16
05140a7e ubuntu-16.04 20160601 P linux lx-dataset 2016-06-01
e331b22a ubuntu-16.04 20161004 P linux lx-dataset 2016-10-04
8879c758 ubuntu-16.04 20161213 P linux lx-dataset 2016-12-13
7b5981c4 ubuntu-16.04 20170403 P linux lx-dataset 2017-04-03 <------- THIS IS THE LATEST UBUNTU IMAGE
```
2017-10-30 13:26:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type MachineImageFilter struct {
|
|
|
|
MostRecent bool `mapstructure:"most_recent"`
|
|
|
|
Name string
|
|
|
|
OS string
|
|
|
|
Version string
|
|
|
|
Public bool
|
|
|
|
State string
|
|
|
|
Owner string
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MachineImageFilter) Empty() bool {
|
|
|
|
return m.Name == "" && m.OS == "" && m.Version == "" && m.State == "" && m.Owner == "" && m.Type == ""
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare performs basic validation on a SourceMachineConfig struct.
|
|
|
|
func (c *SourceMachineConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
if c.MachinePackage == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("A source_machine_package must be specified"))
|
|
|
|
}
|
|
|
|
|
builder/triton: Add a data source for source_machine_image
fixes: #5476
Based on this new template addition:
```
{
"variables": {
"image_version": "",
"triton_account": "",
"triton_key_id": "",
"triton_key_material": ""
},
"builders": [{
"type": "triton",
"triton_account": "{{user `triton_account`}}",
"triton_key_id": "{{user `triton_key_id`}}",
"triton_key_material": "{{user `triton_key_material`}}",
"source_machine_package": "g4-highcpu-128M",
"source_machine_image_filter": {
"name": "ubuntu-16.04",
"most_recent": "true"
},
"ssh_username": "root",
"image_version": "{{user `image_version`}}",
"image_name": "teamcity-server"
}],
"provisioners": [
{
"type": "shell",
"start_retry_timeout": "10m",
"inline": [
"sudo apt-get update -y",
"sudo apt-get install -y nginx"
]
}
]
}
```
I got the following output from packer:
```
packer-testing % make image
packer build \
-var "triton_account=stack72_joyent" \
-var "triton_key_id=40:9d:d3:f9:0b:86:62:48:f4:2e:a5:8e:43:00:2a:9b" \
-var "triton_key_material=""" \
-var "image_version=1.0.0" \
new-template.json
triton output will be in this color.
==> triton: Selecting an image based on search criteria
==> triton: Based, on given search criteria, Machine ID is: "7b5981c4-1889-11e7-b4c5-3f3bdfc9b88b"
==> triton: Waiting for source machine to become available...
==> triton: Waiting for SSH to become available...
==> triton: Connected to SSH!
==> triton: Provisioning with shell script: /var/folders/_p/2_zj9lqn4n11fx20qy787p7c0000gn/T/packer-shell797317310
triton: Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
triton: Hit:2 http://archive.ubuntu.com/ubuntu xenial InRelease
```
I can verify from the triton cli tools that the id `7b5981c4` (from the packer output) is indeed the correct ID
```
terraform [master●] % triton images name=~ubuntu-16.04
SHORTID NAME VERSION FLAGS OS TYPE PUBDATE
49b22aec ubuntu-16.04 20160427 P linux lx-dataset 2016-04-27
675834a0 ubuntu-16.04 20160505 P linux lx-dataset 2016-05-05
4edaa46a ubuntu-16.04 20160516 P linux lx-dataset 2016-05-16
05140a7e ubuntu-16.04 20160601 P linux lx-dataset 2016-06-01
e331b22a ubuntu-16.04 20161004 P linux lx-dataset 2016-10-04
8879c758 ubuntu-16.04 20161213 P linux lx-dataset 2016-12-13
7b5981c4 ubuntu-16.04 20170403 P linux lx-dataset 2017-04-03 <------- THIS IS THE LATEST UBUNTU IMAGE
```
2017-10-30 13:26:42 -04:00
|
|
|
if c.MachineImage != "" && c.MachineImageFilters.Name != "" {
|
|
|
|
errs = append(errs, fmt.Errorf("You cannot specify a Machine Image and also Machine Name filter"))
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.MachineNetworks == nil {
|
|
|
|
c.MachineNetworks = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.MachineMetadata == nil {
|
|
|
|
c.MachineMetadata = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.MachineTags == nil {
|
|
|
|
c.MachineTags = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2020-03-16 10:46:08 -04:00
|
|
|
errs = append(errs, c.MachineTag.CopyOn(&c.MachineTags)...)
|
2016-04-14 20:29:27 -04:00
|
|
|
|
2020-03-16 07:42:56 -04:00
|
|
|
return errs
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|