| 99 | } |
| 100 | |
| 101 | func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifact packersdk.Artifact) (packersdk.Artifact, bool, bool, error) { |
| 102 | files := artifact.Files() |
| 103 | var h hash.Hash |
| 104 | |
| 105 | var generatedData map[interface{}]interface{} |
| 106 | stateData := artifact.State("generated_data") |
| 107 | if stateData != nil { |
| 108 | // Make sure it's not a nil map so we can assign to it later. |
| 109 | generatedData = stateData.(map[interface{}]interface{}) |
| 110 | } |
| 111 | // If stateData has a nil map generatedData will be nil |
| 112 | // and we need to make sure it's not |
| 113 | if generatedData == nil { |
| 114 | generatedData = make(map[interface{}]interface{}) |
| 115 | } |
| 116 | generatedData["BuildName"] = p.config.PackerBuildName |
| 117 | generatedData["BuilderType"] = p.config.PackerBuilderType |
| 118 | |
| 119 | newartifact := NewArtifact(artifact.Files()) |
| 120 | |
| 121 | for _, ct := range p.config.ChecksumTypes { |
| 122 | h = getHash(ct) |
| 123 | generatedData["ChecksumType"] = ct |
| 124 | p.config.ctx.Data = generatedData |
| 125 | |
| 126 | for _, art := range files { |
| 127 | checksumFile, err := interpolate.Render(p.config.OutputPath, &p.config.ctx) |
| 128 | if err != nil { |
| 129 | return nil, false, true, err |
| 130 | } |
| 131 | |
| 132 | if _, err := os.Stat(checksumFile); err != nil { |
| 133 | newartifact.files = append(newartifact.files, checksumFile) |
| 134 | } |
| 135 | if err := os.MkdirAll(filepath.Dir(checksumFile), os.FileMode(0755)); err != nil { |
| 136 | return nil, false, true, fmt.Errorf("unable to create dir: %s", err.Error()) |
| 137 | } |
| 138 | fw, err := os.OpenFile(checksumFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(0644)) |
| 139 | if err != nil { |
| 140 | return nil, false, true, fmt.Errorf("unable to create file %s: %s", checksumFile, err.Error()) |
| 141 | } |
| 142 | fr, err := os.Open(art) |
| 143 | if err != nil { |
| 144 | fw.Close() |
| 145 | return nil, false, true, fmt.Errorf("unable to open file %s: %s", art, err.Error()) |
| 146 | } |
| 147 | |
| 148 | if _, err = io.Copy(h, fr); err != nil { |
| 149 | fr.Close() |
| 150 | fw.Close() |
| 151 | return nil, false, true, fmt.Errorf("unable to compute %s hash for %s", ct, art) |
| 152 | } |
| 153 | fr.Close() |
| 154 | _, _ = fw.WriteString(fmt.Sprintf("%x\t%s\n", h.Sum(nil), filepath.Base(art))) |
| 155 | fw.Close() |
| 156 | h.Reset() |
| 157 | } |
| 158 | } |