generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extra CD support, supply files via cd_files or cd_content (#119)
* Extra CD support, supply files via cd_files or cd_content * Fix highlighted go linter errors * Add suggested tweak * Use driver for adding CDROM and add tests --------- Co-authored-by: Carlos Lapao <[email protected]>
- Loading branch information
Showing
12 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/packer-plugin-sdk/multistep" | ||
packersdk "github.com/hashicorp/packer-plugin-sdk/packer" | ||
) | ||
|
||
// StepAttachCD is a step that attaches a cd to the virtual machine. | ||
// | ||
// Uses: | ||
// | ||
// driver Driver | ||
// ui packersdk.Ui | ||
// vmName string | ||
// | ||
// Produces: | ||
type StepAttachCD struct { | ||
cdPath string | ||
cdromDevice string | ||
} | ||
|
||
// Run adds a virtual CD to the VM and attaches the image. | ||
// If the image is not specified, then this step will be skipped. | ||
func (s *StepAttachCD) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { | ||
// Determine if we even have a cd to attach | ||
var cdPath string | ||
if cdPathRaw, ok := state.GetOk("cd_path"); ok { | ||
cdPath = cdPathRaw.(string) | ||
} else { | ||
log.Println("No cd found, not attaching.") | ||
return multistep.ActionContinue | ||
} | ||
|
||
driver := state.Get("driver").(Driver) | ||
ui := state.Get("ui").(packersdk.Ui) | ||
vmName := state.Get("vmName").(string) | ||
|
||
ui.Say("Attaching cd...") | ||
// Attaching the cd using the driver | ||
cdrom, err := driver.DeviceAddCDROM(vmName, cdPath) | ||
if err != nil { | ||
err = fmt.Errorf("error attaching cd: %s", err) | ||
state.Put("error", err) | ||
ui.Error(err.Error()) | ||
return multistep.ActionHalt | ||
} | ||
// Track the device name so that we can can delete later | ||
s.cdromDevice = cdrom | ||
|
||
return multistep.ActionContinue | ||
} | ||
|
||
// Cleanup removes the virtual FDD device attached to the VM. | ||
func (s *StepAttachCD) Cleanup(state multistep.StateBag) { | ||
driver := state.Get("driver").(Driver) | ||
vmName := state.Get("vmName").(string) | ||
|
||
if s.cdPath == "" { | ||
return | ||
} | ||
|
||
log.Println("Detaching cd disk...") | ||
command := []string{ | ||
"set", vmName, | ||
"--device-del", s.cdromDevice, | ||
} | ||
_ = driver.Prlctl(command...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/packer-plugin-sdk/multistep" | ||
) | ||
|
||
func TestStepAttachCD_impl(t *testing.T) { | ||
var _ multistep.Step = new(StepAttachCD) | ||
} | ||
|
||
func TestStepAttachCD(t *testing.T) { | ||
state := testState(t) | ||
step := new(StepAttachCD) | ||
|
||
// Create a temporary file for our CD | ||
tf, err := ioutil.TempFile("", "packer") | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
tf.Close() | ||
defer os.Remove(tf.Name()) | ||
|
||
state.Put("cd_path", tf.Name()) | ||
state.Put("vmName", "foo") | ||
|
||
driver := state.Get("driver").(*DriverMock) | ||
|
||
// Test the run | ||
if action := step.Run(context.Background(), state); action != multistep.ActionContinue { | ||
t.Fatalf("bad action: %#v", action) | ||
} | ||
if _, ok := state.GetOk("error"); ok { | ||
t.Fatal("should NOT have error") | ||
} | ||
|
||
if !driver.DeviceAddCDROMCalled { | ||
t.Fatal("DeviceAddCDROM not called") | ||
} | ||
if driver.DeviceAddCDROMName != "foo" { | ||
t.Fatal("DeviceAddCDROM name isn't what we specified (foo)") | ||
} | ||
if driver.DeviceAddCDROMResult != "cdrom0" { | ||
t.Fatal("DeviceAddCDROM didn't return cdrom0") | ||
} | ||
if driver.DeviceAddCDROMErr != nil { | ||
t.Fatal("DeviceAddCDROM returned an error") | ||
} | ||
|
||
} | ||
|
||
func TestStepAttachCD_noCD(t *testing.T) { | ||
state := testState(t) | ||
step := new(StepAttachCD) | ||
|
||
driver := state.Get("driver").(*DriverMock) | ||
|
||
// Test the run | ||
if action := step.Run(context.Background(), state); action != multistep.ActionContinue { | ||
t.Fatalf("bad action: %#v", action) | ||
} | ||
if _, ok := state.GetOk("error"); ok { | ||
t.Fatal("should NOT have error") | ||
} | ||
|
||
if driver.DeviceAddCDROMCalled { | ||
t.Fatal("DeviceAddCDROM has been called") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters