Skip to content

Commit

Permalink
Merge pull request #12 from pkorovin/port_prep
Browse files Browse the repository at this point in the history
Fixes/improvements before importing into OpenBSD ports tree
  • Loading branch information
double-p authored Apr 19, 2020
2 parents d8be62a + 9b6ce0c commit cfdd920
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 179 deletions.
21 changes: 19 additions & 2 deletions builder/openbsd-vmm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
const (
_DISK_QCOW2 = "qcow2"
_DISK_RAW = "raw"
_GENFILES_DEFAULT_EXT = "pkr.in"
)

type Config struct {
Expand All @@ -42,6 +43,9 @@ type Config struct {
OutDir string `mapstructure:"output_directory"`
UserData string `mapstructure:"user_data"`

GenFilesExtension string `mapstructure:"gen_files_extension"`
GenFilesPattern string `mapstructure:"gen_files_pattern"`

ctx interpolate.Context
}

Expand Down Expand Up @@ -81,6 +85,14 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
fmt.Errorf("Output directory must be specified (var: output_directory)"))
}

if c.GenFilesExtension == "" {
c.GenFilesExtension = _GENFILES_DEFAULT_EXT
}

if c.GenFilesPattern == "" {
c.GenFilesPattern = c.VMName
}

switch c.DiskFormat {
case _DISK_RAW, _DISK_QCOW2:
// use default raw format if not specified
Expand All @@ -91,6 +103,11 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
fmt.Errorf("Unsupported disk_format name: "+c.DiskFormat+", must be either raw or qcow2"))
}

if c.DiskBase == "" && c.DiskSize == "" {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Disk size must be specified when not using base image (var: disk_size)"))
}

if c.DiskBase != "" && c.DiskFormat != _DISK_QCOW2 {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Cannot use "+c.DiskFormat+" with base image, only qcow2 format is supported"))
Expand All @@ -114,9 +131,9 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
fmt.Errorf("Only one SSH authentication method is supported (vars: ssh_agent_auth, ssh_password, ssh_private_key_file)"))

}
} else {
} else if c.CommConfig.Type != "none" {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Only ssh communicator is supported (var: communicator)"))
fmt.Errorf("Only ssh or none communicator is supported (var: communicator)"))
}

if errs != nil && len(errs.Errors) > 0 {
Expand Down
6 changes: 6 additions & 0 deletions builder/openbsd-vmm/config.hcl2spec.go

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

6 changes: 4 additions & 2 deletions builder/openbsd-vmm/step_create_disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ func (step *stepCreateDisks) Run(ctx context.Context, state multistep.StateBag)

command := []string{
"create",
"-s",
step.size,
}
if step.baseImage != "" {
command = append(command,
"-b", step.baseImage,
)
} else {
command = append(command,
"-s", step.size,
)
}
command = append(command,
step.format+":"+path,
Expand Down
6 changes: 4 additions & 2 deletions builder/openbsd-vmm/step_gen_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func (step *stepGenFiles) Run(ctx context.Context, state multistep.StateBag) mul
httpPort := state.Get("http_port").(int)
hostIP := state.Get("host_ip").(string)
VMName := config.VMName
GenFilesExtension := config.GenFilesExtension
GenFilesPattern := config.GenFilesPattern

step.ctx.Data = &genFilesTemplateData{
VMName,
Expand All @@ -66,7 +68,7 @@ func (step *stepGenFiles) Run(ctx context.Context, state multistep.StateBag) mul
return nil
}

matched, err := filepath.Match(VMName + "*.pkr.in", fileinfo.Name())
matched, err := filepath.Match(GenFilesPattern + "*." + GenFilesExtension, fileinfo.Name())

if matched {
lines, err := scanLines(path)
Expand All @@ -75,7 +77,7 @@ func (step *stepGenFiles) Run(ctx context.Context, state multistep.StateBag) mul
return err
}

newfile, err := os.OpenFile(strings.TrimSuffix(path, ".pkr.in"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
newfile, err := os.OpenFile(strings.TrimSuffix(path, "." + GenFilesExtension), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
state.Put("error", fmt.Errorf("Error writing output file: %s", err))
return err
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ go 1.13
require (
github.com/hashicorp/hcl/v2 v2.3.0
github.com/hashicorp/packer v1.5.5
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.8.1
github.com/zclconf/go-cty v1.3.2-0.20200309235747-0b5d9cf50df7
)
Loading

0 comments on commit cfdd920

Please sign in to comment.