diff --git a/builder/vsphere/common/output_config.go b/builder/vsphere/common/output_config.go index 5708f7f9e..8692a507a 100644 --- a/builder/vsphere/common/output_config.go +++ b/builder/vsphere/common/output_config.go @@ -21,12 +21,22 @@ type OutputConfig struct { // created, must be empty prior to running the builder. By default this is // "output-BUILDNAME" where "BUILDNAME" is the name of the build. 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 { if c.OutputDir == "" { c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName) } + if c.DirPerm == 0 { + c.DirPerm = 0750 + } return nil } diff --git a/builder/vsphere/common/output_config.hcl2spec.go b/builder/vsphere/common/output_config.hcl2spec.go index 4839de60e..1ad51412c 100644 --- a/builder/vsphere/common/output_config.hcl2spec.go +++ b/builder/vsphere/common/output_config.hcl2spec.go @@ -2,6 +2,8 @@ package common import ( + "os" + "github.com/hashicorp/hcl/v2/hcldec" "github.com/zclconf/go-cty/cty" ) @@ -9,7 +11,8 @@ import ( // FlatOutputConfig is an auto-generated flat version of OutputConfig. // Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. 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. @@ -24,7 +27,8 @@ func (*OutputConfig) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec. // The decoded values from this spec will then be applied to a FlatOutputConfig. func (*FlatOutputConfig) HCL2Spec() 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 } diff --git a/builder/vsphere/common/step_export.go b/builder/vsphere/common/step_export.go index ee132c752..8fb49adde 100644 --- a/builder/vsphere/common/step_export.go +++ b/builder/vsphere/common/step_export.go @@ -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")) } diff --git a/builder/vsphere/common/step_export.hcl2spec.go b/builder/vsphere/common/step_export.hcl2spec.go index 71a4283c8..e3cbd0169 100644 --- a/builder/vsphere/common/step_export.hcl2spec.go +++ b/builder/vsphere/common/step_export.hcl2spec.go @@ -2,6 +2,8 @@ package common import ( + "os" + "github.com/hashicorp/hcl/v2/hcldec" "github.com/zclconf/go-cty/cty" ) @@ -9,12 +11,13 @@ import ( // FlatExportConfig is an auto-generated flat version of ExportConfig. // Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. type FlatExportConfig struct { - Name *string `mapstructure:"name" cty:"name" hcl:"name"` - Force *bool `mapstructure:"force" cty:"force" hcl:"force"` - Images *bool `mapstructure:"images" cty:"images" hcl:"images"` - Manifest *string `mapstructure:"manifest" cty:"manifest" hcl:"manifest"` - OutputDir *string `mapstructure:"output_directory" required:"false" cty:"output_directory" hcl:"output_directory"` - Options []string `mapstructure:"options" cty:"options" hcl:"options"` + Name *string `mapstructure:"name" cty:"name" hcl:"name"` + Force *bool `mapstructure:"force" cty:"force" hcl:"force"` + Images *bool `mapstructure:"images" cty:"images" hcl:"images"` + Manifest *string `mapstructure:"manifest" cty:"manifest" hcl:"manifest"` + 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"` } // FlatMapstructure returns a new FlatExportConfig. @@ -29,12 +32,13 @@ func (*ExportConfig) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec. // The decoded values from this spec will then be applied to a FlatExportConfig. func (*FlatExportConfig) HCL2Spec() map[string]hcldec.Spec { s := map[string]hcldec.Spec{ - "name": &hcldec.AttrSpec{Name: "name", Type: cty.String, Required: false}, - "force": &hcldec.AttrSpec{Name: "force", 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}, - "output_directory": &hcldec.AttrSpec{Name: "output_directory", Type: cty.String, Required: false}, - "options": &hcldec.AttrSpec{Name: "options", Type: cty.List(cty.String), Required: false}, + "name": &hcldec.AttrSpec{Name: "name", Type: cty.String, Required: false}, + "force": &hcldec.AttrSpec{Name: "force", 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}, + "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}, } return s } diff --git a/website/pages/partials/builder/vsphere/common/OutputConfig-not-required.mdx b/website/pages/partials/builder/vsphere/common/OutputConfig-not-required.mdx index 4df058530..6e5476c97 100644 --- a/website/pages/partials/builder/vsphere/common/OutputConfig-not-required.mdx +++ b/website/pages/partials/builder/vsphere/common/OutputConfig-not-required.mdx @@ -7,3 +7,10 @@ packer is executed from. This directory must not exist or, if created, must be empty prior to running the builder. By default this is "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.