generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
253 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ main | |
dist/* | ||
packer-plugin-tart | ||
.docs/* | ||
|
||
example/*.iso | ||
example/cidata/* |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package tart | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"github.com/hashicorp/packer-plugin-sdk/multistep" | ||
packersdk "github.com/hashicorp/packer-plugin-sdk/packer" | ||
"os/exec" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type stepCreateLinuxVM struct{} | ||
|
||
func (s *stepCreateLinuxVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { | ||
config := state.Get("config").(*Config) | ||
ui := state.Get("ui").(packersdk.Ui) | ||
|
||
ui.Say("Creating virtual machine...") | ||
|
||
createArguments := []string{ | ||
"create", "--linux", | ||
} | ||
|
||
if config.DiskSizeGb > 0 { | ||
createArguments = append(createArguments, "--disk-size", strconv.Itoa(int(config.DiskSizeGb))) | ||
} | ||
|
||
createArguments = append(createArguments, config.VMName) | ||
|
||
if _, err := TartExec(createArguments...); err != nil { | ||
err := fmt.Errorf("Failed to create a VM: %s", err) | ||
state.Put("error", err) | ||
ui.Error(err.Error()) | ||
|
||
return multistep.ActionHalt | ||
} | ||
|
||
if s.RunInstaller(ctx, state) != multistep.ActionContinue { | ||
return multistep.ActionHalt | ||
} | ||
|
||
if config.CreateGraceTime != 0 { | ||
message := fmt.Sprintf("Waiting %v to let the Virtualization.Framework's installation process "+ | ||
"to finish correctly...", config.CreateGraceTime) | ||
ui.Say(message) | ||
time.Sleep(config.CreateGraceTime) | ||
} | ||
|
||
return multistep.ActionContinue | ||
} | ||
|
||
func (s *stepCreateLinuxVM) Cleanup(state multistep.StateBag) { | ||
// nothing to clean up | ||
} | ||
|
||
func (s *stepCreateLinuxVM) RunInstaller(ctx context.Context, state multistep.StateBag) multistep.StepAction { | ||
config := state.Get("config").(*Config) | ||
ui := state.Get("ui").(packersdk.Ui) | ||
|
||
ui.Say("Starting the virtual machine for installation...") | ||
runArgs := []string{"run", config.VMName, } | ||
if config.Headless { | ||
runArgs = append(runArgs, "--no-graphics") | ||
} else { | ||
runArgs = append(runArgs, "--graphics") | ||
} | ||
if !config.DisableVNC { | ||
runArgs = append(runArgs, "--vnc-experimental") | ||
} | ||
for _, iso := range config.FromISO { | ||
runArgs = append(runArgs, fmt.Sprintf("--disk=%s:ro", iso)) | ||
} | ||
cmd := exec.Command("tart", runArgs...) | ||
stdout := bytes.NewBufferString("") | ||
cmd.Stdout = stdout | ||
cmd.Stderr = uiWriter{ui: ui} | ||
|
||
// Prevent the Tart from opening the Screen Sharing | ||
// window connected to the VNC server we're starting | ||
if !config.DisableVNC { | ||
cmd.Env = cmd.Environ() | ||
cmd.Env = append(cmd.Env, "CI=true") | ||
} | ||
|
||
if err := cmd.Start(); err != nil { | ||
err = fmt.Errorf("Error starting VM: %s", err) | ||
state.Put("error", err) | ||
ui.Error(err.Error()) | ||
return multistep.ActionHalt | ||
} | ||
|
||
defer func() { | ||
ui.Say("Waiting for the install process to shutdown the VM...") | ||
_, _ = cmd.Process.Wait() | ||
}() | ||
|
||
if !config.DisableVNC { | ||
if !typeBootCommandOverVNC(ctx, state, config, ui, stdout) { | ||
return multistep.ActionHalt | ||
} | ||
} | ||
|
||
return multistep.ActionContinue | ||
} |
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,39 @@ | ||
packer { | ||
required_plugins { | ||
tart = { | ||
version = ">= 0.5.3" | ||
source = "github.com/cirruslabs/tart" | ||
} | ||
} | ||
} | ||
|
||
source "tart-cli" "tart" { | ||
vm_base_name = "ubuntu-22.04-vanilla" | ||
vm_name = "ubuntu-22.04-hello" | ||
cpu_count = 4 | ||
memory_gb = 8 | ||
disk_size_gb = 10 | ||
headless = true | ||
disable_vnc = true | ||
ssh_password = "ubuntu" | ||
ssh_username = "ubuntu" | ||
ssh_timeout = "120s" | ||
} | ||
|
||
build { | ||
sources = ["source.tart-cli.tart"] | ||
|
||
# resize the disk | ||
provisioner "shell" { | ||
inline = [ | ||
"sudo growpart /dev/vda 2", | ||
"sudo resize2fs /dev/vda2" | ||
] | ||
} | ||
|
||
provisioner "shell" { | ||
inline = [ | ||
"hello" | ||
] | ||
} | ||
} |
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,41 @@ | ||
#!/bin/zsh | ||
|
||
set -eu | ||
|
||
if [ ! -f ubuntu-22.04.1-live-server-arm64.iso ]; then | ||
echo "Downloading image..." | ||
curl -fSLO https://cdimage.ubuntu.com/releases/22.04/release/ubuntu-22.04.1-live-server-arm64.iso | ||
fi | ||
|
||
if [ -d cidata ]; then | ||
rm -rf cidata* | ||
fi | ||
|
||
mkdir cidata | ||
touch cidata/meta-data | ||
cat << 'EOF' > cidata/user-data | ||
#cloud-config | ||
autoinstall: | ||
version: 1 | ||
identity: | ||
hostname: ubuntu-server | ||
# the password is ubuntu, needs to be encoded to /etc/shadow format | ||
password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0" | ||
username: ubuntu | ||
storage: | ||
swap: | ||
size: 0 | ||
layout: | ||
name: direct | ||
ssh: | ||
install-server: true | ||
allow-pw: true | ||
late-commands: | ||
- "echo 'ubuntu ALL=(ALL) NOPASSWD: ALL' > /target/etc/sudoers.d/ubuntu-nopasswd" | ||
shutdown: "poweroff" | ||
EOF | ||
hdiutil makehybrid -o cidata.iso cidata -joliet -iso |
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,38 @@ | ||
packer { | ||
required_plugins { | ||
tart = { | ||
version = ">= 0.5.3" | ||
source = "github.com/cirruslabs/tart" | ||
} | ||
} | ||
} | ||
|
||
source "tart-cli" "tart" { | ||
from_iso = ["cidata.iso", "ubuntu-22.04.1-live-server-arm64.iso"] | ||
vm_name = "ubuntu-22.04-vanilla" | ||
cpu_count = 4 | ||
memory_gb = 8 | ||
disk_size_gb = 8 | ||
boot_command = [ | ||
# grub | ||
"<wait5s><enter>", | ||
# autoinstall prompt | ||
"<wait30s>yes<enter>" | ||
] | ||
ssh_password = "ubuntu" | ||
ssh_username = "ubuntu" | ||
ssh_timeout = "120s" | ||
} | ||
|
||
build { | ||
sources = ["source.tart-cli.tart"] | ||
|
||
provisioner "shell" { | ||
environment_vars = ["DEBIAN_FRONTEND=noninteractive"] | ||
execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" | ||
inline = [ | ||
"apt-get update", | ||
"apt-get install -y hello" | ||
] | ||
} | ||
} |