From 5493285f65c73b879973a6b18c7db1d35b693198 Mon Sep 17 00:00:00 2001 From: ntoofu Date: Sun, 2 Aug 2020 15:02:31 +0900 Subject: [PATCH 1/2] Add `directory_permission` config option to `vsphere` builder --- builder/vsphere/common/output_config.go | 10 +++++++ .../vsphere/common/output_config.hcl2spec.go | 8 ++++-- builder/vsphere/common/step_export.go | 2 +- .../vsphere/common/step_export.hcl2spec.go | 28 +++++++++++-------- .../common/OutputConfig-not-required.mdx | 7 +++++ 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/builder/vsphere/common/output_config.go b/builder/vsphere/common/output_config.go index 5708f7f9e..a1d2f8ca8 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 permission of the directories newly created to store artifacts, + // possibly including the directory speified by "output_directory" and its + // ancestor directories. 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 789927c94..114504071 100644 --- a/builder/vsphere/common/step_export.go +++ b/builder/vsphere/common/step_export.go @@ -105,7 +105,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..1149f2d7b 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 permission of the directories newly created to store artifacts, + possibly including the directory speified by "output_directory" and its + ancestor directories. 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. From a53e81d9a276be1fdb69266b8b31e19728a6d346 Mon Sep 17 00:00:00 2001 From: ntoofu Date: Thu, 6 Aug 2020 01:11:02 +0900 Subject: [PATCH 2/2] Make docs more clear https://github.com/hashicorp/packer/pull/9704#discussion_r465393240 --- builder/vsphere/common/output_config.go | 12 ++++++------ .../vsphere/common/OutputConfig-not-required.mdx | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/builder/vsphere/common/output_config.go b/builder/vsphere/common/output_config.go index a1d2f8ca8..8692a507a 100644 --- a/builder/vsphere/common/output_config.go +++ b/builder/vsphere/common/output_config.go @@ -21,12 +21,12 @@ 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 permission of the directories newly created to store artifacts, - // possibly including the directory speified by "output_directory" and its - // ancestor directories. 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. + // 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"` } 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 1149f2d7b..6e5476c97 100644 --- a/website/pages/partials/builder/vsphere/common/OutputConfig-not-required.mdx +++ b/website/pages/partials/builder/vsphere/common/OutputConfig-not-required.mdx @@ -8,9 +8,9 @@ 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 permission of the directories newly created to store artifacts, - possibly including the directory speified by "output_directory" and its - ancestor directories. 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. +- `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.