Merge pull request #9704 from ntoofu/f-vsphere-export-permission

Add `directory_permission` config option to `vsphere` builder
This commit is contained in:
Megan Marsh 2020-08-05 10:57:30 -07:00 committed by GitHub
commit 3f61498704
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 15 deletions

View File

@ -21,12 +21,22 @@ type OutputConfig struct {
// created, must be empty prior to running the builder. By default this is // created, must be empty prior to running the builder. By default this is
// "output-BUILDNAME" where "BUILDNAME" is the name of the build. // "output-BUILDNAME" where "BUILDNAME" is the name of the build.
OutputDir string `mapstructure:"output_directory" required:"false"` OutputDir string `mapstructure:"output_directory" required:"false"`
// The permissions to apply to the "output_directory", and to any parent
// directories that get created for output_directory. By default this is
// "0750". You should express the permission as quoted string with a
// leading zero such as "0755" in JSON file, because JSON does not support
// octal value. In Unix-like OS, the actual permission may differ from
// this value because of umask.
DirPerm os.FileMode `mapstructure:"directory_permission" required:"false"`
} }
func (c *OutputConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) []error { func (c *OutputConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) []error {
if c.OutputDir == "" { if c.OutputDir == "" {
c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName) c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName)
} }
if c.DirPerm == 0 {
c.DirPerm = 0750
}
return nil return nil
} }

View File

@ -2,6 +2,8 @@
package common package common
import ( import (
"os"
"github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/hcl/v2/hcldec"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
) )
@ -10,6 +12,7 @@ import (
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. // Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
type FlatOutputConfig struct { type FlatOutputConfig struct {
OutputDir *string `mapstructure:"output_directory" required:"false" cty:"output_directory" hcl:"output_directory"` OutputDir *string `mapstructure:"output_directory" required:"false" cty:"output_directory" hcl:"output_directory"`
DirPerm *os.FileMode `mapstructure:"directory_permission" required:"false" cty:"directory_permission" hcl:"directory_permission"`
} }
// FlatMapstructure returns a new FlatOutputConfig. // FlatMapstructure returns a new FlatOutputConfig.
@ -25,6 +28,7 @@ func (*OutputConfig) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.
func (*FlatOutputConfig) HCL2Spec() map[string]hcldec.Spec { func (*FlatOutputConfig) HCL2Spec() map[string]hcldec.Spec {
s := map[string]hcldec.Spec{ s := map[string]hcldec.Spec{
"output_directory": &hcldec.AttrSpec{Name: "output_directory", Type: cty.String, Required: false}, "output_directory": &hcldec.AttrSpec{Name: "output_directory", Type: cty.String, Required: false},
"directory_permission": &hcldec.AttrSpec{Name: "directory_permission", Type: cty.Number, Required: false},
} }
return s return s
} }

View File

@ -124,7 +124,7 @@ func (c *ExportConfig) Prepare(ctx *interpolate.Context, lc *LocationConfig, pc
} }
} }
if err := os.MkdirAll(c.OutputDir.OutputDir, 0750); err != nil { if err := os.MkdirAll(c.OutputDir.OutputDir, c.OutputDir.DirPerm); err != nil {
errs = packer.MultiErrorAppend(errs, errors.Wrap(err, "unable to make directory for export")) errs = packer.MultiErrorAppend(errs, errors.Wrap(err, "unable to make directory for export"))
} }

View File

@ -2,6 +2,8 @@
package common package common
import ( import (
"os"
"github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/hcl/v2/hcldec"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
) )
@ -14,6 +16,7 @@ type FlatExportConfig struct {
Images *bool `mapstructure:"images" cty:"images" hcl:"images"` Images *bool `mapstructure:"images" cty:"images" hcl:"images"`
Manifest *string `mapstructure:"manifest" cty:"manifest" hcl:"manifest"` Manifest *string `mapstructure:"manifest" cty:"manifest" hcl:"manifest"`
OutputDir *string `mapstructure:"output_directory" required:"false" cty:"output_directory" hcl:"output_directory"` OutputDir *string `mapstructure:"output_directory" required:"false" cty:"output_directory" hcl:"output_directory"`
DirPerm *os.FileMode `mapstructure:"directory_permission" required:"false" cty:"directory_permission" hcl:"directory_permission"`
Options []string `mapstructure:"options" cty:"options" hcl:"options"` Options []string `mapstructure:"options" cty:"options" hcl:"options"`
} }
@ -34,6 +37,7 @@ func (*FlatExportConfig) HCL2Spec() map[string]hcldec.Spec {
"images": &hcldec.AttrSpec{Name: "images", Type: cty.Bool, Required: false}, "images": &hcldec.AttrSpec{Name: "images", Type: cty.Bool, Required: false},
"manifest": &hcldec.AttrSpec{Name: "manifest", Type: cty.String, Required: false}, "manifest": &hcldec.AttrSpec{Name: "manifest", Type: cty.String, Required: false},
"output_directory": &hcldec.AttrSpec{Name: "output_directory", Type: cty.String, Required: false}, "output_directory": &hcldec.AttrSpec{Name: "output_directory", Type: cty.String, Required: false},
"directory_permission": &hcldec.AttrSpec{Name: "directory_permission", Type: cty.Number, Required: false},
"options": &hcldec.AttrSpec{Name: "options", Type: cty.List(cty.String), Required: false}, "options": &hcldec.AttrSpec{Name: "options", Type: cty.List(cty.String), Required: false},
} }
return s return s

View File

@ -7,3 +7,10 @@
packer is executed from. This directory must not exist or, if packer is executed from. This directory must not exist or, if
created, must be empty prior to running the builder. By default this is created, must be empty prior to running the builder. By default this is
"output-BUILDNAME" where "BUILDNAME" is the name of the build. "output-BUILDNAME" where "BUILDNAME" is the name of the build.
- `directory_permission` (os.FileMode) - The permissions to apply to the "output_directory", and to any parent
directories that get created for output_directory. By default this is
"0750". You should express the permission as quoted string with a
leading zero such as "0755" in JSON file, because JSON does not support
octal value. In Unix-like OS, the actual permission may differ from
this value because of umask.