Skip to content

Commit

Permalink
Merge pull request #158 from veertuinc/release/v3.2.0
Browse files Browse the repository at this point in the history
release/v3.2.0
  • Loading branch information
NorseGaud authored Jul 27, 2023
2 parents a7cdcec + 682eae9 commit 524bf5b
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 58 deletions.
21 changes: 14 additions & 7 deletions .github/workflows/check-pr.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Check PR

on:
workflow_dispatch:
pull_request:
branches: [ master ]

Expand All @@ -9,21 +10,27 @@ jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Unshallow
run: git fetch --prune --unshallow
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- run: git tag $(cat VERSION)
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.19
- name: Setup `packer`
uses: hashicorp/setup-packer@main
id: setup
with:
go-version: 1.18
version: 1.9.1
- name: Describe plugin
id: plugin_describe
run: echo "::set-output name=api_version::$(go run . describe | jq -r '.api_version')"
- name: Install packer-pdc
run: go get github.com/hashicorp/packer-plugin-sdk/cmd/packer-sdc@latest
- name: Run GoReleaser build
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v4
with:
version: latest
args: build --single-target --snapshot --clean
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This is a [Packer](https://www.packer.io/) Plugin for building images that work
packer {
required_plugins {
veertu-anka = {
version = "= v3.1.1"
version = "= v3.2.0"
source = "github.com/veertuinc/veertu-anka"
}
}
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.1
3.2.0
108 changes: 96 additions & 12 deletions builder/anka/step_create_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"log"
"regexp"
"strconv"

"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -31,20 +33,28 @@ func (s *StepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis
s.vmName = config.VMName

if s.vmName == "" {
if config.HostArch == "arm64" {
installerData := util.InstallerIPSWPlist{}
installerData, err = ankaUtil.ObtainMacOSVersionFromInstallerIPSW(config.Installer)
if err != nil {
return onError(err)
matchInstaller, err := regexp.Match(".app(/?)$|.ipsw(/?)$", []byte(config.Installer))
if err != nil {
return onError(err)
}
if matchInstaller {
if config.HostArch == "arm64" {
installerData := util.InstallerIPSWPlist{}
installerData, err = ankaUtil.ObtainMacOSVersionFromInstallerIPSW(config.Installer)
if err != nil {
return onError(err)
}
s.vmName = fmt.Sprintf("anka-packer-base-%s-%s", installerData.ProductVersion, installerData.ProductBuildVersion)
} else {
installerData := util.InstallerAppPlist{}
installerData, err = ankaUtil.ObtainMacOSVersionFromInstallerApp(config.Installer)
if err != nil {
return onError(err)
}
s.vmName = fmt.Sprintf("anka-packer-base-%s-%s", installerData.OSVersion, installerData.BundlerVersion)
}
s.vmName = fmt.Sprintf("anka-packer-base-%s-%s", installerData.ProductVersion, installerData.ProductBuildVersion)
} else {
installerData := util.InstallerAppPlist{}
installerData, err = ankaUtil.ObtainMacOSVersionFromInstallerApp(config.Installer)
if err != nil {
return onError(err)
}
s.vmName = fmt.Sprintf("anka-packer-base-%s-%s", installerData.OSVersion, installerData.BundlerVersion)
s.vmName = fmt.Sprintf("anka-packer-base-%s", config.Installer)
}
}

Expand All @@ -70,6 +80,16 @@ func (s *StepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis
return onError(err)
}

createdShow, err := s.client.Show(s.vmName)
if err != nil {
return onError(err)
}

err = s.modifyVMProperties(createdShow, config, ui)
if err != nil {
return onError(err)
}

return multistep.ActionContinue
}

Expand Down Expand Up @@ -104,6 +124,70 @@ func (s *StepCreateVM) createFromInstaller(ui packer.Ui, config *Config) error {
return nil
}

func (s *StepCreateVM) modifyVMProperties(showResponse client.ShowResponse, config *Config, ui packer.Ui) error {
stopParams := client.StopParams{
VMName: showResponse.Name,
}

if len(config.PortForwardingRules) > 0 {
describeResponse, err := s.client.Describe(showResponse.Name)
if err != nil {
return err
}
existingForwardedPorts := make(map[int]struct{})
for _, existingNetworkCard := range describeResponse.NetworkCards {
for _, existingPortForwardingRule := range existingNetworkCard.PortForwardingRules {
existingForwardedPorts[existingPortForwardingRule.HostPort] = struct{}{}
}
}
for _, wantedPortForwardingRule := range config.PortForwardingRules {
ui.Say(fmt.Sprintf("Ensuring %s port-forwarding (Guest Port: %s, Host Port: %s, Rule Name: %s)", showResponse.Name, strconv.Itoa(wantedPortForwardingRule.PortForwardingGuestPort), strconv.Itoa(wantedPortForwardingRule.PortForwardingHostPort), wantedPortForwardingRule.PortForwardingRuleName))
if _, ok := existingForwardedPorts[wantedPortForwardingRule.PortForwardingHostPort]; ok {
if wantedPortForwardingRule.PortForwardingHostPort > 0 {
ui.Error(fmt.Sprintf("Found an existing host port rule (%s)! Skipping without setting...", strconv.Itoa(wantedPortForwardingRule.PortForwardingHostPort)))
continue
}
}
err := s.client.Stop(stopParams)
if err != nil {
return err
}
err = s.client.Modify(showResponse.Name, "add", "port-forwarding", "--host-port", strconv.Itoa(wantedPortForwardingRule.PortForwardingHostPort), "--guest-port", strconv.Itoa(wantedPortForwardingRule.PortForwardingGuestPort), wantedPortForwardingRule.PortForwardingRuleName)
if !config.PackerConfig.PackerForce {
if err != nil {
return err
}
}
}
}

if config.HWUUID != "" {
err := s.client.Stop(stopParams)
if err != nil {
return err
}
ui.Say(fmt.Sprintf("Modifying VM custom-variable hw.uuid to %s", config.HWUUID))
err = s.client.Modify(showResponse.Name, "set", "custom-variable", "hw.uuid", config.HWUUID)
if err != nil {
return err
}
}

if config.DisplayController != "" {
err := s.client.Stop(stopParams)
if err != nil {
return err
}
ui.Say(fmt.Sprintf("Modifying VM display controller to %s", config.DisplayController))
err = s.client.Modify(showResponse.Name, "set", "display", "-c", config.DisplayController)
if err != nil {
return err
}
}

return nil
}

// Cleanup will delete the vm if there happens to be an error and handle anything failed states
func (s *StepCreateVM) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)
Expand Down
Loading

0 comments on commit 524bf5b

Please sign in to comment.