MCPcopy
hub / github.com/hashicorp/packer / PostProcess

Method PostProcess

post-processor/compress/post-processor.go:112–251  ·  view source on GitHub ↗
(
	ctx context.Context,
	ui packersdk.Ui,
	artifact packersdk.Artifact,
)

Source from the content-addressed store, hash-verified

110}
111
112func (p *PostProcessor) PostProcess(
113 ctx context.Context,
114 ui packersdk.Ui,
115 artifact packersdk.Artifact,
116) (packersdk.Artifact, bool, bool, error) {
117 var generatedData map[interface{}]interface{}
118 stateData := artifact.State("generated_data")
119 if stateData != nil {
120 // Make sure it's not a nil map so we can assign to it later.
121 generatedData = stateData.(map[interface{}]interface{})
122 }
123 // If stateData has a nil map generatedData will be nil
124 // and we need to make sure it's not
125 if generatedData == nil {
126 generatedData = make(map[interface{}]interface{})
127 }
128
129 // These are extra variables that will be made available for interpolation.
130 generatedData["BuildName"] = p.config.PackerBuildName
131 generatedData["BuilderType"] = p.config.PackerBuilderType
132 p.config.ctx.Data = generatedData
133
134 target, err := interpolate.Render(p.config.OutputPath, &p.config.ctx)
135 if err != nil {
136 return nil, false, false, fmt.Errorf("Error interpolating output value: %s", err)
137 } else {
138 fmt.Println(target)
139 }
140
141 newArtifact := &Artifact{Path: target}
142
143 if err = os.MkdirAll(filepath.Dir(target), os.FileMode(0755)); err != nil {
144 return nil, false, false, fmt.Errorf(
145 "Unable to create dir for archive %s: %s", target, err)
146 }
147 outputFile, err := os.Create(target)
148 if err != nil {
149 return nil, false, false, fmt.Errorf(
150 "Unable to create archive %s: %s", target, err)
151 }
152 defer outputFile.Close()
153
154 // Setup output interface. If we're using compression, output is a
155 // compression writer. Otherwise it's just a file.
156 var output io.WriteCloser
157 errTmpl := "error creating %s writer: %s"
158 switch p.config.Algorithm {
159 case "bgzf":
160 ui.Say(fmt.Sprintf("Using bgzf compression with %d cores for %s",
161 runtime.GOMAXPROCS(-1), target))
162 output, err = makeBGZFWriter(outputFile, p.config.CompressionLevel)
163 if err != nil {
164 return nil, false, false, fmt.Errorf(errTmpl, p.config.Algorithm, err)
165 }
166 defer output.Close()
167 case "bzip2":
168 ui.Say(fmt.Sprintf("Using bzip2 compression with 1 core for %s (library does not support MT)",
169 target))

Callers 1

testArchiveFunction · 0.95

Calls 14

makeBGZFWriterFunction · 0.85
makeBZIP2WriterFunction · 0.85
makeLZ4WriterFunction · 0.85
makeXZWriterFunction · 0.85
makePgzipWriterFunction · 0.85
createTarArchiveFunction · 0.85
createZipArchiveFunction · 0.85
DirMethod · 0.80
CopyMethod · 0.80
StateMethod · 0.45
ErrorfMethod · 0.45
CloseMethod · 0.45

Tested by 1

testArchiveFunction · 0.76