Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Gofmt, remove network card, examples centos7, fix gopkg, fixing CPU_limit #144

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[[constraint]]
name = "github.com/hashicorp/packer"
version = "~1.2"
version = "~1.2.0"

[[constraint]]
name = "github.com/vmware/govmomi"
version = "~0.17"
version = "~0.17.0"

[[override]]
name = "github.com/masterzen/azure-sdk-for-go"
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ This a plugin for [HashiCorp Packer](https://www.packer.io/). It uses native vSp
* Download binaries from the [releases page](https://github.com/jetbrains-infra/packer-builder-vsphere/releases).
* [Install](https://www.packer.io/docs/extending/plugins.html#installing-plugins) the plugins, or simply put them into the same directory with JSON templates. On Linux and macOS run `chmod +x` on the files.

##Build

Use docker and docker-compose
```
docker-compose up
```
the binary will be in bin/ folder


## Examples

See complete Ubuntu, Windows, and macOS templates in the [examples folder](https://github.com/jetbrains-infra/packer-builder-vsphere/tree/master/examples/).
Expand Down Expand Up @@ -95,6 +104,7 @@ See complete Ubuntu, Windows, and macOS templates in the [examples folder](https

### Postprocessing

* `remove_network_card`(boolean) - Remove all network cards before creating a snapshot/template if set to `true`. Defaults to `false`.
* `create_snapshot`(boolean) - Create a snapshot when set to `true`, so the VM can be used as a base for linked clones. Defaults to `false`.
* `convert_to_template`(boolean) - Convert VM to a template. Defaults to `false`.

Expand Down
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export GOARCH=amd64
mkdir -p bin
rm -f bin/*

gofmt -e -w .

GOOS=darwin go build -o bin/packer-builder-vsphere-iso.macos ./cmd/iso
GOOS=linux go build -o bin/packer-builder-vsphere-iso.linux ./cmd/iso
GOOS=windows go build -o bin/packer-builder-vsphere-iso.exe ./cmd/iso
Expand Down
7 changes: 5 additions & 2 deletions clone/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package clone

import (
packerCommon "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/common"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/helper/communicator"
)

type Builder struct {
Expand Down Expand Up @@ -67,6 +67,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}

steps = append(steps,
&common.StepRemoveNetworkCard{
RemoveNetworkCard: b.config.RemoveNetworkCard,
},
&common.StepCreateSnapshot{
CreateSnapshot: b.config.CreateSnapshot,
},
Expand Down
2 changes: 1 addition & 1 deletion clone/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
commonT "github.com/jetbrains-infra/packer-builder-vsphere/common/testing"

"github.com/hashicorp/packer/packer"
"testing"
"github.com/jetbrains-infra/packer-builder-vsphere/common"
"testing"
)

func TestCloneBuilderAcc_default(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions clone/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package clone
import (
packerCommon "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/jetbrains-infra/packer-builder-vsphere/common"
"github.com/hashicorp/packer/helper/config"
)

type Config struct {
Expand All @@ -18,12 +18,13 @@ type Config struct {
common.HardwareConfig `mapstructure:",squash"`
common.ConfigParamsConfig `mapstructure:",squash"`

common.RunConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`
common.ShutdownConfig `mapstructure:",squash"`
common.RunConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`
common.ShutdownConfig `mapstructure:",squash"`

CreateSnapshot bool `mapstructure:"create_snapshot"`
ConvertToTemplate bool `mapstructure:"convert_to_template"`
RemoveNetworkCard bool `mapstructure:"remove_network_card"`

ctx interpolate.Context
}
Expand Down
16 changes: 8 additions & 8 deletions clone/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestCloneConfig_Timeout(t *testing.T) {
raw["shutdown_timeout"] = "3m"
conf, warns, err := NewConfig(raw)
testConfigOk(t, warns, err)
if conf.ShutdownConfig.Timeout != 3 * time.Minute {
if conf.ShutdownConfig.Timeout != 3*time.Minute {
t.Fatalf("shutdown_timeout sould be equal 3 minutes, got %v", conf.ShutdownConfig.Timeout)
}
}
Expand All @@ -41,13 +41,13 @@ func TestCloneConfig_RAMReservation(t *testing.T) {
func minimalConfig() map[string]interface{} {
return map[string]interface{}{
"vcenter_server": "vcenter.domain.local",
"username": "root",
"password": "vmware",
"template": "ubuntu",
"vm_name": "vm1",
"host": "esxi1.domain.local",
"ssh_username": "root",
"ssh_password": "secret",
"username": "root",
"password": "vmware",
"template": "ubuntu",
"vm_name": "vm1",
"host": "esxi1.domain.local",
"ssh_username": "root",
"ssh_password": "secret",
}
}

Expand Down
6 changes: 3 additions & 3 deletions clone/step_clone.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package clone

import (
"context"
"fmt"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"fmt"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/jetbrains-infra/packer-builder-vsphere/common"
"context"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
)

type CloneConfig struct {
Expand Down
2 changes: 1 addition & 1 deletion common/config_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"io/ioutil"

packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
"golang.org/x/crypto/ssh"
"github.com/hashicorp/packer/helper/communicator"
)

func CommHost(state multistep.StateBag) (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions common/step_config_params.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package common

import (
"context"
"fmt"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/hashicorp/packer/helper/multistep"
"fmt"
"context"
)

type ConfigParamsConfig struct {
Expand Down
4 changes: 2 additions & 2 deletions common/step_connect.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package common

import (
"github.com/hashicorp/packer/helper/multistep"
"context"
"fmt"
"github.com/hashicorp/packer/helper/multistep"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"context"
)

type ConnectConfig struct {
Expand Down
20 changes: 12 additions & 8 deletions common/step_hardware.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package common

import (
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/hashicorp/packer/helper/multistep"
"context"
"fmt"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
)

type HardwareConfig struct {
CPUs int32 `mapstructure:"CPUs"`
CPUReservation int64 `mapstructure:"CPU_reservation"`
CPULimit int64 `mapstructure:"CPU_limit"`
CpuHotAddEnabled bool `mapstructure:"CPU_hot_plug"`
CPUs int32 `mapstructure:"CPUs"`
CPUReservation int64 `mapstructure:"CPU_reservation"`
CPULimit int64 `mapstructure:"CPU_limit"`
CpuHotAddEnabled bool `mapstructure:"CPU_hot_plug"`

RAM int64 `mapstructure:"RAM"`
RAMReservation int64 `mapstructure:"RAM_reservation"`
RAMReserveAll bool `mapstructure:"RAM_reserve_all"`
MemoryHotAddEnabled bool `mapstructure:"RAM_hot_plug"`

NestedHV bool `mapstructure:"NestedHV"`
NestedHV bool `mapstructure:"NestedHV"`
}

func (c *HardwareConfig) Prepare() []error {
Expand All @@ -29,6 +29,10 @@ func (c *HardwareConfig) Prepare() []error {
errs = append(errs, fmt.Errorf("'RAM_reservation' and 'RAM_reserve_all' cannot be used together"))
}

if c.CPULimit == 0 {
c.CPULimit = -1
}

return errs
}

Expand Down
38 changes: 38 additions & 0 deletions common/step_remove_network_card.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package common

import (
"context"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/vmware/govmomi/vim25/types"
)

type StepRemoveNetworkCard struct {
RemoveNetworkCard bool
}

func (s *StepRemoveNetworkCard) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)

if s.RemoveNetworkCard {
ui.Say("Deleting Network Cards...")
devices, err := vm.Devices()
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}

cards := devices.SelectByType((*types.VirtualEthernetCard)(nil))

if err = vm.RemoveDevice(true, cards...); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
}

return multistep.ActionContinue
}

func (s *StepRemoveNetworkCard) Cleanup(state multistep.StateBag) {}
4 changes: 2 additions & 2 deletions common/step_run.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package common

import (
"context"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"strings"
"context"
)

type RunConfig struct {
Expand Down Expand Up @@ -40,7 +40,7 @@ func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.Ste
ui.Say("Power on VM...")
err := vm.PowerOn()
if err != nil {
state.Put("error",err)
state.Put("error", err)
return multistep.ActionHalt
}

Expand Down
10 changes: 5 additions & 5 deletions common/step_shutdown.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package common

import (
"bytes"
"context"
"fmt"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"fmt"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"log"
"time"
"bytes"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"context"
)

type ShutdownConfig struct {
Command string `mapstructure:"shutdown_command"`
RawTimeout string `mapstructure:"shutdown_timeout"`

Timeout time.Duration
Timeout time.Duration
}

func (c *ShutdownConfig) Prepare() []error {
Expand Down
4 changes: 2 additions & 2 deletions common/step_snapshot.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package common

import (
"context"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"context"
)

type StepCreateSnapshot struct{
type StepCreateSnapshot struct {
CreateSnapshot bool
}

Expand Down
4 changes: 2 additions & 2 deletions common/step_template.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package common

import (
"context"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"context"
)

type StepConvertToTemplate struct{
type StepConvertToTemplate struct {
ConvertToTemplate bool
}

Expand Down
4 changes: 2 additions & 2 deletions common/step_wait_for_ip.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package common

import (
"github.com/hashicorp/packer/helper/multistep"
"context"
"fmt"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"time"
"context"
)

type StepWaitForIp struct{}
Expand Down
Loading