2016-04-14 20:29:27 -04:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DriverMock struct {
|
|
|
|
CreateImageFromMachineId string
|
|
|
|
CreateImageFromMachineErr error
|
|
|
|
|
|
|
|
CreateMachineId string
|
|
|
|
CreateMachineErr error
|
|
|
|
|
|
|
|
DeleteImageId string
|
|
|
|
DeleteImageErr error
|
|
|
|
|
|
|
|
DeleteMachineId string
|
|
|
|
DeleteMachineErr error
|
|
|
|
|
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
|
|
|
GetImageId string
|
|
|
|
GetImageErr error
|
|
|
|
|
2016-04-14 20:29:27 -04:00
|
|
|
GetMachineErr error
|
|
|
|
|
|
|
|
StopMachineId string
|
|
|
|
StopMachineErr error
|
|
|
|
|
|
|
|
WaitForImageCreationErr error
|
|
|
|
|
|
|
|
WaitForMachineDeletionErr error
|
|
|
|
|
|
|
|
WaitForMachineStateErr error
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (d *DriverMock) GetImage(config Config) (string, error) {
|
|
|
|
if d.GetImageErr != nil {
|
|
|
|
return "", d.GetImageErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return config.MachineImage, nil
|
|
|
|
}
|
|
|
|
|
2016-04-14 20:29:27 -04:00
|
|
|
func (d *DriverMock) CreateImageFromMachine(machineId string, config Config) (string, error) {
|
|
|
|
if d.CreateImageFromMachineErr != nil {
|
|
|
|
return "", d.CreateImageFromMachineErr
|
|
|
|
}
|
|
|
|
|
|
|
|
d.CreateImageFromMachineId = config.ImageName
|
|
|
|
|
|
|
|
return d.CreateImageFromMachineId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) CreateMachine(config Config) (string, error) {
|
|
|
|
if d.CreateMachineErr != nil {
|
|
|
|
return "", d.CreateMachineErr
|
|
|
|
}
|
|
|
|
|
|
|
|
d.CreateMachineId = config.MachineName
|
|
|
|
|
|
|
|
return d.CreateMachineId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) DeleteImage(imageId string) error {
|
|
|
|
if d.DeleteImageErr != nil {
|
|
|
|
return d.DeleteImageErr
|
|
|
|
}
|
|
|
|
|
|
|
|
d.DeleteImageId = imageId
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) DeleteMachine(machineId string) error {
|
|
|
|
if d.DeleteMachineErr != nil {
|
|
|
|
return d.DeleteMachineErr
|
|
|
|
}
|
|
|
|
|
|
|
|
d.DeleteMachineId = machineId
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-26 15:07:45 -04:00
|
|
|
func (d *DriverMock) GetMachineIP(machineId string) (string, error) {
|
2016-04-14 20:29:27 -04:00
|
|
|
if d.GetMachineErr != nil {
|
|
|
|
return "", d.GetMachineErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return "ip", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) StopMachine(machineId string) error {
|
|
|
|
d.StopMachineId = machineId
|
|
|
|
|
|
|
|
return d.StopMachineErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) WaitForImageCreation(machineId string, timeout time.Duration) error {
|
|
|
|
return d.WaitForImageCreationErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) WaitForMachineDeletion(machineId string, timeout time.Duration) error {
|
|
|
|
return d.WaitForMachineDeletionErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) WaitForMachineState(machineId string, state string, timeout time.Duration) error {
|
|
|
|
return d.WaitForMachineStateErr
|
|
|
|
}
|